blob: 2f6fd48c0ba4d54e56af2de1d234498f18753710 [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 Ma757055a2008-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 Ma757055a2008-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 Beckerec20cec2010-03-19 14:13:52 -0700742 ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800743
Joel Becker19b801f2008-12-09 14:36:50 -0800744 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800745
Tao Ma85db90e2008-11-12 08:27:01 +0800746 /*
747 * We should have already allocated enough space before the transaction,
748 * so no need to restart.
749 */
750 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800751
752leave:
Tao Maf56654c2008-08-18 17:38:48 +0800753
754 return status;
755}
756
757static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800758 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800759 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800760 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800761 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800762{
763 int ret;
764 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800765 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700766 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800767
Joel Becker5e404e92009-02-13 03:54:22 -0800768 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700769
Joel Becker0cf2f762009-02-12 16:41:25 -0800770 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800771 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800772 if (ret) {
773 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800774 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800775 }
776
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800777 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800778 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800779 if (ret) {
780 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800781 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800782 }
783
Joel Beckerd72cc722008-12-09 14:30:41 -0800784 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Joel Beckerec20cec2010-03-19 14:13:52 -0700785 ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800786
Tao Ma492a8a32009-08-18 11:43:17 +0800787 if (ext_flags & OCFS2_EXT_REFCOUNTED)
788 ret = ocfs2_decrease_refcount(inode, handle,
789 ocfs2_blocks_to_clusters(inode->i_sb,
790 phys_blkno),
791 len, ctxt->meta_ac, &ctxt->dealloc, 1);
792 else
793 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
794 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800795 if (ret)
796 mlog_errno(ret);
797
Tao Maf56654c2008-08-18 17:38:48 +0800798out:
Tao Maf56654c2008-08-18 17:38:48 +0800799 return ret;
800}
801
802static int ocfs2_xattr_shrink_size(struct inode *inode,
803 u32 old_clusters,
804 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800805 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800806 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800807{
808 int ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800809 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800810 u32 trunc_len, cpos, phys_cpos, alloc_size;
811 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800812
813 if (old_clusters <= new_clusters)
814 return 0;
815
816 cpos = new_clusters;
817 trunc_len = old_clusters - new_clusters;
818 while (trunc_len) {
819 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800820 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800821 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800822 if (ret) {
823 mlog_errno(ret);
824 goto out;
825 }
826
827 if (alloc_size > trunc_len)
828 alloc_size = trunc_len;
829
Joel Becker19b801f2008-12-09 14:36:50 -0800830 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800831 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800832 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800833 if (ret) {
834 mlog_errno(ret);
835 goto out;
836 }
837
838 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800839 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
840 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800841 cpos += alloc_size;
842 trunc_len -= alloc_size;
843 }
844
845out:
Tao Maf56654c2008-08-18 17:38:48 +0800846 return ret;
847}
848
849static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800850 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800851 int len,
852 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800853{
854 int ret;
855 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800856 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800857
858 if (new_clusters == old_clusters)
859 return 0;
860
861 if (new_clusters > old_clusters)
862 ret = ocfs2_xattr_extend_allocation(inode,
863 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800864 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800865 else
866 ret = ocfs2_xattr_shrink_size(inode,
867 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800868 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800869
870 return ret;
871}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800872
Tao Ma936b8832008-10-09 23:06:14 +0800873static int ocfs2_xattr_list_entry(char *buffer, size_t size,
874 size_t *result, const char *prefix,
875 const char *name, int name_len)
876{
877 char *p = buffer + *result;
878 int prefix_len = strlen(prefix);
879 int total_len = prefix_len + name_len + 1;
880
881 *result += total_len;
882
883 /* we are just looking for how big our buffer needs to be */
884 if (!size)
885 return 0;
886
887 if (*result > size)
888 return -ERANGE;
889
890 memcpy(p, prefix, prefix_len);
891 memcpy(p + prefix_len, name, name_len);
892 p[prefix_len + name_len] = '\0';
893
894 return 0;
895}
896
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800897static int ocfs2_xattr_list_entries(struct inode *inode,
898 struct ocfs2_xattr_header *header,
899 char *buffer, size_t buffer_size)
900{
Tao Ma936b8832008-10-09 23:06:14 +0800901 size_t result = 0;
902 int i, type, ret;
903 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800904
905 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
906 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800907 type = ocfs2_xattr_get_type(entry);
908 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800909
Tao Ma936b8832008-10-09 23:06:14 +0800910 if (prefix) {
911 name = (const char *)header +
912 le16_to_cpu(entry->xe_name_offset);
913
914 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
915 &result, prefix, name,
916 entry->xe_name_len);
917 if (ret)
918 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800919 }
920 }
921
Tao Ma936b8832008-10-09 23:06:14 +0800922 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800923}
924
Tao Ma8b2c0db2009-08-18 11:43:49 +0800925int ocfs2_has_inline_xattr_value_outside(struct inode *inode,
926 struct ocfs2_dinode *di)
927{
928 struct ocfs2_xattr_header *xh;
929 int i;
930
931 xh = (struct ocfs2_xattr_header *)
932 ((void *)di + inode->i_sb->s_blocksize -
933 le16_to_cpu(di->i_xattr_inline_size));
934
935 for (i = 0; i < le16_to_cpu(xh->xh_count); i++)
936 if (!ocfs2_xattr_is_local(&xh->xh_entries[i]))
937 return 1;
938
939 return 0;
940}
941
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800942static int ocfs2_xattr_ibody_list(struct inode *inode,
943 struct ocfs2_dinode *di,
944 char *buffer,
945 size_t buffer_size)
946{
947 struct ocfs2_xattr_header *header = NULL;
948 struct ocfs2_inode_info *oi = OCFS2_I(inode);
949 int ret = 0;
950
951 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
952 return ret;
953
954 header = (struct ocfs2_xattr_header *)
955 ((void *)di + inode->i_sb->s_blocksize -
956 le16_to_cpu(di->i_xattr_inline_size));
957
958 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
959
960 return ret;
961}
962
963static int ocfs2_xattr_block_list(struct inode *inode,
964 struct ocfs2_dinode *di,
965 char *buffer,
966 size_t buffer_size)
967{
968 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800969 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800970 int ret = 0;
971
972 if (!di->i_xattr_loc)
973 return ret;
974
Joel Becker4ae1d692008-11-13 14:49:18 -0800975 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
976 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800977 if (ret < 0) {
978 mlog_errno(ret);
979 return ret;
980 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800981
Tao Ma0c044f02008-08-18 17:38:50 +0800982 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800983 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
984 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
985 ret = ocfs2_xattr_list_entries(inode, header,
986 buffer, buffer_size);
Tao Ma47bca492009-08-18 11:43:42 +0800987 } else
988 ret = ocfs2_xattr_tree_list_index_block(inode, blk_bh,
Tao Ma0c044f02008-08-18 17:38:50 +0800989 buffer, buffer_size);
Joel Becker4ae1d692008-11-13 14:49:18 -0800990
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800991 brelse(blk_bh);
992
993 return ret;
994}
995
996ssize_t ocfs2_listxattr(struct dentry *dentry,
997 char *buffer,
998 size_t size)
999{
1000 int ret = 0, i_ret = 0, b_ret = 0;
1001 struct buffer_head *di_bh = NULL;
1002 struct ocfs2_dinode *di = NULL;
1003 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
1004
Tiger Yang8154da32008-08-18 17:11:46 +08001005 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
1006 return -EOPNOTSUPP;
1007
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001008 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1009 return ret;
1010
1011 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
1012 if (ret < 0) {
1013 mlog_errno(ret);
1014 return ret;
1015 }
1016
1017 di = (struct ocfs2_dinode *)di_bh->b_data;
1018
1019 down_read(&oi->ip_xattr_sem);
1020 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
1021 if (i_ret < 0)
1022 b_ret = 0;
1023 else {
1024 if (buffer) {
1025 buffer += i_ret;
1026 size -= i_ret;
1027 }
1028 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
1029 buffer, size);
1030 if (b_ret < 0)
1031 i_ret = 0;
1032 }
1033 up_read(&oi->ip_xattr_sem);
1034 ocfs2_inode_unlock(dentry->d_inode, 0);
1035
1036 brelse(di_bh);
1037
1038 return i_ret + b_ret;
1039}
1040
1041static int ocfs2_xattr_find_entry(int name_index,
1042 const char *name,
1043 struct ocfs2_xattr_search *xs)
1044{
1045 struct ocfs2_xattr_entry *entry;
1046 size_t name_len;
1047 int i, cmp = 1;
1048
1049 if (name == NULL)
1050 return -EINVAL;
1051
1052 name_len = strlen(name);
1053 entry = xs->here;
1054 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1055 cmp = name_index - ocfs2_xattr_get_type(entry);
1056 if (!cmp)
1057 cmp = name_len - entry->xe_name_len;
1058 if (!cmp)
1059 cmp = memcmp(name, (xs->base +
1060 le16_to_cpu(entry->xe_name_offset)),
1061 name_len);
1062 if (cmp == 0)
1063 break;
1064 entry += 1;
1065 }
1066 xs->here = entry;
1067
1068 return cmp ? -ENODATA : 0;
1069}
1070
1071static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +08001072 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001073 void *buffer,
1074 size_t len)
1075{
1076 u32 cpos, p_cluster, num_clusters, bpc, clusters;
1077 u64 blkno;
1078 int i, ret = 0;
1079 size_t cplen, blocksize;
1080 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001081 struct ocfs2_extent_list *el;
1082
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001083 el = &xv->xr_list;
1084 clusters = le32_to_cpu(xv->xr_clusters);
1085 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1086 blocksize = inode->i_sb->s_blocksize;
1087
1088 cpos = 0;
1089 while (cpos < clusters) {
1090 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001091 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001092 if (ret) {
1093 mlog_errno(ret);
1094 goto out;
1095 }
1096
1097 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1098 /* Copy ocfs2_xattr_value */
1099 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001100 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1101 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001102 if (ret) {
1103 mlog_errno(ret);
1104 goto out;
1105 }
1106
1107 cplen = len >= blocksize ? blocksize : len;
1108 memcpy(buffer, bh->b_data, cplen);
1109 len -= cplen;
1110 buffer += cplen;
1111
1112 brelse(bh);
1113 bh = NULL;
1114 if (len == 0)
1115 break;
1116 }
1117 cpos += num_clusters;
1118 }
1119out:
1120 return ret;
1121}
1122
1123static int ocfs2_xattr_ibody_get(struct inode *inode,
1124 int name_index,
1125 const char *name,
1126 void *buffer,
1127 size_t buffer_size,
1128 struct ocfs2_xattr_search *xs)
1129{
1130 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1131 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001132 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001133 size_t size;
1134 int ret = 0;
1135
1136 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1137 return -ENODATA;
1138
1139 xs->end = (void *)di + inode->i_sb->s_blocksize;
1140 xs->header = (struct ocfs2_xattr_header *)
1141 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1142 xs->base = (void *)xs->header;
1143 xs->here = xs->header->xh_entries;
1144
1145 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1146 if (ret)
1147 return ret;
1148 size = le64_to_cpu(xs->here->xe_value_size);
1149 if (buffer) {
1150 if (size > buffer_size)
1151 return -ERANGE;
1152 if (ocfs2_xattr_is_local(xs->here)) {
1153 memcpy(buffer, (void *)xs->base +
1154 le16_to_cpu(xs->here->xe_name_offset) +
1155 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1156 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001157 xv = (struct ocfs2_xattr_value_root *)
1158 (xs->base + le16_to_cpu(
1159 xs->here->xe_name_offset) +
1160 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1161 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001162 buffer, size);
1163 if (ret < 0) {
1164 mlog_errno(ret);
1165 return ret;
1166 }
1167 }
1168 }
1169
1170 return size;
1171}
1172
1173static int ocfs2_xattr_block_get(struct inode *inode,
1174 int name_index,
1175 const char *name,
1176 void *buffer,
1177 size_t buffer_size,
1178 struct ocfs2_xattr_search *xs)
1179{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001180 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001181 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001182 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301183 int ret = -ENODATA, name_offset, name_len, i;
1184 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001185
Joel Beckerba937122008-10-24 19:13:20 -07001186 xs->bucket = ocfs2_xattr_bucket_new(inode);
1187 if (!xs->bucket) {
1188 ret = -ENOMEM;
1189 mlog_errno(ret);
1190 goto cleanup;
1191 }
Tao Ma589dc262008-08-18 17:38:51 +08001192
Joel Becker54f443f2008-10-20 18:43:07 -07001193 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1194 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001195 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001196 goto cleanup;
1197 }
1198
Tiger Yang6c1e1832008-11-02 19:04:21 +08001199 if (xs->not_found) {
1200 ret = -ENODATA;
1201 goto cleanup;
1202 }
1203
Joel Becker54f443f2008-10-20 18:43:07 -07001204 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001205 size = le64_to_cpu(xs->here->xe_value_size);
1206 if (buffer) {
1207 ret = -ERANGE;
1208 if (size > buffer_size)
1209 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001210
1211 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1212 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1213 i = xs->here - xs->header->xh_entries;
1214
1215 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001216 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001217 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001218 i,
1219 &block_off,
1220 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001221 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001222 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001223 if (ocfs2_xattr_is_local(xs->here)) {
1224 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001225 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001226 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001227 xv = (struct ocfs2_xattr_value_root *)
1228 (xs->base + name_offset + name_len);
1229 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001230 buffer, size);
1231 if (ret < 0) {
1232 mlog_errno(ret);
1233 goto cleanup;
1234 }
1235 }
1236 }
1237 ret = size;
1238cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001239 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001240
Joel Becker54f443f2008-10-20 18:43:07 -07001241 brelse(xs->xattr_bh);
1242 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001243 return ret;
1244}
1245
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001246int ocfs2_xattr_get_nolock(struct inode *inode,
1247 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001248 int name_index,
1249 const char *name,
1250 void *buffer,
1251 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001252{
1253 int ret;
1254 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001255 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1256 struct ocfs2_xattr_search xis = {
1257 .not_found = -ENODATA,
1258 };
1259 struct ocfs2_xattr_search xbs = {
1260 .not_found = -ENODATA,
1261 };
1262
Tiger Yang8154da32008-08-18 17:11:46 +08001263 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1264 return -EOPNOTSUPP;
1265
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001266 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1267 ret = -ENODATA;
1268
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001269 xis.inode_bh = xbs.inode_bh = di_bh;
1270 di = (struct ocfs2_dinode *)di_bh->b_data;
1271
1272 down_read(&oi->ip_xattr_sem);
1273 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1274 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001275 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001276 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1277 buffer_size, &xbs);
1278 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001279
1280 return ret;
1281}
1282
1283/* ocfs2_xattr_get()
1284 *
1285 * Copy an extended attribute into the buffer provided.
1286 * Buffer is NULL to compute the size of buffer required.
1287 */
1288static int ocfs2_xattr_get(struct inode *inode,
1289 int name_index,
1290 const char *name,
1291 void *buffer,
1292 size_t buffer_size)
1293{
1294 int ret;
1295 struct buffer_head *di_bh = NULL;
1296
1297 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1298 if (ret < 0) {
1299 mlog_errno(ret);
1300 return ret;
1301 }
1302 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1303 name, buffer, buffer_size);
1304
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001305 ocfs2_inode_unlock(inode, 0);
1306
1307 brelse(di_bh);
1308
1309 return ret;
1310}
1311
1312static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001313 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001314 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001315 const void *value,
1316 int value_len)
1317{
Tao Ma71d548a2008-12-05 06:20:54 +08001318 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001319 u16 blocksize = inode->i_sb->s_blocksize;
1320 u32 p_cluster, num_clusters;
1321 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1322 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1323 u64 blkno;
1324 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001325 unsigned int ext_flags;
1326 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001327
1328 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1329
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001330 while (cpos < clusters) {
1331 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001332 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001333 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001334 if (ret) {
1335 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001336 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001337 }
1338
Tao Ma492a8a32009-08-18 11:43:17 +08001339 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1340
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001341 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1342
1343 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001344 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1345 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001346 if (ret) {
1347 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001348 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001349 }
1350
1351 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001352 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001353 bh,
1354 OCFS2_JOURNAL_ACCESS_WRITE);
1355 if (ret < 0) {
1356 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001357 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001358 }
1359
1360 cp_len = value_len > blocksize ? blocksize : value_len;
1361 memcpy(bh->b_data, value, cp_len);
1362 value_len -= cp_len;
1363 value += cp_len;
1364 if (cp_len < blocksize)
1365 memset(bh->b_data + cp_len, 0,
1366 blocksize - cp_len);
1367
Joel Beckerec20cec2010-03-19 14:13:52 -07001368 ocfs2_journal_dirty(handle, bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001369 brelse(bh);
1370 bh = NULL;
1371
1372 /*
1373 * XXX: do we need to empty all the following
1374 * blocks in this cluster?
1375 */
1376 if (!value_len)
1377 break;
1378 }
1379 cpos += num_clusters;
1380 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001381out:
1382 brelse(bh);
1383
1384 return ret;
1385}
1386
Joel Becker69a3e532009-08-17 12:24:39 -07001387static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1388 int num_entries)
1389{
1390 int free_space;
1391
1392 if (!needed_space)
1393 return 0;
1394
1395 free_space = free_start -
1396 sizeof(struct ocfs2_xattr_header) -
1397 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1398 OCFS2_XATTR_HEADER_GAP;
1399 if (free_space < 0)
1400 return -EIO;
1401 if (free_space < needed_space)
1402 return -ENOSPC;
1403
1404 return 0;
1405}
1406
Joel Beckercf2bc802009-08-18 13:52:38 -07001407static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1408 int type)
1409{
1410 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1411}
1412
1413static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1414{
1415 loc->xl_ops->xlo_journal_dirty(handle, loc);
1416}
1417
Joel Becker69a3e532009-08-17 12:24:39 -07001418/* Give a pointer into the storage for the given offset */
1419static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1420{
1421 BUG_ON(offset >= loc->xl_size);
1422 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1423}
1424
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001425/*
Joel Becker11179f22009-08-14 16:07:44 -07001426 * Wipe the name+value pair and allow the storage to reclaim it. This
1427 * must be followed by either removal of the entry or a call to
1428 * ocfs2_xa_add_namevalue().
1429 */
1430static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1431{
1432 loc->xl_ops->xlo_wipe_namevalue(loc);
1433}
1434
Joel Becker69a3e532009-08-17 12:24:39 -07001435/*
1436 * Find lowest offset to a name+value pair. This is the start of our
1437 * downward-growing free space.
1438 */
1439static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1440{
1441 return loc->xl_ops->xlo_get_free_start(loc);
1442}
1443
1444/* Can we reuse loc->xl_entry for xi? */
1445static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1446 struct ocfs2_xattr_info *xi)
1447{
1448 return loc->xl_ops->xlo_can_reuse(loc, xi);
1449}
1450
1451/* How much free space is needed to set the new value */
1452static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1453 struct ocfs2_xattr_info *xi)
1454{
1455 return loc->xl_ops->xlo_check_space(loc, xi);
1456}
1457
1458static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1459{
1460 loc->xl_ops->xlo_add_entry(loc, name_hash);
1461 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1462 /*
1463 * We can't leave the new entry's xe_name_offset at zero or
1464 * add_namevalue() will go nuts. We set it to the size of our
1465 * storage so that it can never be less than any other entry.
1466 */
1467 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1468}
1469
1470static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1471 struct ocfs2_xattr_info *xi)
1472{
1473 int size = namevalue_size_xi(xi);
1474 int nameval_offset;
1475 char *nameval_buf;
1476
1477 loc->xl_ops->xlo_add_namevalue(loc, size);
1478 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1479 loc->xl_entry->xe_name_len = xi->xi_name_len;
1480 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1481 ocfs2_xattr_set_local(loc->xl_entry,
1482 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1483
1484 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1485 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1486 memset(nameval_buf, 0, size);
1487 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1488}
1489
Joel Becker3fc12af2009-08-18 13:20:27 -07001490static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1491 struct ocfs2_xattr_value_buf *vb)
1492{
1493 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1494 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1495
1496 /* Value bufs are for value trees */
Joel Becker73857ee2009-08-18 20:26:41 -07001497 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
Joel Becker3fc12af2009-08-18 13:20:27 -07001498 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1499 (name_size + OCFS2_XATTR_ROOT_SIZE));
1500
1501 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1502 vb->vb_xv =
1503 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1504 nameval_offset +
1505 name_size);
1506}
1507
Joel Beckercf2bc802009-08-18 13:52:38 -07001508static int ocfs2_xa_block_journal_access(handle_t *handle,
1509 struct ocfs2_xa_loc *loc, int type)
1510{
1511 struct buffer_head *bh = loc->xl_storage;
1512 ocfs2_journal_access_func access;
1513
1514 if (loc->xl_size == (bh->b_size -
1515 offsetof(struct ocfs2_xattr_block,
1516 xb_attrs.xb_header)))
1517 access = ocfs2_journal_access_xb;
1518 else
1519 access = ocfs2_journal_access_di;
1520 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1521}
1522
1523static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1524 struct ocfs2_xa_loc *loc)
1525{
1526 struct buffer_head *bh = loc->xl_storage;
1527
1528 ocfs2_journal_dirty(handle, bh);
1529}
1530
Joel Becker11179f22009-08-14 16:07:44 -07001531static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1532 int offset)
1533{
Joel Becker11179f22009-08-14 16:07:44 -07001534 return (char *)loc->xl_header + offset;
1535}
1536
Joel Becker69a3e532009-08-17 12:24:39 -07001537static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1538 struct ocfs2_xattr_info *xi)
1539{
1540 /*
1541 * Block storage is strict. If the sizes aren't exact, we will
1542 * remove the old one and reinsert the new.
1543 */
1544 return namevalue_size_xe(loc->xl_entry) ==
1545 namevalue_size_xi(xi);
1546}
1547
1548static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1549{
1550 struct ocfs2_xattr_header *xh = loc->xl_header;
1551 int i, count = le16_to_cpu(xh->xh_count);
1552 int offset, free_start = loc->xl_size;
1553
1554 for (i = 0; i < count; i++) {
1555 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1556 if (offset < free_start)
1557 free_start = offset;
1558 }
1559
1560 return free_start;
1561}
1562
1563static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1564 struct ocfs2_xattr_info *xi)
1565{
1566 int count = le16_to_cpu(loc->xl_header->xh_count);
1567 int free_start = ocfs2_xa_get_free_start(loc);
1568 int needed_space = ocfs2_xi_entry_usage(xi);
1569
1570 /*
1571 * Block storage will reclaim the original entry before inserting
1572 * the new value, so we only need the difference. If the new
1573 * entry is smaller than the old one, we don't need anything.
1574 */
1575 if (loc->xl_entry) {
1576 /* Don't need space if we're reusing! */
1577 if (ocfs2_xa_can_reuse_entry(loc, xi))
1578 needed_space = 0;
1579 else
1580 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1581 }
1582 if (needed_space < 0)
1583 needed_space = 0;
1584 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1585}
1586
Joel Becker11179f22009-08-14 16:07:44 -07001587/*
1588 * Block storage for xattrs keeps the name+value pairs compacted. When
1589 * we remove one, we have to shift any that preceded it towards the end.
1590 */
1591static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1592{
1593 int i, offset;
1594 int namevalue_offset, first_namevalue_offset, namevalue_size;
1595 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1596 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Becker11179f22009-08-14 16:07:44 -07001597 int count = le16_to_cpu(xh->xh_count);
1598
1599 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07001600 namevalue_size = namevalue_size_xe(entry);
Joel Becker69a3e532009-08-17 12:24:39 -07001601 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
Joel Becker11179f22009-08-14 16:07:44 -07001602
1603 /* Shift the name+value pairs */
1604 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1605 (char *)xh + first_namevalue_offset,
1606 namevalue_offset - first_namevalue_offset);
1607 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1608
1609 /* Now tell xh->xh_entries about it */
1610 for (i = 0; i < count; i++) {
1611 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
Tao Madfe4d3d2010-03-19 15:04:23 +08001612 if (offset <= namevalue_offset)
Joel Becker11179f22009-08-14 16:07:44 -07001613 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1614 namevalue_size);
1615 }
1616
1617 /*
1618 * Note that we don't update xh_free_start or xh_name_value_len
1619 * because they're not used in block-stored xattrs.
1620 */
1621}
1622
Joel Becker69a3e532009-08-17 12:24:39 -07001623static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1624{
1625 int count = le16_to_cpu(loc->xl_header->xh_count);
1626 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1627 le16_add_cpu(&loc->xl_header->xh_count, 1);
1628 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1629}
1630
1631static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1632{
1633 int free_start = ocfs2_xa_get_free_start(loc);
1634
1635 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1636}
1637
Joel Becker3fc12af2009-08-18 13:20:27 -07001638static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1639 struct ocfs2_xattr_value_buf *vb)
1640{
1641 struct buffer_head *bh = loc->xl_storage;
1642
1643 if (loc->xl_size == (bh->b_size -
1644 offsetof(struct ocfs2_xattr_block,
1645 xb_attrs.xb_header)))
1646 vb->vb_access = ocfs2_journal_access_xb;
1647 else
1648 vb->vb_access = ocfs2_journal_access_di;
1649 vb->vb_bh = bh;
1650}
1651
Joel Becker11179f22009-08-14 16:07:44 -07001652/*
1653 * Operations for xattrs stored in blocks. This includes inline inode
1654 * storage and unindexed ocfs2_xattr_blocks.
1655 */
1656static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001657 .xlo_journal_access = ocfs2_xa_block_journal_access,
1658 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001659 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001660 .xlo_check_space = ocfs2_xa_block_check_space,
1661 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1662 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001663 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001664 .xlo_add_entry = ocfs2_xa_block_add_entry,
1665 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001666 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001667};
1668
Joel Beckercf2bc802009-08-18 13:52:38 -07001669static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1670 struct ocfs2_xa_loc *loc, int type)
1671{
1672 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1673
1674 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1675}
1676
1677static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1678 struct ocfs2_xa_loc *loc)
1679{
1680 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1681
1682 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1683}
1684
Joel Becker11179f22009-08-14 16:07:44 -07001685static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1686 int offset)
1687{
1688 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1689 int block, block_offset;
1690
Joel Becker11179f22009-08-14 16:07:44 -07001691 /* The header is at the front of the bucket */
Joel Beckercf2bc802009-08-18 13:52:38 -07001692 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1693 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
Joel Becker11179f22009-08-14 16:07:44 -07001694
1695 return bucket_block(bucket, block) + block_offset;
1696}
1697
Joel Becker69a3e532009-08-17 12:24:39 -07001698static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1699 struct ocfs2_xattr_info *xi)
1700{
1701 return namevalue_size_xe(loc->xl_entry) >=
1702 namevalue_size_xi(xi);
1703}
1704
1705static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1706{
1707 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1708 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1709}
1710
1711static int ocfs2_bucket_align_free_start(struct super_block *sb,
1712 int free_start, int size)
1713{
1714 /*
1715 * We need to make sure that the name+value pair fits within
1716 * one block.
1717 */
1718 if (((free_start - size) >> sb->s_blocksize_bits) !=
1719 ((free_start - 1) >> sb->s_blocksize_bits))
1720 free_start -= free_start % sb->s_blocksize;
1721
1722 return free_start;
1723}
1724
1725static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1726 struct ocfs2_xattr_info *xi)
1727{
1728 int rc;
1729 int count = le16_to_cpu(loc->xl_header->xh_count);
1730 int free_start = ocfs2_xa_get_free_start(loc);
1731 int needed_space = ocfs2_xi_entry_usage(xi);
1732 int size = namevalue_size_xi(xi);
Joel Beckercf2bc802009-08-18 13:52:38 -07001733 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001734
1735 /*
1736 * Bucket storage does not reclaim name+value pairs it cannot
1737 * reuse. They live as holes until the bucket fills, and then
1738 * the bucket is defragmented. However, the bucket can reclaim
1739 * the ocfs2_xattr_entry.
1740 */
1741 if (loc->xl_entry) {
1742 /* Don't need space if we're reusing! */
1743 if (ocfs2_xa_can_reuse_entry(loc, xi))
1744 needed_space = 0;
1745 else
1746 needed_space -= sizeof(struct ocfs2_xattr_entry);
1747 }
1748 BUG_ON(needed_space < 0);
1749
1750 if (free_start < size) {
1751 if (needed_space)
1752 return -ENOSPC;
1753 } else {
1754 /*
1755 * First we check if it would fit in the first place.
1756 * Below, we align the free start to a block. This may
1757 * slide us below the minimum gap. By checking unaligned
1758 * first, we avoid that error.
1759 */
1760 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1761 count);
1762 if (rc)
1763 return rc;
1764 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1765 size);
1766 }
1767 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1768}
1769
Joel Becker11179f22009-08-14 16:07:44 -07001770static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1771{
Joel Becker199799a2009-08-14 19:04:15 -07001772 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1773 -namevalue_size_xe(loc->xl_entry));
Joel Becker11179f22009-08-14 16:07:44 -07001774}
1775
Joel Becker69a3e532009-08-17 12:24:39 -07001776static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1777{
1778 struct ocfs2_xattr_header *xh = loc->xl_header;
1779 int count = le16_to_cpu(xh->xh_count);
1780 int low = 0, high = count - 1, tmp;
1781 struct ocfs2_xattr_entry *tmp_xe;
1782
1783 /*
1784 * We keep buckets sorted by name_hash, so we need to find
1785 * our insert place.
1786 */
1787 while (low <= high && count) {
1788 tmp = (low + high) / 2;
1789 tmp_xe = &xh->xh_entries[tmp];
1790
1791 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1792 low = tmp + 1;
1793 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1794 high = tmp - 1;
1795 else {
1796 low = tmp;
1797 break;
1798 }
1799 }
1800
1801 if (low != count)
1802 memmove(&xh->xh_entries[low + 1],
1803 &xh->xh_entries[low],
1804 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1805
1806 le16_add_cpu(&xh->xh_count, 1);
1807 loc->xl_entry = &xh->xh_entries[low];
1808 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1809}
1810
1811static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1812{
1813 int free_start = ocfs2_xa_get_free_start(loc);
1814 struct ocfs2_xattr_header *xh = loc->xl_header;
Joel Beckercf2bc802009-08-18 13:52:38 -07001815 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker69a3e532009-08-17 12:24:39 -07001816 int nameval_offset;
1817
1818 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1819 nameval_offset = free_start - size;
1820 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1821 xh->xh_free_start = cpu_to_le16(nameval_offset);
1822 le16_add_cpu(&xh->xh_name_value_len, size);
1823
1824}
1825
Joel Becker3fc12af2009-08-18 13:20:27 -07001826static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1827 struct ocfs2_xattr_value_buf *vb)
1828{
1829 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
Joel Beckercf2bc802009-08-18 13:52:38 -07001830 struct super_block *sb = loc->xl_inode->i_sb;
Joel Becker3fc12af2009-08-18 13:20:27 -07001831 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1832 int size = namevalue_size_xe(loc->xl_entry);
1833 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1834
1835 /* Values are not allowed to straddle block boundaries */
1836 BUG_ON(block_offset !=
1837 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1838 /* We expect the bucket to be filled in */
1839 BUG_ON(!bucket->bu_bhs[block_offset]);
1840
1841 vb->vb_access = ocfs2_journal_access;
1842 vb->vb_bh = bucket->bu_bhs[block_offset];
1843}
1844
Joel Becker11179f22009-08-14 16:07:44 -07001845/* Operations for xattrs stored in buckets. */
1846static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
Joel Beckercf2bc802009-08-18 13:52:38 -07001847 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1848 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
Joel Becker11179f22009-08-14 16:07:44 -07001849 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
Joel Becker69a3e532009-08-17 12:24:39 -07001850 .xlo_check_space = ocfs2_xa_bucket_check_space,
1851 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1852 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
Joel Becker11179f22009-08-14 16:07:44 -07001853 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
Joel Becker69a3e532009-08-17 12:24:39 -07001854 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1855 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
Joel Becker3fc12af2009-08-18 13:20:27 -07001856 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
Joel Becker11179f22009-08-14 16:07:44 -07001857};
1858
Joel Becker399ff3a72009-09-01 18:38:27 -07001859static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1860{
1861 struct ocfs2_xattr_value_buf vb;
1862
1863 if (ocfs2_xattr_is_local(loc->xl_entry))
1864 return 0;
1865
1866 ocfs2_xa_fill_value_buf(loc, &vb);
1867 return le32_to_cpu(vb.vb_xv->xr_clusters);
1868}
1869
Joel Becker73857ee2009-08-18 20:26:41 -07001870static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1871 struct ocfs2_xattr_set_ctxt *ctxt)
1872{
1873 int trunc_rc, access_rc;
1874 struct ocfs2_xattr_value_buf vb;
1875
1876 ocfs2_xa_fill_value_buf(loc, &vb);
1877 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1878 ctxt);
1879
1880 /*
1881 * The caller of ocfs2_xa_value_truncate() has already called
1882 * ocfs2_xa_journal_access on the loc. However, The truncate code
1883 * calls ocfs2_extend_trans(). This may commit the previous
1884 * transaction and open a new one. If this is a bucket, truncate
1885 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1886 * the caller is expecting to dirty the entire bucket. So we must
1887 * reset the journal work. We do this even if truncate has failed,
1888 * as it could have failed after committing the extend.
1889 */
1890 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1891 OCFS2_JOURNAL_ACCESS_WRITE);
1892
1893 /* Errors in truncate take precedence */
1894 return trunc_rc ? trunc_rc : access_rc;
1895}
1896
Joel Becker11179f22009-08-14 16:07:44 -07001897static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1898{
Joel Beckerbde1e542009-08-14 16:58:38 -07001899 int index, count;
1900 struct ocfs2_xattr_header *xh = loc->xl_header;
1901 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1902
Joel Becker11179f22009-08-14 16:07:44 -07001903 ocfs2_xa_wipe_namevalue(loc);
Joel Beckerbde1e542009-08-14 16:58:38 -07001904 loc->xl_entry = NULL;
1905
1906 le16_add_cpu(&xh->xh_count, -1);
1907 count = le16_to_cpu(xh->xh_count);
1908
1909 /*
1910 * Only zero out the entry if there are more remaining. This is
1911 * important for an empty bucket, as it keeps track of the
1912 * bucket's hash value. It doesn't hurt empty block storage.
1913 */
1914 if (count) {
1915 index = ((char *)entry - (char *)&xh->xh_entries) /
1916 sizeof(struct ocfs2_xattr_entry);
1917 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1918 (count - index) * sizeof(struct ocfs2_xattr_entry));
1919 memset(&xh->xh_entries[count], 0,
1920 sizeof(struct ocfs2_xattr_entry));
1921 }
Joel Becker11179f22009-08-14 16:07:44 -07001922}
1923
Joel Becker399ff3a72009-09-01 18:38:27 -07001924/*
1925 * If we have a problem adjusting the size of an external value during
1926 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1927 * in an intermediate state. For example, the value may be partially
1928 * truncated.
1929 *
1930 * If the value tree hasn't changed, the extend/truncate went nowhere.
1931 * We have nothing to do. The caller can treat it as a straight error.
1932 *
1933 * If the value tree got partially truncated, we now have a corrupted
1934 * extended attribute. We're going to wipe its entry and leak the
1935 * clusters. Better to leak some storage than leave a corrupt entry.
1936 *
1937 * If the value tree grew, it obviously didn't grow enough for the
1938 * new entry. We're not going to try and reclaim those clusters either.
1939 * If there was already an external value there (orig_clusters != 0),
1940 * the new clusters are attached safely and we can just leave the old
1941 * value in place. If there was no external value there, we remove
1942 * the entry.
1943 *
1944 * This way, the xattr block we store in the journal will be consistent.
1945 * If the size change broke because of the journal, no changes will hit
1946 * disk anyway.
1947 */
1948static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1949 const char *what,
1950 unsigned int orig_clusters)
1951{
1952 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1953 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1954 le16_to_cpu(loc->xl_entry->xe_name_offset));
1955
1956 if (new_clusters < orig_clusters) {
1957 mlog(ML_ERROR,
1958 "Partial truncate while %s xattr %.*s. Leaking "
1959 "%u clusters and removing the entry\n",
1960 what, loc->xl_entry->xe_name_len, nameval_buf,
1961 orig_clusters - new_clusters);
1962 ocfs2_xa_remove_entry(loc);
1963 } else if (!orig_clusters) {
1964 mlog(ML_ERROR,
1965 "Unable to allocate an external value for xattr "
1966 "%.*s safely. Leaking %u clusters and removing the "
1967 "entry\n",
1968 loc->xl_entry->xe_name_len, nameval_buf,
1969 new_clusters - orig_clusters);
1970 ocfs2_xa_remove_entry(loc);
1971 } else if (new_clusters > orig_clusters)
1972 mlog(ML_ERROR,
1973 "Unable to grow xattr %.*s safely. %u new clusters "
1974 "have been added, but the value will not be "
1975 "modified\n",
1976 loc->xl_entry->xe_name_len, nameval_buf,
1977 new_clusters - orig_clusters);
1978}
1979
Joel Becker73857ee2009-08-18 20:26:41 -07001980static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1981 struct ocfs2_xattr_set_ctxt *ctxt)
1982{
1983 int rc = 0;
Joel Becker399ff3a72009-09-01 18:38:27 -07001984 unsigned int orig_clusters;
Joel Becker73857ee2009-08-18 20:26:41 -07001985
1986 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
Joel Becker399ff3a72009-09-01 18:38:27 -07001987 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07001988 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
1989 if (rc) {
1990 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07001991 /*
1992 * Since this is remove, we can return 0 if
1993 * ocfs2_xa_cleanup_value_truncate() is going to
1994 * wipe the entry anyway. So we check the
1995 * cluster count as well.
1996 */
1997 if (orig_clusters != ocfs2_xa_value_clusters(loc))
1998 rc = 0;
1999 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2000 orig_clusters);
2001 if (rc)
2002 goto out;
Joel Becker73857ee2009-08-18 20:26:41 -07002003 }
2004 }
2005
2006 ocfs2_xa_remove_entry(loc);
2007
2008out:
2009 return rc;
2010}
2011
2012static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
2013{
2014 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
2015 char *nameval_buf;
2016
2017 nameval_buf = ocfs2_xa_offset_pointer(loc,
2018 le16_to_cpu(loc->xl_entry->xe_name_offset));
2019 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2020}
2021
2022/*
2023 * Take an existing entry and make it ready for the new value. This
2024 * won't allocate space, but it may free space. It should be ready for
2025 * ocfs2_xa_prepare_entry() to finish the work.
2026 */
2027static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
2028 struct ocfs2_xattr_info *xi,
2029 struct ocfs2_xattr_set_ctxt *ctxt)
2030{
2031 int rc = 0;
2032 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker399ff3a72009-09-01 18:38:27 -07002033 unsigned int orig_clusters;
Joel Becker73857ee2009-08-18 20:26:41 -07002034 char *nameval_buf;
2035 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
2036 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
2037
2038 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2039 name_size);
2040
2041 nameval_buf = ocfs2_xa_offset_pointer(loc,
2042 le16_to_cpu(loc->xl_entry->xe_name_offset));
2043 if (xe_local) {
2044 memset(nameval_buf + name_size, 0,
2045 namevalue_size_xe(loc->xl_entry) - name_size);
2046 if (!xi_local)
2047 ocfs2_xa_install_value_root(loc);
2048 } else {
Joel Becker399ff3a72009-09-01 18:38:27 -07002049 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002050 if (xi_local) {
2051 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002052 if (rc < 0)
Joel Becker73857ee2009-08-18 20:26:41 -07002053 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002054 else
2055 memset(nameval_buf + name_size, 0,
2056 namevalue_size_xe(loc->xl_entry) -
2057 name_size);
Joel Becker73857ee2009-08-18 20:26:41 -07002058 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2059 xi->xi_value_len) {
2060 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2061 ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002062 if (rc < 0)
Joel Becker73857ee2009-08-18 20:26:41 -07002063 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002064 }
2065
2066 if (rc) {
2067 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
2068 orig_clusters);
2069 goto out;
Joel Becker73857ee2009-08-18 20:26:41 -07002070 }
2071 }
2072
2073 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
2074 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
2075
2076out:
2077 return rc;
2078}
2079
Joel Becker69a3e532009-08-17 12:24:39 -07002080/*
2081 * Prepares loc->xl_entry to receive the new xattr. This includes
2082 * properly setting up the name+value pair region. If loc->xl_entry
2083 * already exists, it will take care of modifying it appropriately.
Joel Becker69a3e532009-08-17 12:24:39 -07002084 *
2085 * Note that this modifies the data. You did journal_access already,
2086 * right?
2087 */
2088static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
2089 struct ocfs2_xattr_info *xi,
Joel Becker73857ee2009-08-18 20:26:41 -07002090 u32 name_hash,
2091 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002092{
2093 int rc = 0;
Joel Becker399ff3a72009-09-01 18:38:27 -07002094 unsigned int orig_clusters;
2095 __le64 orig_value_size = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002096
Joel Becker69a3e532009-08-17 12:24:39 -07002097 rc = ocfs2_xa_check_space(loc, xi);
2098 if (rc)
2099 goto out;
2100
2101 if (loc->xl_entry) {
2102 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
Joel Becker399ff3a72009-09-01 18:38:27 -07002103 orig_value_size = loc->xl_entry->xe_value_size;
Joel Becker73857ee2009-08-18 20:26:41 -07002104 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
2105 if (rc)
2106 goto out;
2107 goto alloc_value;
Joel Becker69a3e532009-08-17 12:24:39 -07002108 }
2109
Joel Becker73857ee2009-08-18 20:26:41 -07002110 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
Joel Becker399ff3a72009-09-01 18:38:27 -07002111 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002112 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2113 if (rc) {
2114 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002115 ocfs2_xa_cleanup_value_truncate(loc,
2116 "overwriting",
2117 orig_clusters);
Joel Becker73857ee2009-08-18 20:26:41 -07002118 goto out;
2119 }
2120 }
Joel Becker69a3e532009-08-17 12:24:39 -07002121 ocfs2_xa_wipe_namevalue(loc);
2122 } else
2123 ocfs2_xa_add_entry(loc, name_hash);
2124
2125 /*
2126 * If we get here, we have a blank entry. Fill it. We grow our
2127 * name+value pair back from the end.
2128 */
2129 ocfs2_xa_add_namevalue(loc, xi);
Joel Becker73857ee2009-08-18 20:26:41 -07002130 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2131 ocfs2_xa_install_value_root(loc);
2132
2133alloc_value:
2134 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Joel Becker399ff3a72009-09-01 18:38:27 -07002135 orig_clusters = ocfs2_xa_value_clusters(loc);
Joel Becker73857ee2009-08-18 20:26:41 -07002136 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002137 if (rc < 0) {
2138 /*
2139 * If we tried to grow an existing external value,
2140 * ocfs2_xa_cleanuP-value_truncate() is going to
2141 * let it stand. We have to restore its original
2142 * value size.
2143 */
2144 loc->xl_entry->xe_value_size = orig_value_size;
2145 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2146 orig_clusters);
Joel Becker73857ee2009-08-18 20:26:41 -07002147 mlog_errno(rc);
Joel Becker399ff3a72009-09-01 18:38:27 -07002148 }
Joel Becker73857ee2009-08-18 20:26:41 -07002149 }
Joel Becker69a3e532009-08-17 12:24:39 -07002150
2151out:
2152 return rc;
2153}
2154
2155/*
Joel Becker73857ee2009-08-18 20:26:41 -07002156 * Store the value portion of the name+value pair. This will skip
2157 * values that are stored externally. Their tree roots were set up
2158 * by ocfs2_xa_prepare_entry().
Joel Becker69a3e532009-08-17 12:24:39 -07002159 */
Joel Becker73857ee2009-08-18 20:26:41 -07002160static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2161 struct ocfs2_xattr_info *xi,
2162 struct ocfs2_xattr_set_ctxt *ctxt)
Joel Becker69a3e532009-08-17 12:24:39 -07002163{
Joel Becker73857ee2009-08-18 20:26:41 -07002164 int rc = 0;
Joel Becker69a3e532009-08-17 12:24:39 -07002165 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2166 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
Joel Becker69a3e532009-08-17 12:24:39 -07002167 char *nameval_buf;
Joel Becker73857ee2009-08-18 20:26:41 -07002168 struct ocfs2_xattr_value_buf vb;
Joel Becker69a3e532009-08-17 12:24:39 -07002169
Joel Becker69a3e532009-08-17 12:24:39 -07002170 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
Joel Becker73857ee2009-08-18 20:26:41 -07002171 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2172 ocfs2_xa_fill_value_buf(loc, &vb);
2173 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2174 ctxt->handle, &vb,
2175 xi->xi_value,
2176 xi->xi_value_len);
2177 } else
2178 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2179
Joel Becker73857ee2009-08-18 20:26:41 -07002180 return rc;
Joel Becker69a3e532009-08-17 12:24:39 -07002181}
2182
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002183static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2184 struct ocfs2_xattr_info *xi,
2185 struct ocfs2_xattr_set_ctxt *ctxt)
2186{
2187 int ret;
2188 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2189 xi->xi_name_len);
2190
2191 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
2192 OCFS2_JOURNAL_ACCESS_WRITE);
2193 if (ret) {
2194 mlog_errno(ret);
2195 goto out;
2196 }
2197
Joel Becker399ff3a72009-09-01 18:38:27 -07002198 /*
2199 * From here on out, everything is going to modify the buffer a
2200 * little. Errors are going to leave the xattr header in a
2201 * sane state. Thus, even with errors we dirty the sucker.
2202 */
2203
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002204 /* Don't worry, we are never called with !xi_value and !xl_entry */
2205 if (!xi->xi_value) {
2206 ret = ocfs2_xa_remove(loc, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002207 goto out_dirty;
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002208 }
2209
2210 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
2211 if (ret) {
2212 if (ret != -ENOSPC)
2213 mlog_errno(ret);
Joel Becker399ff3a72009-09-01 18:38:27 -07002214 goto out_dirty;
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002215 }
2216
2217 ret = ocfs2_xa_store_value(loc, xi, ctxt);
Joel Becker399ff3a72009-09-01 18:38:27 -07002218 if (ret)
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002219 mlog_errno(ret);
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002220
Joel Becker399ff3a72009-09-01 18:38:27 -07002221out_dirty:
Joel Beckerbca5e9b2009-08-18 20:40:14 -07002222 ocfs2_xa_journal_dirty(ctxt->handle, loc);
2223
2224out:
2225 return ret;
2226}
2227
Joel Becker11179f22009-08-14 16:07:44 -07002228static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2229 struct inode *inode,
2230 struct buffer_head *bh,
2231 struct ocfs2_xattr_entry *entry)
2232{
2233 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2234
Joel Becker139ffac2009-08-19 11:09:17 -07002235 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2236
Joel Beckercf2bc802009-08-18 13:52:38 -07002237 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002238 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2239 loc->xl_storage = bh;
2240 loc->xl_entry = entry;
Joel Becker139ffac2009-08-19 11:09:17 -07002241 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
Joel Becker11179f22009-08-14 16:07:44 -07002242 loc->xl_header =
2243 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2244 loc->xl_size);
2245}
2246
2247static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
Joel Beckercf2bc802009-08-18 13:52:38 -07002248 struct inode *inode,
Joel Becker11179f22009-08-14 16:07:44 -07002249 struct buffer_head *bh,
2250 struct ocfs2_xattr_entry *entry)
2251{
2252 struct ocfs2_xattr_block *xb =
2253 (struct ocfs2_xattr_block *)bh->b_data;
2254
2255 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2256
Joel Beckercf2bc802009-08-18 13:52:38 -07002257 loc->xl_inode = inode;
Joel Becker11179f22009-08-14 16:07:44 -07002258 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2259 loc->xl_storage = bh;
2260 loc->xl_header = &(xb->xb_attrs.xb_header);
2261 loc->xl_entry = entry;
2262 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2263 xb_attrs.xb_header);
2264}
2265
2266static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2267 struct ocfs2_xattr_bucket *bucket,
2268 struct ocfs2_xattr_entry *entry)
2269{
Joel Beckercf2bc802009-08-18 13:52:38 -07002270 loc->xl_inode = bucket->bu_inode;
Joel Becker11179f22009-08-14 16:07:44 -07002271 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2272 loc->xl_storage = bucket;
2273 loc->xl_header = bucket_xh(bucket);
2274 loc->xl_entry = entry;
2275 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2276}
2277
Tao Mace9c5a52009-08-18 11:43:59 +08002278/*
2279 * In xattr remove, if it is stored outside and refcounted, we may have
2280 * the chance to split the refcount tree. So need the allocators.
2281 */
2282static int ocfs2_lock_xattr_remove_allocators(struct inode *inode,
2283 struct ocfs2_xattr_value_root *xv,
2284 struct ocfs2_caching_info *ref_ci,
2285 struct buffer_head *ref_root_bh,
2286 struct ocfs2_alloc_context **meta_ac,
2287 int *ref_credits)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002288{
Tao Mace9c5a52009-08-18 11:43:59 +08002289 int ret, meta_add = 0;
2290 u32 p_cluster, num_clusters;
2291 unsigned int ext_flags;
Tao Ma78f30c32008-11-12 08:27:00 +08002292
Tao Mace9c5a52009-08-18 11:43:59 +08002293 *ref_credits = 0;
2294 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
2295 &num_clusters,
2296 &xv->xr_list,
2297 &ext_flags);
2298 if (ret) {
Tao Ma85db90e2008-11-12 08:27:01 +08002299 mlog_errno(ret);
2300 goto out;
2301 }
2302
Tao Mace9c5a52009-08-18 11:43:59 +08002303 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
2304 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002305
Tao Mace9c5a52009-08-18 11:43:59 +08002306 ret = ocfs2_refcounted_xattr_delete_need(inode, ref_ci,
2307 ref_root_bh, xv,
2308 &meta_add, ref_credits);
2309 if (ret) {
2310 mlog_errno(ret);
2311 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002312 }
2313
Tao Mace9c5a52009-08-18 11:43:59 +08002314 ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2315 meta_add, meta_ac);
2316 if (ret)
2317 mlog_errno(ret);
2318
Tao Ma85db90e2008-11-12 08:27:01 +08002319out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002320 return ret;
2321}
2322
Tao Mace9c5a52009-08-18 11:43:59 +08002323static int ocfs2_remove_value_outside(struct inode*inode,
2324 struct ocfs2_xattr_value_buf *vb,
2325 struct ocfs2_xattr_header *header,
2326 struct ocfs2_caching_info *ref_ci,
2327 struct buffer_head *ref_root_bh)
2328{
2329 int ret = 0, i, ref_credits;
2330 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2331 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
2332 void *val;
2333
2334 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
2335
2336 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
2337 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
2338
2339 if (ocfs2_xattr_is_local(entry))
2340 continue;
2341
2342 val = (void *)header +
2343 le16_to_cpu(entry->xe_name_offset);
2344 vb->vb_xv = (struct ocfs2_xattr_value_root *)
2345 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
2346
2347 ret = ocfs2_lock_xattr_remove_allocators(inode, vb->vb_xv,
2348 ref_ci, ref_root_bh,
2349 &ctxt.meta_ac,
2350 &ref_credits);
2351
2352 ctxt.handle = ocfs2_start_trans(osb, ref_credits +
2353 ocfs2_remove_extent_credits(osb->sb));
2354 if (IS_ERR(ctxt.handle)) {
2355 ret = PTR_ERR(ctxt.handle);
2356 mlog_errno(ret);
2357 break;
2358 }
2359
2360 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
2361 if (ret < 0) {
2362 mlog_errno(ret);
2363 break;
2364 }
2365
2366 ocfs2_commit_trans(osb, ctxt.handle);
2367 if (ctxt.meta_ac) {
2368 ocfs2_free_alloc_context(ctxt.meta_ac);
2369 ctxt.meta_ac = NULL;
2370 }
2371 }
2372
2373 if (ctxt.meta_ac)
2374 ocfs2_free_alloc_context(ctxt.meta_ac);
2375 ocfs2_schedule_truncate_log_flush(osb, 1);
2376 ocfs2_run_deallocs(osb, &ctxt.dealloc);
2377 return ret;
2378}
2379
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002380static int ocfs2_xattr_ibody_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002381 struct buffer_head *di_bh,
2382 struct ocfs2_caching_info *ref_ci,
2383 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002384{
2385
2386 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2387 struct ocfs2_xattr_header *header;
2388 int ret;
Joel Becker43119012008-12-09 16:24:43 -08002389 struct ocfs2_xattr_value_buf vb = {
2390 .vb_bh = di_bh,
2391 .vb_access = ocfs2_journal_access_di,
2392 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002393
2394 header = (struct ocfs2_xattr_header *)
2395 ((void *)di + inode->i_sb->s_blocksize -
2396 le16_to_cpu(di->i_xattr_inline_size));
2397
Tao Mace9c5a52009-08-18 11:43:59 +08002398 ret = ocfs2_remove_value_outside(inode, &vb, header,
2399 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002400
2401 return ret;
2402}
2403
Tao Mace9c5a52009-08-18 11:43:59 +08002404struct ocfs2_rm_xattr_bucket_para {
2405 struct ocfs2_caching_info *ref_ci;
2406 struct buffer_head *ref_root_bh;
2407};
2408
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002409static int ocfs2_xattr_block_remove(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002410 struct buffer_head *blk_bh,
2411 struct ocfs2_caching_info *ref_ci,
2412 struct buffer_head *ref_root_bh)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002413{
2414 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002415 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08002416 struct ocfs2_xattr_value_buf vb = {
2417 .vb_bh = blk_bh,
2418 .vb_access = ocfs2_journal_access_xb,
2419 };
Tao Mace9c5a52009-08-18 11:43:59 +08002420 struct ocfs2_rm_xattr_bucket_para args = {
2421 .ref_ci = ref_ci,
2422 .ref_root_bh = ref_root_bh,
2423 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002424
2425 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08002426 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2427 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Tao Mace9c5a52009-08-18 11:43:59 +08002428 ret = ocfs2_remove_value_outside(inode, &vb, header,
2429 ref_ci, ref_root_bh);
Tao Maa3944252008-08-18 17:38:54 +08002430 } else
Tao Ma47bca492009-08-18 11:43:42 +08002431 ret = ocfs2_iterate_xattr_index_block(inode,
2432 blk_bh,
2433 ocfs2_rm_xattr_cluster,
Tao Mace9c5a52009-08-18 11:43:59 +08002434 &args);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002435
2436 return ret;
2437}
2438
Tao Ma08413892008-08-29 09:00:19 +08002439static int ocfs2_xattr_free_block(struct inode *inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002440 u64 block,
2441 struct ocfs2_caching_info *ref_ci,
2442 struct buffer_head *ref_root_bh)
Tao Ma08413892008-08-29 09:00:19 +08002443{
2444 struct inode *xb_alloc_inode;
2445 struct buffer_head *xb_alloc_bh = NULL;
2446 struct buffer_head *blk_bh = NULL;
2447 struct ocfs2_xattr_block *xb;
2448 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2449 handle_t *handle;
2450 int ret = 0;
2451 u64 blk, bg_blkno;
2452 u16 bit;
2453
Joel Becker4ae1d692008-11-13 14:49:18 -08002454 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08002455 if (ret < 0) {
2456 mlog_errno(ret);
2457 goto out;
2458 }
2459
Tao Mace9c5a52009-08-18 11:43:59 +08002460 ret = ocfs2_xattr_block_remove(inode, blk_bh, ref_ci, ref_root_bh);
Tao Ma08413892008-08-29 09:00:19 +08002461 if (ret < 0) {
2462 mlog_errno(ret);
2463 goto out;
2464 }
2465
Joel Becker4ae1d692008-11-13 14:49:18 -08002466 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08002467 blk = le64_to_cpu(xb->xb_blkno);
2468 bit = le16_to_cpu(xb->xb_suballoc_bit);
2469 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2470
2471 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
2472 EXTENT_ALLOC_SYSTEM_INODE,
2473 le16_to_cpu(xb->xb_suballoc_slot));
2474 if (!xb_alloc_inode) {
2475 ret = -ENOMEM;
2476 mlog_errno(ret);
2477 goto out;
2478 }
2479 mutex_lock(&xb_alloc_inode->i_mutex);
2480
2481 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
2482 if (ret < 0) {
2483 mlog_errno(ret);
2484 goto out_mutex;
2485 }
2486
2487 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
2488 if (IS_ERR(handle)) {
2489 ret = PTR_ERR(handle);
2490 mlog_errno(ret);
2491 goto out_unlock;
2492 }
2493
2494 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
2495 bit, bg_blkno, 1);
2496 if (ret < 0)
2497 mlog_errno(ret);
2498
2499 ocfs2_commit_trans(osb, handle);
2500out_unlock:
2501 ocfs2_inode_unlock(xb_alloc_inode, 1);
2502 brelse(xb_alloc_bh);
2503out_mutex:
2504 mutex_unlock(&xb_alloc_inode->i_mutex);
2505 iput(xb_alloc_inode);
2506out:
2507 brelse(blk_bh);
2508 return ret;
2509}
2510
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002511/*
2512 * ocfs2_xattr_remove()
2513 *
2514 * Free extended attribute resources associated with this inode.
2515 */
2516int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
2517{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002518 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2519 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Tao Mace9c5a52009-08-18 11:43:59 +08002520 struct ocfs2_refcount_tree *ref_tree = NULL;
2521 struct buffer_head *ref_root_bh = NULL;
2522 struct ocfs2_caching_info *ref_ci = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002523 handle_t *handle;
2524 int ret;
2525
Tiger Yang8154da32008-08-18 17:11:46 +08002526 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2527 return 0;
2528
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002529 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
2530 return 0;
2531
Tao Mace9c5a52009-08-18 11:43:59 +08002532 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
2533 ret = ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb),
2534 le64_to_cpu(di->i_refcount_loc),
2535 1, &ref_tree, &ref_root_bh);
2536 if (ret) {
2537 mlog_errno(ret);
2538 goto out;
2539 }
2540 ref_ci = &ref_tree->rf_ci;
2541
2542 }
2543
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002544 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
Tao Mace9c5a52009-08-18 11:43:59 +08002545 ret = ocfs2_xattr_ibody_remove(inode, di_bh,
2546 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002547 if (ret < 0) {
2548 mlog_errno(ret);
2549 goto out;
2550 }
2551 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002552
Tao Ma08413892008-08-29 09:00:19 +08002553 if (di->i_xattr_loc) {
2554 ret = ocfs2_xattr_free_block(inode,
Tao Mace9c5a52009-08-18 11:43:59 +08002555 le64_to_cpu(di->i_xattr_loc),
2556 ref_ci, ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002557 if (ret < 0) {
2558 mlog_errno(ret);
2559 goto out;
2560 }
2561 }
2562
2563 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
2564 OCFS2_INODE_UPDATE_CREDITS);
2565 if (IS_ERR(handle)) {
2566 ret = PTR_ERR(handle);
2567 mlog_errno(ret);
2568 goto out;
2569 }
Joel Becker0cf2f762009-02-12 16:41:25 -08002570 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08002571 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002572 if (ret) {
2573 mlog_errno(ret);
2574 goto out_commit;
2575 }
2576
Tao Ma08413892008-08-29 09:00:19 +08002577 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002578
2579 spin_lock(&oi->ip_lock);
2580 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
2581 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2582 spin_unlock(&oi->ip_lock);
2583
Joel Beckerec20cec2010-03-19 14:13:52 -07002584 ocfs2_journal_dirty(handle, di_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002585out_commit:
2586 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2587out:
Tao Mace9c5a52009-08-18 11:43:59 +08002588 if (ref_tree)
2589 ocfs2_unlock_refcount_tree(OCFS2_SB(inode->i_sb), ref_tree, 1);
2590 brelse(ref_root_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002591 return ret;
2592}
2593
2594static int ocfs2_xattr_has_space_inline(struct inode *inode,
2595 struct ocfs2_dinode *di)
2596{
2597 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2598 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
2599 int free;
2600
2601 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
2602 return 0;
2603
2604 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2605 struct ocfs2_inline_data *idata = &di->id2.i_data;
2606 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
2607 } else if (ocfs2_inode_is_fast_symlink(inode)) {
2608 free = ocfs2_fast_symlink_chars(inode->i_sb) -
2609 le64_to_cpu(di->i_size);
2610 } else {
2611 struct ocfs2_extent_list *el = &di->id2.i_list;
2612 free = (le16_to_cpu(el->l_count) -
2613 le16_to_cpu(el->l_next_free_rec)) *
2614 sizeof(struct ocfs2_extent_rec);
2615 }
2616 if (free >= xattrsize)
2617 return 1;
2618
2619 return 0;
2620}
2621
2622/*
2623 * ocfs2_xattr_ibody_find()
2624 *
2625 * Find extended attribute in inode block and
2626 * fill search info into struct ocfs2_xattr_search.
2627 */
2628static int ocfs2_xattr_ibody_find(struct inode *inode,
2629 int name_index,
2630 const char *name,
2631 struct ocfs2_xattr_search *xs)
2632{
2633 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2634 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2635 int ret;
2636 int has_space = 0;
2637
2638 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2639 return 0;
2640
2641 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2642 down_read(&oi->ip_alloc_sem);
2643 has_space = ocfs2_xattr_has_space_inline(inode, di);
2644 up_read(&oi->ip_alloc_sem);
2645 if (!has_space)
2646 return 0;
2647 }
2648
2649 xs->xattr_bh = xs->inode_bh;
2650 xs->end = (void *)di + inode->i_sb->s_blocksize;
2651 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2652 xs->header = (struct ocfs2_xattr_header *)
2653 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2654 else
2655 xs->header = (struct ocfs2_xattr_header *)
2656 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2657 xs->base = (void *)xs->header;
2658 xs->here = xs->header->xh_entries;
2659
2660 /* Find the named attribute. */
2661 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2662 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2663 if (ret && ret != -ENODATA)
2664 return ret;
2665 xs->not_found = ret;
2666 }
2667
2668 return 0;
2669}
2670
Joel Becker139ffac2009-08-19 11:09:17 -07002671static int ocfs2_xattr_ibody_init(struct inode *inode,
2672 struct buffer_head *di_bh,
2673 struct ocfs2_xattr_set_ctxt *ctxt)
2674{
2675 int ret;
2676 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2677 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2678 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2679 unsigned int xattrsize = osb->s_xattr_inline_size;
2680
2681 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2682 ret = -ENOSPC;
2683 goto out;
2684 }
2685
2686 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2687 OCFS2_JOURNAL_ACCESS_WRITE);
2688 if (ret) {
2689 mlog_errno(ret);
2690 goto out;
2691 }
2692
2693 /*
2694 * Adjust extent record count or inline data size
2695 * to reserve space for extended attribute.
2696 */
2697 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2698 struct ocfs2_inline_data *idata = &di->id2.i_data;
2699 le16_add_cpu(&idata->id_count, -xattrsize);
2700 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2701 struct ocfs2_extent_list *el = &di->id2.i_list;
2702 le16_add_cpu(&el->l_count, -(xattrsize /
2703 sizeof(struct ocfs2_extent_rec)));
2704 }
2705 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2706
2707 spin_lock(&oi->ip_lock);
2708 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2709 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2710 spin_unlock(&oi->ip_lock);
2711
Joel Beckerec20cec2010-03-19 14:13:52 -07002712 ocfs2_journal_dirty(ctxt->handle, di_bh);
Joel Becker139ffac2009-08-19 11:09:17 -07002713
2714out:
2715 return ret;
2716}
2717
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002718/*
2719 * ocfs2_xattr_ibody_set()
2720 *
2721 * Set, replace or remove an extended attribute into inode block.
2722 *
2723 */
2724static int ocfs2_xattr_ibody_set(struct inode *inode,
2725 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002726 struct ocfs2_xattr_search *xs,
2727 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002728{
Joel Becker139ffac2009-08-19 11:09:17 -07002729 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002730 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2731 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Joel Becker139ffac2009-08-19 11:09:17 -07002732 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002733
2734 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2735 return -ENOSPC;
2736
2737 down_write(&oi->ip_alloc_sem);
2738 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2739 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2740 ret = -ENOSPC;
2741 goto out;
2742 }
2743 }
2744
Joel Becker139ffac2009-08-19 11:09:17 -07002745 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2746 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2747 if (ret) {
2748 if (ret != -ENOSPC)
2749 mlog_errno(ret);
2750 goto out;
2751 }
2752 }
2753
2754 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2755 xs->not_found ? NULL : xs->here);
2756 ret = ocfs2_xa_set(&loc, xi, ctxt);
2757 if (ret) {
2758 if (ret != -ENOSPC)
2759 mlog_errno(ret);
2760 goto out;
2761 }
2762 xs->here = loc.xl_entry;
2763
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002764out:
2765 up_write(&oi->ip_alloc_sem);
2766
2767 return ret;
2768}
2769
2770/*
2771 * ocfs2_xattr_block_find()
2772 *
2773 * Find extended attribute in external block and
2774 * fill search info into struct ocfs2_xattr_search.
2775 */
2776static int ocfs2_xattr_block_find(struct inode *inode,
2777 int name_index,
2778 const char *name,
2779 struct ocfs2_xattr_search *xs)
2780{
2781 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2782 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002783 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002784 int ret = 0;
2785
2786 if (!di->i_xattr_loc)
2787 return ret;
2788
Joel Becker4ae1d692008-11-13 14:49:18 -08002789 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2790 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002791 if (ret < 0) {
2792 mlog_errno(ret);
2793 return ret;
2794 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002795
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002796 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002797 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002798
Tao Ma589dc262008-08-18 17:38:51 +08002799 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2800 xs->header = &xb->xb_attrs.xb_header;
2801 xs->base = (void *)xs->header;
2802 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2803 xs->here = xs->header->xh_entries;
2804
2805 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2806 } else
2807 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2808 name_index,
2809 name, xs);
2810
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002811 if (ret && ret != -ENODATA) {
2812 xs->xattr_bh = NULL;
2813 goto cleanup;
2814 }
2815 xs->not_found = ret;
2816 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002817cleanup:
2818 brelse(blk_bh);
2819
2820 return ret;
2821}
2822
Joel Beckerd3981542009-08-19 02:13:50 -07002823static int ocfs2_create_xattr_block(struct inode *inode,
Tao Ma5aea1f02009-08-18 11:43:24 +08002824 struct buffer_head *inode_bh,
Joel Beckerd3981542009-08-19 02:13:50 -07002825 struct ocfs2_xattr_set_ctxt *ctxt,
2826 int indexed,
2827 struct buffer_head **ret_bh)
Tao Ma5aea1f02009-08-18 11:43:24 +08002828{
2829 int ret;
2830 u16 suballoc_bit_start;
2831 u32 num_got;
2832 u64 first_blkno;
2833 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
Tao Ma5aea1f02009-08-18 11:43:24 +08002834 struct buffer_head *new_bh = NULL;
2835 struct ocfs2_xattr_block *xblk;
2836
Joel Beckerd3981542009-08-19 02:13:50 -07002837 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2838 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
Tao Ma5aea1f02009-08-18 11:43:24 +08002839 if (ret < 0) {
2840 mlog_errno(ret);
2841 goto end;
2842 }
2843
Joel Becker1ed9b772010-05-06 13:59:06 +08002844 ret = ocfs2_claim_metadata(ctxt->handle, ctxt->meta_ac, 1,
Tao Ma5aea1f02009-08-18 11:43:24 +08002845 &suballoc_bit_start, &num_got,
2846 &first_blkno);
2847 if (ret < 0) {
2848 mlog_errno(ret);
2849 goto end;
2850 }
2851
2852 new_bh = sb_getblk(inode->i_sb, first_blkno);
2853 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2854
Joel Beckerd3981542009-08-19 02:13:50 -07002855 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
Tao Ma5aea1f02009-08-18 11:43:24 +08002856 new_bh,
2857 OCFS2_JOURNAL_ACCESS_CREATE);
2858 if (ret < 0) {
2859 mlog_errno(ret);
2860 goto end;
2861 }
2862
2863 /* Initialize ocfs2_xattr_block */
2864 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2865 memset(xblk, 0, inode->i_sb->s_blocksize);
2866 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
Joel Beckerd3981542009-08-19 02:13:50 -07002867 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
Tao Ma5aea1f02009-08-18 11:43:24 +08002868 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
Joel Becker1ed9b772010-05-06 13:59:06 +08002869 xblk->xb_fs_generation =
2870 cpu_to_le32(OCFS2_SB(inode->i_sb)->fs_generation);
Tao Ma5aea1f02009-08-18 11:43:24 +08002871 xblk->xb_blkno = cpu_to_le64(first_blkno);
Tao Maa7fe7a32009-08-18 11:43:52 +08002872 if (indexed) {
2873 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2874 xr->xt_clusters = cpu_to_le32(1);
2875 xr->xt_last_eb_blk = 0;
2876 xr->xt_list.l_tree_depth = 0;
2877 xr->xt_list.l_count = cpu_to_le16(
2878 ocfs2_xattr_recs_per_xb(inode->i_sb));
2879 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2880 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2881 }
Joel Beckerd3981542009-08-19 02:13:50 -07002882 ocfs2_journal_dirty(ctxt->handle, new_bh);
Tao Maa7fe7a32009-08-18 11:43:52 +08002883
Joel Beckerd3981542009-08-19 02:13:50 -07002884 /* Add it to the inode */
Tao Ma5aea1f02009-08-18 11:43:24 +08002885 di->i_xattr_loc = cpu_to_le64(first_blkno);
Joel Beckerd3981542009-08-19 02:13:50 -07002886
2887 spin_lock(&OCFS2_I(inode)->ip_lock);
2888 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2889 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2890 spin_unlock(&OCFS2_I(inode)->ip_lock);
2891
2892 ocfs2_journal_dirty(ctxt->handle, inode_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002893
2894 *ret_bh = new_bh;
2895 new_bh = NULL;
2896
2897end:
2898 brelse(new_bh);
2899 return ret;
2900}
2901
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002902/*
2903 * ocfs2_xattr_block_set()
2904 *
2905 * Set, replace or remove an extended attribute into external block.
2906 *
2907 */
2908static int ocfs2_xattr_block_set(struct inode *inode,
2909 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002910 struct ocfs2_xattr_search *xs,
2911 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002912{
2913 struct buffer_head *new_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002914 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002915 int ret;
Joel Beckerd3981542009-08-19 02:13:50 -07002916 struct ocfs2_xa_loc loc;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002917
2918 if (!xs->xattr_bh) {
Joel Beckerd3981542009-08-19 02:13:50 -07002919 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2920 0, &new_bh);
Tao Ma5aea1f02009-08-18 11:43:24 +08002921 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002922 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002923 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002924 }
2925
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002926 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002927 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002928 xs->header = &xblk->xb_attrs.xb_header;
2929 xs->base = (void *)xs->header;
2930 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2931 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002932 } else
2933 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2934
2935 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
Joel Beckerd3981542009-08-19 02:13:50 -07002936 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2937 xs->not_found ? NULL : xs->here);
Tao Ma01225592008-08-18 17:38:53 +08002938
Joel Beckerd3981542009-08-19 02:13:50 -07002939 ret = ocfs2_xa_set(&loc, xi, ctxt);
2940 if (!ret)
2941 xs->here = loc.xl_entry;
2942 else if (ret != -ENOSPC)
Tao Ma01225592008-08-18 17:38:53 +08002943 goto end;
Joel Beckerd3981542009-08-19 02:13:50 -07002944 else {
2945 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2946 if (ret)
2947 goto end;
2948 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002949 }
2950
Joel Beckerd3981542009-08-19 02:13:50 -07002951 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2952 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002953
2954end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002955 return ret;
2956}
2957
Tao Ma78f30c32008-11-12 08:27:00 +08002958/* Check whether the new xattr can be inserted into the inode. */
2959static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2960 struct ocfs2_xattr_info *xi,
2961 struct ocfs2_xattr_search *xs)
2962{
Tao Ma78f30c32008-11-12 08:27:00 +08002963 struct ocfs2_xattr_entry *last;
2964 int free, i;
2965 size_t min_offs = xs->end - xs->base;
2966
2967 if (!xs->header)
2968 return 0;
2969
2970 last = xs->header->xh_entries;
2971
2972 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2973 size_t offs = le16_to_cpu(last->xe_name_offset);
2974 if (offs < min_offs)
2975 min_offs = offs;
2976 last += 1;
2977 }
2978
Tiger Yang4442f512009-02-20 11:11:50 +08002979 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002980 if (free < 0)
2981 return 0;
2982
2983 BUG_ON(!xs->not_found);
2984
Joel Becker199799a2009-08-14 19:04:15 -07002985 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
Tao Ma78f30c32008-11-12 08:27:00 +08002986 return 1;
2987
2988 return 0;
2989}
2990
2991static int ocfs2_calc_xattr_set_need(struct inode *inode,
2992 struct ocfs2_dinode *di,
2993 struct ocfs2_xattr_info *xi,
2994 struct ocfs2_xattr_search *xis,
2995 struct ocfs2_xattr_search *xbs,
2996 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002997 int *meta_need,
2998 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002999{
3000 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003001 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003002 struct buffer_head *bh = NULL;
3003 struct ocfs2_xattr_block *xb = NULL;
3004 struct ocfs2_xattr_entry *xe = NULL;
3005 struct ocfs2_xattr_value_root *xv = NULL;
3006 char *base = NULL;
3007 int name_offset, name_len = 0;
3008 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Joel Becker6b240ff2009-08-14 18:02:52 -07003009 xi->xi_value_len);
Tao Ma78f30c32008-11-12 08:27:00 +08003010 u64 value_size;
3011
Tao Ma71d548a2008-12-05 06:20:54 +08003012 /*
3013 * Calculate the clusters we need to write.
3014 * No matter whether we replace an old one or add a new one,
3015 * we need this for writing.
3016 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003017 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
Tao Ma71d548a2008-12-05 06:20:54 +08003018 credits += new_clusters *
3019 ocfs2_clusters_to_blocks(inode->i_sb, 1);
3020
Tao Ma78f30c32008-11-12 08:27:00 +08003021 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08003022 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3023
Joel Becker6b240ff2009-08-14 18:02:52 -07003024 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003025 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003026 credits += ocfs2_calc_extend_credits(inode->i_sb,
3027 &def_xv.xv.xr_list,
3028 new_clusters);
3029 }
Tao Ma78f30c32008-11-12 08:27:00 +08003030
3031 goto meta_guess;
3032 }
3033
3034 if (!xis->not_found) {
3035 xe = xis->here;
3036 name_offset = le16_to_cpu(xe->xe_name_offset);
3037 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3038 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003039 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08003040 } else {
Joel Becker970e4932008-11-13 14:49:19 -08003041 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003042 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3043 xe = xbs->here;
3044 name_offset = le16_to_cpu(xe->xe_name_offset);
3045 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3046 i = xbs->here - xbs->header->xh_entries;
3047 old_in_xb = 1;
3048
3049 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08003050 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08003051 bucket_xh(xbs->bucket),
3052 i, &block_off,
3053 &name_offset);
3054 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08003055 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3056 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003057 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08003058 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
3059 }
3060 }
3061
3062 /*
3063 * delete a xattr doesn't need metadata and cluster allocation.
3064 * so just calculate the credits and return.
3065 *
3066 * The credits for removing the value tree will be extended
3067 * by ocfs2_remove_extent itself.
3068 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003069 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003070 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02003071 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003072
3073 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003074 }
3075
3076 /* do cluster allocation guess first. */
3077 value_size = le64_to_cpu(xe->xe_value_size);
3078
3079 if (old_in_xb) {
3080 /*
3081 * In xattr set, we always try to set the xe in inode first,
3082 * so if it can be inserted into inode successfully, the old
3083 * one will be removed from the xattr block, and this xattr
3084 * will be inserted into inode as a new xattr in inode.
3085 */
3086 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
3087 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02003088 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08003089 OCFS2_INODE_UPDATE_CREDITS;
3090 if (!ocfs2_xattr_is_local(xe))
3091 credits += ocfs2_calc_extend_credits(
3092 inode->i_sb,
3093 &def_xv.xv.xr_list,
3094 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08003095 goto out;
3096 }
3097 }
3098
Joel Becker6b240ff2009-08-14 18:02:52 -07003099 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08003100 /* the new values will be stored outside. */
3101 u32 old_clusters = 0;
3102
3103 if (!ocfs2_xattr_is_local(xe)) {
3104 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
3105 value_size);
3106 xv = (struct ocfs2_xattr_value_root *)
3107 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08003108 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08003109 } else
3110 xv = &def_xv.xv;
3111
Tao Ma85db90e2008-11-12 08:27:01 +08003112 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02003113 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003114 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08003115 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003116 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
3117 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08003118 credits += ocfs2_calc_extend_credits(inode->i_sb,
3119 &xv->xr_list,
3120 new_clusters -
3121 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08003122 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
3123 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08003124 }
3125 } else {
3126 /*
3127 * Now the new value will be stored inside. So if the new
3128 * value is smaller than the size of value root or the old
3129 * value, we don't need any allocation, otherwise we have
3130 * to guess metadata allocation.
3131 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003132 if ((ocfs2_xattr_is_local(xe) &&
3133 (value_size >= xi->xi_value_len)) ||
Tao Ma78f30c32008-11-12 08:27:00 +08003134 (!ocfs2_xattr_is_local(xe) &&
Joel Becker6b240ff2009-08-14 18:02:52 -07003135 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
Tao Ma78f30c32008-11-12 08:27:00 +08003136 goto out;
3137 }
3138
3139meta_guess:
3140 /* calculate metadata allocation. */
3141 if (di->i_xattr_loc) {
3142 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08003143 ret = ocfs2_read_xattr_block(inode,
3144 le64_to_cpu(di->i_xattr_loc),
3145 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08003146 if (ret) {
3147 mlog_errno(ret);
3148 goto out;
3149 }
3150
3151 xb = (struct ocfs2_xattr_block *)bh->b_data;
3152 } else
3153 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
3154
Tao Ma90cb5462008-12-05 06:20:56 +08003155 /*
3156 * If there is already an xattr tree, good, we can calculate
3157 * like other b-trees. Otherwise we may have the chance of
3158 * create a tree, the credit calculation is borrowed from
3159 * ocfs2_calc_extend_credits with root_el = NULL. And the
3160 * new tree will be cluster based, so no meta is needed.
3161 */
Tao Ma78f30c32008-11-12 08:27:00 +08003162 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
3163 struct ocfs2_extent_list *el =
3164 &xb->xb_attrs.xb_root.xt_list;
3165 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08003166 credits += ocfs2_calc_extend_credits(inode->i_sb,
3167 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08003168 } else
3169 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08003170
3171 /*
3172 * This cluster will be used either for new bucket or for
3173 * new xattr block.
3174 * If the cluster size is the same as the bucket size, one
3175 * more is needed since we may need to extend the bucket
3176 * also.
3177 */
3178 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003179 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003180 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08003181 OCFS2_SB(inode->i_sb)->s_clustersize) {
3182 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08003183 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003184 }
3185 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08003186 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08003187 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
3188 }
Tao Ma78f30c32008-11-12 08:27:00 +08003189out:
3190 if (clusters_need)
3191 *clusters_need = clusters_add;
3192 if (meta_need)
3193 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08003194 if (credits_need)
3195 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08003196 brelse(bh);
3197 return ret;
3198}
3199
3200static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
3201 struct ocfs2_dinode *di,
3202 struct ocfs2_xattr_info *xi,
3203 struct ocfs2_xattr_search *xis,
3204 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003205 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08003206 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08003207 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08003208{
3209 int clusters_add, meta_add, ret;
3210 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3211
3212 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
3213
3214 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
3215
3216 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08003217 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003218 if (ret) {
3219 mlog_errno(ret);
3220 return ret;
3221 }
3222
Tao Ma492a8a32009-08-18 11:43:17 +08003223 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08003224 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
Joel Becker6b240ff2009-08-14 18:02:52 -07003225 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003226
3227 if (meta_add) {
3228 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
3229 &ctxt->meta_ac);
3230 if (ret) {
3231 mlog_errno(ret);
3232 goto out;
3233 }
3234 }
3235
3236 if (clusters_add) {
3237 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
3238 if (ret)
3239 mlog_errno(ret);
3240 }
3241out:
3242 if (ret) {
3243 if (ctxt->meta_ac) {
3244 ocfs2_free_alloc_context(ctxt->meta_ac);
3245 ctxt->meta_ac = NULL;
3246 }
3247
3248 /*
3249 * We cannot have an error and a non null ctxt->data_ac.
3250 */
3251 }
3252
3253 return ret;
3254}
3255
Tao Ma85db90e2008-11-12 08:27:01 +08003256static int __ocfs2_xattr_set_handle(struct inode *inode,
3257 struct ocfs2_dinode *di,
3258 struct ocfs2_xattr_info *xi,
3259 struct ocfs2_xattr_search *xis,
3260 struct ocfs2_xattr_search *xbs,
3261 struct ocfs2_xattr_set_ctxt *ctxt)
3262{
Tao Ma9f868f12008-11-19 16:48:42 +08003263 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003264
Joel Becker6b240ff2009-08-14 18:02:52 -07003265 if (!xi->xi_value) {
Tao Ma85db90e2008-11-12 08:27:01 +08003266 /* Remove existing extended attribute */
3267 if (!xis->not_found)
3268 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3269 else if (!xbs->not_found)
3270 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3271 } else {
3272 /* We always try to set extended attribute into inode first*/
3273 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
3274 if (!ret && !xbs->not_found) {
3275 /*
3276 * If succeed and that extended attribute existing in
3277 * external block, then we will remove it.
3278 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003279 xi->xi_value = NULL;
3280 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003281
Tao Ma9f868f12008-11-19 16:48:42 +08003282 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003283 xis->not_found = -ENODATA;
3284 ret = ocfs2_calc_xattr_set_need(inode,
3285 di,
3286 xi,
3287 xis,
3288 xbs,
3289 NULL,
3290 NULL,
3291 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003292 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003293 if (ret) {
3294 mlog_errno(ret);
3295 goto out;
3296 }
3297
Tao Mac901fb02010-04-26 14:34:57 +08003298 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003299 if (ret) {
3300 mlog_errno(ret);
3301 goto out;
3302 }
3303 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3304 } else if (ret == -ENOSPC) {
3305 if (di->i_xattr_loc && !xbs->xattr_bh) {
3306 ret = ocfs2_xattr_block_find(inode,
Joel Becker6b240ff2009-08-14 18:02:52 -07003307 xi->xi_name_index,
3308 xi->xi_name, xbs);
Tao Ma85db90e2008-11-12 08:27:01 +08003309 if (ret)
3310 goto out;
3311
Tao Ma9f868f12008-11-19 16:48:42 +08003312 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003313 xis->not_found = -ENODATA;
3314 ret = ocfs2_calc_xattr_set_need(inode,
3315 di,
3316 xi,
3317 xis,
3318 xbs,
3319 NULL,
3320 NULL,
3321 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08003322 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08003323 if (ret) {
3324 mlog_errno(ret);
3325 goto out;
3326 }
3327
Tao Mac901fb02010-04-26 14:34:57 +08003328 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003329 if (ret) {
3330 mlog_errno(ret);
3331 goto out;
3332 }
3333 }
3334 /*
3335 * If no space in inode, we will set extended attribute
3336 * into external block.
3337 */
3338 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
3339 if (ret)
3340 goto out;
3341 if (!xis->not_found) {
3342 /*
3343 * If succeed and that extended attribute
3344 * existing in inode, we will remove it.
3345 */
Joel Becker6b240ff2009-08-14 18:02:52 -07003346 xi->xi_value = NULL;
3347 xi->xi_value_len = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08003348 xbs->not_found = -ENODATA;
3349 ret = ocfs2_calc_xattr_set_need(inode,
3350 di,
3351 xi,
3352 xis,
3353 xbs,
3354 NULL,
3355 NULL,
3356 &credits);
3357 if (ret) {
3358 mlog_errno(ret);
3359 goto out;
3360 }
3361
Tao Mac901fb02010-04-26 14:34:57 +08003362 ret = ocfs2_extend_trans(ctxt->handle, credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003363 if (ret) {
3364 mlog_errno(ret);
3365 goto out;
3366 }
3367 ret = ocfs2_xattr_ibody_set(inode, xi,
3368 xis, ctxt);
3369 }
3370 }
3371 }
3372
Tao Ma4b3f6202008-12-05 06:20:55 +08003373 if (!ret) {
3374 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08003375 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08003376 xis->inode_bh,
3377 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08003378 if (ret) {
3379 mlog_errno(ret);
3380 goto out;
3381 }
3382
3383 inode->i_ctime = CURRENT_TIME;
3384 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3385 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3386 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
3387 }
Tao Ma85db90e2008-11-12 08:27:01 +08003388out:
3389 return ret;
3390}
3391
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003392/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08003393 * This function only called duing creating inode
3394 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08003395 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08003396 */
3397int ocfs2_xattr_set_handle(handle_t *handle,
3398 struct inode *inode,
3399 struct buffer_head *di_bh,
3400 int name_index,
3401 const char *name,
3402 const void *value,
3403 size_t value_len,
3404 int flags,
3405 struct ocfs2_alloc_context *meta_ac,
3406 struct ocfs2_alloc_context *data_ac)
3407{
3408 struct ocfs2_dinode *di;
3409 int ret;
3410
3411 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003412 .xi_name_index = name_index,
3413 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003414 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003415 .xi_value = value,
3416 .xi_value_len = value_len,
Tiger Yang6c3faba2008-11-14 11:16:03 +08003417 };
3418
3419 struct ocfs2_xattr_search xis = {
3420 .not_found = -ENODATA,
3421 };
3422
3423 struct ocfs2_xattr_search xbs = {
3424 .not_found = -ENODATA,
3425 };
3426
3427 struct ocfs2_xattr_set_ctxt ctxt = {
3428 .handle = handle,
3429 .meta_ac = meta_ac,
3430 .data_ac = data_ac,
3431 };
3432
3433 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3434 return -EOPNOTSUPP;
3435
Tiger Yang008aafa2008-12-09 16:43:08 +08003436 /*
3437 * In extreme situation, may need xattr bucket when
3438 * block size is too small. And we have already reserved
3439 * the credits for bucket in mknod.
3440 */
3441 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
3442 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3443 if (!xbs.bucket) {
3444 mlog_errno(-ENOMEM);
3445 return -ENOMEM;
3446 }
3447 }
3448
Tiger Yang6c3faba2008-11-14 11:16:03 +08003449 xis.inode_bh = xbs.inode_bh = di_bh;
3450 di = (struct ocfs2_dinode *)di_bh->b_data;
3451
3452 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3453
3454 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3455 if (ret)
3456 goto cleanup;
3457 if (xis.not_found) {
3458 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3459 if (ret)
3460 goto cleanup;
3461 }
3462
3463 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3464
3465cleanup:
3466 up_write(&OCFS2_I(inode)->ip_xattr_sem);
3467 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08003468 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08003469
3470 return ret;
3471}
3472
3473/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003474 * ocfs2_xattr_set()
3475 *
3476 * Set, replace or remove an extended attribute for this inode.
3477 * value is NULL to remove an existing extended attribute, else either
3478 * create or replace an extended attribute.
3479 */
3480int ocfs2_xattr_set(struct inode *inode,
3481 int name_index,
3482 const char *name,
3483 const void *value,
3484 size_t value_len,
3485 int flags)
3486{
3487 struct buffer_head *di_bh = NULL;
3488 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08003489 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08003490 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003491 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08003492 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08003493 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003494
3495 struct ocfs2_xattr_info xi = {
Joel Becker6b240ff2009-08-14 18:02:52 -07003496 .xi_name_index = name_index,
3497 .xi_name = name,
Joel Becker18853b92009-08-14 18:17:07 -07003498 .xi_name_len = strlen(name),
Joel Becker6b240ff2009-08-14 18:02:52 -07003499 .xi_value = value,
3500 .xi_value_len = value_len,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003501 };
3502
3503 struct ocfs2_xattr_search xis = {
3504 .not_found = -ENODATA,
3505 };
3506
3507 struct ocfs2_xattr_search xbs = {
3508 .not_found = -ENODATA,
3509 };
3510
Tiger Yang8154da32008-08-18 17:11:46 +08003511 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
3512 return -EOPNOTSUPP;
3513
Joel Beckerba937122008-10-24 19:13:20 -07003514 /*
3515 * Only xbs will be used on indexed trees. xis doesn't need a
3516 * bucket.
3517 */
3518 xbs.bucket = ocfs2_xattr_bucket_new(inode);
3519 if (!xbs.bucket) {
3520 mlog_errno(-ENOMEM);
3521 return -ENOMEM;
3522 }
3523
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003524 ret = ocfs2_inode_lock(inode, &di_bh, 1);
3525 if (ret < 0) {
3526 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003527 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003528 }
3529 xis.inode_bh = xbs.inode_bh = di_bh;
3530 di = (struct ocfs2_dinode *)di_bh->b_data;
3531
3532 down_write(&OCFS2_I(inode)->ip_xattr_sem);
3533 /*
3534 * Scan inode and external block to find the same name
3535 * extended attribute and collect search infomation.
3536 */
3537 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
3538 if (ret)
3539 goto cleanup;
3540 if (xis.not_found) {
3541 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
3542 if (ret)
3543 goto cleanup;
3544 }
3545
3546 if (xis.not_found && xbs.not_found) {
3547 ret = -ENODATA;
3548 if (flags & XATTR_REPLACE)
3549 goto cleanup;
3550 ret = 0;
3551 if (!value)
3552 goto cleanup;
3553 } else {
3554 ret = -EEXIST;
3555 if (flags & XATTR_CREATE)
3556 goto cleanup;
3557 }
3558
Tao Ma492a8a32009-08-18 11:43:17 +08003559 /* Check whether the value is refcounted and do some prepartion. */
3560 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
3561 (!xis.not_found || !xbs.not_found)) {
3562 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
3563 &xis, &xbs, &ref_tree,
3564 &ref_meta, &ref_credits);
3565 if (ret) {
3566 mlog_errno(ret);
3567 goto cleanup;
3568 }
3569 }
Tao Ma85db90e2008-11-12 08:27:01 +08003570
3571 mutex_lock(&tl_inode->i_mutex);
3572
3573 if (ocfs2_truncate_log_needs_flush(osb)) {
3574 ret = __ocfs2_flush_truncate_log(osb);
3575 if (ret < 0) {
3576 mutex_unlock(&tl_inode->i_mutex);
3577 mlog_errno(ret);
3578 goto cleanup;
3579 }
3580 }
3581 mutex_unlock(&tl_inode->i_mutex);
3582
3583 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08003584 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08003585 if (ret) {
3586 mlog_errno(ret);
3587 goto cleanup;
3588 }
3589
Tao Ma4b3f6202008-12-05 06:20:55 +08003590 /* we need to update inode's ctime field, so add credit for it. */
3591 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08003592 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08003593 if (IS_ERR(ctxt.handle)) {
3594 ret = PTR_ERR(ctxt.handle);
3595 mlog_errno(ret);
3596 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003597 }
Tao Ma85db90e2008-11-12 08:27:01 +08003598
3599 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
3600
3601 ocfs2_commit_trans(osb, ctxt.handle);
3602
Tao Ma78f30c32008-11-12 08:27:00 +08003603 if (ctxt.data_ac)
3604 ocfs2_free_alloc_context(ctxt.data_ac);
3605 if (ctxt.meta_ac)
3606 ocfs2_free_alloc_context(ctxt.meta_ac);
3607 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
3608 ocfs2_schedule_truncate_log_flush(osb, 1);
3609 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003610
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003611cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08003612 if (ref_tree)
3613 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003614 up_write(&OCFS2_I(inode)->ip_xattr_sem);
Tao Ma8b2c0db2009-08-18 11:43:49 +08003615 if (!value && !ret) {
3616 ret = ocfs2_try_remove_refcount_tree(inode, di_bh);
3617 if (ret)
3618 mlog_errno(ret);
3619 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003620 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07003621cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003622 brelse(di_bh);
3623 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07003624 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08003625
3626 return ret;
3627}
3628
Tao Ma0c044f02008-08-18 17:38:50 +08003629/*
3630 * Find the xattr extent rec which may contains name_hash.
3631 * e_cpos will be the first name hash of the xattr rec.
3632 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
3633 */
3634static int ocfs2_xattr_get_rec(struct inode *inode,
3635 u32 name_hash,
3636 u64 *p_blkno,
3637 u32 *e_cpos,
3638 u32 *num_clusters,
3639 struct ocfs2_extent_list *el)
3640{
3641 int ret = 0, i;
3642 struct buffer_head *eb_bh = NULL;
3643 struct ocfs2_extent_block *eb;
3644 struct ocfs2_extent_rec *rec = NULL;
3645 u64 e_blkno = 0;
3646
3647 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003648 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
3649 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08003650 if (ret) {
3651 mlog_errno(ret);
3652 goto out;
3653 }
3654
3655 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
3656 el = &eb->h_list;
3657
3658 if (el->l_tree_depth) {
3659 ocfs2_error(inode->i_sb,
3660 "Inode %lu has non zero tree depth in "
3661 "xattr tree block %llu\n", inode->i_ino,
3662 (unsigned long long)eb_bh->b_blocknr);
3663 ret = -EROFS;
3664 goto out;
3665 }
3666 }
3667
3668 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
3669 rec = &el->l_recs[i];
3670
3671 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
3672 e_blkno = le64_to_cpu(rec->e_blkno);
3673 break;
3674 }
3675 }
3676
3677 if (!e_blkno) {
3678 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
3679 "record (%u, %u, 0) in xattr", inode->i_ino,
3680 le32_to_cpu(rec->e_cpos),
3681 ocfs2_rec_clusters(el, rec));
3682 ret = -EROFS;
3683 goto out;
3684 }
3685
3686 *p_blkno = le64_to_cpu(rec->e_blkno);
3687 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
3688 if (e_cpos)
3689 *e_cpos = le32_to_cpu(rec->e_cpos);
3690out:
3691 brelse(eb_bh);
3692 return ret;
3693}
3694
3695typedef int (xattr_bucket_func)(struct inode *inode,
3696 struct ocfs2_xattr_bucket *bucket,
3697 void *para);
3698
Tao Ma589dc262008-08-18 17:38:51 +08003699static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07003700 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08003701 int name_index,
3702 const char *name,
3703 u32 name_hash,
3704 u16 *xe_index,
3705 int *found)
3706{
3707 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07003708 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08003709 size_t name_len = strlen(name);
3710 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08003711 char *xe_name;
3712
3713 /*
3714 * We don't use binary search in the bucket because there
3715 * may be multiple entries with the same name hash.
3716 */
3717 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3718 xe = &xh->xh_entries[i];
3719
3720 if (name_hash > le32_to_cpu(xe->xe_name_hash))
3721 continue;
3722 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
3723 break;
3724
3725 cmp = name_index - ocfs2_xattr_get_type(xe);
3726 if (!cmp)
3727 cmp = name_len - xe->xe_name_len;
3728 if (cmp)
3729 continue;
3730
Tao Mafd68a892009-08-18 11:43:21 +08003731 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003732 xh,
3733 i,
3734 &block_off,
3735 &new_offset);
3736 if (ret) {
3737 mlog_errno(ret);
3738 break;
3739 }
3740
Joel Becker970e4932008-11-13 14:49:19 -08003741
Joel Beckere2356a32008-10-27 15:01:54 -07003742 xe_name = bucket_block(bucket, block_off) + new_offset;
3743 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003744 *xe_index = i;
3745 *found = 1;
3746 ret = 0;
3747 break;
3748 }
3749 }
3750
3751 return ret;
3752}
3753
3754/*
3755 * Find the specified xattr entry in a series of buckets.
3756 * This series start from p_blkno and last for num_clusters.
3757 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3758 * the num of the valid buckets.
3759 *
3760 * Return the buffer_head this xattr should reside in. And if the xattr's
3761 * hash is in the gap of 2 buckets, return the lower bucket.
3762 */
3763static int ocfs2_xattr_bucket_find(struct inode *inode,
3764 int name_index,
3765 const char *name,
3766 u32 name_hash,
3767 u64 p_blkno,
3768 u32 first_hash,
3769 u32 num_clusters,
3770 struct ocfs2_xattr_search *xs)
3771{
3772 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003773 struct ocfs2_xattr_header *xh = NULL;
3774 struct ocfs2_xattr_entry *xe = NULL;
3775 u16 index = 0;
3776 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3777 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003778 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003779 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003780 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003781
Joel Beckere2356a32008-10-27 15:01:54 -07003782 search = ocfs2_xattr_bucket_new(inode);
3783 if (!search) {
3784 ret = -ENOMEM;
3785 mlog_errno(ret);
3786 goto out;
3787 }
3788
3789 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003790 if (ret) {
3791 mlog_errno(ret);
3792 goto out;
3793 }
3794
Joel Beckere2356a32008-10-27 15:01:54 -07003795 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003796 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003797 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003798 ocfs2_xattr_bucket_relse(search);
3799
Tao Ma589dc262008-08-18 17:38:51 +08003800 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003801 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003802 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003803 if (ret) {
3804 mlog_errno(ret);
3805 goto out;
3806 }
3807
Joel Beckere2356a32008-10-27 15:01:54 -07003808 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003809 xe = &xh->xh_entries[0];
3810 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3811 high_bucket = bucket - 1;
3812 continue;
3813 }
3814
3815 /*
3816 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003817 * bucket is larger than the search one. for an empty
3818 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003819 */
Tao Ma5a095612008-09-19 22:17:41 +08003820 if (xh->xh_count)
3821 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3822
Tao Ma589dc262008-08-18 17:38:51 +08003823 last_hash = le32_to_cpu(xe->xe_name_hash);
3824
Joel Beckere2356a32008-10-27 15:01:54 -07003825 /* record lower_blkno which may be the insert place. */
3826 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003827
3828 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3829 low_bucket = bucket + 1;
3830 continue;
3831 }
3832
3833 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003834 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003835 name_index, name, name_hash,
3836 &index, &found);
3837 if (ret) {
3838 mlog_errno(ret);
3839 goto out;
3840 }
3841 break;
3842 }
3843
3844 /*
3845 * Record the bucket we have found.
3846 * When the xattr's hash value is in the gap of 2 buckets, we will
3847 * always set it to the previous bucket.
3848 */
Joel Beckere2356a32008-10-27 15:01:54 -07003849 if (!lower_blkno)
3850 lower_blkno = p_blkno;
3851
3852 /* This should be in cache - we just read it during the search */
3853 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3854 if (ret) {
3855 mlog_errno(ret);
3856 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003857 }
Tao Ma589dc262008-08-18 17:38:51 +08003858
Joel Beckerba937122008-10-24 19:13:20 -07003859 xs->header = bucket_xh(xs->bucket);
3860 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003861 xs->end = xs->base + inode->i_sb->s_blocksize;
3862
3863 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003864 xs->here = &xs->header->xh_entries[index];
3865 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003866 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003867 } else
3868 ret = -ENODATA;
3869
3870out:
Joel Beckere2356a32008-10-27 15:01:54 -07003871 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003872 return ret;
3873}
3874
3875static int ocfs2_xattr_index_block_find(struct inode *inode,
3876 struct buffer_head *root_bh,
3877 int name_index,
3878 const char *name,
3879 struct ocfs2_xattr_search *xs)
3880{
3881 int ret;
3882 struct ocfs2_xattr_block *xb =
3883 (struct ocfs2_xattr_block *)root_bh->b_data;
3884 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3885 struct ocfs2_extent_list *el = &xb_root->xt_list;
3886 u64 p_blkno = 0;
3887 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003888 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003889
3890 if (le16_to_cpu(el->l_next_free_rec) == 0)
3891 return -ENODATA;
3892
3893 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3894 name, name_hash, name_index);
3895
3896 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3897 &num_clusters, el);
3898 if (ret) {
3899 mlog_errno(ret);
3900 goto out;
3901 }
3902
3903 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3904
3905 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003906 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3907 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003908
3909 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3910 p_blkno, first_hash, num_clusters, xs);
3911
3912out:
3913 return ret;
3914}
3915
Tao Ma0c044f02008-08-18 17:38:50 +08003916static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3917 u64 blkno,
3918 u32 clusters,
3919 xattr_bucket_func *func,
3920 void *para)
3921{
Joel Becker6dde41d2008-10-24 17:16:48 -07003922 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003923 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3924 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003925 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003926
Joel Beckerba937122008-10-24 19:13:20 -07003927 bucket = ocfs2_xattr_bucket_new(inode);
3928 if (!bucket) {
3929 mlog_errno(-ENOMEM);
3930 return -ENOMEM;
3931 }
Tao Ma0c044f02008-08-18 17:38:50 +08003932
3933 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003934 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003935
Joel Beckerba937122008-10-24 19:13:20 -07003936 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3937 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003938 if (ret) {
3939 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003940 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003941 }
3942
Tao Ma0c044f02008-08-18 17:38:50 +08003943 /*
3944 * The real bucket num in this series of blocks is stored
3945 * in the 1st bucket.
3946 */
3947 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003948 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003949
Mark Fashehde29c082008-10-29 14:45:30 -07003950 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3951 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003952 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003953 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003954 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003955 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003956 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003957 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003958 }
3959
Joel Beckerba937122008-10-24 19:13:20 -07003960 ocfs2_xattr_bucket_relse(bucket);
3961 if (ret)
3962 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003963 }
3964
Joel Beckerba937122008-10-24 19:13:20 -07003965 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003966 return ret;
3967}
3968
3969struct ocfs2_xattr_tree_list {
3970 char *buffer;
3971 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003972 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003973};
3974
Tao Mafd68a892009-08-18 11:43:21 +08003975static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08003976 struct ocfs2_xattr_header *xh,
3977 int index,
3978 int *block_off,
3979 int *new_offset)
3980{
3981 u16 name_offset;
3982
3983 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3984 return -EINVAL;
3985
3986 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3987
Tao Mafd68a892009-08-18 11:43:21 +08003988 *block_off = name_offset >> sb->s_blocksize_bits;
3989 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08003990
3991 return 0;
3992}
3993
3994static int ocfs2_list_xattr_bucket(struct inode *inode,
3995 struct ocfs2_xattr_bucket *bucket,
3996 void *para)
3997{
Tao Ma936b8832008-10-09 23:06:14 +08003998 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003999 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08004000 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08004001 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08004002
Joel Becker3e632942008-10-24 17:04:49 -07004003 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
4004 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08004005 type = ocfs2_xattr_get_type(entry);
4006 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08004007
Tao Ma936b8832008-10-09 23:06:14 +08004008 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08004009 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07004010 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08004011 i,
4012 &block_off,
4013 &new_offset);
4014 if (ret)
4015 break;
Tao Ma936b8832008-10-09 23:06:14 +08004016
Joel Becker51def392008-10-24 16:57:21 -07004017 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08004018 new_offset;
4019 ret = ocfs2_xattr_list_entry(xl->buffer,
4020 xl->buffer_size,
4021 &xl->result,
4022 prefix, name,
4023 entry->xe_name_len);
4024 if (ret)
4025 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004026 }
4027 }
4028
4029 return ret;
4030}
4031
Tao Ma47bca492009-08-18 11:43:42 +08004032static int ocfs2_iterate_xattr_index_block(struct inode *inode,
4033 struct buffer_head *blk_bh,
4034 xattr_tree_rec_func *rec_func,
4035 void *para)
Tao Ma0c044f02008-08-18 17:38:50 +08004036{
Tao Ma47bca492009-08-18 11:43:42 +08004037 struct ocfs2_xattr_block *xb =
4038 (struct ocfs2_xattr_block *)blk_bh->b_data;
4039 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
Tao Ma0c044f02008-08-18 17:38:50 +08004040 int ret = 0;
4041 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
4042 u64 p_blkno = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08004043
Tao Ma47bca492009-08-18 11:43:42 +08004044 if (!el->l_next_free_rec || !rec_func)
Tao Ma0c044f02008-08-18 17:38:50 +08004045 return 0;
4046
4047 while (name_hash > 0) {
4048 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4049 &e_cpos, &num_clusters, el);
4050 if (ret) {
4051 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004052 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004053 }
4054
Tao Ma47bca492009-08-18 11:43:42 +08004055 ret = rec_func(inode, blk_bh, p_blkno, e_cpos,
4056 num_clusters, para);
Tao Ma0c044f02008-08-18 17:38:50 +08004057 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08004058 if (ret != -ERANGE)
4059 mlog_errno(ret);
Tao Ma47bca492009-08-18 11:43:42 +08004060 break;
Tao Ma0c044f02008-08-18 17:38:50 +08004061 }
4062
4063 if (e_cpos == 0)
4064 break;
4065
4066 name_hash = e_cpos - 1;
4067 }
4068
Tao Ma47bca492009-08-18 11:43:42 +08004069 return ret;
4070
4071}
4072
4073static int ocfs2_list_xattr_tree_rec(struct inode *inode,
4074 struct buffer_head *root_bh,
4075 u64 blkno, u32 cpos, u32 len, void *para)
4076{
4077 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
4078 ocfs2_list_xattr_bucket, para);
4079}
4080
4081static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
4082 struct buffer_head *blk_bh,
4083 char *buffer,
4084 size_t buffer_size)
4085{
4086 int ret;
4087 struct ocfs2_xattr_tree_list xl = {
4088 .buffer = buffer,
4089 .buffer_size = buffer_size,
4090 .result = 0,
4091 };
4092
4093 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
4094 ocfs2_list_xattr_tree_rec, &xl);
4095 if (ret) {
4096 mlog_errno(ret);
4097 goto out;
4098 }
4099
Tao Ma936b8832008-10-09 23:06:14 +08004100 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08004101out:
4102 return ret;
4103}
Tao Ma01225592008-08-18 17:38:53 +08004104
4105static int cmp_xe(const void *a, const void *b)
4106{
4107 const struct ocfs2_xattr_entry *l = a, *r = b;
4108 u32 l_hash = le32_to_cpu(l->xe_name_hash);
4109 u32 r_hash = le32_to_cpu(r->xe_name_hash);
4110
4111 if (l_hash > r_hash)
4112 return 1;
4113 if (l_hash < r_hash)
4114 return -1;
4115 return 0;
4116}
4117
4118static void swap_xe(void *a, void *b, int size)
4119{
4120 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
4121
4122 tmp = *l;
4123 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
4124 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
4125}
4126
4127/*
4128 * When the ocfs2_xattr_block is filled up, new bucket will be created
4129 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07004130 * The header goes at the start of the bucket, and the names+values are
4131 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08004132 * Note: we need to sort the entries since they are not saved in order
4133 * in the ocfs2_xattr_block.
4134 */
4135static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
4136 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07004137 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08004138{
4139 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07004140 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004141 u16 offset, size, off_change;
4142 struct ocfs2_xattr_entry *xe;
4143 struct ocfs2_xattr_block *xb =
4144 (struct ocfs2_xattr_block *)xb_bh->b_data;
4145 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004146 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004147 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07004148 char *src = xb_bh->b_data;
4149 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08004150
4151 mlog(0, "cp xattr from block %llu to bucket %llu\n",
4152 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07004153 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08004154
Joel Becker178eeac2008-10-27 15:18:29 -07004155 for (i = 0; i < blks; i++)
4156 memset(bucket_block(bucket, i), 0, blocksize);
4157
Tao Ma01225592008-08-18 17:38:53 +08004158 /*
4159 * Since the xe_name_offset is based on ocfs2_xattr_header,
4160 * there is a offset change corresponding to the change of
4161 * ocfs2_xattr_header's position.
4162 */
4163 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4164 xe = &xb_xh->xh_entries[count - 1];
4165 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
4166 size = blocksize - offset;
4167
4168 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08004169 memcpy(target + offset, src + offset, size);
4170
4171 /* Init new header now. */
4172 xh->xh_count = xb_xh->xh_count;
4173 xh->xh_num_buckets = cpu_to_le16(1);
4174 xh->xh_name_value_len = cpu_to_le16(size);
4175 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
4176
4177 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07004178 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004179 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
4180 size = count * sizeof(struct ocfs2_xattr_entry);
4181 memcpy(target + offset, (char *)xb_xh + offset, size);
4182
4183 /* Change the xe offset for all the xe because of the move. */
4184 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
4185 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
4186 for (i = 0; i < count; i++)
4187 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
4188
4189 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
4190 offset, size, off_change);
4191
4192 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
4193 cmp_xe, swap_xe);
4194}
4195
4196/*
4197 * After we move xattr from block to index btree, we have to
4198 * update ocfs2_xattr_search to the new xe and base.
4199 *
4200 * When the entry is in xattr block, xattr_bh indicates the storage place.
4201 * While if the entry is in index b-tree, "bucket" indicates the
4202 * real place of the xattr.
4203 */
Joel Becker178eeac2008-10-27 15:18:29 -07004204static void ocfs2_xattr_update_xattr_search(struct inode *inode,
4205 struct ocfs2_xattr_search *xs,
4206 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08004207{
Tao Ma01225592008-08-18 17:38:53 +08004208 char *buf = old_bh->b_data;
4209 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
4210 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07004211 int i;
Tao Ma01225592008-08-18 17:38:53 +08004212
Joel Beckerba937122008-10-24 19:13:20 -07004213 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07004214 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08004215 xs->end = xs->base + inode->i_sb->s_blocksize;
4216
Joel Becker178eeac2008-10-27 15:18:29 -07004217 if (xs->not_found)
4218 return;
Tao Ma01225592008-08-18 17:38:53 +08004219
Joel Becker178eeac2008-10-27 15:18:29 -07004220 i = xs->here - old_xh->xh_entries;
4221 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004222}
4223
4224static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004225 struct ocfs2_xattr_search *xs,
4226 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004227{
Tao Ma85db90e2008-11-12 08:27:01 +08004228 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004229 u32 bit_off, len;
4230 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08004231 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004232 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08004233 struct buffer_head *xb_bh = xs->xattr_bh;
4234 struct ocfs2_xattr_block *xb =
4235 (struct ocfs2_xattr_block *)xb_bh->b_data;
4236 struct ocfs2_xattr_tree_root *xr;
4237 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08004238
4239 mlog(0, "create xattr index block for %llu\n",
4240 (unsigned long long)xb_bh->b_blocknr);
4241
4242 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07004243 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004244
Tao Ma01225592008-08-18 17:38:53 +08004245 /*
4246 * XXX:
4247 * We can use this lock for now, and maybe move to a dedicated mutex
4248 * if performance becomes a problem later.
4249 */
4250 down_write(&oi->ip_alloc_sem);
4251
Joel Becker0cf2f762009-02-12 16:41:25 -08004252 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08004253 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004254 if (ret) {
4255 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004256 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004257 }
4258
Joel Becker1ed9b772010-05-06 13:59:06 +08004259 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac,
Tao Ma78f30c32008-11-12 08:27:00 +08004260 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08004261 if (ret) {
4262 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004263 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004264 }
4265
4266 /*
4267 * The bucket may spread in many blocks, and
4268 * we will only touch the 1st block and the last block
4269 * in the whole bucket(one for entry and one for data).
4270 */
4271 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
4272
Mark Fashehde29c082008-10-29 14:45:30 -07004273 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
4274 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08004275
Joel Becker178eeac2008-10-27 15:18:29 -07004276 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004277 if (ret) {
4278 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004279 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004280 }
4281
Joel Becker178eeac2008-10-27 15:18:29 -07004282 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4283 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07004284 if (ret) {
4285 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004286 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07004287 }
Tao Ma01225592008-08-18 17:38:53 +08004288
Joel Becker178eeac2008-10-27 15:18:29 -07004289 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
4290 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4291
4292 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
4293
Tao Ma01225592008-08-18 17:38:53 +08004294 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
4295 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
4296 offsetof(struct ocfs2_xattr_block, xb_attrs));
4297
4298 xr = &xb->xb_attrs.xb_root;
4299 xr->xt_clusters = cpu_to_le32(1);
4300 xr->xt_last_eb_blk = 0;
4301 xr->xt_list.l_tree_depth = 0;
4302 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
4303 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
4304
4305 xr->xt_list.l_recs[0].e_cpos = 0;
4306 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
4307 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
4308
4309 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
4310
Tao Ma85db90e2008-11-12 08:27:01 +08004311 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004312
Tao Ma85db90e2008-11-12 08:27:01 +08004313out:
Tao Ma01225592008-08-18 17:38:53 +08004314 up_write(&oi->ip_alloc_sem);
4315
Tao Ma01225592008-08-18 17:38:53 +08004316 return ret;
4317}
4318
4319static int cmp_xe_offset(const void *a, const void *b)
4320{
4321 const struct ocfs2_xattr_entry *l = a, *r = b;
4322 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
4323 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
4324
4325 if (l_name_offset < r_name_offset)
4326 return 1;
4327 if (l_name_offset > r_name_offset)
4328 return -1;
4329 return 0;
4330}
4331
4332/*
4333 * defrag a xattr bucket if we find that the bucket has some
4334 * holes beteen name/value pairs.
4335 * We will move all the name/value pairs to the end of the bucket
4336 * so that we can spare some space for insertion.
4337 */
4338static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004339 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004340 struct ocfs2_xattr_bucket *bucket)
4341{
4342 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004343 size_t end, offset, len;
Tao Ma01225592008-08-18 17:38:53 +08004344 struct ocfs2_xattr_header *xh;
4345 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07004346 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004347 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08004348 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08004349 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08004350
4351 /*
4352 * In order to make the operation more efficient and generic,
4353 * we copy all the blocks into a contiguous memory and do the
4354 * defragment there, so if anything is error, we will not touch
4355 * the real block.
4356 */
4357 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
4358 if (!bucket_buf) {
4359 ret = -EIO;
4360 goto out;
4361 }
4362
Joel Becker161d6f32008-10-27 15:25:18 -07004363 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004364 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4365 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07004366
Tao Ma1c32a2f2008-11-06 08:10:47 +08004367 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07004368 OCFS2_JOURNAL_ACCESS_WRITE);
4369 if (ret < 0) {
4370 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004371 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004372 }
4373
4374 xh = (struct ocfs2_xattr_header *)bucket_buf;
4375 entries = (char *)xh->xh_entries;
4376 xh_free_start = le16_to_cpu(xh->xh_free_start);
4377
4378 mlog(0, "adjust xattr bucket in %llu, count = %u, "
4379 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004380 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
4381 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08004382
4383 /*
4384 * sort all the entries by their offset.
4385 * the largest will be the first, so that we can
4386 * move them to the end one by one.
4387 */
4388 sort(entries, le16_to_cpu(xh->xh_count),
4389 sizeof(struct ocfs2_xattr_entry),
4390 cmp_xe_offset, swap_xe);
4391
4392 /* Move all name/values to the end of the bucket. */
4393 xe = xh->xh_entries;
4394 end = OCFS2_XATTR_BUCKET_SIZE;
4395 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
4396 offset = le16_to_cpu(xe->xe_name_offset);
Joel Becker199799a2009-08-14 19:04:15 -07004397 len = namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004398
4399 /*
4400 * We must make sure that the name/value pair
4401 * exist in the same block. So adjust end to
4402 * the previous block end if needed.
4403 */
4404 if (((end - len) / blocksize !=
4405 (end - 1) / blocksize))
4406 end = end - end % blocksize;
4407
4408 if (end > offset + len) {
4409 memmove(bucket_buf + end - len,
4410 bucket_buf + offset, len);
4411 xe->xe_name_offset = cpu_to_le16(end - len);
4412 }
4413
4414 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
4415 "bucket %llu\n", (unsigned long long)blkno);
4416
4417 end -= len;
4418 }
4419
4420 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
4421 "bucket %llu\n", (unsigned long long)blkno);
4422
4423 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08004424 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004425
4426 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
4427 xh->xh_free_start = cpu_to_le16(end);
4428
4429 /* sort the entries by their name_hash. */
4430 sort(entries, le16_to_cpu(xh->xh_count),
4431 sizeof(struct ocfs2_xattr_entry),
4432 cmp_xe, swap_xe);
4433
4434 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08004435 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
4436 memcpy(bucket_block(bucket, i), buf, blocksize);
4437 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004438
Tao Ma01225592008-08-18 17:38:53 +08004439out:
Tao Ma01225592008-08-18 17:38:53 +08004440 kfree(bucket_buf);
4441 return ret;
4442}
4443
4444/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08004445 * prev_blkno points to the start of an existing extent. new_blkno
4446 * points to a newly allocated extent. Because we know each of our
4447 * clusters contains more than bucket, we can easily split one cluster
4448 * at a bucket boundary. So we take the last cluster of the existing
4449 * extent and split it down the middle. We move the last half of the
4450 * buckets in the last cluster of the existing extent over to the new
4451 * extent.
Tao Ma01225592008-08-18 17:38:53 +08004452 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08004453 * first_bh is the buffer at prev_blkno so we can update the existing
4454 * extent's bucket count. header_bh is the bucket were we were hoping
4455 * to insert our xattr. If the bucket move places the target in the new
4456 * extent, we'll update first_bh and header_bh after modifying the old
4457 * extent.
4458 *
4459 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004460 */
4461static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
4462 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004463 struct ocfs2_xattr_bucket *first,
4464 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004465 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08004466 u32 num_clusters,
4467 u32 *first_hash)
4468{
Joel Beckerc58b6032008-11-26 13:36:24 -08004469 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08004470 struct super_block *sb = inode->i_sb;
4471 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
4472 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08004473 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08004474 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08004475 u64 last_cluster_blkno = bucket_blkno(first) +
4476 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004477
Joel Becker41cb8142008-11-26 14:25:21 -08004478 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
4479 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08004480
Tao Ma01225592008-08-18 17:38:53 +08004481 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08004482 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004483
Joel Becker41cb8142008-11-26 14:25:21 -08004484 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08004485 last_cluster_blkno, new_blkno,
4486 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004487 if (ret) {
4488 mlog_errno(ret);
4489 goto out;
4490 }
4491
Joel Beckerc58b6032008-11-26 13:36:24 -08004492 /* This is the first bucket that got moved */
4493 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
4494
Tao Ma01225592008-08-18 17:38:53 +08004495 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004496 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08004497 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08004498 */
Joel Becker41cb8142008-11-26 14:25:21 -08004499 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08004500 /* Find the block for the new target bucket */
4501 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08004502 (bucket_blkno(target) - src_blkno);
4503
4504 ocfs2_xattr_bucket_relse(first);
4505 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004506
4507 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08004508 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08004509 * journal from ocfs2_cp_xattr_bucket().
4510 */
Joel Becker41cb8142008-11-26 14:25:21 -08004511 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08004512 if (ret) {
4513 mlog_errno(ret);
4514 goto out;
4515 }
Joel Becker41cb8142008-11-26 14:25:21 -08004516 ret = ocfs2_read_xattr_bucket(target, src_blkno);
4517 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08004518 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08004519
Joel Beckerb5c03e72008-11-25 19:58:16 -08004520 }
4521
Tao Ma01225592008-08-18 17:38:53 +08004522out:
Tao Ma01225592008-08-18 17:38:53 +08004523 return ret;
4524}
4525
Tao Ma01225592008-08-18 17:38:53 +08004526/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004527 * Find the suitable pos when we divide a bucket into 2.
4528 * We have to make sure the xattrs with the same hash value exist
4529 * in the same bucket.
4530 *
4531 * If this ocfs2_xattr_header covers more than one hash value, find a
4532 * place where the hash value changes. Try to find the most even split.
4533 * The most common case is that all entries have different hash values,
4534 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08004535 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004536static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
4537{
4538 struct ocfs2_xattr_entry *entries = xh->xh_entries;
4539 int count = le16_to_cpu(xh->xh_count);
4540 int delta, middle = count / 2;
4541
4542 /*
4543 * We start at the middle. Each step gets farther away in both
4544 * directions. We therefore hit the change in hash value
4545 * nearest to the middle. Note that this loop does not execute for
4546 * count < 2.
4547 */
4548 for (delta = 0; delta < middle; delta++) {
4549 /* Let's check delta earlier than middle */
4550 if (cmp_xe(&entries[middle - delta - 1],
4551 &entries[middle - delta]))
4552 return middle - delta;
4553
4554 /* For even counts, don't walk off the end */
4555 if ((middle + delta + 1) == count)
4556 continue;
4557
4558 /* Now try delta past middle */
4559 if (cmp_xe(&entries[middle + delta],
4560 &entries[middle + delta + 1]))
4561 return middle + delta + 1;
4562 }
4563
4564 /* Every entry had the same hash */
4565 return count;
4566}
4567
4568/*
4569 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
4570 * first_hash will record the 1st hash of the new bucket.
4571 *
4572 * Normally half of the xattrs will be moved. But we have to make
4573 * sure that the xattrs with the same hash value are stored in the
4574 * same bucket. If all the xattrs in this bucket have the same hash
4575 * value, the new bucket will be initialized as an empty one and the
4576 * first_hash will be initialized as (hash_value+1).
4577 */
4578static int ocfs2_divide_xattr_bucket(struct inode *inode,
4579 handle_t *handle,
4580 u64 blk,
4581 u64 new_blk,
4582 u32 *first_hash,
4583 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08004584{
4585 int ret, i;
Joel Becker199799a2009-08-14 19:04:15 -07004586 int count, start, len, name_value_len = 0, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07004587 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004588 struct ocfs2_xattr_header *xh;
4589 struct ocfs2_xattr_entry *xe;
4590 int blocksize = inode->i_sb->s_blocksize;
4591
Tao Ma80bcaf32008-10-27 06:06:24 +08004592 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004593 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004594
Joel Beckerba937122008-10-24 19:13:20 -07004595 s_bucket = ocfs2_xattr_bucket_new(inode);
4596 t_bucket = ocfs2_xattr_bucket_new(inode);
4597 if (!s_bucket || !t_bucket) {
4598 ret = -ENOMEM;
4599 mlog_errno(ret);
4600 goto out;
4601 }
Tao Ma01225592008-08-18 17:38:53 +08004602
Joel Beckerba937122008-10-24 19:13:20 -07004603 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08004604 if (ret) {
4605 mlog_errno(ret);
4606 goto out;
4607 }
4608
Joel Beckerba937122008-10-24 19:13:20 -07004609 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004610 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004611 if (ret) {
4612 mlog_errno(ret);
4613 goto out;
4614 }
4615
Joel Becker784b8162008-10-24 17:33:40 -07004616 /*
4617 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
4618 * there's no need to read it.
4619 */
Joel Beckerba937122008-10-24 19:13:20 -07004620 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004621 if (ret) {
4622 mlog_errno(ret);
4623 goto out;
4624 }
4625
Joel Becker2b656c12008-11-25 19:00:15 -08004626 /*
4627 * Hey, if we're overwriting t_bucket, what difference does
4628 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
4629 * same part of ocfs2_cp_xattr_bucket().
4630 */
Joel Beckerba937122008-10-24 19:13:20 -07004631 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004632 new_bucket_head ?
4633 OCFS2_JOURNAL_ACCESS_CREATE :
4634 OCFS2_JOURNAL_ACCESS_WRITE);
4635 if (ret) {
4636 mlog_errno(ret);
4637 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004638 }
4639
Joel Beckerba937122008-10-24 19:13:20 -07004640 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004641 count = le16_to_cpu(xh->xh_count);
4642 start = ocfs2_xattr_find_divide_pos(xh);
4643
4644 if (start == count) {
4645 xe = &xh->xh_entries[start-1];
4646
4647 /*
4648 * initialized a new empty bucket here.
4649 * The hash value is set as one larger than
4650 * that of the last entry in the previous bucket.
4651 */
Joel Beckerba937122008-10-24 19:13:20 -07004652 for (i = 0; i < t_bucket->bu_blocks; i++)
4653 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08004654
Joel Beckerba937122008-10-24 19:13:20 -07004655 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004656 xh->xh_free_start = cpu_to_le16(blocksize);
4657 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
4658 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
4659
4660 goto set_num_buckets;
4661 }
4662
Tao Ma01225592008-08-18 17:38:53 +08004663 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07004664 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004665
4666 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07004667 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004668
4669 /*
4670 * Calculate the total name/value len and xh_free_start for
4671 * the old bucket first.
4672 */
4673 name_offset = OCFS2_XATTR_BUCKET_SIZE;
4674 name_value_len = 0;
4675 for (i = 0; i < start; i++) {
4676 xe = &xh->xh_entries[i];
Joel Becker199799a2009-08-14 19:04:15 -07004677 name_value_len += namevalue_size_xe(xe);
Tao Ma01225592008-08-18 17:38:53 +08004678 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4679 name_offset = le16_to_cpu(xe->xe_name_offset);
4680 }
4681
4682 /*
4683 * Now begin the modification to the new bucket.
4684 *
4685 * In the new bucket, We just move the xattr entry to the beginning
4686 * and don't touch the name/value. So there will be some holes in the
4687 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
4688 * called.
4689 */
4690 xe = &xh->xh_entries[start];
4691 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
4692 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07004693 (int)((char *)xe - (char *)xh),
4694 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08004695 memmove((char *)xh->xh_entries, (char *)xe, len);
4696 xe = &xh->xh_entries[count - start];
4697 len = sizeof(struct ocfs2_xattr_entry) * start;
4698 memset((char *)xe, 0, len);
4699
4700 le16_add_cpu(&xh->xh_count, -start);
4701 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
4702
4703 /* Calculate xh_free_start for the new bucket. */
4704 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4705 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4706 xe = &xh->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08004707 if (le16_to_cpu(xe->xe_name_offset) <
4708 le16_to_cpu(xh->xh_free_start))
4709 xh->xh_free_start = xe->xe_name_offset;
4710 }
4711
Tao Ma80bcaf32008-10-27 06:06:24 +08004712set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08004713 /* set xh->xh_num_buckets for the new xh. */
4714 if (new_bucket_head)
4715 xh->xh_num_buckets = cpu_to_le16(1);
4716 else
4717 xh->xh_num_buckets = 0;
4718
Joel Beckerba937122008-10-24 19:13:20 -07004719 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004720
4721 /* store the first_hash of the new bucket. */
4722 if (first_hash)
4723 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4724
4725 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08004726 * Now only update the 1st block of the old bucket. If we
4727 * just added a new empty bucket, there is no need to modify
4728 * it.
Tao Ma01225592008-08-18 17:38:53 +08004729 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004730 if (start == count)
4731 goto out;
4732
Joel Beckerba937122008-10-24 19:13:20 -07004733 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004734 memset(&xh->xh_entries[start], 0,
4735 sizeof(struct ocfs2_xattr_entry) * (count - start));
4736 xh->xh_count = cpu_to_le16(start);
4737 xh->xh_free_start = cpu_to_le16(name_offset);
4738 xh->xh_name_value_len = cpu_to_le16(name_value_len);
4739
Joel Beckerba937122008-10-24 19:13:20 -07004740 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004741
4742out:
Joel Beckerba937122008-10-24 19:13:20 -07004743 ocfs2_xattr_bucket_free(s_bucket);
4744 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004745
4746 return ret;
4747}
4748
4749/*
4750 * Copy xattr from one bucket to another bucket.
4751 *
4752 * The caller must make sure that the journal transaction
4753 * has enough space for journaling.
4754 */
4755static int ocfs2_cp_xattr_bucket(struct inode *inode,
4756 handle_t *handle,
4757 u64 s_blkno,
4758 u64 t_blkno,
4759 int t_is_new)
4760{
Joel Becker4980c6d2008-10-24 18:54:43 -07004761 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004762 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004763
4764 BUG_ON(s_blkno == t_blkno);
4765
4766 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004767 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4768 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004769
Joel Beckerba937122008-10-24 19:13:20 -07004770 s_bucket = ocfs2_xattr_bucket_new(inode);
4771 t_bucket = ocfs2_xattr_bucket_new(inode);
4772 if (!s_bucket || !t_bucket) {
4773 ret = -ENOMEM;
4774 mlog_errno(ret);
4775 goto out;
4776 }
Joel Becker92de1092008-11-25 17:06:40 -08004777
Joel Beckerba937122008-10-24 19:13:20 -07004778 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004779 if (ret)
4780 goto out;
4781
Joel Becker784b8162008-10-24 17:33:40 -07004782 /*
4783 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4784 * there's no need to read it.
4785 */
Joel Beckerba937122008-10-24 19:13:20 -07004786 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004787 if (ret)
4788 goto out;
4789
Joel Becker2b656c12008-11-25 19:00:15 -08004790 /*
4791 * Hey, if we're overwriting t_bucket, what difference does
4792 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004793 * cluster to fill, we came here from
4794 * ocfs2_mv_xattr_buckets(), and it is really new -
4795 * ACCESS_CREATE is required. But we also might have moved data
4796 * out of t_bucket before extending back into it.
4797 * ocfs2_add_new_xattr_bucket() can do this - its call to
4798 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004799 * and copied out the end of the old extent. Then it re-extends
4800 * the old extent back to create space for new xattrs. That's
4801 * how we get here, and the bucket isn't really new.
4802 */
Joel Beckerba937122008-10-24 19:13:20 -07004803 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004804 t_is_new ?
4805 OCFS2_JOURNAL_ACCESS_CREATE :
4806 OCFS2_JOURNAL_ACCESS_WRITE);
4807 if (ret)
4808 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004809
Joel Beckerba937122008-10-24 19:13:20 -07004810 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4811 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004812
4813out:
Joel Beckerba937122008-10-24 19:13:20 -07004814 ocfs2_xattr_bucket_free(t_bucket);
4815 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004816
4817 return ret;
4818}
4819
4820/*
Joel Becker874d65a2008-11-26 13:02:18 -08004821 * src_blk points to the start of an existing extent. last_blk points to
4822 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004823 * extent. We copy the buckets from the cluster at last_blk to the new
4824 * extent. If start_bucket is non-zero, we skip that many buckets before
4825 * we start copying. The new extent's xh_num_buckets gets set to the
4826 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4827 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004828 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004829static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4830 u64 src_blk, u64 last_blk, u64 to_blk,
4831 unsigned int start_bucket,
4832 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004833{
4834 int i, ret, credits;
4835 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004836 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004837 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004838 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004839
Joel Becker874d65a2008-11-26 13:02:18 -08004840 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4841 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004842
Joel Becker54ecb6b2008-11-26 13:18:31 -08004843 BUG_ON(start_bucket >= num_buckets);
4844 if (start_bucket) {
4845 num_buckets -= start_bucket;
4846 last_blk += (start_bucket * blks_per_bucket);
4847 }
4848
Joel Becker15d60922008-11-25 18:36:42 -08004849 /* The first bucket of the original extent */
4850 old_first = ocfs2_xattr_bucket_new(inode);
4851 /* The first bucket of the new extent */
4852 new_first = ocfs2_xattr_bucket_new(inode);
4853 if (!old_first || !new_first) {
4854 ret = -ENOMEM;
4855 mlog_errno(ret);
4856 goto out;
4857 }
4858
Joel Becker874d65a2008-11-26 13:02:18 -08004859 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004860 if (ret) {
4861 mlog_errno(ret);
4862 goto out;
4863 }
4864
Tao Ma01225592008-08-18 17:38:53 +08004865 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004866 * We need to update the first bucket of the old extent and all
4867 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004868 */
Tao Mac901fb02010-04-26 14:34:57 +08004869 credits = ((num_buckets + 1) * blks_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004870 ret = ocfs2_extend_trans(handle, credits);
4871 if (ret) {
4872 mlog_errno(ret);
4873 goto out;
4874 }
4875
Joel Becker15d60922008-11-25 18:36:42 -08004876 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4877 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004878 if (ret) {
4879 mlog_errno(ret);
4880 goto out;
4881 }
4882
4883 for (i = 0; i < num_buckets; i++) {
4884 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004885 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004886 to_blk + (i * blks_per_bucket),
4887 1);
Tao Ma01225592008-08-18 17:38:53 +08004888 if (ret) {
4889 mlog_errno(ret);
4890 goto out;
4891 }
Tao Ma01225592008-08-18 17:38:53 +08004892 }
4893
Joel Becker15d60922008-11-25 18:36:42 -08004894 /*
4895 * Get the new bucket ready before we dirty anything
4896 * (This actually shouldn't fail, because we already dirtied
4897 * it once in ocfs2_cp_xattr_bucket()).
4898 */
4899 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4900 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004901 mlog_errno(ret);
4902 goto out;
4903 }
Joel Becker15d60922008-11-25 18:36:42 -08004904 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4905 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004906 if (ret) {
4907 mlog_errno(ret);
4908 goto out;
4909 }
4910
Joel Becker15d60922008-11-25 18:36:42 -08004911 /* Now update the headers */
4912 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4913 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004914
Joel Becker15d60922008-11-25 18:36:42 -08004915 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4916 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004917
4918 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004919 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4920
Tao Ma01225592008-08-18 17:38:53 +08004921out:
Joel Becker15d60922008-11-25 18:36:42 -08004922 ocfs2_xattr_bucket_free(new_first);
4923 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004924 return ret;
4925}
4926
4927/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004928 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004929 * This function should only be called when bucket size == cluster size.
4930 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4931 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004932static int ocfs2_divide_xattr_cluster(struct inode *inode,
4933 handle_t *handle,
4934 u64 prev_blk,
4935 u64 new_blk,
4936 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004937{
4938 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mac901fb02010-04-26 14:34:57 +08004939 int ret, credits = 2 * blk_per_bucket;
Tao Ma01225592008-08-18 17:38:53 +08004940
4941 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4942
4943 ret = ocfs2_extend_trans(handle, credits);
4944 if (ret) {
4945 mlog_errno(ret);
4946 return ret;
4947 }
4948
4949 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004950 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4951 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004952}
4953
4954/*
4955 * Move some xattrs from the old cluster to the new one since they are not
4956 * contiguous in ocfs2 xattr tree.
4957 *
4958 * new_blk starts a new separate cluster, and we will move some xattrs from
4959 * prev_blk to it. v_start will be set as the first name hash value in this
4960 * new cluster so that it can be used as e_cpos during tree insertion and
4961 * don't collide with our original b-tree operations. first_bh and header_bh
4962 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4963 * to extend the insert bucket.
4964 *
4965 * The problem is how much xattr should we move to the new one and when should
4966 * we update first_bh and header_bh?
4967 * 1. If cluster size > bucket size, that means the previous cluster has more
4968 * than 1 bucket, so just move half nums of bucket into the new cluster and
4969 * update the first_bh and header_bh if the insert bucket has been moved
4970 * to the new cluster.
4971 * 2. If cluster_size == bucket_size:
4972 * a) If the previous extent rec has more than one cluster and the insert
4973 * place isn't in the last cluster, copy the entire last cluster to the
4974 * new one. This time, we don't need to upate the first_bh and header_bh
4975 * since they will not be moved into the new cluster.
4976 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4977 * the new one. And we set the extend flag to zero if the insert place is
4978 * moved into the new allocated cluster since no extend is needed.
4979 */
4980static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4981 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004982 struct ocfs2_xattr_bucket *first,
4983 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004984 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004985 u32 prev_clusters,
4986 u32 *v_start,
4987 int *extend)
4988{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004989 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004990
4991 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004992 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004993 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004994
Joel Becker41cb8142008-11-26 14:25:21 -08004995 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004996 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4997 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004998 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004999 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08005000 prev_clusters,
5001 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005002 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08005003 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08005004 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08005005 /* The start of the last cluster in the first extent */
5006 u64 last_blk = bucket_blkno(first) +
5007 ((prev_clusters - 1) *
5008 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08005009
Joel Becker012ee912008-11-26 14:43:31 -08005010 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08005011 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08005012 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08005013 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08005014 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005015 if (ret)
5016 mlog_errno(ret);
5017 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08005018 ret = ocfs2_divide_xattr_cluster(inode, handle,
5019 last_blk, new_blk,
5020 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08005021 if (ret)
5022 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08005023
Joel Becker92cf3ad2008-11-26 14:12:09 -08005024 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08005025 *extend = 0;
5026 }
5027 }
5028
5029 return ret;
5030}
5031
5032/*
5033 * Add a new cluster for xattr storage.
5034 *
5035 * If the new cluster is contiguous with the previous one, it will be
5036 * appended to the same extent record, and num_clusters will be updated.
5037 * If not, we will insert a new extent for it and move some xattrs in
5038 * the last cluster into the new allocated one.
5039 * We also need to limit the maximum size of a btree leaf, otherwise we'll
5040 * lose the benefits of hashing because we'll have to search large leaves.
5041 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
5042 * if it's bigger).
5043 *
5044 * first_bh is the first block of the previous extent rec and header_bh
5045 * indicates the bucket we will insert the new xattrs. They will be updated
5046 * when the header_bh is moved into the new cluster.
5047 */
5048static int ocfs2_add_new_xattr_cluster(struct inode *inode,
5049 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005050 struct ocfs2_xattr_bucket *first,
5051 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08005052 u32 *num_clusters,
5053 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005054 int *extend,
5055 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005056{
Tao Ma85db90e2008-11-12 08:27:01 +08005057 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005058 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
5059 u32 prev_clusters = *num_clusters;
5060 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
5061 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08005062 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08005063 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005064 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08005065
5066 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
5067 "previous xattr blkno = %llu\n",
5068 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08005069 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08005070
Joel Becker5e404e92009-02-13 03:54:22 -08005071 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07005072
Joel Becker0cf2f762009-02-12 16:41:25 -08005073 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005074 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005075 if (ret < 0) {
5076 mlog_errno(ret);
5077 goto leave;
5078 }
5079
Joel Becker1ed9b772010-05-06 13:59:06 +08005080 ret = __ocfs2_claim_clusters(handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08005081 clusters_to_add, &bit_off, &num_bits);
5082 if (ret < 0) {
5083 if (ret != -ENOSPC)
5084 mlog_errno(ret);
5085 goto leave;
5086 }
5087
5088 BUG_ON(num_bits > clusters_to_add);
5089
5090 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
5091 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
5092 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
5093
Joel Beckered29c0c2008-11-26 15:08:44 -08005094 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08005095 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
5096 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
5097 /*
5098 * If this cluster is contiguous with the old one and
5099 * adding this new cluster, we don't surpass the limit of
5100 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
5101 * initialized and used like other buckets in the previous
5102 * cluster.
5103 * So add it as a contiguous one. The caller will handle
5104 * its init process.
5105 */
5106 v_start = prev_cpos + prev_clusters;
5107 *num_clusters = prev_clusters + num_bits;
5108 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
5109 num_bits);
5110 } else {
5111 ret = ocfs2_adjust_xattr_cross_cluster(inode,
5112 handle,
Joel Becker012ee912008-11-26 14:43:31 -08005113 first,
5114 target,
Tao Ma01225592008-08-18 17:38:53 +08005115 block,
Tao Ma01225592008-08-18 17:38:53 +08005116 prev_clusters,
5117 &v_start,
5118 extend);
5119 if (ret) {
5120 mlog_errno(ret);
5121 goto leave;
5122 }
5123 }
5124
5125 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07005126 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08005127 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08005128 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005129 if (ret < 0) {
5130 mlog_errno(ret);
5131 goto leave;
5132 }
5133
Joel Beckerec20cec2010-03-19 14:13:52 -07005134 ocfs2_journal_dirty(handle, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005135
5136leave:
Tao Ma01225592008-08-18 17:38:53 +08005137 return ret;
5138}
5139
5140/*
Joel Becker92de1092008-11-25 17:06:40 -08005141 * We are given an extent. 'first' is the bucket at the very front of
5142 * the extent. The extent has space for an additional bucket past
5143 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
5144 * of the target bucket. We wish to shift every bucket past the target
5145 * down one, filling in that additional space. When we get back to the
5146 * target, we split the target between itself and the now-empty bucket
5147 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005148 */
5149static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005150 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08005151 struct ocfs2_xattr_bucket *first,
5152 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08005153 u32 num_clusters)
5154{
5155 int ret, credits;
5156 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5157 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08005158 u64 end_blk;
5159 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08005160
5161 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08005162 "from %llu, len = %u\n", (unsigned long long)target_blk,
5163 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08005164
Joel Becker92de1092008-11-25 17:06:40 -08005165 /* The extent must have room for an additional bucket */
5166 BUG_ON(new_bucket >=
5167 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08005168
Joel Becker92de1092008-11-25 17:06:40 -08005169 /* end_blk points to the last existing bucket */
5170 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08005171
5172 /*
Joel Becker92de1092008-11-25 17:06:40 -08005173 * end_blk is the start of the last existing bucket.
5174 * Thus, (end_blk - target_blk) covers the target bucket and
5175 * every bucket after it up to, but not including, the last
5176 * existing bucket. Then we add the last existing bucket, the
5177 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08005178 */
Tao Mac901fb02010-04-26 14:34:57 +08005179 credits = (end_blk - target_blk) + (3 * blk_per_bucket);
Tao Ma85db90e2008-11-12 08:27:01 +08005180 ret = ocfs2_extend_trans(handle, credits);
5181 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08005182 mlog_errno(ret);
5183 goto out;
5184 }
5185
Joel Becker92de1092008-11-25 17:06:40 -08005186 ret = ocfs2_xattr_bucket_journal_access(handle, first,
5187 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005188 if (ret) {
5189 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08005190 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005191 }
5192
Joel Becker92de1092008-11-25 17:06:40 -08005193 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08005194 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
5195 end_blk + blk_per_bucket, 0);
5196 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08005197 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005198 end_blk -= blk_per_bucket;
5199 }
5200
Joel Becker92de1092008-11-25 17:06:40 -08005201 /* Move half of the xattr in target_blkno to the next bucket. */
5202 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
5203 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08005204
Joel Becker92de1092008-11-25 17:06:40 -08005205 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
5206 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08005207
Tao Ma01225592008-08-18 17:38:53 +08005208out:
5209 return ret;
5210}
5211
5212/*
Joel Becker91f20332008-11-26 15:25:41 -08005213 * Add new xattr bucket in an extent record and adjust the buckets
5214 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
5215 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08005216 *
Joel Becker91f20332008-11-26 15:25:41 -08005217 * In the easy case, we will move all the buckets after target down by
5218 * one. Half of target's xattrs will be moved to the next bucket.
5219 *
5220 * If current cluster is full, we'll allocate a new one. This may not
5221 * be contiguous. The underlying calls will make sure that there is
5222 * space for the insert, shifting buckets around if necessary.
5223 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08005224 */
5225static int ocfs2_add_new_xattr_bucket(struct inode *inode,
5226 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005227 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08005228 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005229{
Tao Ma01225592008-08-18 17:38:53 +08005230 struct ocfs2_xattr_block *xb =
5231 (struct ocfs2_xattr_block *)xb_bh->b_data;
5232 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
5233 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08005234 u32 name_hash =
5235 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08005236 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08005237 int ret, num_buckets, extend = 1;
5238 u64 p_blkno;
5239 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08005240 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08005241 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08005242
Joel Becker91f20332008-11-26 15:25:41 -08005243 mlog(0, "Add new xattr bucket starting from %llu\n",
5244 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08005245
Joel Beckered29c0c2008-11-26 15:08:44 -08005246 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08005247 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08005248 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08005249 ret = -ENOMEM;
5250 mlog_errno(ret);
5251 goto out;
5252 }
5253
Tao Ma01225592008-08-18 17:38:53 +08005254 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
5255 &num_clusters, el);
5256 if (ret) {
5257 mlog_errno(ret);
5258 goto out;
5259 }
5260
Joel Beckered29c0c2008-11-26 15:08:44 -08005261 ret = ocfs2_read_xattr_bucket(first, p_blkno);
5262 if (ret) {
5263 mlog_errno(ret);
5264 goto out;
5265 }
5266
Tao Ma01225592008-08-18 17:38:53 +08005267 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08005268 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
5269 /*
5270 * This can move first+target if the target bucket moves
5271 * to the new extent.
5272 */
Tao Ma01225592008-08-18 17:38:53 +08005273 ret = ocfs2_add_new_xattr_cluster(inode,
5274 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08005275 first,
5276 target,
Tao Ma01225592008-08-18 17:38:53 +08005277 &num_clusters,
5278 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08005279 &extend,
5280 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005281 if (ret) {
5282 mlog_errno(ret);
5283 goto out;
5284 }
5285 }
5286
Joel Becker92de1092008-11-25 17:06:40 -08005287 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08005288 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08005289 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08005290 first,
5291 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08005292 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08005293 if (ret)
5294 mlog_errno(ret);
5295 }
5296
Tao Ma01225592008-08-18 17:38:53 +08005297out:
Joel Becker92de1092008-11-25 17:06:40 -08005298 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08005299
Tao Ma01225592008-08-18 17:38:53 +08005300 return ret;
5301}
5302
5303static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
5304 struct ocfs2_xattr_bucket *bucket,
5305 int offs)
5306{
5307 int block_off = offs >> inode->i_sb->s_blocksize_bits;
5308
5309 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07005310 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08005311}
5312
5313/*
Tao Ma01225592008-08-18 17:38:53 +08005314 * Truncate the specified xe_off entry in xattr bucket.
5315 * bucket is indicated by header_bh and len is the new length.
5316 * Both the ocfs2_xattr_value_root and the entry will be updated here.
5317 *
5318 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
5319 */
5320static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08005321 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08005322 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08005323 int len,
5324 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005325{
5326 int ret, offset;
5327 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08005328 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08005329 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08005330 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08005331 struct ocfs2_xattr_value_buf vb = {
5332 .vb_access = ocfs2_journal_access,
5333 };
Tao Ma01225592008-08-18 17:38:53 +08005334
5335 xe = &xh->xh_entries[xe_off];
5336
5337 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
5338
5339 offset = le16_to_cpu(xe->xe_name_offset) +
5340 OCFS2_XATTR_SIZE(xe->xe_name_len);
5341
5342 value_blk = offset / blocksize;
5343
5344 /* We don't allow ocfs2_xattr_value to be stored in different block. */
5345 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005346
Joel Beckerb3e5d372008-12-09 15:01:04 -08005347 vb.vb_bh = bucket->bu_bhs[value_blk];
5348 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08005349
Joel Beckerb3e5d372008-12-09 15:01:04 -08005350 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5351 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08005352
Joel Becker548b0f22008-11-24 19:32:13 -08005353 /*
5354 * From here on out we have to dirty the bucket. The generic
5355 * value calls only modify one of the bucket's bhs, but we need
5356 * to send the bucket at once. So if they error, they *could* have
5357 * modified something. We have to assume they did, and dirty
5358 * the whole bucket. This leaves us in a consistent state.
5359 */
Tao Ma01225592008-08-18 17:38:53 +08005360 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08005361 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08005362 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005363 if (ret) {
5364 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08005365 goto out;
5366 }
5367
5368 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
5369 OCFS2_JOURNAL_ACCESS_WRITE);
5370 if (ret) {
5371 mlog_errno(ret);
5372 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005373 }
5374
Joel Becker548b0f22008-11-24 19:32:13 -08005375 xe->xe_value_size = cpu_to_le64(len);
5376
Joel Becker548b0f22008-11-24 19:32:13 -08005377 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08005378
5379out:
Tao Ma01225592008-08-18 17:38:53 +08005380 return ret;
5381}
5382
Tao Ma01225592008-08-18 17:38:53 +08005383static int ocfs2_rm_xattr_cluster(struct inode *inode,
5384 struct buffer_head *root_bh,
5385 u64 blkno,
5386 u32 cpos,
Tao Ma47bca492009-08-18 11:43:42 +08005387 u32 len,
5388 void *para)
Tao Ma01225592008-08-18 17:38:53 +08005389{
5390 int ret;
5391 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5392 struct inode *tl_inode = osb->osb_tl_inode;
5393 handle_t *handle;
5394 struct ocfs2_xattr_block *xb =
5395 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08005396 struct ocfs2_alloc_context *meta_ac = NULL;
5397 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07005398 struct ocfs2_extent_tree et;
5399
Tao Ma47bca492009-08-18 11:43:42 +08005400 ret = ocfs2_iterate_xattr_buckets(inode, blkno, len,
Tao Mace9c5a52009-08-18 11:43:59 +08005401 ocfs2_delete_xattr_in_bucket, para);
Tao Ma47bca492009-08-18 11:43:42 +08005402 if (ret) {
5403 mlog_errno(ret);
5404 return ret;
5405 }
5406
Joel Becker5e404e92009-02-13 03:54:22 -08005407 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005408
5409 ocfs2_init_dealloc_ctxt(&dealloc);
5410
5411 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
5412 cpos, len, (unsigned long long)blkno);
5413
Joel Becker8cb471e2009-02-10 20:00:41 -08005414 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
5415 len);
Tao Ma01225592008-08-18 17:38:53 +08005416
Joel Beckerf99b9b72008-08-20 19:36:33 -07005417 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08005418 if (ret) {
5419 mlog_errno(ret);
5420 return ret;
5421 }
5422
5423 mutex_lock(&tl_inode->i_mutex);
5424
5425 if (ocfs2_truncate_log_needs_flush(osb)) {
5426 ret = __ocfs2_flush_truncate_log(osb);
5427 if (ret < 0) {
5428 mlog_errno(ret);
5429 goto out;
5430 }
5431 }
5432
Jan Karaa90714c2008-10-09 19:38:40 +02005433 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08005434 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08005435 ret = -ENOMEM;
5436 mlog_errno(ret);
5437 goto out;
5438 }
5439
Joel Becker0cf2f762009-02-12 16:41:25 -08005440 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08005441 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08005442 if (ret) {
5443 mlog_errno(ret);
5444 goto out_commit;
5445 }
5446
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005447 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005448 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08005449 if (ret) {
5450 mlog_errno(ret);
5451 goto out_commit;
5452 }
5453
5454 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
Joel Beckerec20cec2010-03-19 14:13:52 -07005455 ocfs2_journal_dirty(handle, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08005456
5457 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
5458 if (ret)
5459 mlog_errno(ret);
5460
5461out_commit:
5462 ocfs2_commit_trans(osb, handle);
5463out:
5464 ocfs2_schedule_truncate_log_flush(osb, 1);
5465
5466 mutex_unlock(&tl_inode->i_mutex);
5467
5468 if (meta_ac)
5469 ocfs2_free_alloc_context(meta_ac);
5470
5471 ocfs2_run_deallocs(osb, &dealloc);
5472
5473 return ret;
5474}
5475
Tao Ma01225592008-08-18 17:38:53 +08005476/*
Tao Ma80bcaf32008-10-27 06:06:24 +08005477 * check whether the xattr bucket is filled up with the same hash value.
5478 * If we want to insert the xattr with the same hash, return -ENOSPC.
5479 * If we want to insert a xattr with different hash value, go ahead
5480 * and ocfs2_divide_xattr_bucket will handle this.
5481 */
Tao Ma01225592008-08-18 17:38:53 +08005482static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005483 struct ocfs2_xattr_bucket *bucket,
5484 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005485{
Joel Becker3e632942008-10-24 17:04:49 -07005486 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005487 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5488
5489 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5490 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005491
5492 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5493 xh->xh_entries[0].xe_name_hash) {
5494 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5495 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005496 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005497 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5498 return -ENOSPC;
5499 }
5500
5501 return 0;
5502}
5503
Joel Beckerc5d95df2009-08-18 21:03:24 -07005504/*
5505 * Try to set the entry in the current bucket. If we fail, the caller
5506 * will handle getting us another bucket.
5507 */
5508static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5509 struct ocfs2_xattr_info *xi,
5510 struct ocfs2_xattr_search *xs,
5511 struct ocfs2_xattr_set_ctxt *ctxt)
5512{
5513 int ret;
5514 struct ocfs2_xa_loc loc;
5515
5516 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5517
5518 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5519 xs->not_found ? NULL : xs->here);
5520 ret = ocfs2_xa_set(&loc, xi, ctxt);
5521 if (!ret) {
5522 xs->here = loc.xl_entry;
5523 goto out;
5524 }
5525 if (ret != -ENOSPC) {
5526 mlog_errno(ret);
5527 goto out;
5528 }
5529
5530 /* Ok, we need space. Let's try defragmenting the bucket. */
5531 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5532 xs->bucket);
5533 if (ret) {
5534 mlog_errno(ret);
5535 goto out;
5536 }
5537
5538 ret = ocfs2_xa_set(&loc, xi, ctxt);
5539 if (!ret) {
5540 xs->here = loc.xl_entry;
5541 goto out;
5542 }
5543 if (ret != -ENOSPC)
5544 mlog_errno(ret);
5545
5546
5547out:
5548 mlog_exit(ret);
5549 return ret;
5550}
5551
Tao Ma01225592008-08-18 17:38:53 +08005552static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5553 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005554 struct ocfs2_xattr_search *xs,
5555 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005556{
Joel Beckerc5d95df2009-08-18 21:03:24 -07005557 int ret;
Tao Ma01225592008-08-18 17:38:53 +08005558
Joel Becker6b240ff2009-08-14 18:02:52 -07005559 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
Tao Ma01225592008-08-18 17:38:53 +08005560
Joel Beckerc5d95df2009-08-18 21:03:24 -07005561 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5562 if (!ret)
5563 goto out;
5564 if (ret != -ENOSPC) {
5565 mlog_errno(ret);
5566 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005567 }
5568
Joel Beckerc5d95df2009-08-18 21:03:24 -07005569 /* Ack, need more space. Let's try to get another bucket! */
5570
Tao Ma01225592008-08-18 17:38:53 +08005571 /*
Joel Beckerc5d95df2009-08-18 21:03:24 -07005572 * We do not allow for overlapping ranges between buckets. And
5573 * the maximum number of collisions we will allow for then is
5574 * one bucket's worth, so check it here whether we need to
5575 * add a new bucket for the insert.
Tao Ma01225592008-08-18 17:38:53 +08005576 */
Joel Beckerc5d95df2009-08-18 21:03:24 -07005577 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Becker91f20332008-11-26 15:25:41 -08005578 xs->bucket,
Joel Beckerc5d95df2009-08-18 21:03:24 -07005579 xi->xi_name);
5580 if (ret) {
5581 mlog_errno(ret);
5582 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005583 }
5584
Joel Beckerc5d95df2009-08-18 21:03:24 -07005585 ret = ocfs2_add_new_xattr_bucket(inode,
5586 xs->xattr_bh,
5587 xs->bucket,
5588 ctxt);
5589 if (ret) {
5590 mlog_errno(ret);
5591 goto out;
5592 }
5593
5594 /*
5595 * ocfs2_add_new_xattr_bucket() will have updated
5596 * xs->bucket if it moved, but it will not have updated
5597 * any of the other search fields. Thus, we drop it and
5598 * re-search. Everything should be cached, so it'll be
5599 * quick.
5600 */
5601 ocfs2_xattr_bucket_relse(xs->bucket);
5602 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5603 xi->xi_name_index,
5604 xi->xi_name, xs);
5605 if (ret && ret != -ENODATA)
5606 goto out;
5607 xs->not_found = ret;
5608
5609 /* Ok, we have a new bucket, let's try again */
5610 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5611 if (ret && (ret != -ENOSPC))
5612 mlog_errno(ret);
5613
Tao Ma01225592008-08-18 17:38:53 +08005614out:
5615 mlog_exit(ret);
5616 return ret;
5617}
Tao Maa3944252008-08-18 17:38:54 +08005618
5619static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5620 struct ocfs2_xattr_bucket *bucket,
5621 void *para)
5622{
Tao Mace9c5a52009-08-18 11:43:59 +08005623 int ret = 0, ref_credits;
Joel Becker3e632942008-10-24 17:04:49 -07005624 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005625 u16 i;
5626 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005627 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5628 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005629 int credits = ocfs2_remove_extent_credits(osb->sb) +
5630 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Mace9c5a52009-08-18 11:43:59 +08005631 struct ocfs2_xattr_value_root *xv;
5632 struct ocfs2_rm_xattr_bucket_para *args =
5633 (struct ocfs2_rm_xattr_bucket_para *)para;
Tao Ma78f30c32008-11-12 08:27:00 +08005634
5635 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005636
5637 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5638 xe = &xh->xh_entries[i];
5639 if (ocfs2_xattr_is_local(xe))
5640 continue;
5641
Tao Mace9c5a52009-08-18 11:43:59 +08005642 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket,
5643 i, &xv, NULL);
5644
5645 ret = ocfs2_lock_xattr_remove_allocators(inode, xv,
5646 args->ref_ci,
5647 args->ref_root_bh,
5648 &ctxt.meta_ac,
5649 &ref_credits);
5650
5651 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma88c3b062008-12-11 08:54:11 +08005652 if (IS_ERR(ctxt.handle)) {
5653 ret = PTR_ERR(ctxt.handle);
5654 mlog_errno(ret);
5655 break;
5656 }
5657
Joel Becker548b0f22008-11-24 19:32:13 -08005658 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005659 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005660
5661 ocfs2_commit_trans(osb, ctxt.handle);
Tao Mace9c5a52009-08-18 11:43:59 +08005662 if (ctxt.meta_ac) {
5663 ocfs2_free_alloc_context(ctxt.meta_ac);
5664 ctxt.meta_ac = NULL;
5665 }
Tao Maa3944252008-08-18 17:38:54 +08005666 if (ret) {
5667 mlog_errno(ret);
5668 break;
5669 }
5670 }
5671
Tao Mace9c5a52009-08-18 11:43:59 +08005672 if (ctxt.meta_ac)
5673 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma78f30c32008-11-12 08:27:00 +08005674 ocfs2_schedule_truncate_log_flush(osb, 1);
5675 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005676 return ret;
5677}
5678
Mark Fasheh99219ae2008-10-07 14:52:59 -07005679/*
Tao Ma492a8a32009-08-18 11:43:17 +08005680 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5681 * or change the extent record flag), we need to recalculate
5682 * the metaecc for the whole bucket. So it is done here.
5683 *
5684 * Note:
5685 * We have to give the extra credits for the caller.
5686 */
5687static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5688 handle_t *handle,
5689 void *para)
5690{
5691 int ret;
5692 struct ocfs2_xattr_bucket *bucket =
5693 (struct ocfs2_xattr_bucket *)para;
5694
5695 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5696 OCFS2_JOURNAL_ACCESS_WRITE);
5697 if (ret) {
5698 mlog_errno(ret);
5699 return ret;
5700 }
5701
5702 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5703
5704 return 0;
5705}
5706
5707/*
5708 * Special action we need if the xattr value is refcounted.
5709 *
5710 * 1. If the xattr is refcounted, lock the tree.
5711 * 2. CoW the xattr if we are setting the new value and the value
5712 * will be stored outside.
5713 * 3. In other case, decrease_refcount will work for us, so just
5714 * lock the refcount tree, calculate the meta and credits is OK.
5715 *
5716 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5717 * currently CoW is a completed transaction, while this function
5718 * will also lock the allocators and let us deadlock. So we will
5719 * CoW the whole xattr value.
5720 */
5721static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5722 struct ocfs2_dinode *di,
5723 struct ocfs2_xattr_info *xi,
5724 struct ocfs2_xattr_search *xis,
5725 struct ocfs2_xattr_search *xbs,
5726 struct ocfs2_refcount_tree **ref_tree,
5727 int *meta_add,
5728 int *credits)
5729{
5730 int ret = 0;
5731 struct ocfs2_xattr_block *xb;
5732 struct ocfs2_xattr_entry *xe;
5733 char *base;
5734 u32 p_cluster, num_clusters;
5735 unsigned int ext_flags;
5736 int name_offset, name_len;
5737 struct ocfs2_xattr_value_buf vb;
5738 struct ocfs2_xattr_bucket *bucket = NULL;
5739 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5740 struct ocfs2_post_refcount refcount;
5741 struct ocfs2_post_refcount *p = NULL;
5742 struct buffer_head *ref_root_bh = NULL;
5743
5744 if (!xis->not_found) {
5745 xe = xis->here;
5746 name_offset = le16_to_cpu(xe->xe_name_offset);
5747 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5748 base = xis->base;
5749 vb.vb_bh = xis->inode_bh;
5750 vb.vb_access = ocfs2_journal_access_di;
5751 } else {
5752 int i, block_off = 0;
5753 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5754 xe = xbs->here;
5755 name_offset = le16_to_cpu(xe->xe_name_offset);
5756 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5757 i = xbs->here - xbs->header->xh_entries;
5758
5759 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005760 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005761 bucket_xh(xbs->bucket),
5762 i, &block_off,
5763 &name_offset);
5764 if (ret) {
5765 mlog_errno(ret);
5766 goto out;
5767 }
5768 base = bucket_block(xbs->bucket, block_off);
5769 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5770 vb.vb_access = ocfs2_journal_access;
5771
5772 if (ocfs2_meta_ecc(osb)) {
5773 /*create parameters for ocfs2_post_refcount. */
5774 bucket = xbs->bucket;
5775 refcount.credits = bucket->bu_blocks;
5776 refcount.para = bucket;
5777 refcount.func =
5778 ocfs2_xattr_bucket_post_refcount;
5779 p = &refcount;
5780 }
5781 } else {
5782 base = xbs->base;
5783 vb.vb_bh = xbs->xattr_bh;
5784 vb.vb_access = ocfs2_journal_access_xb;
5785 }
5786 }
5787
5788 if (ocfs2_xattr_is_local(xe))
5789 goto out;
5790
5791 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5792 (base + name_offset + name_len);
5793
5794 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5795 &num_clusters, &vb.vb_xv->xr_list,
5796 &ext_flags);
5797 if (ret) {
5798 mlog_errno(ret);
5799 goto out;
5800 }
5801
5802 /*
5803 * We just need to check the 1st extent record, since we always
5804 * CoW the whole xattr. So there shouldn't be a xattr with
5805 * some REFCOUNT extent recs after the 1st one.
5806 */
5807 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5808 goto out;
5809
5810 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5811 1, ref_tree, &ref_root_bh);
5812 if (ret) {
5813 mlog_errno(ret);
5814 goto out;
5815 }
5816
5817 /*
5818 * If we are deleting the xattr or the new size will be stored inside,
5819 * cool, leave it there, the xattr truncate process will remove them
5820 * for us(it still needs the refcount tree lock and the meta, credits).
5821 * And the worse case is that every cluster truncate will split the
5822 * refcount tree, and make the original extent become 3. So we will need
5823 * 2 * cluster more extent recs at most.
5824 */
Joel Becker6b240ff2009-08-14 18:02:52 -07005825 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
Tao Ma492a8a32009-08-18 11:43:17 +08005826
5827 ret = ocfs2_refcounted_xattr_delete_need(inode,
5828 &(*ref_tree)->rf_ci,
5829 ref_root_bh, vb.vb_xv,
5830 meta_add, credits);
5831 if (ret)
5832 mlog_errno(ret);
5833 goto out;
5834 }
5835
5836 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5837 *ref_tree, ref_root_bh, 0,
5838 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5839 if (ret)
5840 mlog_errno(ret);
5841
5842out:
5843 brelse(ref_root_bh);
5844 return ret;
5845}
5846
5847/*
Tao Ma01292412009-09-21 13:04:19 +08005848 * Add the REFCOUNTED flags for all the extent rec in ocfs2_xattr_value_root.
5849 * The physical clusters will be added to refcount tree.
5850 */
5851static int ocfs2_xattr_value_attach_refcount(struct inode *inode,
5852 struct ocfs2_xattr_value_root *xv,
5853 struct ocfs2_extent_tree *value_et,
5854 struct ocfs2_caching_info *ref_ci,
5855 struct buffer_head *ref_root_bh,
5856 struct ocfs2_cached_dealloc_ctxt *dealloc,
5857 struct ocfs2_post_refcount *refcount)
5858{
5859 int ret = 0;
5860 u32 clusters = le32_to_cpu(xv->xr_clusters);
5861 u32 cpos, p_cluster, num_clusters;
5862 struct ocfs2_extent_list *el = &xv->xr_list;
5863 unsigned int ext_flags;
5864
5865 cpos = 0;
5866 while (cpos < clusters) {
5867 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
5868 &num_clusters, el, &ext_flags);
5869
5870 cpos += num_clusters;
5871 if ((ext_flags & OCFS2_EXT_REFCOUNTED))
5872 continue;
5873
5874 BUG_ON(!p_cluster);
5875
5876 ret = ocfs2_add_refcount_flag(inode, value_et,
5877 ref_ci, ref_root_bh,
5878 cpos - num_clusters,
5879 p_cluster, num_clusters,
5880 dealloc, refcount);
5881 if (ret) {
5882 mlog_errno(ret);
5883 break;
5884 }
5885 }
5886
5887 return ret;
5888}
5889
5890/*
5891 * Given a normal ocfs2_xattr_header, refcount all the entries which
5892 * have value stored outside.
5893 * Used for xattrs stored in inode and ocfs2_xattr_block.
5894 */
5895static int ocfs2_xattr_attach_refcount_normal(struct inode *inode,
5896 struct ocfs2_xattr_value_buf *vb,
5897 struct ocfs2_xattr_header *header,
5898 struct ocfs2_caching_info *ref_ci,
5899 struct buffer_head *ref_root_bh,
5900 struct ocfs2_cached_dealloc_ctxt *dealloc)
5901{
5902
5903 struct ocfs2_xattr_entry *xe;
5904 struct ocfs2_xattr_value_root *xv;
5905 struct ocfs2_extent_tree et;
5906 int i, ret = 0;
5907
5908 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
5909 xe = &header->xh_entries[i];
5910
5911 if (ocfs2_xattr_is_local(xe))
5912 continue;
5913
5914 xv = (struct ocfs2_xattr_value_root *)((void *)header +
5915 le16_to_cpu(xe->xe_name_offset) +
5916 OCFS2_XATTR_SIZE(xe->xe_name_len));
5917
5918 vb->vb_xv = xv;
5919 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
5920
5921 ret = ocfs2_xattr_value_attach_refcount(inode, xv, &et,
5922 ref_ci, ref_root_bh,
5923 dealloc, NULL);
5924 if (ret) {
5925 mlog_errno(ret);
5926 break;
5927 }
5928 }
5929
5930 return ret;
5931}
5932
5933static int ocfs2_xattr_inline_attach_refcount(struct inode *inode,
5934 struct buffer_head *fe_bh,
5935 struct ocfs2_caching_info *ref_ci,
5936 struct buffer_head *ref_root_bh,
5937 struct ocfs2_cached_dealloc_ctxt *dealloc)
5938{
5939 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
5940 struct ocfs2_xattr_header *header = (struct ocfs2_xattr_header *)
5941 (fe_bh->b_data + inode->i_sb->s_blocksize -
5942 le16_to_cpu(di->i_xattr_inline_size));
5943 struct ocfs2_xattr_value_buf vb = {
5944 .vb_bh = fe_bh,
5945 .vb_access = ocfs2_journal_access_di,
5946 };
5947
5948 return ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
5949 ref_ci, ref_root_bh, dealloc);
5950}
5951
5952struct ocfs2_xattr_tree_value_refcount_para {
5953 struct ocfs2_caching_info *ref_ci;
5954 struct buffer_head *ref_root_bh;
5955 struct ocfs2_cached_dealloc_ctxt *dealloc;
5956};
5957
5958static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
5959 struct ocfs2_xattr_bucket *bucket,
5960 int offset,
5961 struct ocfs2_xattr_value_root **xv,
5962 struct buffer_head **bh)
5963{
5964 int ret, block_off, name_offset;
5965 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
5966 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
5967 void *base;
5968
5969 ret = ocfs2_xattr_bucket_get_name_value(sb,
5970 bucket_xh(bucket),
5971 offset,
5972 &block_off,
5973 &name_offset);
5974 if (ret) {
5975 mlog_errno(ret);
5976 goto out;
5977 }
5978
5979 base = bucket_block(bucket, block_off);
5980
5981 *xv = (struct ocfs2_xattr_value_root *)(base + name_offset +
5982 OCFS2_XATTR_SIZE(xe->xe_name_len));
5983
5984 if (bh)
5985 *bh = bucket->bu_bhs[block_off];
5986out:
5987 return ret;
5988}
5989
5990/*
5991 * For a given xattr bucket, refcount all the entries which
5992 * have value stored outside.
5993 */
5994static int ocfs2_xattr_bucket_value_refcount(struct inode *inode,
5995 struct ocfs2_xattr_bucket *bucket,
5996 void *para)
5997{
5998 int i, ret = 0;
5999 struct ocfs2_extent_tree et;
6000 struct ocfs2_xattr_tree_value_refcount_para *ref =
6001 (struct ocfs2_xattr_tree_value_refcount_para *)para;
6002 struct ocfs2_xattr_header *xh =
6003 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6004 struct ocfs2_xattr_entry *xe;
6005 struct ocfs2_xattr_value_buf vb = {
6006 .vb_access = ocfs2_journal_access,
6007 };
6008 struct ocfs2_post_refcount refcount = {
6009 .credits = bucket->bu_blocks,
6010 .para = bucket,
6011 .func = ocfs2_xattr_bucket_post_refcount,
6012 };
6013 struct ocfs2_post_refcount *p = NULL;
6014
6015 /* We only need post_refcount if we support metaecc. */
6016 if (ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)))
6017 p = &refcount;
6018
6019 mlog(0, "refcount bucket %llu, count = %u\n",
6020 (unsigned long long)bucket_blkno(bucket),
6021 le16_to_cpu(xh->xh_count));
6022 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6023 xe = &xh->xh_entries[i];
6024
6025 if (ocfs2_xattr_is_local(xe))
6026 continue;
6027
6028 ret = ocfs2_get_xattr_tree_value_root(inode->i_sb, bucket, i,
6029 &vb.vb_xv, &vb.vb_bh);
6030 if (ret) {
6031 mlog_errno(ret);
6032 break;
6033 }
6034
6035 ocfs2_init_xattr_value_extent_tree(&et,
6036 INODE_CACHE(inode), &vb);
6037
6038 ret = ocfs2_xattr_value_attach_refcount(inode, vb.vb_xv,
6039 &et, ref->ref_ci,
6040 ref->ref_root_bh,
6041 ref->dealloc, p);
6042 if (ret) {
6043 mlog_errno(ret);
6044 break;
6045 }
6046 }
6047
6048 return ret;
6049
6050}
6051
6052static int ocfs2_refcount_xattr_tree_rec(struct inode *inode,
6053 struct buffer_head *root_bh,
6054 u64 blkno, u32 cpos, u32 len, void *para)
6055{
6056 return ocfs2_iterate_xattr_buckets(inode, blkno, len,
6057 ocfs2_xattr_bucket_value_refcount,
6058 para);
6059}
6060
6061static int ocfs2_xattr_block_attach_refcount(struct inode *inode,
6062 struct buffer_head *blk_bh,
6063 struct ocfs2_caching_info *ref_ci,
6064 struct buffer_head *ref_root_bh,
6065 struct ocfs2_cached_dealloc_ctxt *dealloc)
6066{
6067 int ret = 0;
6068 struct ocfs2_xattr_block *xb =
6069 (struct ocfs2_xattr_block *)blk_bh->b_data;
6070
6071 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
6072 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
6073 struct ocfs2_xattr_value_buf vb = {
6074 .vb_bh = blk_bh,
6075 .vb_access = ocfs2_journal_access_xb,
6076 };
6077
6078 ret = ocfs2_xattr_attach_refcount_normal(inode, &vb, header,
6079 ref_ci, ref_root_bh,
6080 dealloc);
6081 } else {
6082 struct ocfs2_xattr_tree_value_refcount_para para = {
6083 .ref_ci = ref_ci,
6084 .ref_root_bh = ref_root_bh,
6085 .dealloc = dealloc,
6086 };
6087
6088 ret = ocfs2_iterate_xattr_index_block(inode, blk_bh,
6089 ocfs2_refcount_xattr_tree_rec,
6090 &para);
6091 }
6092
6093 return ret;
6094}
6095
6096int ocfs2_xattr_attach_refcount_tree(struct inode *inode,
6097 struct buffer_head *fe_bh,
6098 struct ocfs2_caching_info *ref_ci,
6099 struct buffer_head *ref_root_bh,
6100 struct ocfs2_cached_dealloc_ctxt *dealloc)
6101{
6102 int ret = 0;
6103 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6104 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
6105 struct buffer_head *blk_bh = NULL;
6106
6107 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
6108 ret = ocfs2_xattr_inline_attach_refcount(inode, fe_bh,
6109 ref_ci, ref_root_bh,
6110 dealloc);
6111 if (ret) {
6112 mlog_errno(ret);
6113 goto out;
6114 }
6115 }
6116
6117 if (!di->i_xattr_loc)
6118 goto out;
6119
6120 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
6121 &blk_bh);
6122 if (ret < 0) {
6123 mlog_errno(ret);
6124 goto out;
6125 }
6126
6127 ret = ocfs2_xattr_block_attach_refcount(inode, blk_bh, ref_ci,
6128 ref_root_bh, dealloc);
6129 if (ret)
6130 mlog_errno(ret);
6131
6132 brelse(blk_bh);
6133out:
6134
6135 return ret;
6136}
6137
Tao Ma0fe9b662009-08-18 11:47:56 +08006138typedef int (should_xattr_reflinked)(struct ocfs2_xattr_entry *xe);
Tao Ma01292412009-09-21 13:04:19 +08006139/*
Tao Ma2999d122009-08-18 11:43:55 +08006140 * Store the information we need in xattr reflink.
6141 * old_bh and new_bh are inode bh for the old and new inode.
6142 */
6143struct ocfs2_xattr_reflink {
6144 struct inode *old_inode;
6145 struct inode *new_inode;
6146 struct buffer_head *old_bh;
6147 struct buffer_head *new_bh;
6148 struct ocfs2_caching_info *ref_ci;
6149 struct buffer_head *ref_root_bh;
6150 struct ocfs2_cached_dealloc_ctxt *dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08006151 should_xattr_reflinked *xattr_reflinked;
Tao Ma2999d122009-08-18 11:43:55 +08006152};
6153
6154/*
6155 * Given a xattr header and xe offset,
6156 * return the proper xv and the corresponding bh.
6157 * xattr in inode, block and xattr tree have different implementaions.
6158 */
6159typedef int (get_xattr_value_root)(struct super_block *sb,
6160 struct buffer_head *bh,
6161 struct ocfs2_xattr_header *xh,
6162 int offset,
6163 struct ocfs2_xattr_value_root **xv,
6164 struct buffer_head **ret_bh,
6165 void *para);
6166
6167/*
6168 * Calculate all the xattr value root metadata stored in this xattr header and
6169 * credits we need if we create them from the scratch.
6170 * We use get_xattr_value_root so that all types of xattr container can use it.
6171 */
6172static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6173 struct buffer_head *bh,
6174 struct ocfs2_xattr_header *xh,
6175 int *metas, int *credits,
6176 int *num_recs,
6177 get_xattr_value_root *func,
6178 void *para)
6179{
6180 int i, ret = 0;
6181 struct ocfs2_xattr_value_root *xv;
6182 struct ocfs2_xattr_entry *xe;
6183
6184 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
6185 xe = &xh->xh_entries[i];
6186 if (ocfs2_xattr_is_local(xe))
6187 continue;
6188
6189 ret = func(sb, bh, xh, i, &xv, NULL, para);
6190 if (ret) {
6191 mlog_errno(ret);
6192 break;
6193 }
6194
6195 *metas += le16_to_cpu(xv->xr_list.l_tree_depth) *
6196 le16_to_cpu(xv->xr_list.l_next_free_rec);
6197
6198 *credits += ocfs2_calc_extend_credits(sb,
6199 &def_xv.xv.xr_list,
6200 le32_to_cpu(xv->xr_clusters));
6201
6202 /*
6203 * If the value is a tree with depth > 1, We don't go deep
6204 * to the extent block, so just calculate a maximum record num.
6205 */
6206 if (!xv->xr_list.l_tree_depth)
Tao Ma8ff6af82009-12-23 14:31:15 +08006207 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
Tao Ma2999d122009-08-18 11:43:55 +08006208 else
6209 *num_recs += ocfs2_clusters_for_bytes(sb,
6210 XATTR_SIZE_MAX);
6211 }
6212
6213 return ret;
6214}
6215
6216/* Used by xattr inode and block to return the right xv and buffer_head. */
6217static int ocfs2_get_xattr_value_root(struct super_block *sb,
6218 struct buffer_head *bh,
6219 struct ocfs2_xattr_header *xh,
6220 int offset,
6221 struct ocfs2_xattr_value_root **xv,
6222 struct buffer_head **ret_bh,
6223 void *para)
6224{
6225 struct ocfs2_xattr_entry *xe = &xh->xh_entries[offset];
6226
6227 *xv = (struct ocfs2_xattr_value_root *)((void *)xh +
6228 le16_to_cpu(xe->xe_name_offset) +
6229 OCFS2_XATTR_SIZE(xe->xe_name_len));
6230
6231 if (ret_bh)
6232 *ret_bh = bh;
6233
6234 return 0;
6235}
6236
6237/*
6238 * Lock the meta_ac and caculate how much credits we need for reflink xattrs.
6239 * It is only used for inline xattr and xattr block.
6240 */
6241static int ocfs2_reflink_lock_xattr_allocators(struct ocfs2_super *osb,
6242 struct ocfs2_xattr_header *xh,
6243 struct buffer_head *ref_root_bh,
6244 int *credits,
6245 struct ocfs2_alloc_context **meta_ac)
6246{
6247 int ret, meta_add = 0, num_recs = 0;
6248 struct ocfs2_refcount_block *rb =
6249 (struct ocfs2_refcount_block *)ref_root_bh->b_data;
6250
6251 *credits = 0;
6252
6253 ret = ocfs2_value_metas_in_xattr_header(osb->sb, NULL, xh,
6254 &meta_add, credits, &num_recs,
6255 ocfs2_get_xattr_value_root,
6256 NULL);
6257 if (ret) {
6258 mlog_errno(ret);
6259 goto out;
6260 }
6261
6262 /*
6263 * We need to add/modify num_recs in refcount tree, so just calculate
6264 * an approximate number we need for refcount tree change.
6265 * Sometimes we need to split the tree, and after split, half recs
6266 * will be moved to the new block, and a new block can only provide
6267 * half number of recs. So we multiple new blocks by 2.
6268 */
6269 num_recs = num_recs / ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6270 meta_add += num_recs;
6271 *credits += num_recs + num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6272 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6273 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6274 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6275 else
6276 *credits += 1;
6277
6278 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, meta_ac);
6279 if (ret)
6280 mlog_errno(ret);
6281
6282out:
6283 return ret;
6284}
6285
6286/*
6287 * Given a xattr header, reflink all the xattrs in this container.
6288 * It can be used for inode, block and bucket.
6289 *
6290 * NOTE:
6291 * Before we call this function, the caller has memcpy the xattr in
6292 * old_xh to the new_xh.
Tao Ma0fe9b662009-08-18 11:47:56 +08006293 *
6294 * If args.xattr_reflinked is set, call it to decide whether the xe should
6295 * be reflinked or not. If not, remove it from the new xattr header.
Tao Ma2999d122009-08-18 11:43:55 +08006296 */
6297static int ocfs2_reflink_xattr_header(handle_t *handle,
6298 struct ocfs2_xattr_reflink *args,
6299 struct buffer_head *old_bh,
6300 struct ocfs2_xattr_header *xh,
6301 struct buffer_head *new_bh,
6302 struct ocfs2_xattr_header *new_xh,
6303 struct ocfs2_xattr_value_buf *vb,
6304 struct ocfs2_alloc_context *meta_ac,
6305 get_xattr_value_root *func,
6306 void *para)
6307{
Tao Ma0fe9b662009-08-18 11:47:56 +08006308 int ret = 0, i, j;
Tao Ma2999d122009-08-18 11:43:55 +08006309 struct super_block *sb = args->old_inode->i_sb;
6310 struct buffer_head *value_bh;
Tao Ma0fe9b662009-08-18 11:47:56 +08006311 struct ocfs2_xattr_entry *xe, *last;
Tao Ma2999d122009-08-18 11:43:55 +08006312 struct ocfs2_xattr_value_root *xv, *new_xv;
6313 struct ocfs2_extent_tree data_et;
6314 u32 clusters, cpos, p_cluster, num_clusters;
6315 unsigned int ext_flags = 0;
6316
6317 mlog(0, "reflink xattr in container %llu, count = %u\n",
6318 (unsigned long long)old_bh->b_blocknr, le16_to_cpu(xh->xh_count));
Tao Ma0fe9b662009-08-18 11:47:56 +08006319
6320 last = &new_xh->xh_entries[le16_to_cpu(new_xh->xh_count)];
6321 for (i = 0, j = 0; i < le16_to_cpu(xh->xh_count); i++, j++) {
Tao Ma2999d122009-08-18 11:43:55 +08006322 xe = &xh->xh_entries[i];
6323
Tao Ma0fe9b662009-08-18 11:47:56 +08006324 if (args->xattr_reflinked && !args->xattr_reflinked(xe)) {
6325 xe = &new_xh->xh_entries[j];
6326
6327 le16_add_cpu(&new_xh->xh_count, -1);
6328 if (new_xh->xh_count) {
6329 memmove(xe, xe + 1,
6330 (void *)last - (void *)xe);
6331 memset(last, 0,
6332 sizeof(struct ocfs2_xattr_entry));
6333 }
6334
6335 /*
6336 * We don't want j to increase in the next round since
6337 * it is already moved ahead.
6338 */
6339 j--;
6340 continue;
6341 }
6342
Tao Ma2999d122009-08-18 11:43:55 +08006343 if (ocfs2_xattr_is_local(xe))
6344 continue;
6345
6346 ret = func(sb, old_bh, xh, i, &xv, NULL, para);
6347 if (ret) {
6348 mlog_errno(ret);
6349 break;
6350 }
6351
Tao Ma0fe9b662009-08-18 11:47:56 +08006352 ret = func(sb, new_bh, new_xh, j, &new_xv, &value_bh, para);
Tao Ma2999d122009-08-18 11:43:55 +08006353 if (ret) {
6354 mlog_errno(ret);
6355 break;
6356 }
6357
6358 /*
6359 * For the xattr which has l_tree_depth = 0, all the extent
6360 * recs have already be copied to the new xh with the
6361 * propriate OCFS2_EXT_REFCOUNTED flag we just need to
6362 * increase the refount count int the refcount tree.
6363 *
6364 * For the xattr which has l_tree_depth > 0, we need
6365 * to initialize it to the empty default value root,
6366 * and then insert the extents one by one.
6367 */
6368 if (xv->xr_list.l_tree_depth) {
6369 memcpy(new_xv, &def_xv, sizeof(def_xv));
6370 vb->vb_xv = new_xv;
6371 vb->vb_bh = value_bh;
6372 ocfs2_init_xattr_value_extent_tree(&data_et,
6373 INODE_CACHE(args->new_inode), vb);
6374 }
6375
6376 clusters = le32_to_cpu(xv->xr_clusters);
6377 cpos = 0;
6378 while (cpos < clusters) {
6379 ret = ocfs2_xattr_get_clusters(args->old_inode,
6380 cpos,
6381 &p_cluster,
6382 &num_clusters,
6383 &xv->xr_list,
6384 &ext_flags);
6385 if (ret) {
6386 mlog_errno(ret);
6387 goto out;
6388 }
6389
6390 BUG_ON(!p_cluster);
6391
6392 if (xv->xr_list.l_tree_depth) {
6393 ret = ocfs2_insert_extent(handle,
6394 &data_et, cpos,
6395 ocfs2_clusters_to_blocks(
6396 args->old_inode->i_sb,
6397 p_cluster),
6398 num_clusters, ext_flags,
6399 meta_ac);
6400 if (ret) {
6401 mlog_errno(ret);
6402 goto out;
6403 }
6404 }
6405
6406 ret = ocfs2_increase_refcount(handle, args->ref_ci,
6407 args->ref_root_bh,
6408 p_cluster, num_clusters,
6409 meta_ac, args->dealloc);
6410 if (ret) {
6411 mlog_errno(ret);
6412 goto out;
6413 }
6414
6415 cpos += num_clusters;
6416 }
6417 }
6418
6419out:
6420 return ret;
6421}
6422
6423static int ocfs2_reflink_xattr_inline(struct ocfs2_xattr_reflink *args)
6424{
6425 int ret = 0, credits = 0;
6426 handle_t *handle;
6427 struct ocfs2_super *osb = OCFS2_SB(args->old_inode->i_sb);
6428 struct ocfs2_dinode *di = (struct ocfs2_dinode *)args->old_bh->b_data;
6429 int inline_size = le16_to_cpu(di->i_xattr_inline_size);
6430 int header_off = osb->sb->s_blocksize - inline_size;
6431 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)
6432 (args->old_bh->b_data + header_off);
6433 struct ocfs2_xattr_header *new_xh = (struct ocfs2_xattr_header *)
6434 (args->new_bh->b_data + header_off);
6435 struct ocfs2_alloc_context *meta_ac = NULL;
6436 struct ocfs2_inode_info *new_oi;
6437 struct ocfs2_dinode *new_di;
6438 struct ocfs2_xattr_value_buf vb = {
6439 .vb_bh = args->new_bh,
6440 .vb_access = ocfs2_journal_access_di,
6441 };
6442
6443 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6444 &credits, &meta_ac);
6445 if (ret) {
6446 mlog_errno(ret);
6447 goto out;
6448 }
6449
6450 handle = ocfs2_start_trans(osb, credits);
6451 if (IS_ERR(handle)) {
6452 ret = PTR_ERR(handle);
6453 mlog_errno(ret);
6454 goto out;
6455 }
6456
6457 ret = ocfs2_journal_access_di(handle, INODE_CACHE(args->new_inode),
6458 args->new_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6459 if (ret) {
6460 mlog_errno(ret);
6461 goto out_commit;
6462 }
6463
6464 memcpy(args->new_bh->b_data + header_off,
6465 args->old_bh->b_data + header_off, inline_size);
6466
6467 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6468 new_di->i_xattr_inline_size = cpu_to_le16(inline_size);
6469
6470 ret = ocfs2_reflink_xattr_header(handle, args, args->old_bh, xh,
6471 args->new_bh, new_xh, &vb, meta_ac,
6472 ocfs2_get_xattr_value_root, NULL);
6473 if (ret) {
6474 mlog_errno(ret);
6475 goto out_commit;
6476 }
6477
6478 new_oi = OCFS2_I(args->new_inode);
6479 spin_lock(&new_oi->ip_lock);
6480 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL | OCFS2_INLINE_XATTR_FL;
6481 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6482 spin_unlock(&new_oi->ip_lock);
6483
6484 ocfs2_journal_dirty(handle, args->new_bh);
6485
6486out_commit:
6487 ocfs2_commit_trans(osb, handle);
6488
6489out:
6490 if (meta_ac)
6491 ocfs2_free_alloc_context(meta_ac);
6492 return ret;
6493}
6494
6495static int ocfs2_create_empty_xattr_block(struct inode *inode,
6496 struct buffer_head *fe_bh,
6497 struct buffer_head **ret_bh,
6498 int indexed)
6499{
6500 int ret;
Tao Ma2999d122009-08-18 11:43:55 +08006501 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Mab2317962010-03-19 15:04:24 +08006502 struct ocfs2_xattr_set_ctxt ctxt;
Tao Ma2999d122009-08-18 11:43:55 +08006503
Tao Mab2317962010-03-19 15:04:24 +08006504 memset(&ctxt, 0, sizeof(ctxt));
6505 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
Tao Ma2999d122009-08-18 11:43:55 +08006506 if (ret < 0) {
6507 mlog_errno(ret);
6508 return ret;
6509 }
6510
Joel Beckerd3981542009-08-19 02:13:50 -07006511 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6512 if (IS_ERR(ctxt.handle)) {
6513 ret = PTR_ERR(ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006514 mlog_errno(ret);
6515 goto out;
6516 }
6517
6518 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6519 (unsigned long long)fe_bh->b_blocknr, indexed);
Joel Beckerd3981542009-08-19 02:13:50 -07006520 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6521 ret_bh);
Tao Ma2999d122009-08-18 11:43:55 +08006522 if (ret)
6523 mlog_errno(ret);
6524
Joel Beckerd3981542009-08-19 02:13:50 -07006525 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma2999d122009-08-18 11:43:55 +08006526out:
Tao Mab2317962010-03-19 15:04:24 +08006527 ocfs2_free_alloc_context(ctxt.meta_ac);
Tao Ma2999d122009-08-18 11:43:55 +08006528 return ret;
6529}
6530
6531static int ocfs2_reflink_xattr_block(struct ocfs2_xattr_reflink *args,
6532 struct buffer_head *blk_bh,
6533 struct buffer_head *new_blk_bh)
6534{
6535 int ret = 0, credits = 0;
6536 handle_t *handle;
6537 struct ocfs2_inode_info *new_oi = OCFS2_I(args->new_inode);
6538 struct ocfs2_dinode *new_di;
6539 struct ocfs2_super *osb = OCFS2_SB(args->new_inode->i_sb);
6540 int header_off = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
6541 struct ocfs2_xattr_block *xb =
6542 (struct ocfs2_xattr_block *)blk_bh->b_data;
6543 struct ocfs2_xattr_header *xh = &xb->xb_attrs.xb_header;
6544 struct ocfs2_xattr_block *new_xb =
6545 (struct ocfs2_xattr_block *)new_blk_bh->b_data;
6546 struct ocfs2_xattr_header *new_xh = &new_xb->xb_attrs.xb_header;
6547 struct ocfs2_alloc_context *meta_ac;
6548 struct ocfs2_xattr_value_buf vb = {
6549 .vb_bh = new_blk_bh,
6550 .vb_access = ocfs2_journal_access_xb,
6551 };
6552
6553 ret = ocfs2_reflink_lock_xattr_allocators(osb, xh, args->ref_root_bh,
6554 &credits, &meta_ac);
6555 if (ret) {
6556 mlog_errno(ret);
6557 return ret;
6558 }
6559
6560 /* One more credits in case we need to add xattr flags in new inode. */
6561 handle = ocfs2_start_trans(osb, credits + 1);
6562 if (IS_ERR(handle)) {
6563 ret = PTR_ERR(handle);
6564 mlog_errno(ret);
6565 goto out;
6566 }
6567
6568 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6569 ret = ocfs2_journal_access_di(handle,
6570 INODE_CACHE(args->new_inode),
6571 args->new_bh,
6572 OCFS2_JOURNAL_ACCESS_WRITE);
6573 if (ret) {
6574 mlog_errno(ret);
6575 goto out_commit;
6576 }
6577 }
6578
6579 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(args->new_inode),
6580 new_blk_bh, OCFS2_JOURNAL_ACCESS_WRITE);
6581 if (ret) {
6582 mlog_errno(ret);
6583 goto out_commit;
6584 }
6585
6586 memcpy(new_blk_bh->b_data + header_off, blk_bh->b_data + header_off,
6587 osb->sb->s_blocksize - header_off);
6588
6589 ret = ocfs2_reflink_xattr_header(handle, args, blk_bh, xh,
6590 new_blk_bh, new_xh, &vb, meta_ac,
6591 ocfs2_get_xattr_value_root, NULL);
6592 if (ret) {
6593 mlog_errno(ret);
6594 goto out_commit;
6595 }
6596
6597 ocfs2_journal_dirty(handle, new_blk_bh);
6598
6599 if (!(new_oi->ip_dyn_features & OCFS2_HAS_XATTR_FL)) {
6600 new_di = (struct ocfs2_dinode *)args->new_bh->b_data;
6601 spin_lock(&new_oi->ip_lock);
6602 new_oi->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
6603 new_di->i_dyn_features = cpu_to_le16(new_oi->ip_dyn_features);
6604 spin_unlock(&new_oi->ip_lock);
6605
6606 ocfs2_journal_dirty(handle, args->new_bh);
6607 }
6608
6609out_commit:
6610 ocfs2_commit_trans(osb, handle);
6611
6612out:
6613 ocfs2_free_alloc_context(meta_ac);
6614 return ret;
6615}
6616
6617struct ocfs2_reflink_xattr_tree_args {
6618 struct ocfs2_xattr_reflink *reflink;
6619 struct buffer_head *old_blk_bh;
6620 struct buffer_head *new_blk_bh;
6621 struct ocfs2_xattr_bucket *old_bucket;
6622 struct ocfs2_xattr_bucket *new_bucket;
6623};
6624
6625/*
6626 * NOTE:
6627 * We have to handle the case that both old bucket and new bucket
6628 * will call this function to get the right ret_bh.
6629 * So The caller must give us the right bh.
6630 */
6631static int ocfs2_get_reflink_xattr_value_root(struct super_block *sb,
6632 struct buffer_head *bh,
6633 struct ocfs2_xattr_header *xh,
6634 int offset,
6635 struct ocfs2_xattr_value_root **xv,
6636 struct buffer_head **ret_bh,
6637 void *para)
6638{
6639 struct ocfs2_reflink_xattr_tree_args *args =
6640 (struct ocfs2_reflink_xattr_tree_args *)para;
6641 struct ocfs2_xattr_bucket *bucket;
6642
6643 if (bh == args->old_bucket->bu_bhs[0])
6644 bucket = args->old_bucket;
6645 else
6646 bucket = args->new_bucket;
6647
6648 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6649 xv, ret_bh);
6650}
6651
6652struct ocfs2_value_tree_metas {
6653 int num_metas;
6654 int credits;
6655 int num_recs;
6656};
6657
6658static int ocfs2_value_tree_metas_in_bucket(struct super_block *sb,
6659 struct buffer_head *bh,
6660 struct ocfs2_xattr_header *xh,
6661 int offset,
6662 struct ocfs2_xattr_value_root **xv,
6663 struct buffer_head **ret_bh,
6664 void *para)
6665{
6666 struct ocfs2_xattr_bucket *bucket =
6667 (struct ocfs2_xattr_bucket *)para;
6668
6669 return ocfs2_get_xattr_tree_value_root(sb, bucket, offset,
6670 xv, ret_bh);
6671}
6672
6673static int ocfs2_calc_value_tree_metas(struct inode *inode,
6674 struct ocfs2_xattr_bucket *bucket,
6675 void *para)
6676{
6677 struct ocfs2_value_tree_metas *metas =
6678 (struct ocfs2_value_tree_metas *)para;
6679 struct ocfs2_xattr_header *xh =
6680 (struct ocfs2_xattr_header *)bucket->bu_bhs[0]->b_data;
6681
6682 /* Add the credits for this bucket first. */
6683 metas->credits += bucket->bu_blocks;
6684 return ocfs2_value_metas_in_xattr_header(inode->i_sb, bucket->bu_bhs[0],
6685 xh, &metas->num_metas,
6686 &metas->credits, &metas->num_recs,
6687 ocfs2_value_tree_metas_in_bucket,
6688 bucket);
6689}
6690
6691/*
6692 * Given a xattr extent rec starting from blkno and having len clusters,
6693 * iterate all the buckets calculate how much metadata we need for reflinking
6694 * all the ocfs2_xattr_value_root and lock the allocators accordingly.
6695 */
6696static int ocfs2_lock_reflink_xattr_rec_allocators(
6697 struct ocfs2_reflink_xattr_tree_args *args,
6698 struct ocfs2_extent_tree *xt_et,
6699 u64 blkno, u32 len, int *credits,
6700 struct ocfs2_alloc_context **meta_ac,
6701 struct ocfs2_alloc_context **data_ac)
6702{
6703 int ret, num_free_extents;
6704 struct ocfs2_value_tree_metas metas;
6705 struct ocfs2_super *osb = OCFS2_SB(args->reflink->old_inode->i_sb);
6706 struct ocfs2_refcount_block *rb;
6707
6708 memset(&metas, 0, sizeof(metas));
6709
6710 ret = ocfs2_iterate_xattr_buckets(args->reflink->old_inode, blkno, len,
6711 ocfs2_calc_value_tree_metas, &metas);
6712 if (ret) {
6713 mlog_errno(ret);
6714 goto out;
6715 }
6716
6717 *credits = metas.credits;
6718
6719 /*
6720 * Calculate we need for refcount tree change.
6721 *
6722 * We need to add/modify num_recs in refcount tree, so just calculate
6723 * an approximate number we need for refcount tree change.
6724 * Sometimes we need to split the tree, and after split, half recs
6725 * will be moved to the new block, and a new block can only provide
6726 * half number of recs. So we multiple new blocks by 2.
6727 * In the end, we have to add credits for modifying the already
6728 * existed refcount block.
6729 */
6730 rb = (struct ocfs2_refcount_block *)args->reflink->ref_root_bh->b_data;
6731 metas.num_recs =
6732 (metas.num_recs + ocfs2_refcount_recs_per_rb(osb->sb) - 1) /
6733 ocfs2_refcount_recs_per_rb(osb->sb) * 2;
6734 metas.num_metas += metas.num_recs;
6735 *credits += metas.num_recs +
6736 metas.num_recs * OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
6737 if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
6738 *credits += le16_to_cpu(rb->rf_list.l_tree_depth) *
6739 le16_to_cpu(rb->rf_list.l_next_free_rec) + 1;
6740 else
6741 *credits += 1;
6742
6743 /* count in the xattr tree change. */
6744 num_free_extents = ocfs2_num_free_extents(osb, xt_et);
6745 if (num_free_extents < 0) {
6746 ret = num_free_extents;
6747 mlog_errno(ret);
6748 goto out;
6749 }
6750
6751 if (num_free_extents < len)
6752 metas.num_metas += ocfs2_extend_meta_needed(xt_et->et_root_el);
6753
6754 *credits += ocfs2_calc_extend_credits(osb->sb,
6755 xt_et->et_root_el, len);
6756
6757 if (metas.num_metas) {
6758 ret = ocfs2_reserve_new_metadata_blocks(osb, metas.num_metas,
6759 meta_ac);
6760 if (ret) {
6761 mlog_errno(ret);
6762 goto out;
6763 }
6764 }
6765
6766 if (len) {
6767 ret = ocfs2_reserve_clusters(osb, len, data_ac);
6768 if (ret)
6769 mlog_errno(ret);
6770 }
6771out:
6772 if (ret) {
6773 if (*meta_ac) {
6774 ocfs2_free_alloc_context(*meta_ac);
6775 meta_ac = NULL;
6776 }
6777 }
6778
6779 return ret;
6780}
6781
6782static int ocfs2_reflink_xattr_buckets(handle_t *handle,
6783 u64 blkno, u64 new_blkno, u32 clusters,
6784 struct ocfs2_alloc_context *meta_ac,
6785 struct ocfs2_alloc_context *data_ac,
6786 struct ocfs2_reflink_xattr_tree_args *args)
6787{
6788 int i, j, ret = 0;
6789 struct super_block *sb = args->reflink->old_inode->i_sb;
6790 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
6791 u32 num_buckets = clusters * bpc;
6792 int bpb = args->old_bucket->bu_blocks;
6793 struct ocfs2_xattr_value_buf vb = {
6794 .vb_access = ocfs2_journal_access,
6795 };
6796
6797 for (i = 0; i < num_buckets; i++, blkno += bpb, new_blkno += bpb) {
6798 ret = ocfs2_read_xattr_bucket(args->old_bucket, blkno);
6799 if (ret) {
6800 mlog_errno(ret);
6801 break;
6802 }
6803
6804 ret = ocfs2_init_xattr_bucket(args->new_bucket, new_blkno);
6805 if (ret) {
6806 mlog_errno(ret);
6807 break;
6808 }
6809
6810 /*
6811 * The real bucket num in this series of blocks is stored
6812 * in the 1st bucket.
6813 */
6814 if (i == 0)
6815 num_buckets = le16_to_cpu(
6816 bucket_xh(args->old_bucket)->xh_num_buckets);
6817
6818 ret = ocfs2_xattr_bucket_journal_access(handle,
6819 args->new_bucket,
6820 OCFS2_JOURNAL_ACCESS_CREATE);
6821 if (ret) {
6822 mlog_errno(ret);
6823 break;
6824 }
6825
6826 for (j = 0; j < bpb; j++)
6827 memcpy(bucket_block(args->new_bucket, j),
6828 bucket_block(args->old_bucket, j),
6829 sb->s_blocksize);
6830
6831 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6832
6833 ret = ocfs2_reflink_xattr_header(handle, args->reflink,
6834 args->old_bucket->bu_bhs[0],
6835 bucket_xh(args->old_bucket),
6836 args->new_bucket->bu_bhs[0],
6837 bucket_xh(args->new_bucket),
6838 &vb, meta_ac,
6839 ocfs2_get_reflink_xattr_value_root,
6840 args);
6841 if (ret) {
6842 mlog_errno(ret);
6843 break;
6844 }
6845
6846 /*
6847 * Re-access and dirty the bucket to calculate metaecc.
6848 * Because we may extend the transaction in reflink_xattr_header
6849 * which will let the already accessed block gone.
6850 */
6851 ret = ocfs2_xattr_bucket_journal_access(handle,
6852 args->new_bucket,
6853 OCFS2_JOURNAL_ACCESS_WRITE);
6854 if (ret) {
6855 mlog_errno(ret);
6856 break;
6857 }
6858
6859 ocfs2_xattr_bucket_journal_dirty(handle, args->new_bucket);
6860 ocfs2_xattr_bucket_relse(args->old_bucket);
6861 ocfs2_xattr_bucket_relse(args->new_bucket);
6862 }
6863
6864 ocfs2_xattr_bucket_relse(args->old_bucket);
6865 ocfs2_xattr_bucket_relse(args->new_bucket);
6866 return ret;
6867}
6868/*
6869 * Create the same xattr extent record in the new inode's xattr tree.
6870 */
6871static int ocfs2_reflink_xattr_rec(struct inode *inode,
6872 struct buffer_head *root_bh,
6873 u64 blkno,
6874 u32 cpos,
6875 u32 len,
6876 void *para)
6877{
6878 int ret, credits = 0;
6879 u32 p_cluster, num_clusters;
6880 u64 new_blkno;
6881 handle_t *handle;
6882 struct ocfs2_reflink_xattr_tree_args *args =
6883 (struct ocfs2_reflink_xattr_tree_args *)para;
6884 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6885 struct ocfs2_alloc_context *meta_ac = NULL;
6886 struct ocfs2_alloc_context *data_ac = NULL;
6887 struct ocfs2_extent_tree et;
6888
6889 ocfs2_init_xattr_tree_extent_tree(&et,
6890 INODE_CACHE(args->reflink->new_inode),
6891 args->new_blk_bh);
6892
6893 ret = ocfs2_lock_reflink_xattr_rec_allocators(args, &et, blkno,
6894 len, &credits,
6895 &meta_ac, &data_ac);
6896 if (ret) {
6897 mlog_errno(ret);
6898 goto out;
6899 }
6900
6901 handle = ocfs2_start_trans(osb, credits);
6902 if (IS_ERR(handle)) {
6903 ret = PTR_ERR(handle);
6904 mlog_errno(ret);
6905 goto out;
6906 }
6907
Joel Becker1ed9b772010-05-06 13:59:06 +08006908 ret = ocfs2_claim_clusters(handle, data_ac,
Tao Ma2999d122009-08-18 11:43:55 +08006909 len, &p_cluster, &num_clusters);
6910 if (ret) {
6911 mlog_errno(ret);
6912 goto out_commit;
6913 }
6914
6915 new_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cluster);
6916
6917 mlog(0, "reflink xattr buckets %llu to %llu, len %u\n",
6918 (unsigned long long)blkno, (unsigned long long)new_blkno, len);
6919 ret = ocfs2_reflink_xattr_buckets(handle, blkno, new_blkno, len,
6920 meta_ac, data_ac, args);
6921 if (ret) {
6922 mlog_errno(ret);
6923 goto out_commit;
6924 }
6925
6926 mlog(0, "insert new xattr extent rec start %llu len %u to %u\n",
6927 (unsigned long long)new_blkno, len, cpos);
6928 ret = ocfs2_insert_extent(handle, &et, cpos, new_blkno,
6929 len, 0, meta_ac);
6930 if (ret)
6931 mlog_errno(ret);
6932
6933out_commit:
6934 ocfs2_commit_trans(osb, handle);
6935
6936out:
6937 if (meta_ac)
6938 ocfs2_free_alloc_context(meta_ac);
6939 if (data_ac)
6940 ocfs2_free_alloc_context(data_ac);
6941 return ret;
6942}
6943
6944/*
6945 * Create reflinked xattr buckets.
6946 * We will add bucket one by one, and refcount all the xattrs in the bucket
6947 * if they are stored outside.
6948 */
6949static int ocfs2_reflink_xattr_tree(struct ocfs2_xattr_reflink *args,
6950 struct buffer_head *blk_bh,
6951 struct buffer_head *new_blk_bh)
6952{
6953 int ret;
6954 struct ocfs2_reflink_xattr_tree_args para;
6955
6956 memset(&para, 0, sizeof(para));
6957 para.reflink = args;
6958 para.old_blk_bh = blk_bh;
6959 para.new_blk_bh = new_blk_bh;
6960
6961 para.old_bucket = ocfs2_xattr_bucket_new(args->old_inode);
6962 if (!para.old_bucket) {
6963 mlog_errno(-ENOMEM);
6964 return -ENOMEM;
6965 }
6966
6967 para.new_bucket = ocfs2_xattr_bucket_new(args->new_inode);
6968 if (!para.new_bucket) {
6969 ret = -ENOMEM;
6970 mlog_errno(ret);
6971 goto out;
6972 }
6973
6974 ret = ocfs2_iterate_xattr_index_block(args->old_inode, blk_bh,
6975 ocfs2_reflink_xattr_rec,
6976 &para);
6977 if (ret)
6978 mlog_errno(ret);
6979
6980out:
6981 ocfs2_xattr_bucket_free(para.old_bucket);
6982 ocfs2_xattr_bucket_free(para.new_bucket);
6983 return ret;
6984}
6985
6986static int ocfs2_reflink_xattr_in_block(struct ocfs2_xattr_reflink *args,
6987 struct buffer_head *blk_bh)
6988{
6989 int ret, indexed = 0;
6990 struct buffer_head *new_blk_bh = NULL;
6991 struct ocfs2_xattr_block *xb =
6992 (struct ocfs2_xattr_block *)blk_bh->b_data;
6993
6994
6995 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)
6996 indexed = 1;
6997
6998 ret = ocfs2_create_empty_xattr_block(args->new_inode, args->new_bh,
6999 &new_blk_bh, indexed);
7000 if (ret) {
7001 mlog_errno(ret);
7002 goto out;
7003 }
7004
7005 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED))
7006 ret = ocfs2_reflink_xattr_block(args, blk_bh, new_blk_bh);
7007 else
7008 ret = ocfs2_reflink_xattr_tree(args, blk_bh, new_blk_bh);
7009 if (ret)
7010 mlog_errno(ret);
7011
7012out:
7013 brelse(new_blk_bh);
7014 return ret;
7015}
7016
Tao Ma0fe9b662009-08-18 11:47:56 +08007017static int ocfs2_reflink_xattr_no_security(struct ocfs2_xattr_entry *xe)
7018{
7019 int type = ocfs2_xattr_get_type(xe);
7020
7021 return type != OCFS2_XATTR_INDEX_SECURITY &&
7022 type != OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS &&
7023 type != OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
7024}
7025
Tao Ma2999d122009-08-18 11:43:55 +08007026int ocfs2_reflink_xattrs(struct inode *old_inode,
7027 struct buffer_head *old_bh,
7028 struct inode *new_inode,
Tao Ma0fe9b662009-08-18 11:47:56 +08007029 struct buffer_head *new_bh,
7030 bool preserve_security)
Tao Ma2999d122009-08-18 11:43:55 +08007031{
7032 int ret;
7033 struct ocfs2_xattr_reflink args;
7034 struct ocfs2_inode_info *oi = OCFS2_I(old_inode);
7035 struct ocfs2_dinode *di = (struct ocfs2_dinode *)old_bh->b_data;
7036 struct buffer_head *blk_bh = NULL;
7037 struct ocfs2_cached_dealloc_ctxt dealloc;
7038 struct ocfs2_refcount_tree *ref_tree;
7039 struct buffer_head *ref_root_bh = NULL;
7040
7041 ret = ocfs2_lock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7042 le64_to_cpu(di->i_refcount_loc),
7043 1, &ref_tree, &ref_root_bh);
7044 if (ret) {
7045 mlog_errno(ret);
7046 goto out;
7047 }
7048
7049 ocfs2_init_dealloc_ctxt(&dealloc);
7050
7051 args.old_inode = old_inode;
7052 args.new_inode = new_inode;
7053 args.old_bh = old_bh;
7054 args.new_bh = new_bh;
7055 args.ref_ci = &ref_tree->rf_ci;
7056 args.ref_root_bh = ref_root_bh;
7057 args.dealloc = &dealloc;
Tao Ma0fe9b662009-08-18 11:47:56 +08007058 if (preserve_security)
7059 args.xattr_reflinked = NULL;
7060 else
7061 args.xattr_reflinked = ocfs2_reflink_xattr_no_security;
Tao Ma2999d122009-08-18 11:43:55 +08007062
7063 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
7064 ret = ocfs2_reflink_xattr_inline(&args);
7065 if (ret) {
7066 mlog_errno(ret);
7067 goto out_unlock;
7068 }
7069 }
7070
7071 if (!di->i_xattr_loc)
7072 goto out_unlock;
7073
7074 ret = ocfs2_read_xattr_block(old_inode, le64_to_cpu(di->i_xattr_loc),
7075 &blk_bh);
7076 if (ret < 0) {
7077 mlog_errno(ret);
7078 goto out_unlock;
7079 }
7080
7081 ret = ocfs2_reflink_xattr_in_block(&args, blk_bh);
7082 if (ret)
7083 mlog_errno(ret);
7084
7085 brelse(blk_bh);
7086
7087out_unlock:
7088 ocfs2_unlock_refcount_tree(OCFS2_SB(old_inode->i_sb),
7089 ref_tree, 1);
7090 brelse(ref_root_bh);
7091
7092 if (ocfs2_dealloc_has_cluster(&dealloc)) {
7093 ocfs2_schedule_truncate_log_flush(OCFS2_SB(old_inode->i_sb), 1);
7094 ocfs2_run_deallocs(OCFS2_SB(old_inode->i_sb), &dealloc);
7095 }
7096
7097out:
7098 return ret;
7099}
7100
7101/*
Tao Ma0fe9b662009-08-18 11:47:56 +08007102 * Initialize security and acl for a already created inode.
7103 * Used for reflink a non-preserve-security file.
7104 *
7105 * It uses common api like ocfs2_xattr_set, so the caller
7106 * must not hold any lock expect i_mutex.
7107 */
7108int ocfs2_init_security_and_acl(struct inode *dir,
7109 struct inode *inode)
7110{
7111 int ret = 0;
7112 struct buffer_head *dir_bh = NULL;
7113 struct ocfs2_security_xattr_info si = {
7114 .enable = 1,
7115 };
7116
7117 ret = ocfs2_init_security_get(inode, dir, &si);
7118 if (!ret) {
Christoph Hellwig431547b2009-11-13 09:52:56 +00007119 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
7120 si.name, si.value, si.value_len,
7121 XATTR_CREATE);
Tao Ma0fe9b662009-08-18 11:47:56 +08007122 if (ret) {
7123 mlog_errno(ret);
7124 goto leave;
7125 }
7126 } else if (ret != -EOPNOTSUPP) {
7127 mlog_errno(ret);
7128 goto leave;
7129 }
7130
7131 ret = ocfs2_inode_lock(dir, &dir_bh, 0);
7132 if (ret) {
7133 mlog_errno(ret);
7134 goto leave;
7135 }
7136
7137 ret = ocfs2_init_acl(NULL, inode, dir, NULL, dir_bh, NULL, NULL);
7138 if (ret)
7139 mlog_errno(ret);
7140
7141 ocfs2_inode_unlock(dir, 0);
7142 brelse(dir_bh);
7143leave:
7144 return ret;
7145}
7146/*
Tiger Yang923f7f32008-11-14 11:16:27 +08007147 * 'security' attributes support
7148 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007149static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
Tiger Yang923f7f32008-11-14 11:16:27 +08007150 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007151 size_t name_len, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007152{
7153 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7154 const size_t total_len = prefix_len + name_len + 1;
7155
7156 if (list && total_len <= list_size) {
7157 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
7158 memcpy(list + prefix_len, name, name_len);
7159 list[prefix_len + name_len] = '\0';
7160 }
7161 return total_len;
7162}
7163
Christoph Hellwig431547b2009-11-13 09:52:56 +00007164static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7165 void *buffer, size_t size, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007166{
7167 if (strcmp(name, "") == 0)
7168 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007169 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7170 name, buffer, size);
Tiger Yang923f7f32008-11-14 11:16:27 +08007171}
7172
Christoph Hellwig431547b2009-11-13 09:52:56 +00007173static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7174 const void *value, size_t size, int flags, int type)
Tiger Yang923f7f32008-11-14 11:16:27 +08007175{
7176 if (strcmp(name, "") == 0)
7177 return -EINVAL;
7178
Christoph Hellwig431547b2009-11-13 09:52:56 +00007179 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7180 name, value, size, flags);
Tiger Yang923f7f32008-11-14 11:16:27 +08007181}
7182
Tiger Yang534eadd2008-11-14 11:16:41 +08007183int ocfs2_init_security_get(struct inode *inode,
7184 struct inode *dir,
7185 struct ocfs2_security_xattr_info *si)
7186{
Tiger Yang38d59ef2008-12-17 10:22:56 +08007187 /* check whether ocfs2 support feature xattr */
7188 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
7189 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08007190 return security_inode_init_security(inode, dir, &si->name, &si->value,
7191 &si->value_len);
7192}
7193
7194int ocfs2_init_security_set(handle_t *handle,
7195 struct inode *inode,
7196 struct buffer_head *di_bh,
7197 struct ocfs2_security_xattr_info *si,
7198 struct ocfs2_alloc_context *xattr_ac,
7199 struct ocfs2_alloc_context *data_ac)
7200{
7201 return ocfs2_xattr_set_handle(handle, inode, di_bh,
7202 OCFS2_XATTR_INDEX_SECURITY,
7203 si->name, si->value, si->value_len, 0,
7204 xattr_ac, data_ac);
7205}
7206
Tiger Yang923f7f32008-11-14 11:16:27 +08007207struct xattr_handler ocfs2_xattr_security_handler = {
7208 .prefix = XATTR_SECURITY_PREFIX,
7209 .list = ocfs2_xattr_security_list,
7210 .get = ocfs2_xattr_security_get,
7211 .set = ocfs2_xattr_security_set,
7212};
7213
7214/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07007215 * 'trusted' attributes support
7216 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007217static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007218 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007219 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007220{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007221 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007222 const size_t total_len = prefix_len + name_len + 1;
7223
7224 if (list && total_len <= list_size) {
7225 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
7226 memcpy(list + prefix_len, name, name_len);
7227 list[prefix_len + name_len] = '\0';
7228 }
7229 return total_len;
7230}
7231
Christoph Hellwig431547b2009-11-13 09:52:56 +00007232static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7233 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007234{
7235 if (strcmp(name, "") == 0)
7236 return -EINVAL;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007237 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7238 name, buffer, size);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007239}
7240
Christoph Hellwig431547b2009-11-13 09:52:56 +00007241static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7242 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007243{
7244 if (strcmp(name, "") == 0)
7245 return -EINVAL;
7246
Christoph Hellwig431547b2009-11-13 09:52:56 +00007247 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7248 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007249}
7250
7251struct xattr_handler ocfs2_xattr_trusted_handler = {
7252 .prefix = XATTR_TRUSTED_PREFIX,
7253 .list = ocfs2_xattr_trusted_list,
7254 .get = ocfs2_xattr_trusted_get,
7255 .set = ocfs2_xattr_trusted_set,
7256};
7257
Mark Fasheh99219ae2008-10-07 14:52:59 -07007258/*
7259 * 'user' attributes support
7260 */
Christoph Hellwig431547b2009-11-13 09:52:56 +00007261static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007262 size_t list_size, const char *name,
Christoph Hellwig431547b2009-11-13 09:52:56 +00007263 size_t name_len, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007264{
Tiger Yangceb1eba2008-10-23 16:34:13 +08007265 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07007266 const size_t total_len = prefix_len + name_len + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007267 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007268
7269 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7270 return 0;
7271
7272 if (list && total_len <= list_size) {
7273 memcpy(list, XATTR_USER_PREFIX, prefix_len);
7274 memcpy(list + prefix_len, name, name_len);
7275 list[prefix_len + name_len] = '\0';
7276 }
7277 return total_len;
7278}
7279
Christoph Hellwig431547b2009-11-13 09:52:56 +00007280static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7281 void *buffer, size_t size, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007282{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007283 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007284
7285 if (strcmp(name, "") == 0)
7286 return -EINVAL;
7287 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7288 return -EOPNOTSUPP;
Christoph Hellwig431547b2009-11-13 09:52:56 +00007289 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
Mark Fasheh99219ae2008-10-07 14:52:59 -07007290 buffer, size);
7291}
7292
Christoph Hellwig431547b2009-11-13 09:52:56 +00007293static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7294 const void *value, size_t size, int flags, int type)
Mark Fasheh99219ae2008-10-07 14:52:59 -07007295{
Christoph Hellwig431547b2009-11-13 09:52:56 +00007296 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007297
7298 if (strcmp(name, "") == 0)
7299 return -EINVAL;
7300 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7301 return -EOPNOTSUPP;
7302
Christoph Hellwig431547b2009-11-13 09:52:56 +00007303 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7304 name, value, size, flags);
Mark Fasheh99219ae2008-10-07 14:52:59 -07007305}
7306
7307struct xattr_handler ocfs2_xattr_user_handler = {
7308 .prefix = XATTR_USER_PREFIX,
7309 .list = ocfs2_xattr_user_list,
7310 .get = ocfs2_xattr_user_get,
7311 .set = ocfs2_xattr_user_set,
7312};