blob: 6d362e2d39607eae30f879e8245d75c204e225e7 [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 /*
145 * Return a pointer to the appropriate buffer in loc->xl_storage
146 * at the given offset from loc->xl_header.
147 */
148 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
149
Joel Becker69a3e532009-08-17 12:24:39 -0700150 /* Can we reuse the existing entry for the new value? */
151 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
152 struct ocfs2_xattr_info *xi);
153
154 /* How much space is needed for the new value? */
155 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
156 struct ocfs2_xattr_info *xi);
157
158 /*
159 * Return the offset of the first name+value pair. This is
160 * the start of our downward-filling free space.
161 */
162 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
163
Joel Becker11179f22009-08-14 16:07:44 -0700164 /*
165 * Remove the name+value at this location. Do whatever is
166 * appropriate with the remaining name+value pairs.
167 */
168 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
Joel Becker69a3e532009-08-17 12:24:39 -0700169
170 /* Fill xl_entry with a new entry */
171 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
172
173 /* Add name+value storage to an entry */
174 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
Joel Becker11179f22009-08-14 16:07:44 -0700175};
176
177/*
178 * Describes an xattr entry location. This is a memory structure
179 * tracking the on-disk structure.
180 */
181struct ocfs2_xa_loc {
182 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
183 struct ocfs2_xattr_header *xl_header;
184
185 /* Bytes from xl_header to the end of the storage */
186 int xl_size;
187
188 /*
189 * The ocfs2_xattr_entry this location describes. If this is
190 * NULL, this location describes the on-disk structure where it
191 * would have been.
192 */
193 struct ocfs2_xattr_entry *xl_entry;
194
195 /*
196 * Internal housekeeping
197 */
198
199 /* Buffer(s) containing this entry */
200 void *xl_storage;
201
202 /* Operations on the storage backing this location */
203 const struct ocfs2_xa_loc_operations *xl_ops;
204};
205
Joel Becker199799a2009-08-14 19:04:15 -0700206/*
207 * Convenience functions to calculate how much space is needed for a
208 * given name+value pair
209 */
210static int namevalue_size(int name_len, uint64_t value_len)
211{
212 if (value_len > OCFS2_XATTR_INLINE_SIZE)
213 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
214 else
215 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
216}
217
218static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
219{
220 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
221}
222
223static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
224{
225 u64 value_len = le64_to_cpu(xe->xe_value_size);
226
227 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
228 ocfs2_xattr_is_local(xe));
229 return namevalue_size(xe->xe_name_len, value_len);
230}
231
232
Tao Mafd68a892009-08-18 11:43:21 +0800233static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800234 struct ocfs2_xattr_header *xh,
235 int index,
236 int *block_off,
237 int *new_offset);
238
Joel Becker54f443f2008-10-20 18:43:07 -0700239static int ocfs2_xattr_block_find(struct inode *inode,
240 int name_index,
241 const char *name,
242 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800243static int ocfs2_xattr_index_block_find(struct inode *inode,
244 struct buffer_head *root_bh,
245 int name_index,
246 const char *name,
247 struct ocfs2_xattr_search *xs);
248
Tao Ma0c044f02008-08-18 17:38:50 +0800249static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
Tao Ma47bca492009-08-18 11:43:42 +0800250 struct buffer_head *blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800251 char *buffer,
252 size_t buffer_size);
253
Tao Ma01225592008-08-18 17:38:53 +0800254static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800255 struct ocfs2_xattr_search *xs,
256 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800257
258static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
259 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800260 struct ocfs2_xattr_search *xs,
261 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800262
Tao Ma47bca492009-08-18 11:43:42 +0800263typedef int (xattr_tree_rec_func)(struct inode *inode,
264 struct buffer_head *root_bh,
265 u64 blkno, u32 cpos, u32 len, void *para);
266static int ocfs2_iterate_xattr_index_block(struct inode *inode,
267 struct buffer_head *root_bh,
268 xattr_tree_rec_func *rec_func,
269 void *para);
270static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
271 struct ocfs2_xattr_bucket *bucket,
272 void *para);
273static int ocfs2_rm_xattr_cluster(struct inode *inode,
274 struct buffer_head *root_bh,
275 u64 blkno,
276 u32 cpos,
277 u32 len,
278 void *para);
279
Joel Beckerc58b6032008-11-26 13:36:24 -0800280static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
281 u64 src_blk, u64 last_blk, u64 to_blk,
282 unsigned int start_bucket,
283 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800284static int ocfs2_prepare_refcount_xattr(struct inode *inode,
285 struct ocfs2_dinode *di,
286 struct ocfs2_xattr_info *xi,
287 struct ocfs2_xattr_search *xis,
288 struct ocfs2_xattr_search *xbs,
289 struct ocfs2_refcount_tree **ref_tree,
290 int *meta_need,
291 int *credits);
Tao Mace9c5a52009-08-18 11:43:59 +0800292static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
293 struct ocfs2_xattr_bucket *bucket,
294 int offset,
295 struct ocfs2_xattr_value_root **xv,
296 struct buffer_head **bh);
Tao Maa3944252008-08-18 17:38:54 +0800297
Tiger Yang0030e002008-10-23 16:33:33 +0800298static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
299{
300 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
301}
302
303static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
304{
305 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
306}
307
308static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
309{
310 u16 len = sb->s_blocksize -
311 offsetof(struct ocfs2_xattr_header, xh_entries);
312
313 return len / sizeof(struct ocfs2_xattr_entry);
314}
315
Joel Becker9c7759a2008-10-24 16:21:03 -0700316#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700317#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700318#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700319
Joel Beckerba937122008-10-24 19:13:20 -0700320static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700321{
Joel Beckerba937122008-10-24 19:13:20 -0700322 struct ocfs2_xattr_bucket *bucket;
323 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700324
Joel Beckerba937122008-10-24 19:13:20 -0700325 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
326
327 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
328 if (bucket) {
329 bucket->bu_inode = inode;
330 bucket->bu_blocks = blks;
331 }
332
333 return bucket;
334}
335
336static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
337{
338 int i;
339
340 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700341 brelse(bucket->bu_bhs[i]);
342 bucket->bu_bhs[i] = NULL;
343 }
344}
345
Joel Beckerba937122008-10-24 19:13:20 -0700346static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
347{
348 if (bucket) {
349 ocfs2_xattr_bucket_relse(bucket);
350 bucket->bu_inode = NULL;
351 kfree(bucket);
352 }
353}
354
Joel Becker784b8162008-10-24 17:33:40 -0700355/*
356 * A bucket that has never been written to disk doesn't need to be
357 * read. We just need the buffer_heads. Don't call this for
358 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
359 * them fully.
360 */
Joel Beckerba937122008-10-24 19:13:20 -0700361static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700362 u64 xb_blkno)
363{
364 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700365
Joel Beckerba937122008-10-24 19:13:20 -0700366 for (i = 0; i < bucket->bu_blocks; i++) {
367 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
368 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700369 if (!bucket->bu_bhs[i]) {
370 rc = -EIO;
371 mlog_errno(rc);
372 break;
373 }
374
Joel Becker8cb471e2009-02-10 20:00:41 -0800375 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800376 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800377 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800378 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700379 }
380
381 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700382 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700383 return rc;
384}
385
386/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700387static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700388 u64 xb_blkno)
389{
Joel Beckerba937122008-10-24 19:13:20 -0700390 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700391
Joel Becker8cb471e2009-02-10 20:00:41 -0800392 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800393 bucket->bu_blocks, bucket->bu_bhs, 0,
394 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800395 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800396 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800397 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
398 bucket->bu_bhs,
399 bucket->bu_blocks,
400 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800401 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800402 if (rc)
403 mlog_errno(rc);
404 }
405
Joel Becker784b8162008-10-24 17:33:40 -0700406 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700407 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700408 return rc;
409}
410
Joel Becker1224be02008-10-24 18:47:33 -0700411static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700412 struct ocfs2_xattr_bucket *bucket,
413 int type)
414{
415 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700416
Joel Beckerba937122008-10-24 19:13:20 -0700417 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800418 rc = ocfs2_journal_access(handle,
419 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700420 bucket->bu_bhs[i], type);
421 if (rc) {
422 mlog_errno(rc);
423 break;
424 }
425 }
426
427 return rc;
428}
429
430static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700431 struct ocfs2_xattr_bucket *bucket)
432{
Joel Beckerba937122008-10-24 19:13:20 -0700433 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700434
Tao Mac8b9cf92009-02-24 17:40:26 -0800435 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800436 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
437 bucket->bu_bhs, bucket->bu_blocks,
438 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800439 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800440
Joel Beckerba937122008-10-24 19:13:20 -0700441 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700442 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
443}
444
Joel Beckerba937122008-10-24 19:13:20 -0700445static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700446 struct ocfs2_xattr_bucket *src)
447{
448 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700449 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700450
Joel Beckerba937122008-10-24 19:13:20 -0700451 BUG_ON(dest->bu_blocks != src->bu_blocks);
452 BUG_ON(dest->bu_inode != src->bu_inode);
453
454 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700455 memcpy(bucket_block(dest, i), bucket_block(src, i),
456 blocksize);
457 }
458}
Joel Becker1224be02008-10-24 18:47:33 -0700459
Joel Becker4ae1d692008-11-13 14:49:18 -0800460static int ocfs2_validate_xattr_block(struct super_block *sb,
461 struct buffer_head *bh)
462{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700463 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800464 struct ocfs2_xattr_block *xb =
465 (struct ocfs2_xattr_block *)bh->b_data;
466
467 mlog(0, "Validating xattr block %llu\n",
468 (unsigned long long)bh->b_blocknr);
469
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700470 BUG_ON(!buffer_uptodate(bh));
471
472 /*
473 * If the ecc fails, we return the error but otherwise
474 * leave the filesystem running. We know any error is
475 * local to this block.
476 */
477 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
478 if (rc)
479 return rc;
480
481 /*
482 * Errors after here are fatal
483 */
484
Joel Becker4ae1d692008-11-13 14:49:18 -0800485 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
486 ocfs2_error(sb,
487 "Extended attribute block #%llu has bad "
488 "signature %.*s",
489 (unsigned long long)bh->b_blocknr, 7,
490 xb->xb_signature);
491 return -EINVAL;
492 }
493
494 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
495 ocfs2_error(sb,
496 "Extended attribute block #%llu has an "
497 "invalid xb_blkno of %llu",
498 (unsigned long long)bh->b_blocknr,
499 (unsigned long long)le64_to_cpu(xb->xb_blkno));
500 return -EINVAL;
501 }
502
503 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
504 ocfs2_error(sb,
505 "Extended attribute block #%llu has an invalid "
506 "xb_fs_generation of #%u",
507 (unsigned long long)bh->b_blocknr,
508 le32_to_cpu(xb->xb_fs_generation));
509 return -EINVAL;
510 }
511
512 return 0;
513}
514
515static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
516 struct buffer_head **bh)
517{
518 int rc;
519 struct buffer_head *tmp = *bh;
520
Joel Becker8cb471e2009-02-10 20:00:41 -0800521 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800522 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800523
524 /* If ocfs2_read_block() got us a new bh, pass it up. */
525 if (!rc && !*bh)
526 *bh = tmp;
527
528 return rc;
529}
530
Tao Ma936b8832008-10-09 23:06:14 +0800531static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800532{
533 struct xattr_handler *handler = NULL;
534
535 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
536 handler = ocfs2_xattr_handler_map[name_index];
537
Tao Ma936b8832008-10-09 23:06:14 +0800538 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800539}
540
Mark Fasheh40daa162008-10-07 14:31:42 -0700541static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800542 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700543 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800544{
545 /* Get hash value of uuid from super block */
546 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
547 int i;
548
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800549 /* hash extended attribute name */
550 for (i = 0; i < name_len; i++) {
551 hash = (hash << OCFS2_HASH_SHIFT) ^
552 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
553 *name++;
554 }
555
556 return hash;
557}
558
559/*
560 * ocfs2_xattr_hash_entry()
561 *
562 * Compute the hash of an extended attribute.
563 */
564static void ocfs2_xattr_hash_entry(struct inode *inode,
565 struct ocfs2_xattr_header *header,
566 struct ocfs2_xattr_entry *entry)
567{
568 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800569 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800570
Tao Ma2057e5c2008-10-09 23:06:13 +0800571 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800572 entry->xe_name_hash = cpu_to_le32(hash);
573
574 return;
575}
Tao Maf56654c2008-08-18 17:38:48 +0800576
Tiger Yang534eadd2008-11-14 11:16:41 +0800577static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
578{
Joel Becker199799a2009-08-14 19:04:15 -0700579 return namevalue_size(name_len, value_len) +
580 sizeof(struct ocfs2_xattr_entry);
581}
Tiger Yang534eadd2008-11-14 11:16:41 +0800582
Joel Becker199799a2009-08-14 19:04:15 -0700583static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
584{
585 return namevalue_size_xi(xi) +
586 sizeof(struct ocfs2_xattr_entry);
587}
Tiger Yang534eadd2008-11-14 11:16:41 +0800588
Joel Becker199799a2009-08-14 19:04:15 -0700589static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
590{
591 return namevalue_size_xe(xe) +
592 sizeof(struct ocfs2_xattr_entry);
Tiger Yang534eadd2008-11-14 11:16:41 +0800593}
594
595int ocfs2_calc_security_init(struct inode *dir,
596 struct ocfs2_security_xattr_info *si,
597 int *want_clusters,
598 int *xattr_credits,
599 struct ocfs2_alloc_context **xattr_ac)
600{
601 int ret = 0;
602 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
603 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
604 si->value_len);
605
606 /*
607 * The max space of security xattr taken inline is
608 * 256(name) + 80(value) + 16(entry) = 352 bytes,
609 * So reserve one metadata block for it is ok.
610 */
611 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
612 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
613 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
614 if (ret) {
615 mlog_errno(ret);
616 return ret;
617 }
618 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
619 }
620
621 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800622 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
623 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
624 si->value_len);
625
626 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
627 new_clusters);
628 *want_clusters += new_clusters;
629 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800630 return ret;
631}
632
Tiger Yang89c38bd2008-11-14 11:17:41 +0800633int ocfs2_calc_xattr_init(struct inode *dir,
634 struct buffer_head *dir_bh,
635 int mode,
636 struct ocfs2_security_xattr_info *si,
637 int *want_clusters,
638 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800639 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800640{
641 int ret = 0;
642 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800643 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800644
645 if (si->enable)
646 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
647 si->value_len);
648
649 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
650 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
651 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
652 "", NULL, 0);
653 if (acl_len > 0) {
654 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
655 if (S_ISDIR(mode))
656 a_size <<= 1;
657 } else if (acl_len != 0 && acl_len != -ENODATA) {
658 mlog_errno(ret);
659 return ret;
660 }
661 }
662
663 if (!(s_size + a_size))
664 return ret;
665
666 /*
667 * The max space of security xattr taken inline is
668 * 256(name) + 80(value) + 16(entry) = 352 bytes,
669 * The max space of acl xattr taken inline is
670 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
671 * when blocksize = 512, may reserve one more cluser for
672 * xattr bucket, otherwise reserve one metadata block
673 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800674 * If this is a new directory with inline data,
675 * we choose to reserve the entire inline area for
676 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800677 */
678 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800679 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800680 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800681 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800682 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
683 }
684
685 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
686 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
687 *want_clusters += 1;
688 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
689 }
690
Tiger Yang0e445b62008-12-09 16:42:51 +0800691 /*
692 * reserve credits and clusters for xattrs which has large value
693 * and have to be set outside
694 */
695 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
696 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
697 si->value_len);
698 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
699 new_clusters);
700 *want_clusters += new_clusters;
701 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800702 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
703 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800704 /* for directory, it has DEFAULT and ACCESS two types of acls */
705 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
706 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
707 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
708 new_clusters);
709 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800710 }
711
712 return ret;
713}
714
Tao Maf56654c2008-08-18 17:38:48 +0800715static int ocfs2_xattr_extend_allocation(struct inode *inode,
716 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800717 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800718 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800719{
720 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800721 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800722 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800723 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700724 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800725
726 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
727
Joel Becker5e404e92009-02-13 03:54:22 -0800728 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700729
Joel Becker0cf2f762009-02-12 16:41:25 -0800730 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800731 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800732 if (status < 0) {
733 mlog_errno(status);
734 goto leave;
735 }
736
Joel Becker19b801f2008-12-09 14:36:50 -0800737 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800738 status = ocfs2_add_clusters_in_btree(handle,
739 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800740 &logical_start,
741 clusters_to_add,
742 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800743 ctxt->data_ac,
744 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700745 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800746 if (status < 0) {
747 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800748 goto leave;
749 }
750
Joel Becker19b801f2008-12-09 14:36:50 -0800751 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800752 if (status < 0) {
753 mlog_errno(status);
754 goto leave;
755 }
756
Joel Becker19b801f2008-12-09 14:36:50 -0800757 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800758
Tao Ma85db90e2008-11-12 08:27:01 +0800759 /*
760 * We should have already allocated enough space before the transaction,
761 * so no need to restart.
762 */
763 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800764
765leave:
Tao Maf56654c2008-08-18 17:38:48 +0800766
767 return status;
768}
769
770static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800771 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800772 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800773 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800774 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800775{
776 int ret;
777 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800778 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700779 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800780
Joel Becker5e404e92009-02-13 03:54:22 -0800781 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700782
Joel Becker0cf2f762009-02-12 16:41:25 -0800783 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800784 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800785 if (ret) {
786 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800787 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800788 }
789
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800790 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800791 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800792 if (ret) {
793 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800794 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800795 }
796
Joel Beckerd72cc722008-12-09 14:30:41 -0800797 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800798
Joel Beckerd72cc722008-12-09 14:30:41 -0800799 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800800 if (ret) {
801 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800802 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800803 }
804
Tao Ma492a8a32009-08-18 11:43:17 +0800805 if (ext_flags & OCFS2_EXT_REFCOUNTED)
806 ret = ocfs2_decrease_refcount(inode, handle,
807 ocfs2_blocks_to_clusters(inode->i_sb,
808 phys_blkno),
809 len, ctxt->meta_ac, &ctxt->dealloc, 1);
810 else
811 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
812 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800813 if (ret)
814 mlog_errno(ret);
815
Tao Maf56654c2008-08-18 17:38:48 +0800816out:
Tao Maf56654c2008-08-18 17:38:48 +0800817 return ret;
818}
819
820static int ocfs2_xattr_shrink_size(struct inode *inode,
821 u32 old_clusters,
822 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800823 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800824 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800825{
826 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800827 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800828 u32 trunc_len, cpos, phys_cpos, alloc_size;
829 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800830
831 if (old_clusters <= new_clusters)
832 return 0;
833
834 cpos = new_clusters;
835 trunc_len = old_clusters - new_clusters;
836 while (trunc_len) {
837 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800838 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800839 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800840 if (ret) {
841 mlog_errno(ret);
842 goto out;
843 }
844
845 if (alloc_size > trunc_len)
846 alloc_size = trunc_len;
847
Joel Becker19b801f2008-12-09 14:36:50 -0800848 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800849 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800850 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800851 if (ret) {
852 mlog_errno(ret);
853 goto out;
854 }
855
856 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800857 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
858 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800859 cpos += alloc_size;
860 trunc_len -= alloc_size;
861 }
862
863out:
Tao Maf56654c2008-08-18 17:38:48 +0800864 return ret;
865}
866
867static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800868 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800869 int len,
870 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800871{
872 int ret;
873 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800874 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800875
876 if (new_clusters == old_clusters)
877 return 0;
878
879 if (new_clusters > old_clusters)
880 ret = ocfs2_xattr_extend_allocation(inode,
881 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800882 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800883 else
884 ret = ocfs2_xattr_shrink_size(inode,
885 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800886 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800887
888 return ret;
889}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800890
Tao Ma936b8832008-10-09 23:06:14 +0800891static int ocfs2_xattr_list_entry(char *buffer, size_t size,
892 size_t *result, const char *prefix,
893 const char *name, int name_len)
894{
895 char *p = buffer + *result;
896 int prefix_len = strlen(prefix);
897 int total_len = prefix_len + name_len + 1;
898
899 *result += total_len;
900
901 /* we are just looking for how big our buffer needs to be */
902 if (!size)
903 return 0;
904
905 if (*result > size)
906 return -ERANGE;
907
908 memcpy(p, prefix, prefix_len);
909 memcpy(p + prefix_len, name, name_len);
910 p[prefix_len + name_len] = '\0';
911
912 return 0;
913}
914
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800915static int ocfs2_xattr_list_entries(struct inode *inode,
916 struct ocfs2_xattr_header *header,
917 char *buffer, size_t buffer_size)
918{
Tao Ma936b8832008-10-09 23:06:14 +0800919 size_t result = 0;
920 int i, type, ret;
921 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800922
923 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
924 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800925 type = ocfs2_xattr_get_type(entry);
926 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800927
Tao Ma936b8832008-10-09 23:06:14 +0800928 if (prefix) {
929 name = (const char *)header +
930 le16_to_cpu(entry->xe_name_offset);
931
932 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
933 &result, prefix, name,
934 entry->xe_name_len);
935 if (ret)
936 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800937 }
938 }
939
Tao Ma936b8832008-10-09 23:06:14 +0800940 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800941}
942
Tao Ma8b2c0db2009-08-18 11:43:49 +0800943int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
944 struct ocfs2_dinode *di)
945{
946 struct ocfs2_xattr_header *xh;
947 int i;
948
949 xh = (struct ocfs2_xattr_header *)
950 ((void *)di + inode->i_sb->s_blocksize -
951 le16_to_cpu(di->i_xattr_inline_size));
952
953 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
954 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
955 return 1;
956
957 return 0;
958}
959
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800960static int ocfs2_xattr_ibody_list(struct inode *inode,
961 struct ocfs2_dinode *di,
962 char *buffer,
963 size_t buffer_size)
964{
965 struct ocfs2_xattr_header *header = NULL;
966 struct ocfs2_inode_info *oi = OCFS2_I(inode);
967 int ret = 0;
968
969 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
970 return ret;
971
972 header = (struct ocfs2_xattr_header *)
973 ((void *)di + inode->i_sb->s_blocksize -
974 le16_to_cpu(di->i_xattr_inline_size));
975
976 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
977
978 return ret;
979}
980
981static int ocfs2_xattr_block_list(struct inode *inode,
982 struct ocfs2_dinode *di,
983 char *buffer,
984 size_t buffer_size)
985{
986 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800987 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800988 int ret = 0;
989
990 if (!di->i_xattr_loc)
991 return ret;
992
Joel Becker4ae1d692008-11-13 14:49:18 -0800993 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
994 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800995 if (ret < 0) {
996 mlog_errno(ret);
997 return ret;
998 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800999
Tao Ma0c044f02008-08-18 17:38:50 +08001000 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +08001001 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1002 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1003 ret = ocfs2_xattr_list_entries(inode, header,
1004 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +08001005 } else
1006 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +08001007 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -08001008
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001009 brelse(blk_bh);
1010
1011 return ret;
1012}
1013
1014ssize_t ocfs2_listxattr(struct dentry *dentry,
1015 char *buffer,
1016 size_t size)
1017{
1018 int ret = 0, i_ret = 0, b_ret = 0;
1019 struct buffer_head *di_bh = NULL;
1020 struct ocfs2_dinode *di = NULL;
1021 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1022
Tiger Yang8154da32008-08-18 17:11:46 +08001023 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1024 return -EOPNOTSUPP;
1025
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001026 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1027 return ret;
1028
1029 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1030 if (ret < 0) {
1031 mlog_errno(ret);
1032 return ret;
1033 }
1034
1035 di = (struct ocfs2_dinode *)di_bh->b_data;
1036
1037 down_read(&oi->ip_xattr_sem);
1038 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1039 if (i_ret < 0)
1040 b_ret = 0;
1041 else {
1042 if (buffer) {
1043 buffer += i_ret;
1044 size -= i_ret;
1045 }
1046 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1047 buffer, size);
1048 if (b_ret < 0)
1049 i_ret = 0;
1050 }
1051 up_read(&oi->ip_xattr_sem);
1052 ocfs2_inode_unlock(dentry->d_inode, 0);
1053
1054 brelse(di_bh);
1055
1056 return i_ret + b_ret;
1057}
1058
1059static int ocfs2_xattr_find_entry(int name_index,
1060 const char *name,
1061 struct ocfs2_xattr_search *xs)
1062{
1063 struct ocfs2_xattr_entry *entry;
1064 size_t name_len;
1065 int i, cmp = 1;
1066
1067 if (name == NULL)
1068 return -EINVAL;
1069
1070 name_len = strlen(name);
1071 entry = xs->here;
1072 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1073 cmp = name_index - ocfs2_xattr_get_type(entry);
1074 if (!cmp)
1075 cmp = name_len - entry->xe_name_len;
1076 if (!cmp)
1077 cmp = memcmp(name, (xs->base +
1078 le16_to_cpu(entry->xe_name_offset)),
1079 name_len);
1080 if (cmp == 0)
1081 break;
1082 entry += 1;
1083 }
1084 xs->here = entry;
1085
1086 return cmp ? -ENODATA : 0;
1087}
1088
1089static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +08001090 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001091 void *buffer,
1092 size_t len)
1093{
1094 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1095 u64 blkno;
1096 int i, ret = 0;
1097 size_t cplen, blocksize;
1098 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001099 struct ocfs2_extent_list *el;
1100
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001101 el = &xv->xr_list;
1102 clusters = le32_to_cpu(xv->xr_clusters);
1103 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1104 blocksize = inode->i_sb->s_blocksize;
1105
1106 cpos = 0;
1107 while (cpos < clusters) {
1108 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001109 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001110 if (ret) {
1111 mlog_errno(ret);
1112 goto out;
1113 }
1114
1115 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1116 /* Copy ocfs2_xattr_value */
1117 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001118 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1119 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001120 if (ret) {
1121 mlog_errno(ret);
1122 goto out;
1123 }
1124
1125 cplen = len >= blocksize ? blocksize : len;
1126 memcpy(buffer, bh->b_data, cplen);
1127 len -= cplen;
1128 buffer += cplen;
1129
1130 brelse(bh);
1131 bh = NULL;
1132 if (len == 0)
1133 break;
1134 }
1135 cpos += num_clusters;
1136 }
1137out:
1138 return ret;
1139}
1140
1141static int ocfs2_xattr_ibody_get(struct inode *inode,
1142 int name_index,
1143 const char *name,
1144 void *buffer,
1145 size_t buffer_size,
1146 struct ocfs2_xattr_search *xs)
1147{
1148 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1149 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001150 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001151 size_t size;
1152 int ret = 0;
1153
1154 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1155 return -ENODATA;
1156
1157 xs->end = (void *)di + inode->i_sb->s_blocksize;
1158 xs->header = (struct ocfs2_xattr_header *)
1159 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1160 xs->base = (void *)xs->header;
1161 xs->here = xs->header->xh_entries;
1162
1163 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1164 if (ret)
1165 return ret;
1166 size = le64_to_cpu(xs->here->xe_value_size);
1167 if (buffer) {
1168 if (size > buffer_size)
1169 return -ERANGE;
1170 if (ocfs2_xattr_is_local(xs->here)) {
1171 memcpy(buffer, (void *)xs->base +
1172 le16_to_cpu(xs->here->xe_name_offset) +
1173 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1174 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001175 xv = (struct ocfs2_xattr_value_root *)
1176 (xs->base + le16_to_cpu(
1177 xs->here->xe_name_offset) +
1178 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1179 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001180 buffer, size);
1181 if (ret < 0) {
1182 mlog_errno(ret);
1183 return ret;
1184 }
1185 }
1186 }
1187
1188 return size;
1189}
1190
1191static int ocfs2_xattr_block_get(struct inode *inode,
1192 int name_index,
1193 const char *name,
1194 void *buffer,
1195 size_t buffer_size,
1196 struct ocfs2_xattr_search *xs)
1197{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001198 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001199 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001200 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301201 int ret = -ENODATA, name_offset, name_len, i;
1202 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001203
Joel Beckerba937122008-10-24 19:13:20 -07001204 xs->bucket = ocfs2_xattr_bucket_new(inode);
1205 if (!xs->bucket) {
1206 ret = -ENOMEM;
1207 mlog_errno(ret);
1208 goto cleanup;
1209 }
Tao Ma589dc262008-08-18 17:38:51 +08001210
Joel Becker54f443f2008-10-20 18:43:07 -07001211 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1212 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001213 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001214 goto cleanup;
1215 }
1216
Tiger Yang6c1e1832008-11-02 19:04:21 +08001217 if (xs->not_found) {
1218 ret = -ENODATA;
1219 goto cleanup;
1220 }
1221
Joel Becker54f443f2008-10-20 18:43:07 -07001222 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001223 size = le64_to_cpu(xs->here->xe_value_size);
1224 if (buffer) {
1225 ret = -ERANGE;
1226 if (size > buffer_size)
1227 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001228
1229 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1230 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1231 i = xs->here - xs->header->xh_entries;
1232
1233 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001234 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001235 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001236 i,
1237 &block_off,
1238 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001239 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001240 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001241 if (ocfs2_xattr_is_local(xs->here)) {
1242 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001243 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001244 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001245 xv = (struct ocfs2_xattr_value_root *)
1246 (xs->base + name_offset + name_len);
1247 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001248 buffer, size);
1249 if (ret < 0) {
1250 mlog_errno(ret);
1251 goto cleanup;
1252 }
1253 }
1254 }
1255 ret = size;
1256cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001257 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001258
Joel Becker54f443f2008-10-20 18:43:07 -07001259 brelse(xs->xattr_bh);
1260 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001261 return ret;
1262}
1263
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001264int ocfs2_xattr_get_nolock(struct inode *inode,
1265 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001266 int name_index,
1267 const char *name,
1268 void *buffer,
1269 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001270{
1271 int ret;
1272 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001273 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1274 struct ocfs2_xattr_search xis = {
1275 .not_found = -ENODATA,
1276 };
1277 struct ocfs2_xattr_search xbs = {
1278 .not_found = -ENODATA,
1279 };
1280
Tiger Yang8154da32008-08-18 17:11:46 +08001281 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1282 return -EOPNOTSUPP;
1283
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001284 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1285 ret = -ENODATA;
1286
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001287 xis.inode_bh = xbs.inode_bh = di_bh;
1288 di = (struct ocfs2_dinode *)di_bh->b_data;
1289
1290 down_read(&oi->ip_xattr_sem);
1291 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1292 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001293 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001294 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1295 buffer_size, &xbs);
1296 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001297
1298 return ret;
1299}
1300
1301/* ocfs2_xattr_get()
1302 *
1303 * Copy an extended attribute into the buffer provided.
1304 * Buffer is NULL to compute the size of buffer required.
1305 */
1306static int ocfs2_xattr_get(struct inode *inode,
1307 int name_index,
1308 const char *name,
1309 void *buffer,
1310 size_t buffer_size)
1311{
1312 int ret;
1313 struct buffer_head *di_bh = NULL;
1314
1315 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1316 if (ret < 0) {
1317 mlog_errno(ret);
1318 return ret;
1319 }
1320 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1321 name, buffer, buffer_size);
1322
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001323 ocfs2_inode_unlock(inode, 0);
1324
1325 brelse(di_bh);
1326
1327 return ret;
1328}
1329
1330static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001331 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001332 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001333 const void *value,
1334 int value_len)
1335{
Tao Ma71d548a2008-12-05 06:20:54 +08001336 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001337 u16 blocksize = inode->i_sb->s_blocksize;
1338 u32 p_cluster, num_clusters;
1339 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1340 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1341 u64 blkno;
1342 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001343 unsigned int ext_flags;
1344 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001345
1346 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1347
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001348 while (cpos < clusters) {
1349 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001350 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001351 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001352 if (ret) {
1353 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001354 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001355 }
1356
Tao Ma492a8a32009-08-18 11:43:17 +08001357 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1358
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001359 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1360
1361 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001362 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1363 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001364 if (ret) {
1365 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001366 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001367 }
1368
1369 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001370 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001371 bh,
1372 OCFS2_JOURNAL_ACCESS_WRITE);
1373 if (ret < 0) {
1374 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001375 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001376 }
1377
1378 cp_len = value_len > blocksize ? blocksize : value_len;
1379 memcpy(bh->b_data, value, cp_len);
1380 value_len -= cp_len;
1381 value += cp_len;
1382 if (cp_len < blocksize)
1383 memset(bh->b_data + cp_len, 0,
1384 blocksize - cp_len);
1385
1386 ret = ocfs2_journal_dirty(handle, bh);
1387 if (ret < 0) {
1388 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001389 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001390 }
1391 brelse(bh);
1392 bh = NULL;
1393
1394 /*
1395 * XXX: do we need to empty all the following
1396 * blocks in this cluster?
1397 */
1398 if (!value_len)
1399 break;
1400 }
1401 cpos += num_clusters;
1402 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001403out:
1404 brelse(bh);
1405
1406 return ret;
1407}
1408
1409static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001410 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001411 struct ocfs2_xattr_info *xi,
1412 struct ocfs2_xattr_search *xs,
Joel Becker512620f2008-12-09 15:58:35 -08001413 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001414 size_t offs)
1415{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001416 int ret = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001417 void *val = xs->base + offs;
Joel Becker199799a2009-08-14 19:04:15 -07001418 size_t size = namevalue_size_xi(xi);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001419
Joel Becker0cf2f762009-02-12 16:41:25 -08001420 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001421 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001422 if (ret) {
1423 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001424 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001425 }
1426 /* Decrease xattr count */
1427 le16_add_cpu(&xs->header->xh_count, -1);
1428 /* Remove the xattr entry and tree root which has already be set*/
1429 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1430 memset(val, 0, size);
1431
Joel Becker512620f2008-12-09 15:58:35 -08001432 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001433 if (ret < 0)
1434 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001435out:
1436 return ret;
1437}
1438
1439static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001440 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001441 struct ocfs2_xattr_info *xi,
1442 struct ocfs2_xattr_search *xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001443 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001444 size_t offs)
1445{
Tao Ma85db90e2008-11-12 08:27:01 +08001446 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001447
Joel Becker0cf2f762009-02-12 16:41:25 -08001448 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker0c748e92008-12-09 15:46:15 -08001449 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001450 if (ret) {
1451 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001452 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001453 }
1454
1455 xs->here->xe_name_offset = cpu_to_le16(offs);
Joel Becker6b240ff2009-08-14 18:02:52 -07001456 xs->here->xe_value_size = cpu_to_le64(xi->xi_value_len);
1457 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001458 ocfs2_xattr_set_local(xs->here, 1);
1459 else
1460 ocfs2_xattr_set_local(xs->here, 0);
1461 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1462
Joel Becker0c748e92008-12-09 15:46:15 -08001463 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001464 if (ret < 0)
1465 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001466out:
1467 return ret;
1468}
1469
1470/*
1471 * ocfs2_xattr_set_value_outside()
1472 *
1473 * Set large size value in B tree.
1474 */
1475static int ocfs2_xattr_set_value_outside(struct inode *inode,
1476 struct ocfs2_xattr_info *xi,
1477 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001478 struct ocfs2_xattr_set_ctxt *ctxt,
Joel Becker512620f2008-12-09 15:58:35 -08001479 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001480 size_t offs)
1481{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001482 void *val = xs->base + offs;
1483 struct ocfs2_xattr_value_root *xv = NULL;
Joel Becker199799a2009-08-14 19:04:15 -07001484 size_t size = namevalue_size_xi(xi);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001485 int ret = 0;
1486
1487 memset(val, 0, size);
Joel Becker18853b92009-08-14 18:17:07 -07001488 memcpy(val, xi->xi_name, xi->xi_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001489 xv = (struct ocfs2_xattr_value_root *)
Joel Becker18853b92009-08-14 18:17:07 -07001490 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001491 xv->xr_clusters = 0;
1492 xv->xr_last_eb_blk = 0;
1493 xv->xr_list.l_tree_depth = 0;
1494 xv->xr_list.l_count = cpu_to_le16(1);
1495 xv->xr_list.l_next_free_rec = 0;
Joel Becker512620f2008-12-09 15:58:35 -08001496 vb->vb_xv = xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001497
Joel Becker6b240ff2009-08-14 18:02:52 -07001498 ret = ocfs2_xattr_value_truncate(inode, vb, xi->xi_value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001499 if (ret < 0) {
1500 mlog_errno(ret);
1501 return ret;
1502 }
Joel Becker512620f2008-12-09 15:58:35 -08001503 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001504 if (ret < 0) {
1505 mlog_errno(ret);
1506 return ret;
1507 }
Tao Ma492a8a32009-08-18 11:43:17 +08001508 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
Joel Becker6b240ff2009-08-14 18:02:52 -07001509 xi->xi_value, xi->xi_value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001510 if (ret < 0)
1511 mlog_errno(ret);
1512
1513 return ret;
1514}
1515
Joel Becker69a3e532009-08-17 12:24:39 -07001516static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1517 int num_entries)
1518{
1519 int free_space;
1520
1521 if (!needed_space)
1522 return 0;
1523
1524 free_space = free_start -
1525 sizeof(struct ocfs2_xattr_header) -
1526 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1527 OCFS2_XATTR_HEADER_GAP;
1528 if (free_space < 0)
1529 return -EIO;
1530 if (free_space < needed_space)
1531 return -ENOSPC;
1532
1533 return 0;
1534}
1535
1536/* Give a pointer into the storage for the given offset */
1537static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1538{
1539 BUG_ON(offset >= loc->xl_size);
1540 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1541}
1542
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001543/*
Joel Becker11179f22009-08-14 16:07:44 -07001544 * Wipe the name+value pair and allow the storage to reclaim it. This
1545 * must be followed by either removal of the entry or a call to
1546 * ocfs2_xa_add_namevalue().
1547 */
1548static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1549{
1550 loc->xl_ops->xlo_wipe_namevalue(loc);
1551}
1552
Joel Becker69a3e532009-08-17 12:24:39 -07001553/*
1554 * Find lowest offset to a name+value pair. This is the start of our
1555 * downward-growing free space.
1556 */
1557static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1558{
1559 return loc->xl_ops->xlo_get_free_start(loc);
1560}
1561
1562/* Can we reuse loc->xl_entry for xi? */
1563static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1564 struct ocfs2_xattr_info *xi)
1565{
1566 return loc->xl_ops->xlo_can_reuse(loc, xi);
1567}
1568
1569/* How much free space is needed to set the new value */
1570static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1571 struct ocfs2_xattr_info *xi)
1572{
1573 return loc->xl_ops->xlo_check_space(loc, xi);
1574}
1575
1576static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1577{
1578 loc->xl_ops->xlo_add_entry(loc, name_hash);
1579 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1580 /*
1581 * We can't leave the new entry's xe_name_offset at zero or
1582 * add_namevalue() will go nuts. We set it to the size of our
1583 * storage so that it can never be less than any other entry.
1584 */
1585 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1586}
1587
1588static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1589 struct ocfs2_xattr_info *xi)
1590{
1591 int size = namevalue_size_xi(xi);
1592 int nameval_offset;
1593 char *nameval_buf;
1594
1595 loc->xl_ops->xlo_add_namevalue(loc, size);
1596 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1597 loc->xl_entry->xe_name_len = xi->xi_name_len;
1598 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1599 ocfs2_xattr_set_local(loc->xl_entry,
1600 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1601
1602 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1603 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1604 memset(nameval_buf, 0, size);
1605 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1606}
1607
Joel Becker11179f22009-08-14 16:07:44 -07001608static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1609 int offset)
1610{
Joel Becker11179f22009-08-14 16:07:44 -07001611 return (char *)loc->xl_header + offset;
1612}
1613
Joel Becker69a3e532009-08-17 12:24:39 -07001614static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1615 struct ocfs2_xattr_info *xi)
1616{
1617 /*
1618 * Block storage is strict. If the sizes aren't exact, we will
1619 * remove the old one and reinsert the new.
1620 */
1621 return namevalue_size_xe(loc->xl_entry) ==
1622 namevalue_size_xi(xi);
1623}
1624
1625static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1626{
1627 struct ocfs2_xattr_header *xh = loc->xl_header;
1628 int i, count = le16_to_cpu(xh->xh_count);
1629 int offset, free_start = loc->xl_size;
1630
1631 for (i = 0; i < count; i++) {
1632 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1633 if (offset < free_start)
1634 free_start = offset;
1635 }
1636
1637 return free_start;
1638}
1639
1640static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1641 struct ocfs2_xattr_info *xi)
1642{
1643 int count = le16_to_cpu(loc->xl_header->xh_count);
1644 int free_start = ocfs2_xa_get_free_start(loc);
1645 int needed_space = ocfs2_xi_entry_usage(xi);
1646
1647 /*
1648 * Block storage will reclaim the original entry before inserting
1649 * the new value, so we only need the difference. If the new
1650 * entry is smaller than the old one, we don't need anything.
1651 */
1652 if (loc->xl_entry) {
1653 /* Don't need space if we're reusing! */
1654 if (ocfs2_xa_can_reuse_entry(loc, xi))
1655 needed_space = 0;
1656 else
1657 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1658 }
1659 if (needed_space < 0)
1660 needed_space = 0;
1661 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1662}
1663
Joel Becker11179f22009-08-14 16:07:44 -07001664/*
1665 * Block storage for xattrs keeps the name+value pairs compacted. When
1666 * we remove one, we have to shift any that preceded it towards the end.
1667 */
1668static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1669{
1670 int i, offset;
1671 int namevalue_offset, first_namevalue_offset, namevalue_size;
1672 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1673 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Becker11179f22009-08-14 16:07:44 -07001674 int count = le16_to_cpu(xh->xh_count);
1675
1676 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07001677 namevalue_size = namevalue_size_xe(entry);
Joel Becker69a3e532009-08-17 12:24:39 -07001678 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
Joel Becker11179f22009-08-14 16:07:44 -07001679
1680 /* Shift the name+value pairs */
1681 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1682 (char *)xh + first_namevalue_offset,
1683 namevalue_offset - first_namevalue_offset);
1684 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1685
1686 /* Now tell xh->xh_entries about it */
1687 for (i = 0; i < count; i++) {
1688 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1689 if (offset < namevalue_offset)
1690 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1691 namevalue_size);
1692 }
1693
1694 /*
1695 * Note that we don't update xh_free_start or xh_name_value_len
1696 * because they're not used in block-stored xattrs.
1697 */
1698}
1699
Joel Becker69a3e532009-08-17 12:24:39 -07001700static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1701{
1702 int count = le16_to_cpu(loc->xl_header->xh_count);
1703 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1704 le16_add_cpu(&loc->xl_header->xh_count, 1);
1705 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1706}
1707
1708static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1709{
1710 int free_start = ocfs2_xa_get_free_start(loc);
1711
1712 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1713}
1714
Joel Becker11179f22009-08-14 16:07:44 -07001715/*
1716 * Operations for xattrs stored in blocks. This includes inline inode
1717 * storage and unindexed ocfs2_xattr_blocks.
1718 */
1719static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1720 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001721 .xlo_check_space = ocfs2_xa_block_check_space,
1722 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1723 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001724 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001725 .xlo_add_entry = ocfs2_xa_block_add_entry,
1726 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
Joel Becker11179f22009-08-14 16:07:44 -07001727};
1728
1729static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1730 int offset)
1731{
1732 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1733 int block, block_offset;
1734
Joel Becker11179f22009-08-14 16:07:44 -07001735 /* The header is at the front of the bucket */
1736 block = offset >> bucket->bu_inode->i_sb->s_blocksize_bits;
1737 block_offset = offset % bucket->bu_inode->i_sb->s_blocksize;
1738
1739 return bucket_block(bucket, block) + block_offset;
1740}
1741
Joel Becker69a3e532009-08-17 12:24:39 -07001742static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1743 struct ocfs2_xattr_info *xi)
1744{
1745 return namevalue_size_xe(loc->xl_entry) >=
1746 namevalue_size_xi(xi);
1747}
1748
1749static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1750{
1751 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1752 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1753}
1754
1755static int ocfs2_bucket_align_free_start(struct super_block *sb,
1756 int free_start, int size)
1757{
1758 /*
1759 * We need to make sure that the name+value pair fits within
1760 * one block.
1761 */
1762 if (((free_start - size) >> sb->s_blocksize_bits) !=
1763 ((free_start - 1) >> sb->s_blocksize_bits))
1764 free_start -= free_start % sb->s_blocksize;
1765
1766 return free_start;
1767}
1768
1769static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1770 struct ocfs2_xattr_info *xi)
1771{
1772 int rc;
1773 int count = le16_to_cpu(loc->xl_header->xh_count);
1774 int free_start = ocfs2_xa_get_free_start(loc);
1775 int needed_space = ocfs2_xi_entry_usage(xi);
1776 int size = namevalue_size_xi(xi);
1777 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1778 struct super_block *sb = bucket->bu_inode->i_sb;
1779
1780 /*
1781 * Bucket storage does not reclaim name+value pairs it cannot
1782 * reuse. They live as holes until the bucket fills, and then
1783 * the bucket is defragmented. However, the bucket can reclaim
1784 * the ocfs2_xattr_entry.
1785 */
1786 if (loc->xl_entry) {
1787 /* Don't need space if we're reusing! */
1788 if (ocfs2_xa_can_reuse_entry(loc, xi))
1789 needed_space = 0;
1790 else
1791 needed_space -= sizeof(struct ocfs2_xattr_entry);
1792 }
1793 BUG_ON(needed_space < 0);
1794
1795 if (free_start < size) {
1796 if (needed_space)
1797 return -ENOSPC;
1798 } else {
1799 /*
1800 * First we check if it would fit in the first place.
1801 * Below, we align the free start to a block. This may
1802 * slide us below the minimum gap. By checking unaligned
1803 * first, we avoid that error.
1804 */
1805 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1806 count);
1807 if (rc)
1808 return rc;
1809 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1810 size);
1811 }
1812 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1813}
1814
Joel Becker11179f22009-08-14 16:07:44 -07001815static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1816{
Joel Becker199799a2009-08-14 19:04:15 -07001817 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1818 -namevalue_size_xe(loc->xl_entry));
Joel Becker11179f22009-08-14 16:07:44 -07001819}
1820
Joel Becker69a3e532009-08-17 12:24:39 -07001821static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1822{
1823 struct ocfs2_xattr_header *xh = loc->xl_header;
1824 int count = le16_to_cpu(xh->xh_count);
1825 int low = 0, high = count - 1, tmp;
1826 struct ocfs2_xattr_entry *tmp_xe;
1827
1828 /*
1829 * We keep buckets sorted by name_hash, so we need to find
1830 * our insert place.
1831 */
1832 while (low <= high && count) {
1833 tmp = (low + high) / 2;
1834 tmp_xe = &xh->xh_entries[tmp];
1835
1836 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1837 low = tmp + 1;
1838 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1839 high = tmp - 1;
1840 else {
1841 low = tmp;
1842 break;
1843 }
1844 }
1845
1846 if (low != count)
1847 memmove(&xh->xh_entries[low + 1],
1848 &xh->xh_entries[low],
1849 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1850
1851 le16_add_cpu(&xh->xh_count, 1);
1852 loc->xl_entry = &xh->xh_entries[low];
1853 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1854}
1855
1856static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1857{
1858 int free_start = ocfs2_xa_get_free_start(loc);
1859 struct ocfs2_xattr_header *xh = loc->xl_header;
1860 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1861 struct super_block *sb = bucket->bu_inode->i_sb;
1862 int nameval_offset;
1863
1864 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1865 nameval_offset = free_start - size;
1866 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1867 xh->xh_free_start = cpu_to_le16(nameval_offset);
1868 le16_add_cpu(&xh->xh_name_value_len, size);
1869
1870}
1871
Joel Becker11179f22009-08-14 16:07:44 -07001872/* Operations for xattrs stored in buckets. */
1873static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1874 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001875 .xlo_check_space = ocfs2_xa_bucket_check_space,
1876 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1877 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001878 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001879 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1880 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
Joel Becker11179f22009-08-14 16:07:44 -07001881};
1882
1883static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1884{
Joel Beckerbde1e542009-08-14 16:58:38 -07001885 int index, count;
1886 struct ocfs2_xattr_header *xh = loc->xl_header;
1887 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1888
Joel Becker11179f22009-08-14 16:07:44 -07001889 ocfs2_xa_wipe_namevalue(loc);
Joel Beckerbde1e542009-08-14 16:58:38 -07001890 loc->xl_entry = NULL;
1891
1892 le16_add_cpu(&xh->xh_count, -1);
1893 count = le16_to_cpu(xh->xh_count);
1894
1895 /*
1896 * Only zero out the entry if there are more remaining. This is
1897 * important for an empty bucket, as it keeps track of the
1898 * bucket's hash value. It doesn't hurt empty block storage.
1899 */
1900 if (count) {
1901 index = ((char *)entry - (char *)&xh->xh_entries) /
1902 sizeof(struct ocfs2_xattr_entry);
1903 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1904 (count - index) * sizeof(struct ocfs2_xattr_entry));
1905 memset(&xh->xh_entries[count], 0,
1906 sizeof(struct ocfs2_xattr_entry));
1907 }
Joel Becker11179f22009-08-14 16:07:44 -07001908}
1909
Joel Becker69a3e532009-08-17 12:24:39 -07001910/*
1911 * Prepares loc->xl_entry to receive the new xattr. This includes
1912 * properly setting up the name+value pair region. If loc->xl_entry
1913 * already exists, it will take care of modifying it appropriately.
1914 * This also includes deleting entries, but don't call this to remove
1915 * a non-existant entry. That's just a bug.
1916 *
1917 * Note that this modifies the data. You did journal_access already,
1918 * right?
1919 */
1920static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
1921 struct ocfs2_xattr_info *xi,
1922 u32 name_hash)
1923{
1924 int rc = 0;
1925 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1926 char *nameval_buf;
1927
1928 if (!xi->xi_value) {
1929 ocfs2_xa_remove_entry(loc);
1930 goto out;
1931 }
1932
1933 rc = ocfs2_xa_check_space(loc, xi);
1934 if (rc)
1935 goto out;
1936
1937 if (loc->xl_entry) {
1938 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
1939 nameval_buf = ocfs2_xa_offset_pointer(loc,
1940 le16_to_cpu(loc->xl_entry->xe_name_offset));
1941 memset(nameval_buf + name_size, 0,
1942 namevalue_size_xe(loc->xl_entry) - name_size);
1943 loc->xl_entry->xe_value_size =
1944 cpu_to_le64(xi->xi_value_len);
1945 goto out;
1946 }
1947
1948 ocfs2_xa_wipe_namevalue(loc);
1949 } else
1950 ocfs2_xa_add_entry(loc, name_hash);
1951
1952 /*
1953 * If we get here, we have a blank entry. Fill it. We grow our
1954 * name+value pair back from the end.
1955 */
1956 ocfs2_xa_add_namevalue(loc, xi);
1957
1958out:
1959 return rc;
1960}
1961
1962/*
1963 * Store the value portion of the name+value pair. This is either an
1964 * inline value or the tree root of an external value.
1965 */
1966static void ocfs2_xa_store_inline_value(struct ocfs2_xa_loc *loc,
1967 struct ocfs2_xattr_info *xi)
1968{
1969 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1970 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker9dc47402009-08-17 19:56:01 -07001971 int inline_value_size = namevalue_size_xi(xi) - name_size;
1972 const void *value = xi->xi_value;
Joel Becker69a3e532009-08-17 12:24:39 -07001973 char *nameval_buf;
1974
1975 if (!xi->xi_value)
1976 return;
1977
Joel Becker9dc47402009-08-17 19:56:01 -07001978 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
1979 value = &def_xv;
1980 inline_value_size = OCFS2_XATTR_ROOT_SIZE;
1981 }
Joel Becker69a3e532009-08-17 12:24:39 -07001982 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
Joel Becker9dc47402009-08-17 19:56:01 -07001983 memcpy(nameval_buf + name_size, value, inline_value_size);
Joel Becker69a3e532009-08-17 12:24:39 -07001984}
1985
Joel Becker11179f22009-08-14 16:07:44 -07001986static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
1987 struct inode *inode,
1988 struct buffer_head *bh,
1989 struct ocfs2_xattr_entry *entry)
1990{
1991 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
1992
1993 loc->xl_ops = &ocfs2_xa_block_loc_ops;
1994 loc->xl_storage = bh;
1995 loc->xl_entry = entry;
1996
1997 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1998 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
1999 else {
2000 BUG_ON(entry);
2001 loc->xl_size = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2002 }
2003 loc->xl_header =
2004 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2005 loc->xl_size);
2006}
2007
2008static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2009 struct buffer_head *bh,
2010 struct ocfs2_xattr_entry *entry)
2011{
2012 struct ocfs2_xattr_block *xb =
2013 (struct ocfs2_xattr_block *)bh->b_data;
2014
2015 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2016
2017 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2018 loc->xl_storage = bh;
2019 loc->xl_header = &(xb->xb_attrs.xb_header);
2020 loc->xl_entry = entry;
2021 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2022 xb_attrs.xb_header);
2023}
2024
2025static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2026 struct ocfs2_xattr_bucket *bucket,
2027 struct ocfs2_xattr_entry *entry)
2028{
2029 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2030 loc->xl_storage = bucket;
2031 loc->xl_header = bucket_xh(bucket);
2032 loc->xl_entry = entry;
2033 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2034}
2035
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002036
2037/*
2038 * ocfs2_xattr_set_entry()
2039 *
2040 * Set extended attribute entry into inode or block.
2041 *
2042 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
Joel Becker69a3e532009-08-17 12:24:39 -07002043 * We first insert tree root(ocfs2_xattr_value_root) like a normal value,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002044 * then set value in B tree with set_value_outside().
2045 */
2046static int ocfs2_xattr_set_entry(struct inode *inode,
2047 struct ocfs2_xattr_info *xi,
2048 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08002049 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002050 int flag)
2051{
2052 struct ocfs2_xattr_entry *last;
2053 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2054 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Joel Becker18853b92009-08-14 18:17:07 -07002055 size_t min_offs = xs->end - xs->base;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002056 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002057 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002058 int free, i, ret;
Joel Becker69a3e532009-08-17 12:24:39 -07002059 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
2060 xi->xi_name_len);
2061 struct ocfs2_xa_loc loc;
Joel Becker512620f2008-12-09 15:58:35 -08002062 struct ocfs2_xattr_value_buf vb = {
2063 .vb_bh = xs->xattr_bh,
2064 .vb_access = ocfs2_journal_access_di,
2065 };
2066
2067 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2068 BUG_ON(xs->xattr_bh == xs->inode_bh);
2069 vb.vb_access = ocfs2_journal_access_xb;
2070 } else
2071 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002072
2073 /* Compute min_offs, last and free space. */
2074 last = xs->header->xh_entries;
2075
2076 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
2077 size_t offs = le16_to_cpu(last->xe_name_offset);
2078 if (offs < min_offs)
2079 min_offs = offs;
2080 last += 1;
2081 }
2082
Tiger Yang4442f512009-02-20 11:11:50 +08002083 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002084 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07002085 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002086
Joel Becker199799a2009-08-14 19:04:15 -07002087 if (!xs->not_found)
2088 free += ocfs2_xe_entry_usage(xs->here);
2089
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002090 /* Check free space in inode or block */
Joel Becker9dc47402009-08-17 19:56:01 -07002091 if (xi->xi_value && (free < ocfs2_xi_entry_usage(xi))) {
2092 ret = -ENOSPC;
2093 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002094 }
2095
2096 if (!xs->not_found) {
2097 /* For existing extended attribute */
Joel Becker199799a2009-08-14 19:04:15 -07002098 size_t size = namevalue_size_xe(xs->here);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002099 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
2100 void *val = xs->base + offs;
2101
2102 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
2103 /* Replace existing local xattr with tree root */
2104 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Joel Becker512620f2008-12-09 15:58:35 -08002105 ctxt, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002106 if (ret < 0)
2107 mlog_errno(ret);
2108 goto out;
2109 } else if (!ocfs2_xattr_is_local(xs->here)) {
2110 /* For existing xattr which has value outside */
Joel Becker512620f2008-12-09 15:58:35 -08002111 vb.vb_xv = (struct ocfs2_xattr_value_root *)
Joel Becker18853b92009-08-14 18:17:07 -07002112 (val + OCFS2_XATTR_SIZE(xi->xi_name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002113
Joel Becker6b240ff2009-08-14 18:02:52 -07002114 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002115 /*
2116 * If new value need set outside also,
2117 * first truncate old value to new value,
2118 * then set new value with set_value_outside().
2119 */
2120 ret = ocfs2_xattr_value_truncate(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07002121 &vb,
2122 xi->xi_value_len,
2123 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002124 if (ret < 0) {
2125 mlog_errno(ret);
2126 goto out;
2127 }
2128
Tao Ma85db90e2008-11-12 08:27:01 +08002129 ret = ocfs2_xattr_update_entry(inode,
2130 handle,
2131 xi,
2132 xs,
Joel Becker0c748e92008-12-09 15:46:15 -08002133 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08002134 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002135 if (ret < 0) {
2136 mlog_errno(ret);
2137 goto out;
2138 }
2139
Tao Ma85db90e2008-11-12 08:27:01 +08002140 ret = __ocfs2_xattr_set_value_outside(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07002141 handle,
2142 &vb,
2143 xi->xi_value,
2144 xi->xi_value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002145 if (ret < 0)
2146 mlog_errno(ret);
2147 goto out;
2148 } else {
2149 /*
2150 * If new value need set in local,
2151 * just trucate old value to zero.
2152 */
2153 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08002154 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08002155 0,
2156 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002157 if (ret < 0)
2158 mlog_errno(ret);
2159 }
2160 }
2161 }
2162
Joel Becker0cf2f762009-02-12 16:41:25 -08002163 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
Joel Becker512620f2008-12-09 15:58:35 -08002164 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002165 if (ret) {
2166 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002167 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002168 }
2169
2170 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002171 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08002172 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002173 if (ret) {
2174 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002175 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002176 }
2177 }
2178
Joel Becker69a3e532009-08-17 12:24:39 -07002179 if (xs->xattr_bh == xs->inode_bh)
2180 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2181 xs->not_found ? NULL : xs->here);
2182 else
2183 ocfs2_init_xattr_block_xa_loc(&loc, xs->xattr_bh,
2184 xs->not_found ? NULL : xs->here);
2185
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002186 /*
Joel Becker69a3e532009-08-17 12:24:39 -07002187 * Prepare our entry and insert the inline value. This will
2188 * be a value tree root for values that are larger than
2189 * OCFS2_XATTR_INLINE_SIZE.
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002190 */
Joel Becker69a3e532009-08-17 12:24:39 -07002191 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
2192 if (ret) {
2193 if (ret != -ENOSPC)
2194 mlog_errno(ret);
2195 goto out;
2196 }
2197 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
2198 BUG_ON(ret == -ENOSPC);
2199 ocfs2_xa_store_inline_value(&loc, xi);
2200 xs->here = loc.xl_entry;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002201
2202 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
2203 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
2204 if (ret < 0) {
2205 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002206 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002207 }
2208 }
2209
2210 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
2211 (flag & OCFS2_INLINE_XATTR_FL)) {
2212 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2213 unsigned int xattrsize = osb->s_xattr_inline_size;
2214
2215 /*
2216 * Adjust extent record count or inline data size
2217 * to reserve space for extended attribute.
2218 */
2219 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2220 struct ocfs2_inline_data *idata = &di->id2.i_data;
2221 le16_add_cpu(&idata->id_count, -xattrsize);
2222 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2223 struct ocfs2_extent_list *el = &di->id2.i_list;
2224 le16_add_cpu(&el->l_count, -(xattrsize /
2225 sizeof(struct ocfs2_extent_rec)));
2226 }
2227 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2228 }
2229 /* Update xattr flag */
2230 spin_lock(&oi->ip_lock);
2231 oi->ip_dyn_features |= flag;
2232 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2233 spin_unlock(&oi->ip_lock);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002234
2235 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2236 if (ret < 0)
2237 mlog_errno(ret);
2238
Joel Becker6b240ff2009-08-14 18:02:52 -07002239 if (!ret && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002240 /*
2241 * Set value outside in B tree.
2242 * This is the second step for value size > INLINE_SIZE.
2243 */
2244 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Joel Becker512620f2008-12-09 15:58:35 -08002245 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
2246 &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002247 if (ret < 0) {
2248 int ret2;
2249
2250 mlog_errno(ret);
2251 /*
2252 * If set value outside failed, we have to clean
2253 * the junk tree root we have already set in local.
2254 */
Tao Ma85db90e2008-11-12 08:27:01 +08002255 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
Joel Becker512620f2008-12-09 15:58:35 -08002256 xi, xs, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002257 if (ret2 < 0)
2258 mlog_errno(ret2);
2259 }
2260 }
2261out:
2262 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002263}
2264
Tao Mace9c5a52009-08-18 11:43:59 +08002265/*
2266 * In xattr remove, if it is stored outside and refcounted, we may have
2267 * the chance to split the refcount tree. So need the allocators.
2268 */
2269static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2270 struct ocfs2_xattr_value_root *xv,
2271 struct ocfs2_caching_info *ref_ci,
2272 struct buffer_head *ref_root_bh,
2273 struct ocfs2_alloc_context **meta_ac,
2274 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002275{
Tao Mace9c5a52009-08-18 11:43:59 +08002276 int ret, meta_add = 0;
2277 u32 p_cluster, num_clusters;
2278 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08002279
Tao Mace9c5a52009-08-18 11:43:59 +08002280 *ref_credits = 0;
2281 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2282 &num_clusters,
2283 &xv->xr_list,
2284 &ext_flags);
2285 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08002286 mlog_errno(ret);
2287 goto out;
2288 }
2289
Tao Mace9c5a52009-08-18 11:43:59 +08002290 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2291 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002292
Tao Mace9c5a52009-08-18 11:43:59 +08002293 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2294 ref_root_bh, xv,
2295 &meta_add, ref_credits);
2296 if (ret) {
2297 mlog_errno(ret);
2298 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002299 }
2300
Tao Mace9c5a52009-08-18 11:43:59 +08002301 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2302 meta_add, meta_ac);
2303 if (ret)
2304 mlog_errno(ret);
2305
Tao Ma85db90e2008-11-12 08:27:01 +08002306out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002307 return ret;
2308}
2309
Tao Mace9c5a52009-08-18 11:43:59 +08002310static int ocfs2_remove_value_outside(struct inode*inode,
2311 struct ocfs2_xattr_value_buf *vb,
2312 struct ocfs2_xattr_header *header,
2313 struct ocfs2_caching_info *ref_ci,
2314 struct buffer_head *ref_root_bh)
2315{
2316 int ret = 0, i, ref_credits;
2317 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2318 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2319 void *val;
2320
2321 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2322
2323 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2324 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2325
2326 if (ocfs2_xattr_is_local(entry))
2327 continue;
2328
2329 val = (void *)header +
2330 le16_to_cpu(entry->xe_name_offset);
2331 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2332 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2333
2334 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2335 ref_ci, ref_root_bh,
2336 &ctxt.meta_ac,
2337 &ref_credits);
2338
2339 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2340 ocfs2_remove_extent_credits(osb->sb));
2341 if (IS_ERR(ctxt.handle)) {
2342 ret = PTR_ERR(ctxt.handle);
2343 mlog_errno(ret);
2344 break;
2345 }
2346
2347 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2348 if (ret < 0) {
2349 mlog_errno(ret);
2350 break;
2351 }
2352
2353 ocfs2_commit_trans(osb, ctxt.handle);
2354 if (ctxt.meta_ac) {
2355 ocfs2_free_alloc_context(ctxt.meta_ac);
2356 ctxt.meta_ac = NULL;
2357 }
2358 }
2359
2360 if (ctxt.meta_ac)
2361 ocfs2_free_alloc_context(ctxt.meta_ac);
2362 ocfs2_schedule_truncate_log_flush(osb, 1);
2363 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2364 return ret;
2365}
2366
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002367static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002368 struct buffer_head *di_bh,
2369 struct ocfs2_caching_info *ref_ci,
2370 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002371{
2372
2373 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2374 struct ocfs2_xattr_header *header;
2375 int ret;
Joel Becker43119012008-12-09 16:24:43 -08002376 struct ocfs2_xattr_value_buf vb = {
2377 .vb_bh = di_bh,
2378 .vb_access = ocfs2_journal_access_di,
2379 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002380
2381 header = (struct ocfs2_xattr_header *)
2382 ((void *)di + inode->i_sb->s_blocksize -
2383 le16_to_cpu(di->i_xattr_inline_size));
2384
Tao Mace9c5a52009-08-18 11:43:59 +08002385 ret = ocfs2_remove_value_outside(inode, &vb, header,
2386 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002387
2388 return ret;
2389}
2390
Tao Mace9c5a52009-08-18 11:43:59 +08002391struct ocfs2_rm_xattr_bucket_para {
2392 struct ocfs2_caching_info *ref_ci;
2393 struct buffer_head *ref_root_bh;
2394};
2395
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002396static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002397 struct buffer_head *blk_bh,
2398 struct ocfs2_caching_info *ref_ci,
2399 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002400{
2401 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002402 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08002403 struct ocfs2_xattr_value_buf vb = {
2404 .vb_bh = blk_bh,
2405 .vb_access = ocfs2_journal_access_xb,
2406 };
Tao Mace9c5a52009-08-18 11:43:59 +08002407 struct ocfs2_rm_xattr_bucket_para args = {
2408 .ref_ci = ref_ci,
2409 .ref_root_bh = ref_root_bh,
2410 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002411
2412 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08002413 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2414 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08002415 ret = ocfs2_remove_value_outside(inode, &vb, header,
2416 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08002417 } else
Tao Ma47bca492009-08-18 11:43:42 +08002418 ret = ocfs2_iterate_xattr_index_block(inode,
2419 blk_bh,
2420 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08002421 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002422
2423 return ret;
2424}
2425
Tao Ma08413892008-08-29 09:00:19 +08002426static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002427 u64 block,
2428 struct ocfs2_caching_info *ref_ci,
2429 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08002430{
2431 struct inode *xb_alloc_inode;
2432 struct buffer_head *xb_alloc_bh = NULL;
2433 struct buffer_head *blk_bh = NULL;
2434 struct ocfs2_xattr_block *xb;
2435 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2436 handle_t *handle;
2437 int ret = 0;
2438 u64 blk, bg_blkno;
2439 u16 bit;
2440
Joel Becker4ae1d692008-11-13 14:49:18 -08002441 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08002442 if (ret < 0) {
2443 mlog_errno(ret);
2444 goto out;
2445 }
2446
Tao Mace9c5a52009-08-18 11:43:59 +08002447 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08002448 if (ret < 0) {
2449 mlog_errno(ret);
2450 goto out;
2451 }
2452
Joel Becker4ae1d692008-11-13 14:49:18 -08002453 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08002454 blk = le64_to_cpu(xb->xb_blkno);
2455 bit = le16_to_cpu(xb->xb_suballoc_bit);
2456 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2457
2458 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2459 EXTENT_ALLOC_SYSTEM_INODE,
2460 le16_to_cpu(xb->xb_suballoc_slot));
2461 if (!xb_alloc_inode) {
2462 ret = -ENOMEM;
2463 mlog_errno(ret);
2464 goto out;
2465 }
2466 mutex_lock(&xb_alloc_inode->i_mutex);
2467
2468 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2469 if (ret < 0) {
2470 mlog_errno(ret);
2471 goto out_mutex;
2472 }
2473
2474 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2475 if (IS_ERR(handle)) {
2476 ret = PTR_ERR(handle);
2477 mlog_errno(ret);
2478 goto out_unlock;
2479 }
2480
2481 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2482 bit, bg_blkno, 1);
2483 if (ret < 0)
2484 mlog_errno(ret);
2485
2486 ocfs2_commit_trans(osb, handle);
2487out_unlock:
2488 ocfs2_inode_unlock(xb_alloc_inode, 1);
2489 brelse(xb_alloc_bh);
2490out_mutex:
2491 mutex_unlock(&xb_alloc_inode->i_mutex);
2492 iput(xb_alloc_inode);
2493out:
2494 brelse(blk_bh);
2495 return ret;
2496}
2497
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002498/*
2499 * ocfs2_xattr_remove()
2500 *
2501 * Free extended attribute resources associated with this inode.
2502 */
2503int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2504{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002505 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2506 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002507 struct ocfs2_refcount_tree *ref_tree = NULL;
2508 struct buffer_head *ref_root_bh = NULL;
2509 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002510 handle_t *handle;
2511 int ret;
2512
Tiger Yang8154da32008-08-18 17:11:46 +08002513 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2514 return 0;
2515
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002516 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2517 return 0;
2518
Tao Mace9c5a52009-08-18 11:43:59 +08002519 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2520 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2521 le64_to_cpu(di->i_refcount_loc),
2522 1, &ref_tree, &ref_root_bh);
2523 if (ret) {
2524 mlog_errno(ret);
2525 goto out;
2526 }
2527 ref_ci = &ref_tree->rf_ci;
2528
2529 }
2530
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002531 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002532 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2533 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002534 if (ret < 0) {
2535 mlog_errno(ret);
2536 goto out;
2537 }
2538 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002539
Tao Ma08413892008-08-29 09:00:19 +08002540 if (di->i_xattr_loc) {
2541 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002542 le64_to_cpu(di->i_xattr_loc),
2543 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002544 if (ret < 0) {
2545 mlog_errno(ret);
2546 goto out;
2547 }
2548 }
2549
2550 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2551 OCFS2_INODE_UPDATE_CREDITS);
2552 if (IS_ERR(handle)) {
2553 ret = PTR_ERR(handle);
2554 mlog_errno(ret);
2555 goto out;
2556 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002557 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002558 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002559 if (ret) {
2560 mlog_errno(ret);
2561 goto out_commit;
2562 }
2563
Tao Ma08413892008-08-29 09:00:19 +08002564 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002565
2566 spin_lock(&oi->ip_lock);
2567 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2568 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2569 spin_unlock(&oi->ip_lock);
2570
2571 ret = ocfs2_journal_dirty(handle, di_bh);
2572 if (ret < 0)
2573 mlog_errno(ret);
2574out_commit:
2575 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2576out:
Tao Mace9c5a52009-08-18 11:43:59 +08002577 if (ref_tree)
2578 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2579 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002580 return ret;
2581}
2582
2583static int ocfs2_xattr_has_space_inline(struct inode *inode,
2584 struct ocfs2_dinode *di)
2585{
2586 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2587 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2588 int free;
2589
2590 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2591 return 0;
2592
2593 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2594 struct ocfs2_inline_data *idata = &di->id2.i_data;
2595 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2596 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2597 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2598 le64_to_cpu(di->i_size);
2599 } else {
2600 struct ocfs2_extent_list *el = &di->id2.i_list;
2601 free = (le16_to_cpu(el->l_count) -
2602 le16_to_cpu(el->l_next_free_rec)) *
2603 sizeof(struct ocfs2_extent_rec);
2604 }
2605 if (free >= xattrsize)
2606 return 1;
2607
2608 return 0;
2609}
2610
2611/*
2612 * ocfs2_xattr_ibody_find()
2613 *
2614 * Find extended attribute in inode block and
2615 * fill search info into struct ocfs2_xattr_search.
2616 */
2617static int ocfs2_xattr_ibody_find(struct inode *inode,
2618 int name_index,
2619 const char *name,
2620 struct ocfs2_xattr_search *xs)
2621{
2622 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2623 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2624 int ret;
2625 int has_space = 0;
2626
2627 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2628 return 0;
2629
2630 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2631 down_read(&oi->ip_alloc_sem);
2632 has_space = ocfs2_xattr_has_space_inline(inode, di);
2633 up_read(&oi->ip_alloc_sem);
2634 if (!has_space)
2635 return 0;
2636 }
2637
2638 xs->xattr_bh = xs->inode_bh;
2639 xs->end = (void *)di + inode->i_sb->s_blocksize;
2640 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2641 xs->header = (struct ocfs2_xattr_header *)
2642 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2643 else
2644 xs->header = (struct ocfs2_xattr_header *)
2645 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2646 xs->base = (void *)xs->header;
2647 xs->here = xs->header->xh_entries;
2648
2649 /* Find the named attribute. */
2650 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2651 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2652 if (ret && ret != -ENODATA)
2653 return ret;
2654 xs->not_found = ret;
2655 }
2656
2657 return 0;
2658}
2659
2660/*
2661 * ocfs2_xattr_ibody_set()
2662 *
2663 * Set, replace or remove an extended attribute into inode block.
2664 *
2665 */
2666static int ocfs2_xattr_ibody_set(struct inode *inode,
2667 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002668 struct ocfs2_xattr_search *xs,
2669 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002670{
2671 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2672 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2673 int ret;
2674
2675 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2676 return -ENOSPC;
2677
2678 down_write(&oi->ip_alloc_sem);
2679 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2680 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2681 ret = -ENOSPC;
2682 goto out;
2683 }
2684 }
2685
Tao Ma78f30c32008-11-12 08:27:00 +08002686 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002687 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2688out:
2689 up_write(&oi->ip_alloc_sem);
2690
2691 return ret;
2692}
2693
2694/*
2695 * ocfs2_xattr_block_find()
2696 *
2697 * Find extended attribute in external block and
2698 * fill search info into struct ocfs2_xattr_search.
2699 */
2700static int ocfs2_xattr_block_find(struct inode *inode,
2701 int name_index,
2702 const char *name,
2703 struct ocfs2_xattr_search *xs)
2704{
2705 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2706 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002707 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002708 int ret = 0;
2709
2710 if (!di->i_xattr_loc)
2711 return ret;
2712
Joel Becker4ae1d692008-11-13 14:49:18 -08002713 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2714 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002715 if (ret < 0) {
2716 mlog_errno(ret);
2717 return ret;
2718 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002719
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002720 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002721 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002722
Tao Ma589dc262008-08-18 17:38:51 +08002723 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2724 xs->header = &xb->xb_attrs.xb_header;
2725 xs->base = (void *)xs->header;
2726 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2727 xs->here = xs->header->xh_entries;
2728
2729 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2730 } else
2731 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2732 name_index,
2733 name, xs);
2734
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002735 if (ret && ret != -ENODATA) {
2736 xs->xattr_bh = NULL;
2737 goto cleanup;
2738 }
2739 xs->not_found = ret;
2740 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002741cleanup:
2742 brelse(blk_bh);
2743
2744 return ret;
2745}
2746
Tao Ma5aea1f02009-08-18 11:43:24 +08002747static int ocfs2_create_xattr_block(handle_t *handle,
2748 struct inode *inode,
2749 struct buffer_head *inode_bh,
2750 struct ocfs2_alloc_context *meta_ac,
Tao Maa7fe7a32009-08-18 11:43:52 +08002751 struct buffer_head **ret_bh,
2752 int indexed)
Tao Ma5aea1f02009-08-18 11:43:24 +08002753{
2754 int ret;
2755 u16 suballoc_bit_start;
2756 u32 num_got;
2757 u64 first_blkno;
2758 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2759 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2760 struct buffer_head *new_bh = NULL;
2761 struct ocfs2_xattr_block *xblk;
2762
2763 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2764 OCFS2_JOURNAL_ACCESS_CREATE);
2765 if (ret < 0) {
2766 mlog_errno(ret);
2767 goto end;
2768 }
2769
2770 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2771 &suballoc_bit_start, &num_got,
2772 &first_blkno);
2773 if (ret < 0) {
2774 mlog_errno(ret);
2775 goto end;
2776 }
2777
2778 new_bh = sb_getblk(inode->i_sb, first_blkno);
2779 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2780
2781 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2782 new_bh,
2783 OCFS2_JOURNAL_ACCESS_CREATE);
2784 if (ret < 0) {
2785 mlog_errno(ret);
2786 goto end;
2787 }
2788
2789 /* Initialize ocfs2_xattr_block */
2790 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2791 memset(xblk, 0, inode->i_sb->s_blocksize);
2792 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
Tiger Yangb89c5422010-01-25 14:11:06 +08002793 xblk->xb_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
Tao Ma5aea1f02009-08-18 11:43:24 +08002794 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2795 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2796 xblk->xb_blkno = cpu_to_le64(first_blkno);
2797
Tao Maa7fe7a32009-08-18 11:43:52 +08002798 if (indexed) {
2799 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2800 xr->xt_clusters = cpu_to_le32(1);
2801 xr->xt_last_eb_blk = 0;
2802 xr->xt_list.l_tree_depth = 0;
2803 xr->xt_list.l_count = cpu_to_le16(
2804 ocfs2_xattr_recs_per_xb(inode->i_sb));
2805 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2806 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2807 }
2808
Tao Ma5aea1f02009-08-18 11:43:24 +08002809 ret = ocfs2_journal_dirty(handle, new_bh);
2810 if (ret < 0) {
2811 mlog_errno(ret);
2812 goto end;
2813 }
2814 di->i_xattr_loc = cpu_to_le64(first_blkno);
2815 ocfs2_journal_dirty(handle, inode_bh);
2816
2817 *ret_bh = new_bh;
2818 new_bh = NULL;
2819
2820end:
2821 brelse(new_bh);
2822 return ret;
2823}
2824
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002825/*
2826 * ocfs2_xattr_block_set()
2827 *
2828 * Set, replace or remove an extended attribute into external block.
2829 *
2830 */
2831static int ocfs2_xattr_block_set(struct inode *inode,
2832 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002833 struct ocfs2_xattr_search *xs,
2834 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002835{
2836 struct buffer_head *new_bh = NULL;
Tao Ma85db90e2008-11-12 08:27:01 +08002837 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002838 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002839 int ret;
2840
2841 if (!xs->xattr_bh) {
Tao Ma5aea1f02009-08-18 11:43:24 +08002842 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
Tao Maa7fe7a32009-08-18 11:43:52 +08002843 ctxt->meta_ac, &new_bh, 0);
Tao Ma5aea1f02009-08-18 11:43:24 +08002844 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002845 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002846 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002847 }
2848
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002849 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002850 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002851 xs->header = &xblk->xb_attrs.xb_header;
2852 xs->base = (void *)xs->header;
2853 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2854 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002855 } else
2856 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2857
2858 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2859 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002860 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2861 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002862 if (!ret || ret != -ENOSPC)
2863 goto end;
2864
Tao Ma78f30c32008-11-12 08:27:00 +08002865 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002866 if (ret)
2867 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002868 }
2869
Tao Ma78f30c32008-11-12 08:27:00 +08002870 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002871
2872end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002873
2874 return ret;
2875}
2876
Tao Ma78f30c32008-11-12 08:27:00 +08002877/* Check whether the new xattr can be inserted into the inode. */
2878static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2879 struct ocfs2_xattr_info *xi,
2880 struct ocfs2_xattr_search *xs)
2881{
Tao Ma78f30c32008-11-12 08:27:00 +08002882 struct ocfs2_xattr_entry *last;
2883 int free, i;
2884 size_t min_offs = xs->end - xs->base;
2885
2886 if (!xs->header)
2887 return 0;
2888
2889 last = xs->header->xh_entries;
2890
2891 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2892 size_t offs = le16_to_cpu(last->xe_name_offset);
2893 if (offs < min_offs)
2894 min_offs = offs;
2895 last += 1;
2896 }
2897
Tiger Yang4442f512009-02-20 11:11:50 +08002898 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002899 if (free < 0)
2900 return 0;
2901
2902 BUG_ON(!xs->not_found);
2903
Joel Becker199799a2009-08-14 19:04:15 -07002904 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
Tao Ma78f30c32008-11-12 08:27:00 +08002905 return 1;
2906
2907 return 0;
2908}
2909
2910static int ocfs2_calc_xattr_set_need(struct inode *inode,
2911 struct ocfs2_dinode *di,
2912 struct ocfs2_xattr_info *xi,
2913 struct ocfs2_xattr_search *xis,
2914 struct ocfs2_xattr_search *xbs,
2915 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002916 int *meta_need,
2917 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002918{
2919 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002920 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002921 struct buffer_head *bh = NULL;
2922 struct ocfs2_xattr_block *xb = NULL;
2923 struct ocfs2_xattr_entry *xe = NULL;
2924 struct ocfs2_xattr_value_root *xv = NULL;
2925 char *base = NULL;
2926 int name_offset, name_len = 0;
2927 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Joel Becker6b240ff2009-08-14 18:02:52 -07002928 xi->xi_value_len);
Tao Ma78f30c32008-11-12 08:27:00 +08002929 u64 value_size;
2930
Tao Ma71d548a2008-12-05 06:20:54 +08002931 /*
2932 * Calculate the clusters we need to write.
2933 * No matter whether we replace an old one or add a new one,
2934 * we need this for writing.
2935 */
Joel Becker6b240ff2009-08-14 18:02:52 -07002936 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma71d548a2008-12-05 06:20:54 +08002937 credits += new_clusters *
2938 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2939
Tao Ma78f30c32008-11-12 08:27:00 +08002940 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002941 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2942
Joel Becker6b240ff2009-08-14 18:02:52 -07002943 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002944 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002945 credits += ocfs2_calc_extend_credits(inode->i_sb,
2946 &def_xv.xv.xr_list,
2947 new_clusters);
2948 }
Tao Ma78f30c32008-11-12 08:27:00 +08002949
2950 goto meta_guess;
2951 }
2952
2953 if (!xis->not_found) {
2954 xe = xis->here;
2955 name_offset = le16_to_cpu(xe->xe_name_offset);
2956 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2957 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002958 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002959 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002960 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002961 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2962 xe = xbs->here;
2963 name_offset = le16_to_cpu(xe->xe_name_offset);
2964 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2965 i = xbs->here - xbs->header->xh_entries;
2966 old_in_xb = 1;
2967
2968 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08002969 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08002970 bucket_xh(xbs->bucket),
2971 i, &block_off,
2972 &name_offset);
2973 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002974 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2975 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002976 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002977 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2978 }
2979 }
2980
2981 /*
2982 * delete a xattr doesn't need metadata and cluster allocation.
2983 * so just calculate the credits and return.
2984 *
2985 * The credits for removing the value tree will be extended
2986 * by ocfs2_remove_extent itself.
2987 */
Joel Becker6b240ff2009-08-14 18:02:52 -07002988 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08002989 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002990 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002991
2992 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002993 }
2994
2995 /* do cluster allocation guess first. */
2996 value_size = le64_to_cpu(xe->xe_value_size);
2997
2998 if (old_in_xb) {
2999 /*
3000 * In xattr set, we always try to set the xe in inode first,
3001 * so if it can be inserted into inode successfully, the old
3002 * one will be removed from the xattr block, and this xattr
3003 * will be inserted into inode as a new xattr in inode.
3004 */
3005 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3006 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02003007 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08003008 OCFS2_INODE_UPDATE_CREDITS;
3009 if (!ocfs2_xattr_is_local(xe))
3010 credits += ocfs2_calc_extend_credits(
3011 inode->i_sb,
3012 &def_xv.xv.xr_list,
3013 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08003014 goto out;
3015 }
3016 }
3017
Joel Becker6b240ff2009-08-14 18:02:52 -07003018 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003019 /* the new values will be stored outside. */
3020 u32 old_clusters = 0;
3021
3022 if (!ocfs2_xattr_is_local(xe)) {
3023 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3024 value_size);
3025 xv = (struct ocfs2_xattr_value_root *)
3026 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08003027 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08003028 } else
3029 xv = &def_xv.xv;
3030
Tao Ma85db90e2008-11-12 08:27:01 +08003031 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02003032 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003033 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08003034 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003035 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3036 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003037 credits += ocfs2_calc_extend_credits(inode->i_sb,
3038 &xv->xr_list,
3039 new_clusters -
3040 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08003041 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3042 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003043 }
3044 } else {
3045 /*
3046 * Now the new value will be stored inside. So if the new
3047 * value is smaller than the size of value root or the old
3048 * value, we don't need any allocation, otherwise we have
3049 * to guess metadata allocation.
3050 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003051 if ((ocfs2_xattr_is_local(xe) &&
3052 (value_size >= xi->xi_value_len)) ||
Tao Ma78f30c32008-11-12 08:27:00 +08003053 (!ocfs2_xattr_is_local(xe) &&
Joel Becker6b240ff2009-08-14 18:02:52 -07003054 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
Tao Ma78f30c32008-11-12 08:27:00 +08003055 goto out;
3056 }
3057
3058meta_guess:
3059 /* calculate metadata allocation. */
3060 if (di->i_xattr_loc) {
3061 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08003062 ret = ocfs2_read_xattr_block(inode,
3063 le64_to_cpu(di->i_xattr_loc),
3064 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08003065 if (ret) {
3066 mlog_errno(ret);
3067 goto out;
3068 }
3069
3070 xb = (struct ocfs2_xattr_block *)bh->b_data;
3071 } else
3072 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3073
Tao Ma90cb5462008-12-05 06:20:56 +08003074 /*
3075 * If there is already an xattr tree, good, we can calculate
3076 * like other b-trees. Otherwise we may have the chance of
3077 * create a tree, the credit calculation is borrowed from
3078 * ocfs2_calc_extend_credits with root_el = NULL. And the
3079 * new tree will be cluster based, so no meta is needed.
3080 */
Tao Ma78f30c32008-11-12 08:27:00 +08003081 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3082 struct ocfs2_extent_list *el =
3083 &xb->xb_attrs.xb_root.xt_list;
3084 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08003085 credits += ocfs2_calc_extend_credits(inode->i_sb,
3086 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08003087 } else
3088 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08003089
3090 /*
3091 * This cluster will be used either for new bucket or for
3092 * new xattr block.
3093 * If the cluster size is the same as the bucket size, one
3094 * more is needed since we may need to extend the bucket
3095 * also.
3096 */
3097 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003098 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003099 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08003100 OCFS2_SB(inode->i_sb)->s_clustersize) {
3101 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003102 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003103 }
3104 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003105 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003106 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3107 }
Tao Ma78f30c32008-11-12 08:27:00 +08003108out:
3109 if (clusters_need)
3110 *clusters_need = clusters_add;
3111 if (meta_need)
3112 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08003113 if (credits_need)
3114 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08003115 brelse(bh);
3116 return ret;
3117}
3118
3119static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3120 struct ocfs2_dinode *di,
3121 struct ocfs2_xattr_info *xi,
3122 struct ocfs2_xattr_search *xis,
3123 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003124 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08003125 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08003126 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08003127{
3128 int clusters_add, meta_add, ret;
3129 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3130
3131 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3132
3133 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3134
3135 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003136 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003137 if (ret) {
3138 mlog_errno(ret);
3139 return ret;
3140 }
3141
Tao Ma492a8a32009-08-18 11:43:17 +08003142 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08003143 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
Joel Becker6b240ff2009-08-14 18:02:52 -07003144 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003145
3146 if (meta_add) {
3147 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3148 &ctxt->meta_ac);
3149 if (ret) {
3150 mlog_errno(ret);
3151 goto out;
3152 }
3153 }
3154
3155 if (clusters_add) {
3156 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3157 if (ret)
3158 mlog_errno(ret);
3159 }
3160out:
3161 if (ret) {
3162 if (ctxt->meta_ac) {
3163 ocfs2_free_alloc_context(ctxt->meta_ac);
3164 ctxt->meta_ac = NULL;
3165 }
3166
3167 /*
3168 * We cannot have an error and a non null ctxt->data_ac.
3169 */
3170 }
3171
3172 return ret;
3173}
3174
Tao Ma85db90e2008-11-12 08:27:01 +08003175static int __ocfs2_xattr_set_handle(struct inode *inode,
3176 struct ocfs2_dinode *di,
3177 struct ocfs2_xattr_info *xi,
3178 struct ocfs2_xattr_search *xis,
3179 struct ocfs2_xattr_search *xbs,
3180 struct ocfs2_xattr_set_ctxt *ctxt)
3181{
Tao Ma9f868f12008-11-19 16:48:42 +08003182 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003183
Joel Becker6b240ff2009-08-14 18:02:52 -07003184 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003185 /* Remove existing extended attribute */
3186 if (!xis->not_found)
3187 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3188 else if (!xbs->not_found)
3189 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3190 } else {
3191 /* We always try to set extended attribute into inode first*/
3192 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3193 if (!ret && !xbs->not_found) {
3194 /*
3195 * If succeed and that extended attribute existing in
3196 * external block, then we will remove it.
3197 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003198 xi->xi_value = NULL;
3199 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003200
Tao Ma9f868f12008-11-19 16:48:42 +08003201 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003202 xis->not_found = -ENODATA;
3203 ret = ocfs2_calc_xattr_set_need(inode,
3204 di,
3205 xi,
3206 xis,
3207 xbs,
3208 NULL,
3209 NULL,
3210 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003211 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003212 if (ret) {
3213 mlog_errno(ret);
3214 goto out;
3215 }
3216
3217 ret = ocfs2_extend_trans(ctxt->handle, credits +
3218 ctxt->handle->h_buffer_credits);
3219 if (ret) {
3220 mlog_errno(ret);
3221 goto out;
3222 }
3223 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3224 } else if (ret == -ENOSPC) {
3225 if (di->i_xattr_loc && !xbs->xattr_bh) {
3226 ret = ocfs2_xattr_block_find(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07003227 xi->xi_name_index,
3228 xi->xi_name, xbs);
Tao Ma85db90e2008-11-12 08:27:01 +08003229 if (ret)
3230 goto out;
3231
Tao Ma9f868f12008-11-19 16:48:42 +08003232 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003233 xis->not_found = -ENODATA;
3234 ret = ocfs2_calc_xattr_set_need(inode,
3235 di,
3236 xi,
3237 xis,
3238 xbs,
3239 NULL,
3240 NULL,
3241 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003242 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003243 if (ret) {
3244 mlog_errno(ret);
3245 goto out;
3246 }
3247
3248 ret = ocfs2_extend_trans(ctxt->handle, credits +
3249 ctxt->handle->h_buffer_credits);
3250 if (ret) {
3251 mlog_errno(ret);
3252 goto out;
3253 }
3254 }
3255 /*
3256 * If no space in inode, we will set extended attribute
3257 * into external block.
3258 */
3259 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3260 if (ret)
3261 goto out;
3262 if (!xis->not_found) {
3263 /*
3264 * If succeed and that extended attribute
3265 * existing in inode, we will remove it.
3266 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003267 xi->xi_value = NULL;
3268 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003269 xbs->not_found = -ENODATA;
3270 ret = ocfs2_calc_xattr_set_need(inode,
3271 di,
3272 xi,
3273 xis,
3274 xbs,
3275 NULL,
3276 NULL,
3277 &credits);
3278 if (ret) {
3279 mlog_errno(ret);
3280 goto out;
3281 }
3282
3283 ret = ocfs2_extend_trans(ctxt->handle, credits +
3284 ctxt->handle->h_buffer_credits);
3285 if (ret) {
3286 mlog_errno(ret);
3287 goto out;
3288 }
3289 ret = ocfs2_xattr_ibody_set(inode, xi,
3290 xis, ctxt);
3291 }
3292 }
3293 }
3294
Tao Ma4b3f6202008-12-05 06:20:55 +08003295 if (!ret) {
3296 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08003297 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08003298 xis->inode_bh,
3299 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08003300 if (ret) {
3301 mlog_errno(ret);
3302 goto out;
3303 }
3304
3305 inode->i_ctime = CURRENT_TIME;
3306 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3307 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3308 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3309 }
Tao Ma85db90e2008-11-12 08:27:01 +08003310out:
3311 return ret;
3312}
3313
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003314/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08003315 * This function only called duing creating inode
3316 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08003317 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08003318 */
3319int ocfs2_xattr_set_handle(handle_t *handle,
3320 struct inode *inode,
3321 struct buffer_head *di_bh,
3322 int name_index,
3323 const char *name,
3324 const void *value,
3325 size_t value_len,
3326 int flags,
3327 struct ocfs2_alloc_context *meta_ac,
3328 struct ocfs2_alloc_context *data_ac)
3329{
3330 struct ocfs2_dinode *di;
3331 int ret;
3332
3333 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003334 .xi_name_index = name_index,
3335 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003336 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003337 .xi_value = value,
3338 .xi_value_len = value_len,
Tiger Yang6c3faba2008-11-14 11:16:03 +08003339 };
3340
3341 struct ocfs2_xattr_search xis = {
3342 .not_found = -ENODATA,
3343 };
3344
3345 struct ocfs2_xattr_search xbs = {
3346 .not_found = -ENODATA,
3347 };
3348
3349 struct ocfs2_xattr_set_ctxt ctxt = {
3350 .handle = handle,
3351 .meta_ac = meta_ac,
3352 .data_ac = data_ac,
3353 };
3354
3355 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3356 return -EOPNOTSUPP;
3357
Tiger Yang008aafa2008-12-09 16:43:08 +08003358 /*
3359 * In extreme situation, may need xattr bucket when
3360 * block size is too small. And we have already reserved
3361 * the credits for bucket in mknod.
3362 */
3363 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3364 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3365 if (!xbs.bucket) {
3366 mlog_errno(-ENOMEM);
3367 return -ENOMEM;
3368 }
3369 }
3370
Tiger Yang6c3faba2008-11-14 11:16:03 +08003371 xis.inode_bh = xbs.inode_bh = di_bh;
3372 di = (struct ocfs2_dinode *)di_bh->b_data;
3373
3374 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3375
3376 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3377 if (ret)
3378 goto cleanup;
3379 if (xis.not_found) {
3380 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3381 if (ret)
3382 goto cleanup;
3383 }
3384
3385 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3386
3387cleanup:
3388 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3389 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08003390 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08003391
3392 return ret;
3393}
3394
3395/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003396 * ocfs2_xattr_set()
3397 *
3398 * Set, replace or remove an extended attribute for this inode.
3399 * value is NULL to remove an existing extended attribute, else either
3400 * create or replace an extended attribute.
3401 */
3402int ocfs2_xattr_set(struct inode *inode,
3403 int name_index,
3404 const char *name,
3405 const void *value,
3406 size_t value_len,
3407 int flags)
3408{
3409 struct buffer_head *di_bh = NULL;
3410 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08003411 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003412 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003413 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08003414 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08003415 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003416
3417 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003418 .xi_name_index = name_index,
3419 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003420 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003421 .xi_value = value,
3422 .xi_value_len = value_len,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003423 };
3424
3425 struct ocfs2_xattr_search xis = {
3426 .not_found = -ENODATA,
3427 };
3428
3429 struct ocfs2_xattr_search xbs = {
3430 .not_found = -ENODATA,
3431 };
3432
Tiger Yang8154da32008-08-18 17:11:46 +08003433 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3434 return -EOPNOTSUPP;
3435
Joel Beckerba937122008-10-24 19:13:20 -07003436 /*
3437 * Only xbs will be used on indexed trees. xis doesn't need a
3438 * bucket.
3439 */
3440 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3441 if (!xbs.bucket) {
3442 mlog_errno(-ENOMEM);
3443 return -ENOMEM;
3444 }
3445
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003446 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3447 if (ret < 0) {
3448 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003449 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003450 }
3451 xis.inode_bh = xbs.inode_bh = di_bh;
3452 di = (struct ocfs2_dinode *)di_bh->b_data;
3453
3454 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3455 /*
3456 * Scan inode and external block to find the same name
3457 * extended attribute and collect search infomation.
3458 */
3459 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3460 if (ret)
3461 goto cleanup;
3462 if (xis.not_found) {
3463 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3464 if (ret)
3465 goto cleanup;
3466 }
3467
3468 if (xis.not_found && xbs.not_found) {
3469 ret = -ENODATA;
3470 if (flags & XATTR_REPLACE)
3471 goto cleanup;
3472 ret = 0;
3473 if (!value)
3474 goto cleanup;
3475 } else {
3476 ret = -EEXIST;
3477 if (flags & XATTR_CREATE)
3478 goto cleanup;
3479 }
3480
Tao Ma492a8a32009-08-18 11:43:17 +08003481 /* Check whether the value is refcounted and do some prepartion. */
3482 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3483 (!xis.not_found || !xbs.not_found)) {
3484 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3485 &xis, &xbs, &ref_tree,
3486 &ref_meta, &ref_credits);
3487 if (ret) {
3488 mlog_errno(ret);
3489 goto cleanup;
3490 }
3491 }
Tao Ma85db90e2008-11-12 08:27:01 +08003492
3493 mutex_lock(&tl_inode->i_mutex);
3494
3495 if (ocfs2_truncate_log_needs_flush(osb)) {
3496 ret = __ocfs2_flush_truncate_log(osb);
3497 if (ret < 0) {
3498 mutex_unlock(&tl_inode->i_mutex);
3499 mlog_errno(ret);
3500 goto cleanup;
3501 }
3502 }
3503 mutex_unlock(&tl_inode->i_mutex);
3504
3505 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003506 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003507 if (ret) {
3508 mlog_errno(ret);
3509 goto cleanup;
3510 }
3511
Tao Ma4b3f6202008-12-05 06:20:55 +08003512 /* we need to update inode's ctime field, so add credit for it. */
3513 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003514 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003515 if (IS_ERR(ctxt.handle)) {
3516 ret = PTR_ERR(ctxt.handle);
3517 mlog_errno(ret);
3518 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003519 }
Tao Ma85db90e2008-11-12 08:27:01 +08003520
3521 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3522
3523 ocfs2_commit_trans(osb, ctxt.handle);
3524
Tao Ma78f30c32008-11-12 08:27:00 +08003525 if (ctxt.data_ac)
3526 ocfs2_free_alloc_context(ctxt.data_ac);
3527 if (ctxt.meta_ac)
3528 ocfs2_free_alloc_context(ctxt.meta_ac);
3529 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3530 ocfs2_schedule_truncate_log_flush(osb, 1);
3531 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003532
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003533cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003534 if (ref_tree)
3535 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003536 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003537 if (!value && !ret) {
3538 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3539 if (ret)
3540 mlog_errno(ret);
3541 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003542 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003543cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003544 brelse(di_bh);
3545 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003546 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003547
3548 return ret;
3549}
3550
Tao Ma0c044f02008-08-18 17:38:50 +08003551/*
3552 * Find the xattr extent rec which may contains name_hash.
3553 * e_cpos will be the first name hash of the xattr rec.
3554 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3555 */
3556static int ocfs2_xattr_get_rec(struct inode *inode,
3557 u32 name_hash,
3558 u64 *p_blkno,
3559 u32 *e_cpos,
3560 u32 *num_clusters,
3561 struct ocfs2_extent_list *el)
3562{
3563 int ret = 0, i;
3564 struct buffer_head *eb_bh = NULL;
3565 struct ocfs2_extent_block *eb;
3566 struct ocfs2_extent_rec *rec = NULL;
3567 u64 e_blkno = 0;
3568
3569 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003570 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3571 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003572 if (ret) {
3573 mlog_errno(ret);
3574 goto out;
3575 }
3576
3577 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3578 el = &eb->h_list;
3579
3580 if (el->l_tree_depth) {
3581 ocfs2_error(inode->i_sb,
3582 "Inode %lu has non zero tree depth in "
3583 "xattr tree block %llu\n", inode->i_ino,
3584 (unsigned long long)eb_bh->b_blocknr);
3585 ret = -EROFS;
3586 goto out;
3587 }
3588 }
3589
3590 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3591 rec = &el->l_recs[i];
3592
3593 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3594 e_blkno = le64_to_cpu(rec->e_blkno);
3595 break;
3596 }
3597 }
3598
3599 if (!e_blkno) {
3600 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3601 "record (%u, %u, 0) in xattr", inode->i_ino,
3602 le32_to_cpu(rec->e_cpos),
3603 ocfs2_rec_clusters(el, rec));
3604 ret = -EROFS;
3605 goto out;
3606 }
3607
3608 *p_blkno = le64_to_cpu(rec->e_blkno);
3609 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3610 if (e_cpos)
3611 *e_cpos = le32_to_cpu(rec->e_cpos);
3612out:
3613 brelse(eb_bh);
3614 return ret;
3615}
3616
3617typedef int (xattr_bucket_func)(struct inode *inode,
3618 struct ocfs2_xattr_bucket *bucket,
3619 void *para);
3620
Tao Ma589dc262008-08-18 17:38:51 +08003621static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003622 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003623 int name_index,
3624 const char *name,
3625 u32 name_hash,
3626 u16 *xe_index,
3627 int *found)
3628{
3629 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003630 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003631 size_t name_len = strlen(name);
3632 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003633 char *xe_name;
3634
3635 /*
3636 * We don't use binary search in the bucket because there
3637 * may be multiple entries with the same name hash.
3638 */
3639 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3640 xe = &xh->xh_entries[i];
3641
3642 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3643 continue;
3644 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3645 break;
3646
3647 cmp = name_index - ocfs2_xattr_get_type(xe);
3648 if (!cmp)
3649 cmp = name_len - xe->xe_name_len;
3650 if (cmp)
3651 continue;
3652
Tao Mafd68a892009-08-18 11:43:21 +08003653 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003654 xh,
3655 i,
3656 &block_off,
3657 &new_offset);
3658 if (ret) {
3659 mlog_errno(ret);
3660 break;
3661 }
3662
Joel Becker970e4932008-11-13 14:49:19 -08003663
Joel Beckere2356a32008-10-27 15:01:54 -07003664 xe_name = bucket_block(bucket, block_off) + new_offset;
3665 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003666 *xe_index = i;
3667 *found = 1;
3668 ret = 0;
3669 break;
3670 }
3671 }
3672
3673 return ret;
3674}
3675
3676/*
3677 * Find the specified xattr entry in a series of buckets.
3678 * This series start from p_blkno and last for num_clusters.
3679 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3680 * the num of the valid buckets.
3681 *
3682 * Return the buffer_head this xattr should reside in. And if the xattr's
3683 * hash is in the gap of 2 buckets, return the lower bucket.
3684 */
3685static int ocfs2_xattr_bucket_find(struct inode *inode,
3686 int name_index,
3687 const char *name,
3688 u32 name_hash,
3689 u64 p_blkno,
3690 u32 first_hash,
3691 u32 num_clusters,
3692 struct ocfs2_xattr_search *xs)
3693{
3694 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003695 struct ocfs2_xattr_header *xh = NULL;
3696 struct ocfs2_xattr_entry *xe = NULL;
3697 u16 index = 0;
3698 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3699 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003700 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003701 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003702 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003703
Joel Beckere2356a32008-10-27 15:01:54 -07003704 search = ocfs2_xattr_bucket_new(inode);
3705 if (!search) {
3706 ret = -ENOMEM;
3707 mlog_errno(ret);
3708 goto out;
3709 }
3710
3711 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003712 if (ret) {
3713 mlog_errno(ret);
3714 goto out;
3715 }
3716
Joel Beckere2356a32008-10-27 15:01:54 -07003717 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003718 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003719 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003720 ocfs2_xattr_bucket_relse(search);
3721
Tao Ma589dc262008-08-18 17:38:51 +08003722 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003723 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003724 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003725 if (ret) {
3726 mlog_errno(ret);
3727 goto out;
3728 }
3729
Joel Beckere2356a32008-10-27 15:01:54 -07003730 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003731 xe = &xh->xh_entries[0];
3732 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3733 high_bucket = bucket - 1;
3734 continue;
3735 }
3736
3737 /*
3738 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003739 * bucket is larger than the search one. for an empty
3740 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003741 */
Tao Ma5a095612008-09-19 22:17:41 +08003742 if (xh->xh_count)
3743 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3744
Tao Ma589dc262008-08-18 17:38:51 +08003745 last_hash = le32_to_cpu(xe->xe_name_hash);
3746
Joel Beckere2356a32008-10-27 15:01:54 -07003747 /* record lower_blkno which may be the insert place. */
3748 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003749
3750 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3751 low_bucket = bucket + 1;
3752 continue;
3753 }
3754
3755 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003756 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003757 name_index, name, name_hash,
3758 &index, &found);
3759 if (ret) {
3760 mlog_errno(ret);
3761 goto out;
3762 }
3763 break;
3764 }
3765
3766 /*
3767 * Record the bucket we have found.
3768 * When the xattr's hash value is in the gap of 2 buckets, we will
3769 * always set it to the previous bucket.
3770 */
Joel Beckere2356a32008-10-27 15:01:54 -07003771 if (!lower_blkno)
3772 lower_blkno = p_blkno;
3773
3774 /* This should be in cache - we just read it during the search */
3775 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3776 if (ret) {
3777 mlog_errno(ret);
3778 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003779 }
Tao Ma589dc262008-08-18 17:38:51 +08003780
Joel Beckerba937122008-10-24 19:13:20 -07003781 xs->header = bucket_xh(xs->bucket);
3782 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003783 xs->end = xs->base + inode->i_sb->s_blocksize;
3784
3785 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003786 xs->here = &xs->header->xh_entries[index];
3787 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003788 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003789 } else
3790 ret = -ENODATA;
3791
3792out:
Joel Beckere2356a32008-10-27 15:01:54 -07003793 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003794 return ret;
3795}
3796
3797static int ocfs2_xattr_index_block_find(struct inode *inode,
3798 struct buffer_head *root_bh,
3799 int name_index,
3800 const char *name,
3801 struct ocfs2_xattr_search *xs)
3802{
3803 int ret;
3804 struct ocfs2_xattr_block *xb =
3805 (struct ocfs2_xattr_block *)root_bh->b_data;
3806 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3807 struct ocfs2_extent_list *el = &xb_root->xt_list;
3808 u64 p_blkno = 0;
3809 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003810 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003811
3812 if (le16_to_cpu(el->l_next_free_rec) == 0)
3813 return -ENODATA;
3814
3815 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3816 name, name_hash, name_index);
3817
3818 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3819 &num_clusters, el);
3820 if (ret) {
3821 mlog_errno(ret);
3822 goto out;
3823 }
3824
3825 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3826
3827 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003828 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3829 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003830
3831 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3832 p_blkno, first_hash, num_clusters, xs);
3833
3834out:
3835 return ret;
3836}
3837
Tao Ma0c044f02008-08-18 17:38:50 +08003838static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3839 u64 blkno,
3840 u32 clusters,
3841 xattr_bucket_func *func,
3842 void *para)
3843{
Joel Becker6dde41d2008-10-24 17:16:48 -07003844 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003845 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3846 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003847 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003848
Joel Beckerba937122008-10-24 19:13:20 -07003849 bucket = ocfs2_xattr_bucket_new(inode);
3850 if (!bucket) {
3851 mlog_errno(-ENOMEM);
3852 return -ENOMEM;
3853 }
Tao Ma0c044f02008-08-18 17:38:50 +08003854
3855 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003856 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003857
Joel Beckerba937122008-10-24 19:13:20 -07003858 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3859 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003860 if (ret) {
3861 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003862 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003863 }
3864
Tao Ma0c044f02008-08-18 17:38:50 +08003865 /*
3866 * The real bucket num in this series of blocks is stored
3867 * in the 1st bucket.
3868 */
3869 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003870 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003871
Mark Fashehde29c082008-10-29 14:45:30 -07003872 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3873 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003874 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003875 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003876 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003877 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003878 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003879 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003880 }
3881
Joel Beckerba937122008-10-24 19:13:20 -07003882 ocfs2_xattr_bucket_relse(bucket);
3883 if (ret)
3884 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003885 }
3886
Joel Beckerba937122008-10-24 19:13:20 -07003887 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003888 return ret;
3889}
3890
3891struct ocfs2_xattr_tree_list {
3892 char *buffer;
3893 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003894 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003895};
3896
Tao Mafd68a892009-08-18 11:43:21 +08003897static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08003898 struct ocfs2_xattr_header *xh,
3899 int index,
3900 int *block_off,
3901 int *new_offset)
3902{
3903 u16 name_offset;
3904
3905 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3906 return -EINVAL;
3907
3908 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3909
Tao Mafd68a892009-08-18 11:43:21 +08003910 *block_off = name_offset >> sb->s_blocksize_bits;
3911 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08003912
3913 return 0;
3914}
3915
3916static int ocfs2_list_xattr_bucket(struct inode *inode,
3917 struct ocfs2_xattr_bucket *bucket,
3918 void *para)
3919{
Tao Ma936b8832008-10-09 23:06:14 +08003920 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003921 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003922 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003923 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003924
Joel Becker3e632942008-10-24 17:04:49 -07003925 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3926 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003927 type = ocfs2_xattr_get_type(entry);
3928 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003929
Tao Ma936b8832008-10-09 23:06:14 +08003930 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08003931 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07003932 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003933 i,
3934 &block_off,
3935 &new_offset);
3936 if (ret)
3937 break;
Tao Ma936b8832008-10-09 23:06:14 +08003938
Joel Becker51def392008-10-24 16:57:21 -07003939 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003940 new_offset;
3941 ret = ocfs2_xattr_list_entry(xl->buffer,
3942 xl->buffer_size,
3943 &xl->result,
3944 prefix, name,
3945 entry->xe_name_len);
3946 if (ret)
3947 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003948 }
3949 }
3950
3951 return ret;
3952}
3953
Tao Ma47bca492009-08-18 11:43:42 +08003954static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3955 struct buffer_head *blk_bh,
3956 xattr_tree_rec_func *rec_func,
3957 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08003958{
Tao Ma47bca492009-08-18 11:43:42 +08003959 struct ocfs2_xattr_block *xb =
3960 (struct ocfs2_xattr_block *)blk_bh->b_data;
3961 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08003962 int ret = 0;
3963 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3964 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003965
Tao Ma47bca492009-08-18 11:43:42 +08003966 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08003967 return 0;
3968
3969 while (name_hash > 0) {
3970 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3971 &e_cpos, &num_clusters, el);
3972 if (ret) {
3973 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003974 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003975 }
3976
Tao Ma47bca492009-08-18 11:43:42 +08003977 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3978 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08003979 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08003980 if (ret != -ERANGE)
3981 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003982 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003983 }
3984
3985 if (e_cpos == 0)
3986 break;
3987
3988 name_hash = e_cpos - 1;
3989 }
3990
Tao Ma47bca492009-08-18 11:43:42 +08003991 return ret;
3992
3993}
3994
3995static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3996 struct buffer_head *root_bh,
3997 u64 blkno, u32 cpos, u32 len, void *para)
3998{
3999 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4000 ocfs2_list_xattr_bucket, para);
4001}
4002
4003static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4004 struct buffer_head *blk_bh,
4005 char *buffer,
4006 size_t buffer_size)
4007{
4008 int ret;
4009 struct ocfs2_xattr_tree_list xl = {
4010 .buffer = buffer,
4011 .buffer_size = buffer_size,
4012 .result = 0,
4013 };
4014
4015 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4016 ocfs2_list_xattr_tree_rec, &xl);
4017 if (ret) {
4018 mlog_errno(ret);
4019 goto out;
4020 }
4021
Tao Ma936b8832008-10-09 23:06:14 +08004022 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08004023out:
4024 return ret;
4025}
Tao Ma01225592008-08-18 17:38:53 +08004026
4027static int cmp_xe(const void *a, const void *b)
4028{
4029 const struct ocfs2_xattr_entry *l = a, *r = b;
4030 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4031 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4032
4033 if (l_hash > r_hash)
4034 return 1;
4035 if (l_hash < r_hash)
4036 return -1;
4037 return 0;
4038}
4039
4040static void swap_xe(void *a, void *b, int size)
4041{
4042 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4043
4044 tmp = *l;
4045 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4046 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4047}
4048
4049/*
4050 * When the ocfs2_xattr_block is filled up, new bucket will be created
4051 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07004052 * The header goes at the start of the bucket, and the names+values are
4053 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08004054 * Note: we need to sort the entries since they are not saved in order
4055 * in the ocfs2_xattr_block.
4056 */
4057static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4058 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07004059 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08004060{
4061 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07004062 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004063 u16 offset, size, off_change;
4064 struct ocfs2_xattr_entry *xe;
4065 struct ocfs2_xattr_block *xb =
4066 (struct ocfs2_xattr_block *)xb_bh->b_data;
4067 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004068 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004069 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07004070 char *src = xb_bh->b_data;
4071 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08004072
4073 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4074 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07004075 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08004076
Joel Becker178eeac2008-10-27 15:18:29 -07004077 for (i = 0; i < blks; i++)
4078 memset(bucket_block(bucket, i), 0, blocksize);
4079
Tao Ma01225592008-08-18 17:38:53 +08004080 /*
4081 * Since the xe_name_offset is based on ocfs2_xattr_header,
4082 * there is a offset change corresponding to the change of
4083 * ocfs2_xattr_header's position.
4084 */
4085 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4086 xe = &xb_xh->xh_entries[count - 1];
4087 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4088 size = blocksize - offset;
4089
4090 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08004091 memcpy(target + offset, src + offset, size);
4092
4093 /* Init new header now. */
4094 xh->xh_count = xb_xh->xh_count;
4095 xh->xh_num_buckets = cpu_to_le16(1);
4096 xh->xh_name_value_len = cpu_to_le16(size);
4097 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4098
4099 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07004100 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004101 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4102 size = count * sizeof(struct ocfs2_xattr_entry);
4103 memcpy(target + offset, (char *)xb_xh + offset, size);
4104
4105 /* Change the xe offset for all the xe because of the move. */
4106 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4107 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4108 for (i = 0; i < count; i++)
4109 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4110
4111 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4112 offset, size, off_change);
4113
4114 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4115 cmp_xe, swap_xe);
4116}
4117
4118/*
4119 * After we move xattr from block to index btree, we have to
4120 * update ocfs2_xattr_search to the new xe and base.
4121 *
4122 * When the entry is in xattr block, xattr_bh indicates the storage place.
4123 * While if the entry is in index b-tree, "bucket" indicates the
4124 * real place of the xattr.
4125 */
Joel Becker178eeac2008-10-27 15:18:29 -07004126static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4127 struct ocfs2_xattr_search *xs,
4128 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08004129{
Tao Ma01225592008-08-18 17:38:53 +08004130 char *buf = old_bh->b_data;
4131 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4132 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004133 int i;
Tao Ma01225592008-08-18 17:38:53 +08004134
Joel Beckerba937122008-10-24 19:13:20 -07004135 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07004136 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004137 xs->end = xs->base + inode->i_sb->s_blocksize;
4138
Joel Becker178eeac2008-10-27 15:18:29 -07004139 if (xs->not_found)
4140 return;
Tao Ma01225592008-08-18 17:38:53 +08004141
Joel Becker178eeac2008-10-27 15:18:29 -07004142 i = xs->here - old_xh->xh_entries;
4143 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004144}
4145
4146static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004147 struct ocfs2_xattr_search *xs,
4148 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004149{
Tao Ma85db90e2008-11-12 08:27:01 +08004150 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004151 u32 bit_off, len;
4152 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08004153 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004154 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4155 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08004156 struct buffer_head *xb_bh = xs->xattr_bh;
4157 struct ocfs2_xattr_block *xb =
4158 (struct ocfs2_xattr_block *)xb_bh->b_data;
4159 struct ocfs2_xattr_tree_root *xr;
4160 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08004161
4162 mlog(0, "create xattr index block for %llu\n",
4163 (unsigned long long)xb_bh->b_blocknr);
4164
4165 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07004166 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004167
Tao Ma01225592008-08-18 17:38:53 +08004168 /*
4169 * XXX:
4170 * We can use this lock for now, and maybe move to a dedicated mutex
4171 * if performance becomes a problem later.
4172 */
4173 down_write(&oi->ip_alloc_sem);
4174
Joel Becker0cf2f762009-02-12 16:41:25 -08004175 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08004176 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004177 if (ret) {
4178 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004179 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004180 }
4181
Tao Ma78f30c32008-11-12 08:27:00 +08004182 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4183 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08004184 if (ret) {
4185 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004186 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004187 }
4188
4189 /*
4190 * The bucket may spread in many blocks, and
4191 * we will only touch the 1st block and the last block
4192 * in the whole bucket(one for entry and one for data).
4193 */
4194 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4195
Mark Fashehde29c082008-10-29 14:45:30 -07004196 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4197 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08004198
Joel Becker178eeac2008-10-27 15:18:29 -07004199 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004200 if (ret) {
4201 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004202 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004203 }
4204
Joel Becker178eeac2008-10-27 15:18:29 -07004205 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4206 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07004207 if (ret) {
4208 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004209 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07004210 }
Tao Ma01225592008-08-18 17:38:53 +08004211
Joel Becker178eeac2008-10-27 15:18:29 -07004212 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4213 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4214
4215 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4216
Tao Ma01225592008-08-18 17:38:53 +08004217 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4218 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4219 offsetof(struct ocfs2_xattr_block, xb_attrs));
4220
4221 xr = &xb->xb_attrs.xb_root;
4222 xr->xt_clusters = cpu_to_le32(1);
4223 xr->xt_last_eb_blk = 0;
4224 xr->xt_list.l_tree_depth = 0;
4225 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4226 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4227
4228 xr->xt_list.l_recs[0].e_cpos = 0;
4229 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4230 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4231
4232 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4233
Tao Ma85db90e2008-11-12 08:27:01 +08004234 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004235
Tao Ma85db90e2008-11-12 08:27:01 +08004236out:
Tao Ma01225592008-08-18 17:38:53 +08004237 up_write(&oi->ip_alloc_sem);
4238
Tao Ma01225592008-08-18 17:38:53 +08004239 return ret;
4240}
4241
4242static int cmp_xe_offset(const void *a, const void *b)
4243{
4244 const struct ocfs2_xattr_entry *l = a, *r = b;
4245 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4246 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4247
4248 if (l_name_offset < r_name_offset)
4249 return 1;
4250 if (l_name_offset > r_name_offset)
4251 return -1;
4252 return 0;
4253}
4254
4255/*
4256 * defrag a xattr bucket if we find that the bucket has some
4257 * holes beteen name/value pairs.
4258 * We will move all the name/value pairs to the end of the bucket
4259 * so that we can spare some space for insertion.
4260 */
4261static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004262 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004263 struct ocfs2_xattr_bucket *bucket)
4264{
4265 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004266 size_t end, offset, len;
Tao Ma01225592008-08-18 17:38:53 +08004267 struct ocfs2_xattr_header *xh;
4268 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07004269 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004270 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08004271 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08004272 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08004273
4274 /*
4275 * In order to make the operation more efficient and generic,
4276 * we copy all the blocks into a contiguous memory and do the
4277 * defragment there, so if anything is error, we will not touch
4278 * the real block.
4279 */
4280 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4281 if (!bucket_buf) {
4282 ret = -EIO;
4283 goto out;
4284 }
4285
Joel Becker161d6f32008-10-27 15:25:18 -07004286 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004287 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4288 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07004289
Tao Ma1c32a2f2008-11-06 08:10:47 +08004290 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07004291 OCFS2_JOURNAL_ACCESS_WRITE);
4292 if (ret < 0) {
4293 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004294 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004295 }
4296
4297 xh = (struct ocfs2_xattr_header *)bucket_buf;
4298 entries = (char *)xh->xh_entries;
4299 xh_free_start = le16_to_cpu(xh->xh_free_start);
4300
4301 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4302 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004303 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4304 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08004305
4306 /*
4307 * sort all the entries by their offset.
4308 * the largest will be the first, so that we can
4309 * move them to the end one by one.
4310 */
4311 sort(entries, le16_to_cpu(xh->xh_count),
4312 sizeof(struct ocfs2_xattr_entry),
4313 cmp_xe_offset, swap_xe);
4314
4315 /* Move all name/values to the end of the bucket. */
4316 xe = xh->xh_entries;
4317 end = OCFS2_XATTR_BUCKET_SIZE;
4318 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4319 offset = le16_to_cpu(xe->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07004320 len = namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004321
4322 /*
4323 * We must make sure that the name/value pair
4324 * exist in the same block. So adjust end to
4325 * the previous block end if needed.
4326 */
4327 if (((end - len) / blocksize !=
4328 (end - 1) / blocksize))
4329 end = end - end % blocksize;
4330
4331 if (end > offset + len) {
4332 memmove(bucket_buf + end - len,
4333 bucket_buf + offset, len);
4334 xe->xe_name_offset = cpu_to_le16(end - len);
4335 }
4336
4337 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4338 "bucket %llu\n", (unsigned long long)blkno);
4339
4340 end -= len;
4341 }
4342
4343 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4344 "bucket %llu\n", (unsigned long long)blkno);
4345
4346 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08004347 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004348
4349 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4350 xh->xh_free_start = cpu_to_le16(end);
4351
4352 /* sort the entries by their name_hash. */
4353 sort(entries, le16_to_cpu(xh->xh_count),
4354 sizeof(struct ocfs2_xattr_entry),
4355 cmp_xe, swap_xe);
4356
4357 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004358 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4359 memcpy(bucket_block(bucket, i), buf, blocksize);
4360 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004361
Tao Ma01225592008-08-18 17:38:53 +08004362out:
Tao Ma01225592008-08-18 17:38:53 +08004363 kfree(bucket_buf);
4364 return ret;
4365}
4366
4367/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08004368 * prev_blkno points to the start of an existing extent. new_blkno
4369 * points to a newly allocated extent. Because we know each of our
4370 * clusters contains more than bucket, we can easily split one cluster
4371 * at a bucket boundary. So we take the last cluster of the existing
4372 * extent and split it down the middle. We move the last half of the
4373 * buckets in the last cluster of the existing extent over to the new
4374 * extent.
Tao Ma01225592008-08-18 17:38:53 +08004375 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08004376 * first_bh is the buffer at prev_blkno so we can update the existing
4377 * extent's bucket count. header_bh is the bucket were we were hoping
4378 * to insert our xattr. If the bucket move places the target in the new
4379 * extent, we'll update first_bh and header_bh after modifying the old
4380 * extent.
4381 *
4382 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004383 */
4384static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4385 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004386 struct ocfs2_xattr_bucket *first,
4387 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004388 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08004389 u32 num_clusters,
4390 u32 *first_hash)
4391{
Joel Beckerc58b6032008-11-26 13:36:24 -08004392 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08004393 struct super_block *sb = inode->i_sb;
4394 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4395 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08004396 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08004397 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08004398 u64 last_cluster_blkno = bucket_blkno(first) +
4399 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004400
Joel Becker41cb8142008-11-26 14:25:21 -08004401 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4402 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08004403
Tao Ma01225592008-08-18 17:38:53 +08004404 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08004405 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004406
Joel Becker41cb8142008-11-26 14:25:21 -08004407 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08004408 last_cluster_blkno, new_blkno,
4409 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004410 if (ret) {
4411 mlog_errno(ret);
4412 goto out;
4413 }
4414
Joel Beckerc58b6032008-11-26 13:36:24 -08004415 /* This is the first bucket that got moved */
4416 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4417
Tao Ma01225592008-08-18 17:38:53 +08004418 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004419 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08004420 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08004421 */
Joel Becker41cb8142008-11-26 14:25:21 -08004422 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08004423 /* Find the block for the new target bucket */
4424 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08004425 (bucket_blkno(target) - src_blkno);
4426
4427 ocfs2_xattr_bucket_relse(first);
4428 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004429
4430 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004431 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08004432 * journal from ocfs2_cp_xattr_bucket().
4433 */
Joel Becker41cb8142008-11-26 14:25:21 -08004434 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08004435 if (ret) {
4436 mlog_errno(ret);
4437 goto out;
4438 }
Joel Becker41cb8142008-11-26 14:25:21 -08004439 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4440 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08004441 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004442
Joel Beckerb5c03e72008-11-25 19:58:16 -08004443 }
4444
Tao Ma01225592008-08-18 17:38:53 +08004445out:
Tao Ma01225592008-08-18 17:38:53 +08004446 return ret;
4447}
4448
Tao Ma01225592008-08-18 17:38:53 +08004449/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004450 * Find the suitable pos when we divide a bucket into 2.
4451 * We have to make sure the xattrs with the same hash value exist
4452 * in the same bucket.
4453 *
4454 * If this ocfs2_xattr_header covers more than one hash value, find a
4455 * place where the hash value changes. Try to find the most even split.
4456 * The most common case is that all entries have different hash values,
4457 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08004458 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004459static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4460{
4461 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4462 int count = le16_to_cpu(xh->xh_count);
4463 int delta, middle = count / 2;
4464
4465 /*
4466 * We start at the middle. Each step gets farther away in both
4467 * directions. We therefore hit the change in hash value
4468 * nearest to the middle. Note that this loop does not execute for
4469 * count < 2.
4470 */
4471 for (delta = 0; delta < middle; delta++) {
4472 /* Let's check delta earlier than middle */
4473 if (cmp_xe(&entries[middle - delta - 1],
4474 &entries[middle - delta]))
4475 return middle - delta;
4476
4477 /* For even counts, don't walk off the end */
4478 if ((middle + delta + 1) == count)
4479 continue;
4480
4481 /* Now try delta past middle */
4482 if (cmp_xe(&entries[middle + delta],
4483 &entries[middle + delta + 1]))
4484 return middle + delta + 1;
4485 }
4486
4487 /* Every entry had the same hash */
4488 return count;
4489}
4490
4491/*
4492 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4493 * first_hash will record the 1st hash of the new bucket.
4494 *
4495 * Normally half of the xattrs will be moved. But we have to make
4496 * sure that the xattrs with the same hash value are stored in the
4497 * same bucket. If all the xattrs in this bucket have the same hash
4498 * value, the new bucket will be initialized as an empty one and the
4499 * first_hash will be initialized as (hash_value+1).
4500 */
4501static int ocfs2_divide_xattr_bucket(struct inode *inode,
4502 handle_t *handle,
4503 u64 blk,
4504 u64 new_blk,
4505 u32 *first_hash,
4506 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004507{
4508 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004509 int count, start, len, name_value_len = 0, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004510 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004511 struct ocfs2_xattr_header *xh;
4512 struct ocfs2_xattr_entry *xe;
4513 int blocksize = inode->i_sb->s_blocksize;
4514
Tao Ma80bcaf32008-10-27 06:06:24 +08004515 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004516 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004517
Joel Beckerba937122008-10-24 19:13:20 -07004518 s_bucket = ocfs2_xattr_bucket_new(inode);
4519 t_bucket = ocfs2_xattr_bucket_new(inode);
4520 if (!s_bucket || !t_bucket) {
4521 ret = -ENOMEM;
4522 mlog_errno(ret);
4523 goto out;
4524 }
Tao Ma01225592008-08-18 17:38:53 +08004525
Joel Beckerba937122008-10-24 19:13:20 -07004526 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004527 if (ret) {
4528 mlog_errno(ret);
4529 goto out;
4530 }
4531
Joel Beckerba937122008-10-24 19:13:20 -07004532 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004533 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004534 if (ret) {
4535 mlog_errno(ret);
4536 goto out;
4537 }
4538
Joel Becker784b8162008-10-24 17:33:40 -07004539 /*
4540 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4541 * there's no need to read it.
4542 */
Joel Beckerba937122008-10-24 19:13:20 -07004543 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004544 if (ret) {
4545 mlog_errno(ret);
4546 goto out;
4547 }
4548
Joel Becker2b656c12008-11-25 19:00:15 -08004549 /*
4550 * Hey, if we're overwriting t_bucket, what difference does
4551 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4552 * same part of ocfs2_cp_xattr_bucket().
4553 */
Joel Beckerba937122008-10-24 19:13:20 -07004554 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004555 new_bucket_head ?
4556 OCFS2_JOURNAL_ACCESS_CREATE :
4557 OCFS2_JOURNAL_ACCESS_WRITE);
4558 if (ret) {
4559 mlog_errno(ret);
4560 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004561 }
4562
Joel Beckerba937122008-10-24 19:13:20 -07004563 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004564 count = le16_to_cpu(xh->xh_count);
4565 start = ocfs2_xattr_find_divide_pos(xh);
4566
4567 if (start == count) {
4568 xe = &xh->xh_entries[start-1];
4569
4570 /*
4571 * initialized a new empty bucket here.
4572 * The hash value is set as one larger than
4573 * that of the last entry in the previous bucket.
4574 */
Joel Beckerba937122008-10-24 19:13:20 -07004575 for (i = 0; i < t_bucket->bu_blocks; i++)
4576 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004577
Joel Beckerba937122008-10-24 19:13:20 -07004578 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004579 xh->xh_free_start = cpu_to_le16(blocksize);
4580 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4581 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4582
4583 goto set_num_buckets;
4584 }
4585
Tao Ma01225592008-08-18 17:38:53 +08004586 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004587 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004588
4589 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004590 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004591
4592 /*
4593 * Calculate the total name/value len and xh_free_start for
4594 * the old bucket first.
4595 */
4596 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4597 name_value_len = 0;
4598 for (i = 0; i < start; i++) {
4599 xe = &xh->xh_entries[i];
Joel Becker199799a2009-08-14 19:04:15 -07004600 name_value_len += namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004601 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4602 name_offset = le16_to_cpu(xe->xe_name_offset);
4603 }
4604
4605 /*
4606 * Now begin the modification to the new bucket.
4607 *
4608 * In the new bucket, We just move the xattr entry to the beginning
4609 * and don't touch the name/value. So there will be some holes in the
4610 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4611 * called.
4612 */
4613 xe = &xh->xh_entries[start];
4614 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4615 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004616 (int)((char *)xe - (char *)xh),
4617 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004618 memmove((char *)xh->xh_entries, (char *)xe, len);
4619 xe = &xh->xh_entries[count - start];
4620 len = sizeof(struct ocfs2_xattr_entry) * start;
4621 memset((char *)xe, 0, len);
4622
4623 le16_add_cpu(&xh->xh_count, -start);
4624 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4625
4626 /* Calculate xh_free_start for the new bucket. */
4627 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4628 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4629 xe = &xh->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004630 if (le16_to_cpu(xe->xe_name_offset) <
4631 le16_to_cpu(xh->xh_free_start))
4632 xh->xh_free_start = xe->xe_name_offset;
4633 }
4634
Tao Ma80bcaf32008-10-27 06:06:24 +08004635set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004636 /* set xh->xh_num_buckets for the new xh. */
4637 if (new_bucket_head)
4638 xh->xh_num_buckets = cpu_to_le16(1);
4639 else
4640 xh->xh_num_buckets = 0;
4641
Joel Beckerba937122008-10-24 19:13:20 -07004642 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004643
4644 /* store the first_hash of the new bucket. */
4645 if (first_hash)
4646 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4647
4648 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004649 * Now only update the 1st block of the old bucket. If we
4650 * just added a new empty bucket, there is no need to modify
4651 * it.
Tao Ma01225592008-08-18 17:38:53 +08004652 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004653 if (start == count)
4654 goto out;
4655
Joel Beckerba937122008-10-24 19:13:20 -07004656 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004657 memset(&xh->xh_entries[start], 0,
4658 sizeof(struct ocfs2_xattr_entry) * (count - start));
4659 xh->xh_count = cpu_to_le16(start);
4660 xh->xh_free_start = cpu_to_le16(name_offset);
4661 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4662
Joel Beckerba937122008-10-24 19:13:20 -07004663 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004664
4665out:
Joel Beckerba937122008-10-24 19:13:20 -07004666 ocfs2_xattr_bucket_free(s_bucket);
4667 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004668
4669 return ret;
4670}
4671
4672/*
4673 * Copy xattr from one bucket to another bucket.
4674 *
4675 * The caller must make sure that the journal transaction
4676 * has enough space for journaling.
4677 */
4678static int ocfs2_cp_xattr_bucket(struct inode *inode,
4679 handle_t *handle,
4680 u64 s_blkno,
4681 u64 t_blkno,
4682 int t_is_new)
4683{
Joel Becker4980c6d2008-10-24 18:54:43 -07004684 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004685 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004686
4687 BUG_ON(s_blkno == t_blkno);
4688
4689 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004690 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4691 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004692
Joel Beckerba937122008-10-24 19:13:20 -07004693 s_bucket = ocfs2_xattr_bucket_new(inode);
4694 t_bucket = ocfs2_xattr_bucket_new(inode);
4695 if (!s_bucket || !t_bucket) {
4696 ret = -ENOMEM;
4697 mlog_errno(ret);
4698 goto out;
4699 }
Joel Becker92de1092008-11-25 17:06:40 -08004700
Joel Beckerba937122008-10-24 19:13:20 -07004701 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004702 if (ret)
4703 goto out;
4704
Joel Becker784b8162008-10-24 17:33:40 -07004705 /*
4706 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4707 * there's no need to read it.
4708 */
Joel Beckerba937122008-10-24 19:13:20 -07004709 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004710 if (ret)
4711 goto out;
4712
Joel Becker2b656c12008-11-25 19:00:15 -08004713 /*
4714 * Hey, if we're overwriting t_bucket, what difference does
4715 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004716 * cluster to fill, we came here from
4717 * ocfs2_mv_xattr_buckets(), and it is really new -
4718 * ACCESS_CREATE is required. But we also might have moved data
4719 * out of t_bucket before extending back into it.
4720 * ocfs2_add_new_xattr_bucket() can do this - its call to
4721 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004722 * and copied out the end of the old extent. Then it re-extends
4723 * the old extent back to create space for new xattrs. That's
4724 * how we get here, and the bucket isn't really new.
4725 */
Joel Beckerba937122008-10-24 19:13:20 -07004726 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004727 t_is_new ?
4728 OCFS2_JOURNAL_ACCESS_CREATE :
4729 OCFS2_JOURNAL_ACCESS_WRITE);
4730 if (ret)
4731 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004732
Joel Beckerba937122008-10-24 19:13:20 -07004733 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4734 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004735
4736out:
Joel Beckerba937122008-10-24 19:13:20 -07004737 ocfs2_xattr_bucket_free(t_bucket);
4738 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004739
4740 return ret;
4741}
4742
4743/*
Joel Becker874d65a2008-11-26 13:02:18 -08004744 * src_blk points to the start of an existing extent. last_blk points to
4745 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004746 * extent. We copy the buckets from the cluster at last_blk to the new
4747 * extent. If start_bucket is non-zero, we skip that many buckets before
4748 * we start copying. The new extent's xh_num_buckets gets set to the
4749 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4750 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004751 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004752static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4753 u64 src_blk, u64 last_blk, u64 to_blk,
4754 unsigned int start_bucket,
4755 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004756{
4757 int i, ret, credits;
4758 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004759 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004760 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004761 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004762
Joel Becker874d65a2008-11-26 13:02:18 -08004763 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4764 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004765
Joel Becker54ecb6b2008-11-26 13:18:31 -08004766 BUG_ON(start_bucket >= num_buckets);
4767 if (start_bucket) {
4768 num_buckets -= start_bucket;
4769 last_blk += (start_bucket * blks_per_bucket);
4770 }
4771
Joel Becker15d60922008-11-25 18:36:42 -08004772 /* The first bucket of the original extent */
4773 old_first = ocfs2_xattr_bucket_new(inode);
4774 /* The first bucket of the new extent */
4775 new_first = ocfs2_xattr_bucket_new(inode);
4776 if (!old_first || !new_first) {
4777 ret = -ENOMEM;
4778 mlog_errno(ret);
4779 goto out;
4780 }
4781
Joel Becker874d65a2008-11-26 13:02:18 -08004782 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004783 if (ret) {
4784 mlog_errno(ret);
4785 goto out;
4786 }
4787
Tao Ma01225592008-08-18 17:38:53 +08004788 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004789 * We need to update the first bucket of the old extent and all
4790 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004791 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004792 credits = ((num_buckets + 1) * blks_per_bucket) +
4793 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004794 ret = ocfs2_extend_trans(handle, credits);
4795 if (ret) {
4796 mlog_errno(ret);
4797 goto out;
4798 }
4799
Joel Becker15d60922008-11-25 18:36:42 -08004800 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4801 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004802 if (ret) {
4803 mlog_errno(ret);
4804 goto out;
4805 }
4806
4807 for (i = 0; i < num_buckets; i++) {
4808 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004809 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004810 to_blk + (i * blks_per_bucket),
4811 1);
Tao Ma01225592008-08-18 17:38:53 +08004812 if (ret) {
4813 mlog_errno(ret);
4814 goto out;
4815 }
Tao Ma01225592008-08-18 17:38:53 +08004816 }
4817
Joel Becker15d60922008-11-25 18:36:42 -08004818 /*
4819 * Get the new bucket ready before we dirty anything
4820 * (This actually shouldn't fail, because we already dirtied
4821 * it once in ocfs2_cp_xattr_bucket()).
4822 */
4823 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4824 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004825 mlog_errno(ret);
4826 goto out;
4827 }
Joel Becker15d60922008-11-25 18:36:42 -08004828 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4829 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004830 if (ret) {
4831 mlog_errno(ret);
4832 goto out;
4833 }
4834
Joel Becker15d60922008-11-25 18:36:42 -08004835 /* Now update the headers */
4836 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4837 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004838
Joel Becker15d60922008-11-25 18:36:42 -08004839 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4840 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004841
4842 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004843 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4844
Tao Ma01225592008-08-18 17:38:53 +08004845out:
Joel Becker15d60922008-11-25 18:36:42 -08004846 ocfs2_xattr_bucket_free(new_first);
4847 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004848 return ret;
4849}
4850
4851/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004852 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004853 * This function should only be called when bucket size == cluster size.
4854 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4855 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004856static int ocfs2_divide_xattr_cluster(struct inode *inode,
4857 handle_t *handle,
4858 u64 prev_blk,
4859 u64 new_blk,
4860 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004861{
4862 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004863 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004864
4865 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4866
4867 ret = ocfs2_extend_trans(handle, credits);
4868 if (ret) {
4869 mlog_errno(ret);
4870 return ret;
4871 }
4872
4873 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004874 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4875 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004876}
4877
4878/*
4879 * Move some xattrs from the old cluster to the new one since they are not
4880 * contiguous in ocfs2 xattr tree.
4881 *
4882 * new_blk starts a new separate cluster, and we will move some xattrs from
4883 * prev_blk to it. v_start will be set as the first name hash value in this
4884 * new cluster so that it can be used as e_cpos during tree insertion and
4885 * don't collide with our original b-tree operations. first_bh and header_bh
4886 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4887 * to extend the insert bucket.
4888 *
4889 * The problem is how much xattr should we move to the new one and when should
4890 * we update first_bh and header_bh?
4891 * 1. If cluster size > bucket size, that means the previous cluster has more
4892 * than 1 bucket, so just move half nums of bucket into the new cluster and
4893 * update the first_bh and header_bh if the insert bucket has been moved
4894 * to the new cluster.
4895 * 2. If cluster_size == bucket_size:
4896 * a) If the previous extent rec has more than one cluster and the insert
4897 * place isn't in the last cluster, copy the entire last cluster to the
4898 * new one. This time, we don't need to upate the first_bh and header_bh
4899 * since they will not be moved into the new cluster.
4900 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4901 * the new one. And we set the extend flag to zero if the insert place is
4902 * moved into the new allocated cluster since no extend is needed.
4903 */
4904static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4905 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004906 struct ocfs2_xattr_bucket *first,
4907 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004908 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004909 u32 prev_clusters,
4910 u32 *v_start,
4911 int *extend)
4912{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004913 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004914
4915 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004916 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004917 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004918
Joel Becker41cb8142008-11-26 14:25:21 -08004919 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004920 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4921 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004922 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004923 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004924 prev_clusters,
4925 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004926 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004927 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004928 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004929 /* The start of the last cluster in the first extent */
4930 u64 last_blk = bucket_blkno(first) +
4931 ((prev_clusters - 1) *
4932 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004933
Joel Becker012ee912008-11-26 14:43:31 -08004934 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004935 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004936 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004937 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004938 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004939 if (ret)
4940 mlog_errno(ret);
4941 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004942 ret = ocfs2_divide_xattr_cluster(inode, handle,
4943 last_blk, new_blk,
4944 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004945 if (ret)
4946 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004947
Joel Becker92cf3ad2008-11-26 14:12:09 -08004948 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004949 *extend = 0;
4950 }
4951 }
4952
4953 return ret;
4954}
4955
4956/*
4957 * Add a new cluster for xattr storage.
4958 *
4959 * If the new cluster is contiguous with the previous one, it will be
4960 * appended to the same extent record, and num_clusters will be updated.
4961 * If not, we will insert a new extent for it and move some xattrs in
4962 * the last cluster into the new allocated one.
4963 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4964 * lose the benefits of hashing because we'll have to search large leaves.
4965 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4966 * if it's bigger).
4967 *
4968 * first_bh is the first block of the previous extent rec and header_bh
4969 * indicates the bucket we will insert the new xattrs. They will be updated
4970 * when the header_bh is moved into the new cluster.
4971 */
4972static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4973 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004974 struct ocfs2_xattr_bucket *first,
4975 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004976 u32 *num_clusters,
4977 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004978 int *extend,
4979 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004980{
Tao Ma85db90e2008-11-12 08:27:01 +08004981 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004982 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4983 u32 prev_clusters = *num_clusters;
4984 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4985 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004986 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004987 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004988 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004989
4990 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4991 "previous xattr blkno = %llu\n",
4992 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004993 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004994
Joel Becker5e404e92009-02-13 03:54:22 -08004995 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004996
Joel Becker0cf2f762009-02-12 16:41:25 -08004997 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004998 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004999 if (ret < 0) {
5000 mlog_errno(ret);
5001 goto leave;
5002 }
5003
Tao Ma78f30c32008-11-12 08:27:00 +08005004 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08005005 clusters_to_add, &bit_off, &num_bits);
5006 if (ret < 0) {
5007 if (ret != -ENOSPC)
5008 mlog_errno(ret);
5009 goto leave;
5010 }
5011
5012 BUG_ON(num_bits > clusters_to_add);
5013
5014 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5015 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5016 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5017
Joel Beckered29c0c2008-11-26 15:08:44 -08005018 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08005019 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5020 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5021 /*
5022 * If this cluster is contiguous with the old one and
5023 * adding this new cluster, we don't surpass the limit of
5024 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5025 * initialized and used like other buckets in the previous
5026 * cluster.
5027 * So add it as a contiguous one. The caller will handle
5028 * its init process.
5029 */
5030 v_start = prev_cpos + prev_clusters;
5031 *num_clusters = prev_clusters + num_bits;
5032 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5033 num_bits);
5034 } else {
5035 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5036 handle,
Joel Becker012ee912008-11-26 14:43:31 -08005037 first,
5038 target,
Tao Ma01225592008-08-18 17:38:53 +08005039 block,
Tao Ma01225592008-08-18 17:38:53 +08005040 prev_clusters,
5041 &v_start,
5042 extend);
5043 if (ret) {
5044 mlog_errno(ret);
5045 goto leave;
5046 }
5047 }
5048
5049 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07005050 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08005051 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08005052 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005053 if (ret < 0) {
5054 mlog_errno(ret);
5055 goto leave;
5056 }
5057
5058 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08005059 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08005060 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005061
5062leave:
Tao Ma01225592008-08-18 17:38:53 +08005063 return ret;
5064}
5065
5066/*
Joel Becker92de1092008-11-25 17:06:40 -08005067 * We are given an extent. 'first' is the bucket at the very front of
5068 * the extent. The extent has space for an additional bucket past
5069 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5070 * of the target bucket. We wish to shift every bucket past the target
5071 * down one, filling in that additional space. When we get back to the
5072 * target, we split the target between itself and the now-empty bucket
5073 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005074 */
5075static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005076 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08005077 struct ocfs2_xattr_bucket *first,
5078 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08005079 u32 num_clusters)
5080{
5081 int ret, credits;
5082 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5083 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08005084 u64 end_blk;
5085 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08005086
5087 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08005088 "from %llu, len = %u\n", (unsigned long long)target_blk,
5089 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005090
Joel Becker92de1092008-11-25 17:06:40 -08005091 /* The extent must have room for an additional bucket */
5092 BUG_ON(new_bucket >=
5093 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08005094
Joel Becker92de1092008-11-25 17:06:40 -08005095 /* end_blk points to the last existing bucket */
5096 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005097
5098 /*
Joel Becker92de1092008-11-25 17:06:40 -08005099 * end_blk is the start of the last existing bucket.
5100 * Thus, (end_blk - target_blk) covers the target bucket and
5101 * every bucket after it up to, but not including, the last
5102 * existing bucket. Then we add the last existing bucket, the
5103 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005104 */
Joel Becker92de1092008-11-25 17:06:40 -08005105 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08005106 handle->h_buffer_credits;
5107 ret = ocfs2_extend_trans(handle, credits);
5108 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08005109 mlog_errno(ret);
5110 goto out;
5111 }
5112
Joel Becker92de1092008-11-25 17:06:40 -08005113 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5114 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005115 if (ret) {
5116 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005117 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005118 }
5119
Joel Becker92de1092008-11-25 17:06:40 -08005120 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08005121 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5122 end_blk + blk_per_bucket, 0);
5123 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08005124 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005125 end_blk -= blk_per_bucket;
5126 }
5127
Joel Becker92de1092008-11-25 17:06:40 -08005128 /* Move half of the xattr in target_blkno to the next bucket. */
5129 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5130 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08005131
Joel Becker92de1092008-11-25 17:06:40 -08005132 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5133 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08005134
Tao Ma01225592008-08-18 17:38:53 +08005135out:
5136 return ret;
5137}
5138
5139/*
Joel Becker91f20332008-11-26 15:25:41 -08005140 * Add new xattr bucket in an extent record and adjust the buckets
5141 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5142 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08005143 *
Joel Becker91f20332008-11-26 15:25:41 -08005144 * In the easy case, we will move all the buckets after target down by
5145 * one. Half of target's xattrs will be moved to the next bucket.
5146 *
5147 * If current cluster is full, we'll allocate a new one. This may not
5148 * be contiguous. The underlying calls will make sure that there is
5149 * space for the insert, shifting buckets around if necessary.
5150 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08005151 */
5152static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5153 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005154 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08005155 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005156{
Tao Ma01225592008-08-18 17:38:53 +08005157 struct ocfs2_xattr_block *xb =
5158 (struct ocfs2_xattr_block *)xb_bh->b_data;
5159 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5160 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08005161 u32 name_hash =
5162 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08005163 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08005164 int ret, num_buckets, extend = 1;
5165 u64 p_blkno;
5166 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08005167 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08005168 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08005169
Joel Becker91f20332008-11-26 15:25:41 -08005170 mlog(0, "Add new xattr bucket starting from %llu\n",
5171 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08005172
Joel Beckered29c0c2008-11-26 15:08:44 -08005173 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08005174 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08005175 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08005176 ret = -ENOMEM;
5177 mlog_errno(ret);
5178 goto out;
5179 }
5180
Tao Ma01225592008-08-18 17:38:53 +08005181 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5182 &num_clusters, el);
5183 if (ret) {
5184 mlog_errno(ret);
5185 goto out;
5186 }
5187
Joel Beckered29c0c2008-11-26 15:08:44 -08005188 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5189 if (ret) {
5190 mlog_errno(ret);
5191 goto out;
5192 }
5193
Tao Ma01225592008-08-18 17:38:53 +08005194 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08005195 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5196 /*
5197 * This can move first+target if the target bucket moves
5198 * to the new extent.
5199 */
Tao Ma01225592008-08-18 17:38:53 +08005200 ret = ocfs2_add_new_xattr_cluster(inode,
5201 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005202 first,
5203 target,
Tao Ma01225592008-08-18 17:38:53 +08005204 &num_clusters,
5205 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005206 &extend,
5207 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005208 if (ret) {
5209 mlog_errno(ret);
5210 goto out;
5211 }
5212 }
5213
Joel Becker92de1092008-11-25 17:06:40 -08005214 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08005215 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005216 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08005217 first,
5218 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08005219 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08005220 if (ret)
5221 mlog_errno(ret);
5222 }
5223
Tao Ma01225592008-08-18 17:38:53 +08005224out:
Joel Becker92de1092008-11-25 17:06:40 -08005225 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08005226
Tao Ma01225592008-08-18 17:38:53 +08005227 return ret;
5228}
5229
5230static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5231 struct ocfs2_xattr_bucket *bucket,
5232 int offs)
5233{
5234 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5235
5236 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07005237 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08005238}
5239
5240/*
Tao Ma01225592008-08-18 17:38:53 +08005241 * Set the xattr entry in the specified bucket.
5242 * The bucket is indicated by xs->bucket and it should have the enough
5243 * space for the xattr insertion.
5244 */
5245static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005246 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005247 struct ocfs2_xattr_info *xi,
5248 struct ocfs2_xattr_search *xs,
Joel Becker9dc47402009-08-17 19:56:01 -07005249 u32 name_hash)
Tao Ma01225592008-08-18 17:38:53 +08005250{
Joel Becker1224be02008-10-24 18:47:33 -07005251 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07005252 u64 blkno;
Joel Becker69a3e532009-08-17 12:24:39 -07005253 struct ocfs2_xa_loc loc;
Tao Ma01225592008-08-18 17:38:53 +08005254
Mark Fashehff1ec202008-08-19 10:54:29 -07005255 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
Joel Becker6b240ff2009-08-14 18:02:52 -07005256 (unsigned long)xi->xi_value_len, xi->xi_name_index,
Joel Beckerba937122008-10-24 19:13:20 -07005257 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08005258
Joel Beckerba937122008-10-24 19:13:20 -07005259 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07005260 blkno = bucket_blkno(xs->bucket);
5261 ocfs2_xattr_bucket_relse(xs->bucket);
5262 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08005263 if (ret) {
5264 mlog_errno(ret);
5265 goto out;
5266 }
5267 }
5268
Joel Beckerba937122008-10-24 19:13:20 -07005269 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07005270 OCFS2_JOURNAL_ACCESS_WRITE);
5271 if (ret < 0) {
5272 mlog_errno(ret);
5273 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005274 }
5275
Joel Becker69a3e532009-08-17 12:24:39 -07005276 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5277 xs->not_found ? NULL : xs->here);
5278 ret = ocfs2_xa_prepare_entry(&loc, xi, name_hash);
5279 if (ret) {
5280 if (ret != -ENOSPC)
5281 mlog_errno(ret);
5282 goto out;
5283 }
5284 /* XXX For now, until we make ocfs2_xa_prepare_entry() primary */
5285 BUG_ON(ret == -ENOSPC);
5286 ocfs2_xa_store_inline_value(&loc, xi);
5287 xs->here = loc.xl_entry;
5288
Joel Beckerba937122008-10-24 19:13:20 -07005289 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005290
Tao Ma01225592008-08-18 17:38:53 +08005291out:
Tao Ma01225592008-08-18 17:38:53 +08005292 return ret;
5293}
5294
Tao Ma01225592008-08-18 17:38:53 +08005295/*
5296 * Truncate the specified xe_off entry in xattr bucket.
5297 * bucket is indicated by header_bh and len is the new length.
5298 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5299 *
5300 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5301 */
5302static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08005303 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08005304 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08005305 int len,
5306 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005307{
5308 int ret, offset;
5309 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08005310 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08005311 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08005312 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08005313 struct ocfs2_xattr_value_buf vb = {
5314 .vb_access = ocfs2_journal_access,
5315 };
Tao Ma01225592008-08-18 17:38:53 +08005316
5317 xe = &xh->xh_entries[xe_off];
5318
5319 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5320
5321 offset = le16_to_cpu(xe->xe_name_offset) +
5322 OCFS2_XATTR_SIZE(xe->xe_name_len);
5323
5324 value_blk = offset / blocksize;
5325
5326 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5327 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005328
Joel Beckerb3e5d372008-12-09 15:01:04 -08005329 vb.vb_bh = bucket->bu_bhs[value_blk];
5330 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08005331
Joel Beckerb3e5d372008-12-09 15:01:04 -08005332 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5333 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005334
Joel Becker548b0f22008-11-24 19:32:13 -08005335 /*
5336 * From here on out we have to dirty the bucket. The generic
5337 * value calls only modify one of the bucket's bhs, but we need
5338 * to send the bucket at once. So if they error, they *could* have
5339 * modified something. We have to assume they did, and dirty
5340 * the whole bucket. This leaves us in a consistent state.
5341 */
Tao Ma01225592008-08-18 17:38:53 +08005342 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08005343 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08005344 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005345 if (ret) {
5346 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005347 goto out;
5348 }
5349
5350 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5351 OCFS2_JOURNAL_ACCESS_WRITE);
5352 if (ret) {
5353 mlog_errno(ret);
5354 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005355 }
5356
Joel Becker548b0f22008-11-24 19:32:13 -08005357 xe->xe_value_size = cpu_to_le64(len);
5358
Joel Becker548b0f22008-11-24 19:32:13 -08005359 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005360
5361out:
Tao Ma01225592008-08-18 17:38:53 +08005362 return ret;
5363}
5364
5365static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08005366 struct ocfs2_xattr_search *xs,
5367 int len,
5368 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005369{
5370 int ret, offset;
5371 struct ocfs2_xattr_entry *xe = xs->here;
5372 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5373
Joel Beckerba937122008-10-24 19:13:20 -07005374 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08005375
5376 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08005377 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005378 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005379 if (ret)
5380 mlog_errno(ret);
5381
5382 return ret;
5383}
5384
5385static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005386 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005387 struct ocfs2_xattr_search *xs,
5388 char *val,
5389 int value_len)
5390{
Tao Ma712e53e2009-03-12 08:37:34 +08005391 int ret, offset, block_off;
Tao Ma01225592008-08-18 17:38:53 +08005392 struct ocfs2_xattr_value_root *xv;
5393 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma712e53e2009-03-12 08:37:34 +08005394 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5395 void *base;
Tao Ma492a8a32009-08-18 11:43:17 +08005396 struct ocfs2_xattr_value_buf vb = {
5397 .vb_access = ocfs2_journal_access,
5398 };
Tao Ma01225592008-08-18 17:38:53 +08005399
5400 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5401
Tao Mafd68a892009-08-18 11:43:21 +08005402 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
Tao Ma712e53e2009-03-12 08:37:34 +08005403 xe - xh->xh_entries,
5404 &block_off,
5405 &offset);
5406 if (ret) {
5407 mlog_errno(ret);
5408 goto out;
5409 }
Tao Ma01225592008-08-18 17:38:53 +08005410
Tao Ma712e53e2009-03-12 08:37:34 +08005411 base = bucket_block(xs->bucket, block_off);
5412 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5413 OCFS2_XATTR_SIZE(xe->xe_name_len));
Tao Ma01225592008-08-18 17:38:53 +08005414
Tao Ma492a8a32009-08-18 11:43:17 +08005415 vb.vb_xv = xv;
5416 vb.vb_bh = xs->bucket->bu_bhs[block_off];
Tao Ma712e53e2009-03-12 08:37:34 +08005417 ret = __ocfs2_xattr_set_value_outside(inode, handle,
Tao Ma492a8a32009-08-18 11:43:17 +08005418 &vb, val, value_len);
Tao Ma712e53e2009-03-12 08:37:34 +08005419 if (ret)
5420 mlog_errno(ret);
5421out:
5422 return ret;
Tao Ma01225592008-08-18 17:38:53 +08005423}
5424
Tao Ma01225592008-08-18 17:38:53 +08005425static int ocfs2_rm_xattr_cluster(struct inode *inode,
5426 struct buffer_head *root_bh,
5427 u64 blkno,
5428 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005429 u32 len,
5430 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005431{
5432 int ret;
5433 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5434 struct inode *tl_inode = osb->osb_tl_inode;
5435 handle_t *handle;
5436 struct ocfs2_xattr_block *xb =
5437 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005438 struct ocfs2_alloc_context *meta_ac = NULL;
5439 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005440 struct ocfs2_extent_tree et;
5441
Tao Ma47bca492009-08-18 11:43:42 +08005442 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005443 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005444 if (ret) {
5445 mlog_errno(ret);
5446 return ret;
5447 }
5448
Joel Becker5e404e92009-02-13 03:54:22 -08005449 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005450
5451 ocfs2_init_dealloc_ctxt(&dealloc);
5452
5453 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5454 cpos, len, (unsigned long long)blkno);
5455
Joel Becker8cb471e2009-02-10 20:00:41 -08005456 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5457 len);
Tao Ma01225592008-08-18 17:38:53 +08005458
Joel Beckerf99b9b72008-08-20 19:36:33 -07005459 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005460 if (ret) {
5461 mlog_errno(ret);
5462 return ret;
5463 }
5464
5465 mutex_lock(&tl_inode->i_mutex);
5466
5467 if (ocfs2_truncate_log_needs_flush(osb)) {
5468 ret = __ocfs2_flush_truncate_log(osb);
5469 if (ret < 0) {
5470 mlog_errno(ret);
5471 goto out;
5472 }
5473 }
5474
Jan Karaa90714c2008-10-09 19:38:40 +02005475 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005476 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005477 ret = -ENOMEM;
5478 mlog_errno(ret);
5479 goto out;
5480 }
5481
Joel Becker0cf2f762009-02-12 16:41:25 -08005482 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005483 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005484 if (ret) {
5485 mlog_errno(ret);
5486 goto out_commit;
5487 }
5488
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005489 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005490 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005491 if (ret) {
5492 mlog_errno(ret);
5493 goto out_commit;
5494 }
5495
5496 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5497
5498 ret = ocfs2_journal_dirty(handle, root_bh);
5499 if (ret) {
5500 mlog_errno(ret);
5501 goto out_commit;
5502 }
5503
5504 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5505 if (ret)
5506 mlog_errno(ret);
5507
5508out_commit:
5509 ocfs2_commit_trans(osb, handle);
5510out:
5511 ocfs2_schedule_truncate_log_flush(osb, 1);
5512
5513 mutex_unlock(&tl_inode->i_mutex);
5514
5515 if (meta_ac)
5516 ocfs2_free_alloc_context(meta_ac);
5517
5518 ocfs2_run_deallocs(osb, &dealloc);
5519
5520 return ret;
5521}
5522
Tao Ma01225592008-08-18 17:38:53 +08005523static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005524 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08005525 struct ocfs2_xattr_search *xs)
5526{
Joel Beckerba937122008-10-24 19:13:20 -07005527 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005528 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5529 le16_to_cpu(xh->xh_count) - 1];
5530 int ret = 0;
5531
Joel Beckerba937122008-10-24 19:13:20 -07005532 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07005533 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005534 if (ret) {
5535 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005536 return;
Tao Ma01225592008-08-18 17:38:53 +08005537 }
5538
5539 /* Remove the old entry. */
5540 memmove(xs->here, xs->here + 1,
5541 (void *)last - (void *)xs->here);
5542 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5543 le16_add_cpu(&xh->xh_count, -1);
5544
Joel Beckerba937122008-10-24 19:13:20 -07005545 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005546}
5547
5548/*
5549 * Set the xattr name/value in the bucket specified in xs.
5550 *
5551 * As the new value in xi may be stored in the bucket or in an outside cluster,
5552 * we divide the whole process into 3 steps:
5553 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5554 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5555 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5556 * 4. If the clusters for the new outside value can't be allocated, we need
5557 * to free the xattr we allocated in set.
5558 */
5559static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5560 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005561 struct ocfs2_xattr_search *xs,
5562 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005563{
Joel Becker9dc47402009-08-17 19:56:01 -07005564 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005565 size_t value_len;
Joel Becker6b240ff2009-08-14 18:02:52 -07005566 char *val = (char *)xi->xi_value;
Tao Ma01225592008-08-18 17:38:53 +08005567 struct ocfs2_xattr_entry *xe = xs->here;
Joel Becker6b240ff2009-08-14 18:02:52 -07005568 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->xi_name,
Joel Becker18853b92009-08-14 18:17:07 -07005569 xi->xi_name_len);
Tao Ma01225592008-08-18 17:38:53 +08005570
Joel Becker9dc47402009-08-17 19:56:01 -07005571 value_len = xi->xi_value_len;
Tao Ma01225592008-08-18 17:38:53 +08005572 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5573 /*
5574 * We need to truncate the xattr storage first.
5575 *
5576 * If both the old and new value are stored to
5577 * outside block, we only need to truncate
5578 * the storage and then set the value outside.
5579 *
5580 * If the new value should be stored within block,
5581 * we should free all the outside block first and
5582 * the modification to the xattr block will be done
5583 * by following steps.
5584 */
Joel Becker9dc47402009-08-17 19:56:01 -07005585 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
Tao Ma01225592008-08-18 17:38:53 +08005586 value_len = 0;
5587
5588 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005589 value_len,
5590 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005591 if (ret)
5592 goto out;
5593
5594 if (value_len)
5595 goto set_value_outside;
5596 }
5597
Tao Ma01225592008-08-18 17:38:53 +08005598 /* So we have to handle the inside block change now. */
Tao Ma85db90e2008-11-12 08:27:01 +08005599 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
Joel Becker9dc47402009-08-17 19:56:01 -07005600 name_hash);
Tao Ma01225592008-08-18 17:38:53 +08005601 if (ret) {
5602 mlog_errno(ret);
5603 goto out;
5604 }
5605
Joel Becker9dc47402009-08-17 19:56:01 -07005606 if (xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE)
Tao Ma5a095612008-09-19 22:17:41 +08005607 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005608
Tao Ma5a095612008-09-19 22:17:41 +08005609 /* allocate the space now for the outside block storage. */
5610 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005611 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08005612 if (ret) {
5613 mlog_errno(ret);
5614
5615 if (xs->not_found) {
5616 /*
5617 * We can't allocate enough clusters for outside
5618 * storage and we have allocated xattr already,
5619 * so need to remove it.
5620 */
Tao Ma85db90e2008-11-12 08:27:01 +08005621 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08005622 }
Tao Ma01225592008-08-18 17:38:53 +08005623 goto out;
5624 }
5625
5626set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08005627 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5628 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08005629out:
5630 return ret;
5631}
5632
Tao Ma80bcaf32008-10-27 06:06:24 +08005633/*
5634 * check whether the xattr bucket is filled up with the same hash value.
5635 * If we want to insert the xattr with the same hash, return -ENOSPC.
5636 * If we want to insert a xattr with different hash value, go ahead
5637 * and ocfs2_divide_xattr_bucket will handle this.
5638 */
Tao Ma01225592008-08-18 17:38:53 +08005639static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005640 struct ocfs2_xattr_bucket *bucket,
5641 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005642{
Joel Becker3e632942008-10-24 17:04:49 -07005643 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005644 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5645
5646 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5647 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005648
5649 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5650 xh->xh_entries[0].xe_name_hash) {
5651 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5652 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005653 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005654 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5655 return -ENOSPC;
5656 }
5657
5658 return 0;
5659}
5660
5661static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5662 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005663 struct ocfs2_xattr_search *xs,
5664 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005665{
5666 struct ocfs2_xattr_header *xh;
5667 struct ocfs2_xattr_entry *xe;
5668 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005669 int free, max_free, need, old;
Joel Becker18853b92009-08-14 18:17:07 -07005670 size_t value_size = 0;
Tao Ma01225592008-08-18 17:38:53 +08005671 size_t blocksize = inode->i_sb->s_blocksize;
5672 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005673
Joel Becker6b240ff2009-08-14 18:02:52 -07005674 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005675
5676try_again:
5677 xh = xs->header;
5678 count = le16_to_cpu(xh->xh_count);
5679 xh_free_start = le16_to_cpu(xh->xh_free_start);
5680 header_size = sizeof(struct ocfs2_xattr_header) +
5681 count * sizeof(struct ocfs2_xattr_entry);
Tiger Yang4442f512009-02-20 11:11:50 +08005682 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5683 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005684
5685 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5686 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005687 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005688 header_size);
5689
Joel Becker6b240ff2009-08-14 18:02:52 -07005690 if (xi->xi_value && xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma01225592008-08-18 17:38:53 +08005691 value_size = OCFS2_XATTR_ROOT_SIZE;
Joel Becker6b240ff2009-08-14 18:02:52 -07005692 else if (xi->xi_value)
5693 value_size = OCFS2_XATTR_SIZE(xi->xi_value_len);
Tao Ma01225592008-08-18 17:38:53 +08005694
5695 if (xs->not_found)
5696 need = sizeof(struct ocfs2_xattr_entry) +
Joel Becker18853b92009-08-14 18:17:07 -07005697 OCFS2_XATTR_SIZE(xi->xi_name_len) + value_size;
Tao Ma01225592008-08-18 17:38:53 +08005698 else {
Joel Becker18853b92009-08-14 18:17:07 -07005699 need = value_size + OCFS2_XATTR_SIZE(xi->xi_name_len);
Tao Ma01225592008-08-18 17:38:53 +08005700
5701 /*
5702 * We only replace the old value if the new length is smaller
5703 * than the old one. Otherwise we will allocate new space in the
5704 * bucket to store it.
5705 */
5706 xe = xs->here;
5707 if (ocfs2_xattr_is_local(xe))
5708 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5709 else
5710 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5711
5712 if (old >= value_size)
5713 need = 0;
5714 }
5715
Tiger Yang4442f512009-02-20 11:11:50 +08005716 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005717 /*
5718 * We need to make sure the new name/value pair
5719 * can exist in the same block.
5720 */
5721 if (xh_free_start % blocksize < need)
5722 free -= xh_free_start % blocksize;
5723
5724 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5725 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5726 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005727 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005728 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5729 le16_to_cpu(xh->xh_name_value_len));
5730
Tao Ma976331d2008-11-12 08:26:57 +08005731 if (free < need ||
5732 (xs->not_found &&
5733 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005734 if (need <= max_free &&
5735 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5736 /*
5737 * We can create the space by defragment. Since only the
5738 * name/value will be moved, the xe shouldn't be changed
5739 * in xs.
5740 */
Tao Ma85db90e2008-11-12 08:27:01 +08005741 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5742 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005743 if (ret) {
5744 mlog_errno(ret);
5745 goto out;
5746 }
5747
5748 xh_free_start = le16_to_cpu(xh->xh_free_start);
Tiger Yang4442f512009-02-20 11:11:50 +08005749 free = xh_free_start - header_size
5750 - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005751 if (xh_free_start % blocksize < need)
5752 free -= xh_free_start % blocksize;
5753
5754 if (free >= need)
5755 goto xattr_set;
5756
5757 mlog(0, "Can't get enough space for xattr insert by "
5758 "defragment. Need %u bytes, but we have %d, so "
5759 "allocate new bucket for it.\n", need, free);
5760 }
5761
5762 /*
5763 * We have to add new buckets or clusters and one
5764 * allocation should leave us enough space for insert.
5765 */
5766 BUG_ON(allocation);
5767
5768 /*
5769 * We do not allow for overlapping ranges between buckets. And
5770 * the maximum number of collisions we will allow for then is
5771 * one bucket's worth, so check it here whether we need to
5772 * add a new bucket for the insert.
5773 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005774 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005775 xs->bucket,
Joel Becker6b240ff2009-08-14 18:02:52 -07005776 xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005777 if (ret) {
5778 mlog_errno(ret);
5779 goto out;
5780 }
5781
5782 ret = ocfs2_add_new_xattr_bucket(inode,
5783 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005784 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005785 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005786 if (ret) {
5787 mlog_errno(ret);
5788 goto out;
5789 }
5790
Joel Becker91f20332008-11-26 15:25:41 -08005791 /*
5792 * ocfs2_add_new_xattr_bucket() will have updated
5793 * xs->bucket if it moved, but it will not have updated
5794 * any of the other search fields. Thus, we drop it and
5795 * re-search. Everything should be cached, so it'll be
5796 * quick.
5797 */
Joel Beckerba937122008-10-24 19:13:20 -07005798 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005799 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
Joel Becker6b240ff2009-08-14 18:02:52 -07005800 xi->xi_name_index,
5801 xi->xi_name, xs);
Tao Ma01225592008-08-18 17:38:53 +08005802 if (ret && ret != -ENODATA)
5803 goto out;
5804 xs->not_found = ret;
5805 allocation = 1;
5806 goto try_again;
5807 }
5808
5809xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005810 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005811out:
5812 mlog_exit(ret);
5813 return ret;
5814}
Tao Maa3944252008-08-18 17:38:54 +08005815
5816static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5817 struct ocfs2_xattr_bucket *bucket,
5818 void *para)
5819{
Tao Mace9c5a52009-08-18 11:43:59 +08005820 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005821 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005822 u16 i;
5823 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005824 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5825 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005826 int credits = ocfs2_remove_extent_credits(osb->sb) +
5827 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005828 struct ocfs2_xattr_value_root *xv;
5829 struct ocfs2_rm_xattr_bucket_para *args =
5830 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005831
5832 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005833
5834 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5835 xe = &xh->xh_entries[i];
5836 if (ocfs2_xattr_is_local(xe))
5837 continue;
5838
Tao Mace9c5a52009-08-18 11:43:59 +08005839 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5840 i, &xv, NULL);
5841
5842 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5843 args->ref_ci,
5844 args->ref_root_bh,
5845 &ctxt.meta_ac,
5846 &ref_credits);
5847
5848 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005849 if (IS_ERR(ctxt.handle)) {
5850 ret = PTR_ERR(ctxt.handle);
5851 mlog_errno(ret);
5852 break;
5853 }
5854
Joel Becker548b0f22008-11-24 19:32:13 -08005855 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005856 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005857
5858 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005859 if (ctxt.meta_ac) {
5860 ocfs2_free_alloc_context(ctxt.meta_ac);
5861 ctxt.meta_ac = NULL;
5862 }
Tao Maa3944252008-08-18 17:38:54 +08005863 if (ret) {
5864 mlog_errno(ret);
5865 break;
5866 }
5867 }
5868
Tao Mace9c5a52009-08-18 11:43:59 +08005869 if (ctxt.meta_ac)
5870 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005871 ocfs2_schedule_truncate_log_flush(osb, 1);
5872 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005873 return ret;
5874}
5875
Mark Fasheh99219ae2008-10-07 14:52:59 -07005876/*
Tao Ma492a8a32009-08-18 11:43:17 +08005877 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5878 * or change the extent record flag), we need to recalculate
5879 * the metaecc for the whole bucket. So it is done here.
5880 *
5881 * Note:
5882 * We have to give the extra credits for the caller.
5883 */
5884static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5885 handle_t *handle,
5886 void *para)
5887{
5888 int ret;
5889 struct ocfs2_xattr_bucket *bucket =
5890 (struct ocfs2_xattr_bucket *)para;
5891
5892 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5893 OCFS2_JOURNAL_ACCESS_WRITE);
5894 if (ret) {
5895 mlog_errno(ret);
5896 return ret;
5897 }
5898
5899 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5900
5901 return 0;
5902}
5903
5904/*
5905 * Special action we need if the xattr value is refcounted.
5906 *
5907 * 1. If the xattr is refcounted, lock the tree.
5908 * 2. CoW the xattr if we are setting the new value and the value
5909 * will be stored outside.
5910 * 3. In other case, decrease_refcount will work for us, so just
5911 * lock the refcount tree, calculate the meta and credits is OK.
5912 *
5913 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5914 * currently CoW is a completed transaction, while this function
5915 * will also lock the allocators and let us deadlock. So we will
5916 * CoW the whole xattr value.
5917 */
5918static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5919 struct ocfs2_dinode *di,
5920 struct ocfs2_xattr_info *xi,
5921 struct ocfs2_xattr_search *xis,
5922 struct ocfs2_xattr_search *xbs,
5923 struct ocfs2_refcount_tree **ref_tree,
5924 int *meta_add,
5925 int *credits)
5926{
5927 int ret = 0;
5928 struct ocfs2_xattr_block *xb;
5929 struct ocfs2_xattr_entry *xe;
5930 char *base;
5931 u32 p_cluster, num_clusters;
5932 unsigned int ext_flags;
5933 int name_offset, name_len;
5934 struct ocfs2_xattr_value_buf vb;
5935 struct ocfs2_xattr_bucket *bucket = NULL;
5936 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5937 struct ocfs2_post_refcount refcount;
5938 struct ocfs2_post_refcount *p = NULL;
5939 struct buffer_head *ref_root_bh = NULL;
5940
5941 if (!xis->not_found) {
5942 xe = xis->here;
5943 name_offset = le16_to_cpu(xe->xe_name_offset);
5944 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5945 base = xis->base;
5946 vb.vb_bh = xis->inode_bh;
5947 vb.vb_access = ocfs2_journal_access_di;
5948 } else {
5949 int i, block_off = 0;
5950 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5951 xe = xbs->here;
5952 name_offset = le16_to_cpu(xe->xe_name_offset);
5953 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5954 i = xbs->here - xbs->header->xh_entries;
5955
5956 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005957 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005958 bucket_xh(xbs->bucket),
5959 i, &block_off,
5960 &name_offset);
5961 if (ret) {
5962 mlog_errno(ret);
5963 goto out;
5964 }
5965 base = bucket_block(xbs->bucket, block_off);
5966 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5967 vb.vb_access = ocfs2_journal_access;
5968
5969 if (ocfs2_meta_ecc(osb)) {
5970 /*create parameters for ocfs2_post_refcount. */
5971 bucket = xbs->bucket;
5972 refcount.credits = bucket->bu_blocks;
5973 refcount.para = bucket;
5974 refcount.func =
5975 ocfs2_xattr_bucket_post_refcount;
5976 p = &refcount;
5977 }
5978 } else {
5979 base = xbs->base;
5980 vb.vb_bh = xbs->xattr_bh;
5981 vb.vb_access = ocfs2_journal_access_xb;
5982 }
5983 }
5984
5985 if (ocfs2_xattr_is_local(xe))
5986 goto out;
5987
5988 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5989 (base + name_offset + name_len);
5990
5991 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5992 &num_clusters, &vb.vb_xv->xr_list,
5993 &ext_flags);
5994 if (ret) {
5995 mlog_errno(ret);
5996 goto out;
5997 }
5998
5999 /*
6000 * We just need to check the 1st extent record, since we always
6001 * CoW the whole xattr. So there shouldn't be a xattr with
6002 * some REFCOUNT extent recs after the 1st one.
6003 */
6004 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
6005 goto out;
6006
6007 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
6008 1, ref_tree, &ref_root_bh);
6009 if (ret) {
6010 mlog_errno(ret);
6011 goto out;
6012 }
6013
6014 /*
6015 * If we are deleting the xattr or the new size will be stored inside,
6016 * cool, leave it there, the xattr truncate process will remove them
6017 * for us(it still needs the refcount tree lock and the meta, credits).
6018 * And the worse case is that every cluster truncate will split the
6019 * refcount tree, and make the original extent become 3. So we will need
6020 * 2 * cluster more extent recs at most.
6021 */
Joel Becker6b240ff2009-08-14 18:02:52 -07006022 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
Tao Ma492a8a32009-08-18 11:43:17 +08006023
6024 ret = ocfs2_refcounted_xattr_delete_need(inode,
6025 &(*ref_tree)->rf_ci,
6026 ref_root_bh, vb.vb_xv,
6027 meta_add, credits);
6028 if (ret)
6029 mlog_errno(ret);
6030 goto out;
6031 }
6032
6033 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
6034 *ref_tree, ref_root_bh, 0,
6035 le32_to_cpu(vb.vb_xv->xr_clusters), p);
6036 if (ret)
6037 mlog_errno(ret);
6038
6039out:
6040 brelse(ref_root_bh);
6041 return ret;
6042}
6043
6044/*
Tao Ma01292412009-09-21 13:04:19 +08006045 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
6046 * The physical clusters will be added to refcount tree.
6047 */
6048static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
6049 struct ocfs2_xattr_value_root *xv,
6050 struct ocfs2_extent_tree *value_et,
6051 struct ocfs2_caching_info *ref_ci,
6052 struct buffer_head *ref_root_bh,
6053 struct ocfs2_cached_dealloc_ctxt *dealloc,
6054 struct ocfs2_post_refcount *refcount)
6055{
6056 int ret = 0;
6057 u32 clusters = le32_to_cpu(xv->xr_clusters);
6058 u32 cpos, p_cluster, num_clusters;
6059 struct ocfs2_extent_list *el = &xv->xr_list;
6060 unsigned int ext_flags;
6061
6062 cpos = 0;
6063 while (cpos < clusters) {
6064 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
6065 &num_clusters, el, &ext_flags);
6066
6067 cpos += num_clusters;
6068 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
6069 continue;
6070
6071 BUG_ON(!p_cluster);
6072
6073 ret = ocfs2_add_refcount_flag(inode, value_et,
6074 ref_ci, ref_root_bh,
6075 cpos - num_clusters,
6076 p_cluster, num_clusters,
6077 dealloc, refcount);
6078 if (ret) {
6079 mlog_errno(ret);
6080 break;
6081 }
6082 }
6083
6084 return ret;
6085}
6086
6087/*
6088 * Given a normal ocfs2_xattr_header, refcount all the entries which
6089 * have value stored outside.
6090 * Used for xattrs stored in inode and ocfs2_xattr_block.
6091 */
6092static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
6093 struct ocfs2_xattr_value_buf *vb,
6094 struct ocfs2_xattr_header *header,
6095 struct ocfs2_caching_info *ref_ci,
6096 struct buffer_head *ref_root_bh,
6097 struct ocfs2_cached_dealloc_ctxt *dealloc)
6098{
6099
6100 struct ocfs2_xattr_entry *xe;
6101 struct ocfs2_xattr_value_root *xv;
6102 struct ocfs2_extent_tree et;
6103 int i, ret = 0;
6104
6105 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
6106 xe = &header->xh_entries[i];
6107
6108 if (ocfs2_xattr_is_local(xe))
6109 continue;
6110
6111 xv = (struct ocfs2_xattr_value_root *)((void *)header +
6112 le16_to_cpu(xe->xe_name_offset) +
6113 OCFS2_XATTR_SIZE(xe->xe_name_len));
6114
6115 vb->vb_xv = xv;
6116 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
6117
6118 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
6119 ref_ci, ref_root_bh,
6120 dealloc, NULL);
6121 if (ret) {
6122 mlog_errno(ret);
6123 break;
6124 }
6125 }
6126
6127 return ret;
6128}
6129
6130static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
6131 struct buffer_head *fe_bh,
6132 struct ocfs2_caching_info *ref_ci,
6133 struct buffer_head *ref_root_bh,
6134 struct ocfs2_cached_dealloc_ctxt *dealloc)
6135{
6136 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6137 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
6138 (fe_bh->b_data + inode->i_sb->s_blocksize -
6139 le16_to_cpu(di->i_xattr_inline_size));
6140 struct ocfs2_xattr_value_buf vb = {
6141 .vb_bh = fe_bh,
6142 .vb_access = ocfs2_journal_access_di,
6143 };
6144
6145 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6146 ref_ci, ref_root_bh, dealloc);
6147}
6148
6149struct ocfs2_xattr_tree_value_refcount_para {
6150 struct ocfs2_caching_info *ref_ci;
6151 struct buffer_head *ref_root_bh;
6152 struct ocfs2_cached_dealloc_ctxt *dealloc;
6153};
6154
6155static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
6156 struct ocfs2_xattr_bucket *bucket,
6157 int offset,
6158 struct ocfs2_xattr_value_root **xv,
6159 struct buffer_head **bh)
6160{
6161 int ret, block_off, name_offset;
6162 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
6163 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6164 void *base;
6165
6166 ret = ocfs2_xattr_bucket_get_name_value(sb,
6167 bucket_xh(bucket),
6168 offset,
6169 &block_off,
6170 &name_offset);
6171 if (ret) {
6172 mlog_errno(ret);
6173 goto out;
6174 }
6175
6176 base = bucket_block(bucket, block_off);
6177
6178 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6179 OCFS2_XATTR_SIZE(xe->xe_name_len));
6180
6181 if (bh)
6182 *bh = bucket->bu_bhs[block_off];
6183out:
6184 return ret;
6185}
6186
6187/*
6188 * For a given xattr bucket, refcount all the entries which
6189 * have value stored outside.
6190 */
6191static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6192 struct ocfs2_xattr_bucket *bucket,
6193 void *para)
6194{
6195 int i, ret = 0;
6196 struct ocfs2_extent_tree et;
6197 struct ocfs2_xattr_tree_value_refcount_para *ref =
6198 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6199 struct ocfs2_xattr_header *xh =
6200 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6201 struct ocfs2_xattr_entry *xe;
6202 struct ocfs2_xattr_value_buf vb = {
6203 .vb_access = ocfs2_journal_access,
6204 };
6205 struct ocfs2_post_refcount refcount = {
6206 .credits = bucket->bu_blocks,
6207 .para = bucket,
6208 .func = ocfs2_xattr_bucket_post_refcount,
6209 };
6210 struct ocfs2_post_refcount *p = NULL;
6211
6212 /* We only need post_refcount if we support metaecc. */
6213 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6214 p = &refcount;
6215
6216 mlog(0, "refcount bucket %llu, count = %u\n",
6217 (unsigned long long)bucket_blkno(bucket),
6218 le16_to_cpu(xh->xh_count));
6219 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6220 xe = &xh->xh_entries[i];
6221
6222 if (ocfs2_xattr_is_local(xe))
6223 continue;
6224
6225 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6226 &vb.vb_xv, &vb.vb_bh);
6227 if (ret) {
6228 mlog_errno(ret);
6229 break;
6230 }
6231
6232 ocfs2_init_xattr_value_extent_tree(&et,
6233 INODE_CACHE(inode), &vb);
6234
6235 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6236 &et, ref->ref_ci,
6237 ref->ref_root_bh,
6238 ref->dealloc, p);
6239 if (ret) {
6240 mlog_errno(ret);
6241 break;
6242 }
6243 }
6244
6245 return ret;
6246
6247}
6248
6249static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6250 struct buffer_head *root_bh,
6251 u64 blkno, u32 cpos, u32 len, void *para)
6252{
6253 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6254 ocfs2_xattr_bucket_value_refcount,
6255 para);
6256}
6257
6258static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6259 struct buffer_head *blk_bh,
6260 struct ocfs2_caching_info *ref_ci,
6261 struct buffer_head *ref_root_bh,
6262 struct ocfs2_cached_dealloc_ctxt *dealloc)
6263{
6264 int ret = 0;
6265 struct ocfs2_xattr_block *xb =
6266 (struct ocfs2_xattr_block *)blk_bh->b_data;
6267
6268 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6269 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6270 struct ocfs2_xattr_value_buf vb = {
6271 .vb_bh = blk_bh,
6272 .vb_access = ocfs2_journal_access_xb,
6273 };
6274
6275 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6276 ref_ci, ref_root_bh,
6277 dealloc);
6278 } else {
6279 struct ocfs2_xattr_tree_value_refcount_para para = {
6280 .ref_ci = ref_ci,
6281 .ref_root_bh = ref_root_bh,
6282 .dealloc = dealloc,
6283 };
6284
6285 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6286 ocfs2_refcount_xattr_tree_rec,
6287 &para);
6288 }
6289
6290 return ret;
6291}
6292
6293int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6294 struct buffer_head *fe_bh,
6295 struct ocfs2_caching_info *ref_ci,
6296 struct buffer_head *ref_root_bh,
6297 struct ocfs2_cached_dealloc_ctxt *dealloc)
6298{
6299 int ret = 0;
6300 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6301 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6302 struct buffer_head *blk_bh = NULL;
6303
6304 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6305 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6306 ref_ci, ref_root_bh,
6307 dealloc);
6308 if (ret) {
6309 mlog_errno(ret);
6310 goto out;
6311 }
6312 }
6313
6314 if (!di->i_xattr_loc)
6315 goto out;
6316
6317 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6318 &blk_bh);
6319 if (ret < 0) {
6320 mlog_errno(ret);
6321 goto out;
6322 }
6323
6324 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6325 ref_root_bh, dealloc);
6326 if (ret)
6327 mlog_errno(ret);
6328
6329 brelse(blk_bh);
6330out:
6331
6332 return ret;
6333}
6334
Tao Ma0fe9b662009-08-18 11:47:56 +08006335typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006336/*
Tao Ma2999d122009-08-18 11:43:55 +08006337 * Store the information we need in xattr reflink.
6338 * old_bh and new_bh are inode bh for the old and new inode.
6339 */
6340struct ocfs2_xattr_reflink {
6341 struct inode *old_inode;
6342 struct inode *new_inode;
6343 struct buffer_head *old_bh;
6344 struct buffer_head *new_bh;
6345 struct ocfs2_caching_info *ref_ci;
6346 struct buffer_head *ref_root_bh;
6347 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006348 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006349};
6350
6351/*
6352 * Given a xattr header and xe offset,
6353 * return the proper xv and the corresponding bh.
6354 * xattr in inode, block and xattr tree have different implementaions.
6355 */
6356typedef int (get_xattr_value_root)(struct super_block *sb,
6357 struct buffer_head *bh,
6358 struct ocfs2_xattr_header *xh,
6359 int offset,
6360 struct ocfs2_xattr_value_root **xv,
6361 struct buffer_head **ret_bh,
6362 void *para);
6363
6364/*
6365 * Calculate all the xattr value root metadata stored in this xattr header and
6366 * credits we need if we create them from the scratch.
6367 * We use get_xattr_value_root so that all types of xattr container can use it.
6368 */
6369static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6370 struct buffer_head *bh,
6371 struct ocfs2_xattr_header *xh,
6372 int *metas, int *credits,
6373 int *num_recs,
6374 get_xattr_value_root *func,
6375 void *para)
6376{
6377 int i, ret = 0;
6378 struct ocfs2_xattr_value_root *xv;
6379 struct ocfs2_xattr_entry *xe;
6380
6381 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6382 xe = &xh->xh_entries[i];
6383 if (ocfs2_xattr_is_local(xe))
6384 continue;
6385
6386 ret = func(sb, bh, xh, i, &xv, NULL, para);
6387 if (ret) {
6388 mlog_errno(ret);
6389 break;
6390 }
6391
6392 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6393 le16_to_cpu(xv->xr_list.l_next_free_rec);
6394
6395 *credits += ocfs2_calc_extend_credits(sb,
6396 &def_xv.xv.xr_list,
6397 le32_to_cpu(xv->xr_clusters));
6398
6399 /*
6400 * If the value is a tree with depth > 1, We don't go deep
6401 * to the extent block, so just calculate a maximum record num.
6402 */
6403 if (!xv->xr_list.l_tree_depth)
Tao Ma8ff6af82009-12-23 14:31:15 +08006404 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
Tao Ma2999d122009-08-18 11:43:55 +08006405 else
6406 *num_recs += ocfs2_clusters_for_bytes(sb,
6407 XATTR_SIZE_MAX);
6408 }
6409
6410 return ret;
6411}
6412
6413/* Used by xattr inode and block to return the right xv and buffer_head. */
6414static int ocfs2_get_xattr_value_root(struct super_block *sb,
6415 struct buffer_head *bh,
6416 struct ocfs2_xattr_header *xh,
6417 int offset,
6418 struct ocfs2_xattr_value_root **xv,
6419 struct buffer_head **ret_bh,
6420 void *para)
6421{
6422 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6423
6424 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6425 le16_to_cpu(xe->xe_name_offset) +
6426 OCFS2_XATTR_SIZE(xe->xe_name_len));
6427
6428 if (ret_bh)
6429 *ret_bh = bh;
6430
6431 return 0;
6432}
6433
6434/*
6435 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6436 * It is only used for inline xattr and xattr block.
6437 */
6438static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6439 struct ocfs2_xattr_header *xh,
6440 struct buffer_head *ref_root_bh,
6441 int *credits,
6442 struct ocfs2_alloc_context **meta_ac)
6443{
6444 int ret, meta_add = 0, num_recs = 0;
6445 struct ocfs2_refcount_block *rb =
6446 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6447
6448 *credits = 0;
6449
6450 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6451 &meta_add, credits, &num_recs,
6452 ocfs2_get_xattr_value_root,
6453 NULL);
6454 if (ret) {
6455 mlog_errno(ret);
6456 goto out;
6457 }
6458
6459 /*
6460 * We need to add/modify num_recs in refcount tree, so just calculate
6461 * an approximate number we need for refcount tree change.
6462 * Sometimes we need to split the tree, and after split, half recs
6463 * will be moved to the new block, and a new block can only provide
6464 * half number of recs. So we multiple new blocks by 2.
6465 */
6466 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6467 meta_add += num_recs;
6468 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6469 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6470 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6471 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6472 else
6473 *credits += 1;
6474
6475 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6476 if (ret)
6477 mlog_errno(ret);
6478
6479out:
6480 return ret;
6481}
6482
6483/*
6484 * Given a xattr header, reflink all the xattrs in this container.
6485 * It can be used for inode, block and bucket.
6486 *
6487 * NOTE:
6488 * Before we call this function, the caller has memcpy the xattr in
6489 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006490 *
6491 * If args.xattr_reflinked is set, call it to decide whether the xe should
6492 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006493 */
6494static int ocfs2_reflink_xattr_header(handle_t *handle,
6495 struct ocfs2_xattr_reflink *args,
6496 struct buffer_head *old_bh,
6497 struct ocfs2_xattr_header *xh,
6498 struct buffer_head *new_bh,
6499 struct ocfs2_xattr_header *new_xh,
6500 struct ocfs2_xattr_value_buf *vb,
6501 struct ocfs2_alloc_context *meta_ac,
6502 get_xattr_value_root *func,
6503 void *para)
6504{
Tao Ma0fe9b662009-08-18 11:47:56 +08006505 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006506 struct super_block *sb = args->old_inode->i_sb;
6507 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006508 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006509 struct ocfs2_xattr_value_root *xv, *new_xv;
6510 struct ocfs2_extent_tree data_et;
6511 u32 clusters, cpos, p_cluster, num_clusters;
6512 unsigned int ext_flags = 0;
6513
6514 mlog(0, "reflink xattr in container %llu, count = %u\n",
6515 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006516
6517 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6518 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006519 xe = &xh->xh_entries[i];
6520
Tao Ma0fe9b662009-08-18 11:47:56 +08006521 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6522 xe = &new_xh->xh_entries[j];
6523
6524 le16_add_cpu(&new_xh->xh_count, -1);
6525 if (new_xh->xh_count) {
6526 memmove(xe, xe + 1,
6527 (void *)last - (void *)xe);
6528 memset(last, 0,
6529 sizeof(struct ocfs2_xattr_entry));
6530 }
6531
6532 /*
6533 * We don't want j to increase in the next round since
6534 * it is already moved ahead.
6535 */
6536 j--;
6537 continue;
6538 }
6539
Tao Ma2999d122009-08-18 11:43:55 +08006540 if (ocfs2_xattr_is_local(xe))
6541 continue;
6542
6543 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6544 if (ret) {
6545 mlog_errno(ret);
6546 break;
6547 }
6548
Tao Ma0fe9b662009-08-18 11:47:56 +08006549 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006550 if (ret) {
6551 mlog_errno(ret);
6552 break;
6553 }
6554
6555 /*
6556 * For the xattr which has l_tree_depth = 0, all the extent
6557 * recs have already be copied to the new xh with the
6558 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6559 * increase the refount count int the refcount tree.
6560 *
6561 * For the xattr which has l_tree_depth > 0, we need
6562 * to initialize it to the empty default value root,
6563 * and then insert the extents one by one.
6564 */
6565 if (xv->xr_list.l_tree_depth) {
6566 memcpy(new_xv, &def_xv, sizeof(def_xv));
6567 vb->vb_xv = new_xv;
6568 vb->vb_bh = value_bh;
6569 ocfs2_init_xattr_value_extent_tree(&data_et,
6570 INODE_CACHE(args->new_inode), vb);
6571 }
6572
6573 clusters = le32_to_cpu(xv->xr_clusters);
6574 cpos = 0;
6575 while (cpos < clusters) {
6576 ret = ocfs2_xattr_get_clusters(args->old_inode,
6577 cpos,
6578 &p_cluster,
6579 &num_clusters,
6580 &xv->xr_list,
6581 &ext_flags);
6582 if (ret) {
6583 mlog_errno(ret);
6584 goto out;
6585 }
6586
6587 BUG_ON(!p_cluster);
6588
6589 if (xv->xr_list.l_tree_depth) {
6590 ret = ocfs2_insert_extent(handle,
6591 &data_et, cpos,
6592 ocfs2_clusters_to_blocks(
6593 args->old_inode->i_sb,
6594 p_cluster),
6595 num_clusters, ext_flags,
6596 meta_ac);
6597 if (ret) {
6598 mlog_errno(ret);
6599 goto out;
6600 }
6601 }
6602
6603 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6604 args->ref_root_bh,
6605 p_cluster, num_clusters,
6606 meta_ac, args->dealloc);
6607 if (ret) {
6608 mlog_errno(ret);
6609 goto out;
6610 }
6611
6612 cpos += num_clusters;
6613 }
6614 }
6615
6616out:
6617 return ret;
6618}
6619
6620static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6621{
6622 int ret = 0, credits = 0;
6623 handle_t *handle;
6624 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6625 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6626 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6627 int header_off = osb->sb->s_blocksize - inline_size;
6628 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6629 (args->old_bh->b_data + header_off);
6630 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6631 (args->new_bh->b_data + header_off);
6632 struct ocfs2_alloc_context *meta_ac = NULL;
6633 struct ocfs2_inode_info *new_oi;
6634 struct ocfs2_dinode *new_di;
6635 struct ocfs2_xattr_value_buf vb = {
6636 .vb_bh = args->new_bh,
6637 .vb_access = ocfs2_journal_access_di,
6638 };
6639
6640 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6641 &credits, &meta_ac);
6642 if (ret) {
6643 mlog_errno(ret);
6644 goto out;
6645 }
6646
6647 handle = ocfs2_start_trans(osb, credits);
6648 if (IS_ERR(handle)) {
6649 ret = PTR_ERR(handle);
6650 mlog_errno(ret);
6651 goto out;
6652 }
6653
6654 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6655 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6656 if (ret) {
6657 mlog_errno(ret);
6658 goto out_commit;
6659 }
6660
6661 memcpy(args->new_bh->b_data + header_off,
6662 args->old_bh->b_data + header_off, inline_size);
6663
6664 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6665 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6666
6667 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6668 args->new_bh, new_xh, &vb, meta_ac,
6669 ocfs2_get_xattr_value_root, NULL);
6670 if (ret) {
6671 mlog_errno(ret);
6672 goto out_commit;
6673 }
6674
6675 new_oi = OCFS2_I(args->new_inode);
6676 spin_lock(&new_oi->ip_lock);
6677 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6678 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6679 spin_unlock(&new_oi->ip_lock);
6680
6681 ocfs2_journal_dirty(handle, args->new_bh);
6682
6683out_commit:
6684 ocfs2_commit_trans(osb, handle);
6685
6686out:
6687 if (meta_ac)
6688 ocfs2_free_alloc_context(meta_ac);
6689 return ret;
6690}
6691
6692static int ocfs2_create_empty_xattr_block(struct inode *inode,
6693 struct buffer_head *fe_bh,
6694 struct buffer_head **ret_bh,
6695 int indexed)
6696{
6697 int ret;
6698 handle_t *handle;
6699 struct ocfs2_alloc_context *meta_ac;
6700 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6701
6702 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6703 if (ret < 0) {
6704 mlog_errno(ret);
6705 return ret;
6706 }
6707
6708 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6709 if (IS_ERR(handle)) {
6710 ret = PTR_ERR(handle);
6711 mlog_errno(ret);
6712 goto out;
6713 }
6714
6715 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6716 (unsigned long long)fe_bh->b_blocknr, indexed);
6717 ret = ocfs2_create_xattr_block(handle, inode, fe_bh,
6718 meta_ac, ret_bh, indexed);
6719 if (ret)
6720 mlog_errno(ret);
6721
6722 ocfs2_commit_trans(osb, handle);
6723out:
6724 ocfs2_free_alloc_context(meta_ac);
6725 return ret;
6726}
6727
6728static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6729 struct buffer_head *blk_bh,
6730 struct buffer_head *new_blk_bh)
6731{
6732 int ret = 0, credits = 0;
6733 handle_t *handle;
6734 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6735 struct ocfs2_dinode *new_di;
6736 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6737 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6738 struct ocfs2_xattr_block *xb =
6739 (struct ocfs2_xattr_block *)blk_bh->b_data;
6740 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6741 struct ocfs2_xattr_block *new_xb =
6742 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6743 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6744 struct ocfs2_alloc_context *meta_ac;
6745 struct ocfs2_xattr_value_buf vb = {
6746 .vb_bh = new_blk_bh,
6747 .vb_access = ocfs2_journal_access_xb,
6748 };
6749
6750 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6751 &credits, &meta_ac);
6752 if (ret) {
6753 mlog_errno(ret);
6754 return ret;
6755 }
6756
6757 /* One more credits in case we need to add xattr flags in new inode. */
6758 handle = ocfs2_start_trans(osb, credits + 1);
6759 if (IS_ERR(handle)) {
6760 ret = PTR_ERR(handle);
6761 mlog_errno(ret);
6762 goto out;
6763 }
6764
6765 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6766 ret = ocfs2_journal_access_di(handle,
6767 INODE_CACHE(args->new_inode),
6768 args->new_bh,
6769 OCFS2_JOURNAL_ACCESS_WRITE);
6770 if (ret) {
6771 mlog_errno(ret);
6772 goto out_commit;
6773 }
6774 }
6775
6776 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6777 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6778 if (ret) {
6779 mlog_errno(ret);
6780 goto out_commit;
6781 }
6782
6783 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6784 osb->sb->s_blocksize - header_off);
6785
6786 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6787 new_blk_bh, new_xh, &vb, meta_ac,
6788 ocfs2_get_xattr_value_root, NULL);
6789 if (ret) {
6790 mlog_errno(ret);
6791 goto out_commit;
6792 }
6793
6794 ocfs2_journal_dirty(handle, new_blk_bh);
6795
6796 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6797 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6798 spin_lock(&new_oi->ip_lock);
6799 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6800 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6801 spin_unlock(&new_oi->ip_lock);
6802
6803 ocfs2_journal_dirty(handle, args->new_bh);
6804 }
6805
6806out_commit:
6807 ocfs2_commit_trans(osb, handle);
6808
6809out:
6810 ocfs2_free_alloc_context(meta_ac);
6811 return ret;
6812}
6813
6814struct ocfs2_reflink_xattr_tree_args {
6815 struct ocfs2_xattr_reflink *reflink;
6816 struct buffer_head *old_blk_bh;
6817 struct buffer_head *new_blk_bh;
6818 struct ocfs2_xattr_bucket *old_bucket;
6819 struct ocfs2_xattr_bucket *new_bucket;
6820};
6821
6822/*
6823 * NOTE:
6824 * We have to handle the case that both old bucket and new bucket
6825 * will call this function to get the right ret_bh.
6826 * So The caller must give us the right bh.
6827 */
6828static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6829 struct buffer_head *bh,
6830 struct ocfs2_xattr_header *xh,
6831 int offset,
6832 struct ocfs2_xattr_value_root **xv,
6833 struct buffer_head **ret_bh,
6834 void *para)
6835{
6836 struct ocfs2_reflink_xattr_tree_args *args =
6837 (struct ocfs2_reflink_xattr_tree_args *)para;
6838 struct ocfs2_xattr_bucket *bucket;
6839
6840 if (bh == args->old_bucket->bu_bhs[0])
6841 bucket = args->old_bucket;
6842 else
6843 bucket = args->new_bucket;
6844
6845 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6846 xv, ret_bh);
6847}
6848
6849struct ocfs2_value_tree_metas {
6850 int num_metas;
6851 int credits;
6852 int num_recs;
6853};
6854
6855static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6856 struct buffer_head *bh,
6857 struct ocfs2_xattr_header *xh,
6858 int offset,
6859 struct ocfs2_xattr_value_root **xv,
6860 struct buffer_head **ret_bh,
6861 void *para)
6862{
6863 struct ocfs2_xattr_bucket *bucket =
6864 (struct ocfs2_xattr_bucket *)para;
6865
6866 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6867 xv, ret_bh);
6868}
6869
6870static int ocfs2_calc_value_tree_metas(struct inode *inode,
6871 struct ocfs2_xattr_bucket *bucket,
6872 void *para)
6873{
6874 struct ocfs2_value_tree_metas *metas =
6875 (struct ocfs2_value_tree_metas *)para;
6876 struct ocfs2_xattr_header *xh =
6877 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6878
6879 /* Add the credits for this bucket first. */
6880 metas->credits += bucket->bu_blocks;
6881 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6882 xh, &metas->num_metas,
6883 &metas->credits, &metas->num_recs,
6884 ocfs2_value_tree_metas_in_bucket,
6885 bucket);
6886}
6887
6888/*
6889 * Given a xattr extent rec starting from blkno and having len clusters,
6890 * iterate all the buckets calculate how much metadata we need for reflinking
6891 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6892 */
6893static int ocfs2_lock_reflink_xattr_rec_allocators(
6894 struct ocfs2_reflink_xattr_tree_args *args,
6895 struct ocfs2_extent_tree *xt_et,
6896 u64 blkno, u32 len, int *credits,
6897 struct ocfs2_alloc_context **meta_ac,
6898 struct ocfs2_alloc_context **data_ac)
6899{
6900 int ret, num_free_extents;
6901 struct ocfs2_value_tree_metas metas;
6902 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6903 struct ocfs2_refcount_block *rb;
6904
6905 memset(&metas, 0, sizeof(metas));
6906
6907 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6908 ocfs2_calc_value_tree_metas, &metas);
6909 if (ret) {
6910 mlog_errno(ret);
6911 goto out;
6912 }
6913
6914 *credits = metas.credits;
6915
6916 /*
6917 * Calculate we need for refcount tree change.
6918 *
6919 * We need to add/modify num_recs in refcount tree, so just calculate
6920 * an approximate number we need for refcount tree change.
6921 * Sometimes we need to split the tree, and after split, half recs
6922 * will be moved to the new block, and a new block can only provide
6923 * half number of recs. So we multiple new blocks by 2.
6924 * In the end, we have to add credits for modifying the already
6925 * existed refcount block.
6926 */
6927 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6928 metas.num_recs =
6929 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6930 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6931 metas.num_metas += metas.num_recs;
6932 *credits += metas.num_recs +
6933 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6934 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6935 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6936 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6937 else
6938 *credits += 1;
6939
6940 /* count in the xattr tree change. */
6941 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6942 if (num_free_extents < 0) {
6943 ret = num_free_extents;
6944 mlog_errno(ret);
6945 goto out;
6946 }
6947
6948 if (num_free_extents < len)
6949 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6950
6951 *credits += ocfs2_calc_extend_credits(osb->sb,
6952 xt_et->et_root_el, len);
6953
6954 if (metas.num_metas) {
6955 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6956 meta_ac);
6957 if (ret) {
6958 mlog_errno(ret);
6959 goto out;
6960 }
6961 }
6962
6963 if (len) {
6964 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6965 if (ret)
6966 mlog_errno(ret);
6967 }
6968out:
6969 if (ret) {
6970 if (*meta_ac) {
6971 ocfs2_free_alloc_context(*meta_ac);
6972 meta_ac = NULL;
6973 }
6974 }
6975
6976 return ret;
6977}
6978
6979static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6980 u64 blkno, u64 new_blkno, u32 clusters,
6981 struct ocfs2_alloc_context *meta_ac,
6982 struct ocfs2_alloc_context *data_ac,
6983 struct ocfs2_reflink_xattr_tree_args *args)
6984{
6985 int i, j, ret = 0;
6986 struct super_block *sb = args->reflink->old_inode->i_sb;
6987 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6988 u32 num_buckets = clusters * bpc;
6989 int bpb = args->old_bucket->bu_blocks;
6990 struct ocfs2_xattr_value_buf vb = {
6991 .vb_access = ocfs2_journal_access,
6992 };
6993
6994 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6995 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6996 if (ret) {
6997 mlog_errno(ret);
6998 break;
6999 }
7000
7001 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
7002 if (ret) {
7003 mlog_errno(ret);
7004 break;
7005 }
7006
7007 /*
7008 * The real bucket num in this series of blocks is stored
7009 * in the 1st bucket.
7010 */
7011 if (i == 0)
7012 num_buckets = le16_to_cpu(
7013 bucket_xh(args->old_bucket)->xh_num_buckets);
7014
7015 ret = ocfs2_xattr_bucket_journal_access(handle,
7016 args->new_bucket,
7017 OCFS2_JOURNAL_ACCESS_CREATE);
7018 if (ret) {
7019 mlog_errno(ret);
7020 break;
7021 }
7022
7023 for (j = 0; j < bpb; j++)
7024 memcpy(bucket_block(args->new_bucket, j),
7025 bucket_block(args->old_bucket, j),
7026 sb->s_blocksize);
7027
7028 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7029
7030 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
7031 args->old_bucket->bu_bhs[0],
7032 bucket_xh(args->old_bucket),
7033 args->new_bucket->bu_bhs[0],
7034 bucket_xh(args->new_bucket),
7035 &vb, meta_ac,
7036 ocfs2_get_reflink_xattr_value_root,
7037 args);
7038 if (ret) {
7039 mlog_errno(ret);
7040 break;
7041 }
7042
7043 /*
7044 * Re-access and dirty the bucket to calculate metaecc.
7045 * Because we may extend the transaction in reflink_xattr_header
7046 * which will let the already accessed block gone.
7047 */
7048 ret = ocfs2_xattr_bucket_journal_access(handle,
7049 args->new_bucket,
7050 OCFS2_JOURNAL_ACCESS_WRITE);
7051 if (ret) {
7052 mlog_errno(ret);
7053 break;
7054 }
7055
7056 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
7057 ocfs2_xattr_bucket_relse(args->old_bucket);
7058 ocfs2_xattr_bucket_relse(args->new_bucket);
7059 }
7060
7061 ocfs2_xattr_bucket_relse(args->old_bucket);
7062 ocfs2_xattr_bucket_relse(args->new_bucket);
7063 return ret;
7064}
7065/*
7066 * Create the same xattr extent record in the new inode's xattr tree.
7067 */
7068static int ocfs2_reflink_xattr_rec(struct inode *inode,
7069 struct buffer_head *root_bh,
7070 u64 blkno,
7071 u32 cpos,
7072 u32 len,
7073 void *para)
7074{
7075 int ret, credits = 0;
7076 u32 p_cluster, num_clusters;
7077 u64 new_blkno;
7078 handle_t *handle;
7079 struct ocfs2_reflink_xattr_tree_args *args =
7080 (struct ocfs2_reflink_xattr_tree_args *)para;
7081 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7082 struct ocfs2_alloc_context *meta_ac = NULL;
7083 struct ocfs2_alloc_context *data_ac = NULL;
7084 struct ocfs2_extent_tree et;
7085
7086 ocfs2_init_xattr_tree_extent_tree(&et,
7087 INODE_CACHE(args->reflink->new_inode),
7088 args->new_blk_bh);
7089
7090 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
7091 len, &credits,
7092 &meta_ac, &data_ac);
7093 if (ret) {
7094 mlog_errno(ret);
7095 goto out;
7096 }
7097
7098 handle = ocfs2_start_trans(osb, credits);
7099 if (IS_ERR(handle)) {
7100 ret = PTR_ERR(handle);
7101 mlog_errno(ret);
7102 goto out;
7103 }
7104
7105 ret = ocfs2_claim_clusters(osb, handle, data_ac,
7106 len, &p_cluster, &num_clusters);
7107 if (ret) {
7108 mlog_errno(ret);
7109 goto out_commit;
7110 }
7111
7112 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
7113
7114 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
7115 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
7116 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
7117 meta_ac, data_ac, args);
7118 if (ret) {
7119 mlog_errno(ret);
7120 goto out_commit;
7121 }
7122
7123 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
7124 (unsigned long long)new_blkno, len, cpos);
7125 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
7126 len, 0, meta_ac);
7127 if (ret)
7128 mlog_errno(ret);
7129
7130out_commit:
7131 ocfs2_commit_trans(osb, handle);
7132
7133out:
7134 if (meta_ac)
7135 ocfs2_free_alloc_context(meta_ac);
7136 if (data_ac)
7137 ocfs2_free_alloc_context(data_ac);
7138 return ret;
7139}
7140
7141/*
7142 * Create reflinked xattr buckets.
7143 * We will add bucket one by one, and refcount all the xattrs in the bucket
7144 * if they are stored outside.
7145 */
7146static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7147 struct buffer_head *blk_bh,
7148 struct buffer_head *new_blk_bh)
7149{
7150 int ret;
7151 struct ocfs2_reflink_xattr_tree_args para;
7152
7153 memset(&para, 0, sizeof(para));
7154 para.reflink = args;
7155 para.old_blk_bh = blk_bh;
7156 para.new_blk_bh = new_blk_bh;
7157
7158 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7159 if (!para.old_bucket) {
7160 mlog_errno(-ENOMEM);
7161 return -ENOMEM;
7162 }
7163
7164 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7165 if (!para.new_bucket) {
7166 ret = -ENOMEM;
7167 mlog_errno(ret);
7168 goto out;
7169 }
7170
7171 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7172 ocfs2_reflink_xattr_rec,
7173 &para);
7174 if (ret)
7175 mlog_errno(ret);
7176
7177out:
7178 ocfs2_xattr_bucket_free(para.old_bucket);
7179 ocfs2_xattr_bucket_free(para.new_bucket);
7180 return ret;
7181}
7182
7183static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7184 struct buffer_head *blk_bh)
7185{
7186 int ret, indexed = 0;
7187 struct buffer_head *new_blk_bh = NULL;
7188 struct ocfs2_xattr_block *xb =
7189 (struct ocfs2_xattr_block *)blk_bh->b_data;
7190
7191
7192 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7193 indexed = 1;
7194
7195 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7196 &new_blk_bh, indexed);
7197 if (ret) {
7198 mlog_errno(ret);
7199 goto out;
7200 }
7201
7202 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7203 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7204 else
7205 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7206 if (ret)
7207 mlog_errno(ret);
7208
7209out:
7210 brelse(new_blk_bh);
7211 return ret;
7212}
7213
Tao Ma0fe9b662009-08-18 11:47:56 +08007214static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7215{
7216 int type = ocfs2_xattr_get_type(xe);
7217
7218 return type != OCFS2_XATTR_INDEX_SECURITY &&
7219 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7220 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7221}
7222
Tao Ma2999d122009-08-18 11:43:55 +08007223int ocfs2_reflink_xattrs(struct inode *old_inode,
7224 struct buffer_head *old_bh,
7225 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08007226 struct buffer_head *new_bh,
7227 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08007228{
7229 int ret;
7230 struct ocfs2_xattr_reflink args;
7231 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7232 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7233 struct buffer_head *blk_bh = NULL;
7234 struct ocfs2_cached_dealloc_ctxt dealloc;
7235 struct ocfs2_refcount_tree *ref_tree;
7236 struct buffer_head *ref_root_bh = NULL;
7237
7238 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7239 le64_to_cpu(di->i_refcount_loc),
7240 1, &ref_tree, &ref_root_bh);
7241 if (ret) {
7242 mlog_errno(ret);
7243 goto out;
7244 }
7245
7246 ocfs2_init_dealloc_ctxt(&dealloc);
7247
7248 args.old_inode = old_inode;
7249 args.new_inode = new_inode;
7250 args.old_bh = old_bh;
7251 args.new_bh = new_bh;
7252 args.ref_ci = &ref_tree->rf_ci;
7253 args.ref_root_bh = ref_root_bh;
7254 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08007255 if (preserve_security)
7256 args.xattr_reflinked = NULL;
7257 else
7258 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08007259
7260 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7261 ret = ocfs2_reflink_xattr_inline(&args);
7262 if (ret) {
7263 mlog_errno(ret);
7264 goto out_unlock;
7265 }
7266 }
7267
7268 if (!di->i_xattr_loc)
7269 goto out_unlock;
7270
7271 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7272 &blk_bh);
7273 if (ret < 0) {
7274 mlog_errno(ret);
7275 goto out_unlock;
7276 }
7277
7278 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7279 if (ret)
7280 mlog_errno(ret);
7281
7282 brelse(blk_bh);
7283
7284out_unlock:
7285 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7286 ref_tree, 1);
7287 brelse(ref_root_bh);
7288
7289 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7290 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7291 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7292 }
7293
7294out:
7295 return ret;
7296}
7297
7298/*
Tao Ma0fe9b662009-08-18 11:47:56 +08007299 * Initialize security and acl for a already created inode.
7300 * Used for reflink a non-preserve-security file.
7301 *
7302 * It uses common api like ocfs2_xattr_set, so the caller
7303 * must not hold any lock expect i_mutex.
7304 */
7305int ocfs2_init_security_and_acl(struct inode *dir,
7306 struct inode *inode)
7307{
7308 int ret = 0;
7309 struct buffer_head *dir_bh = NULL;
7310 struct ocfs2_security_xattr_info si = {
7311 .enable = 1,
7312 };
7313
7314 ret = ocfs2_init_security_get(inode, dir, &si);
7315 if (!ret) {
Christoph Hellwig431547b2009-11-13 09:52:56 +00007316 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7317 si.name, si.value, si.value_len,
7318 XATTR_CREATE);
Tao Ma0fe9b662009-08-18 11:47:56 +08007319 if (ret) {
7320 mlog_errno(ret);
7321 goto leave;
7322 }
7323 } else if (ret != -EOPNOTSUPP) {
7324 mlog_errno(ret);
7325 goto leave;
7326 }
7327
7328 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7329 if (ret) {
7330 mlog_errno(ret);
7331 goto leave;
7332 }
7333
7334 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7335 if (ret)
7336 mlog_errno(ret);
7337
7338 ocfs2_inode_unlock(dir, 0);
7339 brelse(dir_bh);
7340leave:
7341 return ret;
7342}
7343/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007344 * 'security' attributes support
7345 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007346static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
Tiger Yang923f7f32008-11-14 11:16:27 +08007347 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007348 size_t name_len, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007349{
7350 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7351 const size_t total_len = prefix_len + name_len + 1;
7352
7353 if (list && total_len <= list_size) {
7354 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7355 memcpy(list + prefix_len, name, name_len);
7356 list[prefix_len + name_len] = '\0';
7357 }
7358 return total_len;
7359}
7360
Christoph Hellwig431547b2009-11-13 09:52:56 +00007361static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7362 void *buffer, size_t size, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007363{
7364 if (strcmp(name, "") == 0)
7365 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007366 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7367 name, buffer, size);
Tiger Yang923f7f32008-11-14 11:16:27 +08007368}
7369
Christoph Hellwig431547b2009-11-13 09:52:56 +00007370static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7371 const void *value, size_t size, int flags, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007372{
7373 if (strcmp(name, "") == 0)
7374 return -EINVAL;
7375
Christoph Hellwig431547b2009-11-13 09:52:56 +00007376 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7377 name, value, size, flags);
Tiger Yang923f7f32008-11-14 11:16:27 +08007378}
7379
Tiger Yang534eadd2008-11-14 11:16:41 +08007380int ocfs2_init_security_get(struct inode *inode,
7381 struct inode *dir,
7382 struct ocfs2_security_xattr_info *si)
7383{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007384 /* check whether ocfs2 support feature xattr */
7385 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7386 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08007387 return security_inode_init_security(inode, dir, &si->name, &si->value,
7388 &si->value_len);
7389}
7390
7391int ocfs2_init_security_set(handle_t *handle,
7392 struct inode *inode,
7393 struct buffer_head *di_bh,
7394 struct ocfs2_security_xattr_info *si,
7395 struct ocfs2_alloc_context *xattr_ac,
7396 struct ocfs2_alloc_context *data_ac)
7397{
7398 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7399 OCFS2_XATTR_INDEX_SECURITY,
7400 si->name, si->value, si->value_len, 0,
7401 xattr_ac, data_ac);
7402}
7403
Tiger Yang923f7f32008-11-14 11:16:27 +08007404struct xattr_handler ocfs2_xattr_security_handler = {
7405 .prefix = XATTR_SECURITY_PREFIX,
7406 .list = ocfs2_xattr_security_list,
7407 .get = ocfs2_xattr_security_get,
7408 .set = ocfs2_xattr_security_set,
7409};
7410
7411/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007412 * 'trusted' attributes support
7413 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007414static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007415 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007416 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007417{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007418 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007419 const size_t total_len = prefix_len + name_len + 1;
7420
7421 if (list && total_len <= list_size) {
7422 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7423 memcpy(list + prefix_len, name, name_len);
7424 list[prefix_len + name_len] = '\0';
7425 }
7426 return total_len;
7427}
7428
Christoph Hellwig431547b2009-11-13 09:52:56 +00007429static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7430 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007431{
7432 if (strcmp(name, "") == 0)
7433 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007434 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7435 name, buffer, size);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007436}
7437
Christoph Hellwig431547b2009-11-13 09:52:56 +00007438static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7439 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007440{
7441 if (strcmp(name, "") == 0)
7442 return -EINVAL;
7443
Christoph Hellwig431547b2009-11-13 09:52:56 +00007444 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7445 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007446}
7447
7448struct xattr_handler ocfs2_xattr_trusted_handler = {
7449 .prefix = XATTR_TRUSTED_PREFIX,
7450 .list = ocfs2_xattr_trusted_list,
7451 .get = ocfs2_xattr_trusted_get,
7452 .set = ocfs2_xattr_trusted_set,
7453};
7454
Mark Fasheh99219ae2008-10-07 14:52:59 -07007455/*
7456 * 'user' attributes support
7457 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007458static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007459 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007460 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007461{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007462 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007463 const size_t total_len = prefix_len + name_len + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007464 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007465
7466 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7467 return 0;
7468
7469 if (list && total_len <= list_size) {
7470 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7471 memcpy(list + prefix_len, name, name_len);
7472 list[prefix_len + name_len] = '\0';
7473 }
7474 return total_len;
7475}
7476
Christoph Hellwig431547b2009-11-13 09:52:56 +00007477static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7478 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007479{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007480 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007481
7482 if (strcmp(name, "") == 0)
7483 return -EINVAL;
7484 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7485 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007486 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007487 buffer, size);
7488}
7489
Christoph Hellwig431547b2009-11-13 09:52:56 +00007490static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7491 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007492{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007493 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007494
7495 if (strcmp(name, "") == 0)
7496 return -EINVAL;
7497 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7498 return -EOPNOTSUPP;
7499
Christoph Hellwig431547b2009-11-13 09:52:56 +00007500 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7501 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007502}
7503
7504struct xattr_handler ocfs2_xattr_user_handler = {
7505 .prefix = XATTR_USER_PREFIX,
7506 .list = ocfs2_xattr_user_list,
7507 .get = ocfs2_xattr_user_get,
7508 .set = ocfs2_xattr_user_set,
7509};