blob: 06fa5e77c40ef7aa0a3c0483ddbf3b9dd59f9dcf [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;
Tao Ma5f5261a2010-05-13 22:49:05 +080082 int set_abort;
Tao Ma78f30c32008-11-12 08:27:00 +080083};
84
Tiger Yangcf1d6c72008-08-18 17:11:00 +080085#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
86#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang4442f512009-02-20 11:11:50 +080087#define OCFS2_XATTR_HEADER_GAP 4
Tiger Yang534eadd2008-11-14 11:16:41 +080088#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
89 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080090 - OCFS2_XATTR_HEADER_GAP)
Tiger Yang89c38bd2008-11-14 11:17:41 +080091#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
92 - sizeof(struct ocfs2_xattr_block) \
93 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080094 - OCFS2_XATTR_HEADER_GAP)
Tiger Yangcf1d6c72008-08-18 17:11:00 +080095
96static struct ocfs2_xattr_def_value_root def_xv = {
97 .xv.xr_list.l_count = cpu_to_le16(1),
98};
99
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700100const struct xattr_handler *ocfs2_xattr_handlers[] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800101 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800102 &ocfs2_xattr_acl_access_handler,
103 &ocfs2_xattr_acl_default_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800104 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800105 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800106 NULL
107};
108
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700109static const struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800110 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800111 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
112 = &ocfs2_xattr_acl_access_handler,
113 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
114 = &ocfs2_xattr_acl_default_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800115 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800116 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800117};
118
119struct ocfs2_xattr_info {
Joel Becker6b240ff2009-08-14 18:02:52 -0700120 int xi_name_index;
121 const char *xi_name;
Joel Becker18853b92009-08-14 18:17:07 -0700122 int xi_name_len;
Joel Becker6b240ff2009-08-14 18:02:52 -0700123 const void *xi_value;
124 size_t xi_value_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800125};
126
127struct ocfs2_xattr_search {
128 struct buffer_head *inode_bh;
129 /*
130 * xattr_bh point to the block buffer head which has extended attribute
131 * when extended attribute in inode, xattr_bh is equal to inode_bh.
132 */
133 struct buffer_head *xattr_bh;
134 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700135 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800136 void *base;
137 void *end;
138 struct ocfs2_xattr_entry *here;
139 int not_found;
140};
141
Joel Becker11179f22009-08-14 16:07:44 -0700142/* Operations on struct ocfs2_xa_entry */
143struct ocfs2_xa_loc;
144struct ocfs2_xa_loc_operations {
145 /*
Joel Beckercf2bc802009-08-18 13:52:38 -0700146 * Journal functions
147 */
148 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
149 int type);
150 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
151
152 /*
Joel Becker11179f22009-08-14 16:07:44 -0700153 * Return a pointer to the appropriate buffer in loc->xl_storage
154 * at the given offset from loc->xl_header.
155 */
156 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
157
Joel Becker69a3e532009-08-17 12:24:39 -0700158 /* Can we reuse the existing entry for the new value? */
159 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
160 struct ocfs2_xattr_info *xi);
161
162 /* How much space is needed for the new value? */
163 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
164 struct ocfs2_xattr_info *xi);
165
166 /*
167 * Return the offset of the first name+value pair. This is
168 * the start of our downward-filling free space.
169 */
170 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
171
Joel Becker11179f22009-08-14 16:07:44 -0700172 /*
173 * Remove the name+value at this location. Do whatever is
174 * appropriate with the remaining name+value pairs.
175 */
176 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
Joel Becker69a3e532009-08-17 12:24:39 -0700177
178 /* Fill xl_entry with a new entry */
179 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
180
181 /* Add name+value storage to an entry */
182 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
Joel Becker3fc12af2009-08-18 13:20:27 -0700183
184 /*
185 * Initialize the value buf's access and bh fields for this entry.
186 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
187 */
188 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
189 struct ocfs2_xattr_value_buf *vb);
Joel Becker11179f22009-08-14 16:07:44 -0700190};
191
192/*
193 * Describes an xattr entry location. This is a memory structure
194 * tracking the on-disk structure.
195 */
196struct ocfs2_xa_loc {
Joel Beckercf2bc802009-08-18 13:52:38 -0700197 /* This xattr belongs to this inode */
198 struct inode *xl_inode;
199
Joel Becker11179f22009-08-14 16:07:44 -0700200 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
201 struct ocfs2_xattr_header *xl_header;
202
203 /* Bytes from xl_header to the end of the storage */
204 int xl_size;
205
206 /*
207 * The ocfs2_xattr_entry this location describes. If this is
208 * NULL, this location describes the on-disk structure where it
209 * would have been.
210 */
211 struct ocfs2_xattr_entry *xl_entry;
212
213 /*
214 * Internal housekeeping
215 */
216
217 /* Buffer(s) containing this entry */
218 void *xl_storage;
219
220 /* Operations on the storage backing this location */
221 const struct ocfs2_xa_loc_operations *xl_ops;
222};
223
Joel Becker199799a2009-08-14 19:04:15 -0700224/*
225 * Convenience functions to calculate how much space is needed for a
226 * given name+value pair
227 */
228static int namevalue_size(int name_len, uint64_t value_len)
229{
230 if (value_len > OCFS2_XATTR_INLINE_SIZE)
231 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
232 else
233 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
234}
235
236static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
237{
238 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
239}
240
241static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
242{
243 u64 value_len = le64_to_cpu(xe->xe_value_size);
244
245 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
246 ocfs2_xattr_is_local(xe));
247 return namevalue_size(xe->xe_name_len, value_len);
248}
249
250
Tao Mafd68a892009-08-18 11:43:21 +0800251static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800252 struct ocfs2_xattr_header *xh,
253 int index,
254 int *block_off,
255 int *new_offset);
256
Joel Becker54f443f2008-10-20 18:43:07 -0700257static int ocfs2_xattr_block_find(struct inode *inode,
258 int name_index,
259 const char *name,
260 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800261static int ocfs2_xattr_index_block_find(struct inode *inode,
262 struct buffer_head *root_bh,
263 int name_index,
264 const char *name,
265 struct ocfs2_xattr_search *xs);
266
Tao Ma0c044f02008-08-18 17:38:50 +0800267static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
Tao Ma47bca492009-08-18 11:43:42 +0800268 struct buffer_head *blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800269 char *buffer,
270 size_t buffer_size);
271
Tao Ma01225592008-08-18 17:38:53 +0800272static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800273 struct ocfs2_xattr_search *xs,
274 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800275
276static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
277 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800278 struct ocfs2_xattr_search *xs,
279 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800280
Tao Ma47bca492009-08-18 11:43:42 +0800281typedef int (xattr_tree_rec_func)(struct inode *inode,
282 struct buffer_head *root_bh,
283 u64 blkno, u32 cpos, u32 len, void *para);
284static int ocfs2_iterate_xattr_index_block(struct inode *inode,
285 struct buffer_head *root_bh,
286 xattr_tree_rec_func *rec_func,
287 void *para);
288static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
289 struct ocfs2_xattr_bucket *bucket,
290 void *para);
291static int ocfs2_rm_xattr_cluster(struct inode *inode,
292 struct buffer_head *root_bh,
293 u64 blkno,
294 u32 cpos,
295 u32 len,
296 void *para);
297
Joel Beckerc58b6032008-11-26 13:36:24 -0800298static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
299 u64 src_blk, u64 last_blk, u64 to_blk,
300 unsigned int start_bucket,
301 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800302static int ocfs2_prepare_refcount_xattr(struct inode *inode,
303 struct ocfs2_dinode *di,
304 struct ocfs2_xattr_info *xi,
305 struct ocfs2_xattr_search *xis,
306 struct ocfs2_xattr_search *xbs,
307 struct ocfs2_refcount_tree **ref_tree,
308 int *meta_need,
309 int *credits);
Tao Mace9c5a52009-08-18 11:43:59 +0800310static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
311 struct ocfs2_xattr_bucket *bucket,
312 int offset,
313 struct ocfs2_xattr_value_root **xv,
314 struct buffer_head **bh);
Tao Maa3944252008-08-18 17:38:54 +0800315
Tiger Yang0030e002008-10-23 16:33:33 +0800316static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
317{
318 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
319}
320
321static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
322{
323 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
324}
325
Joel Becker9c7759a2008-10-24 16:21:03 -0700326#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700327#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700328#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700329
Joel Beckerba937122008-10-24 19:13:20 -0700330static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700331{
Joel Beckerba937122008-10-24 19:13:20 -0700332 struct ocfs2_xattr_bucket *bucket;
333 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700334
Joel Beckerba937122008-10-24 19:13:20 -0700335 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
336
337 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
338 if (bucket) {
339 bucket->bu_inode = inode;
340 bucket->bu_blocks = blks;
341 }
342
343 return bucket;
344}
345
346static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
347{
348 int i;
349
350 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700351 brelse(bucket->bu_bhs[i]);
352 bucket->bu_bhs[i] = NULL;
353 }
354}
355
Joel Beckerba937122008-10-24 19:13:20 -0700356static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
357{
358 if (bucket) {
359 ocfs2_xattr_bucket_relse(bucket);
360 bucket->bu_inode = NULL;
361 kfree(bucket);
362 }
363}
364
Joel Becker784b8162008-10-24 17:33:40 -0700365/*
366 * A bucket that has never been written to disk doesn't need to be
367 * read. We just need the buffer_heads. Don't call this for
368 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
369 * them fully.
370 */
Joel Beckerba937122008-10-24 19:13:20 -0700371static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700372 u64 xb_blkno)
373{
374 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700375
Joel Beckerba937122008-10-24 19:13:20 -0700376 for (i = 0; i < bucket->bu_blocks; i++) {
377 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
378 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700379 if (!bucket->bu_bhs[i]) {
380 rc = -EIO;
381 mlog_errno(rc);
382 break;
383 }
384
Joel Becker8cb471e2009-02-10 20:00:41 -0800385 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800386 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800387 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800388 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700389 }
390
391 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700392 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700393 return rc;
394}
395
396/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700397static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700398 u64 xb_blkno)
399{
Joel Beckerba937122008-10-24 19:13:20 -0700400 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700401
Joel Becker8cb471e2009-02-10 20:00:41 -0800402 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800403 bucket->bu_blocks, bucket->bu_bhs, 0,
404 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800405 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800406 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800407 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
408 bucket->bu_bhs,
409 bucket->bu_blocks,
410 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800411 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800412 if (rc)
413 mlog_errno(rc);
414 }
415
Joel Becker784b8162008-10-24 17:33:40 -0700416 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700417 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700418 return rc;
419}
420
Joel Becker1224be02008-10-24 18:47:33 -0700421static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700422 struct ocfs2_xattr_bucket *bucket,
423 int type)
424{
425 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700426
Joel Beckerba937122008-10-24 19:13:20 -0700427 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800428 rc = ocfs2_journal_access(handle,
429 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700430 bucket->bu_bhs[i], type);
431 if (rc) {
432 mlog_errno(rc);
433 break;
434 }
435 }
436
437 return rc;
438}
439
440static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700441 struct ocfs2_xattr_bucket *bucket)
442{
Joel Beckerba937122008-10-24 19:13:20 -0700443 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700444
Tao Mac8b9cf92009-02-24 17:40:26 -0800445 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800446 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
447 bucket->bu_bhs, bucket->bu_blocks,
448 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800449 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800450
Joel Beckerba937122008-10-24 19:13:20 -0700451 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700452 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
453}
454
Joel Beckerba937122008-10-24 19:13:20 -0700455static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700456 struct ocfs2_xattr_bucket *src)
457{
458 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700459 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700460
Joel Beckerba937122008-10-24 19:13:20 -0700461 BUG_ON(dest->bu_blocks != src->bu_blocks);
462 BUG_ON(dest->bu_inode != src->bu_inode);
463
464 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700465 memcpy(bucket_block(dest, i), bucket_block(src, i),
466 blocksize);
467 }
468}
Joel Becker1224be02008-10-24 18:47:33 -0700469
Joel Becker4ae1d692008-11-13 14:49:18 -0800470static int ocfs2_validate_xattr_block(struct super_block *sb,
471 struct buffer_head *bh)
472{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700473 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800474 struct ocfs2_xattr_block *xb =
475 (struct ocfs2_xattr_block *)bh->b_data;
476
477 mlog(0, "Validating xattr block %llu\n",
478 (unsigned long long)bh->b_blocknr);
479
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700480 BUG_ON(!buffer_uptodate(bh));
481
482 /*
483 * If the ecc fails, we return the error but otherwise
484 * leave the filesystem running. We know any error is
485 * local to this block.
486 */
487 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
488 if (rc)
489 return rc;
490
491 /*
492 * Errors after here are fatal
493 */
494
Joel Becker4ae1d692008-11-13 14:49:18 -0800495 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
496 ocfs2_error(sb,
497 "Extended attribute block #%llu has bad "
498 "signature %.*s",
499 (unsigned long long)bh->b_blocknr, 7,
500 xb->xb_signature);
501 return -EINVAL;
502 }
503
504 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
505 ocfs2_error(sb,
506 "Extended attribute block #%llu has an "
507 "invalid xb_blkno of %llu",
508 (unsigned long long)bh->b_blocknr,
509 (unsigned long long)le64_to_cpu(xb->xb_blkno));
510 return -EINVAL;
511 }
512
513 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
514 ocfs2_error(sb,
515 "Extended attribute block #%llu has an invalid "
516 "xb_fs_generation of #%u",
517 (unsigned long long)bh->b_blocknr,
518 le32_to_cpu(xb->xb_fs_generation));
519 return -EINVAL;
520 }
521
522 return 0;
523}
524
525static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
526 struct buffer_head **bh)
527{
528 int rc;
529 struct buffer_head *tmp = *bh;
530
Joel Becker8cb471e2009-02-10 20:00:41 -0800531 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800532 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800533
534 /* If ocfs2_read_block() got us a new bh, pass it up. */
535 if (!rc && !*bh)
536 *bh = tmp;
537
538 return rc;
539}
540
Tao Ma936b8832008-10-09 23:06:14 +0800541static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800542{
Stephen Hemminger537d81c2010-05-13 17:53:22 -0700543 const struct xattr_handler *handler = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800544
545 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
546 handler = ocfs2_xattr_handler_map[name_index];
547
Tao Ma936b8832008-10-09 23:06:14 +0800548 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800549}
550
Mark Fasheh40daa162008-10-07 14:31:42 -0700551static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800552 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700553 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800554{
555 /* Get hash value of uuid from super block */
556 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
557 int i;
558
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800559 /* hash extended attribute name */
560 for (i = 0; i < name_len; i++) {
561 hash = (hash << OCFS2_HASH_SHIFT) ^
562 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
563 *name++;
564 }
565
566 return hash;
567}
568
Tiger Yang534eadd2008-11-14 11:16:41 +0800569static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
570{
Joel Becker199799a2009-08-14 19:04:15 -0700571 return namevalue_size(name_len, value_len) +
572 sizeof(struct ocfs2_xattr_entry);
573}
Tiger Yang534eadd2008-11-14 11:16:41 +0800574
Joel Becker199799a2009-08-14 19:04:15 -0700575static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
576{
577 return namevalue_size_xi(xi) +
578 sizeof(struct ocfs2_xattr_entry);
579}
Tiger Yang534eadd2008-11-14 11:16:41 +0800580
Joel Becker199799a2009-08-14 19:04:15 -0700581static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
582{
583 return namevalue_size_xe(xe) +
584 sizeof(struct ocfs2_xattr_entry);
Tiger Yang534eadd2008-11-14 11:16:41 +0800585}
586
587int ocfs2_calc_security_init(struct inode *dir,
588 struct ocfs2_security_xattr_info *si,
589 int *want_clusters,
590 int *xattr_credits,
591 struct ocfs2_alloc_context **xattr_ac)
592{
593 int ret = 0;
594 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
595 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
596 si->value_len);
597
598 /*
599 * The max space of security xattr taken inline is
600 * 256(name) + 80(value) + 16(entry) = 352 bytes,
601 * So reserve one metadata block for it is ok.
602 */
603 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
604 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
605 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
606 if (ret) {
607 mlog_errno(ret);
608 return ret;
609 }
610 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
611 }
612
613 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800614 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
615 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
616 si->value_len);
617
618 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
619 new_clusters);
620 *want_clusters += new_clusters;
621 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800622 return ret;
623}
624
Tiger Yang89c38bd2008-11-14 11:17:41 +0800625int ocfs2_calc_xattr_init(struct inode *dir,
626 struct buffer_head *dir_bh,
627 int mode,
628 struct ocfs2_security_xattr_info *si,
629 int *want_clusters,
630 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800631 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800632{
633 int ret = 0;
634 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800635 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800636
637 if (si->enable)
638 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
639 si->value_len);
640
641 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
642 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
643 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
644 "", NULL, 0);
645 if (acl_len > 0) {
646 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
647 if (S_ISDIR(mode))
648 a_size <<= 1;
649 } else if (acl_len != 0 && acl_len != -ENODATA) {
650 mlog_errno(ret);
651 return ret;
652 }
653 }
654
655 if (!(s_size + a_size))
656 return ret;
657
658 /*
659 * The max space of security xattr taken inline is
660 * 256(name) + 80(value) + 16(entry) = 352 bytes,
661 * The max space of acl xattr taken inline is
662 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
663 * when blocksize = 512, may reserve one more cluser for
664 * xattr bucket, otherwise reserve one metadata block
665 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800666 * If this is a new directory with inline data,
667 * we choose to reserve the entire inline area for
668 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800669 */
670 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800671 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800672 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800673 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800674 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
675 }
676
677 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
678 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
679 *want_clusters += 1;
680 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
681 }
682
Tiger Yang0e445b62008-12-09 16:42:51 +0800683 /*
684 * reserve credits and clusters for xattrs which has large value
685 * and have to be set outside
686 */
687 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
688 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
689 si->value_len);
690 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
691 new_clusters);
692 *want_clusters += new_clusters;
693 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800694 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
695 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800696 /* for directory, it has DEFAULT and ACCESS two types of acls */
697 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
698 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
699 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
700 new_clusters);
701 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800702 }
703
704 return ret;
705}
706
Tao Maf56654c2008-08-18 17:38:48 +0800707static int ocfs2_xattr_extend_allocation(struct inode *inode,
708 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800709 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800710 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800711{
Tao Maa78f9f42010-07-09 14:53:11 +0800712 int status = 0, credits;
Tao Ma85db90e2008-11-12 08:27:01 +0800713 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800714 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800715 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700716 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800717
718 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
719
Joel Becker5e404e92009-02-13 03:54:22 -0800720 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700721
Tao Maa78f9f42010-07-09 14:53:11 +0800722 while (clusters_to_add) {
723 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
724 OCFS2_JOURNAL_ACCESS_WRITE);
725 if (status < 0) {
726 mlog_errno(status);
727 break;
728 }
729
730 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
731 status = ocfs2_add_clusters_in_btree(handle,
732 &et,
733 &logical_start,
734 clusters_to_add,
735 0,
736 ctxt->data_ac,
737 ctxt->meta_ac,
738 &why);
739 if ((status < 0) && (status != -EAGAIN)) {
740 if (status != -ENOSPC)
741 mlog_errno(status);
742 break;
743 }
744
745 ocfs2_journal_dirty(handle, vb->vb_bh);
746
747 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) -
748 prev_clusters;
749
750 if (why != RESTART_NONE && clusters_to_add) {
751 /*
752 * We can only fail in case the alloc file doesn't give
753 * up enough clusters.
754 */
755 BUG_ON(why == RESTART_META);
756
757 mlog(0, "restarting xattr value extension for %u"
758 " clusters,.\n", clusters_to_add);
759 credits = ocfs2_calc_extend_credits(inode->i_sb,
760 &vb->vb_xv->xr_list,
761 clusters_to_add);
762 status = ocfs2_extend_trans(handle, credits);
763 if (status < 0) {
764 status = -ENOMEM;
765 mlog_errno(status);
766 break;
767 }
768 }
Tao Maf56654c2008-08-18 17:38:48 +0800769 }
770
Tao Maf56654c2008-08-18 17:38:48 +0800771 return status;
772}
773
774static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800775 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800776 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800777 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800778 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800779{
780 int ret;
781 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800782 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700783 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800784
Joel Becker5e404e92009-02-13 03:54:22 -0800785 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700786
Joel Becker0cf2f762009-02-12 16:41:25 -0800787 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800788 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800789 if (ret) {
790 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800791 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800792 }
793
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800794 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800795 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800796 if (ret) {
797 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800798 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800799 }
800
Joel Beckerd72cc722008-12-09 14:30:41 -0800801 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Joel Beckerec20cec2010-03-19 14:13:52 -0700802 ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800803
Tao Ma492a8a32009-08-18 11:43:17 +0800804 if (ext_flags & OCFS2_EXT_REFCOUNTED)
805 ret = ocfs2_decrease_refcount(inode, handle,
806 ocfs2_blocks_to_clusters(inode->i_sb,
807 phys_blkno),
808 len, ctxt->meta_ac, &ctxt->dealloc, 1);
809 else
810 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
811 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800812 if (ret)
813 mlog_errno(ret);
814
Tao Maf56654c2008-08-18 17:38:48 +0800815out:
Tao Maf56654c2008-08-18 17:38:48 +0800816 return ret;
817}
818
819static int ocfs2_xattr_shrink_size(struct inode *inode,
820 u32 old_clusters,
821 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800822 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800823 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800824{
825 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800826 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800827 u32 trunc_len, cpos, phys_cpos, alloc_size;
828 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800829
830 if (old_clusters <= new_clusters)
831 return 0;
832
833 cpos = new_clusters;
834 trunc_len = old_clusters - new_clusters;
835 while (trunc_len) {
836 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800837 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800838 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800839 if (ret) {
840 mlog_errno(ret);
841 goto out;
842 }
843
844 if (alloc_size > trunc_len)
845 alloc_size = trunc_len;
846
Joel Becker19b801f2008-12-09 14:36:50 -0800847 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800848 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800849 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800850 if (ret) {
851 mlog_errno(ret);
852 goto out;
853 }
854
855 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800856 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
857 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800858 cpos += alloc_size;
859 trunc_len -= alloc_size;
860 }
861
862out:
Tao Maf56654c2008-08-18 17:38:48 +0800863 return ret;
864}
865
866static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800867 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800868 int len,
869 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800870{
871 int ret;
872 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800873 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800874
875 if (new_clusters == old_clusters)
876 return 0;
877
878 if (new_clusters > old_clusters)
879 ret = ocfs2_xattr_extend_allocation(inode,
880 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800881 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800882 else
883 ret = ocfs2_xattr_shrink_size(inode,
884 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800885 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800886
887 return ret;
888}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800889
Tao Ma936b8832008-10-09 23:06:14 +0800890static int ocfs2_xattr_list_entry(char *buffer, size_t size,
891 size_t *result, const char *prefix,
892 const char *name, int name_len)
893{
894 char *p = buffer + *result;
895 int prefix_len = strlen(prefix);
896 int total_len = prefix_len + name_len + 1;
897
898 *result += total_len;
899
900 /* we are just looking for how big our buffer needs to be */
901 if (!size)
902 return 0;
903
904 if (*result > size)
905 return -ERANGE;
906
907 memcpy(p, prefix, prefix_len);
908 memcpy(p + prefix_len, name, name_len);
909 p[prefix_len + name_len] = '\0';
910
911 return 0;
912}
913
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800914static int ocfs2_xattr_list_entries(struct inode *inode,
915 struct ocfs2_xattr_header *header,
916 char *buffer, size_t buffer_size)
917{
Tao Ma936b8832008-10-09 23:06:14 +0800918 size_t result = 0;
919 int i, type, ret;
920 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800921
922 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
923 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800924 type = ocfs2_xattr_get_type(entry);
925 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800926
Tao Ma936b8832008-10-09 23:06:14 +0800927 if (prefix) {
928 name = (const char *)header +
929 le16_to_cpu(entry->xe_name_offset);
930
931 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
932 &result, prefix, name,
933 entry->xe_name_len);
934 if (ret)
935 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800936 }
937 }
938
Tao Ma936b8832008-10-09 23:06:14 +0800939 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800940}
941
Tao Ma8b2c0db2009-08-18 11:43:49 +0800942int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
943 struct ocfs2_dinode *di)
944{
945 struct ocfs2_xattr_header *xh;
946 int i;
947
948 xh = (struct ocfs2_xattr_header *)
949 ((void *)di + inode->i_sb->s_blocksize -
950 le16_to_cpu(di->i_xattr_inline_size));
951
952 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
953 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
954 return 1;
955
956 return 0;
957}
958
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800959static int ocfs2_xattr_ibody_list(struct inode *inode,
960 struct ocfs2_dinode *di,
961 char *buffer,
962 size_t buffer_size)
963{
964 struct ocfs2_xattr_header *header = NULL;
965 struct ocfs2_inode_info *oi = OCFS2_I(inode);
966 int ret = 0;
967
968 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
969 return ret;
970
971 header = (struct ocfs2_xattr_header *)
972 ((void *)di + inode->i_sb->s_blocksize -
973 le16_to_cpu(di->i_xattr_inline_size));
974
975 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
976
977 return ret;
978}
979
980static int ocfs2_xattr_block_list(struct inode *inode,
981 struct ocfs2_dinode *di,
982 char *buffer,
983 size_t buffer_size)
984{
985 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800986 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800987 int ret = 0;
988
989 if (!di->i_xattr_loc)
990 return ret;
991
Joel Becker4ae1d692008-11-13 14:49:18 -0800992 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
993 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800994 if (ret < 0) {
995 mlog_errno(ret);
996 return ret;
997 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800998
Tao Ma0c044f02008-08-18 17:38:50 +0800999 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +08001000 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1001 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
1002 ret = ocfs2_xattr_list_entries(inode, header,
1003 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +08001004 } else
1005 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +08001006 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -08001007
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001008 brelse(blk_bh);
1009
1010 return ret;
1011}
1012
1013ssize_t ocfs2_listxattr(struct dentry *dentry,
1014 char *buffer,
1015 size_t size)
1016{
1017 int ret = 0, i_ret = 0, b_ret = 0;
1018 struct buffer_head *di_bh = NULL;
1019 struct ocfs2_dinode *di = NULL;
1020 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1021
Tiger Yang8154da32008-08-18 17:11:46 +08001022 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1023 return -EOPNOTSUPP;
1024
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001025 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1026 return ret;
1027
1028 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1029 if (ret < 0) {
1030 mlog_errno(ret);
1031 return ret;
1032 }
1033
1034 di = (struct ocfs2_dinode *)di_bh->b_data;
1035
1036 down_read(&oi->ip_xattr_sem);
1037 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1038 if (i_ret < 0)
1039 b_ret = 0;
1040 else {
1041 if (buffer) {
1042 buffer += i_ret;
1043 size -= i_ret;
1044 }
1045 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1046 buffer, size);
1047 if (b_ret < 0)
1048 i_ret = 0;
1049 }
1050 up_read(&oi->ip_xattr_sem);
1051 ocfs2_inode_unlock(dentry->d_inode, 0);
1052
1053 brelse(di_bh);
1054
1055 return i_ret + b_ret;
1056}
1057
1058static int ocfs2_xattr_find_entry(int name_index,
1059 const char *name,
1060 struct ocfs2_xattr_search *xs)
1061{
1062 struct ocfs2_xattr_entry *entry;
1063 size_t name_len;
1064 int i, cmp = 1;
1065
1066 if (name == NULL)
1067 return -EINVAL;
1068
1069 name_len = strlen(name);
1070 entry = xs->here;
1071 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1072 cmp = name_index - ocfs2_xattr_get_type(entry);
1073 if (!cmp)
1074 cmp = name_len - entry->xe_name_len;
1075 if (!cmp)
1076 cmp = memcmp(name, (xs->base +
1077 le16_to_cpu(entry->xe_name_offset)),
1078 name_len);
1079 if (cmp == 0)
1080 break;
1081 entry += 1;
1082 }
1083 xs->here = entry;
1084
1085 return cmp ? -ENODATA : 0;
1086}
1087
1088static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +08001089 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001090 void *buffer,
1091 size_t len)
1092{
1093 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1094 u64 blkno;
1095 int i, ret = 0;
1096 size_t cplen, blocksize;
1097 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001098 struct ocfs2_extent_list *el;
1099
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001100 el = &xv->xr_list;
1101 clusters = le32_to_cpu(xv->xr_clusters);
1102 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1103 blocksize = inode->i_sb->s_blocksize;
1104
1105 cpos = 0;
1106 while (cpos < clusters) {
1107 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001108 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001109 if (ret) {
1110 mlog_errno(ret);
1111 goto out;
1112 }
1113
1114 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1115 /* Copy ocfs2_xattr_value */
1116 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001117 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1118 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001119 if (ret) {
1120 mlog_errno(ret);
1121 goto out;
1122 }
1123
1124 cplen = len >= blocksize ? blocksize : len;
1125 memcpy(buffer, bh->b_data, cplen);
1126 len -= cplen;
1127 buffer += cplen;
1128
1129 brelse(bh);
1130 bh = NULL;
1131 if (len == 0)
1132 break;
1133 }
1134 cpos += num_clusters;
1135 }
1136out:
1137 return ret;
1138}
1139
1140static int ocfs2_xattr_ibody_get(struct inode *inode,
1141 int name_index,
1142 const char *name,
1143 void *buffer,
1144 size_t buffer_size,
1145 struct ocfs2_xattr_search *xs)
1146{
1147 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1148 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001149 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001150 size_t size;
1151 int ret = 0;
1152
1153 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1154 return -ENODATA;
1155
1156 xs->end = (void *)di + inode->i_sb->s_blocksize;
1157 xs->header = (struct ocfs2_xattr_header *)
1158 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1159 xs->base = (void *)xs->header;
1160 xs->here = xs->header->xh_entries;
1161
1162 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1163 if (ret)
1164 return ret;
1165 size = le64_to_cpu(xs->here->xe_value_size);
1166 if (buffer) {
1167 if (size > buffer_size)
1168 return -ERANGE;
1169 if (ocfs2_xattr_is_local(xs->here)) {
1170 memcpy(buffer, (void *)xs->base +
1171 le16_to_cpu(xs->here->xe_name_offset) +
1172 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1173 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001174 xv = (struct ocfs2_xattr_value_root *)
1175 (xs->base + le16_to_cpu(
1176 xs->here->xe_name_offset) +
1177 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1178 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001179 buffer, size);
1180 if (ret < 0) {
1181 mlog_errno(ret);
1182 return ret;
1183 }
1184 }
1185 }
1186
1187 return size;
1188}
1189
1190static int ocfs2_xattr_block_get(struct inode *inode,
1191 int name_index,
1192 const char *name,
1193 void *buffer,
1194 size_t buffer_size,
1195 struct ocfs2_xattr_search *xs)
1196{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001197 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001198 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001199 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301200 int ret = -ENODATA, name_offset, name_len, i;
1201 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001202
Joel Beckerba937122008-10-24 19:13:20 -07001203 xs->bucket = ocfs2_xattr_bucket_new(inode);
1204 if (!xs->bucket) {
1205 ret = -ENOMEM;
1206 mlog_errno(ret);
1207 goto cleanup;
1208 }
Tao Ma589dc262008-08-18 17:38:51 +08001209
Joel Becker54f443f2008-10-20 18:43:07 -07001210 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1211 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001212 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001213 goto cleanup;
1214 }
1215
Tiger Yang6c1e1832008-11-02 19:04:21 +08001216 if (xs->not_found) {
1217 ret = -ENODATA;
1218 goto cleanup;
1219 }
1220
Joel Becker54f443f2008-10-20 18:43:07 -07001221 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001222 size = le64_to_cpu(xs->here->xe_value_size);
1223 if (buffer) {
1224 ret = -ERANGE;
1225 if (size > buffer_size)
1226 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001227
1228 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1229 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1230 i = xs->here - xs->header->xh_entries;
1231
1232 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001233 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001234 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001235 i,
1236 &block_off,
1237 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001238 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001239 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001240 if (ocfs2_xattr_is_local(xs->here)) {
1241 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001242 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001243 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001244 xv = (struct ocfs2_xattr_value_root *)
1245 (xs->base + name_offset + name_len);
1246 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001247 buffer, size);
1248 if (ret < 0) {
1249 mlog_errno(ret);
1250 goto cleanup;
1251 }
1252 }
1253 }
1254 ret = size;
1255cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001256 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001257
Joel Becker54f443f2008-10-20 18:43:07 -07001258 brelse(xs->xattr_bh);
1259 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260 return ret;
1261}
1262
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001263int ocfs2_xattr_get_nolock(struct inode *inode,
1264 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001265 int name_index,
1266 const char *name,
1267 void *buffer,
1268 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001269{
1270 int ret;
1271 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001272 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1273 struct ocfs2_xattr_search xis = {
1274 .not_found = -ENODATA,
1275 };
1276 struct ocfs2_xattr_search xbs = {
1277 .not_found = -ENODATA,
1278 };
1279
Tiger Yang8154da32008-08-18 17:11:46 +08001280 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1281 return -EOPNOTSUPP;
1282
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001283 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1284 ret = -ENODATA;
1285
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001286 xis.inode_bh = xbs.inode_bh = di_bh;
1287 di = (struct ocfs2_dinode *)di_bh->b_data;
1288
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001289 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1290 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001291 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001292 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1293 buffer_size, &xbs);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001294
1295 return ret;
1296}
1297
1298/* ocfs2_xattr_get()
1299 *
1300 * Copy an extended attribute into the buffer provided.
1301 * Buffer is NULL to compute the size of buffer required.
1302 */
1303static int ocfs2_xattr_get(struct inode *inode,
1304 int name_index,
1305 const char *name,
1306 void *buffer,
1307 size_t buffer_size)
1308{
1309 int ret;
1310 struct buffer_head *di_bh = NULL;
1311
1312 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1313 if (ret < 0) {
1314 mlog_errno(ret);
1315 return ret;
1316 }
Tao Ma5e64b0d2010-09-07 13:30:05 +08001317 down_read(&OCFS2_I(inode)->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001318 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1319 name, buffer, buffer_size);
Tao Ma5e64b0d2010-09-07 13:30:05 +08001320 up_read(&OCFS2_I(inode)->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001321
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001322 ocfs2_inode_unlock(inode, 0);
1323
1324 brelse(di_bh);
1325
1326 return ret;
1327}
1328
1329static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001330 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001331 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001332 const void *value,
1333 int value_len)
1334{
Tao Ma71d548a2008-12-05 06:20:54 +08001335 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001336 u16 blocksize = inode->i_sb->s_blocksize;
1337 u32 p_cluster, num_clusters;
1338 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1339 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1340 u64 blkno;
1341 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001342 unsigned int ext_flags;
1343 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001344
1345 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1346
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001347 while (cpos < clusters) {
1348 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001349 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001350 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001351 if (ret) {
1352 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001353 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001354 }
1355
Tao Ma492a8a32009-08-18 11:43:17 +08001356 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1357
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001358 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1359
1360 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001361 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1362 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001363 if (ret) {
1364 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001365 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001366 }
1367
1368 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001369 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001370 bh,
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1372 if (ret < 0) {
1373 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001374 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001375 }
1376
1377 cp_len = value_len > blocksize ? blocksize : value_len;
1378 memcpy(bh->b_data, value, cp_len);
1379 value_len -= cp_len;
1380 value += cp_len;
1381 if (cp_len < blocksize)
1382 memset(bh->b_data + cp_len, 0,
1383 blocksize - cp_len);
1384
Joel Beckerec20cec2010-03-19 14:13:52 -07001385 ocfs2_journal_dirty(handle, bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001386 brelse(bh);
1387 bh = NULL;
1388
1389 /*
1390 * XXX: do we need to empty all the following
1391 * blocks in this cluster?
1392 */
1393 if (!value_len)
1394 break;
1395 }
1396 cpos += num_clusters;
1397 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001398out:
1399 brelse(bh);
1400
1401 return ret;
1402}
1403
Joel Becker69a3e532009-08-17 12:24:39 -07001404static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1405 int num_entries)
1406{
1407 int free_space;
1408
1409 if (!needed_space)
1410 return 0;
1411
1412 free_space = free_start -
1413 sizeof(struct ocfs2_xattr_header) -
1414 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1415 OCFS2_XATTR_HEADER_GAP;
1416 if (free_space < 0)
1417 return -EIO;
1418 if (free_space < needed_space)
1419 return -ENOSPC;
1420
1421 return 0;
1422}
1423
Joel Beckercf2bc802009-08-18 13:52:38 -07001424static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1425 int type)
1426{
1427 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1428}
1429
1430static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1431{
1432 loc->xl_ops->xlo_journal_dirty(handle, loc);
1433}
1434
Joel Becker69a3e532009-08-17 12:24:39 -07001435/* Give a pointer into the storage for the given offset */
1436static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1437{
1438 BUG_ON(offset >= loc->xl_size);
1439 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1440}
1441
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001442/*
Joel Becker11179f22009-08-14 16:07:44 -07001443 * Wipe the name+value pair and allow the storage to reclaim it. This
1444 * must be followed by either removal of the entry or a call to
1445 * ocfs2_xa_add_namevalue().
1446 */
1447static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1448{
1449 loc->xl_ops->xlo_wipe_namevalue(loc);
1450}
1451
Joel Becker69a3e532009-08-17 12:24:39 -07001452/*
1453 * Find lowest offset to a name+value pair. This is the start of our
1454 * downward-growing free space.
1455 */
1456static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1457{
1458 return loc->xl_ops->xlo_get_free_start(loc);
1459}
1460
1461/* Can we reuse loc->xl_entry for xi? */
1462static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1463 struct ocfs2_xattr_info *xi)
1464{
1465 return loc->xl_ops->xlo_can_reuse(loc, xi);
1466}
1467
1468/* How much free space is needed to set the new value */
1469static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1470 struct ocfs2_xattr_info *xi)
1471{
1472 return loc->xl_ops->xlo_check_space(loc, xi);
1473}
1474
1475static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1476{
1477 loc->xl_ops->xlo_add_entry(loc, name_hash);
1478 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1479 /*
1480 * We can't leave the new entry's xe_name_offset at zero or
1481 * add_namevalue() will go nuts. We set it to the size of our
1482 * storage so that it can never be less than any other entry.
1483 */
1484 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1485}
1486
1487static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1488 struct ocfs2_xattr_info *xi)
1489{
1490 int size = namevalue_size_xi(xi);
1491 int nameval_offset;
1492 char *nameval_buf;
1493
1494 loc->xl_ops->xlo_add_namevalue(loc, size);
1495 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1496 loc->xl_entry->xe_name_len = xi->xi_name_len;
1497 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1498 ocfs2_xattr_set_local(loc->xl_entry,
1499 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1500
1501 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1502 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1503 memset(nameval_buf, 0, size);
1504 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1505}
1506
Joel Becker3fc12af2009-08-18 13:20:27 -07001507static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1508 struct ocfs2_xattr_value_buf *vb)
1509{
1510 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1511 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1512
1513 /* Value bufs are for value trees */
Joel Becker73857ee2009-08-18 20:26:41 -07001514 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
Joel Becker3fc12af2009-08-18 13:20:27 -07001515 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1516 (name_size + OCFS2_XATTR_ROOT_SIZE));
1517
1518 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1519 vb->vb_xv =
1520 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1521 nameval_offset +
1522 name_size);
1523}
1524
Joel Beckercf2bc802009-08-18 13:52:38 -07001525static int ocfs2_xa_block_journal_access(handle_t *handle,
1526 struct ocfs2_xa_loc *loc, int type)
1527{
1528 struct buffer_head *bh = loc->xl_storage;
1529 ocfs2_journal_access_func access;
1530
1531 if (loc->xl_size == (bh->b_size -
1532 offsetof(struct ocfs2_xattr_block,
1533 xb_attrs.xb_header)))
1534 access = ocfs2_journal_access_xb;
1535 else
1536 access = ocfs2_journal_access_di;
1537 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1538}
1539
1540static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1541 struct ocfs2_xa_loc *loc)
1542{
1543 struct buffer_head *bh = loc->xl_storage;
1544
1545 ocfs2_journal_dirty(handle, bh);
1546}
1547
Joel Becker11179f22009-08-14 16:07:44 -07001548static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1549 int offset)
1550{
Joel Becker11179f22009-08-14 16:07:44 -07001551 return (char *)loc->xl_header + offset;
1552}
1553
Joel Becker69a3e532009-08-17 12:24:39 -07001554static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1555 struct ocfs2_xattr_info *xi)
1556{
1557 /*
1558 * Block storage is strict. If the sizes aren't exact, we will
1559 * remove the old one and reinsert the new.
1560 */
1561 return namevalue_size_xe(loc->xl_entry) ==
1562 namevalue_size_xi(xi);
1563}
1564
1565static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1566{
1567 struct ocfs2_xattr_header *xh = loc->xl_header;
1568 int i, count = le16_to_cpu(xh->xh_count);
1569 int offset, free_start = loc->xl_size;
1570
1571 for (i = 0; i < count; i++) {
1572 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1573 if (offset < free_start)
1574 free_start = offset;
1575 }
1576
1577 return free_start;
1578}
1579
1580static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1581 struct ocfs2_xattr_info *xi)
1582{
1583 int count = le16_to_cpu(loc->xl_header->xh_count);
1584 int free_start = ocfs2_xa_get_free_start(loc);
1585 int needed_space = ocfs2_xi_entry_usage(xi);
1586
1587 /*
1588 * Block storage will reclaim the original entry before inserting
1589 * the new value, so we only need the difference. If the new
1590 * entry is smaller than the old one, we don't need anything.
1591 */
1592 if (loc->xl_entry) {
1593 /* Don't need space if we're reusing! */
1594 if (ocfs2_xa_can_reuse_entry(loc, xi))
1595 needed_space = 0;
1596 else
1597 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1598 }
1599 if (needed_space < 0)
1600 needed_space = 0;
1601 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1602}
1603
Joel Becker11179f22009-08-14 16:07:44 -07001604/*
1605 * Block storage for xattrs keeps the name+value pairs compacted. When
1606 * we remove one, we have to shift any that preceded it towards the end.
1607 */
1608static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1609{
1610 int i, offset;
1611 int namevalue_offset, first_namevalue_offset, namevalue_size;
1612 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1613 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Becker11179f22009-08-14 16:07:44 -07001614 int count = le16_to_cpu(xh->xh_count);
1615
1616 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07001617 namevalue_size = namevalue_size_xe(entry);
Joel Becker69a3e532009-08-17 12:24:39 -07001618 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
Joel Becker11179f22009-08-14 16:07:44 -07001619
1620 /* Shift the name+value pairs */
1621 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1622 (char *)xh + first_namevalue_offset,
1623 namevalue_offset - first_namevalue_offset);
1624 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1625
1626 /* Now tell xh->xh_entries about it */
1627 for (i = 0; i < count; i++) {
1628 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
Tao Madfe4d3d2010-03-19 15:04:23 +08001629 if (offset <= namevalue_offset)
Joel Becker11179f22009-08-14 16:07:44 -07001630 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1631 namevalue_size);
1632 }
1633
1634 /*
1635 * Note that we don't update xh_free_start or xh_name_value_len
1636 * because they're not used in block-stored xattrs.
1637 */
1638}
1639
Joel Becker69a3e532009-08-17 12:24:39 -07001640static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1641{
1642 int count = le16_to_cpu(loc->xl_header->xh_count);
1643 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1644 le16_add_cpu(&loc->xl_header->xh_count, 1);
1645 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1646}
1647
1648static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1649{
1650 int free_start = ocfs2_xa_get_free_start(loc);
1651
1652 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1653}
1654
Joel Becker3fc12af2009-08-18 13:20:27 -07001655static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1656 struct ocfs2_xattr_value_buf *vb)
1657{
1658 struct buffer_head *bh = loc->xl_storage;
1659
1660 if (loc->xl_size == (bh->b_size -
1661 offsetof(struct ocfs2_xattr_block,
1662 xb_attrs.xb_header)))
1663 vb->vb_access = ocfs2_journal_access_xb;
1664 else
1665 vb->vb_access = ocfs2_journal_access_di;
1666 vb->vb_bh = bh;
1667}
1668
Joel Becker11179f22009-08-14 16:07:44 -07001669/*
1670 * Operations for xattrs stored in blocks. This includes inline inode
1671 * storage and unindexed ocfs2_xattr_blocks.
1672 */
1673static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001674 .xlo_journal_access = ocfs2_xa_block_journal_access,
1675 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001676 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001677 .xlo_check_space = ocfs2_xa_block_check_space,
1678 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1679 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001680 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001681 .xlo_add_entry = ocfs2_xa_block_add_entry,
1682 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001683 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001684};
1685
Joel Beckercf2bc802009-08-18 13:52:38 -07001686static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1687 struct ocfs2_xa_loc *loc, int type)
1688{
1689 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1690
1691 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1692}
1693
1694static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1695 struct ocfs2_xa_loc *loc)
1696{
1697 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1698
1699 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1700}
1701
Joel Becker11179f22009-08-14 16:07:44 -07001702static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1703 int offset)
1704{
1705 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1706 int block, block_offset;
1707
Joel Becker11179f22009-08-14 16:07:44 -07001708 /* The header is at the front of the bucket */
Joel Beckercf2bc802009-08-18 13:52:38 -07001709 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1710 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
Joel Becker11179f22009-08-14 16:07:44 -07001711
1712 return bucket_block(bucket, block) + block_offset;
1713}
1714
Joel Becker69a3e532009-08-17 12:24:39 -07001715static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1716 struct ocfs2_xattr_info *xi)
1717{
1718 return namevalue_size_xe(loc->xl_entry) >=
1719 namevalue_size_xi(xi);
1720}
1721
1722static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1723{
1724 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1725 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1726}
1727
1728static int ocfs2_bucket_align_free_start(struct super_block *sb,
1729 int free_start, int size)
1730{
1731 /*
1732 * We need to make sure that the name+value pair fits within
1733 * one block.
1734 */
1735 if (((free_start - size) >> sb->s_blocksize_bits) !=
1736 ((free_start - 1) >> sb->s_blocksize_bits))
1737 free_start -= free_start % sb->s_blocksize;
1738
1739 return free_start;
1740}
1741
1742static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1743 struct ocfs2_xattr_info *xi)
1744{
1745 int rc;
1746 int count = le16_to_cpu(loc->xl_header->xh_count);
1747 int free_start = ocfs2_xa_get_free_start(loc);
1748 int needed_space = ocfs2_xi_entry_usage(xi);
1749 int size = namevalue_size_xi(xi);
Joel Beckercf2bc802009-08-18 13:52:38 -07001750 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001751
1752 /*
1753 * Bucket storage does not reclaim name+value pairs it cannot
1754 * reuse. They live as holes until the bucket fills, and then
1755 * the bucket is defragmented. However, the bucket can reclaim
1756 * the ocfs2_xattr_entry.
1757 */
1758 if (loc->xl_entry) {
1759 /* Don't need space if we're reusing! */
1760 if (ocfs2_xa_can_reuse_entry(loc, xi))
1761 needed_space = 0;
1762 else
1763 needed_space -= sizeof(struct ocfs2_xattr_entry);
1764 }
1765 BUG_ON(needed_space < 0);
1766
1767 if (free_start < size) {
1768 if (needed_space)
1769 return -ENOSPC;
1770 } else {
1771 /*
1772 * First we check if it would fit in the first place.
1773 * Below, we align the free start to a block. This may
1774 * slide us below the minimum gap. By checking unaligned
1775 * first, we avoid that error.
1776 */
1777 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1778 count);
1779 if (rc)
1780 return rc;
1781 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1782 size);
1783 }
1784 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1785}
1786
Joel Becker11179f22009-08-14 16:07:44 -07001787static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1788{
Joel Becker199799a2009-08-14 19:04:15 -07001789 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1790 -namevalue_size_xe(loc->xl_entry));
Joel Becker11179f22009-08-14 16:07:44 -07001791}
1792
Joel Becker69a3e532009-08-17 12:24:39 -07001793static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1794{
1795 struct ocfs2_xattr_header *xh = loc->xl_header;
1796 int count = le16_to_cpu(xh->xh_count);
1797 int low = 0, high = count - 1, tmp;
1798 struct ocfs2_xattr_entry *tmp_xe;
1799
1800 /*
1801 * We keep buckets sorted by name_hash, so we need to find
1802 * our insert place.
1803 */
1804 while (low <= high && count) {
1805 tmp = (low + high) / 2;
1806 tmp_xe = &xh->xh_entries[tmp];
1807
1808 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1809 low = tmp + 1;
1810 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1811 high = tmp - 1;
1812 else {
1813 low = tmp;
1814 break;
1815 }
1816 }
1817
1818 if (low != count)
1819 memmove(&xh->xh_entries[low + 1],
1820 &xh->xh_entries[low],
1821 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1822
1823 le16_add_cpu(&xh->xh_count, 1);
1824 loc->xl_entry = &xh->xh_entries[low];
1825 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1826}
1827
1828static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1829{
1830 int free_start = ocfs2_xa_get_free_start(loc);
1831 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Beckercf2bc802009-08-18 13:52:38 -07001832 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001833 int nameval_offset;
1834
1835 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1836 nameval_offset = free_start - size;
1837 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1838 xh->xh_free_start = cpu_to_le16(nameval_offset);
1839 le16_add_cpu(&xh->xh_name_value_len, size);
1840
1841}
1842
Joel Becker3fc12af2009-08-18 13:20:27 -07001843static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1844 struct ocfs2_xattr_value_buf *vb)
1845{
1846 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
Joel Beckercf2bc802009-08-18 13:52:38 -07001847 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker3fc12af2009-08-18 13:20:27 -07001848 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1849 int size = namevalue_size_xe(loc->xl_entry);
1850 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1851
1852 /* Values are not allowed to straddle block boundaries */
1853 BUG_ON(block_offset !=
1854 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1855 /* We expect the bucket to be filled in */
1856 BUG_ON(!bucket->bu_bhs[block_offset]);
1857
1858 vb->vb_access = ocfs2_journal_access;
1859 vb->vb_bh = bucket->bu_bhs[block_offset];
1860}
1861
Joel Becker11179f22009-08-14 16:07:44 -07001862/* Operations for xattrs stored in buckets. */
1863static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001864 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1865 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001866 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001867 .xlo_check_space = ocfs2_xa_bucket_check_space,
1868 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1869 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001870 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001871 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1872 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001873 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001874};
1875
Joel Becker399ff3a2009-09-01 18:38:27 -07001876static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1877{
1878 struct ocfs2_xattr_value_buf vb;
1879
1880 if (ocfs2_xattr_is_local(loc->xl_entry))
1881 return 0;
1882
1883 ocfs2_xa_fill_value_buf(loc, &vb);
1884 return le32_to_cpu(vb.vb_xv->xr_clusters);
1885}
1886
Joel Becker73857ee2009-08-18 20:26:41 -07001887static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1888 struct ocfs2_xattr_set_ctxt *ctxt)
1889{
1890 int trunc_rc, access_rc;
1891 struct ocfs2_xattr_value_buf vb;
1892
1893 ocfs2_xa_fill_value_buf(loc, &vb);
1894 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1895 ctxt);
1896
1897 /*
1898 * The caller of ocfs2_xa_value_truncate() has already called
1899 * ocfs2_xa_journal_access on the loc. However, The truncate code
1900 * calls ocfs2_extend_trans(). This may commit the previous
1901 * transaction and open a new one. If this is a bucket, truncate
1902 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1903 * the caller is expecting to dirty the entire bucket. So we must
1904 * reset the journal work. We do this even if truncate has failed,
1905 * as it could have failed after committing the extend.
1906 */
1907 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1908 OCFS2_JOURNAL_ACCESS_WRITE);
1909
1910 /* Errors in truncate take precedence */
1911 return trunc_rc ? trunc_rc : access_rc;
1912}
1913
Joel Becker11179f22009-08-14 16:07:44 -07001914static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1915{
Joel Beckerbde1e542009-08-14 16:58:38 -07001916 int index, count;
1917 struct ocfs2_xattr_header *xh = loc->xl_header;
1918 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1919
Joel Becker11179f22009-08-14 16:07:44 -07001920 ocfs2_xa_wipe_namevalue(loc);
Joel Beckerbde1e542009-08-14 16:58:38 -07001921 loc->xl_entry = NULL;
1922
1923 le16_add_cpu(&xh->xh_count, -1);
1924 count = le16_to_cpu(xh->xh_count);
1925
1926 /*
1927 * Only zero out the entry if there are more remaining. This is
1928 * important for an empty bucket, as it keeps track of the
1929 * bucket's hash value. It doesn't hurt empty block storage.
1930 */
1931 if (count) {
1932 index = ((char *)entry - (char *)&xh->xh_entries) /
1933 sizeof(struct ocfs2_xattr_entry);
1934 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1935 (count - index) * sizeof(struct ocfs2_xattr_entry));
1936 memset(&xh->xh_entries[count], 0,
1937 sizeof(struct ocfs2_xattr_entry));
1938 }
Joel Becker11179f22009-08-14 16:07:44 -07001939}
1940
Joel Becker399ff3a2009-09-01 18:38:27 -07001941/*
1942 * If we have a problem adjusting the size of an external value during
1943 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1944 * in an intermediate state. For example, the value may be partially
1945 * truncated.
1946 *
1947 * If the value tree hasn't changed, the extend/truncate went nowhere.
1948 * We have nothing to do. The caller can treat it as a straight error.
1949 *
1950 * If the value tree got partially truncated, we now have a corrupted
1951 * extended attribute. We're going to wipe its entry and leak the
1952 * clusters. Better to leak some storage than leave a corrupt entry.
1953 *
1954 * If the value tree grew, it obviously didn't grow enough for the
1955 * new entry. We're not going to try and reclaim those clusters either.
1956 * If there was already an external value there (orig_clusters != 0),
1957 * the new clusters are attached safely and we can just leave the old
1958 * value in place. If there was no external value there, we remove
1959 * the entry.
1960 *
1961 * This way, the xattr block we store in the journal will be consistent.
1962 * If the size change broke because of the journal, no changes will hit
1963 * disk anyway.
1964 */
1965static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1966 const char *what,
1967 unsigned int orig_clusters)
1968{
1969 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1970 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1971 le16_to_cpu(loc->xl_entry->xe_name_offset));
1972
1973 if (new_clusters < orig_clusters) {
1974 mlog(ML_ERROR,
1975 "Partial truncate while %s xattr %.*s. Leaking "
1976 "%u clusters and removing the entry\n",
1977 what, loc->xl_entry->xe_name_len, nameval_buf,
1978 orig_clusters - new_clusters);
1979 ocfs2_xa_remove_entry(loc);
1980 } else if (!orig_clusters) {
1981 mlog(ML_ERROR,
1982 "Unable to allocate an external value for xattr "
1983 "%.*s safely. Leaking %u clusters and removing the "
1984 "entry\n",
1985 loc->xl_entry->xe_name_len, nameval_buf,
1986 new_clusters - orig_clusters);
1987 ocfs2_xa_remove_entry(loc);
1988 } else if (new_clusters > orig_clusters)
1989 mlog(ML_ERROR,
1990 "Unable to grow xattr %.*s safely. %u new clusters "
1991 "have been added, but the value will not be "
1992 "modified\n",
1993 loc->xl_entry->xe_name_len, nameval_buf,
1994 new_clusters - orig_clusters);
1995}
1996
Joel Becker73857ee2009-08-18 20:26:41 -07001997static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1998 struct ocfs2_xattr_set_ctxt *ctxt)
1999{
2000 int rc = 0;
Joel Becker399ff3a2009-09-01 18:38:27 -07002001 unsigned int orig_clusters;
Joel Becker73857ee2009-08-18 20:26:41 -07002002
2003 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
Joel Becker399ff3a2009-09-01 18:38:27 -07002004 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002005 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2006 if (rc) {
2007 mlog_errno(rc);
Joel Becker399ff3a2009-09-01 18:38:27 -07002008 /*
2009 * Since this is remove, we can return 0 if
2010 * ocfs2_xa_cleanup_value_truncate() is going to
2011 * wipe the entry anyway. So we check the
2012 * cluster count as well.
2013 */
2014 if (orig_clusters != ocfs2_xa_value_clusters(loc))
2015 rc = 0;
2016 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2017 orig_clusters);
2018 if (rc)
2019 goto out;
Joel Becker73857ee2009-08-18 20:26:41 -07002020 }
2021 }
2022
2023 ocfs2_xa_remove_entry(loc);
2024
2025out:
2026 return rc;
2027}
2028
2029static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2030{
2031 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2032 char *nameval_buf;
2033
2034 nameval_buf = ocfs2_xa_offset_pointer(loc,
2035 le16_to_cpu(loc->xl_entry->xe_name_offset));
2036 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2037}
2038
2039/*
2040 * Take an existing entry and make it ready for the new value. This
2041 * won't allocate space, but it may free space. It should be ready for
2042 * ocfs2_xa_prepare_entry() to finish the work.
2043 */
2044static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2045 struct ocfs2_xattr_info *xi,
2046 struct ocfs2_xattr_set_ctxt *ctxt)
2047{
2048 int rc = 0;
2049 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker399ff3a2009-09-01 18:38:27 -07002050 unsigned int orig_clusters;
Joel Becker73857ee2009-08-18 20:26:41 -07002051 char *nameval_buf;
2052 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2053 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2054
2055 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2056 name_size);
2057
2058 nameval_buf = ocfs2_xa_offset_pointer(loc,
2059 le16_to_cpu(loc->xl_entry->xe_name_offset));
2060 if (xe_local) {
2061 memset(nameval_buf + name_size, 0,
2062 namevalue_size_xe(loc->xl_entry) - name_size);
2063 if (!xi_local)
2064 ocfs2_xa_install_value_root(loc);
2065 } else {
Joel Becker399ff3a2009-09-01 18:38:27 -07002066 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002067 if (xi_local) {
2068 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
Joel Becker399ff3a2009-09-01 18:38:27 -07002069 if (rc < 0)
Joel Becker73857ee2009-08-18 20:26:41 -07002070 mlog_errno(rc);
Joel Becker399ff3a2009-09-01 18:38:27 -07002071 else
2072 memset(nameval_buf + name_size, 0,
2073 namevalue_size_xe(loc->xl_entry) -
2074 name_size);
Joel Becker73857ee2009-08-18 20:26:41 -07002075 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2076 xi->xi_value_len) {
2077 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2078 ctxt);
Joel Becker399ff3a2009-09-01 18:38:27 -07002079 if (rc < 0)
Joel Becker73857ee2009-08-18 20:26:41 -07002080 mlog_errno(rc);
Joel Becker399ff3a2009-09-01 18:38:27 -07002081 }
2082
2083 if (rc) {
2084 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2085 orig_clusters);
2086 goto out;
Joel Becker73857ee2009-08-18 20:26:41 -07002087 }
2088 }
2089
2090 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2091 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2092
2093out:
2094 return rc;
2095}
2096
Joel Becker69a3e532009-08-17 12:24:39 -07002097/*
2098 * Prepares loc->xl_entry to receive the new xattr. This includes
2099 * properly setting up the name+value pair region. If loc->xl_entry
2100 * already exists, it will take care of modifying it appropriately.
Joel Becker69a3e532009-08-17 12:24:39 -07002101 *
2102 * Note that this modifies the data. You did journal_access already,
2103 * right?
2104 */
2105static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2106 struct ocfs2_xattr_info *xi,
Joel Becker73857ee2009-08-18 20:26:41 -07002107 u32 name_hash,
2108 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002109{
2110 int rc = 0;
Joel Becker399ff3a2009-09-01 18:38:27 -07002111 unsigned int orig_clusters;
2112 __le64 orig_value_size = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002113
Joel Becker69a3e532009-08-17 12:24:39 -07002114 rc = ocfs2_xa_check_space(loc, xi);
2115 if (rc)
2116 goto out;
2117
2118 if (loc->xl_entry) {
2119 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
Joel Becker399ff3a2009-09-01 18:38:27 -07002120 orig_value_size = loc->xl_entry->xe_value_size;
Joel Becker73857ee2009-08-18 20:26:41 -07002121 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2122 if (rc)
2123 goto out;
2124 goto alloc_value;
Joel Becker69a3e532009-08-17 12:24:39 -07002125 }
2126
Joel Becker73857ee2009-08-18 20:26:41 -07002127 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
Joel Becker399ff3a2009-09-01 18:38:27 -07002128 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002129 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2130 if (rc) {
2131 mlog_errno(rc);
Joel Becker399ff3a2009-09-01 18:38:27 -07002132 ocfs2_xa_cleanup_value_truncate(loc,
2133 "overwriting",
2134 orig_clusters);
Joel Becker73857ee2009-08-18 20:26:41 -07002135 goto out;
2136 }
2137 }
Joel Becker69a3e532009-08-17 12:24:39 -07002138 ocfs2_xa_wipe_namevalue(loc);
2139 } else
2140 ocfs2_xa_add_entry(loc, name_hash);
2141
2142 /*
2143 * If we get here, we have a blank entry. Fill it. We grow our
2144 * name+value pair back from the end.
2145 */
2146 ocfs2_xa_add_namevalue(loc, xi);
Joel Becker73857ee2009-08-18 20:26:41 -07002147 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2148 ocfs2_xa_install_value_root(loc);
2149
2150alloc_value:
2151 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Joel Becker399ff3a2009-09-01 18:38:27 -07002152 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002153 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
Joel Becker399ff3a2009-09-01 18:38:27 -07002154 if (rc < 0) {
Tao Ma5f5261a2010-05-13 22:49:05 +08002155 ctxt->set_abort = 1;
Joel Becker399ff3a2009-09-01 18:38:27 -07002156 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2157 orig_clusters);
Tao Mad5a7df02010-05-10 18:09:47 +08002158 /*
2159 * If we were growing an existing value,
2160 * ocfs2_xa_cleanup_value_truncate() won't remove
2161 * the entry. We need to restore the original value
2162 * size.
2163 */
2164 if (loc->xl_entry) {
2165 BUG_ON(!orig_value_size);
2166 loc->xl_entry->xe_value_size = orig_value_size;
2167 }
Joel Becker73857ee2009-08-18 20:26:41 -07002168 mlog_errno(rc);
Joel Becker399ff3a2009-09-01 18:38:27 -07002169 }
Joel Becker73857ee2009-08-18 20:26:41 -07002170 }
Joel Becker69a3e532009-08-17 12:24:39 -07002171
2172out:
2173 return rc;
2174}
2175
2176/*
Joel Becker73857ee2009-08-18 20:26:41 -07002177 * Store the value portion of the name+value pair. This will skip
2178 * values that are stored externally. Their tree roots were set up
2179 * by ocfs2_xa_prepare_entry().
Joel Becker69a3e532009-08-17 12:24:39 -07002180 */
Joel Becker73857ee2009-08-18 20:26:41 -07002181static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2182 struct ocfs2_xattr_info *xi,
2183 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002184{
Joel Becker73857ee2009-08-18 20:26:41 -07002185 int rc = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002186 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2187 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker69a3e532009-08-17 12:24:39 -07002188 char *nameval_buf;
Joel Becker73857ee2009-08-18 20:26:41 -07002189 struct ocfs2_xattr_value_buf vb;
Joel Becker69a3e532009-08-17 12:24:39 -07002190
Joel Becker69a3e532009-08-17 12:24:39 -07002191 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
Joel Becker73857ee2009-08-18 20:26:41 -07002192 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2193 ocfs2_xa_fill_value_buf(loc, &vb);
2194 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2195 ctxt->handle, &vb,
2196 xi->xi_value,
2197 xi->xi_value_len);
2198 } else
2199 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2200
Joel Becker73857ee2009-08-18 20:26:41 -07002201 return rc;
Joel Becker69a3e532009-08-17 12:24:39 -07002202}
2203
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002204static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2205 struct ocfs2_xattr_info *xi,
2206 struct ocfs2_xattr_set_ctxt *ctxt)
2207{
2208 int ret;
2209 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2210 xi->xi_name_len);
2211
2212 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2213 OCFS2_JOURNAL_ACCESS_WRITE);
2214 if (ret) {
2215 mlog_errno(ret);
2216 goto out;
2217 }
2218
Joel Becker399ff3a2009-09-01 18:38:27 -07002219 /*
2220 * From here on out, everything is going to modify the buffer a
2221 * little. Errors are going to leave the xattr header in a
2222 * sane state. Thus, even with errors we dirty the sucker.
2223 */
2224
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002225 /* Don't worry, we are never called with !xi_value and !xl_entry */
2226 if (!xi->xi_value) {
2227 ret = ocfs2_xa_remove(loc, ctxt);
Joel Becker399ff3a2009-09-01 18:38:27 -07002228 goto out_dirty;
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002229 }
2230
2231 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2232 if (ret) {
2233 if (ret != -ENOSPC)
2234 mlog_errno(ret);
Joel Becker399ff3a2009-09-01 18:38:27 -07002235 goto out_dirty;
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002236 }
2237
2238 ret = ocfs2_xa_store_value(loc, xi, ctxt);
Joel Becker399ff3a2009-09-01 18:38:27 -07002239 if (ret)
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002240 mlog_errno(ret);
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002241
Joel Becker399ff3a2009-09-01 18:38:27 -07002242out_dirty:
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002243 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2244
2245out:
2246 return ret;
2247}
2248
Joel Becker11179f22009-08-14 16:07:44 -07002249static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2250 struct inode *inode,
2251 struct buffer_head *bh,
2252 struct ocfs2_xattr_entry *entry)
2253{
2254 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2255
Joel Becker139ffac2009-08-19 11:09:17 -07002256 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2257
Joel Beckercf2bc802009-08-18 13:52:38 -07002258 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002259 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2260 loc->xl_storage = bh;
2261 loc->xl_entry = entry;
Joel Becker139ffac2009-08-19 11:09:17 -07002262 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
Joel Becker11179f22009-08-14 16:07:44 -07002263 loc->xl_header =
2264 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2265 loc->xl_size);
2266}
2267
2268static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
Joel Beckercf2bc802009-08-18 13:52:38 -07002269 struct inode *inode,
Joel Becker11179f22009-08-14 16:07:44 -07002270 struct buffer_head *bh,
2271 struct ocfs2_xattr_entry *entry)
2272{
2273 struct ocfs2_xattr_block *xb =
2274 (struct ocfs2_xattr_block *)bh->b_data;
2275
2276 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2277
Joel Beckercf2bc802009-08-18 13:52:38 -07002278 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002279 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2280 loc->xl_storage = bh;
2281 loc->xl_header = &(xb->xb_attrs.xb_header);
2282 loc->xl_entry = entry;
2283 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2284 xb_attrs.xb_header);
2285}
2286
2287static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2288 struct ocfs2_xattr_bucket *bucket,
2289 struct ocfs2_xattr_entry *entry)
2290{
Joel Beckercf2bc802009-08-18 13:52:38 -07002291 loc->xl_inode = bucket->bu_inode;
Joel Becker11179f22009-08-14 16:07:44 -07002292 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2293 loc->xl_storage = bucket;
2294 loc->xl_header = bucket_xh(bucket);
2295 loc->xl_entry = entry;
2296 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2297}
2298
Tao Mace9c5a52009-08-18 11:43:59 +08002299/*
2300 * In xattr remove, if it is stored outside and refcounted, we may have
2301 * the chance to split the refcount tree. So need the allocators.
2302 */
2303static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2304 struct ocfs2_xattr_value_root *xv,
2305 struct ocfs2_caching_info *ref_ci,
2306 struct buffer_head *ref_root_bh,
2307 struct ocfs2_alloc_context **meta_ac,
2308 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002309{
Tao Mace9c5a52009-08-18 11:43:59 +08002310 int ret, meta_add = 0;
2311 u32 p_cluster, num_clusters;
2312 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08002313
Tao Mace9c5a52009-08-18 11:43:59 +08002314 *ref_credits = 0;
2315 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2316 &num_clusters,
2317 &xv->xr_list,
2318 &ext_flags);
2319 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08002320 mlog_errno(ret);
2321 goto out;
2322 }
2323
Tao Mace9c5a52009-08-18 11:43:59 +08002324 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2325 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002326
Tao Mace9c5a52009-08-18 11:43:59 +08002327 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2328 ref_root_bh, xv,
2329 &meta_add, ref_credits);
2330 if (ret) {
2331 mlog_errno(ret);
2332 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002333 }
2334
Tao Mace9c5a52009-08-18 11:43:59 +08002335 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2336 meta_add, meta_ac);
2337 if (ret)
2338 mlog_errno(ret);
2339
Tao Ma85db90e2008-11-12 08:27:01 +08002340out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002341 return ret;
2342}
2343
Tao Mace9c5a52009-08-18 11:43:59 +08002344static int ocfs2_remove_value_outside(struct inode*inode,
2345 struct ocfs2_xattr_value_buf *vb,
2346 struct ocfs2_xattr_header *header,
2347 struct ocfs2_caching_info *ref_ci,
2348 struct buffer_head *ref_root_bh)
2349{
2350 int ret = 0, i, ref_credits;
2351 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2352 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2353 void *val;
2354
2355 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2356
2357 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2358 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2359
2360 if (ocfs2_xattr_is_local(entry))
2361 continue;
2362
2363 val = (void *)header +
2364 le16_to_cpu(entry->xe_name_offset);
2365 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2366 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2367
2368 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2369 ref_ci, ref_root_bh,
2370 &ctxt.meta_ac,
2371 &ref_credits);
2372
2373 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2374 ocfs2_remove_extent_credits(osb->sb));
2375 if (IS_ERR(ctxt.handle)) {
2376 ret = PTR_ERR(ctxt.handle);
2377 mlog_errno(ret);
2378 break;
2379 }
2380
2381 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2382 if (ret < 0) {
2383 mlog_errno(ret);
2384 break;
2385 }
2386
2387 ocfs2_commit_trans(osb, ctxt.handle);
2388 if (ctxt.meta_ac) {
2389 ocfs2_free_alloc_context(ctxt.meta_ac);
2390 ctxt.meta_ac = NULL;
2391 }
2392 }
2393
2394 if (ctxt.meta_ac)
2395 ocfs2_free_alloc_context(ctxt.meta_ac);
2396 ocfs2_schedule_truncate_log_flush(osb, 1);
2397 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2398 return ret;
2399}
2400
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002401static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002402 struct buffer_head *di_bh,
2403 struct ocfs2_caching_info *ref_ci,
2404 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002405{
2406
2407 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2408 struct ocfs2_xattr_header *header;
2409 int ret;
Joel Becker43119012008-12-09 16:24:43 -08002410 struct ocfs2_xattr_value_buf vb = {
2411 .vb_bh = di_bh,
2412 .vb_access = ocfs2_journal_access_di,
2413 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002414
2415 header = (struct ocfs2_xattr_header *)
2416 ((void *)di + inode->i_sb->s_blocksize -
2417 le16_to_cpu(di->i_xattr_inline_size));
2418
Tao Mace9c5a52009-08-18 11:43:59 +08002419 ret = ocfs2_remove_value_outside(inode, &vb, header,
2420 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002421
2422 return ret;
2423}
2424
Tao Mace9c5a52009-08-18 11:43:59 +08002425struct ocfs2_rm_xattr_bucket_para {
2426 struct ocfs2_caching_info *ref_ci;
2427 struct buffer_head *ref_root_bh;
2428};
2429
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002430static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002431 struct buffer_head *blk_bh,
2432 struct ocfs2_caching_info *ref_ci,
2433 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002434{
2435 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002436 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08002437 struct ocfs2_xattr_value_buf vb = {
2438 .vb_bh = blk_bh,
2439 .vb_access = ocfs2_journal_access_xb,
2440 };
Tao Mace9c5a52009-08-18 11:43:59 +08002441 struct ocfs2_rm_xattr_bucket_para args = {
2442 .ref_ci = ref_ci,
2443 .ref_root_bh = ref_root_bh,
2444 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002445
2446 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08002447 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2448 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08002449 ret = ocfs2_remove_value_outside(inode, &vb, header,
2450 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08002451 } else
Tao Ma47bca492009-08-18 11:43:42 +08002452 ret = ocfs2_iterate_xattr_index_block(inode,
2453 blk_bh,
2454 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08002455 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002456
2457 return ret;
2458}
2459
Tao Ma08413892008-08-29 09:00:19 +08002460static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002461 u64 block,
2462 struct ocfs2_caching_info *ref_ci,
2463 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08002464{
2465 struct inode *xb_alloc_inode;
2466 struct buffer_head *xb_alloc_bh = NULL;
2467 struct buffer_head *blk_bh = NULL;
2468 struct ocfs2_xattr_block *xb;
2469 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2470 handle_t *handle;
2471 int ret = 0;
2472 u64 blk, bg_blkno;
2473 u16 bit;
2474
Joel Becker4ae1d692008-11-13 14:49:18 -08002475 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08002476 if (ret < 0) {
2477 mlog_errno(ret);
2478 goto out;
2479 }
2480
Tao Mace9c5a52009-08-18 11:43:59 +08002481 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08002482 if (ret < 0) {
2483 mlog_errno(ret);
2484 goto out;
2485 }
2486
Joel Becker4ae1d692008-11-13 14:49:18 -08002487 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08002488 blk = le64_to_cpu(xb->xb_blkno);
2489 bit = le16_to_cpu(xb->xb_suballoc_bit);
Tao Ma74380c42010-03-22 14:20:18 +08002490 if (xb->xb_suballoc_loc)
2491 bg_blkno = le64_to_cpu(xb->xb_suballoc_loc);
2492 else
2493 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
Tao Ma08413892008-08-29 09:00:19 +08002494
2495 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2496 EXTENT_ALLOC_SYSTEM_INODE,
2497 le16_to_cpu(xb->xb_suballoc_slot));
2498 if (!xb_alloc_inode) {
2499 ret = -ENOMEM;
2500 mlog_errno(ret);
2501 goto out;
2502 }
2503 mutex_lock(&xb_alloc_inode->i_mutex);
2504
2505 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2506 if (ret < 0) {
2507 mlog_errno(ret);
2508 goto out_mutex;
2509 }
2510
2511 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2512 if (IS_ERR(handle)) {
2513 ret = PTR_ERR(handle);
2514 mlog_errno(ret);
2515 goto out_unlock;
2516 }
2517
2518 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2519 bit, bg_blkno, 1);
2520 if (ret < 0)
2521 mlog_errno(ret);
2522
2523 ocfs2_commit_trans(osb, handle);
2524out_unlock:
2525 ocfs2_inode_unlock(xb_alloc_inode, 1);
2526 brelse(xb_alloc_bh);
2527out_mutex:
2528 mutex_unlock(&xb_alloc_inode->i_mutex);
2529 iput(xb_alloc_inode);
2530out:
2531 brelse(blk_bh);
2532 return ret;
2533}
2534
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002535/*
2536 * ocfs2_xattr_remove()
2537 *
2538 * Free extended attribute resources associated with this inode.
2539 */
2540int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2541{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002542 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2543 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002544 struct ocfs2_refcount_tree *ref_tree = NULL;
2545 struct buffer_head *ref_root_bh = NULL;
2546 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002547 handle_t *handle;
2548 int ret;
2549
Tiger Yang8154da32008-08-18 17:11:46 +08002550 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2551 return 0;
2552
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002553 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2554 return 0;
2555
Tao Mace9c5a52009-08-18 11:43:59 +08002556 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2557 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2558 le64_to_cpu(di->i_refcount_loc),
2559 1, &ref_tree, &ref_root_bh);
2560 if (ret) {
2561 mlog_errno(ret);
2562 goto out;
2563 }
2564 ref_ci = &ref_tree->rf_ci;
2565
2566 }
2567
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002568 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002569 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2570 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002571 if (ret < 0) {
2572 mlog_errno(ret);
2573 goto out;
2574 }
2575 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002576
Tao Ma08413892008-08-29 09:00:19 +08002577 if (di->i_xattr_loc) {
2578 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002579 le64_to_cpu(di->i_xattr_loc),
2580 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002581 if (ret < 0) {
2582 mlog_errno(ret);
2583 goto out;
2584 }
2585 }
2586
2587 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2588 OCFS2_INODE_UPDATE_CREDITS);
2589 if (IS_ERR(handle)) {
2590 ret = PTR_ERR(handle);
2591 mlog_errno(ret);
2592 goto out;
2593 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002594 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002595 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002596 if (ret) {
2597 mlog_errno(ret);
2598 goto out_commit;
2599 }
2600
Tao Ma08413892008-08-29 09:00:19 +08002601 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002602
2603 spin_lock(&oi->ip_lock);
2604 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2605 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2606 spin_unlock(&oi->ip_lock);
2607
Joel Beckerec20cec2010-03-19 14:13:52 -07002608 ocfs2_journal_dirty(handle, di_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002609out_commit:
2610 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2611out:
Tao Mace9c5a52009-08-18 11:43:59 +08002612 if (ref_tree)
2613 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2614 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002615 return ret;
2616}
2617
2618static int ocfs2_xattr_has_space_inline(struct inode *inode,
2619 struct ocfs2_dinode *di)
2620{
2621 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2622 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2623 int free;
2624
2625 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2626 return 0;
2627
2628 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2629 struct ocfs2_inline_data *idata = &di->id2.i_data;
2630 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2631 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2632 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2633 le64_to_cpu(di->i_size);
2634 } else {
2635 struct ocfs2_extent_list *el = &di->id2.i_list;
2636 free = (le16_to_cpu(el->l_count) -
2637 le16_to_cpu(el->l_next_free_rec)) *
2638 sizeof(struct ocfs2_extent_rec);
2639 }
2640 if (free >= xattrsize)
2641 return 1;
2642
2643 return 0;
2644}
2645
2646/*
2647 * ocfs2_xattr_ibody_find()
2648 *
2649 * Find extended attribute in inode block and
2650 * fill search info into struct ocfs2_xattr_search.
2651 */
2652static int ocfs2_xattr_ibody_find(struct inode *inode,
2653 int name_index,
2654 const char *name,
2655 struct ocfs2_xattr_search *xs)
2656{
2657 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2658 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2659 int ret;
2660 int has_space = 0;
2661
2662 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2663 return 0;
2664
2665 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2666 down_read(&oi->ip_alloc_sem);
2667 has_space = ocfs2_xattr_has_space_inline(inode, di);
2668 up_read(&oi->ip_alloc_sem);
2669 if (!has_space)
2670 return 0;
2671 }
2672
2673 xs->xattr_bh = xs->inode_bh;
2674 xs->end = (void *)di + inode->i_sb->s_blocksize;
2675 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2676 xs->header = (struct ocfs2_xattr_header *)
2677 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2678 else
2679 xs->header = (struct ocfs2_xattr_header *)
2680 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2681 xs->base = (void *)xs->header;
2682 xs->here = xs->header->xh_entries;
2683
2684 /* Find the named attribute. */
2685 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2686 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2687 if (ret && ret != -ENODATA)
2688 return ret;
2689 xs->not_found = ret;
2690 }
2691
2692 return 0;
2693}
2694
Joel Becker139ffac2009-08-19 11:09:17 -07002695static int ocfs2_xattr_ibody_init(struct inode *inode,
2696 struct buffer_head *di_bh,
2697 struct ocfs2_xattr_set_ctxt *ctxt)
2698{
2699 int ret;
2700 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2701 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2702 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2703 unsigned int xattrsize = osb->s_xattr_inline_size;
2704
2705 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2706 ret = -ENOSPC;
2707 goto out;
2708 }
2709
2710 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2711 OCFS2_JOURNAL_ACCESS_WRITE);
2712 if (ret) {
2713 mlog_errno(ret);
2714 goto out;
2715 }
2716
2717 /*
2718 * Adjust extent record count or inline data size
2719 * to reserve space for extended attribute.
2720 */
2721 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2722 struct ocfs2_inline_data *idata = &di->id2.i_data;
2723 le16_add_cpu(&idata->id_count, -xattrsize);
2724 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2725 struct ocfs2_extent_list *el = &di->id2.i_list;
2726 le16_add_cpu(&el->l_count, -(xattrsize /
2727 sizeof(struct ocfs2_extent_rec)));
2728 }
2729 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2730
2731 spin_lock(&oi->ip_lock);
2732 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2733 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2734 spin_unlock(&oi->ip_lock);
2735
Joel Beckerec20cec2010-03-19 14:13:52 -07002736 ocfs2_journal_dirty(ctxt->handle, di_bh);
Joel Becker139ffac2009-08-19 11:09:17 -07002737
2738out:
2739 return ret;
2740}
2741
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002742/*
2743 * ocfs2_xattr_ibody_set()
2744 *
2745 * Set, replace or remove an extended attribute into inode block.
2746 *
2747 */
2748static int ocfs2_xattr_ibody_set(struct inode *inode,
2749 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002750 struct ocfs2_xattr_search *xs,
2751 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002752{
Joel Becker139ffac2009-08-19 11:09:17 -07002753 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002754 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2755 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Joel Becker139ffac2009-08-19 11:09:17 -07002756 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002757
2758 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2759 return -ENOSPC;
2760
2761 down_write(&oi->ip_alloc_sem);
2762 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2763 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2764 ret = -ENOSPC;
2765 goto out;
2766 }
2767 }
2768
Joel Becker139ffac2009-08-19 11:09:17 -07002769 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2770 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2771 if (ret) {
2772 if (ret != -ENOSPC)
2773 mlog_errno(ret);
2774 goto out;
2775 }
2776 }
2777
2778 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2779 xs->not_found ? NULL : xs->here);
2780 ret = ocfs2_xa_set(&loc, xi, ctxt);
2781 if (ret) {
2782 if (ret != -ENOSPC)
2783 mlog_errno(ret);
2784 goto out;
2785 }
2786 xs->here = loc.xl_entry;
2787
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002788out:
2789 up_write(&oi->ip_alloc_sem);
2790
2791 return ret;
2792}
2793
2794/*
2795 * ocfs2_xattr_block_find()
2796 *
2797 * Find extended attribute in external block and
2798 * fill search info into struct ocfs2_xattr_search.
2799 */
2800static int ocfs2_xattr_block_find(struct inode *inode,
2801 int name_index,
2802 const char *name,
2803 struct ocfs2_xattr_search *xs)
2804{
2805 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2806 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002807 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002808 int ret = 0;
2809
2810 if (!di->i_xattr_loc)
2811 return ret;
2812
Joel Becker4ae1d692008-11-13 14:49:18 -08002813 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2814 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002815 if (ret < 0) {
2816 mlog_errno(ret);
2817 return ret;
2818 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002819
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002820 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002821 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002822
Tao Ma589dc262008-08-18 17:38:51 +08002823 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2824 xs->header = &xb->xb_attrs.xb_header;
2825 xs->base = (void *)xs->header;
2826 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2827 xs->here = xs->header->xh_entries;
2828
2829 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2830 } else
2831 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2832 name_index,
2833 name, xs);
2834
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002835 if (ret && ret != -ENODATA) {
2836 xs->xattr_bh = NULL;
2837 goto cleanup;
2838 }
2839 xs->not_found = ret;
2840 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002841cleanup:
2842 brelse(blk_bh);
2843
2844 return ret;
2845}
2846
Joel Beckerd3981542009-08-19 02:13:50 -07002847static int ocfs2_create_xattr_block(struct inode *inode,
Tao Ma5aea1f02009-08-18 11:43:24 +08002848 struct buffer_head *inode_bh,
Joel Beckerd3981542009-08-19 02:13:50 -07002849 struct ocfs2_xattr_set_ctxt *ctxt,
2850 int indexed,
2851 struct buffer_head **ret_bh)
Tao Ma5aea1f02009-08-18 11:43:24 +08002852{
2853 int ret;
2854 u16 suballoc_bit_start;
2855 u32 num_got;
Joel Becker2b6cb572010-03-26 10:09:15 +08002856 u64 suballoc_loc, first_blkno;
Tao Ma5aea1f02009-08-18 11:43:24 +08002857 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
Tao Ma5aea1f02009-08-18 11:43:24 +08002858 struct buffer_head *new_bh = NULL;
2859 struct ocfs2_xattr_block *xblk;
2860
Joel Beckerd3981542009-08-19 02:13:50 -07002861 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2862 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
Tao Ma5aea1f02009-08-18 11:43:24 +08002863 if (ret < 0) {
2864 mlog_errno(ret);
2865 goto end;
2866 }
2867
Joel Becker1ed9b772010-05-06 13:59:06 +08002868 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
Joel Becker2b6cb572010-03-26 10:09:15 +08002869 &suballoc_loc, &suballoc_bit_start,
2870 &num_got, &first_blkno);
Tao Ma5aea1f02009-08-18 11:43:24 +08002871 if (ret < 0) {
2872 mlog_errno(ret);
2873 goto end;
2874 }
2875
2876 new_bh = sb_getblk(inode->i_sb, first_blkno);
2877 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2878
Joel Beckerd3981542009-08-19 02:13:50 -07002879 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
Tao Ma5aea1f02009-08-18 11:43:24 +08002880 new_bh,
2881 OCFS2_JOURNAL_ACCESS_CREATE);
2882 if (ret < 0) {
2883 mlog_errno(ret);
2884 goto end;
2885 }
2886
2887 /* Initialize ocfs2_xattr_block */
2888 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2889 memset(xblk, 0, inode->i_sb->s_blocksize);
2890 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
Joel Beckerd3981542009-08-19 02:13:50 -07002891 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
Joel Becker2b6cb572010-03-26 10:09:15 +08002892 xblk->xb_suballoc_loc = cpu_to_le64(suballoc_loc);
Tao Ma5aea1f02009-08-18 11:43:24 +08002893 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
Joel Becker1ed9b772010-05-06 13:59:06 +08002894 xblk->xb_fs_generation =
2895 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
Tao Ma5aea1f02009-08-18 11:43:24 +08002896 xblk->xb_blkno = cpu_to_le64(first_blkno);
Tao Maa7fe7a32009-08-18 11:43:52 +08002897 if (indexed) {
2898 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2899 xr->xt_clusters = cpu_to_le32(1);
2900 xr->xt_last_eb_blk = 0;
2901 xr->xt_list.l_tree_depth = 0;
2902 xr->xt_list.l_count = cpu_to_le16(
2903 ocfs2_xattr_recs_per_xb(inode->i_sb));
2904 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2905 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2906 }
Joel Beckerd3981542009-08-19 02:13:50 -07002907 ocfs2_journal_dirty(ctxt->handle, new_bh);
Tao Maa7fe7a32009-08-18 11:43:52 +08002908
Joel Beckerd3981542009-08-19 02:13:50 -07002909 /* Add it to the inode */
Tao Ma5aea1f02009-08-18 11:43:24 +08002910 di->i_xattr_loc = cpu_to_le64(first_blkno);
Joel Beckerd3981542009-08-19 02:13:50 -07002911
2912 spin_lock(&OCFS2_I(inode)->ip_lock);
2913 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2914 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2915 spin_unlock(&OCFS2_I(inode)->ip_lock);
2916
2917 ocfs2_journal_dirty(ctxt->handle, inode_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002918
2919 *ret_bh = new_bh;
2920 new_bh = NULL;
2921
2922end:
2923 brelse(new_bh);
2924 return ret;
2925}
2926
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002927/*
2928 * ocfs2_xattr_block_set()
2929 *
2930 * Set, replace or remove an extended attribute into external block.
2931 *
2932 */
2933static int ocfs2_xattr_block_set(struct inode *inode,
2934 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002935 struct ocfs2_xattr_search *xs,
2936 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002937{
2938 struct buffer_head *new_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002939 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002940 int ret;
Joel Beckerd3981542009-08-19 02:13:50 -07002941 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002942
2943 if (!xs->xattr_bh) {
Joel Beckerd3981542009-08-19 02:13:50 -07002944 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2945 0, &new_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002946 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002947 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002948 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002949 }
2950
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002951 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002952 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002953 xs->header = &xblk->xb_attrs.xb_header;
2954 xs->base = (void *)xs->header;
2955 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2956 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002957 } else
2958 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2959
2960 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
Joel Beckerd3981542009-08-19 02:13:50 -07002961 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2962 xs->not_found ? NULL : xs->here);
Tao Ma01225592008-08-18 17:38:53 +08002963
Joel Beckerd3981542009-08-19 02:13:50 -07002964 ret = ocfs2_xa_set(&loc, xi, ctxt);
2965 if (!ret)
2966 xs->here = loc.xl_entry;
Tao Ma5f5261a2010-05-13 22:49:05 +08002967 else if ((ret != -ENOSPC) || ctxt->set_abort)
Tao Ma01225592008-08-18 17:38:53 +08002968 goto end;
Joel Beckerd3981542009-08-19 02:13:50 -07002969 else {
2970 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2971 if (ret)
2972 goto end;
2973 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002974 }
2975
Joel Beckerd3981542009-08-19 02:13:50 -07002976 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2977 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002978
2979end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002980 return ret;
2981}
2982
Tao Ma78f30c32008-11-12 08:27:00 +08002983/* Check whether the new xattr can be inserted into the inode. */
2984static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2985 struct ocfs2_xattr_info *xi,
2986 struct ocfs2_xattr_search *xs)
2987{
Tao Ma78f30c32008-11-12 08:27:00 +08002988 struct ocfs2_xattr_entry *last;
2989 int free, i;
2990 size_t min_offs = xs->end - xs->base;
2991
2992 if (!xs->header)
2993 return 0;
2994
2995 last = xs->header->xh_entries;
2996
2997 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2998 size_t offs = le16_to_cpu(last->xe_name_offset);
2999 if (offs < min_offs)
3000 min_offs = offs;
3001 last += 1;
3002 }
3003
Tiger Yang4442f512009-02-20 11:11:50 +08003004 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08003005 if (free < 0)
3006 return 0;
3007
3008 BUG_ON(!xs->not_found);
3009
Joel Becker199799a2009-08-14 19:04:15 -07003010 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
Tao Ma78f30c32008-11-12 08:27:00 +08003011 return 1;
3012
3013 return 0;
3014}
3015
3016static int ocfs2_calc_xattr_set_need(struct inode *inode,
3017 struct ocfs2_dinode *di,
3018 struct ocfs2_xattr_info *xi,
3019 struct ocfs2_xattr_search *xis,
3020 struct ocfs2_xattr_search *xbs,
3021 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08003022 int *meta_need,
3023 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08003024{
3025 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003026 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003027 struct buffer_head *bh = NULL;
3028 struct ocfs2_xattr_block *xb = NULL;
3029 struct ocfs2_xattr_entry *xe = NULL;
3030 struct ocfs2_xattr_value_root *xv = NULL;
3031 char *base = NULL;
3032 int name_offset, name_len = 0;
3033 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Joel Becker6b240ff2009-08-14 18:02:52 -07003034 xi->xi_value_len);
Tao Ma78f30c32008-11-12 08:27:00 +08003035 u64 value_size;
3036
Tao Ma71d548a2008-12-05 06:20:54 +08003037 /*
3038 * Calculate the clusters we need to write.
3039 * No matter whether we replace an old one or add a new one,
3040 * we need this for writing.
3041 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003042 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma71d548a2008-12-05 06:20:54 +08003043 credits += new_clusters *
3044 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3045
Tao Ma78f30c32008-11-12 08:27:00 +08003046 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08003047 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3048
Joel Becker6b240ff2009-08-14 18:02:52 -07003049 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003050 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003051 credits += ocfs2_calc_extend_credits(inode->i_sb,
3052 &def_xv.xv.xr_list,
3053 new_clusters);
3054 }
Tao Ma78f30c32008-11-12 08:27:00 +08003055
3056 goto meta_guess;
3057 }
3058
3059 if (!xis->not_found) {
3060 xe = xis->here;
3061 name_offset = le16_to_cpu(xe->xe_name_offset);
3062 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3063 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003064 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08003065 } else {
Joel Becker970e4932008-11-13 14:49:19 -08003066 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003067 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3068 xe = xbs->here;
3069 name_offset = le16_to_cpu(xe->xe_name_offset);
3070 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3071 i = xbs->here - xbs->header->xh_entries;
3072 old_in_xb = 1;
3073
3074 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08003075 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08003076 bucket_xh(xbs->bucket),
3077 i, &block_off,
3078 &name_offset);
3079 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08003080 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3081 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003082 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003083 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3084 }
3085 }
3086
3087 /*
3088 * delete a xattr doesn't need metadata and cluster allocation.
3089 * so just calculate the credits and return.
3090 *
3091 * The credits for removing the value tree will be extended
3092 * by ocfs2_remove_extent itself.
3093 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003094 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003095 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02003096 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003097
3098 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003099 }
3100
3101 /* do cluster allocation guess first. */
3102 value_size = le64_to_cpu(xe->xe_value_size);
3103
3104 if (old_in_xb) {
3105 /*
3106 * In xattr set, we always try to set the xe in inode first,
3107 * so if it can be inserted into inode successfully, the old
3108 * one will be removed from the xattr block, and this xattr
3109 * will be inserted into inode as a new xattr in inode.
3110 */
3111 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3112 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02003113 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08003114 OCFS2_INODE_UPDATE_CREDITS;
3115 if (!ocfs2_xattr_is_local(xe))
3116 credits += ocfs2_calc_extend_credits(
3117 inode->i_sb,
3118 &def_xv.xv.xr_list,
3119 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08003120 goto out;
3121 }
3122 }
3123
Joel Becker6b240ff2009-08-14 18:02:52 -07003124 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003125 /* the new values will be stored outside. */
3126 u32 old_clusters = 0;
3127
3128 if (!ocfs2_xattr_is_local(xe)) {
3129 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3130 value_size);
3131 xv = (struct ocfs2_xattr_value_root *)
3132 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08003133 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08003134 } else
3135 xv = &def_xv.xv;
3136
Tao Ma85db90e2008-11-12 08:27:01 +08003137 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02003138 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003139 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08003140 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003141 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3142 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003143 credits += ocfs2_calc_extend_credits(inode->i_sb,
3144 &xv->xr_list,
3145 new_clusters -
3146 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08003147 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3148 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003149 }
3150 } else {
3151 /*
3152 * Now the new value will be stored inside. So if the new
3153 * value is smaller than the size of value root or the old
3154 * value, we don't need any allocation, otherwise we have
3155 * to guess metadata allocation.
3156 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003157 if ((ocfs2_xattr_is_local(xe) &&
3158 (value_size >= xi->xi_value_len)) ||
Tao Ma78f30c32008-11-12 08:27:00 +08003159 (!ocfs2_xattr_is_local(xe) &&
Joel Becker6b240ff2009-08-14 18:02:52 -07003160 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
Tao Ma78f30c32008-11-12 08:27:00 +08003161 goto out;
3162 }
3163
3164meta_guess:
3165 /* calculate metadata allocation. */
3166 if (di->i_xattr_loc) {
3167 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08003168 ret = ocfs2_read_xattr_block(inode,
3169 le64_to_cpu(di->i_xattr_loc),
3170 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08003171 if (ret) {
3172 mlog_errno(ret);
3173 goto out;
3174 }
3175
3176 xb = (struct ocfs2_xattr_block *)bh->b_data;
3177 } else
3178 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3179
Tao Ma90cb5462008-12-05 06:20:56 +08003180 /*
3181 * If there is already an xattr tree, good, we can calculate
3182 * like other b-trees. Otherwise we may have the chance of
3183 * create a tree, the credit calculation is borrowed from
3184 * ocfs2_calc_extend_credits with root_el = NULL. And the
3185 * new tree will be cluster based, so no meta is needed.
3186 */
Tao Ma78f30c32008-11-12 08:27:00 +08003187 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3188 struct ocfs2_extent_list *el =
3189 &xb->xb_attrs.xb_root.xt_list;
3190 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08003191 credits += ocfs2_calc_extend_credits(inode->i_sb,
3192 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08003193 } else
3194 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08003195
3196 /*
3197 * This cluster will be used either for new bucket or for
3198 * new xattr block.
3199 * If the cluster size is the same as the bucket size, one
3200 * more is needed since we may need to extend the bucket
3201 * also.
3202 */
3203 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003204 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003205 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08003206 OCFS2_SB(inode->i_sb)->s_clustersize) {
3207 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003208 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003209 }
3210 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003211 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003212 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3213 }
Tao Ma78f30c32008-11-12 08:27:00 +08003214out:
3215 if (clusters_need)
3216 *clusters_need = clusters_add;
3217 if (meta_need)
3218 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08003219 if (credits_need)
3220 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08003221 brelse(bh);
3222 return ret;
3223}
3224
3225static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3226 struct ocfs2_dinode *di,
3227 struct ocfs2_xattr_info *xi,
3228 struct ocfs2_xattr_search *xis,
3229 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003230 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08003231 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08003232 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08003233{
3234 int clusters_add, meta_add, ret;
3235 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3236
3237 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3238
3239 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3240
3241 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003242 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003243 if (ret) {
3244 mlog_errno(ret);
3245 return ret;
3246 }
3247
Tao Ma492a8a32009-08-18 11:43:17 +08003248 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08003249 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
Joel Becker6b240ff2009-08-14 18:02:52 -07003250 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003251
3252 if (meta_add) {
3253 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3254 &ctxt->meta_ac);
3255 if (ret) {
3256 mlog_errno(ret);
3257 goto out;
3258 }
3259 }
3260
3261 if (clusters_add) {
3262 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3263 if (ret)
3264 mlog_errno(ret);
3265 }
3266out:
3267 if (ret) {
3268 if (ctxt->meta_ac) {
3269 ocfs2_free_alloc_context(ctxt->meta_ac);
3270 ctxt->meta_ac = NULL;
3271 }
3272
3273 /*
3274 * We cannot have an error and a non null ctxt->data_ac.
3275 */
3276 }
3277
3278 return ret;
3279}
3280
Tao Ma85db90e2008-11-12 08:27:01 +08003281static int __ocfs2_xattr_set_handle(struct inode *inode,
3282 struct ocfs2_dinode *di,
3283 struct ocfs2_xattr_info *xi,
3284 struct ocfs2_xattr_search *xis,
3285 struct ocfs2_xattr_search *xbs,
3286 struct ocfs2_xattr_set_ctxt *ctxt)
3287{
Tao Ma9f868f12008-11-19 16:48:42 +08003288 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003289
Joel Becker6b240ff2009-08-14 18:02:52 -07003290 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003291 /* Remove existing extended attribute */
3292 if (!xis->not_found)
3293 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3294 else if (!xbs->not_found)
3295 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3296 } else {
3297 /* We always try to set extended attribute into inode first*/
3298 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3299 if (!ret && !xbs->not_found) {
3300 /*
3301 * If succeed and that extended attribute existing in
3302 * external block, then we will remove it.
3303 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003304 xi->xi_value = NULL;
3305 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003306
Tao Ma9f868f12008-11-19 16:48:42 +08003307 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003308 xis->not_found = -ENODATA;
3309 ret = ocfs2_calc_xattr_set_need(inode,
3310 di,
3311 xi,
3312 xis,
3313 xbs,
3314 NULL,
3315 NULL,
3316 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003317 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003318 if (ret) {
3319 mlog_errno(ret);
3320 goto out;
3321 }
3322
Tao Mac901fb02010-04-26 14:34:57 +08003323 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003324 if (ret) {
3325 mlog_errno(ret);
3326 goto out;
3327 }
3328 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
Tao Ma5f5261a2010-05-13 22:49:05 +08003329 } else if ((ret == -ENOSPC) && !ctxt->set_abort) {
Tao Ma85db90e2008-11-12 08:27:01 +08003330 if (di->i_xattr_loc && !xbs->xattr_bh) {
3331 ret = ocfs2_xattr_block_find(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07003332 xi->xi_name_index,
3333 xi->xi_name, xbs);
Tao Ma85db90e2008-11-12 08:27:01 +08003334 if (ret)
3335 goto out;
3336
Tao Ma9f868f12008-11-19 16:48:42 +08003337 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003338 xis->not_found = -ENODATA;
3339 ret = ocfs2_calc_xattr_set_need(inode,
3340 di,
3341 xi,
3342 xis,
3343 xbs,
3344 NULL,
3345 NULL,
3346 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003347 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003348 if (ret) {
3349 mlog_errno(ret);
3350 goto out;
3351 }
3352
Tao Mac901fb02010-04-26 14:34:57 +08003353 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003354 if (ret) {
3355 mlog_errno(ret);
3356 goto out;
3357 }
3358 }
3359 /*
3360 * If no space in inode, we will set extended attribute
3361 * into external block.
3362 */
3363 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3364 if (ret)
3365 goto out;
3366 if (!xis->not_found) {
3367 /*
3368 * If succeed and that extended attribute
3369 * existing in inode, we will remove it.
3370 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003371 xi->xi_value = NULL;
3372 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003373 xbs->not_found = -ENODATA;
3374 ret = ocfs2_calc_xattr_set_need(inode,
3375 di,
3376 xi,
3377 xis,
3378 xbs,
3379 NULL,
3380 NULL,
3381 &credits);
3382 if (ret) {
3383 mlog_errno(ret);
3384 goto out;
3385 }
3386
Tao Mac901fb02010-04-26 14:34:57 +08003387 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003388 if (ret) {
3389 mlog_errno(ret);
3390 goto out;
3391 }
3392 ret = ocfs2_xattr_ibody_set(inode, xi,
3393 xis, ctxt);
3394 }
3395 }
3396 }
3397
Tao Ma4b3f6202008-12-05 06:20:55 +08003398 if (!ret) {
3399 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08003400 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08003401 xis->inode_bh,
3402 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08003403 if (ret) {
3404 mlog_errno(ret);
3405 goto out;
3406 }
3407
3408 inode->i_ctime = CURRENT_TIME;
3409 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3410 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3411 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3412 }
Tao Ma85db90e2008-11-12 08:27:01 +08003413out:
3414 return ret;
3415}
3416
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003417/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08003418 * This function only called duing creating inode
3419 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08003420 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08003421 */
3422int ocfs2_xattr_set_handle(handle_t *handle,
3423 struct inode *inode,
3424 struct buffer_head *di_bh,
3425 int name_index,
3426 const char *name,
3427 const void *value,
3428 size_t value_len,
3429 int flags,
3430 struct ocfs2_alloc_context *meta_ac,
3431 struct ocfs2_alloc_context *data_ac)
3432{
3433 struct ocfs2_dinode *di;
3434 int ret;
3435
3436 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003437 .xi_name_index = name_index,
3438 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003439 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003440 .xi_value = value,
3441 .xi_value_len = value_len,
Tiger Yang6c3faba2008-11-14 11:16:03 +08003442 };
3443
3444 struct ocfs2_xattr_search xis = {
3445 .not_found = -ENODATA,
3446 };
3447
3448 struct ocfs2_xattr_search xbs = {
3449 .not_found = -ENODATA,
3450 };
3451
3452 struct ocfs2_xattr_set_ctxt ctxt = {
3453 .handle = handle,
3454 .meta_ac = meta_ac,
3455 .data_ac = data_ac,
3456 };
3457
3458 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3459 return -EOPNOTSUPP;
3460
Tiger Yang008aafa2008-12-09 16:43:08 +08003461 /*
3462 * In extreme situation, may need xattr bucket when
3463 * block size is too small. And we have already reserved
3464 * the credits for bucket in mknod.
3465 */
3466 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3467 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3468 if (!xbs.bucket) {
3469 mlog_errno(-ENOMEM);
3470 return -ENOMEM;
3471 }
3472 }
3473
Tiger Yang6c3faba2008-11-14 11:16:03 +08003474 xis.inode_bh = xbs.inode_bh = di_bh;
3475 di = (struct ocfs2_dinode *)di_bh->b_data;
3476
3477 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3478
3479 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3480 if (ret)
3481 goto cleanup;
3482 if (xis.not_found) {
3483 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3484 if (ret)
3485 goto cleanup;
3486 }
3487
3488 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3489
3490cleanup:
3491 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3492 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08003493 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08003494
3495 return ret;
3496}
3497
3498/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003499 * ocfs2_xattr_set()
3500 *
3501 * Set, replace or remove an extended attribute for this inode.
3502 * value is NULL to remove an existing extended attribute, else either
3503 * create or replace an extended attribute.
3504 */
3505int ocfs2_xattr_set(struct inode *inode,
3506 int name_index,
3507 const char *name,
3508 const void *value,
3509 size_t value_len,
3510 int flags)
3511{
3512 struct buffer_head *di_bh = NULL;
3513 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08003514 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003515 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003516 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08003517 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08003518 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003519
3520 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003521 .xi_name_index = name_index,
3522 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003523 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003524 .xi_value = value,
3525 .xi_value_len = value_len,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003526 };
3527
3528 struct ocfs2_xattr_search xis = {
3529 .not_found = -ENODATA,
3530 };
3531
3532 struct ocfs2_xattr_search xbs = {
3533 .not_found = -ENODATA,
3534 };
3535
Tiger Yang8154da32008-08-18 17:11:46 +08003536 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3537 return -EOPNOTSUPP;
3538
Joel Beckerba937122008-10-24 19:13:20 -07003539 /*
3540 * Only xbs will be used on indexed trees. xis doesn't need a
3541 * bucket.
3542 */
3543 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3544 if (!xbs.bucket) {
3545 mlog_errno(-ENOMEM);
3546 return -ENOMEM;
3547 }
3548
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003549 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3550 if (ret < 0) {
3551 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003552 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003553 }
3554 xis.inode_bh = xbs.inode_bh = di_bh;
3555 di = (struct ocfs2_dinode *)di_bh->b_data;
3556
3557 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3558 /*
3559 * Scan inode and external block to find the same name
3560 * extended attribute and collect search infomation.
3561 */
3562 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3563 if (ret)
3564 goto cleanup;
3565 if (xis.not_found) {
3566 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3567 if (ret)
3568 goto cleanup;
3569 }
3570
3571 if (xis.not_found && xbs.not_found) {
3572 ret = -ENODATA;
3573 if (flags & XATTR_REPLACE)
3574 goto cleanup;
3575 ret = 0;
3576 if (!value)
3577 goto cleanup;
3578 } else {
3579 ret = -EEXIST;
3580 if (flags & XATTR_CREATE)
3581 goto cleanup;
3582 }
3583
Tao Ma492a8a32009-08-18 11:43:17 +08003584 /* Check whether the value is refcounted and do some prepartion. */
3585 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3586 (!xis.not_found || !xbs.not_found)) {
3587 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3588 &xis, &xbs, &ref_tree,
3589 &ref_meta, &ref_credits);
3590 if (ret) {
3591 mlog_errno(ret);
3592 goto cleanup;
3593 }
3594 }
Tao Ma85db90e2008-11-12 08:27:01 +08003595
3596 mutex_lock(&tl_inode->i_mutex);
3597
3598 if (ocfs2_truncate_log_needs_flush(osb)) {
3599 ret = __ocfs2_flush_truncate_log(osb);
3600 if (ret < 0) {
3601 mutex_unlock(&tl_inode->i_mutex);
3602 mlog_errno(ret);
3603 goto cleanup;
3604 }
3605 }
3606 mutex_unlock(&tl_inode->i_mutex);
3607
3608 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003609 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003610 if (ret) {
3611 mlog_errno(ret);
3612 goto cleanup;
3613 }
3614
Tao Ma4b3f6202008-12-05 06:20:55 +08003615 /* we need to update inode's ctime field, so add credit for it. */
3616 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003617 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003618 if (IS_ERR(ctxt.handle)) {
3619 ret = PTR_ERR(ctxt.handle);
3620 mlog_errno(ret);
3621 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003622 }
Tao Ma85db90e2008-11-12 08:27:01 +08003623
3624 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3625
3626 ocfs2_commit_trans(osb, ctxt.handle);
3627
Tao Ma78f30c32008-11-12 08:27:00 +08003628 if (ctxt.data_ac)
3629 ocfs2_free_alloc_context(ctxt.data_ac);
3630 if (ctxt.meta_ac)
3631 ocfs2_free_alloc_context(ctxt.meta_ac);
3632 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3633 ocfs2_schedule_truncate_log_flush(osb, 1);
3634 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003635
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003636cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003637 if (ref_tree)
3638 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003639 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003640 if (!value && !ret) {
3641 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3642 if (ret)
3643 mlog_errno(ret);
3644 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003645 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003646cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003647 brelse(di_bh);
3648 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003649 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003650
3651 return ret;
3652}
3653
Tao Ma0c044f02008-08-18 17:38:50 +08003654/*
3655 * Find the xattr extent rec which may contains name_hash.
3656 * e_cpos will be the first name hash of the xattr rec.
3657 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3658 */
3659static int ocfs2_xattr_get_rec(struct inode *inode,
3660 u32 name_hash,
3661 u64 *p_blkno,
3662 u32 *e_cpos,
3663 u32 *num_clusters,
3664 struct ocfs2_extent_list *el)
3665{
3666 int ret = 0, i;
3667 struct buffer_head *eb_bh = NULL;
3668 struct ocfs2_extent_block *eb;
3669 struct ocfs2_extent_rec *rec = NULL;
3670 u64 e_blkno = 0;
3671
3672 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003673 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3674 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003675 if (ret) {
3676 mlog_errno(ret);
3677 goto out;
3678 }
3679
3680 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3681 el = &eb->h_list;
3682
3683 if (el->l_tree_depth) {
3684 ocfs2_error(inode->i_sb,
3685 "Inode %lu has non zero tree depth in "
3686 "xattr tree block %llu\n", inode->i_ino,
3687 (unsigned long long)eb_bh->b_blocknr);
3688 ret = -EROFS;
3689 goto out;
3690 }
3691 }
3692
3693 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3694 rec = &el->l_recs[i];
3695
3696 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3697 e_blkno = le64_to_cpu(rec->e_blkno);
3698 break;
3699 }
3700 }
3701
3702 if (!e_blkno) {
3703 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3704 "record (%u, %u, 0) in xattr", inode->i_ino,
3705 le32_to_cpu(rec->e_cpos),
3706 ocfs2_rec_clusters(el, rec));
3707 ret = -EROFS;
3708 goto out;
3709 }
3710
3711 *p_blkno = le64_to_cpu(rec->e_blkno);
3712 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3713 if (e_cpos)
3714 *e_cpos = le32_to_cpu(rec->e_cpos);
3715out:
3716 brelse(eb_bh);
3717 return ret;
3718}
3719
3720typedef int (xattr_bucket_func)(struct inode *inode,
3721 struct ocfs2_xattr_bucket *bucket,
3722 void *para);
3723
Tao Ma589dc262008-08-18 17:38:51 +08003724static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003725 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003726 int name_index,
3727 const char *name,
3728 u32 name_hash,
3729 u16 *xe_index,
3730 int *found)
3731{
3732 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003733 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003734 size_t name_len = strlen(name);
3735 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003736 char *xe_name;
3737
3738 /*
3739 * We don't use binary search in the bucket because there
3740 * may be multiple entries with the same name hash.
3741 */
3742 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3743 xe = &xh->xh_entries[i];
3744
3745 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3746 continue;
3747 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3748 break;
3749
3750 cmp = name_index - ocfs2_xattr_get_type(xe);
3751 if (!cmp)
3752 cmp = name_len - xe->xe_name_len;
3753 if (cmp)
3754 continue;
3755
Tao Mafd68a892009-08-18 11:43:21 +08003756 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003757 xh,
3758 i,
3759 &block_off,
3760 &new_offset);
3761 if (ret) {
3762 mlog_errno(ret);
3763 break;
3764 }
3765
Joel Becker970e4932008-11-13 14:49:19 -08003766
Joel Beckere2356a32008-10-27 15:01:54 -07003767 xe_name = bucket_block(bucket, block_off) + new_offset;
3768 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003769 *xe_index = i;
3770 *found = 1;
3771 ret = 0;
3772 break;
3773 }
3774 }
3775
3776 return ret;
3777}
3778
3779/*
3780 * Find the specified xattr entry in a series of buckets.
3781 * This series start from p_blkno and last for num_clusters.
3782 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3783 * the num of the valid buckets.
3784 *
3785 * Return the buffer_head this xattr should reside in. And if the xattr's
3786 * hash is in the gap of 2 buckets, return the lower bucket.
3787 */
3788static int ocfs2_xattr_bucket_find(struct inode *inode,
3789 int name_index,
3790 const char *name,
3791 u32 name_hash,
3792 u64 p_blkno,
3793 u32 first_hash,
3794 u32 num_clusters,
3795 struct ocfs2_xattr_search *xs)
3796{
3797 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003798 struct ocfs2_xattr_header *xh = NULL;
3799 struct ocfs2_xattr_entry *xe = NULL;
3800 u16 index = 0;
3801 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3802 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003803 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003804 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003805 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003806
Joel Beckere2356a32008-10-27 15:01:54 -07003807 search = ocfs2_xattr_bucket_new(inode);
3808 if (!search) {
3809 ret = -ENOMEM;
3810 mlog_errno(ret);
3811 goto out;
3812 }
3813
3814 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003815 if (ret) {
3816 mlog_errno(ret);
3817 goto out;
3818 }
3819
Joel Beckere2356a32008-10-27 15:01:54 -07003820 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003821 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003822 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003823 ocfs2_xattr_bucket_relse(search);
3824
Tao Ma589dc262008-08-18 17:38:51 +08003825 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003826 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003827 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003828 if (ret) {
3829 mlog_errno(ret);
3830 goto out;
3831 }
3832
Joel Beckere2356a32008-10-27 15:01:54 -07003833 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003834 xe = &xh->xh_entries[0];
3835 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3836 high_bucket = bucket - 1;
3837 continue;
3838 }
3839
3840 /*
3841 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003842 * bucket is larger than the search one. for an empty
3843 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003844 */
Tao Ma5a095612008-09-19 22:17:41 +08003845 if (xh->xh_count)
3846 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3847
Tao Ma589dc262008-08-18 17:38:51 +08003848 last_hash = le32_to_cpu(xe->xe_name_hash);
3849
Joel Beckere2356a32008-10-27 15:01:54 -07003850 /* record lower_blkno which may be the insert place. */
3851 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003852
3853 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3854 low_bucket = bucket + 1;
3855 continue;
3856 }
3857
3858 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003859 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003860 name_index, name, name_hash,
3861 &index, &found);
3862 if (ret) {
3863 mlog_errno(ret);
3864 goto out;
3865 }
3866 break;
3867 }
3868
3869 /*
3870 * Record the bucket we have found.
3871 * When the xattr's hash value is in the gap of 2 buckets, we will
3872 * always set it to the previous bucket.
3873 */
Joel Beckere2356a32008-10-27 15:01:54 -07003874 if (!lower_blkno)
3875 lower_blkno = p_blkno;
3876
3877 /* This should be in cache - we just read it during the search */
3878 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3879 if (ret) {
3880 mlog_errno(ret);
3881 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003882 }
Tao Ma589dc262008-08-18 17:38:51 +08003883
Joel Beckerba937122008-10-24 19:13:20 -07003884 xs->header = bucket_xh(xs->bucket);
3885 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003886 xs->end = xs->base + inode->i_sb->s_blocksize;
3887
3888 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003889 xs->here = &xs->header->xh_entries[index];
3890 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003891 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003892 } else
3893 ret = -ENODATA;
3894
3895out:
Joel Beckere2356a32008-10-27 15:01:54 -07003896 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003897 return ret;
3898}
3899
3900static int ocfs2_xattr_index_block_find(struct inode *inode,
3901 struct buffer_head *root_bh,
3902 int name_index,
3903 const char *name,
3904 struct ocfs2_xattr_search *xs)
3905{
3906 int ret;
3907 struct ocfs2_xattr_block *xb =
3908 (struct ocfs2_xattr_block *)root_bh->b_data;
3909 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3910 struct ocfs2_extent_list *el = &xb_root->xt_list;
3911 u64 p_blkno = 0;
3912 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003913 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003914
3915 if (le16_to_cpu(el->l_next_free_rec) == 0)
3916 return -ENODATA;
3917
3918 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3919 name, name_hash, name_index);
3920
3921 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3922 &num_clusters, el);
3923 if (ret) {
3924 mlog_errno(ret);
3925 goto out;
3926 }
3927
3928 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3929
3930 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003931 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3932 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003933
3934 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3935 p_blkno, first_hash, num_clusters, xs);
3936
3937out:
3938 return ret;
3939}
3940
Tao Ma0c044f02008-08-18 17:38:50 +08003941static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3942 u64 blkno,
3943 u32 clusters,
3944 xattr_bucket_func *func,
3945 void *para)
3946{
Joel Becker6dde41d2008-10-24 17:16:48 -07003947 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003948 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3949 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003950 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003951
Joel Beckerba937122008-10-24 19:13:20 -07003952 bucket = ocfs2_xattr_bucket_new(inode);
3953 if (!bucket) {
3954 mlog_errno(-ENOMEM);
3955 return -ENOMEM;
3956 }
Tao Ma0c044f02008-08-18 17:38:50 +08003957
3958 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003959 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003960
Joel Beckerba937122008-10-24 19:13:20 -07003961 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3962 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003963 if (ret) {
3964 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003965 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003966 }
3967
Tao Ma0c044f02008-08-18 17:38:50 +08003968 /*
3969 * The real bucket num in this series of blocks is stored
3970 * in the 1st bucket.
3971 */
3972 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003973 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003974
Mark Fashehde29c082008-10-29 14:45:30 -07003975 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3976 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003977 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003978 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003979 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003980 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003981 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003982 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003983 }
3984
Joel Beckerba937122008-10-24 19:13:20 -07003985 ocfs2_xattr_bucket_relse(bucket);
3986 if (ret)
3987 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003988 }
3989
Joel Beckerba937122008-10-24 19:13:20 -07003990 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003991 return ret;
3992}
3993
3994struct ocfs2_xattr_tree_list {
3995 char *buffer;
3996 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003997 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003998};
3999
Tao Mafd68a892009-08-18 11:43:21 +08004000static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08004001 struct ocfs2_xattr_header *xh,
4002 int index,
4003 int *block_off,
4004 int *new_offset)
4005{
4006 u16 name_offset;
4007
4008 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
4009 return -EINVAL;
4010
4011 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
4012
Tao Mafd68a892009-08-18 11:43:21 +08004013 *block_off = name_offset >> sb->s_blocksize_bits;
4014 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08004015
4016 return 0;
4017}
4018
4019static int ocfs2_list_xattr_bucket(struct inode *inode,
4020 struct ocfs2_xattr_bucket *bucket,
4021 void *para)
4022{
Tao Ma936b8832008-10-09 23:06:14 +08004023 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08004024 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08004025 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08004026 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08004027
Joel Becker3e632942008-10-24 17:04:49 -07004028 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4029 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08004030 type = ocfs2_xattr_get_type(entry);
4031 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08004032
Tao Ma936b8832008-10-09 23:06:14 +08004033 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08004034 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07004035 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08004036 i,
4037 &block_off,
4038 &new_offset);
4039 if (ret)
4040 break;
Tao Ma936b8832008-10-09 23:06:14 +08004041
Joel Becker51def392008-10-24 16:57:21 -07004042 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08004043 new_offset;
4044 ret = ocfs2_xattr_list_entry(xl->buffer,
4045 xl->buffer_size,
4046 &xl->result,
4047 prefix, name,
4048 entry->xe_name_len);
4049 if (ret)
4050 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004051 }
4052 }
4053
4054 return ret;
4055}
4056
Tao Ma47bca492009-08-18 11:43:42 +08004057static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4058 struct buffer_head *blk_bh,
4059 xattr_tree_rec_func *rec_func,
4060 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08004061{
Tao Ma47bca492009-08-18 11:43:42 +08004062 struct ocfs2_xattr_block *xb =
4063 (struct ocfs2_xattr_block *)blk_bh->b_data;
4064 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08004065 int ret = 0;
4066 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4067 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08004068
Tao Ma47bca492009-08-18 11:43:42 +08004069 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08004070 return 0;
4071
4072 while (name_hash > 0) {
4073 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4074 &e_cpos, &num_clusters, el);
4075 if (ret) {
4076 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004077 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004078 }
4079
Tao Ma47bca492009-08-18 11:43:42 +08004080 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4081 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08004082 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08004083 if (ret != -ERANGE)
4084 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004085 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004086 }
4087
4088 if (e_cpos == 0)
4089 break;
4090
4091 name_hash = e_cpos - 1;
4092 }
4093
Tao Ma47bca492009-08-18 11:43:42 +08004094 return ret;
4095
4096}
4097
4098static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4099 struct buffer_head *root_bh,
4100 u64 blkno, u32 cpos, u32 len, void *para)
4101{
4102 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4103 ocfs2_list_xattr_bucket, para);
4104}
4105
4106static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4107 struct buffer_head *blk_bh,
4108 char *buffer,
4109 size_t buffer_size)
4110{
4111 int ret;
4112 struct ocfs2_xattr_tree_list xl = {
4113 .buffer = buffer,
4114 .buffer_size = buffer_size,
4115 .result = 0,
4116 };
4117
4118 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4119 ocfs2_list_xattr_tree_rec, &xl);
4120 if (ret) {
4121 mlog_errno(ret);
4122 goto out;
4123 }
4124
Tao Ma936b8832008-10-09 23:06:14 +08004125 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08004126out:
4127 return ret;
4128}
Tao Ma01225592008-08-18 17:38:53 +08004129
4130static int cmp_xe(const void *a, const void *b)
4131{
4132 const struct ocfs2_xattr_entry *l = a, *r = b;
4133 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4134 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4135
4136 if (l_hash > r_hash)
4137 return 1;
4138 if (l_hash < r_hash)
4139 return -1;
4140 return 0;
4141}
4142
4143static void swap_xe(void *a, void *b, int size)
4144{
4145 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4146
4147 tmp = *l;
4148 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4149 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4150}
4151
4152/*
4153 * When the ocfs2_xattr_block is filled up, new bucket will be created
4154 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07004155 * The header goes at the start of the bucket, and the names+values are
4156 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08004157 * Note: we need to sort the entries since they are not saved in order
4158 * in the ocfs2_xattr_block.
4159 */
4160static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4161 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07004162 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08004163{
4164 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07004165 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004166 u16 offset, size, off_change;
4167 struct ocfs2_xattr_entry *xe;
4168 struct ocfs2_xattr_block *xb =
4169 (struct ocfs2_xattr_block *)xb_bh->b_data;
4170 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004171 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004172 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07004173 char *src = xb_bh->b_data;
4174 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08004175
4176 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4177 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07004178 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08004179
Joel Becker178eeac2008-10-27 15:18:29 -07004180 for (i = 0; i < blks; i++)
4181 memset(bucket_block(bucket, i), 0, blocksize);
4182
Tao Ma01225592008-08-18 17:38:53 +08004183 /*
4184 * Since the xe_name_offset is based on ocfs2_xattr_header,
4185 * there is a offset change corresponding to the change of
4186 * ocfs2_xattr_header's position.
4187 */
4188 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4189 xe = &xb_xh->xh_entries[count - 1];
4190 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4191 size = blocksize - offset;
4192
4193 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08004194 memcpy(target + offset, src + offset, size);
4195
4196 /* Init new header now. */
4197 xh->xh_count = xb_xh->xh_count;
4198 xh->xh_num_buckets = cpu_to_le16(1);
4199 xh->xh_name_value_len = cpu_to_le16(size);
4200 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4201
4202 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07004203 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004204 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4205 size = count * sizeof(struct ocfs2_xattr_entry);
4206 memcpy(target + offset, (char *)xb_xh + offset, size);
4207
4208 /* Change the xe offset for all the xe because of the move. */
4209 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4210 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4211 for (i = 0; i < count; i++)
4212 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4213
4214 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4215 offset, size, off_change);
4216
4217 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4218 cmp_xe, swap_xe);
4219}
4220
4221/*
4222 * After we move xattr from block to index btree, we have to
4223 * update ocfs2_xattr_search to the new xe and base.
4224 *
4225 * When the entry is in xattr block, xattr_bh indicates the storage place.
4226 * While if the entry is in index b-tree, "bucket" indicates the
4227 * real place of the xattr.
4228 */
Joel Becker178eeac2008-10-27 15:18:29 -07004229static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4230 struct ocfs2_xattr_search *xs,
4231 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08004232{
Tao Ma01225592008-08-18 17:38:53 +08004233 char *buf = old_bh->b_data;
4234 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4235 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004236 int i;
Tao Ma01225592008-08-18 17:38:53 +08004237
Joel Beckerba937122008-10-24 19:13:20 -07004238 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07004239 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004240 xs->end = xs->base + inode->i_sb->s_blocksize;
4241
Joel Becker178eeac2008-10-27 15:18:29 -07004242 if (xs->not_found)
4243 return;
Tao Ma01225592008-08-18 17:38:53 +08004244
Joel Becker178eeac2008-10-27 15:18:29 -07004245 i = xs->here - old_xh->xh_entries;
4246 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004247}
4248
4249static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004250 struct ocfs2_xattr_search *xs,
4251 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004252{
Tao Ma85db90e2008-11-12 08:27:01 +08004253 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004254 u32 bit_off, len;
4255 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08004256 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004257 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08004258 struct buffer_head *xb_bh = xs->xattr_bh;
4259 struct ocfs2_xattr_block *xb =
4260 (struct ocfs2_xattr_block *)xb_bh->b_data;
4261 struct ocfs2_xattr_tree_root *xr;
4262 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08004263
4264 mlog(0, "create xattr index block for %llu\n",
4265 (unsigned long long)xb_bh->b_blocknr);
4266
4267 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07004268 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004269
Tao Ma01225592008-08-18 17:38:53 +08004270 /*
4271 * XXX:
4272 * We can use this lock for now, and maybe move to a dedicated mutex
4273 * if performance becomes a problem later.
4274 */
4275 down_write(&oi->ip_alloc_sem);
4276
Joel Becker0cf2f762009-02-12 16:41:25 -08004277 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08004278 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004279 if (ret) {
4280 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004281 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004282 }
4283
Joel Becker1ed9b772010-05-06 13:59:06 +08004284 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
Tao Ma78f30c32008-11-12 08:27:00 +08004285 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08004286 if (ret) {
4287 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004288 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004289 }
4290
4291 /*
4292 * The bucket may spread in many blocks, and
4293 * we will only touch the 1st block and the last block
4294 * in the whole bucket(one for entry and one for data).
4295 */
4296 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4297
Mark Fashehde29c082008-10-29 14:45:30 -07004298 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4299 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08004300
Joel Becker178eeac2008-10-27 15:18:29 -07004301 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004302 if (ret) {
4303 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004304 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004305 }
4306
Joel Becker178eeac2008-10-27 15:18:29 -07004307 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4308 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07004309 if (ret) {
4310 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004311 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07004312 }
Tao Ma01225592008-08-18 17:38:53 +08004313
Joel Becker178eeac2008-10-27 15:18:29 -07004314 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4315 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4316
4317 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4318
Tao Ma01225592008-08-18 17:38:53 +08004319 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4320 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4321 offsetof(struct ocfs2_xattr_block, xb_attrs));
4322
4323 xr = &xb->xb_attrs.xb_root;
4324 xr->xt_clusters = cpu_to_le32(1);
4325 xr->xt_last_eb_blk = 0;
4326 xr->xt_list.l_tree_depth = 0;
4327 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4328 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4329
4330 xr->xt_list.l_recs[0].e_cpos = 0;
4331 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4332 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4333
4334 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4335
Tao Ma85db90e2008-11-12 08:27:01 +08004336 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004337
Tao Ma85db90e2008-11-12 08:27:01 +08004338out:
Tao Ma01225592008-08-18 17:38:53 +08004339 up_write(&oi->ip_alloc_sem);
4340
Tao Ma01225592008-08-18 17:38:53 +08004341 return ret;
4342}
4343
4344static int cmp_xe_offset(const void *a, const void *b)
4345{
4346 const struct ocfs2_xattr_entry *l = a, *r = b;
4347 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4348 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4349
4350 if (l_name_offset < r_name_offset)
4351 return 1;
4352 if (l_name_offset > r_name_offset)
4353 return -1;
4354 return 0;
4355}
4356
4357/*
4358 * defrag a xattr bucket if we find that the bucket has some
4359 * holes beteen name/value pairs.
4360 * We will move all the name/value pairs to the end of the bucket
4361 * so that we can spare some space for insertion.
4362 */
4363static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004364 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004365 struct ocfs2_xattr_bucket *bucket)
4366{
4367 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004368 size_t end, offset, len;
Tao Ma01225592008-08-18 17:38:53 +08004369 struct ocfs2_xattr_header *xh;
4370 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07004371 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004372 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08004373 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08004374 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08004375
4376 /*
4377 * In order to make the operation more efficient and generic,
4378 * we copy all the blocks into a contiguous memory and do the
4379 * defragment there, so if anything is error, we will not touch
4380 * the real block.
4381 */
4382 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4383 if (!bucket_buf) {
4384 ret = -EIO;
4385 goto out;
4386 }
4387
Joel Becker161d6f32008-10-27 15:25:18 -07004388 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004389 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4390 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07004391
Tao Ma1c32a2f2008-11-06 08:10:47 +08004392 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07004393 OCFS2_JOURNAL_ACCESS_WRITE);
4394 if (ret < 0) {
4395 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004396 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004397 }
4398
4399 xh = (struct ocfs2_xattr_header *)bucket_buf;
4400 entries = (char *)xh->xh_entries;
4401 xh_free_start = le16_to_cpu(xh->xh_free_start);
4402
4403 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4404 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004405 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4406 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08004407
4408 /*
4409 * sort all the entries by their offset.
4410 * the largest will be the first, so that we can
4411 * move them to the end one by one.
4412 */
4413 sort(entries, le16_to_cpu(xh->xh_count),
4414 sizeof(struct ocfs2_xattr_entry),
4415 cmp_xe_offset, swap_xe);
4416
4417 /* Move all name/values to the end of the bucket. */
4418 xe = xh->xh_entries;
4419 end = OCFS2_XATTR_BUCKET_SIZE;
4420 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4421 offset = le16_to_cpu(xe->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07004422 len = namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004423
4424 /*
4425 * We must make sure that the name/value pair
4426 * exist in the same block. So adjust end to
4427 * the previous block end if needed.
4428 */
4429 if (((end - len) / blocksize !=
4430 (end - 1) / blocksize))
4431 end = end - end % blocksize;
4432
4433 if (end > offset + len) {
4434 memmove(bucket_buf + end - len,
4435 bucket_buf + offset, len);
4436 xe->xe_name_offset = cpu_to_le16(end - len);
4437 }
4438
4439 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4440 "bucket %llu\n", (unsigned long long)blkno);
4441
4442 end -= len;
4443 }
4444
4445 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4446 "bucket %llu\n", (unsigned long long)blkno);
4447
4448 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08004449 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004450
4451 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4452 xh->xh_free_start = cpu_to_le16(end);
4453
4454 /* sort the entries by their name_hash. */
4455 sort(entries, le16_to_cpu(xh->xh_count),
4456 sizeof(struct ocfs2_xattr_entry),
4457 cmp_xe, swap_xe);
4458
4459 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004460 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4461 memcpy(bucket_block(bucket, i), buf, blocksize);
4462 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004463
Tao Ma01225592008-08-18 17:38:53 +08004464out:
Tao Ma01225592008-08-18 17:38:53 +08004465 kfree(bucket_buf);
4466 return ret;
4467}
4468
4469/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08004470 * prev_blkno points to the start of an existing extent. new_blkno
4471 * points to a newly allocated extent. Because we know each of our
4472 * clusters contains more than bucket, we can easily split one cluster
4473 * at a bucket boundary. So we take the last cluster of the existing
4474 * extent and split it down the middle. We move the last half of the
4475 * buckets in the last cluster of the existing extent over to the new
4476 * extent.
Tao Ma01225592008-08-18 17:38:53 +08004477 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08004478 * first_bh is the buffer at prev_blkno so we can update the existing
4479 * extent's bucket count. header_bh is the bucket were we were hoping
4480 * to insert our xattr. If the bucket move places the target in the new
4481 * extent, we'll update first_bh and header_bh after modifying the old
4482 * extent.
4483 *
4484 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004485 */
4486static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4487 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004488 struct ocfs2_xattr_bucket *first,
4489 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004490 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08004491 u32 num_clusters,
4492 u32 *first_hash)
4493{
Joel Beckerc58b6032008-11-26 13:36:24 -08004494 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08004495 struct super_block *sb = inode->i_sb;
4496 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4497 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08004498 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08004499 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08004500 u64 last_cluster_blkno = bucket_blkno(first) +
4501 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004502
Joel Becker41cb8142008-11-26 14:25:21 -08004503 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4504 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08004505
Tao Ma01225592008-08-18 17:38:53 +08004506 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08004507 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004508
Joel Becker41cb8142008-11-26 14:25:21 -08004509 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08004510 last_cluster_blkno, new_blkno,
4511 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004512 if (ret) {
4513 mlog_errno(ret);
4514 goto out;
4515 }
4516
Joel Beckerc58b6032008-11-26 13:36:24 -08004517 /* This is the first bucket that got moved */
4518 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4519
Tao Ma01225592008-08-18 17:38:53 +08004520 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004521 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08004522 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08004523 */
Joel Becker41cb8142008-11-26 14:25:21 -08004524 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08004525 /* Find the block for the new target bucket */
4526 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08004527 (bucket_blkno(target) - src_blkno);
4528
4529 ocfs2_xattr_bucket_relse(first);
4530 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004531
4532 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004533 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08004534 * journal from ocfs2_cp_xattr_bucket().
4535 */
Joel Becker41cb8142008-11-26 14:25:21 -08004536 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08004537 if (ret) {
4538 mlog_errno(ret);
4539 goto out;
4540 }
Joel Becker41cb8142008-11-26 14:25:21 -08004541 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4542 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08004543 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004544
Joel Beckerb5c03e72008-11-25 19:58:16 -08004545 }
4546
Tao Ma01225592008-08-18 17:38:53 +08004547out:
Tao Ma01225592008-08-18 17:38:53 +08004548 return ret;
4549}
4550
Tao Ma01225592008-08-18 17:38:53 +08004551/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004552 * Find the suitable pos when we divide a bucket into 2.
4553 * We have to make sure the xattrs with the same hash value exist
4554 * in the same bucket.
4555 *
4556 * If this ocfs2_xattr_header covers more than one hash value, find a
4557 * place where the hash value changes. Try to find the most even split.
4558 * The most common case is that all entries have different hash values,
4559 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08004560 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004561static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4562{
4563 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4564 int count = le16_to_cpu(xh->xh_count);
4565 int delta, middle = count / 2;
4566
4567 /*
4568 * We start at the middle. Each step gets farther away in both
4569 * directions. We therefore hit the change in hash value
4570 * nearest to the middle. Note that this loop does not execute for
4571 * count < 2.
4572 */
4573 for (delta = 0; delta < middle; delta++) {
4574 /* Let's check delta earlier than middle */
4575 if (cmp_xe(&entries[middle - delta - 1],
4576 &entries[middle - delta]))
4577 return middle - delta;
4578
4579 /* For even counts, don't walk off the end */
4580 if ((middle + delta + 1) == count)
4581 continue;
4582
4583 /* Now try delta past middle */
4584 if (cmp_xe(&entries[middle + delta],
4585 &entries[middle + delta + 1]))
4586 return middle + delta + 1;
4587 }
4588
4589 /* Every entry had the same hash */
4590 return count;
4591}
4592
4593/*
4594 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4595 * first_hash will record the 1st hash of the new bucket.
4596 *
4597 * Normally half of the xattrs will be moved. But we have to make
4598 * sure that the xattrs with the same hash value are stored in the
4599 * same bucket. If all the xattrs in this bucket have the same hash
4600 * value, the new bucket will be initialized as an empty one and the
4601 * first_hash will be initialized as (hash_value+1).
4602 */
4603static int ocfs2_divide_xattr_bucket(struct inode *inode,
4604 handle_t *handle,
4605 u64 blk,
4606 u64 new_blk,
4607 u32 *first_hash,
4608 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004609{
4610 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004611 int count, start, len, name_value_len = 0, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004612 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004613 struct ocfs2_xattr_header *xh;
4614 struct ocfs2_xattr_entry *xe;
4615 int blocksize = inode->i_sb->s_blocksize;
4616
Tao Ma80bcaf32008-10-27 06:06:24 +08004617 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004618 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004619
Joel Beckerba937122008-10-24 19:13:20 -07004620 s_bucket = ocfs2_xattr_bucket_new(inode);
4621 t_bucket = ocfs2_xattr_bucket_new(inode);
4622 if (!s_bucket || !t_bucket) {
4623 ret = -ENOMEM;
4624 mlog_errno(ret);
4625 goto out;
4626 }
Tao Ma01225592008-08-18 17:38:53 +08004627
Joel Beckerba937122008-10-24 19:13:20 -07004628 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004629 if (ret) {
4630 mlog_errno(ret);
4631 goto out;
4632 }
4633
Joel Beckerba937122008-10-24 19:13:20 -07004634 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004635 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004636 if (ret) {
4637 mlog_errno(ret);
4638 goto out;
4639 }
4640
Joel Becker784b8162008-10-24 17:33:40 -07004641 /*
4642 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4643 * there's no need to read it.
4644 */
Joel Beckerba937122008-10-24 19:13:20 -07004645 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004646 if (ret) {
4647 mlog_errno(ret);
4648 goto out;
4649 }
4650
Joel Becker2b656c12008-11-25 19:00:15 -08004651 /*
4652 * Hey, if we're overwriting t_bucket, what difference does
4653 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4654 * same part of ocfs2_cp_xattr_bucket().
4655 */
Joel Beckerba937122008-10-24 19:13:20 -07004656 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004657 new_bucket_head ?
4658 OCFS2_JOURNAL_ACCESS_CREATE :
4659 OCFS2_JOURNAL_ACCESS_WRITE);
4660 if (ret) {
4661 mlog_errno(ret);
4662 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004663 }
4664
Joel Beckerba937122008-10-24 19:13:20 -07004665 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004666 count = le16_to_cpu(xh->xh_count);
4667 start = ocfs2_xattr_find_divide_pos(xh);
4668
4669 if (start == count) {
4670 xe = &xh->xh_entries[start-1];
4671
4672 /*
4673 * initialized a new empty bucket here.
4674 * The hash value is set as one larger than
4675 * that of the last entry in the previous bucket.
4676 */
Joel Beckerba937122008-10-24 19:13:20 -07004677 for (i = 0; i < t_bucket->bu_blocks; i++)
4678 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004679
Joel Beckerba937122008-10-24 19:13:20 -07004680 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004681 xh->xh_free_start = cpu_to_le16(blocksize);
4682 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4683 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4684
4685 goto set_num_buckets;
4686 }
4687
Tao Ma01225592008-08-18 17:38:53 +08004688 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004689 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004690
4691 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004692 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004693
4694 /*
4695 * Calculate the total name/value len and xh_free_start for
4696 * the old bucket first.
4697 */
4698 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4699 name_value_len = 0;
4700 for (i = 0; i < start; i++) {
4701 xe = &xh->xh_entries[i];
Joel Becker199799a2009-08-14 19:04:15 -07004702 name_value_len += namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004703 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4704 name_offset = le16_to_cpu(xe->xe_name_offset);
4705 }
4706
4707 /*
4708 * Now begin the modification to the new bucket.
4709 *
4710 * In the new bucket, We just move the xattr entry to the beginning
4711 * and don't touch the name/value. So there will be some holes in the
4712 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4713 * called.
4714 */
4715 xe = &xh->xh_entries[start];
4716 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4717 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004718 (int)((char *)xe - (char *)xh),
4719 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004720 memmove((char *)xh->xh_entries, (char *)xe, len);
4721 xe = &xh->xh_entries[count - start];
4722 len = sizeof(struct ocfs2_xattr_entry) * start;
4723 memset((char *)xe, 0, len);
4724
4725 le16_add_cpu(&xh->xh_count, -start);
4726 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4727
4728 /* Calculate xh_free_start for the new bucket. */
4729 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4730 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4731 xe = &xh->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004732 if (le16_to_cpu(xe->xe_name_offset) <
4733 le16_to_cpu(xh->xh_free_start))
4734 xh->xh_free_start = xe->xe_name_offset;
4735 }
4736
Tao Ma80bcaf32008-10-27 06:06:24 +08004737set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004738 /* set xh->xh_num_buckets for the new xh. */
4739 if (new_bucket_head)
4740 xh->xh_num_buckets = cpu_to_le16(1);
4741 else
4742 xh->xh_num_buckets = 0;
4743
Joel Beckerba937122008-10-24 19:13:20 -07004744 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004745
4746 /* store the first_hash of the new bucket. */
4747 if (first_hash)
4748 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4749
4750 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004751 * Now only update the 1st block of the old bucket. If we
4752 * just added a new empty bucket, there is no need to modify
4753 * it.
Tao Ma01225592008-08-18 17:38:53 +08004754 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004755 if (start == count)
4756 goto out;
4757
Joel Beckerba937122008-10-24 19:13:20 -07004758 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004759 memset(&xh->xh_entries[start], 0,
4760 sizeof(struct ocfs2_xattr_entry) * (count - start));
4761 xh->xh_count = cpu_to_le16(start);
4762 xh->xh_free_start = cpu_to_le16(name_offset);
4763 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4764
Joel Beckerba937122008-10-24 19:13:20 -07004765 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004766
4767out:
Joel Beckerba937122008-10-24 19:13:20 -07004768 ocfs2_xattr_bucket_free(s_bucket);
4769 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004770
4771 return ret;
4772}
4773
4774/*
4775 * Copy xattr from one bucket to another bucket.
4776 *
4777 * The caller must make sure that the journal transaction
4778 * has enough space for journaling.
4779 */
4780static int ocfs2_cp_xattr_bucket(struct inode *inode,
4781 handle_t *handle,
4782 u64 s_blkno,
4783 u64 t_blkno,
4784 int t_is_new)
4785{
Joel Becker4980c6d2008-10-24 18:54:43 -07004786 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004787 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004788
4789 BUG_ON(s_blkno == t_blkno);
4790
4791 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004792 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4793 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004794
Joel Beckerba937122008-10-24 19:13:20 -07004795 s_bucket = ocfs2_xattr_bucket_new(inode);
4796 t_bucket = ocfs2_xattr_bucket_new(inode);
4797 if (!s_bucket || !t_bucket) {
4798 ret = -ENOMEM;
4799 mlog_errno(ret);
4800 goto out;
4801 }
Joel Becker92de1092008-11-25 17:06:40 -08004802
Joel Beckerba937122008-10-24 19:13:20 -07004803 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004804 if (ret)
4805 goto out;
4806
Joel Becker784b8162008-10-24 17:33:40 -07004807 /*
4808 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4809 * there's no need to read it.
4810 */
Joel Beckerba937122008-10-24 19:13:20 -07004811 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004812 if (ret)
4813 goto out;
4814
Joel Becker2b656c12008-11-25 19:00:15 -08004815 /*
4816 * Hey, if we're overwriting t_bucket, what difference does
4817 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004818 * cluster to fill, we came here from
4819 * ocfs2_mv_xattr_buckets(), and it is really new -
4820 * ACCESS_CREATE is required. But we also might have moved data
4821 * out of t_bucket before extending back into it.
4822 * ocfs2_add_new_xattr_bucket() can do this - its call to
4823 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004824 * and copied out the end of the old extent. Then it re-extends
4825 * the old extent back to create space for new xattrs. That's
4826 * how we get here, and the bucket isn't really new.
4827 */
Joel Beckerba937122008-10-24 19:13:20 -07004828 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004829 t_is_new ?
4830 OCFS2_JOURNAL_ACCESS_CREATE :
4831 OCFS2_JOURNAL_ACCESS_WRITE);
4832 if (ret)
4833 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004834
Joel Beckerba937122008-10-24 19:13:20 -07004835 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4836 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004837
4838out:
Joel Beckerba937122008-10-24 19:13:20 -07004839 ocfs2_xattr_bucket_free(t_bucket);
4840 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004841
4842 return ret;
4843}
4844
4845/*
Joel Becker874d65a2008-11-26 13:02:18 -08004846 * src_blk points to the start of an existing extent. last_blk points to
4847 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004848 * extent. We copy the buckets from the cluster at last_blk to the new
4849 * extent. If start_bucket is non-zero, we skip that many buckets before
4850 * we start copying. The new extent's xh_num_buckets gets set to the
4851 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4852 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004853 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004854static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4855 u64 src_blk, u64 last_blk, u64 to_blk,
4856 unsigned int start_bucket,
4857 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004858{
4859 int i, ret, credits;
4860 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004861 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004862 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004863 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004864
Joel Becker874d65a2008-11-26 13:02:18 -08004865 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4866 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004867
Joel Becker54ecb6b2008-11-26 13:18:31 -08004868 BUG_ON(start_bucket >= num_buckets);
4869 if (start_bucket) {
4870 num_buckets -= start_bucket;
4871 last_blk += (start_bucket * blks_per_bucket);
4872 }
4873
Joel Becker15d60922008-11-25 18:36:42 -08004874 /* The first bucket of the original extent */
4875 old_first = ocfs2_xattr_bucket_new(inode);
4876 /* The first bucket of the new extent */
4877 new_first = ocfs2_xattr_bucket_new(inode);
4878 if (!old_first || !new_first) {
4879 ret = -ENOMEM;
4880 mlog_errno(ret);
4881 goto out;
4882 }
4883
Joel Becker874d65a2008-11-26 13:02:18 -08004884 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004885 if (ret) {
4886 mlog_errno(ret);
4887 goto out;
4888 }
4889
Tao Ma01225592008-08-18 17:38:53 +08004890 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004891 * We need to update the first bucket of the old extent and all
4892 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004893 */
Tao Mac901fb02010-04-26 14:34:57 +08004894 credits = ((num_buckets + 1) * blks_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004895 ret = ocfs2_extend_trans(handle, credits);
4896 if (ret) {
4897 mlog_errno(ret);
4898 goto out;
4899 }
4900
Joel Becker15d60922008-11-25 18:36:42 -08004901 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4902 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004903 if (ret) {
4904 mlog_errno(ret);
4905 goto out;
4906 }
4907
4908 for (i = 0; i < num_buckets; i++) {
4909 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004910 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004911 to_blk + (i * blks_per_bucket),
4912 1);
Tao Ma01225592008-08-18 17:38:53 +08004913 if (ret) {
4914 mlog_errno(ret);
4915 goto out;
4916 }
Tao Ma01225592008-08-18 17:38:53 +08004917 }
4918
Joel Becker15d60922008-11-25 18:36:42 -08004919 /*
4920 * Get the new bucket ready before we dirty anything
4921 * (This actually shouldn't fail, because we already dirtied
4922 * it once in ocfs2_cp_xattr_bucket()).
4923 */
4924 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4925 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004926 mlog_errno(ret);
4927 goto out;
4928 }
Joel Becker15d60922008-11-25 18:36:42 -08004929 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4930 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004931 if (ret) {
4932 mlog_errno(ret);
4933 goto out;
4934 }
4935
Joel Becker15d60922008-11-25 18:36:42 -08004936 /* Now update the headers */
4937 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4938 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004939
Joel Becker15d60922008-11-25 18:36:42 -08004940 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4941 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004942
4943 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004944 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4945
Tao Ma01225592008-08-18 17:38:53 +08004946out:
Joel Becker15d60922008-11-25 18:36:42 -08004947 ocfs2_xattr_bucket_free(new_first);
4948 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004949 return ret;
4950}
4951
4952/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004953 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004954 * This function should only be called when bucket size == cluster size.
4955 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4956 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004957static int ocfs2_divide_xattr_cluster(struct inode *inode,
4958 handle_t *handle,
4959 u64 prev_blk,
4960 u64 new_blk,
4961 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004962{
4963 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mac901fb02010-04-26 14:34:57 +08004964 int ret, credits = 2 * blk_per_bucket;
Tao Ma01225592008-08-18 17:38:53 +08004965
4966 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4967
4968 ret = ocfs2_extend_trans(handle, credits);
4969 if (ret) {
4970 mlog_errno(ret);
4971 return ret;
4972 }
4973
4974 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004975 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4976 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004977}
4978
4979/*
4980 * Move some xattrs from the old cluster to the new one since they are not
4981 * contiguous in ocfs2 xattr tree.
4982 *
4983 * new_blk starts a new separate cluster, and we will move some xattrs from
4984 * prev_blk to it. v_start will be set as the first name hash value in this
4985 * new cluster so that it can be used as e_cpos during tree insertion and
4986 * don't collide with our original b-tree operations. first_bh and header_bh
4987 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4988 * to extend the insert bucket.
4989 *
4990 * The problem is how much xattr should we move to the new one and when should
4991 * we update first_bh and header_bh?
4992 * 1. If cluster size > bucket size, that means the previous cluster has more
4993 * than 1 bucket, so just move half nums of bucket into the new cluster and
4994 * update the first_bh and header_bh if the insert bucket has been moved
4995 * to the new cluster.
4996 * 2. If cluster_size == bucket_size:
4997 * a) If the previous extent rec has more than one cluster and the insert
4998 * place isn't in the last cluster, copy the entire last cluster to the
4999 * new one. This time, we don't need to upate the first_bh and header_bh
5000 * since they will not be moved into the new cluster.
5001 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
5002 * the new one. And we set the extend flag to zero if the insert place is
5003 * moved into the new allocated cluster since no extend is needed.
5004 */
5005static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
5006 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08005007 struct ocfs2_xattr_bucket *first,
5008 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08005009 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08005010 u32 prev_clusters,
5011 u32 *v_start,
5012 int *extend)
5013{
Joel Becker92cf3ad2008-11-26 14:12:09 -08005014 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005015
5016 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08005017 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07005018 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08005019
Joel Becker41cb8142008-11-26 14:25:21 -08005020 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08005021 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
5022 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08005023 first, target,
Tao Ma01225592008-08-18 17:38:53 +08005024 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08005025 prev_clusters,
5026 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005027 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08005028 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08005029 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08005030 /* The start of the last cluster in the first extent */
5031 u64 last_blk = bucket_blkno(first) +
5032 ((prev_clusters - 1) *
5033 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08005034
Joel Becker012ee912008-11-26 14:43:31 -08005035 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08005036 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08005037 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08005038 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08005039 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005040 if (ret)
5041 mlog_errno(ret);
5042 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08005043 ret = ocfs2_divide_xattr_cluster(inode, handle,
5044 last_blk, new_blk,
5045 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005046 if (ret)
5047 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005048
Joel Becker92cf3ad2008-11-26 14:12:09 -08005049 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08005050 *extend = 0;
5051 }
5052 }
5053
5054 return ret;
5055}
5056
5057/*
5058 * Add a new cluster for xattr storage.
5059 *
5060 * If the new cluster is contiguous with the previous one, it will be
5061 * appended to the same extent record, and num_clusters will be updated.
5062 * If not, we will insert a new extent for it and move some xattrs in
5063 * the last cluster into the new allocated one.
5064 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5065 * lose the benefits of hashing because we'll have to search large leaves.
5066 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5067 * if it's bigger).
5068 *
5069 * first_bh is the first block of the previous extent rec and header_bh
5070 * indicates the bucket we will insert the new xattrs. They will be updated
5071 * when the header_bh is moved into the new cluster.
5072 */
5073static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5074 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005075 struct ocfs2_xattr_bucket *first,
5076 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08005077 u32 *num_clusters,
5078 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005079 int *extend,
5080 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005081{
Tao Ma85db90e2008-11-12 08:27:01 +08005082 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005083 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5084 u32 prev_clusters = *num_clusters;
5085 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5086 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08005087 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08005088 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005089 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08005090
5091 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5092 "previous xattr blkno = %llu\n",
5093 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08005094 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08005095
Joel Becker5e404e92009-02-13 03:54:22 -08005096 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005097
Joel Becker0cf2f762009-02-12 16:41:25 -08005098 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005099 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005100 if (ret < 0) {
5101 mlog_errno(ret);
5102 goto leave;
5103 }
5104
Joel Becker1ed9b772010-05-06 13:59:06 +08005105 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08005106 clusters_to_add, &bit_off, &num_bits);
5107 if (ret < 0) {
5108 if (ret != -ENOSPC)
5109 mlog_errno(ret);
5110 goto leave;
5111 }
5112
5113 BUG_ON(num_bits > clusters_to_add);
5114
5115 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5116 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5117 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5118
Joel Beckered29c0c2008-11-26 15:08:44 -08005119 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08005120 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5121 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5122 /*
5123 * If this cluster is contiguous with the old one and
5124 * adding this new cluster, we don't surpass the limit of
5125 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5126 * initialized and used like other buckets in the previous
5127 * cluster.
5128 * So add it as a contiguous one. The caller will handle
5129 * its init process.
5130 */
5131 v_start = prev_cpos + prev_clusters;
5132 *num_clusters = prev_clusters + num_bits;
5133 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5134 num_bits);
5135 } else {
5136 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5137 handle,
Joel Becker012ee912008-11-26 14:43:31 -08005138 first,
5139 target,
Tao Ma01225592008-08-18 17:38:53 +08005140 block,
Tao Ma01225592008-08-18 17:38:53 +08005141 prev_clusters,
5142 &v_start,
5143 extend);
5144 if (ret) {
5145 mlog_errno(ret);
5146 goto leave;
5147 }
5148 }
5149
5150 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07005151 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08005152 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08005153 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005154 if (ret < 0) {
5155 mlog_errno(ret);
5156 goto leave;
5157 }
5158
Joel Beckerec20cec2010-03-19 14:13:52 -07005159 ocfs2_journal_dirty(handle, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005160
5161leave:
Tao Ma01225592008-08-18 17:38:53 +08005162 return ret;
5163}
5164
5165/*
Joel Becker92de1092008-11-25 17:06:40 -08005166 * We are given an extent. 'first' is the bucket at the very front of
5167 * the extent. The extent has space for an additional bucket past
5168 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5169 * of the target bucket. We wish to shift every bucket past the target
5170 * down one, filling in that additional space. When we get back to the
5171 * target, we split the target between itself and the now-empty bucket
5172 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005173 */
5174static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005175 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08005176 struct ocfs2_xattr_bucket *first,
5177 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08005178 u32 num_clusters)
5179{
5180 int ret, credits;
5181 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5182 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08005183 u64 end_blk;
5184 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08005185
5186 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08005187 "from %llu, len = %u\n", (unsigned long long)target_blk,
5188 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005189
Joel Becker92de1092008-11-25 17:06:40 -08005190 /* The extent must have room for an additional bucket */
5191 BUG_ON(new_bucket >=
5192 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08005193
Joel Becker92de1092008-11-25 17:06:40 -08005194 /* end_blk points to the last existing bucket */
5195 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005196
5197 /*
Joel Becker92de1092008-11-25 17:06:40 -08005198 * end_blk is the start of the last existing bucket.
5199 * Thus, (end_blk - target_blk) covers the target bucket and
5200 * every bucket after it up to, but not including, the last
5201 * existing bucket. Then we add the last existing bucket, the
5202 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005203 */
Tao Mac901fb02010-04-26 14:34:57 +08005204 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
Tao Ma85db90e2008-11-12 08:27:01 +08005205 ret = ocfs2_extend_trans(handle, credits);
5206 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08005207 mlog_errno(ret);
5208 goto out;
5209 }
5210
Joel Becker92de1092008-11-25 17:06:40 -08005211 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5212 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005213 if (ret) {
5214 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005215 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005216 }
5217
Joel Becker92de1092008-11-25 17:06:40 -08005218 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08005219 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5220 end_blk + blk_per_bucket, 0);
5221 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08005222 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005223 end_blk -= blk_per_bucket;
5224 }
5225
Joel Becker92de1092008-11-25 17:06:40 -08005226 /* Move half of the xattr in target_blkno to the next bucket. */
5227 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5228 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08005229
Joel Becker92de1092008-11-25 17:06:40 -08005230 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5231 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08005232
Tao Ma01225592008-08-18 17:38:53 +08005233out:
5234 return ret;
5235}
5236
5237/*
Joel Becker91f20332008-11-26 15:25:41 -08005238 * Add new xattr bucket in an extent record and adjust the buckets
5239 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5240 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08005241 *
Joel Becker91f20332008-11-26 15:25:41 -08005242 * In the easy case, we will move all the buckets after target down by
5243 * one. Half of target's xattrs will be moved to the next bucket.
5244 *
5245 * If current cluster is full, we'll allocate a new one. This may not
5246 * be contiguous. The underlying calls will make sure that there is
5247 * space for the insert, shifting buckets around if necessary.
5248 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08005249 */
5250static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5251 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005252 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08005253 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005254{
Tao Ma01225592008-08-18 17:38:53 +08005255 struct ocfs2_xattr_block *xb =
5256 (struct ocfs2_xattr_block *)xb_bh->b_data;
5257 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5258 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08005259 u32 name_hash =
5260 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08005261 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08005262 int ret, num_buckets, extend = 1;
5263 u64 p_blkno;
5264 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08005265 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08005266 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08005267
Joel Becker91f20332008-11-26 15:25:41 -08005268 mlog(0, "Add new xattr bucket starting from %llu\n",
5269 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08005270
Joel Beckered29c0c2008-11-26 15:08:44 -08005271 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08005272 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08005273 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08005274 ret = -ENOMEM;
5275 mlog_errno(ret);
5276 goto out;
5277 }
5278
Tao Ma01225592008-08-18 17:38:53 +08005279 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5280 &num_clusters, el);
5281 if (ret) {
5282 mlog_errno(ret);
5283 goto out;
5284 }
5285
Joel Beckered29c0c2008-11-26 15:08:44 -08005286 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5287 if (ret) {
5288 mlog_errno(ret);
5289 goto out;
5290 }
5291
Tao Ma01225592008-08-18 17:38:53 +08005292 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08005293 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5294 /*
5295 * This can move first+target if the target bucket moves
5296 * to the new extent.
5297 */
Tao Ma01225592008-08-18 17:38:53 +08005298 ret = ocfs2_add_new_xattr_cluster(inode,
5299 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005300 first,
5301 target,
Tao Ma01225592008-08-18 17:38:53 +08005302 &num_clusters,
5303 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005304 &extend,
5305 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005306 if (ret) {
5307 mlog_errno(ret);
5308 goto out;
5309 }
5310 }
5311
Joel Becker92de1092008-11-25 17:06:40 -08005312 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08005313 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005314 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08005315 first,
5316 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08005317 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08005318 if (ret)
5319 mlog_errno(ret);
5320 }
5321
Tao Ma01225592008-08-18 17:38:53 +08005322out:
Joel Becker92de1092008-11-25 17:06:40 -08005323 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08005324
Tao Ma01225592008-08-18 17:38:53 +08005325 return ret;
5326}
5327
5328static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5329 struct ocfs2_xattr_bucket *bucket,
5330 int offs)
5331{
5332 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5333
5334 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07005335 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08005336}
5337
5338/*
Tao Ma01225592008-08-18 17:38:53 +08005339 * Truncate the specified xe_off entry in xattr bucket.
5340 * bucket is indicated by header_bh and len is the new length.
5341 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5342 *
5343 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5344 */
5345static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08005346 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08005347 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08005348 int len,
5349 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005350{
5351 int ret, offset;
5352 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08005353 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08005354 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08005355 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08005356 struct ocfs2_xattr_value_buf vb = {
5357 .vb_access = ocfs2_journal_access,
5358 };
Tao Ma01225592008-08-18 17:38:53 +08005359
5360 xe = &xh->xh_entries[xe_off];
5361
5362 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5363
5364 offset = le16_to_cpu(xe->xe_name_offset) +
5365 OCFS2_XATTR_SIZE(xe->xe_name_len);
5366
5367 value_blk = offset / blocksize;
5368
5369 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5370 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005371
Joel Beckerb3e5d372008-12-09 15:01:04 -08005372 vb.vb_bh = bucket->bu_bhs[value_blk];
5373 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08005374
Joel Beckerb3e5d372008-12-09 15:01:04 -08005375 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5376 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005377
Joel Becker548b0f22008-11-24 19:32:13 -08005378 /*
5379 * From here on out we have to dirty the bucket. The generic
5380 * value calls only modify one of the bucket's bhs, but we need
5381 * to send the bucket at once. So if they error, they *could* have
5382 * modified something. We have to assume they did, and dirty
5383 * the whole bucket. This leaves us in a consistent state.
5384 */
Tao Ma01225592008-08-18 17:38:53 +08005385 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08005386 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08005387 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005388 if (ret) {
5389 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005390 goto out;
5391 }
5392
5393 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5394 OCFS2_JOURNAL_ACCESS_WRITE);
5395 if (ret) {
5396 mlog_errno(ret);
5397 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005398 }
5399
Joel Becker548b0f22008-11-24 19:32:13 -08005400 xe->xe_value_size = cpu_to_le64(len);
5401
Joel Becker548b0f22008-11-24 19:32:13 -08005402 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005403
5404out:
Tao Ma01225592008-08-18 17:38:53 +08005405 return ret;
5406}
5407
Tao Ma01225592008-08-18 17:38:53 +08005408static int ocfs2_rm_xattr_cluster(struct inode *inode,
5409 struct buffer_head *root_bh,
5410 u64 blkno,
5411 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005412 u32 len,
5413 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005414{
5415 int ret;
5416 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5417 struct inode *tl_inode = osb->osb_tl_inode;
5418 handle_t *handle;
5419 struct ocfs2_xattr_block *xb =
5420 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005421 struct ocfs2_alloc_context *meta_ac = NULL;
5422 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005423 struct ocfs2_extent_tree et;
5424
Tao Ma47bca492009-08-18 11:43:42 +08005425 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005426 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005427 if (ret) {
5428 mlog_errno(ret);
5429 return ret;
5430 }
5431
Joel Becker5e404e92009-02-13 03:54:22 -08005432 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005433
5434 ocfs2_init_dealloc_ctxt(&dealloc);
5435
5436 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5437 cpos, len, (unsigned long long)blkno);
5438
Joel Becker8cb471e2009-02-10 20:00:41 -08005439 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5440 len);
Tao Ma01225592008-08-18 17:38:53 +08005441
Joel Beckerf99b9b72008-08-20 19:36:33 -07005442 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005443 if (ret) {
5444 mlog_errno(ret);
5445 return ret;
5446 }
5447
5448 mutex_lock(&tl_inode->i_mutex);
5449
5450 if (ocfs2_truncate_log_needs_flush(osb)) {
5451 ret = __ocfs2_flush_truncate_log(osb);
5452 if (ret < 0) {
5453 mlog_errno(ret);
5454 goto out;
5455 }
5456 }
5457
Jan Karaa90714c2008-10-09 19:38:40 +02005458 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005459 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005460 ret = -ENOMEM;
5461 mlog_errno(ret);
5462 goto out;
5463 }
5464
Joel Becker0cf2f762009-02-12 16:41:25 -08005465 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005466 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005467 if (ret) {
5468 mlog_errno(ret);
5469 goto out_commit;
5470 }
5471
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005472 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005473 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005474 if (ret) {
5475 mlog_errno(ret);
5476 goto out_commit;
5477 }
5478
5479 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
Joel Beckerec20cec2010-03-19 14:13:52 -07005480 ocfs2_journal_dirty(handle, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005481
5482 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5483 if (ret)
5484 mlog_errno(ret);
5485
5486out_commit:
5487 ocfs2_commit_trans(osb, handle);
5488out:
5489 ocfs2_schedule_truncate_log_flush(osb, 1);
5490
5491 mutex_unlock(&tl_inode->i_mutex);
5492
5493 if (meta_ac)
5494 ocfs2_free_alloc_context(meta_ac);
5495
5496 ocfs2_run_deallocs(osb, &dealloc);
5497
5498 return ret;
5499}
5500
Tao Ma01225592008-08-18 17:38:53 +08005501/*
Tao Ma80bcaf32008-10-27 06:06:24 +08005502 * check whether the xattr bucket is filled up with the same hash value.
5503 * If we want to insert the xattr with the same hash, return -ENOSPC.
5504 * If we want to insert a xattr with different hash value, go ahead
5505 * and ocfs2_divide_xattr_bucket will handle this.
5506 */
Tao Ma01225592008-08-18 17:38:53 +08005507static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005508 struct ocfs2_xattr_bucket *bucket,
5509 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005510{
Joel Becker3e632942008-10-24 17:04:49 -07005511 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005512 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5513
5514 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5515 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005516
5517 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5518 xh->xh_entries[0].xe_name_hash) {
5519 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5520 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005521 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005522 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5523 return -ENOSPC;
5524 }
5525
5526 return 0;
5527}
5528
Joel Beckerc5d95df2009-08-18 21:03:24 -07005529/*
5530 * Try to set the entry in the current bucket. If we fail, the caller
5531 * will handle getting us another bucket.
5532 */
5533static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5534 struct ocfs2_xattr_info *xi,
5535 struct ocfs2_xattr_search *xs,
5536 struct ocfs2_xattr_set_ctxt *ctxt)
5537{
5538 int ret;
5539 struct ocfs2_xa_loc loc;
5540
5541 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5542
5543 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5544 xs->not_found ? NULL : xs->here);
5545 ret = ocfs2_xa_set(&loc, xi, ctxt);
5546 if (!ret) {
5547 xs->here = loc.xl_entry;
5548 goto out;
5549 }
5550 if (ret != -ENOSPC) {
5551 mlog_errno(ret);
5552 goto out;
5553 }
5554
5555 /* Ok, we need space. Let's try defragmenting the bucket. */
5556 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5557 xs->bucket);
5558 if (ret) {
5559 mlog_errno(ret);
5560 goto out;
5561 }
5562
5563 ret = ocfs2_xa_set(&loc, xi, ctxt);
5564 if (!ret) {
5565 xs->here = loc.xl_entry;
5566 goto out;
5567 }
5568 if (ret != -ENOSPC)
5569 mlog_errno(ret);
5570
5571
5572out:
5573 mlog_exit(ret);
5574 return ret;
5575}
5576
Tao Ma01225592008-08-18 17:38:53 +08005577static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5578 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005579 struct ocfs2_xattr_search *xs,
5580 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005581{
Joel Beckerc5d95df2009-08-18 21:03:24 -07005582 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005583
Joel Becker6b240ff2009-08-14 18:02:52 -07005584 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005585
Joel Beckerc5d95df2009-08-18 21:03:24 -07005586 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5587 if (!ret)
5588 goto out;
5589 if (ret != -ENOSPC) {
5590 mlog_errno(ret);
5591 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005592 }
5593
Joel Beckerc5d95df2009-08-18 21:03:24 -07005594 /* Ack, need more space. Let's try to get another bucket! */
5595
Tao Ma01225592008-08-18 17:38:53 +08005596 /*
Joel Beckerc5d95df2009-08-18 21:03:24 -07005597 * We do not allow for overlapping ranges between buckets. And
5598 * the maximum number of collisions we will allow for then is
5599 * one bucket's worth, so check it here whether we need to
5600 * add a new bucket for the insert.
Tao Ma01225592008-08-18 17:38:53 +08005601 */
Joel Beckerc5d95df2009-08-18 21:03:24 -07005602 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Becker91f20332008-11-26 15:25:41 -08005603 xs->bucket,
Joel Beckerc5d95df2009-08-18 21:03:24 -07005604 xi->xi_name);
5605 if (ret) {
5606 mlog_errno(ret);
5607 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005608 }
5609
Joel Beckerc5d95df2009-08-18 21:03:24 -07005610 ret = ocfs2_add_new_xattr_bucket(inode,
5611 xs->xattr_bh,
5612 xs->bucket,
5613 ctxt);
5614 if (ret) {
5615 mlog_errno(ret);
5616 goto out;
5617 }
5618
5619 /*
5620 * ocfs2_add_new_xattr_bucket() will have updated
5621 * xs->bucket if it moved, but it will not have updated
5622 * any of the other search fields. Thus, we drop it and
5623 * re-search. Everything should be cached, so it'll be
5624 * quick.
5625 */
5626 ocfs2_xattr_bucket_relse(xs->bucket);
5627 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5628 xi->xi_name_index,
5629 xi->xi_name, xs);
5630 if (ret && ret != -ENODATA)
5631 goto out;
5632 xs->not_found = ret;
5633
5634 /* Ok, we have a new bucket, let's try again */
5635 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5636 if (ret && (ret != -ENOSPC))
5637 mlog_errno(ret);
5638
Tao Ma01225592008-08-18 17:38:53 +08005639out:
5640 mlog_exit(ret);
5641 return ret;
5642}
Tao Maa3944252008-08-18 17:38:54 +08005643
5644static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5645 struct ocfs2_xattr_bucket *bucket,
5646 void *para)
5647{
Tao Mace9c5a52009-08-18 11:43:59 +08005648 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005649 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005650 u16 i;
5651 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005652 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5653 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005654 int credits = ocfs2_remove_extent_credits(osb->sb) +
5655 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005656 struct ocfs2_xattr_value_root *xv;
5657 struct ocfs2_rm_xattr_bucket_para *args =
5658 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005659
5660 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005661
5662 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5663 xe = &xh->xh_entries[i];
5664 if (ocfs2_xattr_is_local(xe))
5665 continue;
5666
Tao Mace9c5a52009-08-18 11:43:59 +08005667 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5668 i, &xv, NULL);
5669
5670 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5671 args->ref_ci,
5672 args->ref_root_bh,
5673 &ctxt.meta_ac,
5674 &ref_credits);
5675
5676 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005677 if (IS_ERR(ctxt.handle)) {
5678 ret = PTR_ERR(ctxt.handle);
5679 mlog_errno(ret);
5680 break;
5681 }
5682
Joel Becker548b0f22008-11-24 19:32:13 -08005683 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005684 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005685
5686 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005687 if (ctxt.meta_ac) {
5688 ocfs2_free_alloc_context(ctxt.meta_ac);
5689 ctxt.meta_ac = NULL;
5690 }
Tao Maa3944252008-08-18 17:38:54 +08005691 if (ret) {
5692 mlog_errno(ret);
5693 break;
5694 }
5695 }
5696
Tao Mace9c5a52009-08-18 11:43:59 +08005697 if (ctxt.meta_ac)
5698 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005699 ocfs2_schedule_truncate_log_flush(osb, 1);
5700 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005701 return ret;
5702}
5703
Mark Fasheh99219ae2008-10-07 14:52:59 -07005704/*
Tao Ma492a8a32009-08-18 11:43:17 +08005705 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5706 * or change the extent record flag), we need to recalculate
5707 * the metaecc for the whole bucket. So it is done here.
5708 *
5709 * Note:
5710 * We have to give the extra credits for the caller.
5711 */
5712static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5713 handle_t *handle,
5714 void *para)
5715{
5716 int ret;
5717 struct ocfs2_xattr_bucket *bucket =
5718 (struct ocfs2_xattr_bucket *)para;
5719
5720 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5721 OCFS2_JOURNAL_ACCESS_WRITE);
5722 if (ret) {
5723 mlog_errno(ret);
5724 return ret;
5725 }
5726
5727 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5728
5729 return 0;
5730}
5731
5732/*
5733 * Special action we need if the xattr value is refcounted.
5734 *
5735 * 1. If the xattr is refcounted, lock the tree.
5736 * 2. CoW the xattr if we are setting the new value and the value
5737 * will be stored outside.
5738 * 3. In other case, decrease_refcount will work for us, so just
5739 * lock the refcount tree, calculate the meta and credits is OK.
5740 *
5741 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5742 * currently CoW is a completed transaction, while this function
5743 * will also lock the allocators and let us deadlock. So we will
5744 * CoW the whole xattr value.
5745 */
5746static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5747 struct ocfs2_dinode *di,
5748 struct ocfs2_xattr_info *xi,
5749 struct ocfs2_xattr_search *xis,
5750 struct ocfs2_xattr_search *xbs,
5751 struct ocfs2_refcount_tree **ref_tree,
5752 int *meta_add,
5753 int *credits)
5754{
5755 int ret = 0;
5756 struct ocfs2_xattr_block *xb;
5757 struct ocfs2_xattr_entry *xe;
5758 char *base;
5759 u32 p_cluster, num_clusters;
5760 unsigned int ext_flags;
5761 int name_offset, name_len;
5762 struct ocfs2_xattr_value_buf vb;
5763 struct ocfs2_xattr_bucket *bucket = NULL;
5764 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5765 struct ocfs2_post_refcount refcount;
5766 struct ocfs2_post_refcount *p = NULL;
5767 struct buffer_head *ref_root_bh = NULL;
5768
5769 if (!xis->not_found) {
5770 xe = xis->here;
5771 name_offset = le16_to_cpu(xe->xe_name_offset);
5772 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5773 base = xis->base;
5774 vb.vb_bh = xis->inode_bh;
5775 vb.vb_access = ocfs2_journal_access_di;
5776 } else {
5777 int i, block_off = 0;
5778 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5779 xe = xbs->here;
5780 name_offset = le16_to_cpu(xe->xe_name_offset);
5781 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5782 i = xbs->here - xbs->header->xh_entries;
5783
5784 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005785 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005786 bucket_xh(xbs->bucket),
5787 i, &block_off,
5788 &name_offset);
5789 if (ret) {
5790 mlog_errno(ret);
5791 goto out;
5792 }
5793 base = bucket_block(xbs->bucket, block_off);
5794 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5795 vb.vb_access = ocfs2_journal_access;
5796
5797 if (ocfs2_meta_ecc(osb)) {
5798 /*create parameters for ocfs2_post_refcount. */
5799 bucket = xbs->bucket;
5800 refcount.credits = bucket->bu_blocks;
5801 refcount.para = bucket;
5802 refcount.func =
5803 ocfs2_xattr_bucket_post_refcount;
5804 p = &refcount;
5805 }
5806 } else {
5807 base = xbs->base;
5808 vb.vb_bh = xbs->xattr_bh;
5809 vb.vb_access = ocfs2_journal_access_xb;
5810 }
5811 }
5812
5813 if (ocfs2_xattr_is_local(xe))
5814 goto out;
5815
5816 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5817 (base + name_offset + name_len);
5818
5819 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5820 &num_clusters, &vb.vb_xv->xr_list,
5821 &ext_flags);
5822 if (ret) {
5823 mlog_errno(ret);
5824 goto out;
5825 }
5826
5827 /*
5828 * We just need to check the 1st extent record, since we always
5829 * CoW the whole xattr. So there shouldn't be a xattr with
5830 * some REFCOUNT extent recs after the 1st one.
5831 */
5832 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5833 goto out;
5834
5835 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5836 1, ref_tree, &ref_root_bh);
5837 if (ret) {
5838 mlog_errno(ret);
5839 goto out;
5840 }
5841
5842 /*
5843 * If we are deleting the xattr or the new size will be stored inside,
5844 * cool, leave it there, the xattr truncate process will remove them
5845 * for us(it still needs the refcount tree lock and the meta, credits).
5846 * And the worse case is that every cluster truncate will split the
5847 * refcount tree, and make the original extent become 3. So we will need
5848 * 2 * cluster more extent recs at most.
5849 */
Joel Becker6b240ff2009-08-14 18:02:52 -07005850 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
Tao Ma492a8a32009-08-18 11:43:17 +08005851
5852 ret = ocfs2_refcounted_xattr_delete_need(inode,
5853 &(*ref_tree)->rf_ci,
5854 ref_root_bh, vb.vb_xv,
5855 meta_add, credits);
5856 if (ret)
5857 mlog_errno(ret);
5858 goto out;
5859 }
5860
5861 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5862 *ref_tree, ref_root_bh, 0,
5863 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5864 if (ret)
5865 mlog_errno(ret);
5866
5867out:
5868 brelse(ref_root_bh);
5869 return ret;
5870}
5871
5872/*
Tao Ma01292412009-09-21 13:04:19 +08005873 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5874 * The physical clusters will be added to refcount tree.
5875 */
5876static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5877 struct ocfs2_xattr_value_root *xv,
5878 struct ocfs2_extent_tree *value_et,
5879 struct ocfs2_caching_info *ref_ci,
5880 struct buffer_head *ref_root_bh,
5881 struct ocfs2_cached_dealloc_ctxt *dealloc,
5882 struct ocfs2_post_refcount *refcount)
5883{
5884 int ret = 0;
5885 u32 clusters = le32_to_cpu(xv->xr_clusters);
5886 u32 cpos, p_cluster, num_clusters;
5887 struct ocfs2_extent_list *el = &xv->xr_list;
5888 unsigned int ext_flags;
5889
5890 cpos = 0;
5891 while (cpos < clusters) {
5892 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5893 &num_clusters, el, &ext_flags);
5894
5895 cpos += num_clusters;
5896 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5897 continue;
5898
5899 BUG_ON(!p_cluster);
5900
5901 ret = ocfs2_add_refcount_flag(inode, value_et,
5902 ref_ci, ref_root_bh,
5903 cpos - num_clusters,
5904 p_cluster, num_clusters,
5905 dealloc, refcount);
5906 if (ret) {
5907 mlog_errno(ret);
5908 break;
5909 }
5910 }
5911
5912 return ret;
5913}
5914
5915/*
5916 * Given a normal ocfs2_xattr_header, refcount all the entries which
5917 * have value stored outside.
5918 * Used for xattrs stored in inode and ocfs2_xattr_block.
5919 */
5920static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5921 struct ocfs2_xattr_value_buf *vb,
5922 struct ocfs2_xattr_header *header,
5923 struct ocfs2_caching_info *ref_ci,
5924 struct buffer_head *ref_root_bh,
5925 struct ocfs2_cached_dealloc_ctxt *dealloc)
5926{
5927
5928 struct ocfs2_xattr_entry *xe;
5929 struct ocfs2_xattr_value_root *xv;
5930 struct ocfs2_extent_tree et;
5931 int i, ret = 0;
5932
5933 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5934 xe = &header->xh_entries[i];
5935
5936 if (ocfs2_xattr_is_local(xe))
5937 continue;
5938
5939 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5940 le16_to_cpu(xe->xe_name_offset) +
5941 OCFS2_XATTR_SIZE(xe->xe_name_len));
5942
5943 vb->vb_xv = xv;
5944 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5945
5946 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5947 ref_ci, ref_root_bh,
5948 dealloc, NULL);
5949 if (ret) {
5950 mlog_errno(ret);
5951 break;
5952 }
5953 }
5954
5955 return ret;
5956}
5957
5958static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5959 struct buffer_head *fe_bh,
5960 struct ocfs2_caching_info *ref_ci,
5961 struct buffer_head *ref_root_bh,
5962 struct ocfs2_cached_dealloc_ctxt *dealloc)
5963{
5964 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5965 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5966 (fe_bh->b_data + inode->i_sb->s_blocksize -
5967 le16_to_cpu(di->i_xattr_inline_size));
5968 struct ocfs2_xattr_value_buf vb = {
5969 .vb_bh = fe_bh,
5970 .vb_access = ocfs2_journal_access_di,
5971 };
5972
5973 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5974 ref_ci, ref_root_bh, dealloc);
5975}
5976
5977struct ocfs2_xattr_tree_value_refcount_para {
5978 struct ocfs2_caching_info *ref_ci;
5979 struct buffer_head *ref_root_bh;
5980 struct ocfs2_cached_dealloc_ctxt *dealloc;
5981};
5982
5983static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5984 struct ocfs2_xattr_bucket *bucket,
5985 int offset,
5986 struct ocfs2_xattr_value_root **xv,
5987 struct buffer_head **bh)
5988{
5989 int ret, block_off, name_offset;
5990 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5991 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5992 void *base;
5993
5994 ret = ocfs2_xattr_bucket_get_name_value(sb,
5995 bucket_xh(bucket),
5996 offset,
5997 &block_off,
5998 &name_offset);
5999 if (ret) {
6000 mlog_errno(ret);
6001 goto out;
6002 }
6003
6004 base = bucket_block(bucket, block_off);
6005
6006 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
6007 OCFS2_XATTR_SIZE(xe->xe_name_len));
6008
6009 if (bh)
6010 *bh = bucket->bu_bhs[block_off];
6011out:
6012 return ret;
6013}
6014
6015/*
6016 * For a given xattr bucket, refcount all the entries which
6017 * have value stored outside.
6018 */
6019static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
6020 struct ocfs2_xattr_bucket *bucket,
6021 void *para)
6022{
6023 int i, ret = 0;
6024 struct ocfs2_extent_tree et;
6025 struct ocfs2_xattr_tree_value_refcount_para *ref =
6026 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6027 struct ocfs2_xattr_header *xh =
6028 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6029 struct ocfs2_xattr_entry *xe;
6030 struct ocfs2_xattr_value_buf vb = {
6031 .vb_access = ocfs2_journal_access,
6032 };
6033 struct ocfs2_post_refcount refcount = {
6034 .credits = bucket->bu_blocks,
6035 .para = bucket,
6036 .func = ocfs2_xattr_bucket_post_refcount,
6037 };
6038 struct ocfs2_post_refcount *p = NULL;
6039
6040 /* We only need post_refcount if we support metaecc. */
6041 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6042 p = &refcount;
6043
6044 mlog(0, "refcount bucket %llu, count = %u\n",
6045 (unsigned long long)bucket_blkno(bucket),
6046 le16_to_cpu(xh->xh_count));
6047 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6048 xe = &xh->xh_entries[i];
6049
6050 if (ocfs2_xattr_is_local(xe))
6051 continue;
6052
6053 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6054 &vb.vb_xv, &vb.vb_bh);
6055 if (ret) {
6056 mlog_errno(ret);
6057 break;
6058 }
6059
6060 ocfs2_init_xattr_value_extent_tree(&et,
6061 INODE_CACHE(inode), &vb);
6062
6063 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6064 &et, ref->ref_ci,
6065 ref->ref_root_bh,
6066 ref->dealloc, p);
6067 if (ret) {
6068 mlog_errno(ret);
6069 break;
6070 }
6071 }
6072
6073 return ret;
6074
6075}
6076
6077static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6078 struct buffer_head *root_bh,
6079 u64 blkno, u32 cpos, u32 len, void *para)
6080{
6081 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6082 ocfs2_xattr_bucket_value_refcount,
6083 para);
6084}
6085
6086static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6087 struct buffer_head *blk_bh,
6088 struct ocfs2_caching_info *ref_ci,
6089 struct buffer_head *ref_root_bh,
6090 struct ocfs2_cached_dealloc_ctxt *dealloc)
6091{
6092 int ret = 0;
6093 struct ocfs2_xattr_block *xb =
6094 (struct ocfs2_xattr_block *)blk_bh->b_data;
6095
6096 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6097 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6098 struct ocfs2_xattr_value_buf vb = {
6099 .vb_bh = blk_bh,
6100 .vb_access = ocfs2_journal_access_xb,
6101 };
6102
6103 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6104 ref_ci, ref_root_bh,
6105 dealloc);
6106 } else {
6107 struct ocfs2_xattr_tree_value_refcount_para para = {
6108 .ref_ci = ref_ci,
6109 .ref_root_bh = ref_root_bh,
6110 .dealloc = dealloc,
6111 };
6112
6113 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6114 ocfs2_refcount_xattr_tree_rec,
6115 &para);
6116 }
6117
6118 return ret;
6119}
6120
6121int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6122 struct buffer_head *fe_bh,
6123 struct ocfs2_caching_info *ref_ci,
6124 struct buffer_head *ref_root_bh,
6125 struct ocfs2_cached_dealloc_ctxt *dealloc)
6126{
6127 int ret = 0;
6128 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6129 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6130 struct buffer_head *blk_bh = NULL;
6131
6132 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6133 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6134 ref_ci, ref_root_bh,
6135 dealloc);
6136 if (ret) {
6137 mlog_errno(ret);
6138 goto out;
6139 }
6140 }
6141
6142 if (!di->i_xattr_loc)
6143 goto out;
6144
6145 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6146 &blk_bh);
6147 if (ret < 0) {
6148 mlog_errno(ret);
6149 goto out;
6150 }
6151
6152 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6153 ref_root_bh, dealloc);
6154 if (ret)
6155 mlog_errno(ret);
6156
6157 brelse(blk_bh);
6158out:
6159
6160 return ret;
6161}
6162
Tao Ma0fe9b662009-08-18 11:47:56 +08006163typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006164/*
Tao Ma2999d122009-08-18 11:43:55 +08006165 * Store the information we need in xattr reflink.
6166 * old_bh and new_bh are inode bh for the old and new inode.
6167 */
6168struct ocfs2_xattr_reflink {
6169 struct inode *old_inode;
6170 struct inode *new_inode;
6171 struct buffer_head *old_bh;
6172 struct buffer_head *new_bh;
6173 struct ocfs2_caching_info *ref_ci;
6174 struct buffer_head *ref_root_bh;
6175 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006176 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006177};
6178
6179/*
6180 * Given a xattr header and xe offset,
6181 * return the proper xv and the corresponding bh.
6182 * xattr in inode, block and xattr tree have different implementaions.
6183 */
6184typedef int (get_xattr_value_root)(struct super_block *sb,
6185 struct buffer_head *bh,
6186 struct ocfs2_xattr_header *xh,
6187 int offset,
6188 struct ocfs2_xattr_value_root **xv,
6189 struct buffer_head **ret_bh,
6190 void *para);
6191
6192/*
6193 * Calculate all the xattr value root metadata stored in this xattr header and
6194 * credits we need if we create them from the scratch.
6195 * We use get_xattr_value_root so that all types of xattr container can use it.
6196 */
6197static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6198 struct buffer_head *bh,
6199 struct ocfs2_xattr_header *xh,
6200 int *metas, int *credits,
6201 int *num_recs,
6202 get_xattr_value_root *func,
6203 void *para)
6204{
6205 int i, ret = 0;
6206 struct ocfs2_xattr_value_root *xv;
6207 struct ocfs2_xattr_entry *xe;
6208
6209 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6210 xe = &xh->xh_entries[i];
6211 if (ocfs2_xattr_is_local(xe))
6212 continue;
6213
6214 ret = func(sb, bh, xh, i, &xv, NULL, para);
6215 if (ret) {
6216 mlog_errno(ret);
6217 break;
6218 }
6219
6220 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6221 le16_to_cpu(xv->xr_list.l_next_free_rec);
6222
6223 *credits += ocfs2_calc_extend_credits(sb,
6224 &def_xv.xv.xr_list,
6225 le32_to_cpu(xv->xr_clusters));
6226
6227 /*
6228 * If the value is a tree with depth > 1, We don't go deep
6229 * to the extent block, so just calculate a maximum record num.
6230 */
6231 if (!xv->xr_list.l_tree_depth)
Tao Ma8ff6af82009-12-23 14:31:15 +08006232 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
Tao Ma2999d122009-08-18 11:43:55 +08006233 else
6234 *num_recs += ocfs2_clusters_for_bytes(sb,
6235 XATTR_SIZE_MAX);
6236 }
6237
6238 return ret;
6239}
6240
6241/* Used by xattr inode and block to return the right xv and buffer_head. */
6242static int ocfs2_get_xattr_value_root(struct super_block *sb,
6243 struct buffer_head *bh,
6244 struct ocfs2_xattr_header *xh,
6245 int offset,
6246 struct ocfs2_xattr_value_root **xv,
6247 struct buffer_head **ret_bh,
6248 void *para)
6249{
6250 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6251
6252 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6253 le16_to_cpu(xe->xe_name_offset) +
6254 OCFS2_XATTR_SIZE(xe->xe_name_len));
6255
6256 if (ret_bh)
6257 *ret_bh = bh;
6258
6259 return 0;
6260}
6261
6262/*
6263 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6264 * It is only used for inline xattr and xattr block.
6265 */
6266static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6267 struct ocfs2_xattr_header *xh,
6268 struct buffer_head *ref_root_bh,
6269 int *credits,
6270 struct ocfs2_alloc_context **meta_ac)
6271{
6272 int ret, meta_add = 0, num_recs = 0;
6273 struct ocfs2_refcount_block *rb =
6274 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6275
6276 *credits = 0;
6277
6278 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6279 &meta_add, credits, &num_recs,
6280 ocfs2_get_xattr_value_root,
6281 NULL);
6282 if (ret) {
6283 mlog_errno(ret);
6284 goto out;
6285 }
6286
6287 /*
6288 * We need to add/modify num_recs in refcount tree, so just calculate
6289 * an approximate number we need for refcount tree change.
6290 * Sometimes we need to split the tree, and after split, half recs
6291 * will be moved to the new block, and a new block can only provide
6292 * half number of recs. So we multiple new blocks by 2.
6293 */
6294 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6295 meta_add += num_recs;
6296 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6297 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6298 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6299 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6300 else
6301 *credits += 1;
6302
6303 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6304 if (ret)
6305 mlog_errno(ret);
6306
6307out:
6308 return ret;
6309}
6310
6311/*
6312 * Given a xattr header, reflink all the xattrs in this container.
6313 * It can be used for inode, block and bucket.
6314 *
6315 * NOTE:
6316 * Before we call this function, the caller has memcpy the xattr in
6317 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006318 *
6319 * If args.xattr_reflinked is set, call it to decide whether the xe should
6320 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006321 */
6322static int ocfs2_reflink_xattr_header(handle_t *handle,
6323 struct ocfs2_xattr_reflink *args,
6324 struct buffer_head *old_bh,
6325 struct ocfs2_xattr_header *xh,
6326 struct buffer_head *new_bh,
6327 struct ocfs2_xattr_header *new_xh,
6328 struct ocfs2_xattr_value_buf *vb,
6329 struct ocfs2_alloc_context *meta_ac,
6330 get_xattr_value_root *func,
6331 void *para)
6332{
Tao Ma0fe9b662009-08-18 11:47:56 +08006333 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006334 struct super_block *sb = args->old_inode->i_sb;
6335 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006336 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006337 struct ocfs2_xattr_value_root *xv, *new_xv;
6338 struct ocfs2_extent_tree data_et;
6339 u32 clusters, cpos, p_cluster, num_clusters;
6340 unsigned int ext_flags = 0;
6341
6342 mlog(0, "reflink xattr in container %llu, count = %u\n",
6343 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006344
6345 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6346 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006347 xe = &xh->xh_entries[i];
6348
Tao Ma0fe9b662009-08-18 11:47:56 +08006349 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6350 xe = &new_xh->xh_entries[j];
6351
6352 le16_add_cpu(&new_xh->xh_count, -1);
6353 if (new_xh->xh_count) {
6354 memmove(xe, xe + 1,
6355 (void *)last - (void *)xe);
6356 memset(last, 0,
6357 sizeof(struct ocfs2_xattr_entry));
6358 }
6359
6360 /*
6361 * We don't want j to increase in the next round since
6362 * it is already moved ahead.
6363 */
6364 j--;
6365 continue;
6366 }
6367
Tao Ma2999d122009-08-18 11:43:55 +08006368 if (ocfs2_xattr_is_local(xe))
6369 continue;
6370
6371 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6372 if (ret) {
6373 mlog_errno(ret);
6374 break;
6375 }
6376
Tao Ma0fe9b662009-08-18 11:47:56 +08006377 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006378 if (ret) {
6379 mlog_errno(ret);
6380 break;
6381 }
6382
6383 /*
6384 * For the xattr which has l_tree_depth = 0, all the extent
6385 * recs have already be copied to the new xh with the
6386 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6387 * increase the refount count int the refcount tree.
6388 *
6389 * For the xattr which has l_tree_depth > 0, we need
6390 * to initialize it to the empty default value root,
6391 * and then insert the extents one by one.
6392 */
6393 if (xv->xr_list.l_tree_depth) {
6394 memcpy(new_xv, &def_xv, sizeof(def_xv));
6395 vb->vb_xv = new_xv;
6396 vb->vb_bh = value_bh;
6397 ocfs2_init_xattr_value_extent_tree(&data_et,
6398 INODE_CACHE(args->new_inode), vb);
6399 }
6400
6401 clusters = le32_to_cpu(xv->xr_clusters);
6402 cpos = 0;
6403 while (cpos < clusters) {
6404 ret = ocfs2_xattr_get_clusters(args->old_inode,
6405 cpos,
6406 &p_cluster,
6407 &num_clusters,
6408 &xv->xr_list,
6409 &ext_flags);
6410 if (ret) {
6411 mlog_errno(ret);
6412 goto out;
6413 }
6414
6415 BUG_ON(!p_cluster);
6416
6417 if (xv->xr_list.l_tree_depth) {
6418 ret = ocfs2_insert_extent(handle,
6419 &data_et, cpos,
6420 ocfs2_clusters_to_blocks(
6421 args->old_inode->i_sb,
6422 p_cluster),
6423 num_clusters, ext_flags,
6424 meta_ac);
6425 if (ret) {
6426 mlog_errno(ret);
6427 goto out;
6428 }
6429 }
6430
6431 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6432 args->ref_root_bh,
6433 p_cluster, num_clusters,
6434 meta_ac, args->dealloc);
6435 if (ret) {
6436 mlog_errno(ret);
6437 goto out;
6438 }
6439
6440 cpos += num_clusters;
6441 }
6442 }
6443
6444out:
6445 return ret;
6446}
6447
6448static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6449{
6450 int ret = 0, credits = 0;
6451 handle_t *handle;
6452 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6453 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6454 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6455 int header_off = osb->sb->s_blocksize - inline_size;
6456 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6457 (args->old_bh->b_data + header_off);
6458 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6459 (args->new_bh->b_data + header_off);
6460 struct ocfs2_alloc_context *meta_ac = NULL;
6461 struct ocfs2_inode_info *new_oi;
6462 struct ocfs2_dinode *new_di;
6463 struct ocfs2_xattr_value_buf vb = {
6464 .vb_bh = args->new_bh,
6465 .vb_access = ocfs2_journal_access_di,
6466 };
6467
6468 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6469 &credits, &meta_ac);
6470 if (ret) {
6471 mlog_errno(ret);
6472 goto out;
6473 }
6474
6475 handle = ocfs2_start_trans(osb, credits);
6476 if (IS_ERR(handle)) {
6477 ret = PTR_ERR(handle);
6478 mlog_errno(ret);
6479 goto out;
6480 }
6481
6482 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6483 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6484 if (ret) {
6485 mlog_errno(ret);
6486 goto out_commit;
6487 }
6488
6489 memcpy(args->new_bh->b_data + header_off,
6490 args->old_bh->b_data + header_off, inline_size);
6491
6492 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6493 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6494
6495 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6496 args->new_bh, new_xh, &vb, meta_ac,
6497 ocfs2_get_xattr_value_root, NULL);
6498 if (ret) {
6499 mlog_errno(ret);
6500 goto out_commit;
6501 }
6502
6503 new_oi = OCFS2_I(args->new_inode);
6504 spin_lock(&new_oi->ip_lock);
6505 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6506 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6507 spin_unlock(&new_oi->ip_lock);
6508
6509 ocfs2_journal_dirty(handle, args->new_bh);
6510
6511out_commit:
6512 ocfs2_commit_trans(osb, handle);
6513
6514out:
6515 if (meta_ac)
6516 ocfs2_free_alloc_context(meta_ac);
6517 return ret;
6518}
6519
6520static int ocfs2_create_empty_xattr_block(struct inode *inode,
6521 struct buffer_head *fe_bh,
6522 struct buffer_head **ret_bh,
6523 int indexed)
6524{
6525 int ret;
Tao Ma2999d122009-08-18 11:43:55 +08006526 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Mab2317962010-03-19 15:04:24 +08006527 struct ocfs2_xattr_set_ctxt ctxt;
Tao Ma2999d122009-08-18 11:43:55 +08006528
Tao Mab2317962010-03-19 15:04:24 +08006529 memset(&ctxt, 0, sizeof(ctxt));
6530 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
Tao Ma2999d122009-08-18 11:43:55 +08006531 if (ret < 0) {
6532 mlog_errno(ret);
6533 return ret;
6534 }
6535
Joel Beckerd3981542009-08-19 02:13:50 -07006536 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6537 if (IS_ERR(ctxt.handle)) {
6538 ret = PTR_ERR(ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006539 mlog_errno(ret);
6540 goto out;
6541 }
6542
6543 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6544 (unsigned long long)fe_bh->b_blocknr, indexed);
Joel Beckerd3981542009-08-19 02:13:50 -07006545 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6546 ret_bh);
Tao Ma2999d122009-08-18 11:43:55 +08006547 if (ret)
6548 mlog_errno(ret);
6549
Joel Beckerd3981542009-08-19 02:13:50 -07006550 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006551out:
Tao Mab2317962010-03-19 15:04:24 +08006552 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma2999d122009-08-18 11:43:55 +08006553 return ret;
6554}
6555
6556static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6557 struct buffer_head *blk_bh,
6558 struct buffer_head *new_blk_bh)
6559{
6560 int ret = 0, credits = 0;
6561 handle_t *handle;
6562 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6563 struct ocfs2_dinode *new_di;
6564 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6565 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6566 struct ocfs2_xattr_block *xb =
6567 (struct ocfs2_xattr_block *)blk_bh->b_data;
6568 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6569 struct ocfs2_xattr_block *new_xb =
6570 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6571 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6572 struct ocfs2_alloc_context *meta_ac;
6573 struct ocfs2_xattr_value_buf vb = {
6574 .vb_bh = new_blk_bh,
6575 .vb_access = ocfs2_journal_access_xb,
6576 };
6577
6578 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6579 &credits, &meta_ac);
6580 if (ret) {
6581 mlog_errno(ret);
6582 return ret;
6583 }
6584
6585 /* One more credits in case we need to add xattr flags in new inode. */
6586 handle = ocfs2_start_trans(osb, credits + 1);
6587 if (IS_ERR(handle)) {
6588 ret = PTR_ERR(handle);
6589 mlog_errno(ret);
6590 goto out;
6591 }
6592
6593 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6594 ret = ocfs2_journal_access_di(handle,
6595 INODE_CACHE(args->new_inode),
6596 args->new_bh,
6597 OCFS2_JOURNAL_ACCESS_WRITE);
6598 if (ret) {
6599 mlog_errno(ret);
6600 goto out_commit;
6601 }
6602 }
6603
6604 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6605 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6606 if (ret) {
6607 mlog_errno(ret);
6608 goto out_commit;
6609 }
6610
6611 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6612 osb->sb->s_blocksize - header_off);
6613
6614 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6615 new_blk_bh, new_xh, &vb, meta_ac,
6616 ocfs2_get_xattr_value_root, NULL);
6617 if (ret) {
6618 mlog_errno(ret);
6619 goto out_commit;
6620 }
6621
6622 ocfs2_journal_dirty(handle, new_blk_bh);
6623
6624 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6625 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6626 spin_lock(&new_oi->ip_lock);
6627 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6628 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6629 spin_unlock(&new_oi->ip_lock);
6630
6631 ocfs2_journal_dirty(handle, args->new_bh);
6632 }
6633
6634out_commit:
6635 ocfs2_commit_trans(osb, handle);
6636
6637out:
6638 ocfs2_free_alloc_context(meta_ac);
6639 return ret;
6640}
6641
6642struct ocfs2_reflink_xattr_tree_args {
6643 struct ocfs2_xattr_reflink *reflink;
6644 struct buffer_head *old_blk_bh;
6645 struct buffer_head *new_blk_bh;
6646 struct ocfs2_xattr_bucket *old_bucket;
6647 struct ocfs2_xattr_bucket *new_bucket;
6648};
6649
6650/*
6651 * NOTE:
6652 * We have to handle the case that both old bucket and new bucket
6653 * will call this function to get the right ret_bh.
6654 * So The caller must give us the right bh.
6655 */
6656static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6657 struct buffer_head *bh,
6658 struct ocfs2_xattr_header *xh,
6659 int offset,
6660 struct ocfs2_xattr_value_root **xv,
6661 struct buffer_head **ret_bh,
6662 void *para)
6663{
6664 struct ocfs2_reflink_xattr_tree_args *args =
6665 (struct ocfs2_reflink_xattr_tree_args *)para;
6666 struct ocfs2_xattr_bucket *bucket;
6667
6668 if (bh == args->old_bucket->bu_bhs[0])
6669 bucket = args->old_bucket;
6670 else
6671 bucket = args->new_bucket;
6672
6673 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6674 xv, ret_bh);
6675}
6676
6677struct ocfs2_value_tree_metas {
6678 int num_metas;
6679 int credits;
6680 int num_recs;
6681};
6682
6683static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6684 struct buffer_head *bh,
6685 struct ocfs2_xattr_header *xh,
6686 int offset,
6687 struct ocfs2_xattr_value_root **xv,
6688 struct buffer_head **ret_bh,
6689 void *para)
6690{
6691 struct ocfs2_xattr_bucket *bucket =
6692 (struct ocfs2_xattr_bucket *)para;
6693
6694 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6695 xv, ret_bh);
6696}
6697
6698static int ocfs2_calc_value_tree_metas(struct inode *inode,
6699 struct ocfs2_xattr_bucket *bucket,
6700 void *para)
6701{
6702 struct ocfs2_value_tree_metas *metas =
6703 (struct ocfs2_value_tree_metas *)para;
6704 struct ocfs2_xattr_header *xh =
6705 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6706
6707 /* Add the credits for this bucket first. */
6708 metas->credits += bucket->bu_blocks;
6709 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6710 xh, &metas->num_metas,
6711 &metas->credits, &metas->num_recs,
6712 ocfs2_value_tree_metas_in_bucket,
6713 bucket);
6714}
6715
6716/*
6717 * Given a xattr extent rec starting from blkno and having len clusters,
6718 * iterate all the buckets calculate how much metadata we need for reflinking
6719 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6720 */
6721static int ocfs2_lock_reflink_xattr_rec_allocators(
6722 struct ocfs2_reflink_xattr_tree_args *args,
6723 struct ocfs2_extent_tree *xt_et,
6724 u64 blkno, u32 len, int *credits,
6725 struct ocfs2_alloc_context **meta_ac,
6726 struct ocfs2_alloc_context **data_ac)
6727{
6728 int ret, num_free_extents;
6729 struct ocfs2_value_tree_metas metas;
6730 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6731 struct ocfs2_refcount_block *rb;
6732
6733 memset(&metas, 0, sizeof(metas));
6734
6735 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6736 ocfs2_calc_value_tree_metas, &metas);
6737 if (ret) {
6738 mlog_errno(ret);
6739 goto out;
6740 }
6741
6742 *credits = metas.credits;
6743
6744 /*
6745 * Calculate we need for refcount tree change.
6746 *
6747 * We need to add/modify num_recs in refcount tree, so just calculate
6748 * an approximate number we need for refcount tree change.
6749 * Sometimes we need to split the tree, and after split, half recs
6750 * will be moved to the new block, and a new block can only provide
6751 * half number of recs. So we multiple new blocks by 2.
6752 * In the end, we have to add credits for modifying the already
6753 * existed refcount block.
6754 */
6755 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6756 metas.num_recs =
6757 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6758 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6759 metas.num_metas += metas.num_recs;
6760 *credits += metas.num_recs +
6761 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6762 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6763 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6764 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6765 else
6766 *credits += 1;
6767
6768 /* count in the xattr tree change. */
6769 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6770 if (num_free_extents < 0) {
6771 ret = num_free_extents;
6772 mlog_errno(ret);
6773 goto out;
6774 }
6775
6776 if (num_free_extents < len)
6777 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6778
6779 *credits += ocfs2_calc_extend_credits(osb->sb,
6780 xt_et->et_root_el, len);
6781
6782 if (metas.num_metas) {
6783 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6784 meta_ac);
6785 if (ret) {
6786 mlog_errno(ret);
6787 goto out;
6788 }
6789 }
6790
6791 if (len) {
6792 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6793 if (ret)
6794 mlog_errno(ret);
6795 }
6796out:
6797 if (ret) {
6798 if (*meta_ac) {
6799 ocfs2_free_alloc_context(*meta_ac);
6800 meta_ac = NULL;
6801 }
6802 }
6803
6804 return ret;
6805}
6806
Tao Ma121a39b2010-07-09 14:53:12 +08006807static int ocfs2_reflink_xattr_bucket(handle_t *handle,
Tao Ma2999d122009-08-18 11:43:55 +08006808 u64 blkno, u64 new_blkno, u32 clusters,
Tao Ma121a39b2010-07-09 14:53:12 +08006809 u32 *cpos, int num_buckets,
Tao Ma2999d122009-08-18 11:43:55 +08006810 struct ocfs2_alloc_context *meta_ac,
6811 struct ocfs2_alloc_context *data_ac,
6812 struct ocfs2_reflink_xattr_tree_args *args)
6813{
6814 int i, j, ret = 0;
6815 struct super_block *sb = args->reflink->old_inode->i_sb;
Tao Ma2999d122009-08-18 11:43:55 +08006816 int bpb = args->old_bucket->bu_blocks;
6817 struct ocfs2_xattr_value_buf vb = {
6818 .vb_access = ocfs2_journal_access,
6819 };
6820
6821 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6822 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6823 if (ret) {
6824 mlog_errno(ret);
6825 break;
6826 }
6827
6828 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6829 if (ret) {
6830 mlog_errno(ret);
6831 break;
6832 }
6833
Tao Ma2999d122009-08-18 11:43:55 +08006834 ret = ocfs2_xattr_bucket_journal_access(handle,
6835 args->new_bucket,
6836 OCFS2_JOURNAL_ACCESS_CREATE);
6837 if (ret) {
6838 mlog_errno(ret);
6839 break;
6840 }
6841
6842 for (j = 0; j < bpb; j++)
6843 memcpy(bucket_block(args->new_bucket, j),
6844 bucket_block(args->old_bucket, j),
6845 sb->s_blocksize);
6846
Tao Ma121a39b2010-07-09 14:53:12 +08006847 /*
6848 * Record the start cpos so that we can use it to initialize
6849 * our xattr tree we also set the xh_num_bucket for the new
6850 * bucket.
6851 */
6852 if (i == 0) {
6853 *cpos = le32_to_cpu(bucket_xh(args->new_bucket)->
6854 xh_entries[0].xe_name_hash);
6855 bucket_xh(args->new_bucket)->xh_num_buckets =
6856 cpu_to_le16(num_buckets);
6857 }
6858
Tao Ma2999d122009-08-18 11:43:55 +08006859 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6860
6861 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6862 args->old_bucket->bu_bhs[0],
6863 bucket_xh(args->old_bucket),
6864 args->new_bucket->bu_bhs[0],
6865 bucket_xh(args->new_bucket),
6866 &vb, meta_ac,
6867 ocfs2_get_reflink_xattr_value_root,
6868 args);
6869 if (ret) {
6870 mlog_errno(ret);
6871 break;
6872 }
6873
6874 /*
6875 * Re-access and dirty the bucket to calculate metaecc.
6876 * Because we may extend the transaction in reflink_xattr_header
6877 * which will let the already accessed block gone.
6878 */
6879 ret = ocfs2_xattr_bucket_journal_access(handle,
6880 args->new_bucket,
6881 OCFS2_JOURNAL_ACCESS_WRITE);
6882 if (ret) {
6883 mlog_errno(ret);
6884 break;
6885 }
6886
6887 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
Tao Ma121a39b2010-07-09 14:53:12 +08006888
Tao Ma2999d122009-08-18 11:43:55 +08006889 ocfs2_xattr_bucket_relse(args->old_bucket);
6890 ocfs2_xattr_bucket_relse(args->new_bucket);
6891 }
6892
6893 ocfs2_xattr_bucket_relse(args->old_bucket);
6894 ocfs2_xattr_bucket_relse(args->new_bucket);
6895 return ret;
6896}
Tao Ma121a39b2010-07-09 14:53:12 +08006897
6898static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6899 struct inode *inode,
6900 struct ocfs2_reflink_xattr_tree_args *args,
6901 struct ocfs2_extent_tree *et,
6902 struct ocfs2_alloc_context *meta_ac,
6903 struct ocfs2_alloc_context *data_ac,
6904 u64 blkno, u32 cpos, u32 len)
6905{
6906 int ret, first_inserted = 0;
6907 u32 p_cluster, num_clusters, reflink_cpos = 0;
6908 u64 new_blkno;
6909 unsigned int num_buckets, reflink_buckets;
6910 unsigned int bpc =
6911 ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
6912
6913 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6914 if (ret) {
6915 mlog_errno(ret);
6916 goto out;
6917 }
6918 num_buckets = le16_to_cpu(bucket_xh(args->old_bucket)->xh_num_buckets);
6919 ocfs2_xattr_bucket_relse(args->old_bucket);
6920
6921 while (len && num_buckets) {
6922 ret = ocfs2_claim_clusters(handle, data_ac,
6923 1, &p_cluster, &num_clusters);
6924 if (ret) {
6925 mlog_errno(ret);
6926 goto out;
6927 }
6928
6929 new_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
6930 reflink_buckets = min(num_buckets, bpc * num_clusters);
6931
6932 ret = ocfs2_reflink_xattr_bucket(handle, blkno,
6933 new_blkno, num_clusters,
6934 &reflink_cpos, reflink_buckets,
6935 meta_ac, data_ac, args);
6936 if (ret) {
6937 mlog_errno(ret);
6938 goto out;
6939 }
6940
6941 /*
6942 * For the 1st allocated cluster, we make it use the same cpos
6943 * so that the xattr tree looks the same as the original one
6944 * in the most case.
6945 */
6946 if (!first_inserted) {
6947 reflink_cpos = cpos;
6948 first_inserted = 1;
6949 }
6950 ret = ocfs2_insert_extent(handle, et, reflink_cpos, new_blkno,
6951 num_clusters, 0, meta_ac);
6952 if (ret)
6953 mlog_errno(ret);
6954
6955 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6956 (unsigned long long)new_blkno, num_clusters, reflink_cpos);
6957
6958 len -= num_clusters;
6959 blkno += ocfs2_clusters_to_blocks(inode->i_sb, num_clusters);
6960 num_buckets -= reflink_buckets;
6961 }
6962out:
6963 return ret;
6964}
6965
Tao Ma2999d122009-08-18 11:43:55 +08006966/*
6967 * Create the same xattr extent record in the new inode's xattr tree.
6968 */
6969static int ocfs2_reflink_xattr_rec(struct inode *inode,
6970 struct buffer_head *root_bh,
6971 u64 blkno,
6972 u32 cpos,
6973 u32 len,
6974 void *para)
6975{
6976 int ret, credits = 0;
Tao Ma2999d122009-08-18 11:43:55 +08006977 handle_t *handle;
6978 struct ocfs2_reflink_xattr_tree_args *args =
6979 (struct ocfs2_reflink_xattr_tree_args *)para;
6980 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6981 struct ocfs2_alloc_context *meta_ac = NULL;
6982 struct ocfs2_alloc_context *data_ac = NULL;
6983 struct ocfs2_extent_tree et;
6984
Tao Ma121a39b2010-07-09 14:53:12 +08006985 mlog(0, "reflink xattr buckets %llu len %u\n",
6986 (unsigned long long)blkno, len);
6987
Tao Ma2999d122009-08-18 11:43:55 +08006988 ocfs2_init_xattr_tree_extent_tree(&et,
6989 INODE_CACHE(args->reflink->new_inode),
6990 args->new_blk_bh);
6991
6992 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6993 len, &credits,
6994 &meta_ac, &data_ac);
6995 if (ret) {
6996 mlog_errno(ret);
6997 goto out;
6998 }
6999
7000 handle = ocfs2_start_trans(osb, credits);
7001 if (IS_ERR(handle)) {
7002 ret = PTR_ERR(handle);
7003 mlog_errno(ret);
7004 goto out;
7005 }
7006
Tao Ma121a39b2010-07-09 14:53:12 +08007007 ret = ocfs2_reflink_xattr_buckets(handle, inode, args, &et,
7008 meta_ac, data_ac,
7009 blkno, cpos, len);
Tao Ma2999d122009-08-18 11:43:55 +08007010 if (ret)
7011 mlog_errno(ret);
7012
Tao Ma2999d122009-08-18 11:43:55 +08007013 ocfs2_commit_trans(osb, handle);
7014
7015out:
7016 if (meta_ac)
7017 ocfs2_free_alloc_context(meta_ac);
7018 if (data_ac)
7019 ocfs2_free_alloc_context(data_ac);
7020 return ret;
7021}
7022
7023/*
7024 * Create reflinked xattr buckets.
7025 * We will add bucket one by one, and refcount all the xattrs in the bucket
7026 * if they are stored outside.
7027 */
7028static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
7029 struct buffer_head *blk_bh,
7030 struct buffer_head *new_blk_bh)
7031{
7032 int ret;
7033 struct ocfs2_reflink_xattr_tree_args para;
7034
7035 memset(&para, 0, sizeof(para));
7036 para.reflink = args;
7037 para.old_blk_bh = blk_bh;
7038 para.new_blk_bh = new_blk_bh;
7039
7040 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
7041 if (!para.old_bucket) {
7042 mlog_errno(-ENOMEM);
7043 return -ENOMEM;
7044 }
7045
7046 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
7047 if (!para.new_bucket) {
7048 ret = -ENOMEM;
7049 mlog_errno(ret);
7050 goto out;
7051 }
7052
7053 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
7054 ocfs2_reflink_xattr_rec,
7055 &para);
7056 if (ret)
7057 mlog_errno(ret);
7058
7059out:
7060 ocfs2_xattr_bucket_free(para.old_bucket);
7061 ocfs2_xattr_bucket_free(para.new_bucket);
7062 return ret;
7063}
7064
7065static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
7066 struct buffer_head *blk_bh)
7067{
7068 int ret, indexed = 0;
7069 struct buffer_head *new_blk_bh = NULL;
7070 struct ocfs2_xattr_block *xb =
7071 (struct ocfs2_xattr_block *)blk_bh->b_data;
7072
7073
7074 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
7075 indexed = 1;
7076
7077 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
7078 &new_blk_bh, indexed);
7079 if (ret) {
7080 mlog_errno(ret);
7081 goto out;
7082 }
7083
7084 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7085 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7086 else
7087 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7088 if (ret)
7089 mlog_errno(ret);
7090
7091out:
7092 brelse(new_blk_bh);
7093 return ret;
7094}
7095
Tao Ma0fe9b662009-08-18 11:47:56 +08007096static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7097{
7098 int type = ocfs2_xattr_get_type(xe);
7099
7100 return type != OCFS2_XATTR_INDEX_SECURITY &&
7101 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7102 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7103}
7104
Tao Ma2999d122009-08-18 11:43:55 +08007105int ocfs2_reflink_xattrs(struct inode *old_inode,
7106 struct buffer_head *old_bh,
7107 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08007108 struct buffer_head *new_bh,
7109 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08007110{
7111 int ret;
7112 struct ocfs2_xattr_reflink args;
7113 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7114 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7115 struct buffer_head *blk_bh = NULL;
7116 struct ocfs2_cached_dealloc_ctxt dealloc;
7117 struct ocfs2_refcount_tree *ref_tree;
7118 struct buffer_head *ref_root_bh = NULL;
7119
7120 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7121 le64_to_cpu(di->i_refcount_loc),
7122 1, &ref_tree, &ref_root_bh);
7123 if (ret) {
7124 mlog_errno(ret);
7125 goto out;
7126 }
7127
7128 ocfs2_init_dealloc_ctxt(&dealloc);
7129
7130 args.old_inode = old_inode;
7131 args.new_inode = new_inode;
7132 args.old_bh = old_bh;
7133 args.new_bh = new_bh;
7134 args.ref_ci = &ref_tree->rf_ci;
7135 args.ref_root_bh = ref_root_bh;
7136 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08007137 if (preserve_security)
7138 args.xattr_reflinked = NULL;
7139 else
7140 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08007141
7142 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7143 ret = ocfs2_reflink_xattr_inline(&args);
7144 if (ret) {
7145 mlog_errno(ret);
7146 goto out_unlock;
7147 }
7148 }
7149
7150 if (!di->i_xattr_loc)
7151 goto out_unlock;
7152
7153 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7154 &blk_bh);
7155 if (ret < 0) {
7156 mlog_errno(ret);
7157 goto out_unlock;
7158 }
7159
7160 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7161 if (ret)
7162 mlog_errno(ret);
7163
7164 brelse(blk_bh);
7165
7166out_unlock:
7167 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7168 ref_tree, 1);
7169 brelse(ref_root_bh);
7170
7171 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7172 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7173 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7174 }
7175
7176out:
7177 return ret;
7178}
7179
7180/*
Tao Ma0fe9b662009-08-18 11:47:56 +08007181 * Initialize security and acl for a already created inode.
7182 * Used for reflink a non-preserve-security file.
7183 *
7184 * It uses common api like ocfs2_xattr_set, so the caller
7185 * must not hold any lock expect i_mutex.
7186 */
7187int ocfs2_init_security_and_acl(struct inode *dir,
7188 struct inode *inode)
7189{
7190 int ret = 0;
7191 struct buffer_head *dir_bh = NULL;
7192 struct ocfs2_security_xattr_info si = {
7193 .enable = 1,
7194 };
7195
7196 ret = ocfs2_init_security_get(inode, dir, &si);
7197 if (!ret) {
Christoph Hellwig431547b2009-11-13 09:52:56 +00007198 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7199 si.name, si.value, si.value_len,
7200 XATTR_CREATE);
Tao Ma0fe9b662009-08-18 11:47:56 +08007201 if (ret) {
7202 mlog_errno(ret);
7203 goto leave;
7204 }
7205 } else if (ret != -EOPNOTSUPP) {
7206 mlog_errno(ret);
7207 goto leave;
7208 }
7209
7210 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7211 if (ret) {
7212 mlog_errno(ret);
7213 goto leave;
7214 }
7215
7216 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7217 if (ret)
7218 mlog_errno(ret);
7219
7220 ocfs2_inode_unlock(dir, 0);
7221 brelse(dir_bh);
7222leave:
7223 return ret;
7224}
7225/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007226 * 'security' attributes support
7227 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007228static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
Tiger Yang923f7f32008-11-14 11:16:27 +08007229 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007230 size_t name_len, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007231{
7232 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7233 const size_t total_len = prefix_len + name_len + 1;
7234
7235 if (list && total_len <= list_size) {
7236 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7237 memcpy(list + prefix_len, name, name_len);
7238 list[prefix_len + name_len] = '\0';
7239 }
7240 return total_len;
7241}
7242
Christoph Hellwig431547b2009-11-13 09:52:56 +00007243static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7244 void *buffer, size_t size, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007245{
7246 if (strcmp(name, "") == 0)
7247 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007248 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7249 name, buffer, size);
Tiger Yang923f7f32008-11-14 11:16:27 +08007250}
7251
Christoph Hellwig431547b2009-11-13 09:52:56 +00007252static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7253 const void *value, size_t size, int flags, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007254{
7255 if (strcmp(name, "") == 0)
7256 return -EINVAL;
7257
Christoph Hellwig431547b2009-11-13 09:52:56 +00007258 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7259 name, value, size, flags);
Tiger Yang923f7f32008-11-14 11:16:27 +08007260}
7261
Tiger Yang534eadd2008-11-14 11:16:41 +08007262int ocfs2_init_security_get(struct inode *inode,
7263 struct inode *dir,
7264 struct ocfs2_security_xattr_info *si)
7265{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007266 /* check whether ocfs2 support feature xattr */
7267 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7268 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08007269 return security_inode_init_security(inode, dir, &si->name, &si->value,
7270 &si->value_len);
7271}
7272
7273int ocfs2_init_security_set(handle_t *handle,
7274 struct inode *inode,
7275 struct buffer_head *di_bh,
7276 struct ocfs2_security_xattr_info *si,
7277 struct ocfs2_alloc_context *xattr_ac,
7278 struct ocfs2_alloc_context *data_ac)
7279{
7280 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7281 OCFS2_XATTR_INDEX_SECURITY,
7282 si->name, si->value, si->value_len, 0,
7283 xattr_ac, data_ac);
7284}
7285
Stephen Hemminger537d81c2010-05-13 17:53:22 -07007286const struct xattr_handler ocfs2_xattr_security_handler = {
Tiger Yang923f7f32008-11-14 11:16:27 +08007287 .prefix = XATTR_SECURITY_PREFIX,
7288 .list = ocfs2_xattr_security_list,
7289 .get = ocfs2_xattr_security_get,
7290 .set = ocfs2_xattr_security_set,
7291};
7292
7293/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007294 * 'trusted' attributes support
7295 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007296static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007297 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007298 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007299{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007300 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007301 const size_t total_len = prefix_len + name_len + 1;
7302
7303 if (list && total_len <= list_size) {
7304 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7305 memcpy(list + prefix_len, name, name_len);
7306 list[prefix_len + name_len] = '\0';
7307 }
7308 return total_len;
7309}
7310
Christoph Hellwig431547b2009-11-13 09:52:56 +00007311static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7312 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007313{
7314 if (strcmp(name, "") == 0)
7315 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007316 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7317 name, buffer, size);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007318}
7319
Christoph Hellwig431547b2009-11-13 09:52:56 +00007320static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7321 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007322{
7323 if (strcmp(name, "") == 0)
7324 return -EINVAL;
7325
Christoph Hellwig431547b2009-11-13 09:52:56 +00007326 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7327 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007328}
7329
Stephen Hemminger537d81c2010-05-13 17:53:22 -07007330const struct xattr_handler ocfs2_xattr_trusted_handler = {
Mark Fasheh99219ae2008-10-07 14:52:59 -07007331 .prefix = XATTR_TRUSTED_PREFIX,
7332 .list = ocfs2_xattr_trusted_list,
7333 .get = ocfs2_xattr_trusted_get,
7334 .set = ocfs2_xattr_trusted_set,
7335};
7336
Mark Fasheh99219ae2008-10-07 14:52:59 -07007337/*
7338 * 'user' attributes support
7339 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007340static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007341 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007342 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007343{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007344 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007345 const size_t total_len = prefix_len + name_len + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007346 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007347
7348 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7349 return 0;
7350
7351 if (list && total_len <= list_size) {
7352 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7353 memcpy(list + prefix_len, name, name_len);
7354 list[prefix_len + name_len] = '\0';
7355 }
7356 return total_len;
7357}
7358
Christoph Hellwig431547b2009-11-13 09:52:56 +00007359static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7360 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007361{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007362 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007363
7364 if (strcmp(name, "") == 0)
7365 return -EINVAL;
7366 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7367 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007368 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007369 buffer, size);
7370}
7371
Christoph Hellwig431547b2009-11-13 09:52:56 +00007372static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7373 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007374{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007375 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007376
7377 if (strcmp(name, "") == 0)
7378 return -EINVAL;
7379 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7380 return -EOPNOTSUPP;
7381
Christoph Hellwig431547b2009-11-13 09:52:56 +00007382 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7383 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007384}
7385
Stephen Hemminger537d81c2010-05-13 17:53:22 -07007386const struct xattr_handler ocfs2_xattr_user_handler = {
Mark Fasheh99219ae2008-10-07 14:52:59 -07007387 .prefix = XATTR_USER_PREFIX,
7388 .list = ocfs2_xattr_user_list,
7389 .get = ocfs2_xattr_user_get,
7390 .set = ocfs2_xattr_user_set,
7391};