blob: a9339eb94a2e99bbcc14c76a8a6bf40a832ac61e [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"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080059
60struct ocfs2_xattr_def_value_root {
61 struct ocfs2_xattr_value_root xv;
62 struct ocfs2_extent_rec er;
63};
64
Tao Ma0c044f02008-08-18 17:38:50 +080065struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070066 /* The inode these xattrs are associated with */
67 struct inode *bu_inode;
68
69 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070070 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070071
72 /* How many blocks make up one bucket for this filesystem */
73 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080074};
75
Tao Ma78f30c32008-11-12 08:27:00 +080076struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080077 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080078 struct ocfs2_alloc_context *meta_ac;
79 struct ocfs2_alloc_context *data_ac;
80 struct ocfs2_cached_dealloc_ctxt dealloc;
81};
82
Tiger Yangcf1d6c72008-08-18 17:11:00 +080083#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
84#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang4442f512009-02-20 11:11:50 +080085#define OCFS2_XATTR_HEADER_GAP 4
Tiger Yang534eadd2008-11-14 11:16:41 +080086#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
87 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080088 - OCFS2_XATTR_HEADER_GAP)
Tiger Yang89c38bd2008-11-14 11:17:41 +080089#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
90 - sizeof(struct ocfs2_xattr_block) \
91 - sizeof(struct ocfs2_xattr_header) \
Tiger Yang4442f512009-02-20 11:11:50 +080092 - OCFS2_XATTR_HEADER_GAP)
Tiger Yangcf1d6c72008-08-18 17:11:00 +080093
94static struct ocfs2_xattr_def_value_root def_xv = {
95 .xv.xr_list.l_count = cpu_to_le16(1),
96};
97
98struct xattr_handler *ocfs2_xattr_handlers[] = {
99 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800100#ifdef CONFIG_OCFS2_FS_POSIX_ACL
101 &ocfs2_xattr_acl_access_handler,
102 &ocfs2_xattr_acl_default_handler,
103#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800104 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800105 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800106 NULL
107};
108
Tiger Yangc988fd02008-10-23 16:34:44 +0800109static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800110 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800111#ifdef CONFIG_OCFS2_FS_POSIX_ACL
112 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
113 = &ocfs2_xattr_acl_access_handler,
114 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
115 = &ocfs2_xattr_acl_default_handler,
116#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800117 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800118 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800119};
120
121struct ocfs2_xattr_info {
122 int name_index;
123 const char *name;
124 const void *value;
125 size_t value_len;
126};
127
128struct ocfs2_xattr_search {
129 struct buffer_head *inode_bh;
130 /*
131 * xattr_bh point to the block buffer head which has extended attribute
132 * when extended attribute in inode, xattr_bh is equal to inode_bh.
133 */
134 struct buffer_head *xattr_bh;
135 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700136 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800137 void *base;
138 void *end;
139 struct ocfs2_xattr_entry *here;
140 int not_found;
141};
142
Tao Mafd68a892009-08-18 11:43:21 +0800143static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma589dc262008-08-18 17:38:51 +0800144 struct ocfs2_xattr_header *xh,
145 int index,
146 int *block_off,
147 int *new_offset);
148
Joel Becker54f443f2008-10-20 18:43:07 -0700149static int ocfs2_xattr_block_find(struct inode *inode,
150 int name_index,
151 const char *name,
152 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800153static int ocfs2_xattr_index_block_find(struct inode *inode,
154 struct buffer_head *root_bh,
155 int name_index,
156 const char *name,
157 struct ocfs2_xattr_search *xs);
158
Tao Ma0c044f02008-08-18 17:38:50 +0800159static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
160 struct ocfs2_xattr_tree_root *xt,
161 char *buffer,
162 size_t buffer_size);
163
Tao Ma01225592008-08-18 17:38:53 +0800164static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800165 struct ocfs2_xattr_search *xs,
166 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800167
168static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
169 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800170 struct ocfs2_xattr_search *xs,
171 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800172
Tao Maa3944252008-08-18 17:38:54 +0800173static int ocfs2_delete_xattr_index_block(struct inode *inode,
174 struct buffer_head *xb_bh);
Joel Beckerc58b6032008-11-26 13:36:24 -0800175static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
176 u64 src_blk, u64 last_blk, u64 to_blk,
177 unsigned int start_bucket,
178 u32 *first_hash);
Tao Ma492a8a32009-08-18 11:43:17 +0800179static int ocfs2_prepare_refcount_xattr(struct inode *inode,
180 struct ocfs2_dinode *di,
181 struct ocfs2_xattr_info *xi,
182 struct ocfs2_xattr_search *xis,
183 struct ocfs2_xattr_search *xbs,
184 struct ocfs2_refcount_tree **ref_tree,
185 int *meta_need,
186 int *credits);
Tao Maa3944252008-08-18 17:38:54 +0800187
Tiger Yang0030e002008-10-23 16:33:33 +0800188static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
189{
190 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
191}
192
193static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
194{
195 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
196}
197
198static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
199{
200 u16 len = sb->s_blocksize -
201 offsetof(struct ocfs2_xattr_header, xh_entries);
202
203 return len / sizeof(struct ocfs2_xattr_entry);
204}
205
Joel Becker9c7759a2008-10-24 16:21:03 -0700206#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700207#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700208#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700209
Joel Beckerba937122008-10-24 19:13:20 -0700210static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700211{
Joel Beckerba937122008-10-24 19:13:20 -0700212 struct ocfs2_xattr_bucket *bucket;
213 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700214
Joel Beckerba937122008-10-24 19:13:20 -0700215 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
216
217 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
218 if (bucket) {
219 bucket->bu_inode = inode;
220 bucket->bu_blocks = blks;
221 }
222
223 return bucket;
224}
225
226static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
227{
228 int i;
229
230 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700231 brelse(bucket->bu_bhs[i]);
232 bucket->bu_bhs[i] = NULL;
233 }
234}
235
Joel Beckerba937122008-10-24 19:13:20 -0700236static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
237{
238 if (bucket) {
239 ocfs2_xattr_bucket_relse(bucket);
240 bucket->bu_inode = NULL;
241 kfree(bucket);
242 }
243}
244
Joel Becker784b8162008-10-24 17:33:40 -0700245/*
246 * A bucket that has never been written to disk doesn't need to be
247 * read. We just need the buffer_heads. Don't call this for
248 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
249 * them fully.
250 */
Joel Beckerba937122008-10-24 19:13:20 -0700251static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700252 u64 xb_blkno)
253{
254 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700255
Joel Beckerba937122008-10-24 19:13:20 -0700256 for (i = 0; i < bucket->bu_blocks; i++) {
257 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
258 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700259 if (!bucket->bu_bhs[i]) {
260 rc = -EIO;
261 mlog_errno(rc);
262 break;
263 }
264
Joel Becker8cb471e2009-02-10 20:00:41 -0800265 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800266 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800267 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800268 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700269 }
270
271 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700272 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700273 return rc;
274}
275
276/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700277static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700278 u64 xb_blkno)
279{
Joel Beckerba937122008-10-24 19:13:20 -0700280 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700281
Joel Becker8cb471e2009-02-10 20:00:41 -0800282 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800283 bucket->bu_blocks, bucket->bu_bhs, 0,
284 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800285 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800286 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800287 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
288 bucket->bu_bhs,
289 bucket->bu_blocks,
290 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800291 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800292 if (rc)
293 mlog_errno(rc);
294 }
295
Joel Becker784b8162008-10-24 17:33:40 -0700296 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700297 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700298 return rc;
299}
300
Joel Becker1224be02008-10-24 18:47:33 -0700301static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700302 struct ocfs2_xattr_bucket *bucket,
303 int type)
304{
305 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700306
Joel Beckerba937122008-10-24 19:13:20 -0700307 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800308 rc = ocfs2_journal_access(handle,
309 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700310 bucket->bu_bhs[i], type);
311 if (rc) {
312 mlog_errno(rc);
313 break;
314 }
315 }
316
317 return rc;
318}
319
320static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700321 struct ocfs2_xattr_bucket *bucket)
322{
Joel Beckerba937122008-10-24 19:13:20 -0700323 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700324
Tao Mac8b9cf92009-02-24 17:40:26 -0800325 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800326 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
327 bucket->bu_bhs, bucket->bu_blocks,
328 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800329 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800330
Joel Beckerba937122008-10-24 19:13:20 -0700331 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700332 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
333}
334
Joel Beckerba937122008-10-24 19:13:20 -0700335static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700336 struct ocfs2_xattr_bucket *src)
337{
338 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700339 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700340
Joel Beckerba937122008-10-24 19:13:20 -0700341 BUG_ON(dest->bu_blocks != src->bu_blocks);
342 BUG_ON(dest->bu_inode != src->bu_inode);
343
344 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700345 memcpy(bucket_block(dest, i), bucket_block(src, i),
346 blocksize);
347 }
348}
Joel Becker1224be02008-10-24 18:47:33 -0700349
Joel Becker4ae1d692008-11-13 14:49:18 -0800350static int ocfs2_validate_xattr_block(struct super_block *sb,
351 struct buffer_head *bh)
352{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700353 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800354 struct ocfs2_xattr_block *xb =
355 (struct ocfs2_xattr_block *)bh->b_data;
356
357 mlog(0, "Validating xattr block %llu\n",
358 (unsigned long long)bh->b_blocknr);
359
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700360 BUG_ON(!buffer_uptodate(bh));
361
362 /*
363 * If the ecc fails, we return the error but otherwise
364 * leave the filesystem running. We know any error is
365 * local to this block.
366 */
367 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
368 if (rc)
369 return rc;
370
371 /*
372 * Errors after here are fatal
373 */
374
Joel Becker4ae1d692008-11-13 14:49:18 -0800375 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
376 ocfs2_error(sb,
377 "Extended attribute block #%llu has bad "
378 "signature %.*s",
379 (unsigned long long)bh->b_blocknr, 7,
380 xb->xb_signature);
381 return -EINVAL;
382 }
383
384 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
385 ocfs2_error(sb,
386 "Extended attribute block #%llu has an "
387 "invalid xb_blkno of %llu",
388 (unsigned long long)bh->b_blocknr,
389 (unsigned long long)le64_to_cpu(xb->xb_blkno));
390 return -EINVAL;
391 }
392
393 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
394 ocfs2_error(sb,
395 "Extended attribute block #%llu has an invalid "
396 "xb_fs_generation of #%u",
397 (unsigned long long)bh->b_blocknr,
398 le32_to_cpu(xb->xb_fs_generation));
399 return -EINVAL;
400 }
401
402 return 0;
403}
404
405static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
406 struct buffer_head **bh)
407{
408 int rc;
409 struct buffer_head *tmp = *bh;
410
Joel Becker8cb471e2009-02-10 20:00:41 -0800411 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800412 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800413
414 /* If ocfs2_read_block() got us a new bh, pass it up. */
415 if (!rc && !*bh)
416 *bh = tmp;
417
418 return rc;
419}
420
Tao Ma936b8832008-10-09 23:06:14 +0800421static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800422{
423 struct xattr_handler *handler = NULL;
424
425 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
426 handler = ocfs2_xattr_handler_map[name_index];
427
Tao Ma936b8832008-10-09 23:06:14 +0800428 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800429}
430
Mark Fasheh40daa162008-10-07 14:31:42 -0700431static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800432 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700433 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800434{
435 /* Get hash value of uuid from super block */
436 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
437 int i;
438
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800439 /* hash extended attribute name */
440 for (i = 0; i < name_len; i++) {
441 hash = (hash << OCFS2_HASH_SHIFT) ^
442 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
443 *name++;
444 }
445
446 return hash;
447}
448
449/*
450 * ocfs2_xattr_hash_entry()
451 *
452 * Compute the hash of an extended attribute.
453 */
454static void ocfs2_xattr_hash_entry(struct inode *inode,
455 struct ocfs2_xattr_header *header,
456 struct ocfs2_xattr_entry *entry)
457{
458 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800459 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800460
Tao Ma2057e5c2008-10-09 23:06:13 +0800461 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800462 entry->xe_name_hash = cpu_to_le32(hash);
463
464 return;
465}
Tao Maf56654c2008-08-18 17:38:48 +0800466
Tiger Yang534eadd2008-11-14 11:16:41 +0800467static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
468{
469 int size = 0;
470
471 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
472 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
473 else
474 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
475 size += sizeof(struct ocfs2_xattr_entry);
476
477 return size;
478}
479
480int ocfs2_calc_security_init(struct inode *dir,
481 struct ocfs2_security_xattr_info *si,
482 int *want_clusters,
483 int *xattr_credits,
484 struct ocfs2_alloc_context **xattr_ac)
485{
486 int ret = 0;
487 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
488 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
489 si->value_len);
490
491 /*
492 * The max space of security xattr taken inline is
493 * 256(name) + 80(value) + 16(entry) = 352 bytes,
494 * So reserve one metadata block for it is ok.
495 */
496 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
497 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
498 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
499 if (ret) {
500 mlog_errno(ret);
501 return ret;
502 }
503 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
504 }
505
506 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800507 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
508 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
509 si->value_len);
510
511 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
512 new_clusters);
513 *want_clusters += new_clusters;
514 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800515 return ret;
516}
517
Tiger Yang89c38bd2008-11-14 11:17:41 +0800518int ocfs2_calc_xattr_init(struct inode *dir,
519 struct buffer_head *dir_bh,
520 int mode,
521 struct ocfs2_security_xattr_info *si,
522 int *want_clusters,
523 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800524 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800525{
526 int ret = 0;
527 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800528 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800529
530 if (si->enable)
531 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
532 si->value_len);
533
534 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
535 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
536 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
537 "", NULL, 0);
538 if (acl_len > 0) {
539 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
540 if (S_ISDIR(mode))
541 a_size <<= 1;
542 } else if (acl_len != 0 && acl_len != -ENODATA) {
543 mlog_errno(ret);
544 return ret;
545 }
546 }
547
548 if (!(s_size + a_size))
549 return ret;
550
551 /*
552 * The max space of security xattr taken inline is
553 * 256(name) + 80(value) + 16(entry) = 352 bytes,
554 * The max space of acl xattr taken inline is
555 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
556 * when blocksize = 512, may reserve one more cluser for
557 * xattr bucket, otherwise reserve one metadata block
558 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800559 * If this is a new directory with inline data,
560 * we choose to reserve the entire inline area for
561 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800562 */
563 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800564 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800565 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800566 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800567 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
568 }
569
570 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
571 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
572 *want_clusters += 1;
573 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
574 }
575
Tiger Yang0e445b62008-12-09 16:42:51 +0800576 /*
577 * reserve credits and clusters for xattrs which has large value
578 * and have to be set outside
579 */
580 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
581 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
582 si->value_len);
583 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
584 new_clusters);
585 *want_clusters += new_clusters;
586 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800587 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
588 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800589 /* for directory, it has DEFAULT and ACCESS two types of acls */
590 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
591 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
592 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
593 new_clusters);
594 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800595 }
596
597 return ret;
598}
599
Tao Maf56654c2008-08-18 17:38:48 +0800600static int ocfs2_xattr_extend_allocation(struct inode *inode,
601 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800602 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800603 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800604{
605 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800606 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800607 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800608 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700609 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800610
611 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
612
Joel Becker5e404e92009-02-13 03:54:22 -0800613 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700614
Joel Becker0cf2f762009-02-12 16:41:25 -0800615 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800616 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800617 if (status < 0) {
618 mlog_errno(status);
619 goto leave;
620 }
621
Joel Becker19b801f2008-12-09 14:36:50 -0800622 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800623 status = ocfs2_add_clusters_in_btree(handle,
624 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800625 &logical_start,
626 clusters_to_add,
627 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800628 ctxt->data_ac,
629 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700630 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800631 if (status < 0) {
632 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800633 goto leave;
634 }
635
Joel Becker19b801f2008-12-09 14:36:50 -0800636 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800637 if (status < 0) {
638 mlog_errno(status);
639 goto leave;
640 }
641
Joel Becker19b801f2008-12-09 14:36:50 -0800642 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800643
Tao Ma85db90e2008-11-12 08:27:01 +0800644 /*
645 * We should have already allocated enough space before the transaction,
646 * so no need to restart.
647 */
648 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800649
650leave:
Tao Maf56654c2008-08-18 17:38:48 +0800651
652 return status;
653}
654
655static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800656 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800657 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma492a8a32009-08-18 11:43:17 +0800658 unsigned int ext_flags,
Tao Ma78f30c32008-11-12 08:27:00 +0800659 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800660{
661 int ret;
662 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800663 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700664 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800665
Joel Becker5e404e92009-02-13 03:54:22 -0800666 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700667
Joel Becker0cf2f762009-02-12 16:41:25 -0800668 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800669 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800670 if (ret) {
671 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800672 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800673 }
674
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800675 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800676 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800677 if (ret) {
678 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800679 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800680 }
681
Joel Beckerd72cc722008-12-09 14:30:41 -0800682 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800683
Joel Beckerd72cc722008-12-09 14:30:41 -0800684 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800685 if (ret) {
686 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800687 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800688 }
689
Tao Ma492a8a32009-08-18 11:43:17 +0800690 if (ext_flags & OCFS2_EXT_REFCOUNTED)
691 ret = ocfs2_decrease_refcount(inode, handle,
692 ocfs2_blocks_to_clusters(inode->i_sb,
693 phys_blkno),
694 len, ctxt->meta_ac, &ctxt->dealloc, 1);
695 else
696 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc,
697 phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800698 if (ret)
699 mlog_errno(ret);
700
Tao Maf56654c2008-08-18 17:38:48 +0800701out:
Tao Maf56654c2008-08-18 17:38:48 +0800702 return ret;
703}
704
705static int ocfs2_xattr_shrink_size(struct inode *inode,
706 u32 old_clusters,
707 u32 new_clusters,
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 ret = 0;
Tao Ma492a8a32009-08-18 11:43:17 +0800712 unsigned int ext_flags;
Tao Maf56654c2008-08-18 17:38:48 +0800713 u32 trunc_len, cpos, phys_cpos, alloc_size;
714 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800715
716 if (old_clusters <= new_clusters)
717 return 0;
718
719 cpos = new_clusters;
720 trunc_len = old_clusters - new_clusters;
721 while (trunc_len) {
722 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800723 &alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800724 &vb->vb_xv->xr_list, &ext_flags);
Tao Maf56654c2008-08-18 17:38:48 +0800725 if (ret) {
726 mlog_errno(ret);
727 goto out;
728 }
729
730 if (alloc_size > trunc_len)
731 alloc_size = trunc_len;
732
Joel Becker19b801f2008-12-09 14:36:50 -0800733 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800734 phys_cpos, alloc_size,
Tao Ma492a8a32009-08-18 11:43:17 +0800735 ext_flags, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800736 if (ret) {
737 mlog_errno(ret);
738 goto out;
739 }
740
741 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800742 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
743 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800744 cpos += alloc_size;
745 trunc_len -= alloc_size;
746 }
747
748out:
Tao Maf56654c2008-08-18 17:38:48 +0800749 return ret;
750}
751
752static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800753 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800754 int len,
755 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800756{
757 int ret;
758 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800759 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800760
761 if (new_clusters == old_clusters)
762 return 0;
763
764 if (new_clusters > old_clusters)
765 ret = ocfs2_xattr_extend_allocation(inode,
766 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800767 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800768 else
769 ret = ocfs2_xattr_shrink_size(inode,
770 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800771 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800772
773 return ret;
774}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800775
Tao Ma936b8832008-10-09 23:06:14 +0800776static int ocfs2_xattr_list_entry(char *buffer, size_t size,
777 size_t *result, const char *prefix,
778 const char *name, int name_len)
779{
780 char *p = buffer + *result;
781 int prefix_len = strlen(prefix);
782 int total_len = prefix_len + name_len + 1;
783
784 *result += total_len;
785
786 /* we are just looking for how big our buffer needs to be */
787 if (!size)
788 return 0;
789
790 if (*result > size)
791 return -ERANGE;
792
793 memcpy(p, prefix, prefix_len);
794 memcpy(p + prefix_len, name, name_len);
795 p[prefix_len + name_len] = '\0';
796
797 return 0;
798}
799
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800800static int ocfs2_xattr_list_entries(struct inode *inode,
801 struct ocfs2_xattr_header *header,
802 char *buffer, size_t buffer_size)
803{
Tao Ma936b8832008-10-09 23:06:14 +0800804 size_t result = 0;
805 int i, type, ret;
806 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800807
808 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
809 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800810 type = ocfs2_xattr_get_type(entry);
811 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800812
Tao Ma936b8832008-10-09 23:06:14 +0800813 if (prefix) {
814 name = (const char *)header +
815 le16_to_cpu(entry->xe_name_offset);
816
817 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
818 &result, prefix, name,
819 entry->xe_name_len);
820 if (ret)
821 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800822 }
823 }
824
Tao Ma936b8832008-10-09 23:06:14 +0800825 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800826}
827
828static int ocfs2_xattr_ibody_list(struct inode *inode,
829 struct ocfs2_dinode *di,
830 char *buffer,
831 size_t buffer_size)
832{
833 struct ocfs2_xattr_header *header = NULL;
834 struct ocfs2_inode_info *oi = OCFS2_I(inode);
835 int ret = 0;
836
837 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
838 return ret;
839
840 header = (struct ocfs2_xattr_header *)
841 ((void *)di + inode->i_sb->s_blocksize -
842 le16_to_cpu(di->i_xattr_inline_size));
843
844 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
845
846 return ret;
847}
848
849static int ocfs2_xattr_block_list(struct inode *inode,
850 struct ocfs2_dinode *di,
851 char *buffer,
852 size_t buffer_size)
853{
854 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800855 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800856 int ret = 0;
857
858 if (!di->i_xattr_loc)
859 return ret;
860
Joel Becker4ae1d692008-11-13 14:49:18 -0800861 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
862 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800863 if (ret < 0) {
864 mlog_errno(ret);
865 return ret;
866 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800867
Tao Ma0c044f02008-08-18 17:38:50 +0800868 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800869 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
870 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
871 ret = ocfs2_xattr_list_entries(inode, header,
872 buffer, buffer_size);
873 } else {
874 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
875 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
876 buffer, buffer_size);
877 }
Joel Becker4ae1d692008-11-13 14:49:18 -0800878
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800879 brelse(blk_bh);
880
881 return ret;
882}
883
884ssize_t ocfs2_listxattr(struct dentry *dentry,
885 char *buffer,
886 size_t size)
887{
888 int ret = 0, i_ret = 0, b_ret = 0;
889 struct buffer_head *di_bh = NULL;
890 struct ocfs2_dinode *di = NULL;
891 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
892
Tiger Yang8154da32008-08-18 17:11:46 +0800893 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
894 return -EOPNOTSUPP;
895
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800896 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
897 return ret;
898
899 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
900 if (ret < 0) {
901 mlog_errno(ret);
902 return ret;
903 }
904
905 di = (struct ocfs2_dinode *)di_bh->b_data;
906
907 down_read(&oi->ip_xattr_sem);
908 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
909 if (i_ret < 0)
910 b_ret = 0;
911 else {
912 if (buffer) {
913 buffer += i_ret;
914 size -= i_ret;
915 }
916 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
917 buffer, size);
918 if (b_ret < 0)
919 i_ret = 0;
920 }
921 up_read(&oi->ip_xattr_sem);
922 ocfs2_inode_unlock(dentry->d_inode, 0);
923
924 brelse(di_bh);
925
926 return i_ret + b_ret;
927}
928
929static int ocfs2_xattr_find_entry(int name_index,
930 const char *name,
931 struct ocfs2_xattr_search *xs)
932{
933 struct ocfs2_xattr_entry *entry;
934 size_t name_len;
935 int i, cmp = 1;
936
937 if (name == NULL)
938 return -EINVAL;
939
940 name_len = strlen(name);
941 entry = xs->here;
942 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
943 cmp = name_index - ocfs2_xattr_get_type(entry);
944 if (!cmp)
945 cmp = name_len - entry->xe_name_len;
946 if (!cmp)
947 cmp = memcmp(name, (xs->base +
948 le16_to_cpu(entry->xe_name_offset)),
949 name_len);
950 if (cmp == 0)
951 break;
952 entry += 1;
953 }
954 xs->here = entry;
955
956 return cmp ? -ENODATA : 0;
957}
958
959static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800960 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800961 void *buffer,
962 size_t len)
963{
964 u32 cpos, p_cluster, num_clusters, bpc, clusters;
965 u64 blkno;
966 int i, ret = 0;
967 size_t cplen, blocksize;
968 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800969 struct ocfs2_extent_list *el;
970
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800971 el = &xv->xr_list;
972 clusters = le32_to_cpu(xv->xr_clusters);
973 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
974 blocksize = inode->i_sb->s_blocksize;
975
976 cpos = 0;
977 while (cpos < clusters) {
978 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +0800979 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800980 if (ret) {
981 mlog_errno(ret);
982 goto out;
983 }
984
985 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
986 /* Copy ocfs2_xattr_value */
987 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -0800988 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
989 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800990 if (ret) {
991 mlog_errno(ret);
992 goto out;
993 }
994
995 cplen = len >= blocksize ? blocksize : len;
996 memcpy(buffer, bh->b_data, cplen);
997 len -= cplen;
998 buffer += cplen;
999
1000 brelse(bh);
1001 bh = NULL;
1002 if (len == 0)
1003 break;
1004 }
1005 cpos += num_clusters;
1006 }
1007out:
1008 return ret;
1009}
1010
1011static int ocfs2_xattr_ibody_get(struct inode *inode,
1012 int name_index,
1013 const char *name,
1014 void *buffer,
1015 size_t buffer_size,
1016 struct ocfs2_xattr_search *xs)
1017{
1018 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1019 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001020 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001021 size_t size;
1022 int ret = 0;
1023
1024 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1025 return -ENODATA;
1026
1027 xs->end = (void *)di + inode->i_sb->s_blocksize;
1028 xs->header = (struct ocfs2_xattr_header *)
1029 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1030 xs->base = (void *)xs->header;
1031 xs->here = xs->header->xh_entries;
1032
1033 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1034 if (ret)
1035 return ret;
1036 size = le64_to_cpu(xs->here->xe_value_size);
1037 if (buffer) {
1038 if (size > buffer_size)
1039 return -ERANGE;
1040 if (ocfs2_xattr_is_local(xs->here)) {
1041 memcpy(buffer, (void *)xs->base +
1042 le16_to_cpu(xs->here->xe_name_offset) +
1043 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1044 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001045 xv = (struct ocfs2_xattr_value_root *)
1046 (xs->base + le16_to_cpu(
1047 xs->here->xe_name_offset) +
1048 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1049 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001050 buffer, size);
1051 if (ret < 0) {
1052 mlog_errno(ret);
1053 return ret;
1054 }
1055 }
1056 }
1057
1058 return size;
1059}
1060
1061static int ocfs2_xattr_block_get(struct inode *inode,
1062 int name_index,
1063 const char *name,
1064 void *buffer,
1065 size_t buffer_size,
1066 struct ocfs2_xattr_search *xs)
1067{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001068 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001069 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001070 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301071 int ret = -ENODATA, name_offset, name_len, i;
1072 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001073
Joel Beckerba937122008-10-24 19:13:20 -07001074 xs->bucket = ocfs2_xattr_bucket_new(inode);
1075 if (!xs->bucket) {
1076 ret = -ENOMEM;
1077 mlog_errno(ret);
1078 goto cleanup;
1079 }
Tao Ma589dc262008-08-18 17:38:51 +08001080
Joel Becker54f443f2008-10-20 18:43:07 -07001081 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1082 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001083 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001084 goto cleanup;
1085 }
1086
Tiger Yang6c1e1832008-11-02 19:04:21 +08001087 if (xs->not_found) {
1088 ret = -ENODATA;
1089 goto cleanup;
1090 }
1091
Joel Becker54f443f2008-10-20 18:43:07 -07001092 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001093 size = le64_to_cpu(xs->here->xe_value_size);
1094 if (buffer) {
1095 ret = -ERANGE;
1096 if (size > buffer_size)
1097 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001098
1099 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1100 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1101 i = xs->here - xs->header->xh_entries;
1102
1103 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08001104 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Beckerba937122008-10-24 19:13:20 -07001105 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001106 i,
1107 &block_off,
1108 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001109 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001110 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001111 if (ocfs2_xattr_is_local(xs->here)) {
1112 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001113 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001114 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001115 xv = (struct ocfs2_xattr_value_root *)
1116 (xs->base + name_offset + name_len);
1117 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001118 buffer, size);
1119 if (ret < 0) {
1120 mlog_errno(ret);
1121 goto cleanup;
1122 }
1123 }
1124 }
1125 ret = size;
1126cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001127 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001128
Joel Becker54f443f2008-10-20 18:43:07 -07001129 brelse(xs->xattr_bh);
1130 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001131 return ret;
1132}
1133
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001134int ocfs2_xattr_get_nolock(struct inode *inode,
1135 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001136 int name_index,
1137 const char *name,
1138 void *buffer,
1139 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001140{
1141 int ret;
1142 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001143 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1144 struct ocfs2_xattr_search xis = {
1145 .not_found = -ENODATA,
1146 };
1147 struct ocfs2_xattr_search xbs = {
1148 .not_found = -ENODATA,
1149 };
1150
Tiger Yang8154da32008-08-18 17:11:46 +08001151 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1152 return -EOPNOTSUPP;
1153
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001154 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1155 ret = -ENODATA;
1156
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001157 xis.inode_bh = xbs.inode_bh = di_bh;
1158 di = (struct ocfs2_dinode *)di_bh->b_data;
1159
1160 down_read(&oi->ip_xattr_sem);
1161 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1162 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001163 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001164 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1165 buffer_size, &xbs);
1166 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001167
1168 return ret;
1169}
1170
1171/* ocfs2_xattr_get()
1172 *
1173 * Copy an extended attribute into the buffer provided.
1174 * Buffer is NULL to compute the size of buffer required.
1175 */
1176static int ocfs2_xattr_get(struct inode *inode,
1177 int name_index,
1178 const char *name,
1179 void *buffer,
1180 size_t buffer_size)
1181{
1182 int ret;
1183 struct buffer_head *di_bh = NULL;
1184
1185 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1186 if (ret < 0) {
1187 mlog_errno(ret);
1188 return ret;
1189 }
1190 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1191 name, buffer, buffer_size);
1192
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001193 ocfs2_inode_unlock(inode, 0);
1194
1195 brelse(di_bh);
1196
1197 return ret;
1198}
1199
1200static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001201 handle_t *handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001202 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001203 const void *value,
1204 int value_len)
1205{
Tao Ma71d548a2008-12-05 06:20:54 +08001206 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001207 u16 blocksize = inode->i_sb->s_blocksize;
1208 u32 p_cluster, num_clusters;
1209 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1210 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1211 u64 blkno;
1212 struct buffer_head *bh = NULL;
Tao Ma492a8a32009-08-18 11:43:17 +08001213 unsigned int ext_flags;
1214 struct ocfs2_xattr_value_root *xv = vb->vb_xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001215
1216 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1217
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001218 while (cpos < clusters) {
1219 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001220 &num_clusters, &xv->xr_list,
Tao Ma492a8a32009-08-18 11:43:17 +08001221 &ext_flags);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001222 if (ret) {
1223 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001224 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001225 }
1226
Tao Ma492a8a32009-08-18 11:43:17 +08001227 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1228
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001229 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1230
1231 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001232 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1233 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001234 if (ret) {
1235 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001236 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001237 }
1238
1239 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001240 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001241 bh,
1242 OCFS2_JOURNAL_ACCESS_WRITE);
1243 if (ret < 0) {
1244 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001245 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001246 }
1247
1248 cp_len = value_len > blocksize ? blocksize : value_len;
1249 memcpy(bh->b_data, value, cp_len);
1250 value_len -= cp_len;
1251 value += cp_len;
1252 if (cp_len < blocksize)
1253 memset(bh->b_data + cp_len, 0,
1254 blocksize - cp_len);
1255
1256 ret = ocfs2_journal_dirty(handle, bh);
1257 if (ret < 0) {
1258 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001259 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260 }
1261 brelse(bh);
1262 bh = NULL;
1263
1264 /*
1265 * XXX: do we need to empty all the following
1266 * blocks in this cluster?
1267 */
1268 if (!value_len)
1269 break;
1270 }
1271 cpos += num_clusters;
1272 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001273out:
1274 brelse(bh);
1275
1276 return ret;
1277}
1278
1279static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001280 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001281 struct ocfs2_xattr_info *xi,
1282 struct ocfs2_xattr_search *xs,
Joel Becker512620f2008-12-09 15:58:35 -08001283 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001284 size_t offs)
1285{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001286 int ret = 0;
1287 size_t name_len = strlen(xi->name);
1288 void *val = xs->base + offs;
1289 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1290
Joel Becker0cf2f762009-02-12 16:41:25 -08001291 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001292 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001293 if (ret) {
1294 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001295 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001296 }
1297 /* Decrease xattr count */
1298 le16_add_cpu(&xs->header->xh_count, -1);
1299 /* Remove the xattr entry and tree root which has already be set*/
1300 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1301 memset(val, 0, size);
1302
Joel Becker512620f2008-12-09 15:58:35 -08001303 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001304 if (ret < 0)
1305 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001306out:
1307 return ret;
1308}
1309
1310static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001311 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001312 struct ocfs2_xattr_info *xi,
1313 struct ocfs2_xattr_search *xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001314 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001315 size_t offs)
1316{
Tao Ma85db90e2008-11-12 08:27:01 +08001317 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001318
Joel Becker0cf2f762009-02-12 16:41:25 -08001319 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker0c748e92008-12-09 15:46:15 -08001320 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001321 if (ret) {
1322 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001323 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001324 }
1325
1326 xs->here->xe_name_offset = cpu_to_le16(offs);
1327 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1328 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1329 ocfs2_xattr_set_local(xs->here, 1);
1330 else
1331 ocfs2_xattr_set_local(xs->here, 0);
1332 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1333
Joel Becker0c748e92008-12-09 15:46:15 -08001334 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001335 if (ret < 0)
1336 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001337out:
1338 return ret;
1339}
1340
1341/*
1342 * ocfs2_xattr_set_value_outside()
1343 *
1344 * Set large size value in B tree.
1345 */
1346static int ocfs2_xattr_set_value_outside(struct inode *inode,
1347 struct ocfs2_xattr_info *xi,
1348 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001349 struct ocfs2_xattr_set_ctxt *ctxt,
Joel Becker512620f2008-12-09 15:58:35 -08001350 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001351 size_t offs)
1352{
1353 size_t name_len = strlen(xi->name);
1354 void *val = xs->base + offs;
1355 struct ocfs2_xattr_value_root *xv = NULL;
1356 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1357 int ret = 0;
1358
1359 memset(val, 0, size);
1360 memcpy(val, xi->name, name_len);
1361 xv = (struct ocfs2_xattr_value_root *)
1362 (val + OCFS2_XATTR_SIZE(name_len));
1363 xv->xr_clusters = 0;
1364 xv->xr_last_eb_blk = 0;
1365 xv->xr_list.l_tree_depth = 0;
1366 xv->xr_list.l_count = cpu_to_le16(1);
1367 xv->xr_list.l_next_free_rec = 0;
Joel Becker512620f2008-12-09 15:58:35 -08001368 vb->vb_xv = xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001369
Joel Becker512620f2008-12-09 15:58:35 -08001370 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001371 if (ret < 0) {
1372 mlog_errno(ret);
1373 return ret;
1374 }
Joel Becker512620f2008-12-09 15:58:35 -08001375 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001376 if (ret < 0) {
1377 mlog_errno(ret);
1378 return ret;
1379 }
Tao Ma492a8a32009-08-18 11:43:17 +08001380 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001381 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001382 if (ret < 0)
1383 mlog_errno(ret);
1384
1385 return ret;
1386}
1387
1388/*
1389 * ocfs2_xattr_set_entry_local()
1390 *
1391 * Set, replace or remove extended attribute in local.
1392 */
1393static void ocfs2_xattr_set_entry_local(struct inode *inode,
1394 struct ocfs2_xattr_info *xi,
1395 struct ocfs2_xattr_search *xs,
1396 struct ocfs2_xattr_entry *last,
1397 size_t min_offs)
1398{
1399 size_t name_len = strlen(xi->name);
1400 int i;
1401
1402 if (xi->value && xs->not_found) {
1403 /* Insert the new xattr entry. */
1404 le16_add_cpu(&xs->header->xh_count, 1);
1405 ocfs2_xattr_set_type(last, xi->name_index);
1406 ocfs2_xattr_set_local(last, 1);
1407 last->xe_name_len = name_len;
1408 } else {
1409 void *first_val;
1410 void *val;
1411 size_t offs, size;
1412
1413 first_val = xs->base + min_offs;
1414 offs = le16_to_cpu(xs->here->xe_name_offset);
1415 val = xs->base + offs;
1416
1417 if (le64_to_cpu(xs->here->xe_value_size) >
1418 OCFS2_XATTR_INLINE_SIZE)
1419 size = OCFS2_XATTR_SIZE(name_len) +
1420 OCFS2_XATTR_ROOT_SIZE;
1421 else
1422 size = OCFS2_XATTR_SIZE(name_len) +
1423 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1424
1425 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1426 OCFS2_XATTR_SIZE(xi->value_len)) {
1427 /* The old and the new value have the
1428 same size. Just replace the value. */
1429 ocfs2_xattr_set_local(xs->here, 1);
1430 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1431 /* Clear value bytes. */
1432 memset(val + OCFS2_XATTR_SIZE(name_len),
1433 0,
1434 OCFS2_XATTR_SIZE(xi->value_len));
1435 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1436 xi->value,
1437 xi->value_len);
1438 return;
1439 }
1440 /* Remove the old name+value. */
1441 memmove(first_val + size, first_val, val - first_val);
1442 memset(first_val, 0, size);
1443 xs->here->xe_name_hash = 0;
1444 xs->here->xe_name_offset = 0;
1445 ocfs2_xattr_set_local(xs->here, 1);
1446 xs->here->xe_value_size = 0;
1447
1448 min_offs += size;
1449
1450 /* Adjust all value offsets. */
1451 last = xs->header->xh_entries;
1452 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1453 size_t o = le16_to_cpu(last->xe_name_offset);
1454
1455 if (o < offs)
1456 last->xe_name_offset = cpu_to_le16(o + size);
1457 last += 1;
1458 }
1459
1460 if (!xi->value) {
1461 /* Remove the old entry. */
1462 last -= 1;
1463 memmove(xs->here, xs->here + 1,
1464 (void *)last - (void *)xs->here);
1465 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1466 le16_add_cpu(&xs->header->xh_count, -1);
1467 }
1468 }
1469 if (xi->value) {
1470 /* Insert the new name+value. */
1471 size_t size = OCFS2_XATTR_SIZE(name_len) +
1472 OCFS2_XATTR_SIZE(xi->value_len);
1473 void *val = xs->base + min_offs - size;
1474
1475 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1476 memset(val, 0, size);
1477 memcpy(val, xi->name, name_len);
1478 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1479 xi->value,
1480 xi->value_len);
1481 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1482 ocfs2_xattr_set_local(xs->here, 1);
1483 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1484 }
1485
1486 return;
1487}
1488
1489/*
1490 * ocfs2_xattr_set_entry()
1491 *
1492 * Set extended attribute entry into inode or block.
1493 *
1494 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1495 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1496 * then set value in B tree with set_value_outside().
1497 */
1498static int ocfs2_xattr_set_entry(struct inode *inode,
1499 struct ocfs2_xattr_info *xi,
1500 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001501 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001502 int flag)
1503{
1504 struct ocfs2_xattr_entry *last;
1505 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1506 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1507 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1508 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001509 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001510 int free, i, ret;
1511 struct ocfs2_xattr_info xi_l = {
1512 .name_index = xi->name_index,
1513 .name = xi->name,
1514 .value = xi->value,
1515 .value_len = xi->value_len,
1516 };
Joel Becker512620f2008-12-09 15:58:35 -08001517 struct ocfs2_xattr_value_buf vb = {
1518 .vb_bh = xs->xattr_bh,
1519 .vb_access = ocfs2_journal_access_di,
1520 };
1521
1522 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1523 BUG_ON(xs->xattr_bh == xs->inode_bh);
1524 vb.vb_access = ocfs2_journal_access_xb;
1525 } else
1526 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001527
1528 /* Compute min_offs, last and free space. */
1529 last = xs->header->xh_entries;
1530
1531 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1532 size_t offs = le16_to_cpu(last->xe_name_offset);
1533 if (offs < min_offs)
1534 min_offs = offs;
1535 last += 1;
1536 }
1537
Tiger Yang4442f512009-02-20 11:11:50 +08001538 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001539 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001540 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001541
1542 if (!xs->not_found) {
1543 size_t size = 0;
1544 if (ocfs2_xattr_is_local(xs->here))
1545 size = OCFS2_XATTR_SIZE(name_len) +
1546 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1547 else
1548 size = OCFS2_XATTR_SIZE(name_len) +
1549 OCFS2_XATTR_ROOT_SIZE;
1550 free += (size + sizeof(struct ocfs2_xattr_entry));
1551 }
1552 /* Check free space in inode or block */
1553 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1554 if (free < sizeof(struct ocfs2_xattr_entry) +
1555 OCFS2_XATTR_SIZE(name_len) +
1556 OCFS2_XATTR_ROOT_SIZE) {
1557 ret = -ENOSPC;
1558 goto out;
1559 }
1560 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1561 xi_l.value = (void *)&def_xv;
1562 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1563 } else if (xi->value) {
1564 if (free < sizeof(struct ocfs2_xattr_entry) +
1565 OCFS2_XATTR_SIZE(name_len) +
1566 OCFS2_XATTR_SIZE(xi->value_len)) {
1567 ret = -ENOSPC;
1568 goto out;
1569 }
1570 }
1571
1572 if (!xs->not_found) {
1573 /* For existing extended attribute */
1574 size_t size = OCFS2_XATTR_SIZE(name_len) +
1575 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1576 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1577 void *val = xs->base + offs;
1578
1579 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1580 /* Replace existing local xattr with tree root */
1581 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Joel Becker512620f2008-12-09 15:58:35 -08001582 ctxt, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001583 if (ret < 0)
1584 mlog_errno(ret);
1585 goto out;
1586 } else if (!ocfs2_xattr_is_local(xs->here)) {
1587 /* For existing xattr which has value outside */
Joel Becker512620f2008-12-09 15:58:35 -08001588 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1589 (val + OCFS2_XATTR_SIZE(name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001590
1591 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1592 /*
1593 * If new value need set outside also,
1594 * first truncate old value to new value,
1595 * then set new value with set_value_outside().
1596 */
1597 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001598 &vb,
Tao Ma78f30c32008-11-12 08:27:00 +08001599 xi->value_len,
1600 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001601 if (ret < 0) {
1602 mlog_errno(ret);
1603 goto out;
1604 }
1605
Tao Ma85db90e2008-11-12 08:27:01 +08001606 ret = ocfs2_xattr_update_entry(inode,
1607 handle,
1608 xi,
1609 xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001610 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001611 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001612 if (ret < 0) {
1613 mlog_errno(ret);
1614 goto out;
1615 }
1616
Tao Ma85db90e2008-11-12 08:27:01 +08001617 ret = __ocfs2_xattr_set_value_outside(inode,
1618 handle,
Tao Ma492a8a32009-08-18 11:43:17 +08001619 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001620 xi->value,
1621 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001622 if (ret < 0)
1623 mlog_errno(ret);
1624 goto out;
1625 } else {
1626 /*
1627 * If new value need set in local,
1628 * just trucate old value to zero.
1629 */
1630 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001631 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001632 0,
1633 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001634 if (ret < 0)
1635 mlog_errno(ret);
1636 }
1637 }
1638 }
1639
Joel Becker0cf2f762009-02-12 16:41:25 -08001640 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001641 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001642 if (ret) {
1643 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001644 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001645 }
1646
1647 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001648 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001649 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001650 if (ret) {
1651 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001652 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001653 }
1654 }
1655
1656 /*
1657 * Set value in local, include set tree root in local.
1658 * This is the first step for value size >INLINE_SIZE.
1659 */
1660 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1661
1662 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1663 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1664 if (ret < 0) {
1665 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001666 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001667 }
1668 }
1669
1670 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1671 (flag & OCFS2_INLINE_XATTR_FL)) {
1672 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1673 unsigned int xattrsize = osb->s_xattr_inline_size;
1674
1675 /*
1676 * Adjust extent record count or inline data size
1677 * to reserve space for extended attribute.
1678 */
1679 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1680 struct ocfs2_inline_data *idata = &di->id2.i_data;
1681 le16_add_cpu(&idata->id_count, -xattrsize);
1682 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1683 struct ocfs2_extent_list *el = &di->id2.i_list;
1684 le16_add_cpu(&el->l_count, -(xattrsize /
1685 sizeof(struct ocfs2_extent_rec)));
1686 }
1687 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1688 }
1689 /* Update xattr flag */
1690 spin_lock(&oi->ip_lock);
1691 oi->ip_dyn_features |= flag;
1692 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1693 spin_unlock(&oi->ip_lock);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001694
1695 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1696 if (ret < 0)
1697 mlog_errno(ret);
1698
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001699 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1700 /*
1701 * Set value outside in B tree.
1702 * This is the second step for value size > INLINE_SIZE.
1703 */
1704 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Joel Becker512620f2008-12-09 15:58:35 -08001705 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1706 &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001707 if (ret < 0) {
1708 int ret2;
1709
1710 mlog_errno(ret);
1711 /*
1712 * If set value outside failed, we have to clean
1713 * the junk tree root we have already set in local.
1714 */
Tao Ma85db90e2008-11-12 08:27:01 +08001715 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
Joel Becker512620f2008-12-09 15:58:35 -08001716 xi, xs, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001717 if (ret2 < 0)
1718 mlog_errno(ret2);
1719 }
1720 }
1721out:
1722 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001723}
1724
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001725static int ocfs2_remove_value_outside(struct inode*inode,
Joel Becker43119012008-12-09 16:24:43 -08001726 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001727 struct ocfs2_xattr_header *header)
1728{
1729 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001730 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1731 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1732
1733 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001734
Jan Karaa90714c2008-10-09 19:38:40 +02001735 ctxt.handle = ocfs2_start_trans(osb,
1736 ocfs2_remove_extent_credits(osb->sb));
Tao Ma85db90e2008-11-12 08:27:01 +08001737 if (IS_ERR(ctxt.handle)) {
1738 ret = PTR_ERR(ctxt.handle);
1739 mlog_errno(ret);
1740 goto out;
1741 }
1742
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001743 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1744 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1745
1746 if (!ocfs2_xattr_is_local(entry)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001747 void *val;
1748
1749 val = (void *)header +
1750 le16_to_cpu(entry->xe_name_offset);
Joel Becker43119012008-12-09 16:24:43 -08001751 vb->vb_xv = (struct ocfs2_xattr_value_root *)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001752 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Joel Becker43119012008-12-09 16:24:43 -08001753 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001754 if (ret < 0) {
1755 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001756 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001757 }
1758 }
1759 }
1760
Tao Ma85db90e2008-11-12 08:27:01 +08001761 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001762 ocfs2_schedule_truncate_log_flush(osb, 1);
1763 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001764out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001765 return ret;
1766}
1767
1768static int ocfs2_xattr_ibody_remove(struct inode *inode,
1769 struct buffer_head *di_bh)
1770{
1771
1772 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1773 struct ocfs2_xattr_header *header;
1774 int ret;
Joel Becker43119012008-12-09 16:24:43 -08001775 struct ocfs2_xattr_value_buf vb = {
1776 .vb_bh = di_bh,
1777 .vb_access = ocfs2_journal_access_di,
1778 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001779
1780 header = (struct ocfs2_xattr_header *)
1781 ((void *)di + inode->i_sb->s_blocksize -
1782 le16_to_cpu(di->i_xattr_inline_size));
1783
Joel Becker43119012008-12-09 16:24:43 -08001784 ret = ocfs2_remove_value_outside(inode, &vb, header);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001785
1786 return ret;
1787}
1788
1789static int ocfs2_xattr_block_remove(struct inode *inode,
1790 struct buffer_head *blk_bh)
1791{
1792 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001793 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08001794 struct ocfs2_xattr_value_buf vb = {
1795 .vb_bh = blk_bh,
1796 .vb_access = ocfs2_journal_access_xb,
1797 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001798
1799 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001800 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1801 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Joel Becker43119012008-12-09 16:24:43 -08001802 ret = ocfs2_remove_value_outside(inode, &vb, header);
Tao Maa3944252008-08-18 17:38:54 +08001803 } else
1804 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001805
1806 return ret;
1807}
1808
Tao Ma08413892008-08-29 09:00:19 +08001809static int ocfs2_xattr_free_block(struct inode *inode,
1810 u64 block)
1811{
1812 struct inode *xb_alloc_inode;
1813 struct buffer_head *xb_alloc_bh = NULL;
1814 struct buffer_head *blk_bh = NULL;
1815 struct ocfs2_xattr_block *xb;
1816 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1817 handle_t *handle;
1818 int ret = 0;
1819 u64 blk, bg_blkno;
1820 u16 bit;
1821
Joel Becker4ae1d692008-11-13 14:49:18 -08001822 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001823 if (ret < 0) {
1824 mlog_errno(ret);
1825 goto out;
1826 }
1827
Tao Ma08413892008-08-29 09:00:19 +08001828 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1829 if (ret < 0) {
1830 mlog_errno(ret);
1831 goto out;
1832 }
1833
Joel Becker4ae1d692008-11-13 14:49:18 -08001834 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001835 blk = le64_to_cpu(xb->xb_blkno);
1836 bit = le16_to_cpu(xb->xb_suballoc_bit);
1837 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1838
1839 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1840 EXTENT_ALLOC_SYSTEM_INODE,
1841 le16_to_cpu(xb->xb_suballoc_slot));
1842 if (!xb_alloc_inode) {
1843 ret = -ENOMEM;
1844 mlog_errno(ret);
1845 goto out;
1846 }
1847 mutex_lock(&xb_alloc_inode->i_mutex);
1848
1849 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1850 if (ret < 0) {
1851 mlog_errno(ret);
1852 goto out_mutex;
1853 }
1854
1855 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1856 if (IS_ERR(handle)) {
1857 ret = PTR_ERR(handle);
1858 mlog_errno(ret);
1859 goto out_unlock;
1860 }
1861
1862 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1863 bit, bg_blkno, 1);
1864 if (ret < 0)
1865 mlog_errno(ret);
1866
1867 ocfs2_commit_trans(osb, handle);
1868out_unlock:
1869 ocfs2_inode_unlock(xb_alloc_inode, 1);
1870 brelse(xb_alloc_bh);
1871out_mutex:
1872 mutex_unlock(&xb_alloc_inode->i_mutex);
1873 iput(xb_alloc_inode);
1874out:
1875 brelse(blk_bh);
1876 return ret;
1877}
1878
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001879/*
1880 * ocfs2_xattr_remove()
1881 *
1882 * Free extended attribute resources associated with this inode.
1883 */
1884int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1885{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001886 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1887 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1888 handle_t *handle;
1889 int ret;
1890
Tiger Yang8154da32008-08-18 17:11:46 +08001891 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1892 return 0;
1893
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001894 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1895 return 0;
1896
1897 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1898 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1899 if (ret < 0) {
1900 mlog_errno(ret);
1901 goto out;
1902 }
1903 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001904
Tao Ma08413892008-08-29 09:00:19 +08001905 if (di->i_xattr_loc) {
1906 ret = ocfs2_xattr_free_block(inode,
1907 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001908 if (ret < 0) {
1909 mlog_errno(ret);
1910 goto out;
1911 }
1912 }
1913
1914 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1915 OCFS2_INODE_UPDATE_CREDITS);
1916 if (IS_ERR(handle)) {
1917 ret = PTR_ERR(handle);
1918 mlog_errno(ret);
1919 goto out;
1920 }
Joel Becker0cf2f762009-02-12 16:41:25 -08001921 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08001922 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001923 if (ret) {
1924 mlog_errno(ret);
1925 goto out_commit;
1926 }
1927
Tao Ma08413892008-08-29 09:00:19 +08001928 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001929
1930 spin_lock(&oi->ip_lock);
1931 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1932 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1933 spin_unlock(&oi->ip_lock);
1934
1935 ret = ocfs2_journal_dirty(handle, di_bh);
1936 if (ret < 0)
1937 mlog_errno(ret);
1938out_commit:
1939 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1940out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001941 return ret;
1942}
1943
1944static int ocfs2_xattr_has_space_inline(struct inode *inode,
1945 struct ocfs2_dinode *di)
1946{
1947 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1948 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1949 int free;
1950
1951 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1952 return 0;
1953
1954 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1955 struct ocfs2_inline_data *idata = &di->id2.i_data;
1956 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1957 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1958 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1959 le64_to_cpu(di->i_size);
1960 } else {
1961 struct ocfs2_extent_list *el = &di->id2.i_list;
1962 free = (le16_to_cpu(el->l_count) -
1963 le16_to_cpu(el->l_next_free_rec)) *
1964 sizeof(struct ocfs2_extent_rec);
1965 }
1966 if (free >= xattrsize)
1967 return 1;
1968
1969 return 0;
1970}
1971
1972/*
1973 * ocfs2_xattr_ibody_find()
1974 *
1975 * Find extended attribute in inode block and
1976 * fill search info into struct ocfs2_xattr_search.
1977 */
1978static int ocfs2_xattr_ibody_find(struct inode *inode,
1979 int name_index,
1980 const char *name,
1981 struct ocfs2_xattr_search *xs)
1982{
1983 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1984 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1985 int ret;
1986 int has_space = 0;
1987
1988 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1989 return 0;
1990
1991 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1992 down_read(&oi->ip_alloc_sem);
1993 has_space = ocfs2_xattr_has_space_inline(inode, di);
1994 up_read(&oi->ip_alloc_sem);
1995 if (!has_space)
1996 return 0;
1997 }
1998
1999 xs->xattr_bh = xs->inode_bh;
2000 xs->end = (void *)di + inode->i_sb->s_blocksize;
2001 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
2002 xs->header = (struct ocfs2_xattr_header *)
2003 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
2004 else
2005 xs->header = (struct ocfs2_xattr_header *)
2006 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
2007 xs->base = (void *)xs->header;
2008 xs->here = xs->header->xh_entries;
2009
2010 /* Find the named attribute. */
2011 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
2012 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2013 if (ret && ret != -ENODATA)
2014 return ret;
2015 xs->not_found = ret;
2016 }
2017
2018 return 0;
2019}
2020
2021/*
2022 * ocfs2_xattr_ibody_set()
2023 *
2024 * Set, replace or remove an extended attribute into inode block.
2025 *
2026 */
2027static int ocfs2_xattr_ibody_set(struct inode *inode,
2028 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002029 struct ocfs2_xattr_search *xs,
2030 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002031{
2032 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2033 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2034 int ret;
2035
2036 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2037 return -ENOSPC;
2038
2039 down_write(&oi->ip_alloc_sem);
2040 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2041 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2042 ret = -ENOSPC;
2043 goto out;
2044 }
2045 }
2046
Tao Ma78f30c32008-11-12 08:27:00 +08002047 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002048 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2049out:
2050 up_write(&oi->ip_alloc_sem);
2051
2052 return ret;
2053}
2054
2055/*
2056 * ocfs2_xattr_block_find()
2057 *
2058 * Find extended attribute in external block and
2059 * fill search info into struct ocfs2_xattr_search.
2060 */
2061static int ocfs2_xattr_block_find(struct inode *inode,
2062 int name_index,
2063 const char *name,
2064 struct ocfs2_xattr_search *xs)
2065{
2066 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2067 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002068 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002069 int ret = 0;
2070
2071 if (!di->i_xattr_loc)
2072 return ret;
2073
Joel Becker4ae1d692008-11-13 14:49:18 -08002074 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2075 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002076 if (ret < 0) {
2077 mlog_errno(ret);
2078 return ret;
2079 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002080
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002081 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002082 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002083
Tao Ma589dc262008-08-18 17:38:51 +08002084 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2085 xs->header = &xb->xb_attrs.xb_header;
2086 xs->base = (void *)xs->header;
2087 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2088 xs->here = xs->header->xh_entries;
2089
2090 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2091 } else
2092 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2093 name_index,
2094 name, xs);
2095
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002096 if (ret && ret != -ENODATA) {
2097 xs->xattr_bh = NULL;
2098 goto cleanup;
2099 }
2100 xs->not_found = ret;
2101 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002102cleanup:
2103 brelse(blk_bh);
2104
2105 return ret;
2106}
2107
Tao Ma5aea1f02009-08-18 11:43:24 +08002108static int ocfs2_create_xattr_block(handle_t *handle,
2109 struct inode *inode,
2110 struct buffer_head *inode_bh,
2111 struct ocfs2_alloc_context *meta_ac,
2112 struct buffer_head **ret_bh)
2113{
2114 int ret;
2115 u16 suballoc_bit_start;
2116 u32 num_got;
2117 u64 first_blkno;
2118 struct ocfs2_dinode *di = (struct ocfs2_dinode *)inode_bh->b_data;
2119 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2120 struct buffer_head *new_bh = NULL;
2121 struct ocfs2_xattr_block *xblk;
2122
2123 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh,
2124 OCFS2_JOURNAL_ACCESS_CREATE);
2125 if (ret < 0) {
2126 mlog_errno(ret);
2127 goto end;
2128 }
2129
2130 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
2131 &suballoc_bit_start, &num_got,
2132 &first_blkno);
2133 if (ret < 0) {
2134 mlog_errno(ret);
2135 goto end;
2136 }
2137
2138 new_bh = sb_getblk(inode->i_sb, first_blkno);
2139 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2140
2141 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2142 new_bh,
2143 OCFS2_JOURNAL_ACCESS_CREATE);
2144 if (ret < 0) {
2145 mlog_errno(ret);
2146 goto end;
2147 }
2148
2149 /* Initialize ocfs2_xattr_block */
2150 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2151 memset(xblk, 0, inode->i_sb->s_blocksize);
2152 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2153 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2154 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2155 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2156 xblk->xb_blkno = cpu_to_le64(first_blkno);
2157
2158 ret = ocfs2_journal_dirty(handle, new_bh);
2159 if (ret < 0) {
2160 mlog_errno(ret);
2161 goto end;
2162 }
2163 di->i_xattr_loc = cpu_to_le64(first_blkno);
2164 ocfs2_journal_dirty(handle, inode_bh);
2165
2166 *ret_bh = new_bh;
2167 new_bh = NULL;
2168
2169end:
2170 brelse(new_bh);
2171 return ret;
2172}
2173
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002174/*
2175 * ocfs2_xattr_block_set()
2176 *
2177 * Set, replace or remove an extended attribute into external block.
2178 *
2179 */
2180static int ocfs2_xattr_block_set(struct inode *inode,
2181 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002182 struct ocfs2_xattr_search *xs,
2183 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002184{
2185 struct buffer_head *new_bh = NULL;
Tao Ma85db90e2008-11-12 08:27:01 +08002186 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002187 struct ocfs2_xattr_block *xblk = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002188 int ret;
2189
2190 if (!xs->xattr_bh) {
Tao Ma5aea1f02009-08-18 11:43:24 +08002191 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh,
2192 ctxt->meta_ac, &new_bh);
2193 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002194 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002195 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002196 }
2197
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002198 xs->xattr_bh = new_bh;
Tao Ma5aea1f02009-08-18 11:43:24 +08002199 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002200 xs->header = &xblk->xb_attrs.xb_header;
2201 xs->base = (void *)xs->header;
2202 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2203 xs->here = xs->header->xh_entries;
Tao Ma01225592008-08-18 17:38:53 +08002204 } else
2205 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2206
2207 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2208 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002209 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2210 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002211 if (!ret || ret != -ENOSPC)
2212 goto end;
2213
Tao Ma78f30c32008-11-12 08:27:00 +08002214 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002215 if (ret)
2216 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002217 }
2218
Tao Ma78f30c32008-11-12 08:27:00 +08002219 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002220
2221end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002222
2223 return ret;
2224}
2225
Tao Ma78f30c32008-11-12 08:27:00 +08002226/* Check whether the new xattr can be inserted into the inode. */
2227static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2228 struct ocfs2_xattr_info *xi,
2229 struct ocfs2_xattr_search *xs)
2230{
2231 u64 value_size;
2232 struct ocfs2_xattr_entry *last;
2233 int free, i;
2234 size_t min_offs = xs->end - xs->base;
2235
2236 if (!xs->header)
2237 return 0;
2238
2239 last = xs->header->xh_entries;
2240
2241 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2242 size_t offs = le16_to_cpu(last->xe_name_offset);
2243 if (offs < min_offs)
2244 min_offs = offs;
2245 last += 1;
2246 }
2247
Tiger Yang4442f512009-02-20 11:11:50 +08002248 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002249 if (free < 0)
2250 return 0;
2251
2252 BUG_ON(!xs->not_found);
2253
2254 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2255 value_size = OCFS2_XATTR_ROOT_SIZE;
2256 else
2257 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2258
2259 if (free >= sizeof(struct ocfs2_xattr_entry) +
2260 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2261 return 1;
2262
2263 return 0;
2264}
2265
2266static int ocfs2_calc_xattr_set_need(struct inode *inode,
2267 struct ocfs2_dinode *di,
2268 struct ocfs2_xattr_info *xi,
2269 struct ocfs2_xattr_search *xis,
2270 struct ocfs2_xattr_search *xbs,
2271 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002272 int *meta_need,
2273 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002274{
2275 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002276 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002277 struct buffer_head *bh = NULL;
2278 struct ocfs2_xattr_block *xb = NULL;
2279 struct ocfs2_xattr_entry *xe = NULL;
2280 struct ocfs2_xattr_value_root *xv = NULL;
2281 char *base = NULL;
2282 int name_offset, name_len = 0;
2283 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2284 xi->value_len);
2285 u64 value_size;
2286
Tao Ma71d548a2008-12-05 06:20:54 +08002287 /*
2288 * Calculate the clusters we need to write.
2289 * No matter whether we replace an old one or add a new one,
2290 * we need this for writing.
2291 */
2292 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2293 credits += new_clusters *
2294 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2295
Tao Ma78f30c32008-11-12 08:27:00 +08002296 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002297 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2298
2299 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002300 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002301 credits += ocfs2_calc_extend_credits(inode->i_sb,
2302 &def_xv.xv.xr_list,
2303 new_clusters);
2304 }
Tao Ma78f30c32008-11-12 08:27:00 +08002305
2306 goto meta_guess;
2307 }
2308
2309 if (!xis->not_found) {
2310 xe = xis->here;
2311 name_offset = le16_to_cpu(xe->xe_name_offset);
2312 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2313 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002314 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002315 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002316 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002317 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2318 xe = xbs->here;
2319 name_offset = le16_to_cpu(xe->xe_name_offset);
2320 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2321 i = xbs->here - xbs->header->xh_entries;
2322 old_in_xb = 1;
2323
2324 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08002325 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma78f30c32008-11-12 08:27:00 +08002326 bucket_xh(xbs->bucket),
2327 i, &block_off,
2328 &name_offset);
2329 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002330 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2331 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002332 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002333 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2334 }
2335 }
2336
2337 /*
2338 * delete a xattr doesn't need metadata and cluster allocation.
2339 * so just calculate the credits and return.
2340 *
2341 * The credits for removing the value tree will be extended
2342 * by ocfs2_remove_extent itself.
2343 */
2344 if (!xi->value) {
2345 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002346 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002347
2348 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002349 }
2350
2351 /* do cluster allocation guess first. */
2352 value_size = le64_to_cpu(xe->xe_value_size);
2353
2354 if (old_in_xb) {
2355 /*
2356 * In xattr set, we always try to set the xe in inode first,
2357 * so if it can be inserted into inode successfully, the old
2358 * one will be removed from the xattr block, and this xattr
2359 * will be inserted into inode as a new xattr in inode.
2360 */
2361 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2362 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002363 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002364 OCFS2_INODE_UPDATE_CREDITS;
2365 if (!ocfs2_xattr_is_local(xe))
2366 credits += ocfs2_calc_extend_credits(
2367 inode->i_sb,
2368 &def_xv.xv.xr_list,
2369 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002370 goto out;
2371 }
2372 }
2373
2374 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2375 /* the new values will be stored outside. */
2376 u32 old_clusters = 0;
2377
2378 if (!ocfs2_xattr_is_local(xe)) {
2379 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2380 value_size);
2381 xv = (struct ocfs2_xattr_value_root *)
2382 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002383 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002384 } else
2385 xv = &def_xv.xv;
2386
Tao Ma85db90e2008-11-12 08:27:01 +08002387 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002388 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002389 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002390 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002391 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2392 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002393 credits += ocfs2_calc_extend_credits(inode->i_sb,
2394 &xv->xr_list,
2395 new_clusters -
2396 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002397 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2398 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002399 }
2400 } else {
2401 /*
2402 * Now the new value will be stored inside. So if the new
2403 * value is smaller than the size of value root or the old
2404 * value, we don't need any allocation, otherwise we have
2405 * to guess metadata allocation.
2406 */
2407 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2408 (!ocfs2_xattr_is_local(xe) &&
2409 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2410 goto out;
2411 }
2412
2413meta_guess:
2414 /* calculate metadata allocation. */
2415 if (di->i_xattr_loc) {
2416 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002417 ret = ocfs2_read_xattr_block(inode,
2418 le64_to_cpu(di->i_xattr_loc),
2419 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002420 if (ret) {
2421 mlog_errno(ret);
2422 goto out;
2423 }
2424
2425 xb = (struct ocfs2_xattr_block *)bh->b_data;
2426 } else
2427 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2428
Tao Ma90cb5462008-12-05 06:20:56 +08002429 /*
2430 * If there is already an xattr tree, good, we can calculate
2431 * like other b-trees. Otherwise we may have the chance of
2432 * create a tree, the credit calculation is borrowed from
2433 * ocfs2_calc_extend_credits with root_el = NULL. And the
2434 * new tree will be cluster based, so no meta is needed.
2435 */
Tao Ma78f30c32008-11-12 08:27:00 +08002436 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2437 struct ocfs2_extent_list *el =
2438 &xb->xb_attrs.xb_root.xt_list;
2439 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002440 credits += ocfs2_calc_extend_credits(inode->i_sb,
2441 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08002442 } else
2443 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08002444
2445 /*
2446 * This cluster will be used either for new bucket or for
2447 * new xattr block.
2448 * If the cluster size is the same as the bucket size, one
2449 * more is needed since we may need to extend the bucket
2450 * also.
2451 */
2452 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002453 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002454 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002455 OCFS2_SB(inode->i_sb)->s_clustersize) {
2456 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002457 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002458 }
2459 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002460 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002461 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2462 }
Tao Ma78f30c32008-11-12 08:27:00 +08002463out:
2464 if (clusters_need)
2465 *clusters_need = clusters_add;
2466 if (meta_need)
2467 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002468 if (credits_need)
2469 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002470 brelse(bh);
2471 return ret;
2472}
2473
2474static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2475 struct ocfs2_dinode *di,
2476 struct ocfs2_xattr_info *xi,
2477 struct ocfs2_xattr_search *xis,
2478 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002479 struct ocfs2_xattr_set_ctxt *ctxt,
Tao Ma492a8a32009-08-18 11:43:17 +08002480 int extra_meta,
Tao Ma85db90e2008-11-12 08:27:01 +08002481 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002482{
2483 int clusters_add, meta_add, ret;
2484 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2485
2486 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2487
2488 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2489
2490 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002491 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002492 if (ret) {
2493 mlog_errno(ret);
2494 return ret;
2495 }
2496
Tao Ma492a8a32009-08-18 11:43:17 +08002497 meta_add += extra_meta;
Tao Ma85db90e2008-11-12 08:27:01 +08002498 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2499 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002500
2501 if (meta_add) {
2502 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2503 &ctxt->meta_ac);
2504 if (ret) {
2505 mlog_errno(ret);
2506 goto out;
2507 }
2508 }
2509
2510 if (clusters_add) {
2511 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2512 if (ret)
2513 mlog_errno(ret);
2514 }
2515out:
2516 if (ret) {
2517 if (ctxt->meta_ac) {
2518 ocfs2_free_alloc_context(ctxt->meta_ac);
2519 ctxt->meta_ac = NULL;
2520 }
2521
2522 /*
2523 * We cannot have an error and a non null ctxt->data_ac.
2524 */
2525 }
2526
2527 return ret;
2528}
2529
Tao Ma85db90e2008-11-12 08:27:01 +08002530static int __ocfs2_xattr_set_handle(struct inode *inode,
2531 struct ocfs2_dinode *di,
2532 struct ocfs2_xattr_info *xi,
2533 struct ocfs2_xattr_search *xis,
2534 struct ocfs2_xattr_search *xbs,
2535 struct ocfs2_xattr_set_ctxt *ctxt)
2536{
Tao Ma9f868f12008-11-19 16:48:42 +08002537 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002538
2539 if (!xi->value) {
2540 /* Remove existing extended attribute */
2541 if (!xis->not_found)
2542 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2543 else if (!xbs->not_found)
2544 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2545 } else {
2546 /* We always try to set extended attribute into inode first*/
2547 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2548 if (!ret && !xbs->not_found) {
2549 /*
2550 * If succeed and that extended attribute existing in
2551 * external block, then we will remove it.
2552 */
2553 xi->value = NULL;
2554 xi->value_len = 0;
2555
Tao Ma9f868f12008-11-19 16:48:42 +08002556 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002557 xis->not_found = -ENODATA;
2558 ret = ocfs2_calc_xattr_set_need(inode,
2559 di,
2560 xi,
2561 xis,
2562 xbs,
2563 NULL,
2564 NULL,
2565 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002566 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002567 if (ret) {
2568 mlog_errno(ret);
2569 goto out;
2570 }
2571
2572 ret = ocfs2_extend_trans(ctxt->handle, credits +
2573 ctxt->handle->h_buffer_credits);
2574 if (ret) {
2575 mlog_errno(ret);
2576 goto out;
2577 }
2578 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2579 } else if (ret == -ENOSPC) {
2580 if (di->i_xattr_loc && !xbs->xattr_bh) {
2581 ret = ocfs2_xattr_block_find(inode,
2582 xi->name_index,
2583 xi->name, xbs);
2584 if (ret)
2585 goto out;
2586
Tao Ma9f868f12008-11-19 16:48:42 +08002587 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002588 xis->not_found = -ENODATA;
2589 ret = ocfs2_calc_xattr_set_need(inode,
2590 di,
2591 xi,
2592 xis,
2593 xbs,
2594 NULL,
2595 NULL,
2596 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002597 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002598 if (ret) {
2599 mlog_errno(ret);
2600 goto out;
2601 }
2602
2603 ret = ocfs2_extend_trans(ctxt->handle, credits +
2604 ctxt->handle->h_buffer_credits);
2605 if (ret) {
2606 mlog_errno(ret);
2607 goto out;
2608 }
2609 }
2610 /*
2611 * If no space in inode, we will set extended attribute
2612 * into external block.
2613 */
2614 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2615 if (ret)
2616 goto out;
2617 if (!xis->not_found) {
2618 /*
2619 * If succeed and that extended attribute
2620 * existing in inode, we will remove it.
2621 */
2622 xi->value = NULL;
2623 xi->value_len = 0;
2624 xbs->not_found = -ENODATA;
2625 ret = ocfs2_calc_xattr_set_need(inode,
2626 di,
2627 xi,
2628 xis,
2629 xbs,
2630 NULL,
2631 NULL,
2632 &credits);
2633 if (ret) {
2634 mlog_errno(ret);
2635 goto out;
2636 }
2637
2638 ret = ocfs2_extend_trans(ctxt->handle, credits +
2639 ctxt->handle->h_buffer_credits);
2640 if (ret) {
2641 mlog_errno(ret);
2642 goto out;
2643 }
2644 ret = ocfs2_xattr_ibody_set(inode, xi,
2645 xis, ctxt);
2646 }
2647 }
2648 }
2649
Tao Ma4b3f6202008-12-05 06:20:55 +08002650 if (!ret) {
2651 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08002652 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08002653 xis->inode_bh,
2654 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08002655 if (ret) {
2656 mlog_errno(ret);
2657 goto out;
2658 }
2659
2660 inode->i_ctime = CURRENT_TIME;
2661 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2662 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2663 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2664 }
Tao Ma85db90e2008-11-12 08:27:01 +08002665out:
2666 return ret;
2667}
2668
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002669/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002670 * This function only called duing creating inode
2671 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08002672 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08002673 */
2674int ocfs2_xattr_set_handle(handle_t *handle,
2675 struct inode *inode,
2676 struct buffer_head *di_bh,
2677 int name_index,
2678 const char *name,
2679 const void *value,
2680 size_t value_len,
2681 int flags,
2682 struct ocfs2_alloc_context *meta_ac,
2683 struct ocfs2_alloc_context *data_ac)
2684{
2685 struct ocfs2_dinode *di;
2686 int ret;
2687
2688 struct ocfs2_xattr_info xi = {
2689 .name_index = name_index,
2690 .name = name,
2691 .value = value,
2692 .value_len = value_len,
2693 };
2694
2695 struct ocfs2_xattr_search xis = {
2696 .not_found = -ENODATA,
2697 };
2698
2699 struct ocfs2_xattr_search xbs = {
2700 .not_found = -ENODATA,
2701 };
2702
2703 struct ocfs2_xattr_set_ctxt ctxt = {
2704 .handle = handle,
2705 .meta_ac = meta_ac,
2706 .data_ac = data_ac,
2707 };
2708
2709 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2710 return -EOPNOTSUPP;
2711
Tiger Yang008aafa2008-12-09 16:43:08 +08002712 /*
2713 * In extreme situation, may need xattr bucket when
2714 * block size is too small. And we have already reserved
2715 * the credits for bucket in mknod.
2716 */
2717 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2718 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2719 if (!xbs.bucket) {
2720 mlog_errno(-ENOMEM);
2721 return -ENOMEM;
2722 }
2723 }
2724
Tiger Yang6c3faba2008-11-14 11:16:03 +08002725 xis.inode_bh = xbs.inode_bh = di_bh;
2726 di = (struct ocfs2_dinode *)di_bh->b_data;
2727
2728 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2729
2730 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2731 if (ret)
2732 goto cleanup;
2733 if (xis.not_found) {
2734 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2735 if (ret)
2736 goto cleanup;
2737 }
2738
2739 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2740
2741cleanup:
2742 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2743 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08002744 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08002745
2746 return ret;
2747}
2748
2749/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002750 * ocfs2_xattr_set()
2751 *
2752 * Set, replace or remove an extended attribute for this inode.
2753 * value is NULL to remove an existing extended attribute, else either
2754 * create or replace an extended attribute.
2755 */
2756int ocfs2_xattr_set(struct inode *inode,
2757 int name_index,
2758 const char *name,
2759 const void *value,
2760 size_t value_len,
2761 int flags)
2762{
2763 struct buffer_head *di_bh = NULL;
2764 struct ocfs2_dinode *di;
Tao Ma492a8a32009-08-18 11:43:17 +08002765 int ret, credits, ref_meta = 0, ref_credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002766 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002767 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002768 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tao Ma492a8a32009-08-18 11:43:17 +08002769 struct ocfs2_refcount_tree *ref_tree = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002770
2771 struct ocfs2_xattr_info xi = {
2772 .name_index = name_index,
2773 .name = name,
2774 .value = value,
2775 .value_len = value_len,
2776 };
2777
2778 struct ocfs2_xattr_search xis = {
2779 .not_found = -ENODATA,
2780 };
2781
2782 struct ocfs2_xattr_search xbs = {
2783 .not_found = -ENODATA,
2784 };
2785
Tiger Yang8154da32008-08-18 17:11:46 +08002786 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2787 return -EOPNOTSUPP;
2788
Joel Beckerba937122008-10-24 19:13:20 -07002789 /*
2790 * Only xbs will be used on indexed trees. xis doesn't need a
2791 * bucket.
2792 */
2793 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2794 if (!xbs.bucket) {
2795 mlog_errno(-ENOMEM);
2796 return -ENOMEM;
2797 }
2798
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002799 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2800 if (ret < 0) {
2801 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002802 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002803 }
2804 xis.inode_bh = xbs.inode_bh = di_bh;
2805 di = (struct ocfs2_dinode *)di_bh->b_data;
2806
2807 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2808 /*
2809 * Scan inode and external block to find the same name
2810 * extended attribute and collect search infomation.
2811 */
2812 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2813 if (ret)
2814 goto cleanup;
2815 if (xis.not_found) {
2816 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2817 if (ret)
2818 goto cleanup;
2819 }
2820
2821 if (xis.not_found && xbs.not_found) {
2822 ret = -ENODATA;
2823 if (flags & XATTR_REPLACE)
2824 goto cleanup;
2825 ret = 0;
2826 if (!value)
2827 goto cleanup;
2828 } else {
2829 ret = -EEXIST;
2830 if (flags & XATTR_CREATE)
2831 goto cleanup;
2832 }
2833
Tao Ma492a8a32009-08-18 11:43:17 +08002834 /* Check whether the value is refcounted and do some prepartion. */
2835 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL &&
2836 (!xis.not_found || !xbs.not_found)) {
2837 ret = ocfs2_prepare_refcount_xattr(inode, di, &xi,
2838 &xis, &xbs, &ref_tree,
2839 &ref_meta, &ref_credits);
2840 if (ret) {
2841 mlog_errno(ret);
2842 goto cleanup;
2843 }
2844 }
Tao Ma85db90e2008-11-12 08:27:01 +08002845
2846 mutex_lock(&tl_inode->i_mutex);
2847
2848 if (ocfs2_truncate_log_needs_flush(osb)) {
2849 ret = __ocfs2_flush_truncate_log(osb);
2850 if (ret < 0) {
2851 mutex_unlock(&tl_inode->i_mutex);
2852 mlog_errno(ret);
2853 goto cleanup;
2854 }
2855 }
2856 mutex_unlock(&tl_inode->i_mutex);
2857
2858 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
Tao Ma492a8a32009-08-18 11:43:17 +08002859 &xbs, &ctxt, ref_meta, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002860 if (ret) {
2861 mlog_errno(ret);
2862 goto cleanup;
2863 }
2864
Tao Ma4b3f6202008-12-05 06:20:55 +08002865 /* we need to update inode's ctime field, so add credit for it. */
2866 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma492a8a32009-08-18 11:43:17 +08002867 ctxt.handle = ocfs2_start_trans(osb, credits + ref_credits);
Tao Ma85db90e2008-11-12 08:27:01 +08002868 if (IS_ERR(ctxt.handle)) {
2869 ret = PTR_ERR(ctxt.handle);
2870 mlog_errno(ret);
2871 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002872 }
Tao Ma85db90e2008-11-12 08:27:01 +08002873
2874 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2875
2876 ocfs2_commit_trans(osb, ctxt.handle);
2877
Tao Ma78f30c32008-11-12 08:27:00 +08002878 if (ctxt.data_ac)
2879 ocfs2_free_alloc_context(ctxt.data_ac);
2880 if (ctxt.meta_ac)
2881 ocfs2_free_alloc_context(ctxt.meta_ac);
2882 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2883 ocfs2_schedule_truncate_log_flush(osb, 1);
2884 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002885cleanup:
Tao Ma492a8a32009-08-18 11:43:17 +08002886 if (ref_tree)
2887 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002888 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2889 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002890cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002891 brelse(di_bh);
2892 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002893 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002894
2895 return ret;
2896}
2897
Tao Ma0c044f02008-08-18 17:38:50 +08002898/*
2899 * Find the xattr extent rec which may contains name_hash.
2900 * e_cpos will be the first name hash of the xattr rec.
2901 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2902 */
2903static int ocfs2_xattr_get_rec(struct inode *inode,
2904 u32 name_hash,
2905 u64 *p_blkno,
2906 u32 *e_cpos,
2907 u32 *num_clusters,
2908 struct ocfs2_extent_list *el)
2909{
2910 int ret = 0, i;
2911 struct buffer_head *eb_bh = NULL;
2912 struct ocfs2_extent_block *eb;
2913 struct ocfs2_extent_rec *rec = NULL;
2914 u64 e_blkno = 0;
2915
2916 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002917 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
2918 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08002919 if (ret) {
2920 mlog_errno(ret);
2921 goto out;
2922 }
2923
2924 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2925 el = &eb->h_list;
2926
2927 if (el->l_tree_depth) {
2928 ocfs2_error(inode->i_sb,
2929 "Inode %lu has non zero tree depth in "
2930 "xattr tree block %llu\n", inode->i_ino,
2931 (unsigned long long)eb_bh->b_blocknr);
2932 ret = -EROFS;
2933 goto out;
2934 }
2935 }
2936
2937 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2938 rec = &el->l_recs[i];
2939
2940 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2941 e_blkno = le64_to_cpu(rec->e_blkno);
2942 break;
2943 }
2944 }
2945
2946 if (!e_blkno) {
2947 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2948 "record (%u, %u, 0) in xattr", inode->i_ino,
2949 le32_to_cpu(rec->e_cpos),
2950 ocfs2_rec_clusters(el, rec));
2951 ret = -EROFS;
2952 goto out;
2953 }
2954
2955 *p_blkno = le64_to_cpu(rec->e_blkno);
2956 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2957 if (e_cpos)
2958 *e_cpos = le32_to_cpu(rec->e_cpos);
2959out:
2960 brelse(eb_bh);
2961 return ret;
2962}
2963
2964typedef int (xattr_bucket_func)(struct inode *inode,
2965 struct ocfs2_xattr_bucket *bucket,
2966 void *para);
2967
Tao Ma589dc262008-08-18 17:38:51 +08002968static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002969 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002970 int name_index,
2971 const char *name,
2972 u32 name_hash,
2973 u16 *xe_index,
2974 int *found)
2975{
2976 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002977 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002978 size_t name_len = strlen(name);
2979 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002980 char *xe_name;
2981
2982 /*
2983 * We don't use binary search in the bucket because there
2984 * may be multiple entries with the same name hash.
2985 */
2986 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2987 xe = &xh->xh_entries[i];
2988
2989 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2990 continue;
2991 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2992 break;
2993
2994 cmp = name_index - ocfs2_xattr_get_type(xe);
2995 if (!cmp)
2996 cmp = name_len - xe->xe_name_len;
2997 if (cmp)
2998 continue;
2999
Tao Mafd68a892009-08-18 11:43:21 +08003000 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma589dc262008-08-18 17:38:51 +08003001 xh,
3002 i,
3003 &block_off,
3004 &new_offset);
3005 if (ret) {
3006 mlog_errno(ret);
3007 break;
3008 }
3009
Joel Becker970e4932008-11-13 14:49:19 -08003010
Joel Beckere2356a32008-10-27 15:01:54 -07003011 xe_name = bucket_block(bucket, block_off) + new_offset;
3012 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08003013 *xe_index = i;
3014 *found = 1;
3015 ret = 0;
3016 break;
3017 }
3018 }
3019
3020 return ret;
3021}
3022
3023/*
3024 * Find the specified xattr entry in a series of buckets.
3025 * This series start from p_blkno and last for num_clusters.
3026 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
3027 * the num of the valid buckets.
3028 *
3029 * Return the buffer_head this xattr should reside in. And if the xattr's
3030 * hash is in the gap of 2 buckets, return the lower bucket.
3031 */
3032static int ocfs2_xattr_bucket_find(struct inode *inode,
3033 int name_index,
3034 const char *name,
3035 u32 name_hash,
3036 u64 p_blkno,
3037 u32 first_hash,
3038 u32 num_clusters,
3039 struct ocfs2_xattr_search *xs)
3040{
3041 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003042 struct ocfs2_xattr_header *xh = NULL;
3043 struct ocfs2_xattr_entry *xe = NULL;
3044 u16 index = 0;
3045 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3046 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003047 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08003048 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07003049 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08003050
Joel Beckere2356a32008-10-27 15:01:54 -07003051 search = ocfs2_xattr_bucket_new(inode);
3052 if (!search) {
3053 ret = -ENOMEM;
3054 mlog_errno(ret);
3055 goto out;
3056 }
3057
3058 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003059 if (ret) {
3060 mlog_errno(ret);
3061 goto out;
3062 }
3063
Joel Beckere2356a32008-10-27 15:01:54 -07003064 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003065 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003066 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003067 ocfs2_xattr_bucket_relse(search);
3068
Tao Ma589dc262008-08-18 17:38:51 +08003069 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003070 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003071 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003072 if (ret) {
3073 mlog_errno(ret);
3074 goto out;
3075 }
3076
Joel Beckere2356a32008-10-27 15:01:54 -07003077 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003078 xe = &xh->xh_entries[0];
3079 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3080 high_bucket = bucket - 1;
3081 continue;
3082 }
3083
3084 /*
3085 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003086 * bucket is larger than the search one. for an empty
3087 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003088 */
Tao Ma5a095612008-09-19 22:17:41 +08003089 if (xh->xh_count)
3090 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3091
Tao Ma589dc262008-08-18 17:38:51 +08003092 last_hash = le32_to_cpu(xe->xe_name_hash);
3093
Joel Beckere2356a32008-10-27 15:01:54 -07003094 /* record lower_blkno which may be the insert place. */
3095 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003096
3097 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3098 low_bucket = bucket + 1;
3099 continue;
3100 }
3101
3102 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003103 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003104 name_index, name, name_hash,
3105 &index, &found);
3106 if (ret) {
3107 mlog_errno(ret);
3108 goto out;
3109 }
3110 break;
3111 }
3112
3113 /*
3114 * Record the bucket we have found.
3115 * When the xattr's hash value is in the gap of 2 buckets, we will
3116 * always set it to the previous bucket.
3117 */
Joel Beckere2356a32008-10-27 15:01:54 -07003118 if (!lower_blkno)
3119 lower_blkno = p_blkno;
3120
3121 /* This should be in cache - we just read it during the search */
3122 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3123 if (ret) {
3124 mlog_errno(ret);
3125 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003126 }
Tao Ma589dc262008-08-18 17:38:51 +08003127
Joel Beckerba937122008-10-24 19:13:20 -07003128 xs->header = bucket_xh(xs->bucket);
3129 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003130 xs->end = xs->base + inode->i_sb->s_blocksize;
3131
3132 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003133 xs->here = &xs->header->xh_entries[index];
3134 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003135 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003136 } else
3137 ret = -ENODATA;
3138
3139out:
Joel Beckere2356a32008-10-27 15:01:54 -07003140 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003141 return ret;
3142}
3143
3144static int ocfs2_xattr_index_block_find(struct inode *inode,
3145 struct buffer_head *root_bh,
3146 int name_index,
3147 const char *name,
3148 struct ocfs2_xattr_search *xs)
3149{
3150 int ret;
3151 struct ocfs2_xattr_block *xb =
3152 (struct ocfs2_xattr_block *)root_bh->b_data;
3153 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3154 struct ocfs2_extent_list *el = &xb_root->xt_list;
3155 u64 p_blkno = 0;
3156 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003157 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003158
3159 if (le16_to_cpu(el->l_next_free_rec) == 0)
3160 return -ENODATA;
3161
3162 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3163 name, name_hash, name_index);
3164
3165 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3166 &num_clusters, el);
3167 if (ret) {
3168 mlog_errno(ret);
3169 goto out;
3170 }
3171
3172 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3173
3174 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003175 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3176 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003177
3178 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3179 p_blkno, first_hash, num_clusters, xs);
3180
3181out:
3182 return ret;
3183}
3184
Tao Ma0c044f02008-08-18 17:38:50 +08003185static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3186 u64 blkno,
3187 u32 clusters,
3188 xattr_bucket_func *func,
3189 void *para)
3190{
Joel Becker6dde41d2008-10-24 17:16:48 -07003191 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003192 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3193 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003194 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003195
Joel Beckerba937122008-10-24 19:13:20 -07003196 bucket = ocfs2_xattr_bucket_new(inode);
3197 if (!bucket) {
3198 mlog_errno(-ENOMEM);
3199 return -ENOMEM;
3200 }
Tao Ma0c044f02008-08-18 17:38:50 +08003201
3202 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003203 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003204
Joel Beckerba937122008-10-24 19:13:20 -07003205 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3206 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003207 if (ret) {
3208 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003209 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003210 }
3211
Tao Ma0c044f02008-08-18 17:38:50 +08003212 /*
3213 * The real bucket num in this series of blocks is stored
3214 * in the 1st bucket.
3215 */
3216 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003217 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003218
Mark Fashehde29c082008-10-29 14:45:30 -07003219 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3220 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003221 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003222 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003223 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003224 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003225 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003226 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003227 }
3228
Joel Beckerba937122008-10-24 19:13:20 -07003229 ocfs2_xattr_bucket_relse(bucket);
3230 if (ret)
3231 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003232 }
3233
Joel Beckerba937122008-10-24 19:13:20 -07003234 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003235 return ret;
3236}
3237
3238struct ocfs2_xattr_tree_list {
3239 char *buffer;
3240 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003241 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003242};
3243
Tao Mafd68a892009-08-18 11:43:21 +08003244static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
Tao Ma0c044f02008-08-18 17:38:50 +08003245 struct ocfs2_xattr_header *xh,
3246 int index,
3247 int *block_off,
3248 int *new_offset)
3249{
3250 u16 name_offset;
3251
3252 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3253 return -EINVAL;
3254
3255 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3256
Tao Mafd68a892009-08-18 11:43:21 +08003257 *block_off = name_offset >> sb->s_blocksize_bits;
3258 *new_offset = name_offset % sb->s_blocksize;
Tao Ma0c044f02008-08-18 17:38:50 +08003259
3260 return 0;
3261}
3262
3263static int ocfs2_list_xattr_bucket(struct inode *inode,
3264 struct ocfs2_xattr_bucket *bucket,
3265 void *para)
3266{
Tao Ma936b8832008-10-09 23:06:14 +08003267 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003268 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003269 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003270 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003271
Joel Becker3e632942008-10-24 17:04:49 -07003272 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3273 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003274 type = ocfs2_xattr_get_type(entry);
3275 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003276
Tao Ma936b8832008-10-09 23:06:14 +08003277 if (prefix) {
Tao Mafd68a892009-08-18 11:43:21 +08003278 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Joel Becker3e632942008-10-24 17:04:49 -07003279 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003280 i,
3281 &block_off,
3282 &new_offset);
3283 if (ret)
3284 break;
Tao Ma936b8832008-10-09 23:06:14 +08003285
Joel Becker51def392008-10-24 16:57:21 -07003286 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003287 new_offset;
3288 ret = ocfs2_xattr_list_entry(xl->buffer,
3289 xl->buffer_size,
3290 &xl->result,
3291 prefix, name,
3292 entry->xe_name_len);
3293 if (ret)
3294 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003295 }
3296 }
3297
3298 return ret;
3299}
3300
3301static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3302 struct ocfs2_xattr_tree_root *xt,
3303 char *buffer,
3304 size_t buffer_size)
3305{
3306 struct ocfs2_extent_list *el = &xt->xt_list;
3307 int ret = 0;
3308 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3309 u64 p_blkno = 0;
3310 struct ocfs2_xattr_tree_list xl = {
3311 .buffer = buffer,
3312 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08003313 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08003314 };
3315
3316 if (le16_to_cpu(el->l_next_free_rec) == 0)
3317 return 0;
3318
3319 while (name_hash > 0) {
3320 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3321 &e_cpos, &num_clusters, el);
3322 if (ret) {
3323 mlog_errno(ret);
3324 goto out;
3325 }
3326
3327 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3328 ocfs2_list_xattr_bucket,
3329 &xl);
3330 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08003331 if (ret != -ERANGE)
3332 mlog_errno(ret);
Tao Ma0c044f02008-08-18 17:38:50 +08003333 goto out;
3334 }
3335
3336 if (e_cpos == 0)
3337 break;
3338
3339 name_hash = e_cpos - 1;
3340 }
3341
Tao Ma936b8832008-10-09 23:06:14 +08003342 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003343out:
3344 return ret;
3345}
Tao Ma01225592008-08-18 17:38:53 +08003346
3347static int cmp_xe(const void *a, const void *b)
3348{
3349 const struct ocfs2_xattr_entry *l = a, *r = b;
3350 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3351 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3352
3353 if (l_hash > r_hash)
3354 return 1;
3355 if (l_hash < r_hash)
3356 return -1;
3357 return 0;
3358}
3359
3360static void swap_xe(void *a, void *b, int size)
3361{
3362 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3363
3364 tmp = *l;
3365 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3366 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3367}
3368
3369/*
3370 * When the ocfs2_xattr_block is filled up, new bucket will be created
3371 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003372 * The header goes at the start of the bucket, and the names+values are
3373 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003374 * Note: we need to sort the entries since they are not saved in order
3375 * in the ocfs2_xattr_block.
3376 */
3377static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3378 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003379 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003380{
3381 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003382 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003383 u16 offset, size, off_change;
3384 struct ocfs2_xattr_entry *xe;
3385 struct ocfs2_xattr_block *xb =
3386 (struct ocfs2_xattr_block *)xb_bh->b_data;
3387 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003388 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003389 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003390 char *src = xb_bh->b_data;
3391 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003392
3393 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3394 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003395 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003396
Joel Becker178eeac2008-10-27 15:18:29 -07003397 for (i = 0; i < blks; i++)
3398 memset(bucket_block(bucket, i), 0, blocksize);
3399
Tao Ma01225592008-08-18 17:38:53 +08003400 /*
3401 * Since the xe_name_offset is based on ocfs2_xattr_header,
3402 * there is a offset change corresponding to the change of
3403 * ocfs2_xattr_header's position.
3404 */
3405 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3406 xe = &xb_xh->xh_entries[count - 1];
3407 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3408 size = blocksize - offset;
3409
3410 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003411 memcpy(target + offset, src + offset, size);
3412
3413 /* Init new header now. */
3414 xh->xh_count = xb_xh->xh_count;
3415 xh->xh_num_buckets = cpu_to_le16(1);
3416 xh->xh_name_value_len = cpu_to_le16(size);
3417 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3418
3419 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003420 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003421 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3422 size = count * sizeof(struct ocfs2_xattr_entry);
3423 memcpy(target + offset, (char *)xb_xh + offset, size);
3424
3425 /* Change the xe offset for all the xe because of the move. */
3426 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3427 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3428 for (i = 0; i < count; i++)
3429 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3430
3431 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3432 offset, size, off_change);
3433
3434 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3435 cmp_xe, swap_xe);
3436}
3437
3438/*
3439 * After we move xattr from block to index btree, we have to
3440 * update ocfs2_xattr_search to the new xe and base.
3441 *
3442 * When the entry is in xattr block, xattr_bh indicates the storage place.
3443 * While if the entry is in index b-tree, "bucket" indicates the
3444 * real place of the xattr.
3445 */
Joel Becker178eeac2008-10-27 15:18:29 -07003446static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3447 struct ocfs2_xattr_search *xs,
3448 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003449{
Tao Ma01225592008-08-18 17:38:53 +08003450 char *buf = old_bh->b_data;
3451 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3452 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003453 int i;
Tao Ma01225592008-08-18 17:38:53 +08003454
Joel Beckerba937122008-10-24 19:13:20 -07003455 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003456 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003457 xs->end = xs->base + inode->i_sb->s_blocksize;
3458
Joel Becker178eeac2008-10-27 15:18:29 -07003459 if (xs->not_found)
3460 return;
Tao Ma01225592008-08-18 17:38:53 +08003461
Joel Becker178eeac2008-10-27 15:18:29 -07003462 i = xs->here - old_xh->xh_entries;
3463 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003464}
3465
3466static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003467 struct ocfs2_xattr_search *xs,
3468 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003469{
Tao Ma85db90e2008-11-12 08:27:01 +08003470 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003471 u32 bit_off, len;
3472 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003473 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003474 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3475 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003476 struct buffer_head *xb_bh = xs->xattr_bh;
3477 struct ocfs2_xattr_block *xb =
3478 (struct ocfs2_xattr_block *)xb_bh->b_data;
3479 struct ocfs2_xattr_tree_root *xr;
3480 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003481
3482 mlog(0, "create xattr index block for %llu\n",
3483 (unsigned long long)xb_bh->b_blocknr);
3484
3485 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003486 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003487
Tao Ma01225592008-08-18 17:38:53 +08003488 /*
3489 * XXX:
3490 * We can use this lock for now, and maybe move to a dedicated mutex
3491 * if performance becomes a problem later.
3492 */
3493 down_write(&oi->ip_alloc_sem);
3494
Joel Becker0cf2f762009-02-12 16:41:25 -08003495 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08003496 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003497 if (ret) {
3498 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003499 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003500 }
3501
Tao Ma78f30c32008-11-12 08:27:00 +08003502 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3503 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003504 if (ret) {
3505 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003506 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003507 }
3508
3509 /*
3510 * The bucket may spread in many blocks, and
3511 * we will only touch the 1st block and the last block
3512 * in the whole bucket(one for entry and one for data).
3513 */
3514 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3515
Mark Fashehde29c082008-10-29 14:45:30 -07003516 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3517 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003518
Joel Becker178eeac2008-10-27 15:18:29 -07003519 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003520 if (ret) {
3521 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003522 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003523 }
3524
Joel Becker178eeac2008-10-27 15:18:29 -07003525 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3526 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003527 if (ret) {
3528 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003529 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003530 }
Tao Ma01225592008-08-18 17:38:53 +08003531
Joel Becker178eeac2008-10-27 15:18:29 -07003532 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3533 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3534
3535 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3536
Tao Ma01225592008-08-18 17:38:53 +08003537 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3538 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3539 offsetof(struct ocfs2_xattr_block, xb_attrs));
3540
3541 xr = &xb->xb_attrs.xb_root;
3542 xr->xt_clusters = cpu_to_le32(1);
3543 xr->xt_last_eb_blk = 0;
3544 xr->xt_list.l_tree_depth = 0;
3545 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3546 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3547
3548 xr->xt_list.l_recs[0].e_cpos = 0;
3549 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3550 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3551
3552 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3553
Tao Ma85db90e2008-11-12 08:27:01 +08003554 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003555
Tao Ma85db90e2008-11-12 08:27:01 +08003556out:
Tao Ma01225592008-08-18 17:38:53 +08003557 up_write(&oi->ip_alloc_sem);
3558
Tao Ma01225592008-08-18 17:38:53 +08003559 return ret;
3560}
3561
3562static int cmp_xe_offset(const void *a, const void *b)
3563{
3564 const struct ocfs2_xattr_entry *l = a, *r = b;
3565 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3566 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3567
3568 if (l_name_offset < r_name_offset)
3569 return 1;
3570 if (l_name_offset > r_name_offset)
3571 return -1;
3572 return 0;
3573}
3574
3575/*
3576 * defrag a xattr bucket if we find that the bucket has some
3577 * holes beteen name/value pairs.
3578 * We will move all the name/value pairs to the end of the bucket
3579 * so that we can spare some space for insertion.
3580 */
3581static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003582 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003583 struct ocfs2_xattr_bucket *bucket)
3584{
3585 int ret, i;
3586 size_t end, offset, len, value_len;
3587 struct ocfs2_xattr_header *xh;
3588 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003589 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003590 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003591 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003592 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003593
3594 /*
3595 * In order to make the operation more efficient and generic,
3596 * we copy all the blocks into a contiguous memory and do the
3597 * defragment there, so if anything is error, we will not touch
3598 * the real block.
3599 */
3600 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3601 if (!bucket_buf) {
3602 ret = -EIO;
3603 goto out;
3604 }
3605
Joel Becker161d6f32008-10-27 15:25:18 -07003606 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003607 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3608 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003609
Tao Ma1c32a2f2008-11-06 08:10:47 +08003610 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003611 OCFS2_JOURNAL_ACCESS_WRITE);
3612 if (ret < 0) {
3613 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003614 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003615 }
3616
3617 xh = (struct ocfs2_xattr_header *)bucket_buf;
3618 entries = (char *)xh->xh_entries;
3619 xh_free_start = le16_to_cpu(xh->xh_free_start);
3620
3621 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3622 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003623 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3624 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003625
3626 /*
3627 * sort all the entries by their offset.
3628 * the largest will be the first, so that we can
3629 * move them to the end one by one.
3630 */
3631 sort(entries, le16_to_cpu(xh->xh_count),
3632 sizeof(struct ocfs2_xattr_entry),
3633 cmp_xe_offset, swap_xe);
3634
3635 /* Move all name/values to the end of the bucket. */
3636 xe = xh->xh_entries;
3637 end = OCFS2_XATTR_BUCKET_SIZE;
3638 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3639 offset = le16_to_cpu(xe->xe_name_offset);
3640 if (ocfs2_xattr_is_local(xe))
3641 value_len = OCFS2_XATTR_SIZE(
3642 le64_to_cpu(xe->xe_value_size));
3643 else
3644 value_len = OCFS2_XATTR_ROOT_SIZE;
3645 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3646
3647 /*
3648 * We must make sure that the name/value pair
3649 * exist in the same block. So adjust end to
3650 * the previous block end if needed.
3651 */
3652 if (((end - len) / blocksize !=
3653 (end - 1) / blocksize))
3654 end = end - end % blocksize;
3655
3656 if (end > offset + len) {
3657 memmove(bucket_buf + end - len,
3658 bucket_buf + offset, len);
3659 xe->xe_name_offset = cpu_to_le16(end - len);
3660 }
3661
3662 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3663 "bucket %llu\n", (unsigned long long)blkno);
3664
3665 end -= len;
3666 }
3667
3668 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3669 "bucket %llu\n", (unsigned long long)blkno);
3670
3671 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003672 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003673
3674 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3675 xh->xh_free_start = cpu_to_le16(end);
3676
3677 /* sort the entries by their name_hash. */
3678 sort(entries, le16_to_cpu(xh->xh_count),
3679 sizeof(struct ocfs2_xattr_entry),
3680 cmp_xe, swap_xe);
3681
3682 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003683 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3684 memcpy(bucket_block(bucket, i), buf, blocksize);
3685 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003686
Tao Ma01225592008-08-18 17:38:53 +08003687out:
Tao Ma01225592008-08-18 17:38:53 +08003688 kfree(bucket_buf);
3689 return ret;
3690}
3691
3692/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08003693 * prev_blkno points to the start of an existing extent. new_blkno
3694 * points to a newly allocated extent. Because we know each of our
3695 * clusters contains more than bucket, we can easily split one cluster
3696 * at a bucket boundary. So we take the last cluster of the existing
3697 * extent and split it down the middle. We move the last half of the
3698 * buckets in the last cluster of the existing extent over to the new
3699 * extent.
Tao Ma01225592008-08-18 17:38:53 +08003700 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08003701 * first_bh is the buffer at prev_blkno so we can update the existing
3702 * extent's bucket count. header_bh is the bucket were we were hoping
3703 * to insert our xattr. If the bucket move places the target in the new
3704 * extent, we'll update first_bh and header_bh after modifying the old
3705 * extent.
3706 *
3707 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003708 */
3709static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3710 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08003711 struct ocfs2_xattr_bucket *first,
3712 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08003713 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08003714 u32 num_clusters,
3715 u32 *first_hash)
3716{
Joel Beckerc58b6032008-11-26 13:36:24 -08003717 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08003718 struct super_block *sb = inode->i_sb;
3719 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3720 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08003721 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08003722 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08003723 u64 last_cluster_blkno = bucket_blkno(first) +
3724 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08003725
Joel Becker41cb8142008-11-26 14:25:21 -08003726 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3727 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08003728
Tao Ma01225592008-08-18 17:38:53 +08003729 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08003730 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003731
Joel Becker41cb8142008-11-26 14:25:21 -08003732 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08003733 last_cluster_blkno, new_blkno,
3734 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003735 if (ret) {
3736 mlog_errno(ret);
3737 goto out;
3738 }
3739
Joel Beckerc58b6032008-11-26 13:36:24 -08003740 /* This is the first bucket that got moved */
3741 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3742
Tao Ma01225592008-08-18 17:38:53 +08003743 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003744 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08003745 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08003746 */
Joel Becker41cb8142008-11-26 14:25:21 -08003747 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08003748 /* Find the block for the new target bucket */
3749 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08003750 (bucket_blkno(target) - src_blkno);
3751
3752 ocfs2_xattr_bucket_relse(first);
3753 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003754
3755 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003756 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08003757 * journal from ocfs2_cp_xattr_bucket().
3758 */
Joel Becker41cb8142008-11-26 14:25:21 -08003759 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08003760 if (ret) {
3761 mlog_errno(ret);
3762 goto out;
3763 }
Joel Becker41cb8142008-11-26 14:25:21 -08003764 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3765 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08003766 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003767
Joel Beckerb5c03e72008-11-25 19:58:16 -08003768 }
3769
Tao Ma01225592008-08-18 17:38:53 +08003770out:
Tao Ma01225592008-08-18 17:38:53 +08003771 return ret;
3772}
3773
Tao Ma01225592008-08-18 17:38:53 +08003774/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003775 * Find the suitable pos when we divide a bucket into 2.
3776 * We have to make sure the xattrs with the same hash value exist
3777 * in the same bucket.
3778 *
3779 * If this ocfs2_xattr_header covers more than one hash value, find a
3780 * place where the hash value changes. Try to find the most even split.
3781 * The most common case is that all entries have different hash values,
3782 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003783 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003784static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3785{
3786 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3787 int count = le16_to_cpu(xh->xh_count);
3788 int delta, middle = count / 2;
3789
3790 /*
3791 * We start at the middle. Each step gets farther away in both
3792 * directions. We therefore hit the change in hash value
3793 * nearest to the middle. Note that this loop does not execute for
3794 * count < 2.
3795 */
3796 for (delta = 0; delta < middle; delta++) {
3797 /* Let's check delta earlier than middle */
3798 if (cmp_xe(&entries[middle - delta - 1],
3799 &entries[middle - delta]))
3800 return middle - delta;
3801
3802 /* For even counts, don't walk off the end */
3803 if ((middle + delta + 1) == count)
3804 continue;
3805
3806 /* Now try delta past middle */
3807 if (cmp_xe(&entries[middle + delta],
3808 &entries[middle + delta + 1]))
3809 return middle + delta + 1;
3810 }
3811
3812 /* Every entry had the same hash */
3813 return count;
3814}
3815
3816/*
3817 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3818 * first_hash will record the 1st hash of the new bucket.
3819 *
3820 * Normally half of the xattrs will be moved. But we have to make
3821 * sure that the xattrs with the same hash value are stored in the
3822 * same bucket. If all the xattrs in this bucket have the same hash
3823 * value, the new bucket will be initialized as an empty one and the
3824 * first_hash will be initialized as (hash_value+1).
3825 */
3826static int ocfs2_divide_xattr_bucket(struct inode *inode,
3827 handle_t *handle,
3828 u64 blk,
3829 u64 new_blk,
3830 u32 *first_hash,
3831 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003832{
3833 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003834 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003835 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003836 struct ocfs2_xattr_header *xh;
3837 struct ocfs2_xattr_entry *xe;
3838 int blocksize = inode->i_sb->s_blocksize;
3839
Tao Ma80bcaf32008-10-27 06:06:24 +08003840 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003841 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003842
Joel Beckerba937122008-10-24 19:13:20 -07003843 s_bucket = ocfs2_xattr_bucket_new(inode);
3844 t_bucket = ocfs2_xattr_bucket_new(inode);
3845 if (!s_bucket || !t_bucket) {
3846 ret = -ENOMEM;
3847 mlog_errno(ret);
3848 goto out;
3849 }
Tao Ma01225592008-08-18 17:38:53 +08003850
Joel Beckerba937122008-10-24 19:13:20 -07003851 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003852 if (ret) {
3853 mlog_errno(ret);
3854 goto out;
3855 }
3856
Joel Beckerba937122008-10-24 19:13:20 -07003857 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003858 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003859 if (ret) {
3860 mlog_errno(ret);
3861 goto out;
3862 }
3863
Joel Becker784b8162008-10-24 17:33:40 -07003864 /*
3865 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3866 * there's no need to read it.
3867 */
Joel Beckerba937122008-10-24 19:13:20 -07003868 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003869 if (ret) {
3870 mlog_errno(ret);
3871 goto out;
3872 }
3873
Joel Becker2b656c12008-11-25 19:00:15 -08003874 /*
3875 * Hey, if we're overwriting t_bucket, what difference does
3876 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3877 * same part of ocfs2_cp_xattr_bucket().
3878 */
Joel Beckerba937122008-10-24 19:13:20 -07003879 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003880 new_bucket_head ?
3881 OCFS2_JOURNAL_ACCESS_CREATE :
3882 OCFS2_JOURNAL_ACCESS_WRITE);
3883 if (ret) {
3884 mlog_errno(ret);
3885 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003886 }
3887
Joel Beckerba937122008-10-24 19:13:20 -07003888 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003889 count = le16_to_cpu(xh->xh_count);
3890 start = ocfs2_xattr_find_divide_pos(xh);
3891
3892 if (start == count) {
3893 xe = &xh->xh_entries[start-1];
3894
3895 /*
3896 * initialized a new empty bucket here.
3897 * The hash value is set as one larger than
3898 * that of the last entry in the previous bucket.
3899 */
Joel Beckerba937122008-10-24 19:13:20 -07003900 for (i = 0; i < t_bucket->bu_blocks; i++)
3901 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003902
Joel Beckerba937122008-10-24 19:13:20 -07003903 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003904 xh->xh_free_start = cpu_to_le16(blocksize);
3905 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3906 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3907
3908 goto set_num_buckets;
3909 }
3910
Tao Ma01225592008-08-18 17:38:53 +08003911 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003912 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003913
3914 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003915 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003916
3917 /*
3918 * Calculate the total name/value len and xh_free_start for
3919 * the old bucket first.
3920 */
3921 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3922 name_value_len = 0;
3923 for (i = 0; i < start; i++) {
3924 xe = &xh->xh_entries[i];
3925 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3926 if (ocfs2_xattr_is_local(xe))
3927 xe_len +=
3928 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3929 else
3930 xe_len += OCFS2_XATTR_ROOT_SIZE;
3931 name_value_len += xe_len;
3932 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3933 name_offset = le16_to_cpu(xe->xe_name_offset);
3934 }
3935
3936 /*
3937 * Now begin the modification to the new bucket.
3938 *
3939 * In the new bucket, We just move the xattr entry to the beginning
3940 * and don't touch the name/value. So there will be some holes in the
3941 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3942 * called.
3943 */
3944 xe = &xh->xh_entries[start];
3945 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3946 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003947 (int)((char *)xe - (char *)xh),
3948 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003949 memmove((char *)xh->xh_entries, (char *)xe, len);
3950 xe = &xh->xh_entries[count - start];
3951 len = sizeof(struct ocfs2_xattr_entry) * start;
3952 memset((char *)xe, 0, len);
3953
3954 le16_add_cpu(&xh->xh_count, -start);
3955 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3956
3957 /* Calculate xh_free_start for the new bucket. */
3958 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3959 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3960 xe = &xh->xh_entries[i];
3961 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3962 if (ocfs2_xattr_is_local(xe))
3963 xe_len +=
3964 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3965 else
3966 xe_len += OCFS2_XATTR_ROOT_SIZE;
3967 if (le16_to_cpu(xe->xe_name_offset) <
3968 le16_to_cpu(xh->xh_free_start))
3969 xh->xh_free_start = xe->xe_name_offset;
3970 }
3971
Tao Ma80bcaf32008-10-27 06:06:24 +08003972set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003973 /* set xh->xh_num_buckets for the new xh. */
3974 if (new_bucket_head)
3975 xh->xh_num_buckets = cpu_to_le16(1);
3976 else
3977 xh->xh_num_buckets = 0;
3978
Joel Beckerba937122008-10-24 19:13:20 -07003979 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003980
3981 /* store the first_hash of the new bucket. */
3982 if (first_hash)
3983 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3984
3985 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003986 * Now only update the 1st block of the old bucket. If we
3987 * just added a new empty bucket, there is no need to modify
3988 * it.
Tao Ma01225592008-08-18 17:38:53 +08003989 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003990 if (start == count)
3991 goto out;
3992
Joel Beckerba937122008-10-24 19:13:20 -07003993 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003994 memset(&xh->xh_entries[start], 0,
3995 sizeof(struct ocfs2_xattr_entry) * (count - start));
3996 xh->xh_count = cpu_to_le16(start);
3997 xh->xh_free_start = cpu_to_le16(name_offset);
3998 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3999
Joel Beckerba937122008-10-24 19:13:20 -07004000 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004001
4002out:
Joel Beckerba937122008-10-24 19:13:20 -07004003 ocfs2_xattr_bucket_free(s_bucket);
4004 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004005
4006 return ret;
4007}
4008
4009/*
4010 * Copy xattr from one bucket to another bucket.
4011 *
4012 * The caller must make sure that the journal transaction
4013 * has enough space for journaling.
4014 */
4015static int ocfs2_cp_xattr_bucket(struct inode *inode,
4016 handle_t *handle,
4017 u64 s_blkno,
4018 u64 t_blkno,
4019 int t_is_new)
4020{
Joel Becker4980c6d2008-10-24 18:54:43 -07004021 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07004022 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004023
4024 BUG_ON(s_blkno == t_blkno);
4025
4026 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004027 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
4028 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08004029
Joel Beckerba937122008-10-24 19:13:20 -07004030 s_bucket = ocfs2_xattr_bucket_new(inode);
4031 t_bucket = ocfs2_xattr_bucket_new(inode);
4032 if (!s_bucket || !t_bucket) {
4033 ret = -ENOMEM;
4034 mlog_errno(ret);
4035 goto out;
4036 }
Joel Becker92de1092008-11-25 17:06:40 -08004037
Joel Beckerba937122008-10-24 19:13:20 -07004038 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004039 if (ret)
4040 goto out;
4041
Joel Becker784b8162008-10-24 17:33:40 -07004042 /*
4043 * Even if !t_is_new, we're overwriting t_bucket. Thus,
4044 * there's no need to read it.
4045 */
Joel Beckerba937122008-10-24 19:13:20 -07004046 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004047 if (ret)
4048 goto out;
4049
Joel Becker2b656c12008-11-25 19:00:15 -08004050 /*
4051 * Hey, if we're overwriting t_bucket, what difference does
4052 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08004053 * cluster to fill, we came here from
4054 * ocfs2_mv_xattr_buckets(), and it is really new -
4055 * ACCESS_CREATE is required. But we also might have moved data
4056 * out of t_bucket before extending back into it.
4057 * ocfs2_add_new_xattr_bucket() can do this - its call to
4058 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08004059 * and copied out the end of the old extent. Then it re-extends
4060 * the old extent back to create space for new xattrs. That's
4061 * how we get here, and the bucket isn't really new.
4062 */
Joel Beckerba937122008-10-24 19:13:20 -07004063 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004064 t_is_new ?
4065 OCFS2_JOURNAL_ACCESS_CREATE :
4066 OCFS2_JOURNAL_ACCESS_WRITE);
4067 if (ret)
4068 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004069
Joel Beckerba937122008-10-24 19:13:20 -07004070 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4071 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004072
4073out:
Joel Beckerba937122008-10-24 19:13:20 -07004074 ocfs2_xattr_bucket_free(t_bucket);
4075 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004076
4077 return ret;
4078}
4079
4080/*
Joel Becker874d65a2008-11-26 13:02:18 -08004081 * src_blk points to the start of an existing extent. last_blk points to
4082 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004083 * extent. We copy the buckets from the cluster at last_blk to the new
4084 * extent. If start_bucket is non-zero, we skip that many buckets before
4085 * we start copying. The new extent's xh_num_buckets gets set to the
4086 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4087 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004088 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004089static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4090 u64 src_blk, u64 last_blk, u64 to_blk,
4091 unsigned int start_bucket,
4092 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004093{
4094 int i, ret, credits;
4095 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004096 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004097 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004098 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004099
Joel Becker874d65a2008-11-26 13:02:18 -08004100 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4101 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004102
Joel Becker54ecb6b2008-11-26 13:18:31 -08004103 BUG_ON(start_bucket >= num_buckets);
4104 if (start_bucket) {
4105 num_buckets -= start_bucket;
4106 last_blk += (start_bucket * blks_per_bucket);
4107 }
4108
Joel Becker15d60922008-11-25 18:36:42 -08004109 /* The first bucket of the original extent */
4110 old_first = ocfs2_xattr_bucket_new(inode);
4111 /* The first bucket of the new extent */
4112 new_first = ocfs2_xattr_bucket_new(inode);
4113 if (!old_first || !new_first) {
4114 ret = -ENOMEM;
4115 mlog_errno(ret);
4116 goto out;
4117 }
4118
Joel Becker874d65a2008-11-26 13:02:18 -08004119 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004120 if (ret) {
4121 mlog_errno(ret);
4122 goto out;
4123 }
4124
Tao Ma01225592008-08-18 17:38:53 +08004125 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004126 * We need to update the first bucket of the old extent and all
4127 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004128 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004129 credits = ((num_buckets + 1) * blks_per_bucket) +
4130 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004131 ret = ocfs2_extend_trans(handle, credits);
4132 if (ret) {
4133 mlog_errno(ret);
4134 goto out;
4135 }
4136
Joel Becker15d60922008-11-25 18:36:42 -08004137 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4138 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004139 if (ret) {
4140 mlog_errno(ret);
4141 goto out;
4142 }
4143
4144 for (i = 0; i < num_buckets; i++) {
4145 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004146 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004147 to_blk + (i * blks_per_bucket),
4148 1);
Tao Ma01225592008-08-18 17:38:53 +08004149 if (ret) {
4150 mlog_errno(ret);
4151 goto out;
4152 }
Tao Ma01225592008-08-18 17:38:53 +08004153 }
4154
Joel Becker15d60922008-11-25 18:36:42 -08004155 /*
4156 * Get the new bucket ready before we dirty anything
4157 * (This actually shouldn't fail, because we already dirtied
4158 * it once in ocfs2_cp_xattr_bucket()).
4159 */
4160 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4161 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004162 mlog_errno(ret);
4163 goto out;
4164 }
Joel Becker15d60922008-11-25 18:36:42 -08004165 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4166 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004167 if (ret) {
4168 mlog_errno(ret);
4169 goto out;
4170 }
4171
Joel Becker15d60922008-11-25 18:36:42 -08004172 /* Now update the headers */
4173 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4174 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004175
Joel Becker15d60922008-11-25 18:36:42 -08004176 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4177 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004178
4179 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004180 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4181
Tao Ma01225592008-08-18 17:38:53 +08004182out:
Joel Becker15d60922008-11-25 18:36:42 -08004183 ocfs2_xattr_bucket_free(new_first);
4184 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004185 return ret;
4186}
4187
4188/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004189 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004190 * This function should only be called when bucket size == cluster size.
4191 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4192 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004193static int ocfs2_divide_xattr_cluster(struct inode *inode,
4194 handle_t *handle,
4195 u64 prev_blk,
4196 u64 new_blk,
4197 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004198{
4199 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004200 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004201
4202 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4203
4204 ret = ocfs2_extend_trans(handle, credits);
4205 if (ret) {
4206 mlog_errno(ret);
4207 return ret;
4208 }
4209
4210 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004211 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4212 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004213}
4214
4215/*
4216 * Move some xattrs from the old cluster to the new one since they are not
4217 * contiguous in ocfs2 xattr tree.
4218 *
4219 * new_blk starts a new separate cluster, and we will move some xattrs from
4220 * prev_blk to it. v_start will be set as the first name hash value in this
4221 * new cluster so that it can be used as e_cpos during tree insertion and
4222 * don't collide with our original b-tree operations. first_bh and header_bh
4223 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4224 * to extend the insert bucket.
4225 *
4226 * The problem is how much xattr should we move to the new one and when should
4227 * we update first_bh and header_bh?
4228 * 1. If cluster size > bucket size, that means the previous cluster has more
4229 * than 1 bucket, so just move half nums of bucket into the new cluster and
4230 * update the first_bh and header_bh if the insert bucket has been moved
4231 * to the new cluster.
4232 * 2. If cluster_size == bucket_size:
4233 * a) If the previous extent rec has more than one cluster and the insert
4234 * place isn't in the last cluster, copy the entire last cluster to the
4235 * new one. This time, we don't need to upate the first_bh and header_bh
4236 * since they will not be moved into the new cluster.
4237 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4238 * the new one. And we set the extend flag to zero if the insert place is
4239 * moved into the new allocated cluster since no extend is needed.
4240 */
4241static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4242 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004243 struct ocfs2_xattr_bucket *first,
4244 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004245 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004246 u32 prev_clusters,
4247 u32 *v_start,
4248 int *extend)
4249{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004250 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004251
4252 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004253 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004254 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004255
Joel Becker41cb8142008-11-26 14:25:21 -08004256 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004257 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4258 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004259 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004260 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004261 prev_clusters,
4262 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004263 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004264 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004265 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004266 /* The start of the last cluster in the first extent */
4267 u64 last_blk = bucket_blkno(first) +
4268 ((prev_clusters - 1) *
4269 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004270
Joel Becker012ee912008-11-26 14:43:31 -08004271 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004272 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004273 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004274 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004275 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004276 if (ret)
4277 mlog_errno(ret);
4278 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004279 ret = ocfs2_divide_xattr_cluster(inode, handle,
4280 last_blk, new_blk,
4281 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004282 if (ret)
4283 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004284
Joel Becker92cf3ad2008-11-26 14:12:09 -08004285 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004286 *extend = 0;
4287 }
4288 }
4289
4290 return ret;
4291}
4292
4293/*
4294 * Add a new cluster for xattr storage.
4295 *
4296 * If the new cluster is contiguous with the previous one, it will be
4297 * appended to the same extent record, and num_clusters will be updated.
4298 * If not, we will insert a new extent for it and move some xattrs in
4299 * the last cluster into the new allocated one.
4300 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4301 * lose the benefits of hashing because we'll have to search large leaves.
4302 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4303 * if it's bigger).
4304 *
4305 * first_bh is the first block of the previous extent rec and header_bh
4306 * indicates the bucket we will insert the new xattrs. They will be updated
4307 * when the header_bh is moved into the new cluster.
4308 */
4309static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4310 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004311 struct ocfs2_xattr_bucket *first,
4312 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004313 u32 *num_clusters,
4314 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004315 int *extend,
4316 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004317{
Tao Ma85db90e2008-11-12 08:27:01 +08004318 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004319 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4320 u32 prev_clusters = *num_clusters;
4321 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4322 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004323 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004324 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004325 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004326
4327 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4328 "previous xattr blkno = %llu\n",
4329 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004330 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004331
Joel Becker5e404e92009-02-13 03:54:22 -08004332 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004333
Joel Becker0cf2f762009-02-12 16:41:25 -08004334 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004335 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004336 if (ret < 0) {
4337 mlog_errno(ret);
4338 goto leave;
4339 }
4340
Tao Ma78f30c32008-11-12 08:27:00 +08004341 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004342 clusters_to_add, &bit_off, &num_bits);
4343 if (ret < 0) {
4344 if (ret != -ENOSPC)
4345 mlog_errno(ret);
4346 goto leave;
4347 }
4348
4349 BUG_ON(num_bits > clusters_to_add);
4350
4351 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4352 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4353 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4354
Joel Beckered29c0c2008-11-26 15:08:44 -08004355 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08004356 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4357 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4358 /*
4359 * If this cluster is contiguous with the old one and
4360 * adding this new cluster, we don't surpass the limit of
4361 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4362 * initialized and used like other buckets in the previous
4363 * cluster.
4364 * So add it as a contiguous one. The caller will handle
4365 * its init process.
4366 */
4367 v_start = prev_cpos + prev_clusters;
4368 *num_clusters = prev_clusters + num_bits;
4369 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4370 num_bits);
4371 } else {
4372 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4373 handle,
Joel Becker012ee912008-11-26 14:43:31 -08004374 first,
4375 target,
Tao Ma01225592008-08-18 17:38:53 +08004376 block,
Tao Ma01225592008-08-18 17:38:53 +08004377 prev_clusters,
4378 &v_start,
4379 extend);
4380 if (ret) {
4381 mlog_errno(ret);
4382 goto leave;
4383 }
4384 }
4385
4386 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004387 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08004388 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004389 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004390 if (ret < 0) {
4391 mlog_errno(ret);
4392 goto leave;
4393 }
4394
4395 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004396 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004397 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004398
4399leave:
Tao Ma01225592008-08-18 17:38:53 +08004400 return ret;
4401}
4402
4403/*
Joel Becker92de1092008-11-25 17:06:40 -08004404 * We are given an extent. 'first' is the bucket at the very front of
4405 * the extent. The extent has space for an additional bucket past
4406 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4407 * of the target bucket. We wish to shift every bucket past the target
4408 * down one, filling in that additional space. When we get back to the
4409 * target, we split the target between itself and the now-empty bucket
4410 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004411 */
4412static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004413 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004414 struct ocfs2_xattr_bucket *first,
4415 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004416 u32 num_clusters)
4417{
4418 int ret, credits;
4419 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4420 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004421 u64 end_blk;
4422 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004423
4424 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004425 "from %llu, len = %u\n", (unsigned long long)target_blk,
4426 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004427
Joel Becker92de1092008-11-25 17:06:40 -08004428 /* The extent must have room for an additional bucket */
4429 BUG_ON(new_bucket >=
4430 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004431
Joel Becker92de1092008-11-25 17:06:40 -08004432 /* end_blk points to the last existing bucket */
4433 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004434
4435 /*
Joel Becker92de1092008-11-25 17:06:40 -08004436 * end_blk is the start of the last existing bucket.
4437 * Thus, (end_blk - target_blk) covers the target bucket and
4438 * every bucket after it up to, but not including, the last
4439 * existing bucket. Then we add the last existing bucket, the
4440 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004441 */
Joel Becker92de1092008-11-25 17:06:40 -08004442 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004443 handle->h_buffer_credits;
4444 ret = ocfs2_extend_trans(handle, credits);
4445 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004446 mlog_errno(ret);
4447 goto out;
4448 }
4449
Joel Becker92de1092008-11-25 17:06:40 -08004450 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4451 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004452 if (ret) {
4453 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004454 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004455 }
4456
Joel Becker92de1092008-11-25 17:06:40 -08004457 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004458 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4459 end_blk + blk_per_bucket, 0);
4460 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004461 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004462 end_blk -= blk_per_bucket;
4463 }
4464
Joel Becker92de1092008-11-25 17:06:40 -08004465 /* Move half of the xattr in target_blkno to the next bucket. */
4466 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4467 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004468
Joel Becker92de1092008-11-25 17:06:40 -08004469 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4470 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004471
Tao Ma01225592008-08-18 17:38:53 +08004472out:
4473 return ret;
4474}
4475
4476/*
Joel Becker91f20332008-11-26 15:25:41 -08004477 * Add new xattr bucket in an extent record and adjust the buckets
4478 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4479 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08004480 *
Joel Becker91f20332008-11-26 15:25:41 -08004481 * In the easy case, we will move all the buckets after target down by
4482 * one. Half of target's xattrs will be moved to the next bucket.
4483 *
4484 * If current cluster is full, we'll allocate a new one. This may not
4485 * be contiguous. The underlying calls will make sure that there is
4486 * space for the insert, shifting buckets around if necessary.
4487 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08004488 */
4489static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4490 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08004491 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08004492 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004493{
Tao Ma01225592008-08-18 17:38:53 +08004494 struct ocfs2_xattr_block *xb =
4495 (struct ocfs2_xattr_block *)xb_bh->b_data;
4496 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4497 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08004498 u32 name_hash =
4499 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08004500 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004501 int ret, num_buckets, extend = 1;
4502 u64 p_blkno;
4503 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004504 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08004505 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004506
Joel Becker91f20332008-11-26 15:25:41 -08004507 mlog(0, "Add new xattr bucket starting from %llu\n",
4508 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08004509
Joel Beckered29c0c2008-11-26 15:08:44 -08004510 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08004511 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08004512 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08004513 ret = -ENOMEM;
4514 mlog_errno(ret);
4515 goto out;
4516 }
4517
Tao Ma01225592008-08-18 17:38:53 +08004518 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4519 &num_clusters, el);
4520 if (ret) {
4521 mlog_errno(ret);
4522 goto out;
4523 }
4524
Joel Beckered29c0c2008-11-26 15:08:44 -08004525 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4526 if (ret) {
4527 mlog_errno(ret);
4528 goto out;
4529 }
4530
Tao Ma01225592008-08-18 17:38:53 +08004531 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08004532 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4533 /*
4534 * This can move first+target if the target bucket moves
4535 * to the new extent.
4536 */
Tao Ma01225592008-08-18 17:38:53 +08004537 ret = ocfs2_add_new_xattr_cluster(inode,
4538 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004539 first,
4540 target,
Tao Ma01225592008-08-18 17:38:53 +08004541 &num_clusters,
4542 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004543 &extend,
4544 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004545 if (ret) {
4546 mlog_errno(ret);
4547 goto out;
4548 }
4549 }
4550
Joel Becker92de1092008-11-25 17:06:40 -08004551 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08004552 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004553 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08004554 first,
4555 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08004556 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004557 if (ret)
4558 mlog_errno(ret);
4559 }
4560
Tao Ma01225592008-08-18 17:38:53 +08004561out:
Joel Becker92de1092008-11-25 17:06:40 -08004562 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08004563
Tao Ma01225592008-08-18 17:38:53 +08004564 return ret;
4565}
4566
4567static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4568 struct ocfs2_xattr_bucket *bucket,
4569 int offs)
4570{
4571 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4572
4573 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004574 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004575}
4576
4577/*
4578 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004579 *
4580 * Note: "local" indicates the real data's locality. So we can't
4581 * just its bucket locality by its length.
4582 */
4583static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4584 struct ocfs2_xattr_info *xi,
4585 struct ocfs2_xattr_search *xs,
4586 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004587 int local)
Tao Ma01225592008-08-18 17:38:53 +08004588{
4589 struct ocfs2_xattr_entry *last, *xe;
4590 int name_len = strlen(xi->name);
4591 struct ocfs2_xattr_header *xh = xs->header;
4592 u16 count = le16_to_cpu(xh->xh_count), start;
4593 size_t blocksize = inode->i_sb->s_blocksize;
4594 char *val;
4595 size_t offs, size, new_size;
4596
4597 last = &xh->xh_entries[count];
4598 if (!xs->not_found) {
4599 xe = xs->here;
4600 offs = le16_to_cpu(xe->xe_name_offset);
4601 if (ocfs2_xattr_is_local(xe))
4602 size = OCFS2_XATTR_SIZE(name_len) +
4603 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4604 else
4605 size = OCFS2_XATTR_SIZE(name_len) +
4606 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4607
4608 /*
4609 * If the new value will be stored outside, xi->value has been
4610 * initalized as an empty ocfs2_xattr_value_root, and the same
4611 * goes with xi->value_len, so we can set new_size safely here.
4612 * See ocfs2_xattr_set_in_bucket.
4613 */
4614 new_size = OCFS2_XATTR_SIZE(name_len) +
4615 OCFS2_XATTR_SIZE(xi->value_len);
4616
4617 le16_add_cpu(&xh->xh_name_value_len, -size);
4618 if (xi->value) {
4619 if (new_size > size)
4620 goto set_new_name_value;
4621
4622 /* Now replace the old value with new one. */
4623 if (local)
4624 xe->xe_value_size = cpu_to_le64(xi->value_len);
4625 else
4626 xe->xe_value_size = 0;
4627
4628 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004629 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004630 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4631 size - OCFS2_XATTR_SIZE(name_len));
4632 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4633 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4634 xi->value, xi->value_len);
4635
4636 le16_add_cpu(&xh->xh_name_value_len, new_size);
4637 ocfs2_xattr_set_local(xe, local);
4638 return;
4639 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004640 /*
4641 * Remove the old entry if there is more than one.
4642 * We don't remove the last entry so that we can
4643 * use it to indicate the hash value of the empty
4644 * bucket.
4645 */
Tao Ma01225592008-08-18 17:38:53 +08004646 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004647 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004648 if (xh->xh_count) {
4649 memmove(xe, xe + 1,
4650 (void *)last - (void *)xe);
4651 memset(last, 0,
4652 sizeof(struct ocfs2_xattr_entry));
4653 } else
4654 xh->xh_free_start =
4655 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4656
Tao Ma01225592008-08-18 17:38:53 +08004657 return;
4658 }
4659 } else {
4660 /* find a new entry for insert. */
4661 int low = 0, high = count - 1, tmp;
4662 struct ocfs2_xattr_entry *tmp_xe;
4663
Tao Ma5a095612008-09-19 22:17:41 +08004664 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004665 tmp = (low + high) / 2;
4666 tmp_xe = &xh->xh_entries[tmp];
4667
4668 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4669 low = tmp + 1;
4670 else if (name_hash <
4671 le32_to_cpu(tmp_xe->xe_name_hash))
4672 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004673 else {
4674 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004675 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004676 }
Tao Ma01225592008-08-18 17:38:53 +08004677 }
4678
4679 xe = &xh->xh_entries[low];
4680 if (low != count)
4681 memmove(xe + 1, xe, (void *)last - (void *)xe);
4682
4683 le16_add_cpu(&xh->xh_count, 1);
4684 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4685 xe->xe_name_hash = cpu_to_le32(name_hash);
4686 xe->xe_name_len = name_len;
4687 ocfs2_xattr_set_type(xe, xi->name_index);
4688 }
4689
4690set_new_name_value:
4691 /* Insert the new name+value. */
4692 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4693
4694 /*
4695 * We must make sure that the name/value pair
4696 * exists in the same block.
4697 */
4698 offs = le16_to_cpu(xh->xh_free_start);
4699 start = offs - size;
4700
4701 if (start >> inode->i_sb->s_blocksize_bits !=
4702 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4703 offs = offs - offs % blocksize;
4704 xh->xh_free_start = cpu_to_le16(offs);
4705 }
4706
Joel Beckerba937122008-10-24 19:13:20 -07004707 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004708 xe->xe_name_offset = cpu_to_le16(offs - size);
4709
4710 memset(val, 0, size);
4711 memcpy(val, xi->name, name_len);
4712 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4713
4714 xe->xe_value_size = cpu_to_le64(xi->value_len);
4715 ocfs2_xattr_set_local(xe, local);
4716 xs->here = xe;
4717 le16_add_cpu(&xh->xh_free_start, -size);
4718 le16_add_cpu(&xh->xh_name_value_len, size);
4719
4720 return;
4721}
4722
Tao Ma01225592008-08-18 17:38:53 +08004723/*
4724 * Set the xattr entry in the specified bucket.
4725 * The bucket is indicated by xs->bucket and it should have the enough
4726 * space for the xattr insertion.
4727 */
4728static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004729 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004730 struct ocfs2_xattr_info *xi,
4731 struct ocfs2_xattr_search *xs,
4732 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004733 int local)
Tao Ma01225592008-08-18 17:38:53 +08004734{
Joel Becker1224be02008-10-24 18:47:33 -07004735 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004736 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004737
Mark Fashehff1ec202008-08-19 10:54:29 -07004738 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4739 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004740 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004741
Joel Beckerba937122008-10-24 19:13:20 -07004742 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004743 blkno = bucket_blkno(xs->bucket);
4744 ocfs2_xattr_bucket_relse(xs->bucket);
4745 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004746 if (ret) {
4747 mlog_errno(ret);
4748 goto out;
4749 }
4750 }
4751
Joel Beckerba937122008-10-24 19:13:20 -07004752 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004753 OCFS2_JOURNAL_ACCESS_WRITE);
4754 if (ret < 0) {
4755 mlog_errno(ret);
4756 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004757 }
4758
Tao Ma5a095612008-09-19 22:17:41 +08004759 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004760 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004761
Tao Ma01225592008-08-18 17:38:53 +08004762out:
Tao Ma01225592008-08-18 17:38:53 +08004763 return ret;
4764}
4765
Tao Ma01225592008-08-18 17:38:53 +08004766/*
4767 * Truncate the specified xe_off entry in xattr bucket.
4768 * bucket is indicated by header_bh and len is the new length.
4769 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4770 *
4771 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4772 */
4773static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004774 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004775 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004776 int len,
4777 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004778{
4779 int ret, offset;
4780 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08004781 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004782 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004783 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08004784 struct ocfs2_xattr_value_buf vb = {
4785 .vb_access = ocfs2_journal_access,
4786 };
Tao Ma01225592008-08-18 17:38:53 +08004787
4788 xe = &xh->xh_entries[xe_off];
4789
4790 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4791
4792 offset = le16_to_cpu(xe->xe_name_offset) +
4793 OCFS2_XATTR_SIZE(xe->xe_name_len);
4794
4795 value_blk = offset / blocksize;
4796
4797 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4798 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004799
Joel Beckerb3e5d372008-12-09 15:01:04 -08004800 vb.vb_bh = bucket->bu_bhs[value_blk];
4801 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004802
Joel Beckerb3e5d372008-12-09 15:01:04 -08004803 vb.vb_xv = (struct ocfs2_xattr_value_root *)
4804 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004805
Joel Becker548b0f22008-11-24 19:32:13 -08004806 /*
4807 * From here on out we have to dirty the bucket. The generic
4808 * value calls only modify one of the bucket's bhs, but we need
4809 * to send the bucket at once. So if they error, they *could* have
4810 * modified something. We have to assume they did, and dirty
4811 * the whole bucket. This leaves us in a consistent state.
4812 */
Tao Ma01225592008-08-18 17:38:53 +08004813 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004814 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08004815 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004816 if (ret) {
4817 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08004818 goto out;
4819 }
4820
4821 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4822 OCFS2_JOURNAL_ACCESS_WRITE);
4823 if (ret) {
4824 mlog_errno(ret);
4825 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004826 }
4827
Joel Becker548b0f22008-11-24 19:32:13 -08004828 xe->xe_value_size = cpu_to_le64(len);
4829
Joel Becker548b0f22008-11-24 19:32:13 -08004830 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004831
4832out:
Tao Ma01225592008-08-18 17:38:53 +08004833 return ret;
4834}
4835
4836static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004837 struct ocfs2_xattr_search *xs,
4838 int len,
4839 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004840{
4841 int ret, offset;
4842 struct ocfs2_xattr_entry *xe = xs->here;
4843 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4844
Joel Beckerba937122008-10-24 19:13:20 -07004845 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004846
4847 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08004848 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08004849 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004850 if (ret)
4851 mlog_errno(ret);
4852
4853 return ret;
4854}
4855
4856static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004857 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004858 struct ocfs2_xattr_search *xs,
4859 char *val,
4860 int value_len)
4861{
Tao Ma712e53e2009-03-12 08:37:34 +08004862 int ret, offset, block_off;
Tao Ma01225592008-08-18 17:38:53 +08004863 struct ocfs2_xattr_value_root *xv;
4864 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma712e53e2009-03-12 08:37:34 +08004865 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4866 void *base;
Tao Ma492a8a32009-08-18 11:43:17 +08004867 struct ocfs2_xattr_value_buf vb = {
4868 .vb_access = ocfs2_journal_access,
4869 };
Tao Ma01225592008-08-18 17:38:53 +08004870
4871 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4872
Tao Mafd68a892009-08-18 11:43:21 +08004873 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
Tao Ma712e53e2009-03-12 08:37:34 +08004874 xe - xh->xh_entries,
4875 &block_off,
4876 &offset);
4877 if (ret) {
4878 mlog_errno(ret);
4879 goto out;
4880 }
Tao Ma01225592008-08-18 17:38:53 +08004881
Tao Ma712e53e2009-03-12 08:37:34 +08004882 base = bucket_block(xs->bucket, block_off);
4883 xv = (struct ocfs2_xattr_value_root *)(base + offset +
4884 OCFS2_XATTR_SIZE(xe->xe_name_len));
Tao Ma01225592008-08-18 17:38:53 +08004885
Tao Ma492a8a32009-08-18 11:43:17 +08004886 vb.vb_xv = xv;
4887 vb.vb_bh = xs->bucket->bu_bhs[block_off];
Tao Ma712e53e2009-03-12 08:37:34 +08004888 ret = __ocfs2_xattr_set_value_outside(inode, handle,
Tao Ma492a8a32009-08-18 11:43:17 +08004889 &vb, val, value_len);
Tao Ma712e53e2009-03-12 08:37:34 +08004890 if (ret)
4891 mlog_errno(ret);
4892out:
4893 return ret;
Tao Ma01225592008-08-18 17:38:53 +08004894}
4895
Tao Ma01225592008-08-18 17:38:53 +08004896static int ocfs2_rm_xattr_cluster(struct inode *inode,
4897 struct buffer_head *root_bh,
4898 u64 blkno,
4899 u32 cpos,
4900 u32 len)
4901{
4902 int ret;
4903 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4904 struct inode *tl_inode = osb->osb_tl_inode;
4905 handle_t *handle;
4906 struct ocfs2_xattr_block *xb =
4907 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004908 struct ocfs2_alloc_context *meta_ac = NULL;
4909 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004910 struct ocfs2_extent_tree et;
4911
Joel Becker5e404e92009-02-13 03:54:22 -08004912 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004913
4914 ocfs2_init_dealloc_ctxt(&dealloc);
4915
4916 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4917 cpos, len, (unsigned long long)blkno);
4918
Joel Becker8cb471e2009-02-10 20:00:41 -08004919 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
4920 len);
Tao Ma01225592008-08-18 17:38:53 +08004921
Joel Beckerf99b9b72008-08-20 19:36:33 -07004922 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004923 if (ret) {
4924 mlog_errno(ret);
4925 return ret;
4926 }
4927
4928 mutex_lock(&tl_inode->i_mutex);
4929
4930 if (ocfs2_truncate_log_needs_flush(osb)) {
4931 ret = __ocfs2_flush_truncate_log(osb);
4932 if (ret < 0) {
4933 mlog_errno(ret);
4934 goto out;
4935 }
4936 }
4937
Jan Karaa90714c2008-10-09 19:38:40 +02004938 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08004939 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004940 ret = -ENOMEM;
4941 mlog_errno(ret);
4942 goto out;
4943 }
4944
Joel Becker0cf2f762009-02-12 16:41:25 -08004945 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004946 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004947 if (ret) {
4948 mlog_errno(ret);
4949 goto out_commit;
4950 }
4951
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08004952 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004953 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004954 if (ret) {
4955 mlog_errno(ret);
4956 goto out_commit;
4957 }
4958
4959 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4960
4961 ret = ocfs2_journal_dirty(handle, root_bh);
4962 if (ret) {
4963 mlog_errno(ret);
4964 goto out_commit;
4965 }
4966
4967 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4968 if (ret)
4969 mlog_errno(ret);
4970
4971out_commit:
4972 ocfs2_commit_trans(osb, handle);
4973out:
4974 ocfs2_schedule_truncate_log_flush(osb, 1);
4975
4976 mutex_unlock(&tl_inode->i_mutex);
4977
4978 if (meta_ac)
4979 ocfs2_free_alloc_context(meta_ac);
4980
4981 ocfs2_run_deallocs(osb, &dealloc);
4982
4983 return ret;
4984}
4985
Tao Ma01225592008-08-18 17:38:53 +08004986static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004987 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004988 struct ocfs2_xattr_search *xs)
4989{
Joel Beckerba937122008-10-24 19:13:20 -07004990 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004991 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4992 le16_to_cpu(xh->xh_count) - 1];
4993 int ret = 0;
4994
Joel Beckerba937122008-10-24 19:13:20 -07004995 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004996 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004997 if (ret) {
4998 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004999 return;
Tao Ma01225592008-08-18 17:38:53 +08005000 }
5001
5002 /* Remove the old entry. */
5003 memmove(xs->here, xs->here + 1,
5004 (void *)last - (void *)xs->here);
5005 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5006 le16_add_cpu(&xh->xh_count, -1);
5007
Joel Beckerba937122008-10-24 19:13:20 -07005008 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005009}
5010
5011/*
5012 * Set the xattr name/value in the bucket specified in xs.
5013 *
5014 * As the new value in xi may be stored in the bucket or in an outside cluster,
5015 * we divide the whole process into 3 steps:
5016 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5017 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5018 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5019 * 4. If the clusters for the new outside value can't be allocated, we need
5020 * to free the xattr we allocated in set.
5021 */
5022static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5023 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005024 struct ocfs2_xattr_search *xs,
5025 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005026{
Tao Ma5a095612008-09-19 22:17:41 +08005027 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08005028 size_t value_len;
5029 char *val = (char *)xi->value;
5030 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08005031 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
5032 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08005033
5034 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5035 /*
5036 * We need to truncate the xattr storage first.
5037 *
5038 * If both the old and new value are stored to
5039 * outside block, we only need to truncate
5040 * the storage and then set the value outside.
5041 *
5042 * If the new value should be stored within block,
5043 * we should free all the outside block first and
5044 * the modification to the xattr block will be done
5045 * by following steps.
5046 */
5047 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5048 value_len = xi->value_len;
5049 else
5050 value_len = 0;
5051
5052 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005053 value_len,
5054 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005055 if (ret)
5056 goto out;
5057
5058 if (value_len)
5059 goto set_value_outside;
5060 }
5061
5062 value_len = xi->value_len;
5063 /* So we have to handle the inside block change now. */
5064 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5065 /*
5066 * If the new value will be stored outside of block,
5067 * initalize a new empty value root and insert it first.
5068 */
5069 local = 0;
5070 xi->value = &def_xv;
5071 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
5072 }
5073
Tao Ma85db90e2008-11-12 08:27:01 +08005074 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5075 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08005076 if (ret) {
5077 mlog_errno(ret);
5078 goto out;
5079 }
5080
Tao Ma5a095612008-09-19 22:17:41 +08005081 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5082 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005083
Tao Ma5a095612008-09-19 22:17:41 +08005084 /* allocate the space now for the outside block storage. */
5085 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005086 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08005087 if (ret) {
5088 mlog_errno(ret);
5089
5090 if (xs->not_found) {
5091 /*
5092 * We can't allocate enough clusters for outside
5093 * storage and we have allocated xattr already,
5094 * so need to remove it.
5095 */
Tao Ma85db90e2008-11-12 08:27:01 +08005096 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08005097 }
Tao Ma01225592008-08-18 17:38:53 +08005098 goto out;
5099 }
5100
5101set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08005102 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5103 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08005104out:
5105 return ret;
5106}
5107
Tao Ma80bcaf32008-10-27 06:06:24 +08005108/*
5109 * check whether the xattr bucket is filled up with the same hash value.
5110 * If we want to insert the xattr with the same hash, return -ENOSPC.
5111 * If we want to insert a xattr with different hash value, go ahead
5112 * and ocfs2_divide_xattr_bucket will handle this.
5113 */
Tao Ma01225592008-08-18 17:38:53 +08005114static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005115 struct ocfs2_xattr_bucket *bucket,
5116 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005117{
Joel Becker3e632942008-10-24 17:04:49 -07005118 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005119 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5120
5121 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5122 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005123
5124 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5125 xh->xh_entries[0].xe_name_hash) {
5126 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5127 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005128 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005129 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5130 return -ENOSPC;
5131 }
5132
5133 return 0;
5134}
5135
5136static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5137 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005138 struct ocfs2_xattr_search *xs,
5139 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005140{
5141 struct ocfs2_xattr_header *xh;
5142 struct ocfs2_xattr_entry *xe;
5143 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005144 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08005145 size_t value_size = 0, name_len = strlen(xi->name);
5146 size_t blocksize = inode->i_sb->s_blocksize;
5147 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005148
5149 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5150
5151try_again:
5152 xh = xs->header;
5153 count = le16_to_cpu(xh->xh_count);
5154 xh_free_start = le16_to_cpu(xh->xh_free_start);
5155 header_size = sizeof(struct ocfs2_xattr_header) +
5156 count * sizeof(struct ocfs2_xattr_entry);
Tiger Yang4442f512009-02-20 11:11:50 +08005157 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5158 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005159
5160 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5161 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005162 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005163 header_size);
5164
5165 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5166 value_size = OCFS2_XATTR_ROOT_SIZE;
5167 else if (xi->value)
5168 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5169
5170 if (xs->not_found)
5171 need = sizeof(struct ocfs2_xattr_entry) +
5172 OCFS2_XATTR_SIZE(name_len) + value_size;
5173 else {
5174 need = value_size + OCFS2_XATTR_SIZE(name_len);
5175
5176 /*
5177 * We only replace the old value if the new length is smaller
5178 * than the old one. Otherwise we will allocate new space in the
5179 * bucket to store it.
5180 */
5181 xe = xs->here;
5182 if (ocfs2_xattr_is_local(xe))
5183 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5184 else
5185 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5186
5187 if (old >= value_size)
5188 need = 0;
5189 }
5190
Tiger Yang4442f512009-02-20 11:11:50 +08005191 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005192 /*
5193 * We need to make sure the new name/value pair
5194 * can exist in the same block.
5195 */
5196 if (xh_free_start % blocksize < need)
5197 free -= xh_free_start % blocksize;
5198
5199 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5200 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5201 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005202 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005203 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5204 le16_to_cpu(xh->xh_name_value_len));
5205
Tao Ma976331d2008-11-12 08:26:57 +08005206 if (free < need ||
5207 (xs->not_found &&
5208 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005209 if (need <= max_free &&
5210 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5211 /*
5212 * We can create the space by defragment. Since only the
5213 * name/value will be moved, the xe shouldn't be changed
5214 * in xs.
5215 */
Tao Ma85db90e2008-11-12 08:27:01 +08005216 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5217 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005218 if (ret) {
5219 mlog_errno(ret);
5220 goto out;
5221 }
5222
5223 xh_free_start = le16_to_cpu(xh->xh_free_start);
Tiger Yang4442f512009-02-20 11:11:50 +08005224 free = xh_free_start - header_size
5225 - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005226 if (xh_free_start % blocksize < need)
5227 free -= xh_free_start % blocksize;
5228
5229 if (free >= need)
5230 goto xattr_set;
5231
5232 mlog(0, "Can't get enough space for xattr insert by "
5233 "defragment. Need %u bytes, but we have %d, so "
5234 "allocate new bucket for it.\n", need, free);
5235 }
5236
5237 /*
5238 * We have to add new buckets or clusters and one
5239 * allocation should leave us enough space for insert.
5240 */
5241 BUG_ON(allocation);
5242
5243 /*
5244 * We do not allow for overlapping ranges between buckets. And
5245 * the maximum number of collisions we will allow for then is
5246 * one bucket's worth, so check it here whether we need to
5247 * add a new bucket for the insert.
5248 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005249 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005250 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005251 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005252 if (ret) {
5253 mlog_errno(ret);
5254 goto out;
5255 }
5256
5257 ret = ocfs2_add_new_xattr_bucket(inode,
5258 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005259 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005260 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005261 if (ret) {
5262 mlog_errno(ret);
5263 goto out;
5264 }
5265
Joel Becker91f20332008-11-26 15:25:41 -08005266 /*
5267 * ocfs2_add_new_xattr_bucket() will have updated
5268 * xs->bucket if it moved, but it will not have updated
5269 * any of the other search fields. Thus, we drop it and
5270 * re-search. Everything should be cached, so it'll be
5271 * quick.
5272 */
Joel Beckerba937122008-10-24 19:13:20 -07005273 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005274 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5275 xi->name_index,
5276 xi->name, xs);
5277 if (ret && ret != -ENODATA)
5278 goto out;
5279 xs->not_found = ret;
5280 allocation = 1;
5281 goto try_again;
5282 }
5283
5284xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005285 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005286out:
5287 mlog_exit(ret);
5288 return ret;
5289}
Tao Maa3944252008-08-18 17:38:54 +08005290
5291static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5292 struct ocfs2_xattr_bucket *bucket,
5293 void *para)
5294{
5295 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07005296 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005297 u16 i;
5298 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005299 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5300 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005301 int credits = ocfs2_remove_extent_credits(osb->sb) +
5302 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5303
Tao Ma78f30c32008-11-12 08:27:00 +08005304
5305 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005306
5307 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5308 xe = &xh->xh_entries[i];
5309 if (ocfs2_xattr_is_local(xe))
5310 continue;
5311
Tao Ma88c3b062008-12-11 08:54:11 +08005312 ctxt.handle = ocfs2_start_trans(osb, credits);
5313 if (IS_ERR(ctxt.handle)) {
5314 ret = PTR_ERR(ctxt.handle);
5315 mlog_errno(ret);
5316 break;
5317 }
5318
Joel Becker548b0f22008-11-24 19:32:13 -08005319 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005320 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005321
5322 ocfs2_commit_trans(osb, ctxt.handle);
Tao Maa3944252008-08-18 17:38:54 +08005323 if (ret) {
5324 mlog_errno(ret);
5325 break;
5326 }
5327 }
5328
Tao Ma78f30c32008-11-12 08:27:00 +08005329 ocfs2_schedule_truncate_log_flush(osb, 1);
5330 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005331 return ret;
5332}
5333
5334static int ocfs2_delete_xattr_index_block(struct inode *inode,
5335 struct buffer_head *xb_bh)
5336{
5337 struct ocfs2_xattr_block *xb =
5338 (struct ocfs2_xattr_block *)xb_bh->b_data;
5339 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5340 int ret = 0;
5341 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5342 u64 p_blkno;
5343
5344 if (le16_to_cpu(el->l_next_free_rec) == 0)
5345 return 0;
5346
5347 while (name_hash > 0) {
5348 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5349 &e_cpos, &num_clusters, el);
5350 if (ret) {
5351 mlog_errno(ret);
5352 goto out;
5353 }
5354
5355 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5356 ocfs2_delete_xattr_in_bucket,
5357 NULL);
5358 if (ret) {
5359 mlog_errno(ret);
5360 goto out;
5361 }
5362
5363 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5364 p_blkno, e_cpos, num_clusters);
5365 if (ret) {
5366 mlog_errno(ret);
5367 break;
5368 }
5369
5370 if (e_cpos == 0)
5371 break;
5372
5373 name_hash = e_cpos - 1;
5374 }
5375
5376out:
5377 return ret;
5378}
Mark Fasheh99219ae2008-10-07 14:52:59 -07005379
5380/*
Tao Ma492a8a32009-08-18 11:43:17 +08005381 * Whenever we modify a xattr value root in the bucket(e.g, CoW
5382 * or change the extent record flag), we need to recalculate
5383 * the metaecc for the whole bucket. So it is done here.
5384 *
5385 * Note:
5386 * We have to give the extra credits for the caller.
5387 */
5388static int ocfs2_xattr_bucket_post_refcount(struct inode *inode,
5389 handle_t *handle,
5390 void *para)
5391{
5392 int ret;
5393 struct ocfs2_xattr_bucket *bucket =
5394 (struct ocfs2_xattr_bucket *)para;
5395
5396 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
5397 OCFS2_JOURNAL_ACCESS_WRITE);
5398 if (ret) {
5399 mlog_errno(ret);
5400 return ret;
5401 }
5402
5403 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
5404
5405 return 0;
5406}
5407
5408/*
5409 * Special action we need if the xattr value is refcounted.
5410 *
5411 * 1. If the xattr is refcounted, lock the tree.
5412 * 2. CoW the xattr if we are setting the new value and the value
5413 * will be stored outside.
5414 * 3. In other case, decrease_refcount will work for us, so just
5415 * lock the refcount tree, calculate the meta and credits is OK.
5416 *
5417 * We have to do CoW before ocfs2_init_xattr_set_ctxt since
5418 * currently CoW is a completed transaction, while this function
5419 * will also lock the allocators and let us deadlock. So we will
5420 * CoW the whole xattr value.
5421 */
5422static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5423 struct ocfs2_dinode *di,
5424 struct ocfs2_xattr_info *xi,
5425 struct ocfs2_xattr_search *xis,
5426 struct ocfs2_xattr_search *xbs,
5427 struct ocfs2_refcount_tree **ref_tree,
5428 int *meta_add,
5429 int *credits)
5430{
5431 int ret = 0;
5432 struct ocfs2_xattr_block *xb;
5433 struct ocfs2_xattr_entry *xe;
5434 char *base;
5435 u32 p_cluster, num_clusters;
5436 unsigned int ext_flags;
5437 int name_offset, name_len;
5438 struct ocfs2_xattr_value_buf vb;
5439 struct ocfs2_xattr_bucket *bucket = NULL;
5440 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5441 struct ocfs2_post_refcount refcount;
5442 struct ocfs2_post_refcount *p = NULL;
5443 struct buffer_head *ref_root_bh = NULL;
5444
5445 if (!xis->not_found) {
5446 xe = xis->here;
5447 name_offset = le16_to_cpu(xe->xe_name_offset);
5448 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5449 base = xis->base;
5450 vb.vb_bh = xis->inode_bh;
5451 vb.vb_access = ocfs2_journal_access_di;
5452 } else {
5453 int i, block_off = 0;
5454 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
5455 xe = xbs->here;
5456 name_offset = le16_to_cpu(xe->xe_name_offset);
5457 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
5458 i = xbs->here - xbs->header->xh_entries;
5459
5460 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
Tao Mafd68a892009-08-18 11:43:21 +08005461 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb,
Tao Ma492a8a32009-08-18 11:43:17 +08005462 bucket_xh(xbs->bucket),
5463 i, &block_off,
5464 &name_offset);
5465 if (ret) {
5466 mlog_errno(ret);
5467 goto out;
5468 }
5469 base = bucket_block(xbs->bucket, block_off);
5470 vb.vb_bh = xbs->bucket->bu_bhs[block_off];
5471 vb.vb_access = ocfs2_journal_access;
5472
5473 if (ocfs2_meta_ecc(osb)) {
5474 /*create parameters for ocfs2_post_refcount. */
5475 bucket = xbs->bucket;
5476 refcount.credits = bucket->bu_blocks;
5477 refcount.para = bucket;
5478 refcount.func =
5479 ocfs2_xattr_bucket_post_refcount;
5480 p = &refcount;
5481 }
5482 } else {
5483 base = xbs->base;
5484 vb.vb_bh = xbs->xattr_bh;
5485 vb.vb_access = ocfs2_journal_access_xb;
5486 }
5487 }
5488
5489 if (ocfs2_xattr_is_local(xe))
5490 goto out;
5491
5492 vb.vb_xv = (struct ocfs2_xattr_value_root *)
5493 (base + name_offset + name_len);
5494
5495 ret = ocfs2_xattr_get_clusters(inode, 0, &p_cluster,
5496 &num_clusters, &vb.vb_xv->xr_list,
5497 &ext_flags);
5498 if (ret) {
5499 mlog_errno(ret);
5500 goto out;
5501 }
5502
5503 /*
5504 * We just need to check the 1st extent record, since we always
5505 * CoW the whole xattr. So there shouldn't be a xattr with
5506 * some REFCOUNT extent recs after the 1st one.
5507 */
5508 if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
5509 goto out;
5510
5511 ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
5512 1, ref_tree, &ref_root_bh);
5513 if (ret) {
5514 mlog_errno(ret);
5515 goto out;
5516 }
5517
5518 /*
5519 * If we are deleting the xattr or the new size will be stored inside,
5520 * cool, leave it there, the xattr truncate process will remove them
5521 * for us(it still needs the refcount tree lock and the meta, credits).
5522 * And the worse case is that every cluster truncate will split the
5523 * refcount tree, and make the original extent become 3. So we will need
5524 * 2 * cluster more extent recs at most.
5525 */
5526 if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) {
5527
5528 ret = ocfs2_refcounted_xattr_delete_need(inode,
5529 &(*ref_tree)->rf_ci,
5530 ref_root_bh, vb.vb_xv,
5531 meta_add, credits);
5532 if (ret)
5533 mlog_errno(ret);
5534 goto out;
5535 }
5536
5537 ret = ocfs2_refcount_cow_xattr(inode, di, &vb,
5538 *ref_tree, ref_root_bh, 0,
5539 le32_to_cpu(vb.vb_xv->xr_clusters), p);
5540 if (ret)
5541 mlog_errno(ret);
5542
5543out:
5544 brelse(ref_root_bh);
5545 return ret;
5546}
5547
5548/*
Tiger Yang923f7f32008-11-14 11:16:27 +08005549 * 'security' attributes support
5550 */
5551static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5552 size_t list_size, const char *name,
5553 size_t name_len)
5554{
5555 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5556 const size_t total_len = prefix_len + name_len + 1;
5557
5558 if (list && total_len <= list_size) {
5559 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5560 memcpy(list + prefix_len, name, name_len);
5561 list[prefix_len + name_len] = '\0';
5562 }
5563 return total_len;
5564}
5565
5566static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5567 void *buffer, size_t size)
5568{
5569 if (strcmp(name, "") == 0)
5570 return -EINVAL;
5571 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5572 buffer, size);
5573}
5574
5575static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5576 const void *value, size_t size, int flags)
5577{
5578 if (strcmp(name, "") == 0)
5579 return -EINVAL;
5580
5581 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5582 size, flags);
5583}
5584
Tiger Yang534eadd2008-11-14 11:16:41 +08005585int ocfs2_init_security_get(struct inode *inode,
5586 struct inode *dir,
5587 struct ocfs2_security_xattr_info *si)
5588{
Tiger Yang38d59ef2008-12-17 10:22:56 +08005589 /* check whether ocfs2 support feature xattr */
5590 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
5591 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08005592 return security_inode_init_security(inode, dir, &si->name, &si->value,
5593 &si->value_len);
5594}
5595
5596int ocfs2_init_security_set(handle_t *handle,
5597 struct inode *inode,
5598 struct buffer_head *di_bh,
5599 struct ocfs2_security_xattr_info *si,
5600 struct ocfs2_alloc_context *xattr_ac,
5601 struct ocfs2_alloc_context *data_ac)
5602{
5603 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5604 OCFS2_XATTR_INDEX_SECURITY,
5605 si->name, si->value, si->value_len, 0,
5606 xattr_ac, data_ac);
5607}
5608
Tiger Yang923f7f32008-11-14 11:16:27 +08005609struct xattr_handler ocfs2_xattr_security_handler = {
5610 .prefix = XATTR_SECURITY_PREFIX,
5611 .list = ocfs2_xattr_security_list,
5612 .get = ocfs2_xattr_security_get,
5613 .set = ocfs2_xattr_security_set,
5614};
5615
5616/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07005617 * 'trusted' attributes support
5618 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005619static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5620 size_t list_size, const char *name,
5621 size_t name_len)
5622{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005623 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005624 const size_t total_len = prefix_len + name_len + 1;
5625
5626 if (list && total_len <= list_size) {
5627 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5628 memcpy(list + prefix_len, name, name_len);
5629 list[prefix_len + name_len] = '\0';
5630 }
5631 return total_len;
5632}
5633
5634static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5635 void *buffer, size_t size)
5636{
5637 if (strcmp(name, "") == 0)
5638 return -EINVAL;
5639 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5640 buffer, size);
5641}
5642
5643static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5644 const void *value, size_t size, int flags)
5645{
5646 if (strcmp(name, "") == 0)
5647 return -EINVAL;
5648
5649 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5650 size, flags);
5651}
5652
5653struct xattr_handler ocfs2_xattr_trusted_handler = {
5654 .prefix = XATTR_TRUSTED_PREFIX,
5655 .list = ocfs2_xattr_trusted_list,
5656 .get = ocfs2_xattr_trusted_get,
5657 .set = ocfs2_xattr_trusted_set,
5658};
5659
Mark Fasheh99219ae2008-10-07 14:52:59 -07005660/*
5661 * 'user' attributes support
5662 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005663static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5664 size_t list_size, const char *name,
5665 size_t name_len)
5666{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005667 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005668 const size_t total_len = prefix_len + name_len + 1;
5669 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5670
5671 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5672 return 0;
5673
5674 if (list && total_len <= list_size) {
5675 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5676 memcpy(list + prefix_len, name, name_len);
5677 list[prefix_len + name_len] = '\0';
5678 }
5679 return total_len;
5680}
5681
5682static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5683 void *buffer, size_t size)
5684{
5685 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5686
5687 if (strcmp(name, "") == 0)
5688 return -EINVAL;
5689 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5690 return -EOPNOTSUPP;
5691 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5692 buffer, size);
5693}
5694
5695static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5696 const void *value, size_t size, int flags)
5697{
5698 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5699
5700 if (strcmp(name, "") == 0)
5701 return -EINVAL;
5702 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5703 return -EOPNOTSUPP;
5704
5705 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5706 size, flags);
5707}
5708
5709struct xattr_handler ocfs2_xattr_user_handler = {
5710 .prefix = XATTR_USER_PREFIX,
5711 .list = ocfs2_xattr_user_list,
5712 .get = ocfs2_xattr_user_get,
5713 .set = ocfs2_xattr_user_set,
5714};