blob: a2d912a92dd78a52dfe97f73706033449b3cf7af [file] [log] [blame]
Tao Maf56654c2008-08-18 17:38:48 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
Tiger Yangc3cb6822008-10-23 16:33:03 +08006 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
Tao Maf56654c2008-08-18 17:38:48 +08007 *
Tiger Yangcf1d6c72008-08-18 17:11:00 +08008 * CREDITS:
Tiger Yangc3cb6822008-10-23 16:33:03 +08009 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080011 *
Tao Maf56654c2008-08-18 17:38:48 +080012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
Tiger Yangc3cb6822008-10-23 16:33:03 +080014 * License version 2 as published by the Free Software Foundation.
Tao Maf56654c2008-08-18 17:38:48 +080015 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
Tao Maf56654c2008-08-18 17:38:48 +080020 */
21
Tiger Yangcf1d6c72008-08-18 17:11:00 +080022#include <linux/capability.h>
23#include <linux/fs.h>
24#include <linux/types.h>
25#include <linux/slab.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h>
28#include <linux/uio.h>
29#include <linux/sched.h>
30#include <linux/splice.h>
31#include <linux/mount.h>
32#include <linux/writeback.h>
33#include <linux/falloc.h>
Tao Ma01225592008-08-18 17:38:53 +080034#include <linux/sort.h>
Mark Fasheh99219ae2008-10-07 14:52:59 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
Tiger Yang923f7f32008-11-14 11:16:27 +080038#include <linux/security.h>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080039
Tao Maf56654c2008-08-18 17:38:48 +080040#define MLOG_MASK_PREFIX ML_XATTR
41#include <cluster/masklog.h>
42
43#include "ocfs2.h"
44#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070045#include "blockcheck.h"
Tao Maf56654c2008-08-18 17:38:48 +080046#include "dlmglue.h"
47#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080048#include "symlink.h"
49#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080050#include "inode.h"
51#include "journal.h"
52#include "ocfs2_fs.h"
53#include "suballoc.h"
54#include "uptodate.h"
55#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080056#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080057#include "xattr.h"
Tao Ma492a8a32009-08-18 11:43:17 +080058#include "refcounttree.h"
Tao Ma0fe9b662009-08-18 11:47:56 +080059#include "acl.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080060
61struct ocfs2_xattr_def_value_root {
62 struct ocfs2_xattr_value_root xv;
63 struct ocfs2_extent_rec er;
64};
65
Tao Ma0c044f02008-08-18 17:38:50 +080066struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070067 /* The inode these xattrs are associated with */
68 struct inode *bu_inode;
69
70 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070071 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070072
73 /* How many blocks make up one bucket for this filesystem */
74 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080075};
76
Tao Ma78f30c32008-11-12 08:27:00 +080077struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080078 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080079 struct ocfs2_alloc_context *meta_ac;
80 struct ocfs2_alloc_context *data_ac;
81 struct ocfs2_cached_dealloc_ctxt dealloc;
82};
83
Tiger Yangcf1d6c72008-08-18 17:11:00 +080084#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
85#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang4442f512009-02-20 11:11:50 +080086#define OCFS2_XATTR_HEADER_GAP 4
Tiger Yang534eadd2008-11-14 11:16:41 +080087#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
88 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080089 - OCFS2_XATTR_HEADER_GAP)
Tiger Yang89c38bd2008-11-14 11:17:41 +080090#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
91 - sizeof(struct ocfs2_xattr_block) \
92 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080093 - OCFS2_XATTR_HEADER_GAP)
Tiger Yangcf1d6c72008-08-18 17:11:00 +080094
95static struct ocfs2_xattr_def_value_root def_xv = {
96 .xv.xr_list.l_count = cpu_to_le16(1),
97};
98
99struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800103 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800104 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800105 NULL
106};
107
Tiger Yangc988fd02008-10-23 16:34:44 +0800108static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800116};
117
118struct ocfs2_xattr_info {
Joel Becker6b240ff2009-08-14 18:02:52 -0700119 int xi_name_index;
120 const char *xi_name;
Joel Becker18853b92009-08-14 18:17:07 -0700121 int xi_name_len;
Joel Becker6b240ff2009-08-14 18:02:52 -0700122 const void *xi_value;
123 size_t xi_value_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800124};
125
126struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
128 /*
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
131 */
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700134 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800135 void *base;
136 void *end;
137 struct ocfs2_xattr_entry *here;
138 int not_found;
139};
140
Joel Becker11179f22009-08-14 16:07:44 -0700141/* Operations on struct ocfs2_xa_entry */
142struct ocfs2_xa_loc;
143struct ocfs2_xa_loc_operations {
144 /*
Joel Beckercf2bc802009-08-18 13:52:38 -0700145 * Journal functions
146 */
147 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
148 int type);
149 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
150
151 /*
Joel Becker11179f22009-08-14 16:07:44 -0700152 * Return a pointer to the appropriate buffer in loc->xl_storage
153 * at the given offset from loc->xl_header.
154 */
155 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
156
Joel Becker69a3e532009-08-17 12:24:39 -0700157 /* Can we reuse the existing entry for the new value? */
158 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
159 struct ocfs2_xattr_info *xi);
160
161 /* How much space is needed for the new value? */
162 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
163 struct ocfs2_xattr_info *xi);
164
165 /*
166 * Return the offset of the first name+value pair. This is
167 * the start of our downward-filling free space.
168 */
169 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
170
Joel Becker11179f22009-08-14 16:07:44 -0700171 /*
172 * Remove the name+value at this location. Do whatever is
173 * appropriate with the remaining name+value pairs.
174 */
175 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
Joel Becker69a3e532009-08-17 12:24:39 -0700176
177 /* Fill xl_entry with a new entry */
178 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
179
180 /* Add name+value storage to an entry */
181 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
Joel Becker3fc12af2009-08-18 13:20:27 -0700182
183 /*
184 * Initialize the value buf's access and bh fields for this entry.
185 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
186 */
187 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
188 struct ocfs2_xattr_value_buf *vb);
Joel Becker11179f22009-08-14 16:07:44 -0700189};
190
191/*
192 * Describes an xattr entry location. This is a memory structure
193 * tracking the on-disk structure.
194 */
195struct ocfs2_xa_loc {
Joel Beckercf2bc802009-08-18 13:52:38 -0700196 /* This xattr belongs to this inode */
197 struct inode *xl_inode;
198
Joel Becker11179f22009-08-14 16:07:44 -0700199 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200 struct ocfs2_xattr_header *xl_header;
201
202 /* Bytes from xl_header to the end of the storage */
203 int xl_size;
204
205 /*
206 * The ocfs2_xattr_entry this location describes. If this is
207 * NULL, this location describes the on-disk structure where it
208 * would have been.
209 */
210 struct ocfs2_xattr_entry *xl_entry;
211
212 /*
213 * Internal housekeeping
214 */
215
216 /* Buffer(s) containing this entry */
217 void *xl_storage;
218
219 /* Operations on the storage backing this location */
220 const struct ocfs2_xa_loc_operations *xl_ops;
221};
222
Joel Becker199799a2009-08-14 19:04:15 -0700223/*
224 * Convenience functions to calculate how much space is needed for a
225 * given name+value pair
226 */
227static int namevalue_size(int name_len, uint64_t value_len)
228{
229 if (value_len > OCFS2_XATTR_INLINE_SIZE)
230 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
231 else
232 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
233}
234
235static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
236{
237 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
238}
239
240static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
241{
242 u64 value_len = le64_to_cpu(xe->xe_value_size);
243
244 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
245 ocfs2_xattr_is_local(xe));
246 return namevalue_size(xe->xe_name_len, value_len);
247}
248
249
Tao Mafd68a892009-08-18 11:43:21 +0800250static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800251 struct ocfs2_xattr_header *xh,
252 int index,
253 int *block_off,
254 int *new_offset);
255
Joel Becker54f443f2008-10-20 18:43:07 -0700256static int ocfs2_xattr_block_find(struct inode *inode,
257 int name_index,
258 const char *name,
259 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800260static int ocfs2_xattr_index_block_find(struct inode *inode,
261 struct buffer_head *root_bh,
262 int name_index,
263 const char *name,
264 struct ocfs2_xattr_search *xs);
265
Tao Ma0c044f02008-08-18 17:38:50 +0800266static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
Tao Ma47bca492009-08-18 11:43:42 +0800267 struct buffer_head *blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800268 char *buffer,
269 size_t buffer_size);
270
Tao Ma01225592008-08-18 17:38:53 +0800271static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800272 struct ocfs2_xattr_search *xs,
273 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800274
275static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
276 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800277 struct ocfs2_xattr_search *xs,
278 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800279
Tao Ma47bca492009-08-18 11:43:42 +0800280typedef int (xattr_tree_rec_func)(struct inode *inode,
281 struct buffer_head *root_bh,
282 u64 blkno, u32 cpos, u32 len, void *para);
283static int ocfs2_iterate_xattr_index_block(struct inode *inode,
284 struct buffer_head *root_bh,
285 xattr_tree_rec_func *rec_func,
286 void *para);
287static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
288 struct ocfs2_xattr_bucket *bucket,
289 void *para);
290static int ocfs2_rm_xattr_cluster(struct inode *inode,
291 struct buffer_head *root_bh,
292 u64 blkno,
293 u32 cpos,
294 u32 len,
295 void *para);
296
Joel Beckerc58b6032008-11-26 13:36:24 -0800297static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
298 u64 src_blk, u64 last_blk, u64 to_blk,
299 unsigned int start_bucket,
300 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800301static int ocfs2_prepare_refcount_xattr(struct inode *inode,
302 struct ocfs2_dinode *di,
303 struct ocfs2_xattr_info *xi,
304 struct ocfs2_xattr_search *xis,
305 struct ocfs2_xattr_search *xbs,
306 struct ocfs2_refcount_tree **ref_tree,
307 int *meta_need,
308 int *credits);
Tao Mace9c5a52009-08-18 11:43:59 +0800309static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
310 struct ocfs2_xattr_bucket *bucket,
311 int offset,
312 struct ocfs2_xattr_value_root **xv,
313 struct buffer_head **bh);
Tao Maa3944252008-08-18 17:38:54 +0800314
Tiger Yang0030e002008-10-23 16:33:33 +0800315static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
316{
317 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
318}
319
320static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
321{
322 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
323}
324
Joel Becker9c7759a2008-10-24 16:21:03 -0700325#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700326#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700327#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700328
Joel Beckerba937122008-10-24 19:13:20 -0700329static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700330{
Joel Beckerba937122008-10-24 19:13:20 -0700331 struct ocfs2_xattr_bucket *bucket;
332 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700333
Joel Beckerba937122008-10-24 19:13:20 -0700334 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
335
336 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
337 if (bucket) {
338 bucket->bu_inode = inode;
339 bucket->bu_blocks = blks;
340 }
341
342 return bucket;
343}
344
345static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
346{
347 int i;
348
349 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700350 brelse(bucket->bu_bhs[i]);
351 bucket->bu_bhs[i] = NULL;
352 }
353}
354
Joel Beckerba937122008-10-24 19:13:20 -0700355static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
356{
357 if (bucket) {
358 ocfs2_xattr_bucket_relse(bucket);
359 bucket->bu_inode = NULL;
360 kfree(bucket);
361 }
362}
363
Joel Becker784b8162008-10-24 17:33:40 -0700364/*
365 * A bucket that has never been written to disk doesn't need to be
366 * read. We just need the buffer_heads. Don't call this for
367 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
368 * them fully.
369 */
Joel Beckerba937122008-10-24 19:13:20 -0700370static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700371 u64 xb_blkno)
372{
373 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700374
Joel Beckerba937122008-10-24 19:13:20 -0700375 for (i = 0; i < bucket->bu_blocks; i++) {
376 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
377 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700378 if (!bucket->bu_bhs[i]) {
379 rc = -EIO;
380 mlog_errno(rc);
381 break;
382 }
383
Joel Becker8cb471e2009-02-10 20:00:41 -0800384 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055ad2008-11-06 08:10:48 +0800385 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800386 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055ad2008-11-06 08:10:48 +0800387 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700388 }
389
390 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700391 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700392 return rc;
393}
394
395/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700396static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700397 u64 xb_blkno)
398{
Joel Beckerba937122008-10-24 19:13:20 -0700399 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700400
Joel Becker8cb471e2009-02-10 20:00:41 -0800401 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800402 bucket->bu_blocks, bucket->bu_bhs, 0,
403 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800404 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800405 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800406 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
407 bucket->bu_bhs,
408 bucket->bu_blocks,
409 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800410 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800411 if (rc)
412 mlog_errno(rc);
413 }
414
Joel Becker784b8162008-10-24 17:33:40 -0700415 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700416 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700417 return rc;
418}
419
Joel Becker1224be02008-10-24 18:47:33 -0700420static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700421 struct ocfs2_xattr_bucket *bucket,
422 int type)
423{
424 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700425
Joel Beckerba937122008-10-24 19:13:20 -0700426 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800427 rc = ocfs2_journal_access(handle,
428 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700429 bucket->bu_bhs[i], type);
430 if (rc) {
431 mlog_errno(rc);
432 break;
433 }
434 }
435
436 return rc;
437}
438
439static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700440 struct ocfs2_xattr_bucket *bucket)
441{
Joel Beckerba937122008-10-24 19:13:20 -0700442 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700443
Tao Mac8b9cf92009-02-24 17:40:26 -0800444 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800445 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
446 bucket->bu_bhs, bucket->bu_blocks,
447 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800448 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800449
Joel Beckerba937122008-10-24 19:13:20 -0700450 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700451 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
452}
453
Joel Beckerba937122008-10-24 19:13:20 -0700454static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700455 struct ocfs2_xattr_bucket *src)
456{
457 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700458 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700459
Joel Beckerba937122008-10-24 19:13:20 -0700460 BUG_ON(dest->bu_blocks != src->bu_blocks);
461 BUG_ON(dest->bu_inode != src->bu_inode);
462
463 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700464 memcpy(bucket_block(dest, i), bucket_block(src, i),
465 blocksize);
466 }
467}
Joel Becker1224be02008-10-24 18:47:33 -0700468
Joel Becker4ae1d692008-11-13 14:49:18 -0800469static int ocfs2_validate_xattr_block(struct super_block *sb,
470 struct buffer_head *bh)
471{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700472 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800473 struct ocfs2_xattr_block *xb =
474 (struct ocfs2_xattr_block *)bh->b_data;
475
476 mlog(0, "Validating xattr block %llu\n",
477 (unsigned long long)bh->b_blocknr);
478
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700479 BUG_ON(!buffer_uptodate(bh));
480
481 /*
482 * If the ecc fails, we return the error but otherwise
483 * leave the filesystem running. We know any error is
484 * local to this block.
485 */
486 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
487 if (rc)
488 return rc;
489
490 /*
491 * Errors after here are fatal
492 */
493
Joel Becker4ae1d692008-11-13 14:49:18 -0800494 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
495 ocfs2_error(sb,
496 "Extended attribute block #%llu has bad "
497 "signature %.*s",
498 (unsigned long long)bh->b_blocknr, 7,
499 xb->xb_signature);
500 return -EINVAL;
501 }
502
503 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
504 ocfs2_error(sb,
505 "Extended attribute block #%llu has an "
506 "invalid xb_blkno of %llu",
507 (unsigned long long)bh->b_blocknr,
508 (unsigned long long)le64_to_cpu(xb->xb_blkno));
509 return -EINVAL;
510 }
511
512 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
513 ocfs2_error(sb,
514 "Extended attribute block #%llu has an invalid "
515 "xb_fs_generation of #%u",
516 (unsigned long long)bh->b_blocknr,
517 le32_to_cpu(xb->xb_fs_generation));
518 return -EINVAL;
519 }
520
521 return 0;
522}
523
524static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
525 struct buffer_head **bh)
526{
527 int rc;
528 struct buffer_head *tmp = *bh;
529
Joel Becker8cb471e2009-02-10 20:00:41 -0800530 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800531 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800532
533 /* If ocfs2_read_block() got us a new bh, pass it up. */
534 if (!rc && !*bh)
535 *bh = tmp;
536
537 return rc;
538}
539
Tao Ma936b8832008-10-09 23:06:14 +0800540static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800541{
542 struct xattr_handler *handler = NULL;
543
544 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
545 handler = ocfs2_xattr_handler_map[name_index];
546
Tao Ma936b8832008-10-09 23:06:14 +0800547 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800548}
549
Mark Fasheh40daa162008-10-07 14:31:42 -0700550static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800551 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700552 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800553{
554 /* Get hash value of uuid from super block */
555 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
556 int i;
557
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800558 /* hash extended attribute name */
559 for (i = 0; i < name_len; i++) {
560 hash = (hash << OCFS2_HASH_SHIFT) ^
561 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
562 *name++;
563 }
564
565 return hash;
566}
567
Tiger Yang534eadd2008-11-14 11:16:41 +0800568static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
569{
Joel Becker199799a2009-08-14 19:04:15 -0700570 return namevalue_size(name_len, value_len) +
571 sizeof(struct ocfs2_xattr_entry);
572}
Tiger Yang534eadd2008-11-14 11:16:41 +0800573
Joel Becker199799a2009-08-14 19:04:15 -0700574static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
575{
576 return namevalue_size_xi(xi) +
577 sizeof(struct ocfs2_xattr_entry);
578}
Tiger Yang534eadd2008-11-14 11:16:41 +0800579
Joel Becker199799a2009-08-14 19:04:15 -0700580static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
581{
582 return namevalue_size_xe(xe) +
583 sizeof(struct ocfs2_xattr_entry);
Tiger Yang534eadd2008-11-14 11:16:41 +0800584}
585
586int ocfs2_calc_security_init(struct inode *dir,
587 struct ocfs2_security_xattr_info *si,
588 int *want_clusters,
589 int *xattr_credits,
590 struct ocfs2_alloc_context **xattr_ac)
591{
592 int ret = 0;
593 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
594 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
595 si->value_len);
596
597 /*
598 * The max space of security xattr taken inline is
599 * 256(name) + 80(value) + 16(entry) = 352 bytes,
600 * So reserve one metadata block for it is ok.
601 */
602 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
603 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
604 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
605 if (ret) {
606 mlog_errno(ret);
607 return ret;
608 }
609 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
610 }
611
612 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800613 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
614 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
615 si->value_len);
616
617 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
618 new_clusters);
619 *want_clusters += new_clusters;
620 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800621 return ret;
622}
623
Tiger Yang89c38bd2008-11-14 11:17:41 +0800624int ocfs2_calc_xattr_init(struct inode *dir,
625 struct buffer_head *dir_bh,
626 int mode,
627 struct ocfs2_security_xattr_info *si,
628 int *want_clusters,
629 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800630 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800631{
632 int ret = 0;
633 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800634 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800635
636 if (si->enable)
637 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
638 si->value_len);
639
640 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
641 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
642 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
643 "", NULL, 0);
644 if (acl_len > 0) {
645 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
646 if (S_ISDIR(mode))
647 a_size <<= 1;
648 } else if (acl_len != 0 && acl_len != -ENODATA) {
649 mlog_errno(ret);
650 return ret;
651 }
652 }
653
654 if (!(s_size + a_size))
655 return ret;
656
657 /*
658 * The max space of security xattr taken inline is
659 * 256(name) + 80(value) + 16(entry) = 352 bytes,
660 * The max space of acl xattr taken inline is
661 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
662 * when blocksize = 512, may reserve one more cluser for
663 * xattr bucket, otherwise reserve one metadata block
664 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800665 * If this is a new directory with inline data,
666 * we choose to reserve the entire inline area for
667 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800668 */
669 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800670 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800671 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800672 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800673 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
674 }
675
676 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
677 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
678 *want_clusters += 1;
679 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
680 }
681
Tiger Yang0e445b62008-12-09 16:42:51 +0800682 /*
683 * reserve credits and clusters for xattrs which has large value
684 * and have to be set outside
685 */
686 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
687 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
688 si->value_len);
689 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
690 new_clusters);
691 *want_clusters += new_clusters;
692 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800693 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
694 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800695 /* for directory, it has DEFAULT and ACCESS two types of acls */
696 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
697 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
698 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
699 new_clusters);
700 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800701 }
702
703 return ret;
704}
705
Tao Maf56654c2008-08-18 17:38:48 +0800706static int ocfs2_xattr_extend_allocation(struct inode *inode,
707 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800708 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800709 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800710{
711 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800712 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800713 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800714 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700715 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800716
717 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
718
Joel Becker5e404e92009-02-13 03:54:22 -0800719 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700720
Joel Becker0cf2f762009-02-12 16:41:25 -0800721 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800722 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800723 if (status < 0) {
724 mlog_errno(status);
725 goto leave;
726 }
727
Joel Becker19b801f2008-12-09 14:36:50 -0800728 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800729 status = ocfs2_add_clusters_in_btree(handle,
730 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800731 &logical_start,
732 clusters_to_add,
733 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800734 ctxt->data_ac,
735 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700736 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800737 if (status < 0) {
738 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800739 goto leave;
740 }
741
Joel Becker19b801f2008-12-09 14:36:50 -0800742 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800743 if (status < 0) {
744 mlog_errno(status);
745 goto leave;
746 }
747
Joel Becker19b801f2008-12-09 14:36:50 -0800748 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800749
Tao Ma85db90e2008-11-12 08:27:01 +0800750 /*
751 * We should have already allocated enough space before the transaction,
752 * so no need to restart.
753 */
754 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800755
756leave:
Tao Maf56654c2008-08-18 17:38:48 +0800757
758 return status;
759}
760
761static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800762 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800763 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800764 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800765 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800766{
767 int ret;
768 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800769 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700770 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800771
Joel Becker5e404e92009-02-13 03:54:22 -0800772 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700773
Joel Becker0cf2f762009-02-12 16:41:25 -0800774 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800775 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800776 if (ret) {
777 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800778 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800779 }
780
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800781 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800782 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800783 if (ret) {
784 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800785 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800786 }
787
Joel Beckerd72cc722008-12-09 14:30:41 -0800788 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800789
Joel Beckerd72cc722008-12-09 14:30:41 -0800790 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800791 if (ret) {
792 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800793 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800794 }
795
Tao Ma492a8a32009-08-18 11:43:17 +0800796 if (ext_flags & OCFS2_EXT_REFCOUNTED)
797 ret = ocfs2_decrease_refcount(inode, handle,
798 ocfs2_blocks_to_clusters(inode->i_sb,
799 phys_blkno),
800 len, ctxt->meta_ac, &ctxt->dealloc, 1);
801 else
802 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
803 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800804 if (ret)
805 mlog_errno(ret);
806
Tao Maf56654c2008-08-18 17:38:48 +0800807out:
Tao Maf56654c2008-08-18 17:38:48 +0800808 return ret;
809}
810
811static int ocfs2_xattr_shrink_size(struct inode *inode,
812 u32 old_clusters,
813 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800814 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800815 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800816{
817 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800818 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800819 u32 trunc_len, cpos, phys_cpos, alloc_size;
820 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800821
822 if (old_clusters <= new_clusters)
823 return 0;
824
825 cpos = new_clusters;
826 trunc_len = old_clusters - new_clusters;
827 while (trunc_len) {
828 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800829 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800830 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800831 if (ret) {
832 mlog_errno(ret);
833 goto out;
834 }
835
836 if (alloc_size > trunc_len)
837 alloc_size = trunc_len;
838
Joel Becker19b801f2008-12-09 14:36:50 -0800839 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800840 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800841 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800842 if (ret) {
843 mlog_errno(ret);
844 goto out;
845 }
846
847 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800848 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
849 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800850 cpos += alloc_size;
851 trunc_len -= alloc_size;
852 }
853
854out:
Tao Maf56654c2008-08-18 17:38:48 +0800855 return ret;
856}
857
858static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800859 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800860 int len,
861 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800862{
863 int ret;
864 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800865 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800866
867 if (new_clusters == old_clusters)
868 return 0;
869
870 if (new_clusters > old_clusters)
871 ret = ocfs2_xattr_extend_allocation(inode,
872 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800873 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800874 else
875 ret = ocfs2_xattr_shrink_size(inode,
876 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800877 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800878
879 return ret;
880}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800881
Tao Ma936b8832008-10-09 23:06:14 +0800882static int ocfs2_xattr_list_entry(char *buffer, size_t size,
883 size_t *result, const char *prefix,
884 const char *name, int name_len)
885{
886 char *p = buffer + *result;
887 int prefix_len = strlen(prefix);
888 int total_len = prefix_len + name_len + 1;
889
890 *result += total_len;
891
892 /* we are just looking for how big our buffer needs to be */
893 if (!size)
894 return 0;
895
896 if (*result > size)
897 return -ERANGE;
898
899 memcpy(p, prefix, prefix_len);
900 memcpy(p + prefix_len, name, name_len);
901 p[prefix_len + name_len] = '\0';
902
903 return 0;
904}
905
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800906static int ocfs2_xattr_list_entries(struct inode *inode,
907 struct ocfs2_xattr_header *header,
908 char *buffer, size_t buffer_size)
909{
Tao Ma936b8832008-10-09 23:06:14 +0800910 size_t result = 0;
911 int i, type, ret;
912 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800913
914 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
915 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800916 type = ocfs2_xattr_get_type(entry);
917 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800918
Tao Ma936b8832008-10-09 23:06:14 +0800919 if (prefix) {
920 name = (const char *)header +
921 le16_to_cpu(entry->xe_name_offset);
922
923 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
924 &result, prefix, name,
925 entry->xe_name_len);
926 if (ret)
927 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800928 }
929 }
930
Tao Ma936b8832008-10-09 23:06:14 +0800931 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800932}
933
Tao Ma8b2c0db2009-08-18 11:43:49 +0800934int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
935 struct ocfs2_dinode *di)
936{
937 struct ocfs2_xattr_header *xh;
938 int i;
939
940 xh = (struct ocfs2_xattr_header *)
941 ((void *)di + inode->i_sb->s_blocksize -
942 le16_to_cpu(di->i_xattr_inline_size));
943
944 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
945 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
946 return 1;
947
948 return 0;
949}
950
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800951static int ocfs2_xattr_ibody_list(struct inode *inode,
952 struct ocfs2_dinode *di,
953 char *buffer,
954 size_t buffer_size)
955{
956 struct ocfs2_xattr_header *header = NULL;
957 struct ocfs2_inode_info *oi = OCFS2_I(inode);
958 int ret = 0;
959
960 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
961 return ret;
962
963 header = (struct ocfs2_xattr_header *)
964 ((void *)di + inode->i_sb->s_blocksize -
965 le16_to_cpu(di->i_xattr_inline_size));
966
967 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
968
969 return ret;
970}
971
972static int ocfs2_xattr_block_list(struct inode *inode,
973 struct ocfs2_dinode *di,
974 char *buffer,
975 size_t buffer_size)
976{
977 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800978 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800979 int ret = 0;
980
981 if (!di->i_xattr_loc)
982 return ret;
983
Joel Becker4ae1d692008-11-13 14:49:18 -0800984 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
985 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800986 if (ret < 0) {
987 mlog_errno(ret);
988 return ret;
989 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800990
Tao Ma0c044f02008-08-18 17:38:50 +0800991 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800992 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
993 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
994 ret = ocfs2_xattr_list_entries(inode, header,
995 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +0800996 } else
997 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800998 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -0800999
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001000 brelse(blk_bh);
1001
1002 return ret;
1003}
1004
1005ssize_t ocfs2_listxattr(struct dentry *dentry,
1006 char *buffer,
1007 size_t size)
1008{
1009 int ret = 0, i_ret = 0, b_ret = 0;
1010 struct buffer_head *di_bh = NULL;
1011 struct ocfs2_dinode *di = NULL;
1012 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1013
Tiger Yang8154da32008-08-18 17:11:46 +08001014 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1015 return -EOPNOTSUPP;
1016
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001017 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1018 return ret;
1019
1020 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1021 if (ret < 0) {
1022 mlog_errno(ret);
1023 return ret;
1024 }
1025
1026 di = (struct ocfs2_dinode *)di_bh->b_data;
1027
1028 down_read(&oi->ip_xattr_sem);
1029 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1030 if (i_ret < 0)
1031 b_ret = 0;
1032 else {
1033 if (buffer) {
1034 buffer += i_ret;
1035 size -= i_ret;
1036 }
1037 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1038 buffer, size);
1039 if (b_ret < 0)
1040 i_ret = 0;
1041 }
1042 up_read(&oi->ip_xattr_sem);
1043 ocfs2_inode_unlock(dentry->d_inode, 0);
1044
1045 brelse(di_bh);
1046
1047 return i_ret + b_ret;
1048}
1049
1050static int ocfs2_xattr_find_entry(int name_index,
1051 const char *name,
1052 struct ocfs2_xattr_search *xs)
1053{
1054 struct ocfs2_xattr_entry *entry;
1055 size_t name_len;
1056 int i, cmp = 1;
1057
1058 if (name == NULL)
1059 return -EINVAL;
1060
1061 name_len = strlen(name);
1062 entry = xs->here;
1063 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1064 cmp = name_index - ocfs2_xattr_get_type(entry);
1065 if (!cmp)
1066 cmp = name_len - entry->xe_name_len;
1067 if (!cmp)
1068 cmp = memcmp(name, (xs->base +
1069 le16_to_cpu(entry->xe_name_offset)),
1070 name_len);
1071 if (cmp == 0)
1072 break;
1073 entry += 1;
1074 }
1075 xs->here = entry;
1076
1077 return cmp ? -ENODATA : 0;
1078}
1079
1080static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +08001081 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001082 void *buffer,
1083 size_t len)
1084{
1085 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1086 u64 blkno;
1087 int i, ret = 0;
1088 size_t cplen, blocksize;
1089 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001090 struct ocfs2_extent_list *el;
1091
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001092 el = &xv->xr_list;
1093 clusters = le32_to_cpu(xv->xr_clusters);
1094 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1095 blocksize = inode->i_sb->s_blocksize;
1096
1097 cpos = 0;
1098 while (cpos < clusters) {
1099 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001100 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001101 if (ret) {
1102 mlog_errno(ret);
1103 goto out;
1104 }
1105
1106 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1107 /* Copy ocfs2_xattr_value */
1108 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001109 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1110 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001111 if (ret) {
1112 mlog_errno(ret);
1113 goto out;
1114 }
1115
1116 cplen = len >= blocksize ? blocksize : len;
1117 memcpy(buffer, bh->b_data, cplen);
1118 len -= cplen;
1119 buffer += cplen;
1120
1121 brelse(bh);
1122 bh = NULL;
1123 if (len == 0)
1124 break;
1125 }
1126 cpos += num_clusters;
1127 }
1128out:
1129 return ret;
1130}
1131
1132static int ocfs2_xattr_ibody_get(struct inode *inode,
1133 int name_index,
1134 const char *name,
1135 void *buffer,
1136 size_t buffer_size,
1137 struct ocfs2_xattr_search *xs)
1138{
1139 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1140 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001141 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001142 size_t size;
1143 int ret = 0;
1144
1145 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1146 return -ENODATA;
1147
1148 xs->end = (void *)di + inode->i_sb->s_blocksize;
1149 xs->header = (struct ocfs2_xattr_header *)
1150 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1151 xs->base = (void *)xs->header;
1152 xs->here = xs->header->xh_entries;
1153
1154 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1155 if (ret)
1156 return ret;
1157 size = le64_to_cpu(xs->here->xe_value_size);
1158 if (buffer) {
1159 if (size > buffer_size)
1160 return -ERANGE;
1161 if (ocfs2_xattr_is_local(xs->here)) {
1162 memcpy(buffer, (void *)xs->base +
1163 le16_to_cpu(xs->here->xe_name_offset) +
1164 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1165 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001166 xv = (struct ocfs2_xattr_value_root *)
1167 (xs->base + le16_to_cpu(
1168 xs->here->xe_name_offset) +
1169 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1170 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001171 buffer, size);
1172 if (ret < 0) {
1173 mlog_errno(ret);
1174 return ret;
1175 }
1176 }
1177 }
1178
1179 return size;
1180}
1181
1182static int ocfs2_xattr_block_get(struct inode *inode,
1183 int name_index,
1184 const char *name,
1185 void *buffer,
1186 size_t buffer_size,
1187 struct ocfs2_xattr_search *xs)
1188{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001189 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001190 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001191 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301192 int ret = -ENODATA, name_offset, name_len, i;
1193 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001194
Joel Beckerba937122008-10-24 19:13:20 -07001195 xs->bucket = ocfs2_xattr_bucket_new(inode);
1196 if (!xs->bucket) {
1197 ret = -ENOMEM;
1198 mlog_errno(ret);
1199 goto cleanup;
1200 }
Tao Ma589dc262008-08-18 17:38:51 +08001201
Joel Becker54f443f2008-10-20 18:43:07 -07001202 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1203 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001204 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001205 goto cleanup;
1206 }
1207
Tiger Yang6c1e1832008-11-02 19:04:21 +08001208 if (xs->not_found) {
1209 ret = -ENODATA;
1210 goto cleanup;
1211 }
1212
Joel Becker54f443f2008-10-20 18:43:07 -07001213 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001214 size = le64_to_cpu(xs->here->xe_value_size);
1215 if (buffer) {
1216 ret = -ERANGE;
1217 if (size > buffer_size)
1218 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001219
1220 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1221 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1222 i = xs->here - xs->header->xh_entries;
1223
1224 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001225 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001226 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001227 i,
1228 &block_off,
1229 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001230 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001231 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001232 if (ocfs2_xattr_is_local(xs->here)) {
1233 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001234 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001235 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001236 xv = (struct ocfs2_xattr_value_root *)
1237 (xs->base + name_offset + name_len);
1238 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001239 buffer, size);
1240 if (ret < 0) {
1241 mlog_errno(ret);
1242 goto cleanup;
1243 }
1244 }
1245 }
1246 ret = size;
1247cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001248 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001249
Joel Becker54f443f2008-10-20 18:43:07 -07001250 brelse(xs->xattr_bh);
1251 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001252 return ret;
1253}
1254
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001255int ocfs2_xattr_get_nolock(struct inode *inode,
1256 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001257 int name_index,
1258 const char *name,
1259 void *buffer,
1260 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001261{
1262 int ret;
1263 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001264 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1265 struct ocfs2_xattr_search xis = {
1266 .not_found = -ENODATA,
1267 };
1268 struct ocfs2_xattr_search xbs = {
1269 .not_found = -ENODATA,
1270 };
1271
Tiger Yang8154da32008-08-18 17:11:46 +08001272 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1273 return -EOPNOTSUPP;
1274
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001275 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1276 ret = -ENODATA;
1277
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001278 xis.inode_bh = xbs.inode_bh = di_bh;
1279 di = (struct ocfs2_dinode *)di_bh->b_data;
1280
1281 down_read(&oi->ip_xattr_sem);
1282 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1283 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001284 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001285 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1286 buffer_size, &xbs);
1287 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001288
1289 return ret;
1290}
1291
1292/* ocfs2_xattr_get()
1293 *
1294 * Copy an extended attribute into the buffer provided.
1295 * Buffer is NULL to compute the size of buffer required.
1296 */
1297static int ocfs2_xattr_get(struct inode *inode,
1298 int name_index,
1299 const char *name,
1300 void *buffer,
1301 size_t buffer_size)
1302{
1303 int ret;
1304 struct buffer_head *di_bh = NULL;
1305
1306 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1307 if (ret < 0) {
1308 mlog_errno(ret);
1309 return ret;
1310 }
1311 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1312 name, buffer, buffer_size);
1313
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001314 ocfs2_inode_unlock(inode, 0);
1315
1316 brelse(di_bh);
1317
1318 return ret;
1319}
1320
1321static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001322 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001323 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001324 const void *value,
1325 int value_len)
1326{
Tao Ma71d548a2008-12-05 06:20:54 +08001327 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001328 u16 blocksize = inode->i_sb->s_blocksize;
1329 u32 p_cluster, num_clusters;
1330 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1331 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1332 u64 blkno;
1333 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001334 unsigned int ext_flags;
1335 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001336
1337 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1338
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001339 while (cpos < clusters) {
1340 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001341 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001342 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001343 if (ret) {
1344 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001345 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001346 }
1347
Tao Ma492a8a32009-08-18 11:43:17 +08001348 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1349
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001350 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1351
1352 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001353 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1354 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001355 if (ret) {
1356 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001357 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001358 }
1359
1360 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001361 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001362 bh,
1363 OCFS2_JOURNAL_ACCESS_WRITE);
1364 if (ret < 0) {
1365 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001366 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001367 }
1368
1369 cp_len = value_len > blocksize ? blocksize : value_len;
1370 memcpy(bh->b_data, value, cp_len);
1371 value_len -= cp_len;
1372 value += cp_len;
1373 if (cp_len < blocksize)
1374 memset(bh->b_data + cp_len, 0,
1375 blocksize - cp_len);
1376
1377 ret = ocfs2_journal_dirty(handle, bh);
1378 if (ret < 0) {
1379 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001380 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001381 }
1382 brelse(bh);
1383 bh = NULL;
1384
1385 /*
1386 * XXX: do we need to empty all the following
1387 * blocks in this cluster?
1388 */
1389 if (!value_len)
1390 break;
1391 }
1392 cpos += num_clusters;
1393 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001394out:
1395 brelse(bh);
1396
1397 return ret;
1398}
1399
Joel Becker69a3e532009-08-17 12:24:39 -07001400static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1401 int num_entries)
1402{
1403 int free_space;
1404
1405 if (!needed_space)
1406 return 0;
1407
1408 free_space = free_start -
1409 sizeof(struct ocfs2_xattr_header) -
1410 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1411 OCFS2_XATTR_HEADER_GAP;
1412 if (free_space < 0)
1413 return -EIO;
1414 if (free_space < needed_space)
1415 return -ENOSPC;
1416
1417 return 0;
1418}
1419
Joel Beckercf2bc802009-08-18 13:52:38 -07001420static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1421 int type)
1422{
1423 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1424}
1425
1426static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1427{
1428 loc->xl_ops->xlo_journal_dirty(handle, loc);
1429}
1430
Joel Becker69a3e532009-08-17 12:24:39 -07001431/* Give a pointer into the storage for the given offset */
1432static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1433{
1434 BUG_ON(offset >= loc->xl_size);
1435 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1436}
1437
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001438/*
Joel Becker11179f22009-08-14 16:07:44 -07001439 * Wipe the name+value pair and allow the storage to reclaim it. This
1440 * must be followed by either removal of the entry or a call to
1441 * ocfs2_xa_add_namevalue().
1442 */
1443static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1444{
1445 loc->xl_ops->xlo_wipe_namevalue(loc);
1446}
1447
Joel Becker69a3e532009-08-17 12:24:39 -07001448/*
1449 * Find lowest offset to a name+value pair. This is the start of our
1450 * downward-growing free space.
1451 */
1452static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1453{
1454 return loc->xl_ops->xlo_get_free_start(loc);
1455}
1456
1457/* Can we reuse loc->xl_entry for xi? */
1458static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1459 struct ocfs2_xattr_info *xi)
1460{
1461 return loc->xl_ops->xlo_can_reuse(loc, xi);
1462}
1463
1464/* How much free space is needed to set the new value */
1465static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1466 struct ocfs2_xattr_info *xi)
1467{
1468 return loc->xl_ops->xlo_check_space(loc, xi);
1469}
1470
1471static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1472{
1473 loc->xl_ops->xlo_add_entry(loc, name_hash);
1474 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1475 /*
1476 * We can't leave the new entry's xe_name_offset at zero or
1477 * add_namevalue() will go nuts. We set it to the size of our
1478 * storage so that it can never be less than any other entry.
1479 */
1480 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1481}
1482
1483static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1484 struct ocfs2_xattr_info *xi)
1485{
1486 int size = namevalue_size_xi(xi);
1487 int nameval_offset;
1488 char *nameval_buf;
1489
1490 loc->xl_ops->xlo_add_namevalue(loc, size);
1491 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1492 loc->xl_entry->xe_name_len = xi->xi_name_len;
1493 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1494 ocfs2_xattr_set_local(loc->xl_entry,
1495 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1496
1497 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1498 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1499 memset(nameval_buf, 0, size);
1500 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1501}
1502
Joel Becker3fc12af2009-08-18 13:20:27 -07001503static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1504 struct ocfs2_xattr_value_buf *vb)
1505{
1506 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1507 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1508
1509 /* Value bufs are for value trees */
Joel Becker73857ee2009-08-18 20:26:41 -07001510 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
Joel Becker3fc12af2009-08-18 13:20:27 -07001511 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1512 (name_size + OCFS2_XATTR_ROOT_SIZE));
1513
1514 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1515 vb->vb_xv =
1516 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1517 nameval_offset +
1518 name_size);
1519}
1520
Joel Beckercf2bc802009-08-18 13:52:38 -07001521static int ocfs2_xa_block_journal_access(handle_t *handle,
1522 struct ocfs2_xa_loc *loc, int type)
1523{
1524 struct buffer_head *bh = loc->xl_storage;
1525 ocfs2_journal_access_func access;
1526
1527 if (loc->xl_size == (bh->b_size -
1528 offsetof(struct ocfs2_xattr_block,
1529 xb_attrs.xb_header)))
1530 access = ocfs2_journal_access_xb;
1531 else
1532 access = ocfs2_journal_access_di;
1533 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1534}
1535
1536static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1537 struct ocfs2_xa_loc *loc)
1538{
1539 struct buffer_head *bh = loc->xl_storage;
1540
1541 ocfs2_journal_dirty(handle, bh);
1542}
1543
Joel Becker11179f22009-08-14 16:07:44 -07001544static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1545 int offset)
1546{
Joel Becker11179f22009-08-14 16:07:44 -07001547 return (char *)loc->xl_header + offset;
1548}
1549
Joel Becker69a3e532009-08-17 12:24:39 -07001550static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1551 struct ocfs2_xattr_info *xi)
1552{
1553 /*
1554 * Block storage is strict. If the sizes aren't exact, we will
1555 * remove the old one and reinsert the new.
1556 */
1557 return namevalue_size_xe(loc->xl_entry) ==
1558 namevalue_size_xi(xi);
1559}
1560
1561static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1562{
1563 struct ocfs2_xattr_header *xh = loc->xl_header;
1564 int i, count = le16_to_cpu(xh->xh_count);
1565 int offset, free_start = loc->xl_size;
1566
1567 for (i = 0; i < count; i++) {
1568 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1569 if (offset < free_start)
1570 free_start = offset;
1571 }
1572
1573 return free_start;
1574}
1575
1576static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1577 struct ocfs2_xattr_info *xi)
1578{
1579 int count = le16_to_cpu(loc->xl_header->xh_count);
1580 int free_start = ocfs2_xa_get_free_start(loc);
1581 int needed_space = ocfs2_xi_entry_usage(xi);
1582
1583 /*
1584 * Block storage will reclaim the original entry before inserting
1585 * the new value, so we only need the difference. If the new
1586 * entry is smaller than the old one, we don't need anything.
1587 */
1588 if (loc->xl_entry) {
1589 /* Don't need space if we're reusing! */
1590 if (ocfs2_xa_can_reuse_entry(loc, xi))
1591 needed_space = 0;
1592 else
1593 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1594 }
1595 if (needed_space < 0)
1596 needed_space = 0;
1597 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1598}
1599
Joel Becker11179f22009-08-14 16:07:44 -07001600/*
1601 * Block storage for xattrs keeps the name+value pairs compacted. When
1602 * we remove one, we have to shift any that preceded it towards the end.
1603 */
1604static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1605{
1606 int i, offset;
1607 int namevalue_offset, first_namevalue_offset, namevalue_size;
1608 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1609 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Becker11179f22009-08-14 16:07:44 -07001610 int count = le16_to_cpu(xh->xh_count);
1611
1612 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07001613 namevalue_size = namevalue_size_xe(entry);
Joel Becker69a3e532009-08-17 12:24:39 -07001614 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
Joel Becker11179f22009-08-14 16:07:44 -07001615
1616 /* Shift the name+value pairs */
1617 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1618 (char *)xh + first_namevalue_offset,
1619 namevalue_offset - first_namevalue_offset);
1620 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1621
1622 /* Now tell xh->xh_entries about it */
1623 for (i = 0; i < count; i++) {
1624 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1625 if (offset < namevalue_offset)
1626 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1627 namevalue_size);
1628 }
1629
1630 /*
1631 * Note that we don't update xh_free_start or xh_name_value_len
1632 * because they're not used in block-stored xattrs.
1633 */
1634}
1635
Joel Becker69a3e532009-08-17 12:24:39 -07001636static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1637{
1638 int count = le16_to_cpu(loc->xl_header->xh_count);
1639 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1640 le16_add_cpu(&loc->xl_header->xh_count, 1);
1641 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1642}
1643
1644static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1645{
1646 int free_start = ocfs2_xa_get_free_start(loc);
1647
1648 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1649}
1650
Joel Becker3fc12af2009-08-18 13:20:27 -07001651static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1652 struct ocfs2_xattr_value_buf *vb)
1653{
1654 struct buffer_head *bh = loc->xl_storage;
1655
1656 if (loc->xl_size == (bh->b_size -
1657 offsetof(struct ocfs2_xattr_block,
1658 xb_attrs.xb_header)))
1659 vb->vb_access = ocfs2_journal_access_xb;
1660 else
1661 vb->vb_access = ocfs2_journal_access_di;
1662 vb->vb_bh = bh;
1663}
1664
Joel Becker11179f22009-08-14 16:07:44 -07001665/*
1666 * Operations for xattrs stored in blocks. This includes inline inode
1667 * storage and unindexed ocfs2_xattr_blocks.
1668 */
1669static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001670 .xlo_journal_access = ocfs2_xa_block_journal_access,
1671 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001672 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001673 .xlo_check_space = ocfs2_xa_block_check_space,
1674 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1675 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001676 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001677 .xlo_add_entry = ocfs2_xa_block_add_entry,
1678 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001679 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001680};
1681
Joel Beckercf2bc802009-08-18 13:52:38 -07001682static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1683 struct ocfs2_xa_loc *loc, int type)
1684{
1685 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1686
1687 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1688}
1689
1690static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1691 struct ocfs2_xa_loc *loc)
1692{
1693 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1694
1695 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1696}
1697
Joel Becker11179f22009-08-14 16:07:44 -07001698static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1699 int offset)
1700{
1701 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1702 int block, block_offset;
1703
Joel Becker11179f22009-08-14 16:07:44 -07001704 /* The header is at the front of the bucket */
Joel Beckercf2bc802009-08-18 13:52:38 -07001705 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1706 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
Joel Becker11179f22009-08-14 16:07:44 -07001707
1708 return bucket_block(bucket, block) + block_offset;
1709}
1710
Joel Becker69a3e532009-08-17 12:24:39 -07001711static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1712 struct ocfs2_xattr_info *xi)
1713{
1714 return namevalue_size_xe(loc->xl_entry) >=
1715 namevalue_size_xi(xi);
1716}
1717
1718static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1719{
1720 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1721 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1722}
1723
1724static int ocfs2_bucket_align_free_start(struct super_block *sb,
1725 int free_start, int size)
1726{
1727 /*
1728 * We need to make sure that the name+value pair fits within
1729 * one block.
1730 */
1731 if (((free_start - size) >> sb->s_blocksize_bits) !=
1732 ((free_start - 1) >> sb->s_blocksize_bits))
1733 free_start -= free_start % sb->s_blocksize;
1734
1735 return free_start;
1736}
1737
1738static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1739 struct ocfs2_xattr_info *xi)
1740{
1741 int rc;
1742 int count = le16_to_cpu(loc->xl_header->xh_count);
1743 int free_start = ocfs2_xa_get_free_start(loc);
1744 int needed_space = ocfs2_xi_entry_usage(xi);
1745 int size = namevalue_size_xi(xi);
Joel Beckercf2bc802009-08-18 13:52:38 -07001746 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001747
1748 /*
1749 * Bucket storage does not reclaim name+value pairs it cannot
1750 * reuse. They live as holes until the bucket fills, and then
1751 * the bucket is defragmented. However, the bucket can reclaim
1752 * the ocfs2_xattr_entry.
1753 */
1754 if (loc->xl_entry) {
1755 /* Don't need space if we're reusing! */
1756 if (ocfs2_xa_can_reuse_entry(loc, xi))
1757 needed_space = 0;
1758 else
1759 needed_space -= sizeof(struct ocfs2_xattr_entry);
1760 }
1761 BUG_ON(needed_space < 0);
1762
1763 if (free_start < size) {
1764 if (needed_space)
1765 return -ENOSPC;
1766 } else {
1767 /*
1768 * First we check if it would fit in the first place.
1769 * Below, we align the free start to a block. This may
1770 * slide us below the minimum gap. By checking unaligned
1771 * first, we avoid that error.
1772 */
1773 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1774 count);
1775 if (rc)
1776 return rc;
1777 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1778 size);
1779 }
1780 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1781}
1782
Joel Becker11179f22009-08-14 16:07:44 -07001783static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1784{
Joel Becker199799a2009-08-14 19:04:15 -07001785 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1786 -namevalue_size_xe(loc->xl_entry));
Joel Becker11179f22009-08-14 16:07:44 -07001787}
1788
Joel Becker69a3e532009-08-17 12:24:39 -07001789static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1790{
1791 struct ocfs2_xattr_header *xh = loc->xl_header;
1792 int count = le16_to_cpu(xh->xh_count);
1793 int low = 0, high = count - 1, tmp;
1794 struct ocfs2_xattr_entry *tmp_xe;
1795
1796 /*
1797 * We keep buckets sorted by name_hash, so we need to find
1798 * our insert place.
1799 */
1800 while (low <= high && count) {
1801 tmp = (low + high) / 2;
1802 tmp_xe = &xh->xh_entries[tmp];
1803
1804 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1805 low = tmp + 1;
1806 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1807 high = tmp - 1;
1808 else {
1809 low = tmp;
1810 break;
1811 }
1812 }
1813
1814 if (low != count)
1815 memmove(&xh->xh_entries[low + 1],
1816 &xh->xh_entries[low],
1817 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1818
1819 le16_add_cpu(&xh->xh_count, 1);
1820 loc->xl_entry = &xh->xh_entries[low];
1821 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1822}
1823
1824static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1825{
1826 int free_start = ocfs2_xa_get_free_start(loc);
1827 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Beckercf2bc802009-08-18 13:52:38 -07001828 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001829 int nameval_offset;
1830
1831 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1832 nameval_offset = free_start - size;
1833 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1834 xh->xh_free_start = cpu_to_le16(nameval_offset);
1835 le16_add_cpu(&xh->xh_name_value_len, size);
1836
1837}
1838
Joel Becker3fc12af2009-08-18 13:20:27 -07001839static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1840 struct ocfs2_xattr_value_buf *vb)
1841{
1842 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
Joel Beckercf2bc802009-08-18 13:52:38 -07001843 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker3fc12af2009-08-18 13:20:27 -07001844 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1845 int size = namevalue_size_xe(loc->xl_entry);
1846 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1847
1848 /* Values are not allowed to straddle block boundaries */
1849 BUG_ON(block_offset !=
1850 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1851 /* We expect the bucket to be filled in */
1852 BUG_ON(!bucket->bu_bhs[block_offset]);
1853
1854 vb->vb_access = ocfs2_journal_access;
1855 vb->vb_bh = bucket->bu_bhs[block_offset];
1856}
1857
Joel Becker11179f22009-08-14 16:07:44 -07001858/* Operations for xattrs stored in buckets. */
1859static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001860 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1861 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001862 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001863 .xlo_check_space = ocfs2_xa_bucket_check_space,
1864 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1865 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001866 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001867 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1868 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001869 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001870};
1871
Joel Becker73857ee2009-08-18 20:26:41 -07001872static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1873 struct ocfs2_xattr_set_ctxt *ctxt)
1874{
1875 int trunc_rc, access_rc;
1876 struct ocfs2_xattr_value_buf vb;
1877
1878 ocfs2_xa_fill_value_buf(loc, &vb);
1879 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1880 ctxt);
1881
1882 /*
1883 * The caller of ocfs2_xa_value_truncate() has already called
1884 * ocfs2_xa_journal_access on the loc. However, The truncate code
1885 * calls ocfs2_extend_trans(). This may commit the previous
1886 * transaction and open a new one. If this is a bucket, truncate
1887 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1888 * the caller is expecting to dirty the entire bucket. So we must
1889 * reset the journal work. We do this even if truncate has failed,
1890 * as it could have failed after committing the extend.
1891 */
1892 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1893 OCFS2_JOURNAL_ACCESS_WRITE);
1894
1895 /* Errors in truncate take precedence */
1896 return trunc_rc ? trunc_rc : access_rc;
1897}
1898
Joel Becker11179f22009-08-14 16:07:44 -07001899static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1900{
Joel Beckerbde1e542009-08-14 16:58:38 -07001901 int index, count;
1902 struct ocfs2_xattr_header *xh = loc->xl_header;
1903 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1904
Joel Becker11179f22009-08-14 16:07:44 -07001905 ocfs2_xa_wipe_namevalue(loc);
Joel Beckerbde1e542009-08-14 16:58:38 -07001906 loc->xl_entry = NULL;
1907
1908 le16_add_cpu(&xh->xh_count, -1);
1909 count = le16_to_cpu(xh->xh_count);
1910
1911 /*
1912 * Only zero out the entry if there are more remaining. This is
1913 * important for an empty bucket, as it keeps track of the
1914 * bucket's hash value. It doesn't hurt empty block storage.
1915 */
1916 if (count) {
1917 index = ((char *)entry - (char *)&xh->xh_entries) /
1918 sizeof(struct ocfs2_xattr_entry);
1919 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1920 (count - index) * sizeof(struct ocfs2_xattr_entry));
1921 memset(&xh->xh_entries[count], 0,
1922 sizeof(struct ocfs2_xattr_entry));
1923 }
Joel Becker11179f22009-08-14 16:07:44 -07001924}
1925
Joel Becker73857ee2009-08-18 20:26:41 -07001926static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1927 struct ocfs2_xattr_set_ctxt *ctxt)
1928{
1929 int rc = 0;
1930
1931 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
1932 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1933 if (rc) {
1934 mlog_errno(rc);
1935 goto out;
1936 }
1937 }
1938
1939 ocfs2_xa_remove_entry(loc);
1940
1941out:
1942 return rc;
1943}
1944
1945static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
1946{
1947 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1948 char *nameval_buf;
1949
1950 nameval_buf = ocfs2_xa_offset_pointer(loc,
1951 le16_to_cpu(loc->xl_entry->xe_name_offset));
1952 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
1953}
1954
1955/*
1956 * Take an existing entry and make it ready for the new value. This
1957 * won't allocate space, but it may free space. It should be ready for
1958 * ocfs2_xa_prepare_entry() to finish the work.
1959 */
1960static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
1961 struct ocfs2_xattr_info *xi,
1962 struct ocfs2_xattr_set_ctxt *ctxt)
1963{
1964 int rc = 0;
1965 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1966 char *nameval_buf;
1967 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
1968 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
1969
1970 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
1971 name_size);
1972
1973 nameval_buf = ocfs2_xa_offset_pointer(loc,
1974 le16_to_cpu(loc->xl_entry->xe_name_offset));
1975 if (xe_local) {
1976 memset(nameval_buf + name_size, 0,
1977 namevalue_size_xe(loc->xl_entry) - name_size);
1978 if (!xi_local)
1979 ocfs2_xa_install_value_root(loc);
1980 } else {
1981 if (xi_local) {
1982 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1983 if (rc < 0) {
1984 mlog_errno(rc);
1985 goto out;
1986 }
1987 memset(nameval_buf + name_size, 0,
1988 namevalue_size_xe(loc->xl_entry) -
1989 name_size);
1990 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
1991 xi->xi_value_len) {
1992 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
1993 ctxt);
1994 if (rc < 0) {
1995 mlog_errno(rc);
1996 goto out;
1997 }
1998 }
1999 }
2000
2001 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2002 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2003
2004out:
2005 return rc;
2006}
2007
Joel Becker69a3e532009-08-17 12:24:39 -07002008/*
2009 * Prepares loc->xl_entry to receive the new xattr. This includes
2010 * properly setting up the name+value pair region. If loc->xl_entry
2011 * already exists, it will take care of modifying it appropriately.
Joel Becker69a3e532009-08-17 12:24:39 -07002012 *
2013 * Note that this modifies the data. You did journal_access already,
2014 * right?
2015 */
2016static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2017 struct ocfs2_xattr_info *xi,
Joel Becker73857ee2009-08-18 20:26:41 -07002018 u32 name_hash,
2019 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002020{
2021 int rc = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002022
Joel Becker69a3e532009-08-17 12:24:39 -07002023 rc = ocfs2_xa_check_space(loc, xi);
2024 if (rc)
2025 goto out;
2026
2027 if (loc->xl_entry) {
2028 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
Joel Becker73857ee2009-08-18 20:26:41 -07002029 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2030 if (rc)
2031 goto out;
2032 goto alloc_value;
Joel Becker69a3e532009-08-17 12:24:39 -07002033 }
2034
Joel Becker73857ee2009-08-18 20:26:41 -07002035 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2036 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2037 if (rc) {
2038 mlog_errno(rc);
2039 goto out;
2040 }
2041 }
Joel Becker69a3e532009-08-17 12:24:39 -07002042 ocfs2_xa_wipe_namevalue(loc);
2043 } else
2044 ocfs2_xa_add_entry(loc, name_hash);
2045
2046 /*
2047 * If we get here, we have a blank entry. Fill it. We grow our
2048 * name+value pair back from the end.
2049 */
2050 ocfs2_xa_add_namevalue(loc, xi);
Joel Becker73857ee2009-08-18 20:26:41 -07002051 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2052 ocfs2_xa_install_value_root(loc);
2053
2054alloc_value:
2055 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2056 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2057 if (rc < 0)
2058 mlog_errno(rc);
2059 }
Joel Becker69a3e532009-08-17 12:24:39 -07002060
2061out:
2062 return rc;
2063}
2064
2065/*
Joel Becker73857ee2009-08-18 20:26:41 -07002066 * Store the value portion of the name+value pair. This will skip
2067 * values that are stored externally. Their tree roots were set up
2068 * by ocfs2_xa_prepare_entry().
Joel Becker69a3e532009-08-17 12:24:39 -07002069 */
Joel Becker73857ee2009-08-18 20:26:41 -07002070static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2071 struct ocfs2_xattr_info *xi,
2072 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002073{
Joel Becker73857ee2009-08-18 20:26:41 -07002074 int rc = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002075 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2076 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker69a3e532009-08-17 12:24:39 -07002077 char *nameval_buf;
Joel Becker73857ee2009-08-18 20:26:41 -07002078 struct ocfs2_xattr_value_buf vb;
Joel Becker69a3e532009-08-17 12:24:39 -07002079
Joel Becker69a3e532009-08-17 12:24:39 -07002080 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
Joel Becker73857ee2009-08-18 20:26:41 -07002081 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2082 ocfs2_xa_fill_value_buf(loc, &vb);
2083 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2084 ctxt->handle, &vb,
2085 xi->xi_value,
2086 xi->xi_value_len);
2087 } else
2088 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2089
Joel Becker73857ee2009-08-18 20:26:41 -07002090 return rc;
Joel Becker69a3e532009-08-17 12:24:39 -07002091}
2092
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002093static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2094 struct ocfs2_xattr_info *xi,
2095 struct ocfs2_xattr_set_ctxt *ctxt)
2096{
2097 int ret;
2098 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2099 xi->xi_name_len);
2100
2101 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2102 OCFS2_JOURNAL_ACCESS_WRITE);
2103 if (ret) {
2104 mlog_errno(ret);
2105 goto out;
2106 }
2107
2108 /* Don't worry, we are never called with !xi_value and !xl_entry */
2109 if (!xi->xi_value) {
2110 ret = ocfs2_xa_remove(loc, ctxt);
2111 goto out;
2112 }
2113
2114 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2115 if (ret) {
2116 if (ret != -ENOSPC)
2117 mlog_errno(ret);
2118 goto out;
2119 }
2120
2121 ret = ocfs2_xa_store_value(loc, xi, ctxt);
2122 if (ret) {
2123 mlog_errno(ret);
2124 goto out;
2125 }
2126
2127 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2128
2129out:
2130 return ret;
2131}
2132
Joel Becker11179f22009-08-14 16:07:44 -07002133static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2134 struct inode *inode,
2135 struct buffer_head *bh,
2136 struct ocfs2_xattr_entry *entry)
2137{
2138 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2139
Joel Becker139ffac2009-08-19 11:09:17 -07002140 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2141
Joel Beckercf2bc802009-08-18 13:52:38 -07002142 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002143 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2144 loc->xl_storage = bh;
2145 loc->xl_entry = entry;
Joel Becker139ffac2009-08-19 11:09:17 -07002146 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
Joel Becker11179f22009-08-14 16:07:44 -07002147 loc->xl_header =
2148 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2149 loc->xl_size);
2150}
2151
2152static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
Joel Beckercf2bc802009-08-18 13:52:38 -07002153 struct inode *inode,
Joel Becker11179f22009-08-14 16:07:44 -07002154 struct buffer_head *bh,
2155 struct ocfs2_xattr_entry *entry)
2156{
2157 struct ocfs2_xattr_block *xb =
2158 (struct ocfs2_xattr_block *)bh->b_data;
2159
2160 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2161
Joel Beckercf2bc802009-08-18 13:52:38 -07002162 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002163 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2164 loc->xl_storage = bh;
2165 loc->xl_header = &(xb->xb_attrs.xb_header);
2166 loc->xl_entry = entry;
2167 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2168 xb_attrs.xb_header);
2169}
2170
2171static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2172 struct ocfs2_xattr_bucket *bucket,
2173 struct ocfs2_xattr_entry *entry)
2174{
Joel Beckercf2bc802009-08-18 13:52:38 -07002175 loc->xl_inode = bucket->bu_inode;
Joel Becker11179f22009-08-14 16:07:44 -07002176 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2177 loc->xl_storage = bucket;
2178 loc->xl_header = bucket_xh(bucket);
2179 loc->xl_entry = entry;
2180 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2181}
2182
Tao Mace9c5a52009-08-18 11:43:59 +08002183/*
2184 * In xattr remove, if it is stored outside and refcounted, we may have
2185 * the chance to split the refcount tree. So need the allocators.
2186 */
2187static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2188 struct ocfs2_xattr_value_root *xv,
2189 struct ocfs2_caching_info *ref_ci,
2190 struct buffer_head *ref_root_bh,
2191 struct ocfs2_alloc_context **meta_ac,
2192 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002193{
Tao Mace9c5a52009-08-18 11:43:59 +08002194 int ret, meta_add = 0;
2195 u32 p_cluster, num_clusters;
2196 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08002197
Tao Mace9c5a52009-08-18 11:43:59 +08002198 *ref_credits = 0;
2199 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2200 &num_clusters,
2201 &xv->xr_list,
2202 &ext_flags);
2203 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08002204 mlog_errno(ret);
2205 goto out;
2206 }
2207
Tao Mace9c5a52009-08-18 11:43:59 +08002208 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2209 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002210
Tao Mace9c5a52009-08-18 11:43:59 +08002211 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2212 ref_root_bh, xv,
2213 &meta_add, ref_credits);
2214 if (ret) {
2215 mlog_errno(ret);
2216 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002217 }
2218
Tao Mace9c5a52009-08-18 11:43:59 +08002219 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2220 meta_add, meta_ac);
2221 if (ret)
2222 mlog_errno(ret);
2223
Tao Ma85db90e2008-11-12 08:27:01 +08002224out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002225 return ret;
2226}
2227
Tao Mace9c5a52009-08-18 11:43:59 +08002228static int ocfs2_remove_value_outside(struct inode*inode,
2229 struct ocfs2_xattr_value_buf *vb,
2230 struct ocfs2_xattr_header *header,
2231 struct ocfs2_caching_info *ref_ci,
2232 struct buffer_head *ref_root_bh)
2233{
2234 int ret = 0, i, ref_credits;
2235 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2236 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2237 void *val;
2238
2239 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2240
2241 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2242 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2243
2244 if (ocfs2_xattr_is_local(entry))
2245 continue;
2246
2247 val = (void *)header +
2248 le16_to_cpu(entry->xe_name_offset);
2249 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2250 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2251
2252 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2253 ref_ci, ref_root_bh,
2254 &ctxt.meta_ac,
2255 &ref_credits);
2256
2257 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2258 ocfs2_remove_extent_credits(osb->sb));
2259 if (IS_ERR(ctxt.handle)) {
2260 ret = PTR_ERR(ctxt.handle);
2261 mlog_errno(ret);
2262 break;
2263 }
2264
2265 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2266 if (ret < 0) {
2267 mlog_errno(ret);
2268 break;
2269 }
2270
2271 ocfs2_commit_trans(osb, ctxt.handle);
2272 if (ctxt.meta_ac) {
2273 ocfs2_free_alloc_context(ctxt.meta_ac);
2274 ctxt.meta_ac = NULL;
2275 }
2276 }
2277
2278 if (ctxt.meta_ac)
2279 ocfs2_free_alloc_context(ctxt.meta_ac);
2280 ocfs2_schedule_truncate_log_flush(osb, 1);
2281 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2282 return ret;
2283}
2284
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002285static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002286 struct buffer_head *di_bh,
2287 struct ocfs2_caching_info *ref_ci,
2288 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002289{
2290
2291 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2292 struct ocfs2_xattr_header *header;
2293 int ret;
Joel Becker43119012008-12-09 16:24:43 -08002294 struct ocfs2_xattr_value_buf vb = {
2295 .vb_bh = di_bh,
2296 .vb_access = ocfs2_journal_access_di,
2297 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002298
2299 header = (struct ocfs2_xattr_header *)
2300 ((void *)di + inode->i_sb->s_blocksize -
2301 le16_to_cpu(di->i_xattr_inline_size));
2302
Tao Mace9c5a52009-08-18 11:43:59 +08002303 ret = ocfs2_remove_value_outside(inode, &vb, header,
2304 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002305
2306 return ret;
2307}
2308
Tao Mace9c5a52009-08-18 11:43:59 +08002309struct ocfs2_rm_xattr_bucket_para {
2310 struct ocfs2_caching_info *ref_ci;
2311 struct buffer_head *ref_root_bh;
2312};
2313
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002314static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002315 struct buffer_head *blk_bh,
2316 struct ocfs2_caching_info *ref_ci,
2317 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002318{
2319 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002320 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08002321 struct ocfs2_xattr_value_buf vb = {
2322 .vb_bh = blk_bh,
2323 .vb_access = ocfs2_journal_access_xb,
2324 };
Tao Mace9c5a52009-08-18 11:43:59 +08002325 struct ocfs2_rm_xattr_bucket_para args = {
2326 .ref_ci = ref_ci,
2327 .ref_root_bh = ref_root_bh,
2328 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002329
2330 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08002331 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2332 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08002333 ret = ocfs2_remove_value_outside(inode, &vb, header,
2334 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08002335 } else
Tao Ma47bca492009-08-18 11:43:42 +08002336 ret = ocfs2_iterate_xattr_index_block(inode,
2337 blk_bh,
2338 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08002339 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002340
2341 return ret;
2342}
2343
Tao Ma08413892008-08-29 09:00:19 +08002344static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002345 u64 block,
2346 struct ocfs2_caching_info *ref_ci,
2347 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08002348{
2349 struct inode *xb_alloc_inode;
2350 struct buffer_head *xb_alloc_bh = NULL;
2351 struct buffer_head *blk_bh = NULL;
2352 struct ocfs2_xattr_block *xb;
2353 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2354 handle_t *handle;
2355 int ret = 0;
2356 u64 blk, bg_blkno;
2357 u16 bit;
2358
Joel Becker4ae1d692008-11-13 14:49:18 -08002359 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08002360 if (ret < 0) {
2361 mlog_errno(ret);
2362 goto out;
2363 }
2364
Tao Mace9c5a52009-08-18 11:43:59 +08002365 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08002366 if (ret < 0) {
2367 mlog_errno(ret);
2368 goto out;
2369 }
2370
Joel Becker4ae1d692008-11-13 14:49:18 -08002371 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08002372 blk = le64_to_cpu(xb->xb_blkno);
2373 bit = le16_to_cpu(xb->xb_suballoc_bit);
2374 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2375
2376 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2377 EXTENT_ALLOC_SYSTEM_INODE,
2378 le16_to_cpu(xb->xb_suballoc_slot));
2379 if (!xb_alloc_inode) {
2380 ret = -ENOMEM;
2381 mlog_errno(ret);
2382 goto out;
2383 }
2384 mutex_lock(&xb_alloc_inode->i_mutex);
2385
2386 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2387 if (ret < 0) {
2388 mlog_errno(ret);
2389 goto out_mutex;
2390 }
2391
2392 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2393 if (IS_ERR(handle)) {
2394 ret = PTR_ERR(handle);
2395 mlog_errno(ret);
2396 goto out_unlock;
2397 }
2398
2399 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2400 bit, bg_blkno, 1);
2401 if (ret < 0)
2402 mlog_errno(ret);
2403
2404 ocfs2_commit_trans(osb, handle);
2405out_unlock:
2406 ocfs2_inode_unlock(xb_alloc_inode, 1);
2407 brelse(xb_alloc_bh);
2408out_mutex:
2409 mutex_unlock(&xb_alloc_inode->i_mutex);
2410 iput(xb_alloc_inode);
2411out:
2412 brelse(blk_bh);
2413 return ret;
2414}
2415
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002416/*
2417 * ocfs2_xattr_remove()
2418 *
2419 * Free extended attribute resources associated with this inode.
2420 */
2421int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2422{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002423 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2424 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002425 struct ocfs2_refcount_tree *ref_tree = NULL;
2426 struct buffer_head *ref_root_bh = NULL;
2427 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002428 handle_t *handle;
2429 int ret;
2430
Tiger Yang8154da32008-08-18 17:11:46 +08002431 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2432 return 0;
2433
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002434 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2435 return 0;
2436
Tao Mace9c5a52009-08-18 11:43:59 +08002437 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2438 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2439 le64_to_cpu(di->i_refcount_loc),
2440 1, &ref_tree, &ref_root_bh);
2441 if (ret) {
2442 mlog_errno(ret);
2443 goto out;
2444 }
2445 ref_ci = &ref_tree->rf_ci;
2446
2447 }
2448
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002449 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002450 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2451 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002452 if (ret < 0) {
2453 mlog_errno(ret);
2454 goto out;
2455 }
2456 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002457
Tao Ma08413892008-08-29 09:00:19 +08002458 if (di->i_xattr_loc) {
2459 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002460 le64_to_cpu(di->i_xattr_loc),
2461 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002462 if (ret < 0) {
2463 mlog_errno(ret);
2464 goto out;
2465 }
2466 }
2467
2468 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2469 OCFS2_INODE_UPDATE_CREDITS);
2470 if (IS_ERR(handle)) {
2471 ret = PTR_ERR(handle);
2472 mlog_errno(ret);
2473 goto out;
2474 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002475 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002476 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002477 if (ret) {
2478 mlog_errno(ret);
2479 goto out_commit;
2480 }
2481
Tao Ma08413892008-08-29 09:00:19 +08002482 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002483
2484 spin_lock(&oi->ip_lock);
2485 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2486 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2487 spin_unlock(&oi->ip_lock);
2488
2489 ret = ocfs2_journal_dirty(handle, di_bh);
2490 if (ret < 0)
2491 mlog_errno(ret);
2492out_commit:
2493 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2494out:
Tao Mace9c5a52009-08-18 11:43:59 +08002495 if (ref_tree)
2496 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2497 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002498 return ret;
2499}
2500
2501static int ocfs2_xattr_has_space_inline(struct inode *inode,
2502 struct ocfs2_dinode *di)
2503{
2504 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2505 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2506 int free;
2507
2508 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2509 return 0;
2510
2511 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2512 struct ocfs2_inline_data *idata = &di->id2.i_data;
2513 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2514 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2515 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2516 le64_to_cpu(di->i_size);
2517 } else {
2518 struct ocfs2_extent_list *el = &di->id2.i_list;
2519 free = (le16_to_cpu(el->l_count) -
2520 le16_to_cpu(el->l_next_free_rec)) *
2521 sizeof(struct ocfs2_extent_rec);
2522 }
2523 if (free >= xattrsize)
2524 return 1;
2525
2526 return 0;
2527}
2528
2529/*
2530 * ocfs2_xattr_ibody_find()
2531 *
2532 * Find extended attribute in inode block and
2533 * fill search info into struct ocfs2_xattr_search.
2534 */
2535static int ocfs2_xattr_ibody_find(struct inode *inode,
2536 int name_index,
2537 const char *name,
2538 struct ocfs2_xattr_search *xs)
2539{
2540 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2541 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2542 int ret;
2543 int has_space = 0;
2544
2545 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2546 return 0;
2547
2548 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2549 down_read(&oi->ip_alloc_sem);
2550 has_space = ocfs2_xattr_has_space_inline(inode, di);
2551 up_read(&oi->ip_alloc_sem);
2552 if (!has_space)
2553 return 0;
2554 }
2555
2556 xs->xattr_bh = xs->inode_bh;
2557 xs->end = (void *)di + inode->i_sb->s_blocksize;
2558 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2559 xs->header = (struct ocfs2_xattr_header *)
2560 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2561 else
2562 xs->header = (struct ocfs2_xattr_header *)
2563 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2564 xs->base = (void *)xs->header;
2565 xs->here = xs->header->xh_entries;
2566
2567 /* Find the named attribute. */
2568 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2569 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2570 if (ret && ret != -ENODATA)
2571 return ret;
2572 xs->not_found = ret;
2573 }
2574
2575 return 0;
2576}
2577
Joel Becker139ffac2009-08-19 11:09:17 -07002578static int ocfs2_xattr_ibody_init(struct inode *inode,
2579 struct buffer_head *di_bh,
2580 struct ocfs2_xattr_set_ctxt *ctxt)
2581{
2582 int ret;
2583 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2584 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2585 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2586 unsigned int xattrsize = osb->s_xattr_inline_size;
2587
2588 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2589 ret = -ENOSPC;
2590 goto out;
2591 }
2592
2593 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2594 OCFS2_JOURNAL_ACCESS_WRITE);
2595 if (ret) {
2596 mlog_errno(ret);
2597 goto out;
2598 }
2599
2600 /*
2601 * Adjust extent record count or inline data size
2602 * to reserve space for extended attribute.
2603 */
2604 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2605 struct ocfs2_inline_data *idata = &di->id2.i_data;
2606 le16_add_cpu(&idata->id_count, -xattrsize);
2607 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2608 struct ocfs2_extent_list *el = &di->id2.i_list;
2609 le16_add_cpu(&el->l_count, -(xattrsize /
2610 sizeof(struct ocfs2_extent_rec)));
2611 }
2612 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2613
2614 spin_lock(&oi->ip_lock);
2615 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2616 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2617 spin_unlock(&oi->ip_lock);
2618
2619 ret = ocfs2_journal_dirty(ctxt->handle, di_bh);
2620 if (ret < 0)
2621 mlog_errno(ret);
2622
2623out:
2624 return ret;
2625}
2626
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002627/*
2628 * ocfs2_xattr_ibody_set()
2629 *
2630 * Set, replace or remove an extended attribute into inode block.
2631 *
2632 */
2633static int ocfs2_xattr_ibody_set(struct inode *inode,
2634 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002635 struct ocfs2_xattr_search *xs,
2636 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002637{
Joel Becker139ffac2009-08-19 11:09:17 -07002638 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002639 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2640 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Joel Becker139ffac2009-08-19 11:09:17 -07002641 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002642
2643 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2644 return -ENOSPC;
2645
2646 down_write(&oi->ip_alloc_sem);
2647 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2648 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2649 ret = -ENOSPC;
2650 goto out;
2651 }
2652 }
2653
Joel Becker139ffac2009-08-19 11:09:17 -07002654 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2655 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2656 if (ret) {
2657 if (ret != -ENOSPC)
2658 mlog_errno(ret);
2659 goto out;
2660 }
2661 }
2662
2663 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2664 xs->not_found ? NULL : xs->here);
2665 ret = ocfs2_xa_set(&loc, xi, ctxt);
2666 if (ret) {
2667 if (ret != -ENOSPC)
2668 mlog_errno(ret);
2669 goto out;
2670 }
2671 xs->here = loc.xl_entry;
2672
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002673out:
2674 up_write(&oi->ip_alloc_sem);
2675
2676 return ret;
2677}
2678
2679/*
2680 * ocfs2_xattr_block_find()
2681 *
2682 * Find extended attribute in external block and
2683 * fill search info into struct ocfs2_xattr_search.
2684 */
2685static int ocfs2_xattr_block_find(struct inode *inode,
2686 int name_index,
2687 const char *name,
2688 struct ocfs2_xattr_search *xs)
2689{
2690 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2691 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002692 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002693 int ret = 0;
2694
2695 if (!di->i_xattr_loc)
2696 return ret;
2697
Joel Becker4ae1d692008-11-13 14:49:18 -08002698 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2699 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002700 if (ret < 0) {
2701 mlog_errno(ret);
2702 return ret;
2703 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002704
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002705 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002706 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002707
Tao Ma589dc262008-08-18 17:38:51 +08002708 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2709 xs->header = &xb->xb_attrs.xb_header;
2710 xs->base = (void *)xs->header;
2711 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2712 xs->here = xs->header->xh_entries;
2713
2714 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2715 } else
2716 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2717 name_index,
2718 name, xs);
2719
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002720 if (ret && ret != -ENODATA) {
2721 xs->xattr_bh = NULL;
2722 goto cleanup;
2723 }
2724 xs->not_found = ret;
2725 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002726cleanup:
2727 brelse(blk_bh);
2728
2729 return ret;
2730}
2731
Joel Beckerd3981542009-08-19 02:13:50 -07002732static int ocfs2_create_xattr_block(struct inode *inode,
Tao Ma5aea1f02009-08-18 11:43:24 +08002733 struct buffer_head *inode_bh,
Joel Beckerd3981542009-08-19 02:13:50 -07002734 struct ocfs2_xattr_set_ctxt *ctxt,
2735 int indexed,
2736 struct buffer_head **ret_bh)
Tao Ma5aea1f02009-08-18 11:43:24 +08002737{
2738 int ret;
2739 u16 suballoc_bit_start;
2740 u32 num_got;
2741 u64 first_blkno;
2742 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2743 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2744 struct buffer_head *new_bh = NULL;
2745 struct ocfs2_xattr_block *xblk;
2746
Joel Beckerd3981542009-08-19 02:13:50 -07002747 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2748 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
Tao Ma5aea1f02009-08-18 11:43:24 +08002749 if (ret < 0) {
2750 mlog_errno(ret);
2751 goto end;
2752 }
2753
Joel Beckerd3981542009-08-19 02:13:50 -07002754 ret = ocfs2_claim_metadata(osb, ctxt->handle, ctxt->meta_ac, 1,
Tao Ma5aea1f02009-08-18 11:43:24 +08002755 &suballoc_bit_start, &num_got,
2756 &first_blkno);
2757 if (ret < 0) {
2758 mlog_errno(ret);
2759 goto end;
2760 }
2761
2762 new_bh = sb_getblk(inode->i_sb, first_blkno);
2763 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2764
Joel Beckerd3981542009-08-19 02:13:50 -07002765 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
Tao Ma5aea1f02009-08-18 11:43:24 +08002766 new_bh,
2767 OCFS2_JOURNAL_ACCESS_CREATE);
2768 if (ret < 0) {
2769 mlog_errno(ret);
2770 goto end;
2771 }
2772
2773 /* Initialize ocfs2_xattr_block */
2774 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2775 memset(xblk, 0, inode->i_sb->s_blocksize);
2776 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
Joel Beckerd3981542009-08-19 02:13:50 -07002777 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
Tao Ma5aea1f02009-08-18 11:43:24 +08002778 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2779 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2780 xblk->xb_blkno = cpu_to_le64(first_blkno);
Tao Maa7fe7a32009-08-18 11:43:52 +08002781 if (indexed) {
2782 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2783 xr->xt_clusters = cpu_to_le32(1);
2784 xr->xt_last_eb_blk = 0;
2785 xr->xt_list.l_tree_depth = 0;
2786 xr->xt_list.l_count = cpu_to_le16(
2787 ocfs2_xattr_recs_per_xb(inode->i_sb));
2788 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2789 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2790 }
Joel Beckerd3981542009-08-19 02:13:50 -07002791 ocfs2_journal_dirty(ctxt->handle, new_bh);
Tao Maa7fe7a32009-08-18 11:43:52 +08002792
Joel Beckerd3981542009-08-19 02:13:50 -07002793 /* Add it to the inode */
Tao Ma5aea1f02009-08-18 11:43:24 +08002794 di->i_xattr_loc = cpu_to_le64(first_blkno);
Joel Beckerd3981542009-08-19 02:13:50 -07002795
2796 spin_lock(&OCFS2_I(inode)->ip_lock);
2797 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2798 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2799 spin_unlock(&OCFS2_I(inode)->ip_lock);
2800
2801 ocfs2_journal_dirty(ctxt->handle, inode_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002802
2803 *ret_bh = new_bh;
2804 new_bh = NULL;
2805
2806end:
2807 brelse(new_bh);
2808 return ret;
2809}
2810
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002811/*
2812 * ocfs2_xattr_block_set()
2813 *
2814 * Set, replace or remove an extended attribute into external block.
2815 *
2816 */
2817static int ocfs2_xattr_block_set(struct inode *inode,
2818 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002819 struct ocfs2_xattr_search *xs,
2820 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002821{
2822 struct buffer_head *new_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002823 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002824 int ret;
Joel Beckerd3981542009-08-19 02:13:50 -07002825 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002826
2827 if (!xs->xattr_bh) {
Joel Beckerd3981542009-08-19 02:13:50 -07002828 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2829 0, &new_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002830 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002831 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002832 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002833 }
2834
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002835 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002836 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002837 xs->header = &xblk->xb_attrs.xb_header;
2838 xs->base = (void *)xs->header;
2839 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2840 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002841 } else
2842 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2843
2844 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
Joel Beckerd3981542009-08-19 02:13:50 -07002845 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2846 xs->not_found ? NULL : xs->here);
Tao Ma01225592008-08-18 17:38:53 +08002847
Joel Beckerd3981542009-08-19 02:13:50 -07002848 ret = ocfs2_xa_set(&loc, xi, ctxt);
2849 if (!ret)
2850 xs->here = loc.xl_entry;
2851 else if (ret != -ENOSPC)
Tao Ma01225592008-08-18 17:38:53 +08002852 goto end;
Joel Beckerd3981542009-08-19 02:13:50 -07002853 else {
2854 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2855 if (ret)
2856 goto end;
2857 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002858 }
2859
Joel Beckerd3981542009-08-19 02:13:50 -07002860 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2861 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002862
2863end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002864 return ret;
2865}
2866
Tao Ma78f30c32008-11-12 08:27:00 +08002867/* Check whether the new xattr can be inserted into the inode. */
2868static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2869 struct ocfs2_xattr_info *xi,
2870 struct ocfs2_xattr_search *xs)
2871{
Tao Ma78f30c32008-11-12 08:27:00 +08002872 struct ocfs2_xattr_entry *last;
2873 int free, i;
2874 size_t min_offs = xs->end - xs->base;
2875
2876 if (!xs->header)
2877 return 0;
2878
2879 last = xs->header->xh_entries;
2880
2881 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2882 size_t offs = le16_to_cpu(last->xe_name_offset);
2883 if (offs < min_offs)
2884 min_offs = offs;
2885 last += 1;
2886 }
2887
Tiger Yang4442f512009-02-20 11:11:50 +08002888 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002889 if (free < 0)
2890 return 0;
2891
2892 BUG_ON(!xs->not_found);
2893
Joel Becker199799a2009-08-14 19:04:15 -07002894 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
Tao Ma78f30c32008-11-12 08:27:00 +08002895 return 1;
2896
2897 return 0;
2898}
2899
2900static int ocfs2_calc_xattr_set_need(struct inode *inode,
2901 struct ocfs2_dinode *di,
2902 struct ocfs2_xattr_info *xi,
2903 struct ocfs2_xattr_search *xis,
2904 struct ocfs2_xattr_search *xbs,
2905 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002906 int *meta_need,
2907 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002908{
2909 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002910 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002911 struct buffer_head *bh = NULL;
2912 struct ocfs2_xattr_block *xb = NULL;
2913 struct ocfs2_xattr_entry *xe = NULL;
2914 struct ocfs2_xattr_value_root *xv = NULL;
2915 char *base = NULL;
2916 int name_offset, name_len = 0;
2917 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Joel Becker6b240ff2009-08-14 18:02:52 -07002918 xi->xi_value_len);
Tao Ma78f30c32008-11-12 08:27:00 +08002919 u64 value_size;
2920
Tao Ma71d548a2008-12-05 06:20:54 +08002921 /*
2922 * Calculate the clusters we need to write.
2923 * No matter whether we replace an old one or add a new one,
2924 * we need this for writing.
2925 */
Joel Becker6b240ff2009-08-14 18:02:52 -07002926 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma71d548a2008-12-05 06:20:54 +08002927 credits += new_clusters *
2928 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2929
Tao Ma78f30c32008-11-12 08:27:00 +08002930 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002931 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2932
Joel Becker6b240ff2009-08-14 18:02:52 -07002933 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002934 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002935 credits += ocfs2_calc_extend_credits(inode->i_sb,
2936 &def_xv.xv.xr_list,
2937 new_clusters);
2938 }
Tao Ma78f30c32008-11-12 08:27:00 +08002939
2940 goto meta_guess;
2941 }
2942
2943 if (!xis->not_found) {
2944 xe = xis->here;
2945 name_offset = le16_to_cpu(xe->xe_name_offset);
2946 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2947 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002948 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002949 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002950 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002951 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2952 xe = xbs->here;
2953 name_offset = le16_to_cpu(xe->xe_name_offset);
2954 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2955 i = xbs->here - xbs->header->xh_entries;
2956 old_in_xb = 1;
2957
2958 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08002959 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08002960 bucket_xh(xbs->bucket),
2961 i, &block_off,
2962 &name_offset);
2963 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002964 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2965 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002966 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002967 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2968 }
2969 }
2970
2971 /*
2972 * delete a xattr doesn't need metadata and cluster allocation.
2973 * so just calculate the credits and return.
2974 *
2975 * The credits for removing the value tree will be extended
2976 * by ocfs2_remove_extent itself.
2977 */
Joel Becker6b240ff2009-08-14 18:02:52 -07002978 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08002979 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002980 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002981
2982 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002983 }
2984
2985 /* do cluster allocation guess first. */
2986 value_size = le64_to_cpu(xe->xe_value_size);
2987
2988 if (old_in_xb) {
2989 /*
2990 * In xattr set, we always try to set the xe in inode first,
2991 * so if it can be inserted into inode successfully, the old
2992 * one will be removed from the xattr block, and this xattr
2993 * will be inserted into inode as a new xattr in inode.
2994 */
2995 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2996 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002997 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002998 OCFS2_INODE_UPDATE_CREDITS;
2999 if (!ocfs2_xattr_is_local(xe))
3000 credits += ocfs2_calc_extend_credits(
3001 inode->i_sb,
3002 &def_xv.xv.xr_list,
3003 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08003004 goto out;
3005 }
3006 }
3007
Joel Becker6b240ff2009-08-14 18:02:52 -07003008 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003009 /* the new values will be stored outside. */
3010 u32 old_clusters = 0;
3011
3012 if (!ocfs2_xattr_is_local(xe)) {
3013 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3014 value_size);
3015 xv = (struct ocfs2_xattr_value_root *)
3016 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08003017 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08003018 } else
3019 xv = &def_xv.xv;
3020
Tao Ma85db90e2008-11-12 08:27:01 +08003021 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02003022 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003023 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08003024 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003025 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3026 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003027 credits += ocfs2_calc_extend_credits(inode->i_sb,
3028 &xv->xr_list,
3029 new_clusters -
3030 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08003031 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3032 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003033 }
3034 } else {
3035 /*
3036 * Now the new value will be stored inside. So if the new
3037 * value is smaller than the size of value root or the old
3038 * value, we don't need any allocation, otherwise we have
3039 * to guess metadata allocation.
3040 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003041 if ((ocfs2_xattr_is_local(xe) &&
3042 (value_size >= xi->xi_value_len)) ||
Tao Ma78f30c32008-11-12 08:27:00 +08003043 (!ocfs2_xattr_is_local(xe) &&
Joel Becker6b240ff2009-08-14 18:02:52 -07003044 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
Tao Ma78f30c32008-11-12 08:27:00 +08003045 goto out;
3046 }
3047
3048meta_guess:
3049 /* calculate metadata allocation. */
3050 if (di->i_xattr_loc) {
3051 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08003052 ret = ocfs2_read_xattr_block(inode,
3053 le64_to_cpu(di->i_xattr_loc),
3054 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08003055 if (ret) {
3056 mlog_errno(ret);
3057 goto out;
3058 }
3059
3060 xb = (struct ocfs2_xattr_block *)bh->b_data;
3061 } else
3062 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3063
Tao Ma90cb5462008-12-05 06:20:56 +08003064 /*
3065 * If there is already an xattr tree, good, we can calculate
3066 * like other b-trees. Otherwise we may have the chance of
3067 * create a tree, the credit calculation is borrowed from
3068 * ocfs2_calc_extend_credits with root_el = NULL. And the
3069 * new tree will be cluster based, so no meta is needed.
3070 */
Tao Ma78f30c32008-11-12 08:27:00 +08003071 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3072 struct ocfs2_extent_list *el =
3073 &xb->xb_attrs.xb_root.xt_list;
3074 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08003075 credits += ocfs2_calc_extend_credits(inode->i_sb,
3076 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08003077 } else
3078 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08003079
3080 /*
3081 * This cluster will be used either for new bucket or for
3082 * new xattr block.
3083 * If the cluster size is the same as the bucket size, one
3084 * more is needed since we may need to extend the bucket
3085 * also.
3086 */
3087 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003088 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003089 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08003090 OCFS2_SB(inode->i_sb)->s_clustersize) {
3091 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003092 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003093 }
3094 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003095 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003096 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3097 }
Tao Ma78f30c32008-11-12 08:27:00 +08003098out:
3099 if (clusters_need)
3100 *clusters_need = clusters_add;
3101 if (meta_need)
3102 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08003103 if (credits_need)
3104 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08003105 brelse(bh);
3106 return ret;
3107}
3108
3109static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3110 struct ocfs2_dinode *di,
3111 struct ocfs2_xattr_info *xi,
3112 struct ocfs2_xattr_search *xis,
3113 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003114 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08003115 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08003116 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08003117{
3118 int clusters_add, meta_add, ret;
3119 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3120
3121 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3122
3123 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3124
3125 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003126 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003127 if (ret) {
3128 mlog_errno(ret);
3129 return ret;
3130 }
3131
Tao Ma492a8a32009-08-18 11:43:17 +08003132 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08003133 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
Joel Becker6b240ff2009-08-14 18:02:52 -07003134 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003135
3136 if (meta_add) {
3137 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3138 &ctxt->meta_ac);
3139 if (ret) {
3140 mlog_errno(ret);
3141 goto out;
3142 }
3143 }
3144
3145 if (clusters_add) {
3146 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3147 if (ret)
3148 mlog_errno(ret);
3149 }
3150out:
3151 if (ret) {
3152 if (ctxt->meta_ac) {
3153 ocfs2_free_alloc_context(ctxt->meta_ac);
3154 ctxt->meta_ac = NULL;
3155 }
3156
3157 /*
3158 * We cannot have an error and a non null ctxt->data_ac.
3159 */
3160 }
3161
3162 return ret;
3163}
3164
Tao Ma85db90e2008-11-12 08:27:01 +08003165static int __ocfs2_xattr_set_handle(struct inode *inode,
3166 struct ocfs2_dinode *di,
3167 struct ocfs2_xattr_info *xi,
3168 struct ocfs2_xattr_search *xis,
3169 struct ocfs2_xattr_search *xbs,
3170 struct ocfs2_xattr_set_ctxt *ctxt)
3171{
Tao Ma9f868f12008-11-19 16:48:42 +08003172 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003173
Joel Becker6b240ff2009-08-14 18:02:52 -07003174 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003175 /* Remove existing extended attribute */
3176 if (!xis->not_found)
3177 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3178 else if (!xbs->not_found)
3179 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3180 } else {
3181 /* We always try to set extended attribute into inode first*/
3182 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3183 if (!ret && !xbs->not_found) {
3184 /*
3185 * If succeed and that extended attribute existing in
3186 * external block, then we will remove it.
3187 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003188 xi->xi_value = NULL;
3189 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003190
Tao Ma9f868f12008-11-19 16:48:42 +08003191 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003192 xis->not_found = -ENODATA;
3193 ret = ocfs2_calc_xattr_set_need(inode,
3194 di,
3195 xi,
3196 xis,
3197 xbs,
3198 NULL,
3199 NULL,
3200 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003201 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003202 if (ret) {
3203 mlog_errno(ret);
3204 goto out;
3205 }
3206
3207 ret = ocfs2_extend_trans(ctxt->handle, credits +
3208 ctxt->handle->h_buffer_credits);
3209 if (ret) {
3210 mlog_errno(ret);
3211 goto out;
3212 }
3213 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3214 } else if (ret == -ENOSPC) {
3215 if (di->i_xattr_loc && !xbs->xattr_bh) {
3216 ret = ocfs2_xattr_block_find(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07003217 xi->xi_name_index,
3218 xi->xi_name, xbs);
Tao Ma85db90e2008-11-12 08:27:01 +08003219 if (ret)
3220 goto out;
3221
Tao Ma9f868f12008-11-19 16:48:42 +08003222 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003223 xis->not_found = -ENODATA;
3224 ret = ocfs2_calc_xattr_set_need(inode,
3225 di,
3226 xi,
3227 xis,
3228 xbs,
3229 NULL,
3230 NULL,
3231 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003232 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003233 if (ret) {
3234 mlog_errno(ret);
3235 goto out;
3236 }
3237
3238 ret = ocfs2_extend_trans(ctxt->handle, credits +
3239 ctxt->handle->h_buffer_credits);
3240 if (ret) {
3241 mlog_errno(ret);
3242 goto out;
3243 }
3244 }
3245 /*
3246 * If no space in inode, we will set extended attribute
3247 * into external block.
3248 */
3249 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3250 if (ret)
3251 goto out;
3252 if (!xis->not_found) {
3253 /*
3254 * If succeed and that extended attribute
3255 * existing in inode, we will remove it.
3256 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003257 xi->xi_value = NULL;
3258 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003259 xbs->not_found = -ENODATA;
3260 ret = ocfs2_calc_xattr_set_need(inode,
3261 di,
3262 xi,
3263 xis,
3264 xbs,
3265 NULL,
3266 NULL,
3267 &credits);
3268 if (ret) {
3269 mlog_errno(ret);
3270 goto out;
3271 }
3272
3273 ret = ocfs2_extend_trans(ctxt->handle, credits +
3274 ctxt->handle->h_buffer_credits);
3275 if (ret) {
3276 mlog_errno(ret);
3277 goto out;
3278 }
3279 ret = ocfs2_xattr_ibody_set(inode, xi,
3280 xis, ctxt);
3281 }
3282 }
3283 }
3284
Tao Ma4b3f6202008-12-05 06:20:55 +08003285 if (!ret) {
3286 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08003287 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08003288 xis->inode_bh,
3289 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08003290 if (ret) {
3291 mlog_errno(ret);
3292 goto out;
3293 }
3294
3295 inode->i_ctime = CURRENT_TIME;
3296 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3297 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3298 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3299 }
Tao Ma85db90e2008-11-12 08:27:01 +08003300out:
3301 return ret;
3302}
3303
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003304/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08003305 * This function only called duing creating inode
3306 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08003307 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08003308 */
3309int ocfs2_xattr_set_handle(handle_t *handle,
3310 struct inode *inode,
3311 struct buffer_head *di_bh,
3312 int name_index,
3313 const char *name,
3314 const void *value,
3315 size_t value_len,
3316 int flags,
3317 struct ocfs2_alloc_context *meta_ac,
3318 struct ocfs2_alloc_context *data_ac)
3319{
3320 struct ocfs2_dinode *di;
3321 int ret;
3322
3323 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003324 .xi_name_index = name_index,
3325 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003326 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003327 .xi_value = value,
3328 .xi_value_len = value_len,
Tiger Yang6c3faba2008-11-14 11:16:03 +08003329 };
3330
3331 struct ocfs2_xattr_search xis = {
3332 .not_found = -ENODATA,
3333 };
3334
3335 struct ocfs2_xattr_search xbs = {
3336 .not_found = -ENODATA,
3337 };
3338
3339 struct ocfs2_xattr_set_ctxt ctxt = {
3340 .handle = handle,
3341 .meta_ac = meta_ac,
3342 .data_ac = data_ac,
3343 };
3344
3345 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3346 return -EOPNOTSUPP;
3347
Tiger Yang008aafa2008-12-09 16:43:08 +08003348 /*
3349 * In extreme situation, may need xattr bucket when
3350 * block size is too small. And we have already reserved
3351 * the credits for bucket in mknod.
3352 */
3353 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3354 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3355 if (!xbs.bucket) {
3356 mlog_errno(-ENOMEM);
3357 return -ENOMEM;
3358 }
3359 }
3360
Tiger Yang6c3faba2008-11-14 11:16:03 +08003361 xis.inode_bh = xbs.inode_bh = di_bh;
3362 di = (struct ocfs2_dinode *)di_bh->b_data;
3363
3364 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3365
3366 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3367 if (ret)
3368 goto cleanup;
3369 if (xis.not_found) {
3370 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3371 if (ret)
3372 goto cleanup;
3373 }
3374
3375 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3376
3377cleanup:
3378 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3379 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08003380 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08003381
3382 return ret;
3383}
3384
3385/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003386 * ocfs2_xattr_set()
3387 *
3388 * Set, replace or remove an extended attribute for this inode.
3389 * value is NULL to remove an existing extended attribute, else either
3390 * create or replace an extended attribute.
3391 */
3392int ocfs2_xattr_set(struct inode *inode,
3393 int name_index,
3394 const char *name,
3395 const void *value,
3396 size_t value_len,
3397 int flags)
3398{
3399 struct buffer_head *di_bh = NULL;
3400 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08003401 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003402 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003403 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08003404 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08003405 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003406
3407 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003408 .xi_name_index = name_index,
3409 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003410 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003411 .xi_value = value,
3412 .xi_value_len = value_len,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003413 };
3414
3415 struct ocfs2_xattr_search xis = {
3416 .not_found = -ENODATA,
3417 };
3418
3419 struct ocfs2_xattr_search xbs = {
3420 .not_found = -ENODATA,
3421 };
3422
Tiger Yang8154da32008-08-18 17:11:46 +08003423 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3424 return -EOPNOTSUPP;
3425
Joel Beckerba937122008-10-24 19:13:20 -07003426 /*
3427 * Only xbs will be used on indexed trees. xis doesn't need a
3428 * bucket.
3429 */
3430 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3431 if (!xbs.bucket) {
3432 mlog_errno(-ENOMEM);
3433 return -ENOMEM;
3434 }
3435
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003436 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3437 if (ret < 0) {
3438 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003439 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003440 }
3441 xis.inode_bh = xbs.inode_bh = di_bh;
3442 di = (struct ocfs2_dinode *)di_bh->b_data;
3443
3444 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3445 /*
3446 * Scan inode and external block to find the same name
3447 * extended attribute and collect search infomation.
3448 */
3449 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3450 if (ret)
3451 goto cleanup;
3452 if (xis.not_found) {
3453 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3454 if (ret)
3455 goto cleanup;
3456 }
3457
3458 if (xis.not_found && xbs.not_found) {
3459 ret = -ENODATA;
3460 if (flags & XATTR_REPLACE)
3461 goto cleanup;
3462 ret = 0;
3463 if (!value)
3464 goto cleanup;
3465 } else {
3466 ret = -EEXIST;
3467 if (flags & XATTR_CREATE)
3468 goto cleanup;
3469 }
3470
Tao Ma492a8a32009-08-18 11:43:17 +08003471 /* Check whether the value is refcounted and do some prepartion. */
3472 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3473 (!xis.not_found || !xbs.not_found)) {
3474 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3475 &xis, &xbs, &ref_tree,
3476 &ref_meta, &ref_credits);
3477 if (ret) {
3478 mlog_errno(ret);
3479 goto cleanup;
3480 }
3481 }
Tao Ma85db90e2008-11-12 08:27:01 +08003482
3483 mutex_lock(&tl_inode->i_mutex);
3484
3485 if (ocfs2_truncate_log_needs_flush(osb)) {
3486 ret = __ocfs2_flush_truncate_log(osb);
3487 if (ret < 0) {
3488 mutex_unlock(&tl_inode->i_mutex);
3489 mlog_errno(ret);
3490 goto cleanup;
3491 }
3492 }
3493 mutex_unlock(&tl_inode->i_mutex);
3494
3495 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003496 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003497 if (ret) {
3498 mlog_errno(ret);
3499 goto cleanup;
3500 }
3501
Tao Ma4b3f6202008-12-05 06:20:55 +08003502 /* we need to update inode's ctime field, so add credit for it. */
3503 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003504 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003505 if (IS_ERR(ctxt.handle)) {
3506 ret = PTR_ERR(ctxt.handle);
3507 mlog_errno(ret);
3508 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003509 }
Tao Ma85db90e2008-11-12 08:27:01 +08003510
3511 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3512
3513 ocfs2_commit_trans(osb, ctxt.handle);
3514
Tao Ma78f30c32008-11-12 08:27:00 +08003515 if (ctxt.data_ac)
3516 ocfs2_free_alloc_context(ctxt.data_ac);
3517 if (ctxt.meta_ac)
3518 ocfs2_free_alloc_context(ctxt.meta_ac);
3519 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3520 ocfs2_schedule_truncate_log_flush(osb, 1);
3521 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003522
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003523cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003524 if (ref_tree)
3525 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003526 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003527 if (!value && !ret) {
3528 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3529 if (ret)
3530 mlog_errno(ret);
3531 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003532 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003533cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003534 brelse(di_bh);
3535 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003536 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003537
3538 return ret;
3539}
3540
Tao Ma0c044f02008-08-18 17:38:50 +08003541/*
3542 * Find the xattr extent rec which may contains name_hash.
3543 * e_cpos will be the first name hash of the xattr rec.
3544 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3545 */
3546static int ocfs2_xattr_get_rec(struct inode *inode,
3547 u32 name_hash,
3548 u64 *p_blkno,
3549 u32 *e_cpos,
3550 u32 *num_clusters,
3551 struct ocfs2_extent_list *el)
3552{
3553 int ret = 0, i;
3554 struct buffer_head *eb_bh = NULL;
3555 struct ocfs2_extent_block *eb;
3556 struct ocfs2_extent_rec *rec = NULL;
3557 u64 e_blkno = 0;
3558
3559 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003560 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3561 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003562 if (ret) {
3563 mlog_errno(ret);
3564 goto out;
3565 }
3566
3567 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3568 el = &eb->h_list;
3569
3570 if (el->l_tree_depth) {
3571 ocfs2_error(inode->i_sb,
3572 "Inode %lu has non zero tree depth in "
3573 "xattr tree block %llu\n", inode->i_ino,
3574 (unsigned long long)eb_bh->b_blocknr);
3575 ret = -EROFS;
3576 goto out;
3577 }
3578 }
3579
3580 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3581 rec = &el->l_recs[i];
3582
3583 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3584 e_blkno = le64_to_cpu(rec->e_blkno);
3585 break;
3586 }
3587 }
3588
3589 if (!e_blkno) {
3590 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3591 "record (%u, %u, 0) in xattr", inode->i_ino,
3592 le32_to_cpu(rec->e_cpos),
3593 ocfs2_rec_clusters(el, rec));
3594 ret = -EROFS;
3595 goto out;
3596 }
3597
3598 *p_blkno = le64_to_cpu(rec->e_blkno);
3599 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3600 if (e_cpos)
3601 *e_cpos = le32_to_cpu(rec->e_cpos);
3602out:
3603 brelse(eb_bh);
3604 return ret;
3605}
3606
3607typedef int (xattr_bucket_func)(struct inode *inode,
3608 struct ocfs2_xattr_bucket *bucket,
3609 void *para);
3610
Tao Ma589dc262008-08-18 17:38:51 +08003611static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003612 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003613 int name_index,
3614 const char *name,
3615 u32 name_hash,
3616 u16 *xe_index,
3617 int *found)
3618{
3619 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003620 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003621 size_t name_len = strlen(name);
3622 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003623 char *xe_name;
3624
3625 /*
3626 * We don't use binary search in the bucket because there
3627 * may be multiple entries with the same name hash.
3628 */
3629 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3630 xe = &xh->xh_entries[i];
3631
3632 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3633 continue;
3634 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3635 break;
3636
3637 cmp = name_index - ocfs2_xattr_get_type(xe);
3638 if (!cmp)
3639 cmp = name_len - xe->xe_name_len;
3640 if (cmp)
3641 continue;
3642
Tao Mafd68a892009-08-18 11:43:21 +08003643 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003644 xh,
3645 i,
3646 &block_off,
3647 &new_offset);
3648 if (ret) {
3649 mlog_errno(ret);
3650 break;
3651 }
3652
Joel Becker970e4932008-11-13 14:49:19 -08003653
Joel Beckere2356a32008-10-27 15:01:54 -07003654 xe_name = bucket_block(bucket, block_off) + new_offset;
3655 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003656 *xe_index = i;
3657 *found = 1;
3658 ret = 0;
3659 break;
3660 }
3661 }
3662
3663 return ret;
3664}
3665
3666/*
3667 * Find the specified xattr entry in a series of buckets.
3668 * This series start from p_blkno and last for num_clusters.
3669 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3670 * the num of the valid buckets.
3671 *
3672 * Return the buffer_head this xattr should reside in. And if the xattr's
3673 * hash is in the gap of 2 buckets, return the lower bucket.
3674 */
3675static int ocfs2_xattr_bucket_find(struct inode *inode,
3676 int name_index,
3677 const char *name,
3678 u32 name_hash,
3679 u64 p_blkno,
3680 u32 first_hash,
3681 u32 num_clusters,
3682 struct ocfs2_xattr_search *xs)
3683{
3684 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003685 struct ocfs2_xattr_header *xh = NULL;
3686 struct ocfs2_xattr_entry *xe = NULL;
3687 u16 index = 0;
3688 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3689 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003690 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003691 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003692 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003693
Joel Beckere2356a32008-10-27 15:01:54 -07003694 search = ocfs2_xattr_bucket_new(inode);
3695 if (!search) {
3696 ret = -ENOMEM;
3697 mlog_errno(ret);
3698 goto out;
3699 }
3700
3701 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003702 if (ret) {
3703 mlog_errno(ret);
3704 goto out;
3705 }
3706
Joel Beckere2356a32008-10-27 15:01:54 -07003707 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003708 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003709 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003710 ocfs2_xattr_bucket_relse(search);
3711
Tao Ma589dc262008-08-18 17:38:51 +08003712 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003713 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003714 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003715 if (ret) {
3716 mlog_errno(ret);
3717 goto out;
3718 }
3719
Joel Beckere2356a32008-10-27 15:01:54 -07003720 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003721 xe = &xh->xh_entries[0];
3722 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3723 high_bucket = bucket - 1;
3724 continue;
3725 }
3726
3727 /*
3728 * Check whether the hash of the last entry in our
Tao Ma5a0956112008-09-19 22:17:41 +08003729 * bucket is larger than the search one. for an empty
3730 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003731 */
Tao Ma5a0956112008-09-19 22:17:41 +08003732 if (xh->xh_count)
3733 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3734
Tao Ma589dc262008-08-18 17:38:51 +08003735 last_hash = le32_to_cpu(xe->xe_name_hash);
3736
Joel Beckere2356a32008-10-27 15:01:54 -07003737 /* record lower_blkno which may be the insert place. */
3738 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003739
3740 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3741 low_bucket = bucket + 1;
3742 continue;
3743 }
3744
3745 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003746 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003747 name_index, name, name_hash,
3748 &index, &found);
3749 if (ret) {
3750 mlog_errno(ret);
3751 goto out;
3752 }
3753 break;
3754 }
3755
3756 /*
3757 * Record the bucket we have found.
3758 * When the xattr's hash value is in the gap of 2 buckets, we will
3759 * always set it to the previous bucket.
3760 */
Joel Beckere2356a32008-10-27 15:01:54 -07003761 if (!lower_blkno)
3762 lower_blkno = p_blkno;
3763
3764 /* This should be in cache - we just read it during the search */
3765 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3766 if (ret) {
3767 mlog_errno(ret);
3768 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003769 }
Tao Ma589dc262008-08-18 17:38:51 +08003770
Joel Beckerba937122008-10-24 19:13:20 -07003771 xs->header = bucket_xh(xs->bucket);
3772 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003773 xs->end = xs->base + inode->i_sb->s_blocksize;
3774
3775 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003776 xs->here = &xs->header->xh_entries[index];
3777 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003778 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003779 } else
3780 ret = -ENODATA;
3781
3782out:
Joel Beckere2356a32008-10-27 15:01:54 -07003783 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003784 return ret;
3785}
3786
3787static int ocfs2_xattr_index_block_find(struct inode *inode,
3788 struct buffer_head *root_bh,
3789 int name_index,
3790 const char *name,
3791 struct ocfs2_xattr_search *xs)
3792{
3793 int ret;
3794 struct ocfs2_xattr_block *xb =
3795 (struct ocfs2_xattr_block *)root_bh->b_data;
3796 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3797 struct ocfs2_extent_list *el = &xb_root->xt_list;
3798 u64 p_blkno = 0;
3799 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003800 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003801
3802 if (le16_to_cpu(el->l_next_free_rec) == 0)
3803 return -ENODATA;
3804
3805 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3806 name, name_hash, name_index);
3807
3808 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3809 &num_clusters, el);
3810 if (ret) {
3811 mlog_errno(ret);
3812 goto out;
3813 }
3814
3815 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3816
3817 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003818 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3819 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003820
3821 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3822 p_blkno, first_hash, num_clusters, xs);
3823
3824out:
3825 return ret;
3826}
3827
Tao Ma0c044f02008-08-18 17:38:50 +08003828static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3829 u64 blkno,
3830 u32 clusters,
3831 xattr_bucket_func *func,
3832 void *para)
3833{
Joel Becker6dde41d2008-10-24 17:16:48 -07003834 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003835 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3836 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003837 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003838
Joel Beckerba937122008-10-24 19:13:20 -07003839 bucket = ocfs2_xattr_bucket_new(inode);
3840 if (!bucket) {
3841 mlog_errno(-ENOMEM);
3842 return -ENOMEM;
3843 }
Tao Ma0c044f02008-08-18 17:38:50 +08003844
3845 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003846 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003847
Joel Beckerba937122008-10-24 19:13:20 -07003848 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3849 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003850 if (ret) {
3851 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003852 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003853 }
3854
Tao Ma0c044f02008-08-18 17:38:50 +08003855 /*
3856 * The real bucket num in this series of blocks is stored
3857 * in the 1st bucket.
3858 */
3859 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003860 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003861
Mark Fashehde29c082008-10-29 14:45:30 -07003862 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3863 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003864 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003865 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003866 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003867 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003868 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003869 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003870 }
3871
Joel Beckerba937122008-10-24 19:13:20 -07003872 ocfs2_xattr_bucket_relse(bucket);
3873 if (ret)
3874 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003875 }
3876
Joel Beckerba937122008-10-24 19:13:20 -07003877 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003878 return ret;
3879}
3880
3881struct ocfs2_xattr_tree_list {
3882 char *buffer;
3883 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003884 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003885};
3886
Tao Mafd68a892009-08-18 11:43:21 +08003887static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08003888 struct ocfs2_xattr_header *xh,
3889 int index,
3890 int *block_off,
3891 int *new_offset)
3892{
3893 u16 name_offset;
3894
3895 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3896 return -EINVAL;
3897
3898 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3899
Tao Mafd68a892009-08-18 11:43:21 +08003900 *block_off = name_offset >> sb->s_blocksize_bits;
3901 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08003902
3903 return 0;
3904}
3905
3906static int ocfs2_list_xattr_bucket(struct inode *inode,
3907 struct ocfs2_xattr_bucket *bucket,
3908 void *para)
3909{
Tao Ma936b8832008-10-09 23:06:14 +08003910 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003911 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003912 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003913 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003914
Joel Becker3e632942008-10-24 17:04:49 -07003915 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3916 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003917 type = ocfs2_xattr_get_type(entry);
3918 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003919
Tao Ma936b8832008-10-09 23:06:14 +08003920 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08003921 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07003922 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003923 i,
3924 &block_off,
3925 &new_offset);
3926 if (ret)
3927 break;
Tao Ma936b8832008-10-09 23:06:14 +08003928
Joel Becker51def392008-10-24 16:57:21 -07003929 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003930 new_offset;
3931 ret = ocfs2_xattr_list_entry(xl->buffer,
3932 xl->buffer_size,
3933 &xl->result,
3934 prefix, name,
3935 entry->xe_name_len);
3936 if (ret)
3937 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003938 }
3939 }
3940
3941 return ret;
3942}
3943
Tao Ma47bca492009-08-18 11:43:42 +08003944static int ocfs2_iterate_xattr_index_block(struct inode *inode,
3945 struct buffer_head *blk_bh,
3946 xattr_tree_rec_func *rec_func,
3947 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08003948{
Tao Ma47bca492009-08-18 11:43:42 +08003949 struct ocfs2_xattr_block *xb =
3950 (struct ocfs2_xattr_block *)blk_bh->b_data;
3951 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08003952 int ret = 0;
3953 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3954 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003955
Tao Ma47bca492009-08-18 11:43:42 +08003956 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08003957 return 0;
3958
3959 while (name_hash > 0) {
3960 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3961 &e_cpos, &num_clusters, el);
3962 if (ret) {
3963 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003964 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003965 }
3966
Tao Ma47bca492009-08-18 11:43:42 +08003967 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
3968 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08003969 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08003970 if (ret != -ERANGE)
3971 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08003972 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003973 }
3974
3975 if (e_cpos == 0)
3976 break;
3977
3978 name_hash = e_cpos - 1;
3979 }
3980
Tao Ma47bca492009-08-18 11:43:42 +08003981 return ret;
3982
3983}
3984
3985static int ocfs2_list_xattr_tree_rec(struct inode *inode,
3986 struct buffer_head *root_bh,
3987 u64 blkno, u32 cpos, u32 len, void *para)
3988{
3989 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
3990 ocfs2_list_xattr_bucket, para);
3991}
3992
3993static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3994 struct buffer_head *blk_bh,
3995 char *buffer,
3996 size_t buffer_size)
3997{
3998 int ret;
3999 struct ocfs2_xattr_tree_list xl = {
4000 .buffer = buffer,
4001 .buffer_size = buffer_size,
4002 .result = 0,
4003 };
4004
4005 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4006 ocfs2_list_xattr_tree_rec, &xl);
4007 if (ret) {
4008 mlog_errno(ret);
4009 goto out;
4010 }
4011
Tao Ma936b8832008-10-09 23:06:14 +08004012 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08004013out:
4014 return ret;
4015}
Tao Ma01225592008-08-18 17:38:53 +08004016
4017static int cmp_xe(const void *a, const void *b)
4018{
4019 const struct ocfs2_xattr_entry *l = a, *r = b;
4020 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4021 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4022
4023 if (l_hash > r_hash)
4024 return 1;
4025 if (l_hash < r_hash)
4026 return -1;
4027 return 0;
4028}
4029
4030static void swap_xe(void *a, void *b, int size)
4031{
4032 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4033
4034 tmp = *l;
4035 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4036 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4037}
4038
4039/*
4040 * When the ocfs2_xattr_block is filled up, new bucket will be created
4041 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07004042 * The header goes at the start of the bucket, and the names+values are
4043 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08004044 * Note: we need to sort the entries since they are not saved in order
4045 * in the ocfs2_xattr_block.
4046 */
4047static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4048 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07004049 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08004050{
4051 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07004052 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004053 u16 offset, size, off_change;
4054 struct ocfs2_xattr_entry *xe;
4055 struct ocfs2_xattr_block *xb =
4056 (struct ocfs2_xattr_block *)xb_bh->b_data;
4057 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004058 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004059 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07004060 char *src = xb_bh->b_data;
4061 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08004062
4063 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4064 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07004065 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08004066
Joel Becker178eeac2008-10-27 15:18:29 -07004067 for (i = 0; i < blks; i++)
4068 memset(bucket_block(bucket, i), 0, blocksize);
4069
Tao Ma01225592008-08-18 17:38:53 +08004070 /*
4071 * Since the xe_name_offset is based on ocfs2_xattr_header,
4072 * there is a offset change corresponding to the change of
4073 * ocfs2_xattr_header's position.
4074 */
4075 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4076 xe = &xb_xh->xh_entries[count - 1];
4077 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4078 size = blocksize - offset;
4079
4080 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08004081 memcpy(target + offset, src + offset, size);
4082
4083 /* Init new header now. */
4084 xh->xh_count = xb_xh->xh_count;
4085 xh->xh_num_buckets = cpu_to_le16(1);
4086 xh->xh_name_value_len = cpu_to_le16(size);
4087 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4088
4089 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07004090 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004091 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4092 size = count * sizeof(struct ocfs2_xattr_entry);
4093 memcpy(target + offset, (char *)xb_xh + offset, size);
4094
4095 /* Change the xe offset for all the xe because of the move. */
4096 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4097 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4098 for (i = 0; i < count; i++)
4099 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4100
4101 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4102 offset, size, off_change);
4103
4104 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4105 cmp_xe, swap_xe);
4106}
4107
4108/*
4109 * After we move xattr from block to index btree, we have to
4110 * update ocfs2_xattr_search to the new xe and base.
4111 *
4112 * When the entry is in xattr block, xattr_bh indicates the storage place.
4113 * While if the entry is in index b-tree, "bucket" indicates the
4114 * real place of the xattr.
4115 */
Joel Becker178eeac2008-10-27 15:18:29 -07004116static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4117 struct ocfs2_xattr_search *xs,
4118 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08004119{
Tao Ma01225592008-08-18 17:38:53 +08004120 char *buf = old_bh->b_data;
4121 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4122 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004123 int i;
Tao Ma01225592008-08-18 17:38:53 +08004124
Joel Beckerba937122008-10-24 19:13:20 -07004125 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07004126 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004127 xs->end = xs->base + inode->i_sb->s_blocksize;
4128
Joel Becker178eeac2008-10-27 15:18:29 -07004129 if (xs->not_found)
4130 return;
Tao Ma01225592008-08-18 17:38:53 +08004131
Joel Becker178eeac2008-10-27 15:18:29 -07004132 i = xs->here - old_xh->xh_entries;
4133 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004134}
4135
4136static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004137 struct ocfs2_xattr_search *xs,
4138 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004139{
Tao Ma85db90e2008-11-12 08:27:01 +08004140 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004141 u32 bit_off, len;
4142 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08004143 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004144 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4145 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08004146 struct buffer_head *xb_bh = xs->xattr_bh;
4147 struct ocfs2_xattr_block *xb =
4148 (struct ocfs2_xattr_block *)xb_bh->b_data;
4149 struct ocfs2_xattr_tree_root *xr;
4150 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08004151
4152 mlog(0, "create xattr index block for %llu\n",
4153 (unsigned long long)xb_bh->b_blocknr);
4154
4155 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07004156 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004157
Tao Ma01225592008-08-18 17:38:53 +08004158 /*
4159 * XXX:
4160 * We can use this lock for now, and maybe move to a dedicated mutex
4161 * if performance becomes a problem later.
4162 */
4163 down_write(&oi->ip_alloc_sem);
4164
Joel Becker0cf2f762009-02-12 16:41:25 -08004165 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08004166 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004167 if (ret) {
4168 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004169 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004170 }
4171
Tao Ma78f30c32008-11-12 08:27:00 +08004172 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
4173 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08004174 if (ret) {
4175 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004176 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004177 }
4178
4179 /*
4180 * The bucket may spread in many blocks, and
4181 * we will only touch the 1st block and the last block
4182 * in the whole bucket(one for entry and one for data).
4183 */
4184 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4185
Mark Fashehde29c082008-10-29 14:45:30 -07004186 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4187 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08004188
Joel Becker178eeac2008-10-27 15:18:29 -07004189 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004190 if (ret) {
4191 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004192 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004193 }
4194
Joel Becker178eeac2008-10-27 15:18:29 -07004195 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4196 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07004197 if (ret) {
4198 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004199 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07004200 }
Tao Ma01225592008-08-18 17:38:53 +08004201
Joel Becker178eeac2008-10-27 15:18:29 -07004202 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4203 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4204
4205 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4206
Tao Ma01225592008-08-18 17:38:53 +08004207 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4208 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4209 offsetof(struct ocfs2_xattr_block, xb_attrs));
4210
4211 xr = &xb->xb_attrs.xb_root;
4212 xr->xt_clusters = cpu_to_le32(1);
4213 xr->xt_last_eb_blk = 0;
4214 xr->xt_list.l_tree_depth = 0;
4215 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4216 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4217
4218 xr->xt_list.l_recs[0].e_cpos = 0;
4219 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4220 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4221
4222 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4223
Tao Ma85db90e2008-11-12 08:27:01 +08004224 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004225
Tao Ma85db90e2008-11-12 08:27:01 +08004226out:
Tao Ma01225592008-08-18 17:38:53 +08004227 up_write(&oi->ip_alloc_sem);
4228
Tao Ma01225592008-08-18 17:38:53 +08004229 return ret;
4230}
4231
4232static int cmp_xe_offset(const void *a, const void *b)
4233{
4234 const struct ocfs2_xattr_entry *l = a, *r = b;
4235 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4236 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4237
4238 if (l_name_offset < r_name_offset)
4239 return 1;
4240 if (l_name_offset > r_name_offset)
4241 return -1;
4242 return 0;
4243}
4244
4245/*
4246 * defrag a xattr bucket if we find that the bucket has some
4247 * holes beteen name/value pairs.
4248 * We will move all the name/value pairs to the end of the bucket
4249 * so that we can spare some space for insertion.
4250 */
4251static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004252 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004253 struct ocfs2_xattr_bucket *bucket)
4254{
4255 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004256 size_t end, offset, len;
Tao Ma01225592008-08-18 17:38:53 +08004257 struct ocfs2_xattr_header *xh;
4258 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07004259 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004260 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08004261 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08004262 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08004263
4264 /*
4265 * In order to make the operation more efficient and generic,
4266 * we copy all the blocks into a contiguous memory and do the
4267 * defragment there, so if anything is error, we will not touch
4268 * the real block.
4269 */
4270 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4271 if (!bucket_buf) {
4272 ret = -EIO;
4273 goto out;
4274 }
4275
Joel Becker161d6f302008-10-27 15:25:18 -07004276 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004277 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4278 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f302008-10-27 15:25:18 -07004279
Tao Ma1c32a2f2008-11-06 08:10:47 +08004280 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f302008-10-27 15:25:18 -07004281 OCFS2_JOURNAL_ACCESS_WRITE);
4282 if (ret < 0) {
4283 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004284 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004285 }
4286
4287 xh = (struct ocfs2_xattr_header *)bucket_buf;
4288 entries = (char *)xh->xh_entries;
4289 xh_free_start = le16_to_cpu(xh->xh_free_start);
4290
4291 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4292 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004293 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4294 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08004295
4296 /*
4297 * sort all the entries by their offset.
4298 * the largest will be the first, so that we can
4299 * move them to the end one by one.
4300 */
4301 sort(entries, le16_to_cpu(xh->xh_count),
4302 sizeof(struct ocfs2_xattr_entry),
4303 cmp_xe_offset, swap_xe);
4304
4305 /* Move all name/values to the end of the bucket. */
4306 xe = xh->xh_entries;
4307 end = OCFS2_XATTR_BUCKET_SIZE;
4308 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4309 offset = le16_to_cpu(xe->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07004310 len = namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004311
4312 /*
4313 * We must make sure that the name/value pair
4314 * exist in the same block. So adjust end to
4315 * the previous block end if needed.
4316 */
4317 if (((end - len) / blocksize !=
4318 (end - 1) / blocksize))
4319 end = end - end % blocksize;
4320
4321 if (end > offset + len) {
4322 memmove(bucket_buf + end - len,
4323 bucket_buf + offset, len);
4324 xe->xe_name_offset = cpu_to_le16(end - len);
4325 }
4326
4327 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4328 "bucket %llu\n", (unsigned long long)blkno);
4329
4330 end -= len;
4331 }
4332
4333 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4334 "bucket %llu\n", (unsigned long long)blkno);
4335
4336 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08004337 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004338
4339 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4340 xh->xh_free_start = cpu_to_le16(end);
4341
4342 /* sort the entries by their name_hash. */
4343 sort(entries, le16_to_cpu(xh->xh_count),
4344 sizeof(struct ocfs2_xattr_entry),
4345 cmp_xe, swap_xe);
4346
4347 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004348 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4349 memcpy(bucket_block(bucket, i), buf, blocksize);
4350 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004351
Tao Ma01225592008-08-18 17:38:53 +08004352out:
Tao Ma01225592008-08-18 17:38:53 +08004353 kfree(bucket_buf);
4354 return ret;
4355}
4356
4357/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08004358 * prev_blkno points to the start of an existing extent. new_blkno
4359 * points to a newly allocated extent. Because we know each of our
4360 * clusters contains more than bucket, we can easily split one cluster
4361 * at a bucket boundary. So we take the last cluster of the existing
4362 * extent and split it down the middle. We move the last half of the
4363 * buckets in the last cluster of the existing extent over to the new
4364 * extent.
Tao Ma01225592008-08-18 17:38:53 +08004365 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08004366 * first_bh is the buffer at prev_blkno so we can update the existing
4367 * extent's bucket count. header_bh is the bucket were we were hoping
4368 * to insert our xattr. If the bucket move places the target in the new
4369 * extent, we'll update first_bh and header_bh after modifying the old
4370 * extent.
4371 *
4372 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004373 */
4374static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4375 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004376 struct ocfs2_xattr_bucket *first,
4377 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004378 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08004379 u32 num_clusters,
4380 u32 *first_hash)
4381{
Joel Beckerc58b6032008-11-26 13:36:24 -08004382 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08004383 struct super_block *sb = inode->i_sb;
4384 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4385 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08004386 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08004387 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08004388 u64 last_cluster_blkno = bucket_blkno(first) +
4389 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004390
Joel Becker41cb8142008-11-26 14:25:21 -08004391 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4392 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08004393
Tao Ma01225592008-08-18 17:38:53 +08004394 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08004395 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004396
Joel Becker41cb8142008-11-26 14:25:21 -08004397 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08004398 last_cluster_blkno, new_blkno,
4399 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004400 if (ret) {
4401 mlog_errno(ret);
4402 goto out;
4403 }
4404
Joel Beckerc58b6032008-11-26 13:36:24 -08004405 /* This is the first bucket that got moved */
4406 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4407
Tao Ma01225592008-08-18 17:38:53 +08004408 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004409 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08004410 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08004411 */
Joel Becker41cb8142008-11-26 14:25:21 -08004412 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08004413 /* Find the block for the new target bucket */
4414 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08004415 (bucket_blkno(target) - src_blkno);
4416
4417 ocfs2_xattr_bucket_relse(first);
4418 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004419
4420 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004421 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08004422 * journal from ocfs2_cp_xattr_bucket().
4423 */
Joel Becker41cb8142008-11-26 14:25:21 -08004424 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08004425 if (ret) {
4426 mlog_errno(ret);
4427 goto out;
4428 }
Joel Becker41cb8142008-11-26 14:25:21 -08004429 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4430 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08004431 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004432
Joel Beckerb5c03e72008-11-25 19:58:16 -08004433 }
4434
Tao Ma01225592008-08-18 17:38:53 +08004435out:
Tao Ma01225592008-08-18 17:38:53 +08004436 return ret;
4437}
4438
Tao Ma01225592008-08-18 17:38:53 +08004439/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004440 * Find the suitable pos when we divide a bucket into 2.
4441 * We have to make sure the xattrs with the same hash value exist
4442 * in the same bucket.
4443 *
4444 * If this ocfs2_xattr_header covers more than one hash value, find a
4445 * place where the hash value changes. Try to find the most even split.
4446 * The most common case is that all entries have different hash values,
4447 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08004448 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004449static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4450{
4451 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4452 int count = le16_to_cpu(xh->xh_count);
4453 int delta, middle = count / 2;
4454
4455 /*
4456 * We start at the middle. Each step gets farther away in both
4457 * directions. We therefore hit the change in hash value
4458 * nearest to the middle. Note that this loop does not execute for
4459 * count < 2.
4460 */
4461 for (delta = 0; delta < middle; delta++) {
4462 /* Let's check delta earlier than middle */
4463 if (cmp_xe(&entries[middle - delta - 1],
4464 &entries[middle - delta]))
4465 return middle - delta;
4466
4467 /* For even counts, don't walk off the end */
4468 if ((middle + delta + 1) == count)
4469 continue;
4470
4471 /* Now try delta past middle */
4472 if (cmp_xe(&entries[middle + delta],
4473 &entries[middle + delta + 1]))
4474 return middle + delta + 1;
4475 }
4476
4477 /* Every entry had the same hash */
4478 return count;
4479}
4480
4481/*
4482 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4483 * first_hash will record the 1st hash of the new bucket.
4484 *
4485 * Normally half of the xattrs will be moved. But we have to make
4486 * sure that the xattrs with the same hash value are stored in the
4487 * same bucket. If all the xattrs in this bucket have the same hash
4488 * value, the new bucket will be initialized as an empty one and the
4489 * first_hash will be initialized as (hash_value+1).
4490 */
4491static int ocfs2_divide_xattr_bucket(struct inode *inode,
4492 handle_t *handle,
4493 u64 blk,
4494 u64 new_blk,
4495 u32 *first_hash,
4496 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004497{
4498 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004499 int count, start, len, name_value_len = 0, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004500 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004501 struct ocfs2_xattr_header *xh;
4502 struct ocfs2_xattr_entry *xe;
4503 int blocksize = inode->i_sb->s_blocksize;
4504
Tao Ma80bcaf32008-10-27 06:06:24 +08004505 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004506 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004507
Joel Beckerba937122008-10-24 19:13:20 -07004508 s_bucket = ocfs2_xattr_bucket_new(inode);
4509 t_bucket = ocfs2_xattr_bucket_new(inode);
4510 if (!s_bucket || !t_bucket) {
4511 ret = -ENOMEM;
4512 mlog_errno(ret);
4513 goto out;
4514 }
Tao Ma01225592008-08-18 17:38:53 +08004515
Joel Beckerba937122008-10-24 19:13:20 -07004516 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004517 if (ret) {
4518 mlog_errno(ret);
4519 goto out;
4520 }
4521
Joel Beckerba937122008-10-24 19:13:20 -07004522 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004523 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004524 if (ret) {
4525 mlog_errno(ret);
4526 goto out;
4527 }
4528
Joel Becker784b8162008-10-24 17:33:40 -07004529 /*
4530 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4531 * there's no need to read it.
4532 */
Joel Beckerba937122008-10-24 19:13:20 -07004533 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004534 if (ret) {
4535 mlog_errno(ret);
4536 goto out;
4537 }
4538
Joel Becker2b656c12008-11-25 19:00:15 -08004539 /*
4540 * Hey, if we're overwriting t_bucket, what difference does
4541 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4542 * same part of ocfs2_cp_xattr_bucket().
4543 */
Joel Beckerba937122008-10-24 19:13:20 -07004544 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004545 new_bucket_head ?
4546 OCFS2_JOURNAL_ACCESS_CREATE :
4547 OCFS2_JOURNAL_ACCESS_WRITE);
4548 if (ret) {
4549 mlog_errno(ret);
4550 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004551 }
4552
Joel Beckerba937122008-10-24 19:13:20 -07004553 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004554 count = le16_to_cpu(xh->xh_count);
4555 start = ocfs2_xattr_find_divide_pos(xh);
4556
4557 if (start == count) {
4558 xe = &xh->xh_entries[start-1];
4559
4560 /*
4561 * initialized a new empty bucket here.
4562 * The hash value is set as one larger than
4563 * that of the last entry in the previous bucket.
4564 */
Joel Beckerba937122008-10-24 19:13:20 -07004565 for (i = 0; i < t_bucket->bu_blocks; i++)
4566 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004567
Joel Beckerba937122008-10-24 19:13:20 -07004568 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004569 xh->xh_free_start = cpu_to_le16(blocksize);
4570 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4571 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4572
4573 goto set_num_buckets;
4574 }
4575
Tao Ma01225592008-08-18 17:38:53 +08004576 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004577 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004578
4579 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004580 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004581
4582 /*
4583 * Calculate the total name/value len and xh_free_start for
4584 * the old bucket first.
4585 */
4586 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4587 name_value_len = 0;
4588 for (i = 0; i < start; i++) {
4589 xe = &xh->xh_entries[i];
Joel Becker199799a2009-08-14 19:04:15 -07004590 name_value_len += namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004591 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4592 name_offset = le16_to_cpu(xe->xe_name_offset);
4593 }
4594
4595 /*
4596 * Now begin the modification to the new bucket.
4597 *
4598 * In the new bucket, We just move the xattr entry to the beginning
4599 * and don't touch the name/value. So there will be some holes in the
4600 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4601 * called.
4602 */
4603 xe = &xh->xh_entries[start];
4604 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4605 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004606 (int)((char *)xe - (char *)xh),
4607 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004608 memmove((char *)xh->xh_entries, (char *)xe, len);
4609 xe = &xh->xh_entries[count - start];
4610 len = sizeof(struct ocfs2_xattr_entry) * start;
4611 memset((char *)xe, 0, len);
4612
4613 le16_add_cpu(&xh->xh_count, -start);
4614 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4615
4616 /* Calculate xh_free_start for the new bucket. */
4617 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4618 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4619 xe = &xh->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004620 if (le16_to_cpu(xe->xe_name_offset) <
4621 le16_to_cpu(xh->xh_free_start))
4622 xh->xh_free_start = xe->xe_name_offset;
4623 }
4624
Tao Ma80bcaf32008-10-27 06:06:24 +08004625set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004626 /* set xh->xh_num_buckets for the new xh. */
4627 if (new_bucket_head)
4628 xh->xh_num_buckets = cpu_to_le16(1);
4629 else
4630 xh->xh_num_buckets = 0;
4631
Joel Beckerba937122008-10-24 19:13:20 -07004632 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004633
4634 /* store the first_hash of the new bucket. */
4635 if (first_hash)
4636 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4637
4638 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004639 * Now only update the 1st block of the old bucket. If we
4640 * just added a new empty bucket, there is no need to modify
4641 * it.
Tao Ma01225592008-08-18 17:38:53 +08004642 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004643 if (start == count)
4644 goto out;
4645
Joel Beckerba937122008-10-24 19:13:20 -07004646 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004647 memset(&xh->xh_entries[start], 0,
4648 sizeof(struct ocfs2_xattr_entry) * (count - start));
4649 xh->xh_count = cpu_to_le16(start);
4650 xh->xh_free_start = cpu_to_le16(name_offset);
4651 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4652
Joel Beckerba937122008-10-24 19:13:20 -07004653 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004654
4655out:
Joel Beckerba937122008-10-24 19:13:20 -07004656 ocfs2_xattr_bucket_free(s_bucket);
4657 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004658
4659 return ret;
4660}
4661
4662/*
4663 * Copy xattr from one bucket to another bucket.
4664 *
4665 * The caller must make sure that the journal transaction
4666 * has enough space for journaling.
4667 */
4668static int ocfs2_cp_xattr_bucket(struct inode *inode,
4669 handle_t *handle,
4670 u64 s_blkno,
4671 u64 t_blkno,
4672 int t_is_new)
4673{
Joel Becker4980c6d2008-10-24 18:54:43 -07004674 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004675 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004676
4677 BUG_ON(s_blkno == t_blkno);
4678
4679 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004680 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4681 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004682
Joel Beckerba937122008-10-24 19:13:20 -07004683 s_bucket = ocfs2_xattr_bucket_new(inode);
4684 t_bucket = ocfs2_xattr_bucket_new(inode);
4685 if (!s_bucket || !t_bucket) {
4686 ret = -ENOMEM;
4687 mlog_errno(ret);
4688 goto out;
4689 }
Joel Becker92de1092008-11-25 17:06:40 -08004690
Joel Beckerba937122008-10-24 19:13:20 -07004691 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004692 if (ret)
4693 goto out;
4694
Joel Becker784b8162008-10-24 17:33:40 -07004695 /*
4696 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4697 * there's no need to read it.
4698 */
Joel Beckerba937122008-10-24 19:13:20 -07004699 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004700 if (ret)
4701 goto out;
4702
Joel Becker2b656c12008-11-25 19:00:15 -08004703 /*
4704 * Hey, if we're overwriting t_bucket, what difference does
4705 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004706 * cluster to fill, we came here from
4707 * ocfs2_mv_xattr_buckets(), and it is really new -
4708 * ACCESS_CREATE is required. But we also might have moved data
4709 * out of t_bucket before extending back into it.
4710 * ocfs2_add_new_xattr_bucket() can do this - its call to
4711 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004712 * and copied out the end of the old extent. Then it re-extends
4713 * the old extent back to create space for new xattrs. That's
4714 * how we get here, and the bucket isn't really new.
4715 */
Joel Beckerba937122008-10-24 19:13:20 -07004716 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004717 t_is_new ?
4718 OCFS2_JOURNAL_ACCESS_CREATE :
4719 OCFS2_JOURNAL_ACCESS_WRITE);
4720 if (ret)
4721 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004722
Joel Beckerba937122008-10-24 19:13:20 -07004723 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4724 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004725
4726out:
Joel Beckerba937122008-10-24 19:13:20 -07004727 ocfs2_xattr_bucket_free(t_bucket);
4728 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004729
4730 return ret;
4731}
4732
4733/*
Joel Becker874d65a2008-11-26 13:02:18 -08004734 * src_blk points to the start of an existing extent. last_blk points to
4735 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004736 * extent. We copy the buckets from the cluster at last_blk to the new
4737 * extent. If start_bucket is non-zero, we skip that many buckets before
4738 * we start copying. The new extent's xh_num_buckets gets set to the
4739 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4740 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004741 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004742static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4743 u64 src_blk, u64 last_blk, u64 to_blk,
4744 unsigned int start_bucket,
4745 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004746{
4747 int i, ret, credits;
4748 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004749 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004750 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004751 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004752
Joel Becker874d65a2008-11-26 13:02:18 -08004753 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4754 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004755
Joel Becker54ecb6b2008-11-26 13:18:31 -08004756 BUG_ON(start_bucket >= num_buckets);
4757 if (start_bucket) {
4758 num_buckets -= start_bucket;
4759 last_blk += (start_bucket * blks_per_bucket);
4760 }
4761
Joel Becker15d60922008-11-25 18:36:42 -08004762 /* The first bucket of the original extent */
4763 old_first = ocfs2_xattr_bucket_new(inode);
4764 /* The first bucket of the new extent */
4765 new_first = ocfs2_xattr_bucket_new(inode);
4766 if (!old_first || !new_first) {
4767 ret = -ENOMEM;
4768 mlog_errno(ret);
4769 goto out;
4770 }
4771
Joel Becker874d65a2008-11-26 13:02:18 -08004772 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004773 if (ret) {
4774 mlog_errno(ret);
4775 goto out;
4776 }
4777
Tao Ma01225592008-08-18 17:38:53 +08004778 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004779 * We need to update the first bucket of the old extent and all
4780 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004781 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004782 credits = ((num_buckets + 1) * blks_per_bucket) +
4783 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004784 ret = ocfs2_extend_trans(handle, credits);
4785 if (ret) {
4786 mlog_errno(ret);
4787 goto out;
4788 }
4789
Joel Becker15d60922008-11-25 18:36:42 -08004790 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4791 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004792 if (ret) {
4793 mlog_errno(ret);
4794 goto out;
4795 }
4796
4797 for (i = 0; i < num_buckets; i++) {
4798 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004799 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004800 to_blk + (i * blks_per_bucket),
4801 1);
Tao Ma01225592008-08-18 17:38:53 +08004802 if (ret) {
4803 mlog_errno(ret);
4804 goto out;
4805 }
Tao Ma01225592008-08-18 17:38:53 +08004806 }
4807
Joel Becker15d60922008-11-25 18:36:42 -08004808 /*
4809 * Get the new bucket ready before we dirty anything
4810 * (This actually shouldn't fail, because we already dirtied
4811 * it once in ocfs2_cp_xattr_bucket()).
4812 */
4813 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4814 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004815 mlog_errno(ret);
4816 goto out;
4817 }
Joel Becker15d60922008-11-25 18:36:42 -08004818 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4819 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004820 if (ret) {
4821 mlog_errno(ret);
4822 goto out;
4823 }
4824
Joel Becker15d60922008-11-25 18:36:42 -08004825 /* Now update the headers */
4826 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4827 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004828
Joel Becker15d60922008-11-25 18:36:42 -08004829 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4830 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004831
4832 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004833 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4834
Tao Ma01225592008-08-18 17:38:53 +08004835out:
Joel Becker15d60922008-11-25 18:36:42 -08004836 ocfs2_xattr_bucket_free(new_first);
4837 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004838 return ret;
4839}
4840
4841/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004842 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004843 * This function should only be called when bucket size == cluster size.
4844 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4845 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004846static int ocfs2_divide_xattr_cluster(struct inode *inode,
4847 handle_t *handle,
4848 u64 prev_blk,
4849 u64 new_blk,
4850 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004851{
4852 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004853 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004854
4855 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4856
4857 ret = ocfs2_extend_trans(handle, credits);
4858 if (ret) {
4859 mlog_errno(ret);
4860 return ret;
4861 }
4862
4863 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004864 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4865 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004866}
4867
4868/*
4869 * Move some xattrs from the old cluster to the new one since they are not
4870 * contiguous in ocfs2 xattr tree.
4871 *
4872 * new_blk starts a new separate cluster, and we will move some xattrs from
4873 * prev_blk to it. v_start will be set as the first name hash value in this
4874 * new cluster so that it can be used as e_cpos during tree insertion and
4875 * don't collide with our original b-tree operations. first_bh and header_bh
4876 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4877 * to extend the insert bucket.
4878 *
4879 * The problem is how much xattr should we move to the new one and when should
4880 * we update first_bh and header_bh?
4881 * 1. If cluster size > bucket size, that means the previous cluster has more
4882 * than 1 bucket, so just move half nums of bucket into the new cluster and
4883 * update the first_bh and header_bh if the insert bucket has been moved
4884 * to the new cluster.
4885 * 2. If cluster_size == bucket_size:
4886 * a) If the previous extent rec has more than one cluster and the insert
4887 * place isn't in the last cluster, copy the entire last cluster to the
4888 * new one. This time, we don't need to upate the first_bh and header_bh
4889 * since they will not be moved into the new cluster.
4890 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4891 * the new one. And we set the extend flag to zero if the insert place is
4892 * moved into the new allocated cluster since no extend is needed.
4893 */
4894static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4895 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004896 struct ocfs2_xattr_bucket *first,
4897 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004898 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004899 u32 prev_clusters,
4900 u32 *v_start,
4901 int *extend)
4902{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004903 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004904
4905 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004906 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004907 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004908
Joel Becker41cb8142008-11-26 14:25:21 -08004909 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004910 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4911 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004912 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004913 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004914 prev_clusters,
4915 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004916 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004917 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004918 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004919 /* The start of the last cluster in the first extent */
4920 u64 last_blk = bucket_blkno(first) +
4921 ((prev_clusters - 1) *
4922 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004923
Joel Becker012ee912008-11-26 14:43:31 -08004924 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004925 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004926 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004927 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004928 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004929 if (ret)
4930 mlog_errno(ret);
4931 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004932 ret = ocfs2_divide_xattr_cluster(inode, handle,
4933 last_blk, new_blk,
4934 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004935 if (ret)
4936 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004937
Joel Becker92cf3ad2008-11-26 14:12:09 -08004938 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004939 *extend = 0;
4940 }
4941 }
4942
4943 return ret;
4944}
4945
4946/*
4947 * Add a new cluster for xattr storage.
4948 *
4949 * If the new cluster is contiguous with the previous one, it will be
4950 * appended to the same extent record, and num_clusters will be updated.
4951 * If not, we will insert a new extent for it and move some xattrs in
4952 * the last cluster into the new allocated one.
4953 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4954 * lose the benefits of hashing because we'll have to search large leaves.
4955 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4956 * if it's bigger).
4957 *
4958 * first_bh is the first block of the previous extent rec and header_bh
4959 * indicates the bucket we will insert the new xattrs. They will be updated
4960 * when the header_bh is moved into the new cluster.
4961 */
4962static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4963 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004964 struct ocfs2_xattr_bucket *first,
4965 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004966 u32 *num_clusters,
4967 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004968 int *extend,
4969 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004970{
Tao Ma85db90e2008-11-12 08:27:01 +08004971 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004972 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4973 u32 prev_clusters = *num_clusters;
4974 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4975 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004976 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004977 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004978 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004979
4980 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4981 "previous xattr blkno = %llu\n",
4982 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004983 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004984
Joel Becker5e404e92009-02-13 03:54:22 -08004985 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004986
Joel Becker0cf2f762009-02-12 16:41:25 -08004987 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004988 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004989 if (ret < 0) {
4990 mlog_errno(ret);
4991 goto leave;
4992 }
4993
Tao Ma78f30c32008-11-12 08:27:00 +08004994 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004995 clusters_to_add, &bit_off, &num_bits);
4996 if (ret < 0) {
4997 if (ret != -ENOSPC)
4998 mlog_errno(ret);
4999 goto leave;
5000 }
5001
5002 BUG_ON(num_bits > clusters_to_add);
5003
5004 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5005 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5006 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5007
Joel Beckered29c0c2008-11-26 15:08:44 -08005008 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08005009 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5010 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5011 /*
5012 * If this cluster is contiguous with the old one and
5013 * adding this new cluster, we don't surpass the limit of
5014 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5015 * initialized and used like other buckets in the previous
5016 * cluster.
5017 * So add it as a contiguous one. The caller will handle
5018 * its init process.
5019 */
5020 v_start = prev_cpos + prev_clusters;
5021 *num_clusters = prev_clusters + num_bits;
5022 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5023 num_bits);
5024 } else {
5025 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5026 handle,
Joel Becker012ee912008-11-26 14:43:31 -08005027 first,
5028 target,
Tao Ma01225592008-08-18 17:38:53 +08005029 block,
Tao Ma01225592008-08-18 17:38:53 +08005030 prev_clusters,
5031 &v_start,
5032 extend);
5033 if (ret) {
5034 mlog_errno(ret);
5035 goto leave;
5036 }
5037 }
5038
5039 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07005040 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08005041 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08005042 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005043 if (ret < 0) {
5044 mlog_errno(ret);
5045 goto leave;
5046 }
5047
5048 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08005049 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08005050 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005051
5052leave:
Tao Ma01225592008-08-18 17:38:53 +08005053 return ret;
5054}
5055
5056/*
Joel Becker92de1092008-11-25 17:06:40 -08005057 * We are given an extent. 'first' is the bucket at the very front of
5058 * the extent. The extent has space for an additional bucket past
5059 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5060 * of the target bucket. We wish to shift every bucket past the target
5061 * down one, filling in that additional space. When we get back to the
5062 * target, we split the target between itself and the now-empty bucket
5063 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005064 */
5065static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005066 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08005067 struct ocfs2_xattr_bucket *first,
5068 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08005069 u32 num_clusters)
5070{
5071 int ret, credits;
5072 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5073 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08005074 u64 end_blk;
5075 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08005076
5077 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08005078 "from %llu, len = %u\n", (unsigned long long)target_blk,
5079 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005080
Joel Becker92de1092008-11-25 17:06:40 -08005081 /* The extent must have room for an additional bucket */
5082 BUG_ON(new_bucket >=
5083 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08005084
Joel Becker92de1092008-11-25 17:06:40 -08005085 /* end_blk points to the last existing bucket */
5086 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005087
5088 /*
Joel Becker92de1092008-11-25 17:06:40 -08005089 * end_blk is the start of the last existing bucket.
5090 * Thus, (end_blk - target_blk) covers the target bucket and
5091 * every bucket after it up to, but not including, the last
5092 * existing bucket. Then we add the last existing bucket, the
5093 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005094 */
Joel Becker92de1092008-11-25 17:06:40 -08005095 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08005096 handle->h_buffer_credits;
5097 ret = ocfs2_extend_trans(handle, credits);
5098 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08005099 mlog_errno(ret);
5100 goto out;
5101 }
5102
Joel Becker92de1092008-11-25 17:06:40 -08005103 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5104 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005105 if (ret) {
5106 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005107 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005108 }
5109
Joel Becker92de1092008-11-25 17:06:40 -08005110 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08005111 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5112 end_blk + blk_per_bucket, 0);
5113 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08005114 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005115 end_blk -= blk_per_bucket;
5116 }
5117
Joel Becker92de1092008-11-25 17:06:40 -08005118 /* Move half of the xattr in target_blkno to the next bucket. */
5119 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5120 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08005121
Joel Becker92de1092008-11-25 17:06:40 -08005122 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5123 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08005124
Tao Ma01225592008-08-18 17:38:53 +08005125out:
5126 return ret;
5127}
5128
5129/*
Joel Becker91f20332008-11-26 15:25:41 -08005130 * Add new xattr bucket in an extent record and adjust the buckets
5131 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5132 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08005133 *
Joel Becker91f20332008-11-26 15:25:41 -08005134 * In the easy case, we will move all the buckets after target down by
5135 * one. Half of target's xattrs will be moved to the next bucket.
5136 *
5137 * If current cluster is full, we'll allocate a new one. This may not
5138 * be contiguous. The underlying calls will make sure that there is
5139 * space for the insert, shifting buckets around if necessary.
5140 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08005141 */
5142static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5143 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005144 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08005145 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005146{
Tao Ma01225592008-08-18 17:38:53 +08005147 struct ocfs2_xattr_block *xb =
5148 (struct ocfs2_xattr_block *)xb_bh->b_data;
5149 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5150 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08005151 u32 name_hash =
5152 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08005153 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08005154 int ret, num_buckets, extend = 1;
5155 u64 p_blkno;
5156 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08005157 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08005158 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08005159
Joel Becker91f20332008-11-26 15:25:41 -08005160 mlog(0, "Add new xattr bucket starting from %llu\n",
5161 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08005162
Joel Beckered29c0c2008-11-26 15:08:44 -08005163 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08005164 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08005165 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08005166 ret = -ENOMEM;
5167 mlog_errno(ret);
5168 goto out;
5169 }
5170
Tao Ma01225592008-08-18 17:38:53 +08005171 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5172 &num_clusters, el);
5173 if (ret) {
5174 mlog_errno(ret);
5175 goto out;
5176 }
5177
Joel Beckered29c0c2008-11-26 15:08:44 -08005178 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5179 if (ret) {
5180 mlog_errno(ret);
5181 goto out;
5182 }
5183
Tao Ma01225592008-08-18 17:38:53 +08005184 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08005185 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5186 /*
5187 * This can move first+target if the target bucket moves
5188 * to the new extent.
5189 */
Tao Ma01225592008-08-18 17:38:53 +08005190 ret = ocfs2_add_new_xattr_cluster(inode,
5191 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005192 first,
5193 target,
Tao Ma01225592008-08-18 17:38:53 +08005194 &num_clusters,
5195 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005196 &extend,
5197 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005198 if (ret) {
5199 mlog_errno(ret);
5200 goto out;
5201 }
5202 }
5203
Joel Becker92de1092008-11-25 17:06:40 -08005204 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08005205 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005206 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08005207 first,
5208 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08005209 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08005210 if (ret)
5211 mlog_errno(ret);
5212 }
5213
Tao Ma01225592008-08-18 17:38:53 +08005214out:
Joel Becker92de1092008-11-25 17:06:40 -08005215 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08005216
Tao Ma01225592008-08-18 17:38:53 +08005217 return ret;
5218}
5219
5220static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5221 struct ocfs2_xattr_bucket *bucket,
5222 int offs)
5223{
5224 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5225
5226 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07005227 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08005228}
5229
5230/*
Tao Ma01225592008-08-18 17:38:53 +08005231 * Truncate the specified xe_off entry in xattr bucket.
5232 * bucket is indicated by header_bh and len is the new length.
5233 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5234 *
5235 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5236 */
5237static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08005238 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08005239 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08005240 int len,
5241 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005242{
5243 int ret, offset;
5244 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08005245 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08005246 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08005247 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08005248 struct ocfs2_xattr_value_buf vb = {
5249 .vb_access = ocfs2_journal_access,
5250 };
Tao Ma01225592008-08-18 17:38:53 +08005251
5252 xe = &xh->xh_entries[xe_off];
5253
5254 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5255
5256 offset = le16_to_cpu(xe->xe_name_offset) +
5257 OCFS2_XATTR_SIZE(xe->xe_name_len);
5258
5259 value_blk = offset / blocksize;
5260
5261 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5262 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005263
Joel Beckerb3e5d372008-12-09 15:01:04 -08005264 vb.vb_bh = bucket->bu_bhs[value_blk];
5265 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08005266
Joel Beckerb3e5d372008-12-09 15:01:04 -08005267 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5268 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005269
Joel Becker548b0f22008-11-24 19:32:13 -08005270 /*
5271 * From here on out we have to dirty the bucket. The generic
5272 * value calls only modify one of the bucket's bhs, but we need
5273 * to send the bucket at once. So if they error, they *could* have
5274 * modified something. We have to assume they did, and dirty
5275 * the whole bucket. This leaves us in a consistent state.
5276 */
Tao Ma01225592008-08-18 17:38:53 +08005277 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08005278 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08005279 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005280 if (ret) {
5281 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005282 goto out;
5283 }
5284
5285 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5286 OCFS2_JOURNAL_ACCESS_WRITE);
5287 if (ret) {
5288 mlog_errno(ret);
5289 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005290 }
5291
Joel Becker548b0f22008-11-24 19:32:13 -08005292 xe->xe_value_size = cpu_to_le64(len);
5293
Joel Becker548b0f22008-11-24 19:32:13 -08005294 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005295
5296out:
Tao Ma01225592008-08-18 17:38:53 +08005297 return ret;
5298}
5299
Tao Ma01225592008-08-18 17:38:53 +08005300static int ocfs2_rm_xattr_cluster(struct inode *inode,
5301 struct buffer_head *root_bh,
5302 u64 blkno,
5303 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005304 u32 len,
5305 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005306{
5307 int ret;
5308 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5309 struct inode *tl_inode = osb->osb_tl_inode;
5310 handle_t *handle;
5311 struct ocfs2_xattr_block *xb =
5312 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005313 struct ocfs2_alloc_context *meta_ac = NULL;
5314 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005315 struct ocfs2_extent_tree et;
5316
Tao Ma47bca492009-08-18 11:43:42 +08005317 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005318 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005319 if (ret) {
5320 mlog_errno(ret);
5321 return ret;
5322 }
5323
Joel Becker5e404e92009-02-13 03:54:22 -08005324 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005325
5326 ocfs2_init_dealloc_ctxt(&dealloc);
5327
5328 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5329 cpos, len, (unsigned long long)blkno);
5330
Joel Becker8cb471e2009-02-10 20:00:41 -08005331 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5332 len);
Tao Ma01225592008-08-18 17:38:53 +08005333
Joel Beckerf99b9b72008-08-20 19:36:33 -07005334 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005335 if (ret) {
5336 mlog_errno(ret);
5337 return ret;
5338 }
5339
5340 mutex_lock(&tl_inode->i_mutex);
5341
5342 if (ocfs2_truncate_log_needs_flush(osb)) {
5343 ret = __ocfs2_flush_truncate_log(osb);
5344 if (ret < 0) {
5345 mlog_errno(ret);
5346 goto out;
5347 }
5348 }
5349
Jan Karaa90714c2008-10-09 19:38:40 +02005350 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005351 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005352 ret = -ENOMEM;
5353 mlog_errno(ret);
5354 goto out;
5355 }
5356
Joel Becker0cf2f762009-02-12 16:41:25 -08005357 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005358 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005359 if (ret) {
5360 mlog_errno(ret);
5361 goto out_commit;
5362 }
5363
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005364 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005365 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005366 if (ret) {
5367 mlog_errno(ret);
5368 goto out_commit;
5369 }
5370
5371 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
5372
5373 ret = ocfs2_journal_dirty(handle, root_bh);
5374 if (ret) {
5375 mlog_errno(ret);
5376 goto out_commit;
5377 }
5378
5379 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5380 if (ret)
5381 mlog_errno(ret);
5382
5383out_commit:
5384 ocfs2_commit_trans(osb, handle);
5385out:
5386 ocfs2_schedule_truncate_log_flush(osb, 1);
5387
5388 mutex_unlock(&tl_inode->i_mutex);
5389
5390 if (meta_ac)
5391 ocfs2_free_alloc_context(meta_ac);
5392
5393 ocfs2_run_deallocs(osb, &dealloc);
5394
5395 return ret;
5396}
5397
Tao Ma01225592008-08-18 17:38:53 +08005398/*
Tao Ma80bcaf32008-10-27 06:06:24 +08005399 * check whether the xattr bucket is filled up with the same hash value.
5400 * If we want to insert the xattr with the same hash, return -ENOSPC.
5401 * If we want to insert a xattr with different hash value, go ahead
5402 * and ocfs2_divide_xattr_bucket will handle this.
5403 */
Tao Ma01225592008-08-18 17:38:53 +08005404static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005405 struct ocfs2_xattr_bucket *bucket,
5406 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005407{
Joel Becker3e632942008-10-24 17:04:49 -07005408 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005409 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5410
5411 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5412 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005413
5414 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5415 xh->xh_entries[0].xe_name_hash) {
5416 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5417 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005418 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005419 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5420 return -ENOSPC;
5421 }
5422
5423 return 0;
5424}
5425
Joel Beckerc5d95df2009-08-18 21:03:24 -07005426/*
5427 * Try to set the entry in the current bucket. If we fail, the caller
5428 * will handle getting us another bucket.
5429 */
5430static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5431 struct ocfs2_xattr_info *xi,
5432 struct ocfs2_xattr_search *xs,
5433 struct ocfs2_xattr_set_ctxt *ctxt)
5434{
5435 int ret;
5436 struct ocfs2_xa_loc loc;
5437
5438 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5439
5440 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5441 xs->not_found ? NULL : xs->here);
5442 ret = ocfs2_xa_set(&loc, xi, ctxt);
5443 if (!ret) {
5444 xs->here = loc.xl_entry;
5445 goto out;
5446 }
5447 if (ret != -ENOSPC) {
5448 mlog_errno(ret);
5449 goto out;
5450 }
5451
5452 /* Ok, we need space. Let's try defragmenting the bucket. */
5453 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5454 xs->bucket);
5455 if (ret) {
5456 mlog_errno(ret);
5457 goto out;
5458 }
5459
5460 ret = ocfs2_xa_set(&loc, xi, ctxt);
5461 if (!ret) {
5462 xs->here = loc.xl_entry;
5463 goto out;
5464 }
5465 if (ret != -ENOSPC)
5466 mlog_errno(ret);
5467
5468
5469out:
5470 mlog_exit(ret);
5471 return ret;
5472}
5473
Tao Ma01225592008-08-18 17:38:53 +08005474static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5475 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005476 struct ocfs2_xattr_search *xs,
5477 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005478{
Joel Beckerc5d95df2009-08-18 21:03:24 -07005479 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005480
Joel Becker6b240ff2009-08-14 18:02:52 -07005481 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005482
Joel Beckerc5d95df2009-08-18 21:03:24 -07005483 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5484 if (!ret)
5485 goto out;
5486 if (ret != -ENOSPC) {
5487 mlog_errno(ret);
5488 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005489 }
5490
Joel Beckerc5d95df2009-08-18 21:03:24 -07005491 /* Ack, need more space. Let's try to get another bucket! */
5492
Tao Ma01225592008-08-18 17:38:53 +08005493 /*
Joel Beckerc5d95df2009-08-18 21:03:24 -07005494 * We do not allow for overlapping ranges between buckets. And
5495 * the maximum number of collisions we will allow for then is
5496 * one bucket's worth, so check it here whether we need to
5497 * add a new bucket for the insert.
Tao Ma01225592008-08-18 17:38:53 +08005498 */
Joel Beckerc5d95df2009-08-18 21:03:24 -07005499 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Becker91f20332008-11-26 15:25:41 -08005500 xs->bucket,
Joel Beckerc5d95df2009-08-18 21:03:24 -07005501 xi->xi_name);
5502 if (ret) {
5503 mlog_errno(ret);
5504 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005505 }
5506
Joel Beckerc5d95df2009-08-18 21:03:24 -07005507 ret = ocfs2_add_new_xattr_bucket(inode,
5508 xs->xattr_bh,
5509 xs->bucket,
5510 ctxt);
5511 if (ret) {
5512 mlog_errno(ret);
5513 goto out;
5514 }
5515
5516 /*
5517 * ocfs2_add_new_xattr_bucket() will have updated
5518 * xs->bucket if it moved, but it will not have updated
5519 * any of the other search fields. Thus, we drop it and
5520 * re-search. Everything should be cached, so it'll be
5521 * quick.
5522 */
5523 ocfs2_xattr_bucket_relse(xs->bucket);
5524 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5525 xi->xi_name_index,
5526 xi->xi_name, xs);
5527 if (ret && ret != -ENODATA)
5528 goto out;
5529 xs->not_found = ret;
5530
5531 /* Ok, we have a new bucket, let's try again */
5532 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5533 if (ret && (ret != -ENOSPC))
5534 mlog_errno(ret);
5535
Tao Ma01225592008-08-18 17:38:53 +08005536out:
5537 mlog_exit(ret);
5538 return ret;
5539}
Tao Maa3944252008-08-18 17:38:54 +08005540
5541static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5542 struct ocfs2_xattr_bucket *bucket,
5543 void *para)
5544{
Tao Mace9c5a52009-08-18 11:43:59 +08005545 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005546 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005547 u16 i;
5548 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5550 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005551 int credits = ocfs2_remove_extent_credits(osb->sb) +
5552 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005553 struct ocfs2_xattr_value_root *xv;
5554 struct ocfs2_rm_xattr_bucket_para *args =
5555 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005556
5557 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005558
5559 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5560 xe = &xh->xh_entries[i];
5561 if (ocfs2_xattr_is_local(xe))
5562 continue;
5563
Tao Mace9c5a52009-08-18 11:43:59 +08005564 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5565 i, &xv, NULL);
5566
5567 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5568 args->ref_ci,
5569 args->ref_root_bh,
5570 &ctxt.meta_ac,
5571 &ref_credits);
5572
5573 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005574 if (IS_ERR(ctxt.handle)) {
5575 ret = PTR_ERR(ctxt.handle);
5576 mlog_errno(ret);
5577 break;
5578 }
5579
Joel Becker548b0f22008-11-24 19:32:13 -08005580 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005581 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005582
5583 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005584 if (ctxt.meta_ac) {
5585 ocfs2_free_alloc_context(ctxt.meta_ac);
5586 ctxt.meta_ac = NULL;
5587 }
Tao Maa3944252008-08-18 17:38:54 +08005588 if (ret) {
5589 mlog_errno(ret);
5590 break;
5591 }
5592 }
5593
Tao Mace9c5a52009-08-18 11:43:59 +08005594 if (ctxt.meta_ac)
5595 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005596 ocfs2_schedule_truncate_log_flush(osb, 1);
5597 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005598 return ret;
5599}
5600
Mark Fasheh99219ae2008-10-07 14:52:59 -07005601/*
Tao Ma492a8a32009-08-18 11:43:17 +08005602 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5603 * or change the extent record flag), we need to recalculate
5604 * the metaecc for the whole bucket. So it is done here.
5605 *
5606 * Note:
5607 * We have to give the extra credits for the caller.
5608 */
5609static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5610 handle_t *handle,
5611 void *para)
5612{
5613 int ret;
5614 struct ocfs2_xattr_bucket *bucket =
5615 (struct ocfs2_xattr_bucket *)para;
5616
5617 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5618 OCFS2_JOURNAL_ACCESS_WRITE);
5619 if (ret) {
5620 mlog_errno(ret);
5621 return ret;
5622 }
5623
5624 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5625
5626 return 0;
5627}
5628
5629/*
5630 * Special action we need if the xattr value is refcounted.
5631 *
5632 * 1. If the xattr is refcounted, lock the tree.
5633 * 2. CoW the xattr if we are setting the new value and the value
5634 * will be stored outside.
5635 * 3. In other case, decrease_refcount will work for us, so just
5636 * lock the refcount tree, calculate the meta and credits is OK.
5637 *
5638 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5639 * currently CoW is a completed transaction, while this function
5640 * will also lock the allocators and let us deadlock. So we will
5641 * CoW the whole xattr value.
5642 */
5643static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5644 struct ocfs2_dinode *di,
5645 struct ocfs2_xattr_info *xi,
5646 struct ocfs2_xattr_search *xis,
5647 struct ocfs2_xattr_search *xbs,
5648 struct ocfs2_refcount_tree **ref_tree,
5649 int *meta_add,
5650 int *credits)
5651{
5652 int ret = 0;
5653 struct ocfs2_xattr_block *xb;
5654 struct ocfs2_xattr_entry *xe;
5655 char *base;
5656 u32 p_cluster, num_clusters;
5657 unsigned int ext_flags;
5658 int name_offset, name_len;
5659 struct ocfs2_xattr_value_buf vb;
5660 struct ocfs2_xattr_bucket *bucket = NULL;
5661 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5662 struct ocfs2_post_refcount refcount;
5663 struct ocfs2_post_refcount *p = NULL;
5664 struct buffer_head *ref_root_bh = NULL;
5665
5666 if (!xis->not_found) {
5667 xe = xis->here;
5668 name_offset = le16_to_cpu(xe->xe_name_offset);
5669 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5670 base = xis->base;
5671 vb.vb_bh = xis->inode_bh;
5672 vb.vb_access = ocfs2_journal_access_di;
5673 } else {
5674 int i, block_off = 0;
5675 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5676 xe = xbs->here;
5677 name_offset = le16_to_cpu(xe->xe_name_offset);
5678 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5679 i = xbs->here - xbs->header->xh_entries;
5680
5681 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005682 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005683 bucket_xh(xbs->bucket),
5684 i, &block_off,
5685 &name_offset);
5686 if (ret) {
5687 mlog_errno(ret);
5688 goto out;
5689 }
5690 base = bucket_block(xbs->bucket, block_off);
5691 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5692 vb.vb_access = ocfs2_journal_access;
5693
5694 if (ocfs2_meta_ecc(osb)) {
5695 /*create parameters for ocfs2_post_refcount. */
5696 bucket = xbs->bucket;
5697 refcount.credits = bucket->bu_blocks;
5698 refcount.para = bucket;
5699 refcount.func =
5700 ocfs2_xattr_bucket_post_refcount;
5701 p = &refcount;
5702 }
5703 } else {
5704 base = xbs->base;
5705 vb.vb_bh = xbs->xattr_bh;
5706 vb.vb_access = ocfs2_journal_access_xb;
5707 }
5708 }
5709
5710 if (ocfs2_xattr_is_local(xe))
5711 goto out;
5712
5713 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5714 (base + name_offset + name_len);
5715
5716 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5717 &num_clusters, &vb.vb_xv->xr_list,
5718 &ext_flags);
5719 if (ret) {
5720 mlog_errno(ret);
5721 goto out;
5722 }
5723
5724 /*
5725 * We just need to check the 1st extent record, since we always
5726 * CoW the whole xattr. So there shouldn't be a xattr with
5727 * some REFCOUNT extent recs after the 1st one.
5728 */
5729 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5730 goto out;
5731
5732 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5733 1, ref_tree, &ref_root_bh);
5734 if (ret) {
5735 mlog_errno(ret);
5736 goto out;
5737 }
5738
5739 /*
5740 * If we are deleting the xattr or the new size will be stored inside,
5741 * cool, leave it there, the xattr truncate process will remove them
5742 * for us(it still needs the refcount tree lock and the meta, credits).
5743 * And the worse case is that every cluster truncate will split the
5744 * refcount tree, and make the original extent become 3. So we will need
5745 * 2 * cluster more extent recs at most.
5746 */
Joel Becker6b240ff2009-08-14 18:02:52 -07005747 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
Tao Ma492a8a32009-08-18 11:43:17 +08005748
5749 ret = ocfs2_refcounted_xattr_delete_need(inode,
5750 &(*ref_tree)->rf_ci,
5751 ref_root_bh, vb.vb_xv,
5752 meta_add, credits);
5753 if (ret)
5754 mlog_errno(ret);
5755 goto out;
5756 }
5757
5758 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5759 *ref_tree, ref_root_bh, 0,
5760 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5761 if (ret)
5762 mlog_errno(ret);
5763
5764out:
5765 brelse(ref_root_bh);
5766 return ret;
5767}
5768
5769/*
Tao Ma01292412009-09-21 13:04:19 +08005770 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5771 * The physical clusters will be added to refcount tree.
5772 */
5773static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5774 struct ocfs2_xattr_value_root *xv,
5775 struct ocfs2_extent_tree *value_et,
5776 struct ocfs2_caching_info *ref_ci,
5777 struct buffer_head *ref_root_bh,
5778 struct ocfs2_cached_dealloc_ctxt *dealloc,
5779 struct ocfs2_post_refcount *refcount)
5780{
5781 int ret = 0;
5782 u32 clusters = le32_to_cpu(xv->xr_clusters);
5783 u32 cpos, p_cluster, num_clusters;
5784 struct ocfs2_extent_list *el = &xv->xr_list;
5785 unsigned int ext_flags;
5786
5787 cpos = 0;
5788 while (cpos < clusters) {
5789 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5790 &num_clusters, el, &ext_flags);
5791
5792 cpos += num_clusters;
5793 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5794 continue;
5795
5796 BUG_ON(!p_cluster);
5797
5798 ret = ocfs2_add_refcount_flag(inode, value_et,
5799 ref_ci, ref_root_bh,
5800 cpos - num_clusters,
5801 p_cluster, num_clusters,
5802 dealloc, refcount);
5803 if (ret) {
5804 mlog_errno(ret);
5805 break;
5806 }
5807 }
5808
5809 return ret;
5810}
5811
5812/*
5813 * Given a normal ocfs2_xattr_header, refcount all the entries which
5814 * have value stored outside.
5815 * Used for xattrs stored in inode and ocfs2_xattr_block.
5816 */
5817static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5818 struct ocfs2_xattr_value_buf *vb,
5819 struct ocfs2_xattr_header *header,
5820 struct ocfs2_caching_info *ref_ci,
5821 struct buffer_head *ref_root_bh,
5822 struct ocfs2_cached_dealloc_ctxt *dealloc)
5823{
5824
5825 struct ocfs2_xattr_entry *xe;
5826 struct ocfs2_xattr_value_root *xv;
5827 struct ocfs2_extent_tree et;
5828 int i, ret = 0;
5829
5830 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5831 xe = &header->xh_entries[i];
5832
5833 if (ocfs2_xattr_is_local(xe))
5834 continue;
5835
5836 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5837 le16_to_cpu(xe->xe_name_offset) +
5838 OCFS2_XATTR_SIZE(xe->xe_name_len));
5839
5840 vb->vb_xv = xv;
5841 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5842
5843 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5844 ref_ci, ref_root_bh,
5845 dealloc, NULL);
5846 if (ret) {
5847 mlog_errno(ret);
5848 break;
5849 }
5850 }
5851
5852 return ret;
5853}
5854
5855static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5856 struct buffer_head *fe_bh,
5857 struct ocfs2_caching_info *ref_ci,
5858 struct buffer_head *ref_root_bh,
5859 struct ocfs2_cached_dealloc_ctxt *dealloc)
5860{
5861 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5862 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5863 (fe_bh->b_data + inode->i_sb->s_blocksize -
5864 le16_to_cpu(di->i_xattr_inline_size));
5865 struct ocfs2_xattr_value_buf vb = {
5866 .vb_bh = fe_bh,
5867 .vb_access = ocfs2_journal_access_di,
5868 };
5869
5870 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5871 ref_ci, ref_root_bh, dealloc);
5872}
5873
5874struct ocfs2_xattr_tree_value_refcount_para {
5875 struct ocfs2_caching_info *ref_ci;
5876 struct buffer_head *ref_root_bh;
5877 struct ocfs2_cached_dealloc_ctxt *dealloc;
5878};
5879
5880static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5881 struct ocfs2_xattr_bucket *bucket,
5882 int offset,
5883 struct ocfs2_xattr_value_root **xv,
5884 struct buffer_head **bh)
5885{
5886 int ret, block_off, name_offset;
5887 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5888 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5889 void *base;
5890
5891 ret = ocfs2_xattr_bucket_get_name_value(sb,
5892 bucket_xh(bucket),
5893 offset,
5894 &block_off,
5895 &name_offset);
5896 if (ret) {
5897 mlog_errno(ret);
5898 goto out;
5899 }
5900
5901 base = bucket_block(bucket, block_off);
5902
5903 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5904 OCFS2_XATTR_SIZE(xe->xe_name_len));
5905
5906 if (bh)
5907 *bh = bucket->bu_bhs[block_off];
5908out:
5909 return ret;
5910}
5911
5912/*
5913 * For a given xattr bucket, refcount all the entries which
5914 * have value stored outside.
5915 */
5916static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
5917 struct ocfs2_xattr_bucket *bucket,
5918 void *para)
5919{
5920 int i, ret = 0;
5921 struct ocfs2_extent_tree et;
5922 struct ocfs2_xattr_tree_value_refcount_para *ref =
5923 (struct ocfs2_xattr_tree_value_refcount_para *)para;
5924 struct ocfs2_xattr_header *xh =
5925 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
5926 struct ocfs2_xattr_entry *xe;
5927 struct ocfs2_xattr_value_buf vb = {
5928 .vb_access = ocfs2_journal_access,
5929 };
5930 struct ocfs2_post_refcount refcount = {
5931 .credits = bucket->bu_blocks,
5932 .para = bucket,
5933 .func = ocfs2_xattr_bucket_post_refcount,
5934 };
5935 struct ocfs2_post_refcount *p = NULL;
5936
5937 /* We only need post_refcount if we support metaecc. */
5938 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
5939 p = &refcount;
5940
5941 mlog(0, "refcount bucket %llu, count = %u\n",
5942 (unsigned long long)bucket_blkno(bucket),
5943 le16_to_cpu(xh->xh_count));
5944 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5945 xe = &xh->xh_entries[i];
5946
5947 if (ocfs2_xattr_is_local(xe))
5948 continue;
5949
5950 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
5951 &vb.vb_xv, &vb.vb_bh);
5952 if (ret) {
5953 mlog_errno(ret);
5954 break;
5955 }
5956
5957 ocfs2_init_xattr_value_extent_tree(&et,
5958 INODE_CACHE(inode), &vb);
5959
5960 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
5961 &et, ref->ref_ci,
5962 ref->ref_root_bh,
5963 ref->dealloc, p);
5964 if (ret) {
5965 mlog_errno(ret);
5966 break;
5967 }
5968 }
5969
5970 return ret;
5971
5972}
5973
5974static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
5975 struct buffer_head *root_bh,
5976 u64 blkno, u32 cpos, u32 len, void *para)
5977{
5978 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
5979 ocfs2_xattr_bucket_value_refcount,
5980 para);
5981}
5982
5983static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
5984 struct buffer_head *blk_bh,
5985 struct ocfs2_caching_info *ref_ci,
5986 struct buffer_head *ref_root_bh,
5987 struct ocfs2_cached_dealloc_ctxt *dealloc)
5988{
5989 int ret = 0;
5990 struct ocfs2_xattr_block *xb =
5991 (struct ocfs2_xattr_block *)blk_bh->b_data;
5992
5993 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
5994 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
5995 struct ocfs2_xattr_value_buf vb = {
5996 .vb_bh = blk_bh,
5997 .vb_access = ocfs2_journal_access_xb,
5998 };
5999
6000 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6001 ref_ci, ref_root_bh,
6002 dealloc);
6003 } else {
6004 struct ocfs2_xattr_tree_value_refcount_para para = {
6005 .ref_ci = ref_ci,
6006 .ref_root_bh = ref_root_bh,
6007 .dealloc = dealloc,
6008 };
6009
6010 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6011 ocfs2_refcount_xattr_tree_rec,
6012 &para);
6013 }
6014
6015 return ret;
6016}
6017
6018int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6019 struct buffer_head *fe_bh,
6020 struct ocfs2_caching_info *ref_ci,
6021 struct buffer_head *ref_root_bh,
6022 struct ocfs2_cached_dealloc_ctxt *dealloc)
6023{
6024 int ret = 0;
6025 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6026 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6027 struct buffer_head *blk_bh = NULL;
6028
6029 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6030 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6031 ref_ci, ref_root_bh,
6032 dealloc);
6033 if (ret) {
6034 mlog_errno(ret);
6035 goto out;
6036 }
6037 }
6038
6039 if (!di->i_xattr_loc)
6040 goto out;
6041
6042 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6043 &blk_bh);
6044 if (ret < 0) {
6045 mlog_errno(ret);
6046 goto out;
6047 }
6048
6049 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6050 ref_root_bh, dealloc);
6051 if (ret)
6052 mlog_errno(ret);
6053
6054 brelse(blk_bh);
6055out:
6056
6057 return ret;
6058}
6059
Tao Ma0fe9b662009-08-18 11:47:56 +08006060typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006061/*
Tao Ma2999d122009-08-18 11:43:55 +08006062 * Store the information we need in xattr reflink.
6063 * old_bh and new_bh are inode bh for the old and new inode.
6064 */
6065struct ocfs2_xattr_reflink {
6066 struct inode *old_inode;
6067 struct inode *new_inode;
6068 struct buffer_head *old_bh;
6069 struct buffer_head *new_bh;
6070 struct ocfs2_caching_info *ref_ci;
6071 struct buffer_head *ref_root_bh;
6072 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006073 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006074};
6075
6076/*
6077 * Given a xattr header and xe offset,
6078 * return the proper xv and the corresponding bh.
6079 * xattr in inode, block and xattr tree have different implementaions.
6080 */
6081typedef int (get_xattr_value_root)(struct super_block *sb,
6082 struct buffer_head *bh,
6083 struct ocfs2_xattr_header *xh,
6084 int offset,
6085 struct ocfs2_xattr_value_root **xv,
6086 struct buffer_head **ret_bh,
6087 void *para);
6088
6089/*
6090 * Calculate all the xattr value root metadata stored in this xattr header and
6091 * credits we need if we create them from the scratch.
6092 * We use get_xattr_value_root so that all types of xattr container can use it.
6093 */
6094static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6095 struct buffer_head *bh,
6096 struct ocfs2_xattr_header *xh,
6097 int *metas, int *credits,
6098 int *num_recs,
6099 get_xattr_value_root *func,
6100 void *para)
6101{
6102 int i, ret = 0;
6103 struct ocfs2_xattr_value_root *xv;
6104 struct ocfs2_xattr_entry *xe;
6105
6106 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6107 xe = &xh->xh_entries[i];
6108 if (ocfs2_xattr_is_local(xe))
6109 continue;
6110
6111 ret = func(sb, bh, xh, i, &xv, NULL, para);
6112 if (ret) {
6113 mlog_errno(ret);
6114 break;
6115 }
6116
6117 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6118 le16_to_cpu(xv->xr_list.l_next_free_rec);
6119
6120 *credits += ocfs2_calc_extend_credits(sb,
6121 &def_xv.xv.xr_list,
6122 le32_to_cpu(xv->xr_clusters));
6123
6124 /*
6125 * If the value is a tree with depth > 1, We don't go deep
6126 * to the extent block, so just calculate a maximum record num.
6127 */
6128 if (!xv->xr_list.l_tree_depth)
Tao Ma8ff6af82009-12-23 14:31:15 +08006129 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
Tao Ma2999d122009-08-18 11:43:55 +08006130 else
6131 *num_recs += ocfs2_clusters_for_bytes(sb,
6132 XATTR_SIZE_MAX);
6133 }
6134
6135 return ret;
6136}
6137
6138/* Used by xattr inode and block to return the right xv and buffer_head. */
6139static int ocfs2_get_xattr_value_root(struct super_block *sb,
6140 struct buffer_head *bh,
6141 struct ocfs2_xattr_header *xh,
6142 int offset,
6143 struct ocfs2_xattr_value_root **xv,
6144 struct buffer_head **ret_bh,
6145 void *para)
6146{
6147 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6148
6149 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6150 le16_to_cpu(xe->xe_name_offset) +
6151 OCFS2_XATTR_SIZE(xe->xe_name_len));
6152
6153 if (ret_bh)
6154 *ret_bh = bh;
6155
6156 return 0;
6157}
6158
6159/*
6160 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6161 * It is only used for inline xattr and xattr block.
6162 */
6163static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6164 struct ocfs2_xattr_header *xh,
6165 struct buffer_head *ref_root_bh,
6166 int *credits,
6167 struct ocfs2_alloc_context **meta_ac)
6168{
6169 int ret, meta_add = 0, num_recs = 0;
6170 struct ocfs2_refcount_block *rb =
6171 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6172
6173 *credits = 0;
6174
6175 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6176 &meta_add, credits, &num_recs,
6177 ocfs2_get_xattr_value_root,
6178 NULL);
6179 if (ret) {
6180 mlog_errno(ret);
6181 goto out;
6182 }
6183
6184 /*
6185 * We need to add/modify num_recs in refcount tree, so just calculate
6186 * an approximate number we need for refcount tree change.
6187 * Sometimes we need to split the tree, and after split, half recs
6188 * will be moved to the new block, and a new block can only provide
6189 * half number of recs. So we multiple new blocks by 2.
6190 */
6191 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6192 meta_add += num_recs;
6193 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6194 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6195 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6196 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6197 else
6198 *credits += 1;
6199
6200 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6201 if (ret)
6202 mlog_errno(ret);
6203
6204out:
6205 return ret;
6206}
6207
6208/*
6209 * Given a xattr header, reflink all the xattrs in this container.
6210 * It can be used for inode, block and bucket.
6211 *
6212 * NOTE:
6213 * Before we call this function, the caller has memcpy the xattr in
6214 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006215 *
6216 * If args.xattr_reflinked is set, call it to decide whether the xe should
6217 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006218 */
6219static int ocfs2_reflink_xattr_header(handle_t *handle,
6220 struct ocfs2_xattr_reflink *args,
6221 struct buffer_head *old_bh,
6222 struct ocfs2_xattr_header *xh,
6223 struct buffer_head *new_bh,
6224 struct ocfs2_xattr_header *new_xh,
6225 struct ocfs2_xattr_value_buf *vb,
6226 struct ocfs2_alloc_context *meta_ac,
6227 get_xattr_value_root *func,
6228 void *para)
6229{
Tao Ma0fe9b662009-08-18 11:47:56 +08006230 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006231 struct super_block *sb = args->old_inode->i_sb;
6232 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006233 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006234 struct ocfs2_xattr_value_root *xv, *new_xv;
6235 struct ocfs2_extent_tree data_et;
6236 u32 clusters, cpos, p_cluster, num_clusters;
6237 unsigned int ext_flags = 0;
6238
6239 mlog(0, "reflink xattr in container %llu, count = %u\n",
6240 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006241
6242 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6243 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006244 xe = &xh->xh_entries[i];
6245
Tao Ma0fe9b662009-08-18 11:47:56 +08006246 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6247 xe = &new_xh->xh_entries[j];
6248
6249 le16_add_cpu(&new_xh->xh_count, -1);
6250 if (new_xh->xh_count) {
6251 memmove(xe, xe + 1,
6252 (void *)last - (void *)xe);
6253 memset(last, 0,
6254 sizeof(struct ocfs2_xattr_entry));
6255 }
6256
6257 /*
6258 * We don't want j to increase in the next round since
6259 * it is already moved ahead.
6260 */
6261 j--;
6262 continue;
6263 }
6264
Tao Ma2999d122009-08-18 11:43:55 +08006265 if (ocfs2_xattr_is_local(xe))
6266 continue;
6267
6268 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6269 if (ret) {
6270 mlog_errno(ret);
6271 break;
6272 }
6273
Tao Ma0fe9b662009-08-18 11:47:56 +08006274 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006275 if (ret) {
6276 mlog_errno(ret);
6277 break;
6278 }
6279
6280 /*
6281 * For the xattr which has l_tree_depth = 0, all the extent
6282 * recs have already be copied to the new xh with the
6283 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6284 * increase the refount count int the refcount tree.
6285 *
6286 * For the xattr which has l_tree_depth > 0, we need
6287 * to initialize it to the empty default value root,
6288 * and then insert the extents one by one.
6289 */
6290 if (xv->xr_list.l_tree_depth) {
6291 memcpy(new_xv, &def_xv, sizeof(def_xv));
6292 vb->vb_xv = new_xv;
6293 vb->vb_bh = value_bh;
6294 ocfs2_init_xattr_value_extent_tree(&data_et,
6295 INODE_CACHE(args->new_inode), vb);
6296 }
6297
6298 clusters = le32_to_cpu(xv->xr_clusters);
6299 cpos = 0;
6300 while (cpos < clusters) {
6301 ret = ocfs2_xattr_get_clusters(args->old_inode,
6302 cpos,
6303 &p_cluster,
6304 &num_clusters,
6305 &xv->xr_list,
6306 &ext_flags);
6307 if (ret) {
6308 mlog_errno(ret);
6309 goto out;
6310 }
6311
6312 BUG_ON(!p_cluster);
6313
6314 if (xv->xr_list.l_tree_depth) {
6315 ret = ocfs2_insert_extent(handle,
6316 &data_et, cpos,
6317 ocfs2_clusters_to_blocks(
6318 args->old_inode->i_sb,
6319 p_cluster),
6320 num_clusters, ext_flags,
6321 meta_ac);
6322 if (ret) {
6323 mlog_errno(ret);
6324 goto out;
6325 }
6326 }
6327
6328 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6329 args->ref_root_bh,
6330 p_cluster, num_clusters,
6331 meta_ac, args->dealloc);
6332 if (ret) {
6333 mlog_errno(ret);
6334 goto out;
6335 }
6336
6337 cpos += num_clusters;
6338 }
6339 }
6340
6341out:
6342 return ret;
6343}
6344
6345static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6346{
6347 int ret = 0, credits = 0;
6348 handle_t *handle;
6349 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6350 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6351 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6352 int header_off = osb->sb->s_blocksize - inline_size;
6353 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6354 (args->old_bh->b_data + header_off);
6355 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6356 (args->new_bh->b_data + header_off);
6357 struct ocfs2_alloc_context *meta_ac = NULL;
6358 struct ocfs2_inode_info *new_oi;
6359 struct ocfs2_dinode *new_di;
6360 struct ocfs2_xattr_value_buf vb = {
6361 .vb_bh = args->new_bh,
6362 .vb_access = ocfs2_journal_access_di,
6363 };
6364
6365 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6366 &credits, &meta_ac);
6367 if (ret) {
6368 mlog_errno(ret);
6369 goto out;
6370 }
6371
6372 handle = ocfs2_start_trans(osb, credits);
6373 if (IS_ERR(handle)) {
6374 ret = PTR_ERR(handle);
6375 mlog_errno(ret);
6376 goto out;
6377 }
6378
6379 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6380 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6381 if (ret) {
6382 mlog_errno(ret);
6383 goto out_commit;
6384 }
6385
6386 memcpy(args->new_bh->b_data + header_off,
6387 args->old_bh->b_data + header_off, inline_size);
6388
6389 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6390 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6391
6392 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6393 args->new_bh, new_xh, &vb, meta_ac,
6394 ocfs2_get_xattr_value_root, NULL);
6395 if (ret) {
6396 mlog_errno(ret);
6397 goto out_commit;
6398 }
6399
6400 new_oi = OCFS2_I(args->new_inode);
6401 spin_lock(&new_oi->ip_lock);
6402 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6403 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6404 spin_unlock(&new_oi->ip_lock);
6405
6406 ocfs2_journal_dirty(handle, args->new_bh);
6407
6408out_commit:
6409 ocfs2_commit_trans(osb, handle);
6410
6411out:
6412 if (meta_ac)
6413 ocfs2_free_alloc_context(meta_ac);
6414 return ret;
6415}
6416
6417static int ocfs2_create_empty_xattr_block(struct inode *inode,
6418 struct buffer_head *fe_bh,
6419 struct buffer_head **ret_bh,
6420 int indexed)
6421{
6422 int ret;
Tao Ma2999d122009-08-18 11:43:55 +08006423 struct ocfs2_alloc_context *meta_ac;
6424 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerd3981542009-08-19 02:13:50 -07006425 struct ocfs2_xattr_set_ctxt ctxt = {
6426 .meta_ac = meta_ac,
6427 };
Tao Ma2999d122009-08-18 11:43:55 +08006428
6429 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
6430 if (ret < 0) {
6431 mlog_errno(ret);
6432 return ret;
6433 }
6434
Joel Beckerd3981542009-08-19 02:13:50 -07006435 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6436 if (IS_ERR(ctxt.handle)) {
6437 ret = PTR_ERR(ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006438 mlog_errno(ret);
6439 goto out;
6440 }
6441
6442 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6443 (unsigned long long)fe_bh->b_blocknr, indexed);
Joel Beckerd3981542009-08-19 02:13:50 -07006444 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6445 ret_bh);
Tao Ma2999d122009-08-18 11:43:55 +08006446 if (ret)
6447 mlog_errno(ret);
6448
Joel Beckerd3981542009-08-19 02:13:50 -07006449 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006450out:
6451 ocfs2_free_alloc_context(meta_ac);
6452 return ret;
6453}
6454
6455static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6456 struct buffer_head *blk_bh,
6457 struct buffer_head *new_blk_bh)
6458{
6459 int ret = 0, credits = 0;
6460 handle_t *handle;
6461 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6462 struct ocfs2_dinode *new_di;
6463 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6464 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6465 struct ocfs2_xattr_block *xb =
6466 (struct ocfs2_xattr_block *)blk_bh->b_data;
6467 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6468 struct ocfs2_xattr_block *new_xb =
6469 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6470 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6471 struct ocfs2_alloc_context *meta_ac;
6472 struct ocfs2_xattr_value_buf vb = {
6473 .vb_bh = new_blk_bh,
6474 .vb_access = ocfs2_journal_access_xb,
6475 };
6476
6477 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6478 &credits, &meta_ac);
6479 if (ret) {
6480 mlog_errno(ret);
6481 return ret;
6482 }
6483
6484 /* One more credits in case we need to add xattr flags in new inode. */
6485 handle = ocfs2_start_trans(osb, credits + 1);
6486 if (IS_ERR(handle)) {
6487 ret = PTR_ERR(handle);
6488 mlog_errno(ret);
6489 goto out;
6490 }
6491
6492 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6493 ret = ocfs2_journal_access_di(handle,
6494 INODE_CACHE(args->new_inode),
6495 args->new_bh,
6496 OCFS2_JOURNAL_ACCESS_WRITE);
6497 if (ret) {
6498 mlog_errno(ret);
6499 goto out_commit;
6500 }
6501 }
6502
6503 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6504 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6505 if (ret) {
6506 mlog_errno(ret);
6507 goto out_commit;
6508 }
6509
6510 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6511 osb->sb->s_blocksize - header_off);
6512
6513 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6514 new_blk_bh, new_xh, &vb, meta_ac,
6515 ocfs2_get_xattr_value_root, NULL);
6516 if (ret) {
6517 mlog_errno(ret);
6518 goto out_commit;
6519 }
6520
6521 ocfs2_journal_dirty(handle, new_blk_bh);
6522
6523 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6524 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6525 spin_lock(&new_oi->ip_lock);
6526 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6527 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6528 spin_unlock(&new_oi->ip_lock);
6529
6530 ocfs2_journal_dirty(handle, args->new_bh);
6531 }
6532
6533out_commit:
6534 ocfs2_commit_trans(osb, handle);
6535
6536out:
6537 ocfs2_free_alloc_context(meta_ac);
6538 return ret;
6539}
6540
6541struct ocfs2_reflink_xattr_tree_args {
6542 struct ocfs2_xattr_reflink *reflink;
6543 struct buffer_head *old_blk_bh;
6544 struct buffer_head *new_blk_bh;
6545 struct ocfs2_xattr_bucket *old_bucket;
6546 struct ocfs2_xattr_bucket *new_bucket;
6547};
6548
6549/*
6550 * NOTE:
6551 * We have to handle the case that both old bucket and new bucket
6552 * will call this function to get the right ret_bh.
6553 * So The caller must give us the right bh.
6554 */
6555static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6556 struct buffer_head *bh,
6557 struct ocfs2_xattr_header *xh,
6558 int offset,
6559 struct ocfs2_xattr_value_root **xv,
6560 struct buffer_head **ret_bh,
6561 void *para)
6562{
6563 struct ocfs2_reflink_xattr_tree_args *args =
6564 (struct ocfs2_reflink_xattr_tree_args *)para;
6565 struct ocfs2_xattr_bucket *bucket;
6566
6567 if (bh == args->old_bucket->bu_bhs[0])
6568 bucket = args->old_bucket;
6569 else
6570 bucket = args->new_bucket;
6571
6572 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6573 xv, ret_bh);
6574}
6575
6576struct ocfs2_value_tree_metas {
6577 int num_metas;
6578 int credits;
6579 int num_recs;
6580};
6581
6582static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6583 struct buffer_head *bh,
6584 struct ocfs2_xattr_header *xh,
6585 int offset,
6586 struct ocfs2_xattr_value_root **xv,
6587 struct buffer_head **ret_bh,
6588 void *para)
6589{
6590 struct ocfs2_xattr_bucket *bucket =
6591 (struct ocfs2_xattr_bucket *)para;
6592
6593 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6594 xv, ret_bh);
6595}
6596
6597static int ocfs2_calc_value_tree_metas(struct inode *inode,
6598 struct ocfs2_xattr_bucket *bucket,
6599 void *para)
6600{
6601 struct ocfs2_value_tree_metas *metas =
6602 (struct ocfs2_value_tree_metas *)para;
6603 struct ocfs2_xattr_header *xh =
6604 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6605
6606 /* Add the credits for this bucket first. */
6607 metas->credits += bucket->bu_blocks;
6608 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6609 xh, &metas->num_metas,
6610 &metas->credits, &metas->num_recs,
6611 ocfs2_value_tree_metas_in_bucket,
6612 bucket);
6613}
6614
6615/*
6616 * Given a xattr extent rec starting from blkno and having len clusters,
6617 * iterate all the buckets calculate how much metadata we need for reflinking
6618 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6619 */
6620static int ocfs2_lock_reflink_xattr_rec_allocators(
6621 struct ocfs2_reflink_xattr_tree_args *args,
6622 struct ocfs2_extent_tree *xt_et,
6623 u64 blkno, u32 len, int *credits,
6624 struct ocfs2_alloc_context **meta_ac,
6625 struct ocfs2_alloc_context **data_ac)
6626{
6627 int ret, num_free_extents;
6628 struct ocfs2_value_tree_metas metas;
6629 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6630 struct ocfs2_refcount_block *rb;
6631
6632 memset(&metas, 0, sizeof(metas));
6633
6634 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6635 ocfs2_calc_value_tree_metas, &metas);
6636 if (ret) {
6637 mlog_errno(ret);
6638 goto out;
6639 }
6640
6641 *credits = metas.credits;
6642
6643 /*
6644 * Calculate we need for refcount tree change.
6645 *
6646 * We need to add/modify num_recs in refcount tree, so just calculate
6647 * an approximate number we need for refcount tree change.
6648 * Sometimes we need to split the tree, and after split, half recs
6649 * will be moved to the new block, and a new block can only provide
6650 * half number of recs. So we multiple new blocks by 2.
6651 * In the end, we have to add credits for modifying the already
6652 * existed refcount block.
6653 */
6654 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6655 metas.num_recs =
6656 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6657 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6658 metas.num_metas += metas.num_recs;
6659 *credits += metas.num_recs +
6660 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6661 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6662 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6663 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6664 else
6665 *credits += 1;
6666
6667 /* count in the xattr tree change. */
6668 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6669 if (num_free_extents < 0) {
6670 ret = num_free_extents;
6671 mlog_errno(ret);
6672 goto out;
6673 }
6674
6675 if (num_free_extents < len)
6676 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6677
6678 *credits += ocfs2_calc_extend_credits(osb->sb,
6679 xt_et->et_root_el, len);
6680
6681 if (metas.num_metas) {
6682 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6683 meta_ac);
6684 if (ret) {
6685 mlog_errno(ret);
6686 goto out;
6687 }
6688 }
6689
6690 if (len) {
6691 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6692 if (ret)
6693 mlog_errno(ret);
6694 }
6695out:
6696 if (ret) {
6697 if (*meta_ac) {
6698 ocfs2_free_alloc_context(*meta_ac);
6699 meta_ac = NULL;
6700 }
6701 }
6702
6703 return ret;
6704}
6705
6706static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6707 u64 blkno, u64 new_blkno, u32 clusters,
6708 struct ocfs2_alloc_context *meta_ac,
6709 struct ocfs2_alloc_context *data_ac,
6710 struct ocfs2_reflink_xattr_tree_args *args)
6711{
6712 int i, j, ret = 0;
6713 struct super_block *sb = args->reflink->old_inode->i_sb;
6714 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6715 u32 num_buckets = clusters * bpc;
6716 int bpb = args->old_bucket->bu_blocks;
6717 struct ocfs2_xattr_value_buf vb = {
6718 .vb_access = ocfs2_journal_access,
6719 };
6720
6721 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6722 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6723 if (ret) {
6724 mlog_errno(ret);
6725 break;
6726 }
6727
6728 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6729 if (ret) {
6730 mlog_errno(ret);
6731 break;
6732 }
6733
6734 /*
6735 * The real bucket num in this series of blocks is stored
6736 * in the 1st bucket.
6737 */
6738 if (i == 0)
6739 num_buckets = le16_to_cpu(
6740 bucket_xh(args->old_bucket)->xh_num_buckets);
6741
6742 ret = ocfs2_xattr_bucket_journal_access(handle,
6743 args->new_bucket,
6744 OCFS2_JOURNAL_ACCESS_CREATE);
6745 if (ret) {
6746 mlog_errno(ret);
6747 break;
6748 }
6749
6750 for (j = 0; j < bpb; j++)
6751 memcpy(bucket_block(args->new_bucket, j),
6752 bucket_block(args->old_bucket, j),
6753 sb->s_blocksize);
6754
6755 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6756
6757 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6758 args->old_bucket->bu_bhs[0],
6759 bucket_xh(args->old_bucket),
6760 args->new_bucket->bu_bhs[0],
6761 bucket_xh(args->new_bucket),
6762 &vb, meta_ac,
6763 ocfs2_get_reflink_xattr_value_root,
6764 args);
6765 if (ret) {
6766 mlog_errno(ret);
6767 break;
6768 }
6769
6770 /*
6771 * Re-access and dirty the bucket to calculate metaecc.
6772 * Because we may extend the transaction in reflink_xattr_header
6773 * which will let the already accessed block gone.
6774 */
6775 ret = ocfs2_xattr_bucket_journal_access(handle,
6776 args->new_bucket,
6777 OCFS2_JOURNAL_ACCESS_WRITE);
6778 if (ret) {
6779 mlog_errno(ret);
6780 break;
6781 }
6782
6783 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6784 ocfs2_xattr_bucket_relse(args->old_bucket);
6785 ocfs2_xattr_bucket_relse(args->new_bucket);
6786 }
6787
6788 ocfs2_xattr_bucket_relse(args->old_bucket);
6789 ocfs2_xattr_bucket_relse(args->new_bucket);
6790 return ret;
6791}
6792/*
6793 * Create the same xattr extent record in the new inode's xattr tree.
6794 */
6795static int ocfs2_reflink_xattr_rec(struct inode *inode,
6796 struct buffer_head *root_bh,
6797 u64 blkno,
6798 u32 cpos,
6799 u32 len,
6800 void *para)
6801{
6802 int ret, credits = 0;
6803 u32 p_cluster, num_clusters;
6804 u64 new_blkno;
6805 handle_t *handle;
6806 struct ocfs2_reflink_xattr_tree_args *args =
6807 (struct ocfs2_reflink_xattr_tree_args *)para;
6808 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6809 struct ocfs2_alloc_context *meta_ac = NULL;
6810 struct ocfs2_alloc_context *data_ac = NULL;
6811 struct ocfs2_extent_tree et;
6812
6813 ocfs2_init_xattr_tree_extent_tree(&et,
6814 INODE_CACHE(args->reflink->new_inode),
6815 args->new_blk_bh);
6816
6817 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6818 len, &credits,
6819 &meta_ac, &data_ac);
6820 if (ret) {
6821 mlog_errno(ret);
6822 goto out;
6823 }
6824
6825 handle = ocfs2_start_trans(osb, credits);
6826 if (IS_ERR(handle)) {
6827 ret = PTR_ERR(handle);
6828 mlog_errno(ret);
6829 goto out;
6830 }
6831
6832 ret = ocfs2_claim_clusters(osb, handle, data_ac,
6833 len, &p_cluster, &num_clusters);
6834 if (ret) {
6835 mlog_errno(ret);
6836 goto out_commit;
6837 }
6838
6839 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6840
6841 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6842 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6843 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6844 meta_ac, data_ac, args);
6845 if (ret) {
6846 mlog_errno(ret);
6847 goto out_commit;
6848 }
6849
6850 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6851 (unsigned long long)new_blkno, len, cpos);
6852 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6853 len, 0, meta_ac);
6854 if (ret)
6855 mlog_errno(ret);
6856
6857out_commit:
6858 ocfs2_commit_trans(osb, handle);
6859
6860out:
6861 if (meta_ac)
6862 ocfs2_free_alloc_context(meta_ac);
6863 if (data_ac)
6864 ocfs2_free_alloc_context(data_ac);
6865 return ret;
6866}
6867
6868/*
6869 * Create reflinked xattr buckets.
6870 * We will add bucket one by one, and refcount all the xattrs in the bucket
6871 * if they are stored outside.
6872 */
6873static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6874 struct buffer_head *blk_bh,
6875 struct buffer_head *new_blk_bh)
6876{
6877 int ret;
6878 struct ocfs2_reflink_xattr_tree_args para;
6879
6880 memset(&para, 0, sizeof(para));
6881 para.reflink = args;
6882 para.old_blk_bh = blk_bh;
6883 para.new_blk_bh = new_blk_bh;
6884
6885 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6886 if (!para.old_bucket) {
6887 mlog_errno(-ENOMEM);
6888 return -ENOMEM;
6889 }
6890
6891 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6892 if (!para.new_bucket) {
6893 ret = -ENOMEM;
6894 mlog_errno(ret);
6895 goto out;
6896 }
6897
6898 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6899 ocfs2_reflink_xattr_rec,
6900 &para);
6901 if (ret)
6902 mlog_errno(ret);
6903
6904out:
6905 ocfs2_xattr_bucket_free(para.old_bucket);
6906 ocfs2_xattr_bucket_free(para.new_bucket);
6907 return ret;
6908}
6909
6910static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6911 struct buffer_head *blk_bh)
6912{
6913 int ret, indexed = 0;
6914 struct buffer_head *new_blk_bh = NULL;
6915 struct ocfs2_xattr_block *xb =
6916 (struct ocfs2_xattr_block *)blk_bh->b_data;
6917
6918
6919 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
6920 indexed = 1;
6921
6922 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
6923 &new_blk_bh, indexed);
6924 if (ret) {
6925 mlog_errno(ret);
6926 goto out;
6927 }
6928
6929 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
6930 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
6931 else
6932 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
6933 if (ret)
6934 mlog_errno(ret);
6935
6936out:
6937 brelse(new_blk_bh);
6938 return ret;
6939}
6940
Tao Ma0fe9b662009-08-18 11:47:56 +08006941static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
6942{
6943 int type = ocfs2_xattr_get_type(xe);
6944
6945 return type != OCFS2_XATTR_INDEX_SECURITY &&
6946 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
6947 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
6948}
6949
Tao Ma2999d122009-08-18 11:43:55 +08006950int ocfs2_reflink_xattrs(struct inode *old_inode,
6951 struct buffer_head *old_bh,
6952 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08006953 struct buffer_head *new_bh,
6954 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08006955{
6956 int ret;
6957 struct ocfs2_xattr_reflink args;
6958 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
6959 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
6960 struct buffer_head *blk_bh = NULL;
6961 struct ocfs2_cached_dealloc_ctxt dealloc;
6962 struct ocfs2_refcount_tree *ref_tree;
6963 struct buffer_head *ref_root_bh = NULL;
6964
6965 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
6966 le64_to_cpu(di->i_refcount_loc),
6967 1, &ref_tree, &ref_root_bh);
6968 if (ret) {
6969 mlog_errno(ret);
6970 goto out;
6971 }
6972
6973 ocfs2_init_dealloc_ctxt(&dealloc);
6974
6975 args.old_inode = old_inode;
6976 args.new_inode = new_inode;
6977 args.old_bh = old_bh;
6978 args.new_bh = new_bh;
6979 args.ref_ci = &ref_tree->rf_ci;
6980 args.ref_root_bh = ref_root_bh;
6981 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006982 if (preserve_security)
6983 args.xattr_reflinked = NULL;
6984 else
6985 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08006986
6987 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6988 ret = ocfs2_reflink_xattr_inline(&args);
6989 if (ret) {
6990 mlog_errno(ret);
6991 goto out_unlock;
6992 }
6993 }
6994
6995 if (!di->i_xattr_loc)
6996 goto out_unlock;
6997
6998 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
6999 &blk_bh);
7000 if (ret < 0) {
7001 mlog_errno(ret);
7002 goto out_unlock;
7003 }
7004
7005 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7006 if (ret)
7007 mlog_errno(ret);
7008
7009 brelse(blk_bh);
7010
7011out_unlock:
7012 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7013 ref_tree, 1);
7014 brelse(ref_root_bh);
7015
7016 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7017 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7018 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7019 }
7020
7021out:
7022 return ret;
7023}
7024
7025/*
Tao Ma0fe9b662009-08-18 11:47:56 +08007026 * Initialize security and acl for a already created inode.
7027 * Used for reflink a non-preserve-security file.
7028 *
7029 * It uses common api like ocfs2_xattr_set, so the caller
7030 * must not hold any lock expect i_mutex.
7031 */
7032int ocfs2_init_security_and_acl(struct inode *dir,
7033 struct inode *inode)
7034{
7035 int ret = 0;
7036 struct buffer_head *dir_bh = NULL;
7037 struct ocfs2_security_xattr_info si = {
7038 .enable = 1,
7039 };
7040
7041 ret = ocfs2_init_security_get(inode, dir, &si);
7042 if (!ret) {
Christoph Hellwig431547b2009-11-13 09:52:56 +00007043 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7044 si.name, si.value, si.value_len,
7045 XATTR_CREATE);
Tao Ma0fe9b662009-08-18 11:47:56 +08007046 if (ret) {
7047 mlog_errno(ret);
7048 goto leave;
7049 }
7050 } else if (ret != -EOPNOTSUPP) {
7051 mlog_errno(ret);
7052 goto leave;
7053 }
7054
7055 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7056 if (ret) {
7057 mlog_errno(ret);
7058 goto leave;
7059 }
7060
7061 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7062 if (ret)
7063 mlog_errno(ret);
7064
7065 ocfs2_inode_unlock(dir, 0);
7066 brelse(dir_bh);
7067leave:
7068 return ret;
7069}
7070/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007071 * 'security' attributes support
7072 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007073static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
Tiger Yang923f7f32008-11-14 11:16:27 +08007074 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007075 size_t name_len, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007076{
7077 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7078 const size_t total_len = prefix_len + name_len + 1;
7079
7080 if (list && total_len <= list_size) {
7081 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7082 memcpy(list + prefix_len, name, name_len);
7083 list[prefix_len + name_len] = '\0';
7084 }
7085 return total_len;
7086}
7087
Christoph Hellwig431547b2009-11-13 09:52:56 +00007088static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7089 void *buffer, size_t size, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007090{
7091 if (strcmp(name, "") == 0)
7092 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007093 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7094 name, buffer, size);
Tiger Yang923f7f32008-11-14 11:16:27 +08007095}
7096
Christoph Hellwig431547b2009-11-13 09:52:56 +00007097static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7098 const void *value, size_t size, int flags, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007099{
7100 if (strcmp(name, "") == 0)
7101 return -EINVAL;
7102
Christoph Hellwig431547b2009-11-13 09:52:56 +00007103 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7104 name, value, size, flags);
Tiger Yang923f7f32008-11-14 11:16:27 +08007105}
7106
Tiger Yang534eadd2008-11-14 11:16:41 +08007107int ocfs2_init_security_get(struct inode *inode,
7108 struct inode *dir,
7109 struct ocfs2_security_xattr_info *si)
7110{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007111 /* check whether ocfs2 support feature xattr */
7112 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7113 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08007114 return security_inode_init_security(inode, dir, &si->name, &si->value,
7115 &si->value_len);
7116}
7117
7118int ocfs2_init_security_set(handle_t *handle,
7119 struct inode *inode,
7120 struct buffer_head *di_bh,
7121 struct ocfs2_security_xattr_info *si,
7122 struct ocfs2_alloc_context *xattr_ac,
7123 struct ocfs2_alloc_context *data_ac)
7124{
7125 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7126 OCFS2_XATTR_INDEX_SECURITY,
7127 si->name, si->value, si->value_len, 0,
7128 xattr_ac, data_ac);
7129}
7130
Tiger Yang923f7f32008-11-14 11:16:27 +08007131struct xattr_handler ocfs2_xattr_security_handler = {
7132 .prefix = XATTR_SECURITY_PREFIX,
7133 .list = ocfs2_xattr_security_list,
7134 .get = ocfs2_xattr_security_get,
7135 .set = ocfs2_xattr_security_set,
7136};
7137
7138/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007139 * 'trusted' attributes support
7140 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007141static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007142 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007143 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007144{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007145 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007146 const size_t total_len = prefix_len + name_len + 1;
7147
7148 if (list && total_len <= list_size) {
7149 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7150 memcpy(list + prefix_len, name, name_len);
7151 list[prefix_len + name_len] = '\0';
7152 }
7153 return total_len;
7154}
7155
Christoph Hellwig431547b2009-11-13 09:52:56 +00007156static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7157 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007158{
7159 if (strcmp(name, "") == 0)
7160 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007161 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7162 name, buffer, size);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007163}
7164
Christoph Hellwig431547b2009-11-13 09:52:56 +00007165static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7166 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007167{
7168 if (strcmp(name, "") == 0)
7169 return -EINVAL;
7170
Christoph Hellwig431547b2009-11-13 09:52:56 +00007171 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7172 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007173}
7174
7175struct xattr_handler ocfs2_xattr_trusted_handler = {
7176 .prefix = XATTR_TRUSTED_PREFIX,
7177 .list = ocfs2_xattr_trusted_list,
7178 .get = ocfs2_xattr_trusted_get,
7179 .set = ocfs2_xattr_trusted_set,
7180};
7181
Mark Fasheh99219ae2008-10-07 14:52:59 -07007182/*
7183 * 'user' attributes support
7184 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007185static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007186 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007187 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007188{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007189 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007190 const size_t total_len = prefix_len + name_len + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007191 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007192
7193 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7194 return 0;
7195
7196 if (list && total_len <= list_size) {
7197 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7198 memcpy(list + prefix_len, name, name_len);
7199 list[prefix_len + name_len] = '\0';
7200 }
7201 return total_len;
7202}
7203
Christoph Hellwig431547b2009-11-13 09:52:56 +00007204static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7205 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007206{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007207 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007208
7209 if (strcmp(name, "") == 0)
7210 return -EINVAL;
7211 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7212 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007213 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007214 buffer, size);
7215}
7216
Christoph Hellwig431547b2009-11-13 09:52:56 +00007217static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7218 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007219{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007220 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007221
7222 if (strcmp(name, "") == 0)
7223 return -EINVAL;
7224 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7225 return -EOPNOTSUPP;
7226
Christoph Hellwig431547b2009-11-13 09:52:56 +00007227 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7228 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007229}
7230
7231struct xattr_handler ocfs2_xattr_user_handler = {
7232 .prefix = XATTR_USER_PREFIX,
7233 .list = ocfs2_xattr_user_list,
7234 .get = ocfs2_xattr_user_get,
7235 .set = ocfs2_xattr_user_set,
7236};