blob: dda49c00362aa434a7d8c7cb656b79172a6a0aa5 [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"
58
59
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 Ma589dc262008-08-18 17:38:51 +0800143static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
144 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 Maa3944252008-08-18 17:38:54 +0800179
Tiger Yang0030e002008-10-23 16:33:33 +0800180static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
181{
182 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
183}
184
185static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
186{
187 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
188}
189
190static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
191{
192 u16 len = sb->s_blocksize -
193 offsetof(struct ocfs2_xattr_header, xh_entries);
194
195 return len / sizeof(struct ocfs2_xattr_entry);
196}
197
Joel Becker9c7759a2008-10-24 16:21:03 -0700198#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700199#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700200#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700201
Joel Beckerba937122008-10-24 19:13:20 -0700202static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700203{
Joel Beckerba937122008-10-24 19:13:20 -0700204 struct ocfs2_xattr_bucket *bucket;
205 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700206
Joel Beckerba937122008-10-24 19:13:20 -0700207 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
208
209 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
210 if (bucket) {
211 bucket->bu_inode = inode;
212 bucket->bu_blocks = blks;
213 }
214
215 return bucket;
216}
217
218static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
219{
220 int i;
221
222 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700223 brelse(bucket->bu_bhs[i]);
224 bucket->bu_bhs[i] = NULL;
225 }
226}
227
Joel Beckerba937122008-10-24 19:13:20 -0700228static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
229{
230 if (bucket) {
231 ocfs2_xattr_bucket_relse(bucket);
232 bucket->bu_inode = NULL;
233 kfree(bucket);
234 }
235}
236
Joel Becker784b8162008-10-24 17:33:40 -0700237/*
238 * A bucket that has never been written to disk doesn't need to be
239 * read. We just need the buffer_heads. Don't call this for
240 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
241 * them fully.
242 */
Joel Beckerba937122008-10-24 19:13:20 -0700243static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700244 u64 xb_blkno)
245{
246 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700247
Joel Beckerba937122008-10-24 19:13:20 -0700248 for (i = 0; i < bucket->bu_blocks; i++) {
249 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
250 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700251 if (!bucket->bu_bhs[i]) {
252 rc = -EIO;
253 mlog_errno(rc);
254 break;
255 }
256
Joel Becker8cb471e2009-02-10 20:00:41 -0800257 if (!ocfs2_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800258 bucket->bu_bhs[i]))
Joel Becker8cb471e2009-02-10 20:00:41 -0800259 ocfs2_set_new_buffer_uptodate(INODE_CACHE(bucket->bu_inode),
Tao Ma757055a2008-11-06 08:10:48 +0800260 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700261 }
262
263 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700264 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700265 return rc;
266}
267
268/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700269static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700270 u64 xb_blkno)
271{
Joel Beckerba937122008-10-24 19:13:20 -0700272 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700273
Joel Becker8cb471e2009-02-10 20:00:41 -0800274 rc = ocfs2_read_blocks(INODE_CACHE(bucket->bu_inode), xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800275 bucket->bu_blocks, bucket->bu_bhs, 0,
276 NULL);
Joel Becker4d0e2142008-12-05 11:19:37 -0800277 if (!rc) {
Tao Mac8b9cf92009-02-24 17:40:26 -0800278 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800279 rc = ocfs2_validate_meta_ecc_bhs(bucket->bu_inode->i_sb,
280 bucket->bu_bhs,
281 bucket->bu_blocks,
282 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800283 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800284 if (rc)
285 mlog_errno(rc);
286 }
287
Joel Becker784b8162008-10-24 17:33:40 -0700288 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700289 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700290 return rc;
291}
292
Joel Becker1224be02008-10-24 18:47:33 -0700293static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700294 struct ocfs2_xattr_bucket *bucket,
295 int type)
296{
297 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700298
Joel Beckerba937122008-10-24 19:13:20 -0700299 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800300 rc = ocfs2_journal_access(handle,
301 INODE_CACHE(bucket->bu_inode),
Joel Becker1224be02008-10-24 18:47:33 -0700302 bucket->bu_bhs[i], type);
303 if (rc) {
304 mlog_errno(rc);
305 break;
306 }
307 }
308
309 return rc;
310}
311
312static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700313 struct ocfs2_xattr_bucket *bucket)
314{
Joel Beckerba937122008-10-24 19:13:20 -0700315 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700316
Tao Mac8b9cf92009-02-24 17:40:26 -0800317 spin_lock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800318 ocfs2_compute_meta_ecc_bhs(bucket->bu_inode->i_sb,
319 bucket->bu_bhs, bucket->bu_blocks,
320 &bucket_xh(bucket)->xh_check);
Tao Mac8b9cf92009-02-24 17:40:26 -0800321 spin_unlock(&OCFS2_SB(bucket->bu_inode->i_sb)->osb_xattr_lock);
Joel Becker4d0e2142008-12-05 11:19:37 -0800322
Joel Beckerba937122008-10-24 19:13:20 -0700323 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700324 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
325}
326
Joel Beckerba937122008-10-24 19:13:20 -0700327static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700328 struct ocfs2_xattr_bucket *src)
329{
330 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700331 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700332
Joel Beckerba937122008-10-24 19:13:20 -0700333 BUG_ON(dest->bu_blocks != src->bu_blocks);
334 BUG_ON(dest->bu_inode != src->bu_inode);
335
336 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700337 memcpy(bucket_block(dest, i), bucket_block(src, i),
338 blocksize);
339 }
340}
Joel Becker1224be02008-10-24 18:47:33 -0700341
Joel Becker4ae1d692008-11-13 14:49:18 -0800342static int ocfs2_validate_xattr_block(struct super_block *sb,
343 struct buffer_head *bh)
344{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700345 int rc;
Joel Becker4ae1d692008-11-13 14:49:18 -0800346 struct ocfs2_xattr_block *xb =
347 (struct ocfs2_xattr_block *)bh->b_data;
348
349 mlog(0, "Validating xattr block %llu\n",
350 (unsigned long long)bh->b_blocknr);
351
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700352 BUG_ON(!buffer_uptodate(bh));
353
354 /*
355 * If the ecc fails, we return the error but otherwise
356 * leave the filesystem running. We know any error is
357 * local to this block.
358 */
359 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &xb->xb_check);
360 if (rc)
361 return rc;
362
363 /*
364 * Errors after here are fatal
365 */
366
Joel Becker4ae1d692008-11-13 14:49:18 -0800367 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
368 ocfs2_error(sb,
369 "Extended attribute block #%llu has bad "
370 "signature %.*s",
371 (unsigned long long)bh->b_blocknr, 7,
372 xb->xb_signature);
373 return -EINVAL;
374 }
375
376 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
377 ocfs2_error(sb,
378 "Extended attribute block #%llu has an "
379 "invalid xb_blkno of %llu",
380 (unsigned long long)bh->b_blocknr,
381 (unsigned long long)le64_to_cpu(xb->xb_blkno));
382 return -EINVAL;
383 }
384
385 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
386 ocfs2_error(sb,
387 "Extended attribute block #%llu has an invalid "
388 "xb_fs_generation of #%u",
389 (unsigned long long)bh->b_blocknr,
390 le32_to_cpu(xb->xb_fs_generation));
391 return -EINVAL;
392 }
393
394 return 0;
395}
396
397static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
398 struct buffer_head **bh)
399{
400 int rc;
401 struct buffer_head *tmp = *bh;
402
Joel Becker8cb471e2009-02-10 20:00:41 -0800403 rc = ocfs2_read_block(INODE_CACHE(inode), xb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800404 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800405
406 /* If ocfs2_read_block() got us a new bh, pass it up. */
407 if (!rc && !*bh)
408 *bh = tmp;
409
410 return rc;
411}
412
Tao Ma936b8832008-10-09 23:06:14 +0800413static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800414{
415 struct xattr_handler *handler = NULL;
416
417 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
418 handler = ocfs2_xattr_handler_map[name_index];
419
Tao Ma936b8832008-10-09 23:06:14 +0800420 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800421}
422
Mark Fasheh40daa162008-10-07 14:31:42 -0700423static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800424 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700425 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800426{
427 /* Get hash value of uuid from super block */
428 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
429 int i;
430
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800431 /* hash extended attribute name */
432 for (i = 0; i < name_len; i++) {
433 hash = (hash << OCFS2_HASH_SHIFT) ^
434 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
435 *name++;
436 }
437
438 return hash;
439}
440
441/*
442 * ocfs2_xattr_hash_entry()
443 *
444 * Compute the hash of an extended attribute.
445 */
446static void ocfs2_xattr_hash_entry(struct inode *inode,
447 struct ocfs2_xattr_header *header,
448 struct ocfs2_xattr_entry *entry)
449{
450 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800451 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800452
Tao Ma2057e5c2008-10-09 23:06:13 +0800453 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800454 entry->xe_name_hash = cpu_to_le32(hash);
455
456 return;
457}
Tao Maf56654c2008-08-18 17:38:48 +0800458
Tiger Yang534eadd2008-11-14 11:16:41 +0800459static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
460{
461 int size = 0;
462
463 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
464 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
465 else
466 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
467 size += sizeof(struct ocfs2_xattr_entry);
468
469 return size;
470}
471
472int ocfs2_calc_security_init(struct inode *dir,
473 struct ocfs2_security_xattr_info *si,
474 int *want_clusters,
475 int *xattr_credits,
476 struct ocfs2_alloc_context **xattr_ac)
477{
478 int ret = 0;
479 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
480 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
481 si->value_len);
482
483 /*
484 * The max space of security xattr taken inline is
485 * 256(name) + 80(value) + 16(entry) = 352 bytes,
486 * So reserve one metadata block for it is ok.
487 */
488 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
489 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
490 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
491 if (ret) {
492 mlog_errno(ret);
493 return ret;
494 }
495 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
496 }
497
498 /* reserve clusters for xattr value which will be set in B tree*/
Tiger Yang0e445b62008-12-09 16:42:51 +0800499 if (si->value_len > OCFS2_XATTR_INLINE_SIZE) {
500 int new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
501 si->value_len);
502
503 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
504 new_clusters);
505 *want_clusters += new_clusters;
506 }
Tiger Yang534eadd2008-11-14 11:16:41 +0800507 return ret;
508}
509
Tiger Yang89c38bd2008-11-14 11:17:41 +0800510int ocfs2_calc_xattr_init(struct inode *dir,
511 struct buffer_head *dir_bh,
512 int mode,
513 struct ocfs2_security_xattr_info *si,
514 int *want_clusters,
515 int *xattr_credits,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800516 int *want_meta)
Tiger Yang89c38bd2008-11-14 11:17:41 +0800517{
518 int ret = 0;
519 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Tiger Yang0e445b62008-12-09 16:42:51 +0800520 int s_size = 0, a_size = 0, acl_len = 0, new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800521
522 if (si->enable)
523 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
524 si->value_len);
525
526 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
527 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
528 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
529 "", NULL, 0);
530 if (acl_len > 0) {
531 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
532 if (S_ISDIR(mode))
533 a_size <<= 1;
534 } else if (acl_len != 0 && acl_len != -ENODATA) {
535 mlog_errno(ret);
536 return ret;
537 }
538 }
539
540 if (!(s_size + a_size))
541 return ret;
542
543 /*
544 * The max space of security xattr taken inline is
545 * 256(name) + 80(value) + 16(entry) = 352 bytes,
546 * The max space of acl xattr taken inline is
547 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
548 * when blocksize = 512, may reserve one more cluser for
549 * xattr bucket, otherwise reserve one metadata block
550 * for them is ok.
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800551 * If this is a new directory with inline data,
552 * we choose to reserve the entire inline area for
553 * directory contents and force an external xattr block.
Tiger Yang89c38bd2008-11-14 11:17:41 +0800554 */
555 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
Tiger Yang6c9fd1d2009-03-06 10:19:30 +0800556 (S_ISDIR(mode) && ocfs2_supports_inline_data(osb)) ||
Tiger Yang89c38bd2008-11-14 11:17:41 +0800557 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800558 *want_meta = *want_meta + 1;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800559 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
560 }
561
562 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
563 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
564 *want_clusters += 1;
565 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
566 }
567
Tiger Yang0e445b62008-12-09 16:42:51 +0800568 /*
569 * reserve credits and clusters for xattrs which has large value
570 * and have to be set outside
571 */
572 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE) {
573 new_clusters = ocfs2_clusters_for_bytes(dir->i_sb,
574 si->value_len);
575 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
576 new_clusters);
577 *want_clusters += new_clusters;
578 }
Tiger Yang89c38bd2008-11-14 11:17:41 +0800579 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
580 acl_len > OCFS2_XATTR_INLINE_SIZE) {
Tiger Yang0e445b62008-12-09 16:42:51 +0800581 /* for directory, it has DEFAULT and ACCESS two types of acls */
582 new_clusters = (S_ISDIR(mode) ? 2 : 1) *
583 ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
584 *xattr_credits += ocfs2_clusters_to_blocks(dir->i_sb,
585 new_clusters);
586 *want_clusters += new_clusters;
Tiger Yang89c38bd2008-11-14 11:17:41 +0800587 }
588
589 return ret;
590}
591
Tao Maf56654c2008-08-18 17:38:48 +0800592static int ocfs2_xattr_extend_allocation(struct inode *inode,
593 u32 clusters_to_add,
Joel Becker19b801f2008-12-09 14:36:50 -0800594 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800595 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800596{
597 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800598 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800599 enum ocfs2_alloc_restarted why;
Joel Becker19b801f2008-12-09 14:36:50 -0800600 u32 prev_clusters, logical_start = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700601 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800602
603 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
604
Joel Becker5e404e92009-02-13 03:54:22 -0800605 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700606
Joel Becker0cf2f762009-02-12 16:41:25 -0800607 status = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker2a50a742008-12-09 14:24:33 -0800608 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800609 if (status < 0) {
610 mlog_errno(status);
611 goto leave;
612 }
613
Joel Becker19b801f2008-12-09 14:36:50 -0800614 prev_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Joel Beckercbee7e12009-02-13 03:34:15 -0800615 status = ocfs2_add_clusters_in_btree(handle,
616 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800617 &logical_start,
618 clusters_to_add,
619 0,
Tao Ma78f30c32008-11-12 08:27:00 +0800620 ctxt->data_ac,
621 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700622 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800623 if (status < 0) {
624 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800625 goto leave;
626 }
627
Joel Becker19b801f2008-12-09 14:36:50 -0800628 status = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800629 if (status < 0) {
630 mlog_errno(status);
631 goto leave;
632 }
633
Joel Becker19b801f2008-12-09 14:36:50 -0800634 clusters_to_add -= le32_to_cpu(vb->vb_xv->xr_clusters) - prev_clusters;
Tao Maf56654c2008-08-18 17:38:48 +0800635
Tao Ma85db90e2008-11-12 08:27:01 +0800636 /*
637 * We should have already allocated enough space before the transaction,
638 * so no need to restart.
639 */
640 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800641
642leave:
Tao Maf56654c2008-08-18 17:38:48 +0800643
644 return status;
645}
646
647static int __ocfs2_remove_xattr_range(struct inode *inode,
Joel Beckerd72cc722008-12-09 14:30:41 -0800648 struct ocfs2_xattr_value_buf *vb,
Tao Maf56654c2008-08-18 17:38:48 +0800649 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma78f30c32008-11-12 08:27:00 +0800650 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800651{
652 int ret;
653 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800654 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700655 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800656
Joel Becker5e404e92009-02-13 03:54:22 -0800657 ocfs2_init_xattr_value_extent_tree(&et, INODE_CACHE(inode), vb);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700658
Joel Becker0cf2f762009-02-12 16:41:25 -0800659 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Beckerd72cc722008-12-09 14:30:41 -0800660 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Maf56654c2008-08-18 17:38:48 +0800661 if (ret) {
662 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800663 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800664 }
665
Joel Beckerdbdcf6a2009-02-13 03:41:26 -0800666 ret = ocfs2_remove_extent(handle, &et, cpos, len, ctxt->meta_ac,
Tao Ma78f30c32008-11-12 08:27:00 +0800667 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800668 if (ret) {
669 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800670 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800671 }
672
Joel Beckerd72cc722008-12-09 14:30:41 -0800673 le32_add_cpu(&vb->vb_xv->xr_clusters, -len);
Tao Maf56654c2008-08-18 17:38:48 +0800674
Joel Beckerd72cc722008-12-09 14:30:41 -0800675 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tao Maf56654c2008-08-18 17:38:48 +0800676 if (ret) {
677 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800678 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800679 }
680
Tao Ma78f30c32008-11-12 08:27:00 +0800681 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800682 if (ret)
683 mlog_errno(ret);
684
Tao Maf56654c2008-08-18 17:38:48 +0800685out:
Tao Maf56654c2008-08-18 17:38:48 +0800686 return ret;
687}
688
689static int ocfs2_xattr_shrink_size(struct inode *inode,
690 u32 old_clusters,
691 u32 new_clusters,
Joel Becker19b801f2008-12-09 14:36:50 -0800692 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800693 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800694{
695 int ret = 0;
696 u32 trunc_len, cpos, phys_cpos, alloc_size;
697 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800698
699 if (old_clusters <= new_clusters)
700 return 0;
701
702 cpos = new_clusters;
703 trunc_len = old_clusters - new_clusters;
704 while (trunc_len) {
705 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
Joel Beckerd72cc722008-12-09 14:30:41 -0800706 &alloc_size,
Tao Ma1061f9c2009-08-18 11:41:57 +0800707 &vb->vb_xv->xr_list, NULL);
Tao Maf56654c2008-08-18 17:38:48 +0800708 if (ret) {
709 mlog_errno(ret);
710 goto out;
711 }
712
713 if (alloc_size > trunc_len)
714 alloc_size = trunc_len;
715
Joel Becker19b801f2008-12-09 14:36:50 -0800716 ret = __ocfs2_remove_xattr_range(inode, vb, cpos,
Tao Maf56654c2008-08-18 17:38:48 +0800717 phys_cpos, alloc_size,
Tao Ma78f30c32008-11-12 08:27:00 +0800718 ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800719 if (ret) {
720 mlog_errno(ret);
721 goto out;
722 }
723
724 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Joel Becker8cb471e2009-02-10 20:00:41 -0800725 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode),
726 block, alloc_size);
Tao Maf56654c2008-08-18 17:38:48 +0800727 cpos += alloc_size;
728 trunc_len -= alloc_size;
729 }
730
731out:
Tao Maf56654c2008-08-18 17:38:48 +0800732 return ret;
733}
734
735static int ocfs2_xattr_value_truncate(struct inode *inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800736 struct ocfs2_xattr_value_buf *vb,
Tao Ma78f30c32008-11-12 08:27:00 +0800737 int len,
738 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800739{
740 int ret;
741 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
Joel Beckerb3e5d372008-12-09 15:01:04 -0800742 u32 old_clusters = le32_to_cpu(vb->vb_xv->xr_clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800743
744 if (new_clusters == old_clusters)
745 return 0;
746
747 if (new_clusters > old_clusters)
748 ret = ocfs2_xattr_extend_allocation(inode,
749 new_clusters - old_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800750 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800751 else
752 ret = ocfs2_xattr_shrink_size(inode,
753 old_clusters, new_clusters,
Joel Beckerb3e5d372008-12-09 15:01:04 -0800754 vb, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800755
756 return ret;
757}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800758
Tao Ma936b8832008-10-09 23:06:14 +0800759static int ocfs2_xattr_list_entry(char *buffer, size_t size,
760 size_t *result, const char *prefix,
761 const char *name, int name_len)
762{
763 char *p = buffer + *result;
764 int prefix_len = strlen(prefix);
765 int total_len = prefix_len + name_len + 1;
766
767 *result += total_len;
768
769 /* we are just looking for how big our buffer needs to be */
770 if (!size)
771 return 0;
772
773 if (*result > size)
774 return -ERANGE;
775
776 memcpy(p, prefix, prefix_len);
777 memcpy(p + prefix_len, name, name_len);
778 p[prefix_len + name_len] = '\0';
779
780 return 0;
781}
782
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800783static int ocfs2_xattr_list_entries(struct inode *inode,
784 struct ocfs2_xattr_header *header,
785 char *buffer, size_t buffer_size)
786{
Tao Ma936b8832008-10-09 23:06:14 +0800787 size_t result = 0;
788 int i, type, ret;
789 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800790
791 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
792 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800793 type = ocfs2_xattr_get_type(entry);
794 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800795
Tao Ma936b8832008-10-09 23:06:14 +0800796 if (prefix) {
797 name = (const char *)header +
798 le16_to_cpu(entry->xe_name_offset);
799
800 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
801 &result, prefix, name,
802 entry->xe_name_len);
803 if (ret)
804 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800805 }
806 }
807
Tao Ma936b8832008-10-09 23:06:14 +0800808 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800809}
810
811static int ocfs2_xattr_ibody_list(struct inode *inode,
812 struct ocfs2_dinode *di,
813 char *buffer,
814 size_t buffer_size)
815{
816 struct ocfs2_xattr_header *header = NULL;
817 struct ocfs2_inode_info *oi = OCFS2_I(inode);
818 int ret = 0;
819
820 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
821 return ret;
822
823 header = (struct ocfs2_xattr_header *)
824 ((void *)di + inode->i_sb->s_blocksize -
825 le16_to_cpu(di->i_xattr_inline_size));
826
827 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
828
829 return ret;
830}
831
832static int ocfs2_xattr_block_list(struct inode *inode,
833 struct ocfs2_dinode *di,
834 char *buffer,
835 size_t buffer_size)
836{
837 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800838 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800839 int ret = 0;
840
841 if (!di->i_xattr_loc)
842 return ret;
843
Joel Becker4ae1d692008-11-13 14:49:18 -0800844 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
845 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800846 if (ret < 0) {
847 mlog_errno(ret);
848 return ret;
849 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800850
Tao Ma0c044f02008-08-18 17:38:50 +0800851 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800852 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
853 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
854 ret = ocfs2_xattr_list_entries(inode, header,
855 buffer, buffer_size);
856 } else {
857 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
858 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
859 buffer, buffer_size);
860 }
Joel Becker4ae1d692008-11-13 14:49:18 -0800861
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800862 brelse(blk_bh);
863
864 return ret;
865}
866
867ssize_t ocfs2_listxattr(struct dentry *dentry,
868 char *buffer,
869 size_t size)
870{
871 int ret = 0, i_ret = 0, b_ret = 0;
872 struct buffer_head *di_bh = NULL;
873 struct ocfs2_dinode *di = NULL;
874 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
875
Tiger Yang8154da32008-08-18 17:11:46 +0800876 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
877 return -EOPNOTSUPP;
878
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800879 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
880 return ret;
881
882 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
883 if (ret < 0) {
884 mlog_errno(ret);
885 return ret;
886 }
887
888 di = (struct ocfs2_dinode *)di_bh->b_data;
889
890 down_read(&oi->ip_xattr_sem);
891 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
892 if (i_ret < 0)
893 b_ret = 0;
894 else {
895 if (buffer) {
896 buffer += i_ret;
897 size -= i_ret;
898 }
899 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
900 buffer, size);
901 if (b_ret < 0)
902 i_ret = 0;
903 }
904 up_read(&oi->ip_xattr_sem);
905 ocfs2_inode_unlock(dentry->d_inode, 0);
906
907 brelse(di_bh);
908
909 return i_ret + b_ret;
910}
911
912static int ocfs2_xattr_find_entry(int name_index,
913 const char *name,
914 struct ocfs2_xattr_search *xs)
915{
916 struct ocfs2_xattr_entry *entry;
917 size_t name_len;
918 int i, cmp = 1;
919
920 if (name == NULL)
921 return -EINVAL;
922
923 name_len = strlen(name);
924 entry = xs->here;
925 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
926 cmp = name_index - ocfs2_xattr_get_type(entry);
927 if (!cmp)
928 cmp = name_len - entry->xe_name_len;
929 if (!cmp)
930 cmp = memcmp(name, (xs->base +
931 le16_to_cpu(entry->xe_name_offset)),
932 name_len);
933 if (cmp == 0)
934 break;
935 entry += 1;
936 }
937 xs->here = entry;
938
939 return cmp ? -ENODATA : 0;
940}
941
942static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800943 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800944 void *buffer,
945 size_t len)
946{
947 u32 cpos, p_cluster, num_clusters, bpc, clusters;
948 u64 blkno;
949 int i, ret = 0;
950 size_t cplen, blocksize;
951 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800952 struct ocfs2_extent_list *el;
953
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800954 el = &xv->xr_list;
955 clusters = le32_to_cpu(xv->xr_clusters);
956 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
957 blocksize = inode->i_sb->s_blocksize;
958
959 cpos = 0;
960 while (cpos < clusters) {
961 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +0800962 &num_clusters, el, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800963 if (ret) {
964 mlog_errno(ret);
965 goto out;
966 }
967
968 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
969 /* Copy ocfs2_xattr_value */
970 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -0800971 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
972 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800973 if (ret) {
974 mlog_errno(ret);
975 goto out;
976 }
977
978 cplen = len >= blocksize ? blocksize : len;
979 memcpy(buffer, bh->b_data, cplen);
980 len -= cplen;
981 buffer += cplen;
982
983 brelse(bh);
984 bh = NULL;
985 if (len == 0)
986 break;
987 }
988 cpos += num_clusters;
989 }
990out:
991 return ret;
992}
993
994static int ocfs2_xattr_ibody_get(struct inode *inode,
995 int name_index,
996 const char *name,
997 void *buffer,
998 size_t buffer_size,
999 struct ocfs2_xattr_search *xs)
1000{
1001 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1002 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +08001003 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001004 size_t size;
1005 int ret = 0;
1006
1007 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
1008 return -ENODATA;
1009
1010 xs->end = (void *)di + inode->i_sb->s_blocksize;
1011 xs->header = (struct ocfs2_xattr_header *)
1012 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1013 xs->base = (void *)xs->header;
1014 xs->here = xs->header->xh_entries;
1015
1016 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1017 if (ret)
1018 return ret;
1019 size = le64_to_cpu(xs->here->xe_value_size);
1020 if (buffer) {
1021 if (size > buffer_size)
1022 return -ERANGE;
1023 if (ocfs2_xattr_is_local(xs->here)) {
1024 memcpy(buffer, (void *)xs->base +
1025 le16_to_cpu(xs->here->xe_name_offset) +
1026 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
1027 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001028 xv = (struct ocfs2_xattr_value_root *)
1029 (xs->base + le16_to_cpu(
1030 xs->here->xe_name_offset) +
1031 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
1032 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001033 buffer, size);
1034 if (ret < 0) {
1035 mlog_errno(ret);
1036 return ret;
1037 }
1038 }
1039 }
1040
1041 return size;
1042}
1043
1044static int ocfs2_xattr_block_get(struct inode *inode,
1045 int name_index,
1046 const char *name,
1047 void *buffer,
1048 size_t buffer_size,
1049 struct ocfs2_xattr_search *xs)
1050{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001051 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001052 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001053 size_t size;
Subrata Modak44d8e4e2009-07-14 01:19:31 +05301054 int ret = -ENODATA, name_offset, name_len, i;
1055 int uninitialized_var(block_off);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001056
Joel Beckerba937122008-10-24 19:13:20 -07001057 xs->bucket = ocfs2_xattr_bucket_new(inode);
1058 if (!xs->bucket) {
1059 ret = -ENOMEM;
1060 mlog_errno(ret);
1061 goto cleanup;
1062 }
Tao Ma589dc262008-08-18 17:38:51 +08001063
Joel Becker54f443f2008-10-20 18:43:07 -07001064 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1065 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001066 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001067 goto cleanup;
1068 }
1069
Tiger Yang6c1e1832008-11-02 19:04:21 +08001070 if (xs->not_found) {
1071 ret = -ENODATA;
1072 goto cleanup;
1073 }
1074
Joel Becker54f443f2008-10-20 18:43:07 -07001075 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001076 size = le64_to_cpu(xs->here->xe_value_size);
1077 if (buffer) {
1078 ret = -ERANGE;
1079 if (size > buffer_size)
1080 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001081
1082 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1083 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1084 i = xs->here - xs->header->xh_entries;
1085
1086 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1087 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -07001088 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001089 i,
1090 &block_off,
1091 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001092 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001093 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001094 if (ocfs2_xattr_is_local(xs->here)) {
1095 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001096 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001097 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001098 xv = (struct ocfs2_xattr_value_root *)
1099 (xs->base + name_offset + name_len);
1100 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001101 buffer, size);
1102 if (ret < 0) {
1103 mlog_errno(ret);
1104 goto cleanup;
1105 }
1106 }
1107 }
1108 ret = size;
1109cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001110 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001111
Joel Becker54f443f2008-10-20 18:43:07 -07001112 brelse(xs->xattr_bh);
1113 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001114 return ret;
1115}
1116
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001117int ocfs2_xattr_get_nolock(struct inode *inode,
1118 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001119 int name_index,
1120 const char *name,
1121 void *buffer,
1122 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001123{
1124 int ret;
1125 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001126 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1127 struct ocfs2_xattr_search xis = {
1128 .not_found = -ENODATA,
1129 };
1130 struct ocfs2_xattr_search xbs = {
1131 .not_found = -ENODATA,
1132 };
1133
Tiger Yang8154da32008-08-18 17:11:46 +08001134 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1135 return -EOPNOTSUPP;
1136
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001137 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1138 ret = -ENODATA;
1139
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001140 xis.inode_bh = xbs.inode_bh = di_bh;
1141 di = (struct ocfs2_dinode *)di_bh->b_data;
1142
1143 down_read(&oi->ip_xattr_sem);
1144 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1145 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001146 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001147 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1148 buffer_size, &xbs);
1149 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001150
1151 return ret;
1152}
1153
1154/* ocfs2_xattr_get()
1155 *
1156 * Copy an extended attribute into the buffer provided.
1157 * Buffer is NULL to compute the size of buffer required.
1158 */
1159static int ocfs2_xattr_get(struct inode *inode,
1160 int name_index,
1161 const char *name,
1162 void *buffer,
1163 size_t buffer_size)
1164{
1165 int ret;
1166 struct buffer_head *di_bh = NULL;
1167
1168 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1169 if (ret < 0) {
1170 mlog_errno(ret);
1171 return ret;
1172 }
1173 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1174 name, buffer, buffer_size);
1175
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001176 ocfs2_inode_unlock(inode, 0);
1177
1178 brelse(di_bh);
1179
1180 return ret;
1181}
1182
1183static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001184 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001185 struct ocfs2_xattr_value_root *xv,
1186 const void *value,
1187 int value_len)
1188{
Tao Ma71d548a2008-12-05 06:20:54 +08001189 int ret = 0, i, cp_len;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001190 u16 blocksize = inode->i_sb->s_blocksize;
1191 u32 p_cluster, num_clusters;
1192 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1193 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1194 u64 blkno;
1195 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001196
1197 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1198
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001199 while (cpos < clusters) {
1200 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
Tao Ma1061f9c2009-08-18 11:41:57 +08001201 &num_clusters, &xv->xr_list,
1202 NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001203 if (ret) {
1204 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001205 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001206 }
1207
1208 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1209
1210 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker8cb471e2009-02-10 20:00:41 -08001211 ret = ocfs2_read_block(INODE_CACHE(inode), blkno,
1212 &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001213 if (ret) {
1214 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001215 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001216 }
1217
1218 ret = ocfs2_journal_access(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001219 INODE_CACHE(inode),
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001220 bh,
1221 OCFS2_JOURNAL_ACCESS_WRITE);
1222 if (ret < 0) {
1223 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001224 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001225 }
1226
1227 cp_len = value_len > blocksize ? blocksize : value_len;
1228 memcpy(bh->b_data, value, cp_len);
1229 value_len -= cp_len;
1230 value += cp_len;
1231 if (cp_len < blocksize)
1232 memset(bh->b_data + cp_len, 0,
1233 blocksize - cp_len);
1234
1235 ret = ocfs2_journal_dirty(handle, bh);
1236 if (ret < 0) {
1237 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001238 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001239 }
1240 brelse(bh);
1241 bh = NULL;
1242
1243 /*
1244 * XXX: do we need to empty all the following
1245 * blocks in this cluster?
1246 */
1247 if (!value_len)
1248 break;
1249 }
1250 cpos += num_clusters;
1251 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001252out:
1253 brelse(bh);
1254
1255 return ret;
1256}
1257
1258static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001259 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260 struct ocfs2_xattr_info *xi,
1261 struct ocfs2_xattr_search *xs,
Joel Becker512620f2008-12-09 15:58:35 -08001262 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001263 size_t offs)
1264{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001265 int ret = 0;
1266 size_t name_len = strlen(xi->name);
1267 void *val = xs->base + offs;
1268 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1269
Joel Becker0cf2f762009-02-12 16:41:25 -08001270 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001271 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001272 if (ret) {
1273 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001274 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001275 }
1276 /* Decrease xattr count */
1277 le16_add_cpu(&xs->header->xh_count, -1);
1278 /* Remove the xattr entry and tree root which has already be set*/
1279 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1280 memset(val, 0, size);
1281
Joel Becker512620f2008-12-09 15:58:35 -08001282 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001283 if (ret < 0)
1284 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001285out:
1286 return ret;
1287}
1288
1289static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001290 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001291 struct ocfs2_xattr_info *xi,
1292 struct ocfs2_xattr_search *xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001293 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001294 size_t offs)
1295{
Tao Ma85db90e2008-11-12 08:27:01 +08001296 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001297
Joel Becker0cf2f762009-02-12 16:41:25 -08001298 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh,
Joel Becker0c748e92008-12-09 15:46:15 -08001299 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001300 if (ret) {
1301 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001302 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001303 }
1304
1305 xs->here->xe_name_offset = cpu_to_le16(offs);
1306 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1307 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1308 ocfs2_xattr_set_local(xs->here, 1);
1309 else
1310 ocfs2_xattr_set_local(xs->here, 0);
1311 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1312
Joel Becker0c748e92008-12-09 15:46:15 -08001313 ret = ocfs2_journal_dirty(handle, vb->vb_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001314 if (ret < 0)
1315 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001316out:
1317 return ret;
1318}
1319
1320/*
1321 * ocfs2_xattr_set_value_outside()
1322 *
1323 * Set large size value in B tree.
1324 */
1325static int ocfs2_xattr_set_value_outside(struct inode *inode,
1326 struct ocfs2_xattr_info *xi,
1327 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001328 struct ocfs2_xattr_set_ctxt *ctxt,
Joel Becker512620f2008-12-09 15:58:35 -08001329 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001330 size_t offs)
1331{
1332 size_t name_len = strlen(xi->name);
1333 void *val = xs->base + offs;
1334 struct ocfs2_xattr_value_root *xv = NULL;
1335 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1336 int ret = 0;
1337
1338 memset(val, 0, size);
1339 memcpy(val, xi->name, name_len);
1340 xv = (struct ocfs2_xattr_value_root *)
1341 (val + OCFS2_XATTR_SIZE(name_len));
1342 xv->xr_clusters = 0;
1343 xv->xr_last_eb_blk = 0;
1344 xv->xr_list.l_tree_depth = 0;
1345 xv->xr_list.l_count = cpu_to_le16(1);
1346 xv->xr_list.l_next_free_rec = 0;
Joel Becker512620f2008-12-09 15:58:35 -08001347 vb->vb_xv = xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001348
Joel Becker512620f2008-12-09 15:58:35 -08001349 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001350 if (ret < 0) {
1351 mlog_errno(ret);
1352 return ret;
1353 }
Joel Becker512620f2008-12-09 15:58:35 -08001354 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001355 if (ret < 0) {
1356 mlog_errno(ret);
1357 return ret;
1358 }
Joel Becker512620f2008-12-09 15:58:35 -08001359 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb->vb_xv,
Tao Ma85db90e2008-11-12 08:27:01 +08001360 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001361 if (ret < 0)
1362 mlog_errno(ret);
1363
1364 return ret;
1365}
1366
1367/*
1368 * ocfs2_xattr_set_entry_local()
1369 *
1370 * Set, replace or remove extended attribute in local.
1371 */
1372static void ocfs2_xattr_set_entry_local(struct inode *inode,
1373 struct ocfs2_xattr_info *xi,
1374 struct ocfs2_xattr_search *xs,
1375 struct ocfs2_xattr_entry *last,
1376 size_t min_offs)
1377{
1378 size_t name_len = strlen(xi->name);
1379 int i;
1380
1381 if (xi->value && xs->not_found) {
1382 /* Insert the new xattr entry. */
1383 le16_add_cpu(&xs->header->xh_count, 1);
1384 ocfs2_xattr_set_type(last, xi->name_index);
1385 ocfs2_xattr_set_local(last, 1);
1386 last->xe_name_len = name_len;
1387 } else {
1388 void *first_val;
1389 void *val;
1390 size_t offs, size;
1391
1392 first_val = xs->base + min_offs;
1393 offs = le16_to_cpu(xs->here->xe_name_offset);
1394 val = xs->base + offs;
1395
1396 if (le64_to_cpu(xs->here->xe_value_size) >
1397 OCFS2_XATTR_INLINE_SIZE)
1398 size = OCFS2_XATTR_SIZE(name_len) +
1399 OCFS2_XATTR_ROOT_SIZE;
1400 else
1401 size = OCFS2_XATTR_SIZE(name_len) +
1402 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1403
1404 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1405 OCFS2_XATTR_SIZE(xi->value_len)) {
1406 /* The old and the new value have the
1407 same size. Just replace the value. */
1408 ocfs2_xattr_set_local(xs->here, 1);
1409 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1410 /* Clear value bytes. */
1411 memset(val + OCFS2_XATTR_SIZE(name_len),
1412 0,
1413 OCFS2_XATTR_SIZE(xi->value_len));
1414 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1415 xi->value,
1416 xi->value_len);
1417 return;
1418 }
1419 /* Remove the old name+value. */
1420 memmove(first_val + size, first_val, val - first_val);
1421 memset(first_val, 0, size);
1422 xs->here->xe_name_hash = 0;
1423 xs->here->xe_name_offset = 0;
1424 ocfs2_xattr_set_local(xs->here, 1);
1425 xs->here->xe_value_size = 0;
1426
1427 min_offs += size;
1428
1429 /* Adjust all value offsets. */
1430 last = xs->header->xh_entries;
1431 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1432 size_t o = le16_to_cpu(last->xe_name_offset);
1433
1434 if (o < offs)
1435 last->xe_name_offset = cpu_to_le16(o + size);
1436 last += 1;
1437 }
1438
1439 if (!xi->value) {
1440 /* Remove the old entry. */
1441 last -= 1;
1442 memmove(xs->here, xs->here + 1,
1443 (void *)last - (void *)xs->here);
1444 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1445 le16_add_cpu(&xs->header->xh_count, -1);
1446 }
1447 }
1448 if (xi->value) {
1449 /* Insert the new name+value. */
1450 size_t size = OCFS2_XATTR_SIZE(name_len) +
1451 OCFS2_XATTR_SIZE(xi->value_len);
1452 void *val = xs->base + min_offs - size;
1453
1454 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1455 memset(val, 0, size);
1456 memcpy(val, xi->name, name_len);
1457 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1458 xi->value,
1459 xi->value_len);
1460 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1461 ocfs2_xattr_set_local(xs->here, 1);
1462 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1463 }
1464
1465 return;
1466}
1467
1468/*
1469 * ocfs2_xattr_set_entry()
1470 *
1471 * Set extended attribute entry into inode or block.
1472 *
1473 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1474 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1475 * then set value in B tree with set_value_outside().
1476 */
1477static int ocfs2_xattr_set_entry(struct inode *inode,
1478 struct ocfs2_xattr_info *xi,
1479 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001480 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001481 int flag)
1482{
1483 struct ocfs2_xattr_entry *last;
1484 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1485 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1486 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1487 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001488 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001489 int free, i, ret;
1490 struct ocfs2_xattr_info xi_l = {
1491 .name_index = xi->name_index,
1492 .name = xi->name,
1493 .value = xi->value,
1494 .value_len = xi->value_len,
1495 };
Joel Becker512620f2008-12-09 15:58:35 -08001496 struct ocfs2_xattr_value_buf vb = {
1497 .vb_bh = xs->xattr_bh,
1498 .vb_access = ocfs2_journal_access_di,
1499 };
1500
1501 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1502 BUG_ON(xs->xattr_bh == xs->inode_bh);
1503 vb.vb_access = ocfs2_journal_access_xb;
1504 } else
1505 BUG_ON(xs->xattr_bh != xs->inode_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001506
1507 /* Compute min_offs, last and free space. */
1508 last = xs->header->xh_entries;
1509
1510 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1511 size_t offs = le16_to_cpu(last->xe_name_offset);
1512 if (offs < min_offs)
1513 min_offs = offs;
1514 last += 1;
1515 }
1516
Tiger Yang4442f512009-02-20 11:11:50 +08001517 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001518 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001519 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001520
1521 if (!xs->not_found) {
1522 size_t size = 0;
1523 if (ocfs2_xattr_is_local(xs->here))
1524 size = OCFS2_XATTR_SIZE(name_len) +
1525 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1526 else
1527 size = OCFS2_XATTR_SIZE(name_len) +
1528 OCFS2_XATTR_ROOT_SIZE;
1529 free += (size + sizeof(struct ocfs2_xattr_entry));
1530 }
1531 /* Check free space in inode or block */
1532 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1533 if (free < sizeof(struct ocfs2_xattr_entry) +
1534 OCFS2_XATTR_SIZE(name_len) +
1535 OCFS2_XATTR_ROOT_SIZE) {
1536 ret = -ENOSPC;
1537 goto out;
1538 }
1539 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1540 xi_l.value = (void *)&def_xv;
1541 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1542 } else if (xi->value) {
1543 if (free < sizeof(struct ocfs2_xattr_entry) +
1544 OCFS2_XATTR_SIZE(name_len) +
1545 OCFS2_XATTR_SIZE(xi->value_len)) {
1546 ret = -ENOSPC;
1547 goto out;
1548 }
1549 }
1550
1551 if (!xs->not_found) {
1552 /* For existing extended attribute */
1553 size_t size = OCFS2_XATTR_SIZE(name_len) +
1554 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1555 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1556 void *val = xs->base + offs;
1557
1558 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1559 /* Replace existing local xattr with tree root */
1560 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Joel Becker512620f2008-12-09 15:58:35 -08001561 ctxt, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001562 if (ret < 0)
1563 mlog_errno(ret);
1564 goto out;
1565 } else if (!ocfs2_xattr_is_local(xs->here)) {
1566 /* For existing xattr which has value outside */
Joel Becker512620f2008-12-09 15:58:35 -08001567 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1568 (val + OCFS2_XATTR_SIZE(name_len));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001569
1570 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1571 /*
1572 * If new value need set outside also,
1573 * first truncate old value to new value,
1574 * then set new value with set_value_outside().
1575 */
1576 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001577 &vb,
Tao Ma78f30c32008-11-12 08:27:00 +08001578 xi->value_len,
1579 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001580 if (ret < 0) {
1581 mlog_errno(ret);
1582 goto out;
1583 }
1584
Tao Ma85db90e2008-11-12 08:27:01 +08001585 ret = ocfs2_xattr_update_entry(inode,
1586 handle,
1587 xi,
1588 xs,
Joel Becker0c748e92008-12-09 15:46:15 -08001589 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001590 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001591 if (ret < 0) {
1592 mlog_errno(ret);
1593 goto out;
1594 }
1595
Tao Ma85db90e2008-11-12 08:27:01 +08001596 ret = __ocfs2_xattr_set_value_outside(inode,
1597 handle,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001598 vb.vb_xv,
Tao Ma85db90e2008-11-12 08:27:01 +08001599 xi->value,
1600 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001601 if (ret < 0)
1602 mlog_errno(ret);
1603 goto out;
1604 } else {
1605 /*
1606 * If new value need set in local,
1607 * just trucate old value to zero.
1608 */
1609 ret = ocfs2_xattr_value_truncate(inode,
Joel Beckerb3e5d372008-12-09 15:01:04 -08001610 &vb,
Tao Ma85db90e2008-11-12 08:27:01 +08001611 0,
1612 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001613 if (ret < 0)
1614 mlog_errno(ret);
1615 }
1616 }
1617 }
1618
Joel Becker0cf2f762009-02-12 16:41:25 -08001619 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001620 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001621 if (ret) {
1622 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001623 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001624 }
1625
1626 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001627 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
Joel Becker512620f2008-12-09 15:58:35 -08001628 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001629 if (ret) {
1630 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001631 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001632 }
1633 }
1634
1635 /*
1636 * Set value in local, include set tree root in local.
1637 * This is the first step for value size >INLINE_SIZE.
1638 */
1639 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1640
1641 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1642 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1643 if (ret < 0) {
1644 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001645 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001646 }
1647 }
1648
1649 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1650 (flag & OCFS2_INLINE_XATTR_FL)) {
1651 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1652 unsigned int xattrsize = osb->s_xattr_inline_size;
1653
1654 /*
1655 * Adjust extent record count or inline data size
1656 * to reserve space for extended attribute.
1657 */
1658 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1659 struct ocfs2_inline_data *idata = &di->id2.i_data;
1660 le16_add_cpu(&idata->id_count, -xattrsize);
1661 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1662 struct ocfs2_extent_list *el = &di->id2.i_list;
1663 le16_add_cpu(&el->l_count, -(xattrsize /
1664 sizeof(struct ocfs2_extent_rec)));
1665 }
1666 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1667 }
1668 /* Update xattr flag */
1669 spin_lock(&oi->ip_lock);
1670 oi->ip_dyn_features |= flag;
1671 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1672 spin_unlock(&oi->ip_lock);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001673
1674 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1675 if (ret < 0)
1676 mlog_errno(ret);
1677
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001678 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1679 /*
1680 * Set value outside in B tree.
1681 * This is the second step for value size > INLINE_SIZE.
1682 */
1683 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Joel Becker512620f2008-12-09 15:58:35 -08001684 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1685 &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001686 if (ret < 0) {
1687 int ret2;
1688
1689 mlog_errno(ret);
1690 /*
1691 * If set value outside failed, we have to clean
1692 * the junk tree root we have already set in local.
1693 */
Tao Ma85db90e2008-11-12 08:27:01 +08001694 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
Joel Becker512620f2008-12-09 15:58:35 -08001695 xi, xs, &vb, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001696 if (ret2 < 0)
1697 mlog_errno(ret2);
1698 }
1699 }
1700out:
1701 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001702}
1703
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001704static int ocfs2_remove_value_outside(struct inode*inode,
Joel Becker43119012008-12-09 16:24:43 -08001705 struct ocfs2_xattr_value_buf *vb,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001706 struct ocfs2_xattr_header *header)
1707{
1708 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001709 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1710 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1711
1712 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001713
Jan Karaa90714c2008-10-09 19:38:40 +02001714 ctxt.handle = ocfs2_start_trans(osb,
1715 ocfs2_remove_extent_credits(osb->sb));
Tao Ma85db90e2008-11-12 08:27:01 +08001716 if (IS_ERR(ctxt.handle)) {
1717 ret = PTR_ERR(ctxt.handle);
1718 mlog_errno(ret);
1719 goto out;
1720 }
1721
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001722 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1723 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1724
1725 if (!ocfs2_xattr_is_local(entry)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001726 void *val;
1727
1728 val = (void *)header +
1729 le16_to_cpu(entry->xe_name_offset);
Joel Becker43119012008-12-09 16:24:43 -08001730 vb->vb_xv = (struct ocfs2_xattr_value_root *)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001731 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Joel Becker43119012008-12-09 16:24:43 -08001732 ret = ocfs2_xattr_value_truncate(inode, vb, 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001733 if (ret < 0) {
1734 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001735 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001736 }
1737 }
1738 }
1739
Tao Ma85db90e2008-11-12 08:27:01 +08001740 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001741 ocfs2_schedule_truncate_log_flush(osb, 1);
1742 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001743out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001744 return ret;
1745}
1746
1747static int ocfs2_xattr_ibody_remove(struct inode *inode,
1748 struct buffer_head *di_bh)
1749{
1750
1751 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1752 struct ocfs2_xattr_header *header;
1753 int ret;
Joel Becker43119012008-12-09 16:24:43 -08001754 struct ocfs2_xattr_value_buf vb = {
1755 .vb_bh = di_bh,
1756 .vb_access = ocfs2_journal_access_di,
1757 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001758
1759 header = (struct ocfs2_xattr_header *)
1760 ((void *)di + inode->i_sb->s_blocksize -
1761 le16_to_cpu(di->i_xattr_inline_size));
1762
Joel Becker43119012008-12-09 16:24:43 -08001763 ret = ocfs2_remove_value_outside(inode, &vb, header);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001764
1765 return ret;
1766}
1767
1768static int ocfs2_xattr_block_remove(struct inode *inode,
1769 struct buffer_head *blk_bh)
1770{
1771 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001772 int ret = 0;
Joel Becker43119012008-12-09 16:24:43 -08001773 struct ocfs2_xattr_value_buf vb = {
1774 .vb_bh = blk_bh,
1775 .vb_access = ocfs2_journal_access_xb,
1776 };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001777
1778 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001779 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1780 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
Joel Becker43119012008-12-09 16:24:43 -08001781 ret = ocfs2_remove_value_outside(inode, &vb, header);
Tao Maa3944252008-08-18 17:38:54 +08001782 } else
1783 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001784
1785 return ret;
1786}
1787
Tao Ma08413892008-08-29 09:00:19 +08001788static int ocfs2_xattr_free_block(struct inode *inode,
1789 u64 block)
1790{
1791 struct inode *xb_alloc_inode;
1792 struct buffer_head *xb_alloc_bh = NULL;
1793 struct buffer_head *blk_bh = NULL;
1794 struct ocfs2_xattr_block *xb;
1795 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1796 handle_t *handle;
1797 int ret = 0;
1798 u64 blk, bg_blkno;
1799 u16 bit;
1800
Joel Becker4ae1d692008-11-13 14:49:18 -08001801 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001802 if (ret < 0) {
1803 mlog_errno(ret);
1804 goto out;
1805 }
1806
Tao Ma08413892008-08-29 09:00:19 +08001807 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1808 if (ret < 0) {
1809 mlog_errno(ret);
1810 goto out;
1811 }
1812
Joel Becker4ae1d692008-11-13 14:49:18 -08001813 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001814 blk = le64_to_cpu(xb->xb_blkno);
1815 bit = le16_to_cpu(xb->xb_suballoc_bit);
1816 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1817
1818 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1819 EXTENT_ALLOC_SYSTEM_INODE,
1820 le16_to_cpu(xb->xb_suballoc_slot));
1821 if (!xb_alloc_inode) {
1822 ret = -ENOMEM;
1823 mlog_errno(ret);
1824 goto out;
1825 }
1826 mutex_lock(&xb_alloc_inode->i_mutex);
1827
1828 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1829 if (ret < 0) {
1830 mlog_errno(ret);
1831 goto out_mutex;
1832 }
1833
1834 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1835 if (IS_ERR(handle)) {
1836 ret = PTR_ERR(handle);
1837 mlog_errno(ret);
1838 goto out_unlock;
1839 }
1840
1841 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1842 bit, bg_blkno, 1);
1843 if (ret < 0)
1844 mlog_errno(ret);
1845
1846 ocfs2_commit_trans(osb, handle);
1847out_unlock:
1848 ocfs2_inode_unlock(xb_alloc_inode, 1);
1849 brelse(xb_alloc_bh);
1850out_mutex:
1851 mutex_unlock(&xb_alloc_inode->i_mutex);
1852 iput(xb_alloc_inode);
1853out:
1854 brelse(blk_bh);
1855 return ret;
1856}
1857
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001858/*
1859 * ocfs2_xattr_remove()
1860 *
1861 * Free extended attribute resources associated with this inode.
1862 */
1863int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1864{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001865 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1866 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1867 handle_t *handle;
1868 int ret;
1869
Tiger Yang8154da32008-08-18 17:11:46 +08001870 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1871 return 0;
1872
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001873 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1874 return 0;
1875
1876 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1877 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1878 if (ret < 0) {
1879 mlog_errno(ret);
1880 goto out;
1881 }
1882 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001883
Tao Ma08413892008-08-29 09:00:19 +08001884 if (di->i_xattr_loc) {
1885 ret = ocfs2_xattr_free_block(inode,
1886 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001887 if (ret < 0) {
1888 mlog_errno(ret);
1889 goto out;
1890 }
1891 }
1892
1893 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1894 OCFS2_INODE_UPDATE_CREDITS);
1895 if (IS_ERR(handle)) {
1896 ret = PTR_ERR(handle);
1897 mlog_errno(ret);
1898 goto out;
1899 }
Joel Becker0cf2f762009-02-12 16:41:25 -08001900 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker84008972008-12-09 16:11:49 -08001901 OCFS2_JOURNAL_ACCESS_WRITE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001902 if (ret) {
1903 mlog_errno(ret);
1904 goto out_commit;
1905 }
1906
Tao Ma08413892008-08-29 09:00:19 +08001907 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001908
1909 spin_lock(&oi->ip_lock);
1910 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1911 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1912 spin_unlock(&oi->ip_lock);
1913
1914 ret = ocfs2_journal_dirty(handle, di_bh);
1915 if (ret < 0)
1916 mlog_errno(ret);
1917out_commit:
1918 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1919out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001920 return ret;
1921}
1922
1923static int ocfs2_xattr_has_space_inline(struct inode *inode,
1924 struct ocfs2_dinode *di)
1925{
1926 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1927 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1928 int free;
1929
1930 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1931 return 0;
1932
1933 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1934 struct ocfs2_inline_data *idata = &di->id2.i_data;
1935 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1936 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1937 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1938 le64_to_cpu(di->i_size);
1939 } else {
1940 struct ocfs2_extent_list *el = &di->id2.i_list;
1941 free = (le16_to_cpu(el->l_count) -
1942 le16_to_cpu(el->l_next_free_rec)) *
1943 sizeof(struct ocfs2_extent_rec);
1944 }
1945 if (free >= xattrsize)
1946 return 1;
1947
1948 return 0;
1949}
1950
1951/*
1952 * ocfs2_xattr_ibody_find()
1953 *
1954 * Find extended attribute in inode block and
1955 * fill search info into struct ocfs2_xattr_search.
1956 */
1957static int ocfs2_xattr_ibody_find(struct inode *inode,
1958 int name_index,
1959 const char *name,
1960 struct ocfs2_xattr_search *xs)
1961{
1962 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1963 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1964 int ret;
1965 int has_space = 0;
1966
1967 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1968 return 0;
1969
1970 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1971 down_read(&oi->ip_alloc_sem);
1972 has_space = ocfs2_xattr_has_space_inline(inode, di);
1973 up_read(&oi->ip_alloc_sem);
1974 if (!has_space)
1975 return 0;
1976 }
1977
1978 xs->xattr_bh = xs->inode_bh;
1979 xs->end = (void *)di + inode->i_sb->s_blocksize;
1980 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1981 xs->header = (struct ocfs2_xattr_header *)
1982 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1983 else
1984 xs->header = (struct ocfs2_xattr_header *)
1985 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1986 xs->base = (void *)xs->header;
1987 xs->here = xs->header->xh_entries;
1988
1989 /* Find the named attribute. */
1990 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1991 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1992 if (ret && ret != -ENODATA)
1993 return ret;
1994 xs->not_found = ret;
1995 }
1996
1997 return 0;
1998}
1999
2000/*
2001 * ocfs2_xattr_ibody_set()
2002 *
2003 * Set, replace or remove an extended attribute into inode block.
2004 *
2005 */
2006static int ocfs2_xattr_ibody_set(struct inode *inode,
2007 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002008 struct ocfs2_xattr_search *xs,
2009 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002010{
2011 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2012 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2013 int ret;
2014
2015 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2016 return -ENOSPC;
2017
2018 down_write(&oi->ip_alloc_sem);
2019 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2020 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2021 ret = -ENOSPC;
2022 goto out;
2023 }
2024 }
2025
Tao Ma78f30c32008-11-12 08:27:00 +08002026 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002027 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
2028out:
2029 up_write(&oi->ip_alloc_sem);
2030
2031 return ret;
2032}
2033
2034/*
2035 * ocfs2_xattr_block_find()
2036 *
2037 * Find extended attribute in external block and
2038 * fill search info into struct ocfs2_xattr_search.
2039 */
2040static int ocfs2_xattr_block_find(struct inode *inode,
2041 int name_index,
2042 const char *name,
2043 struct ocfs2_xattr_search *xs)
2044{
2045 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2046 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002047 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002048 int ret = 0;
2049
2050 if (!di->i_xattr_loc)
2051 return ret;
2052
Joel Becker4ae1d692008-11-13 14:49:18 -08002053 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2054 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002055 if (ret < 0) {
2056 mlog_errno(ret);
2057 return ret;
2058 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002059
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002060 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002061 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002062
Tao Ma589dc262008-08-18 17:38:51 +08002063 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2064 xs->header = &xb->xb_attrs.xb_header;
2065 xs->base = (void *)xs->header;
2066 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2067 xs->here = xs->header->xh_entries;
2068
2069 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2070 } else
2071 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2072 name_index,
2073 name, xs);
2074
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002075 if (ret && ret != -ENODATA) {
2076 xs->xattr_bh = NULL;
2077 goto cleanup;
2078 }
2079 xs->not_found = ret;
2080 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002081cleanup:
2082 brelse(blk_bh);
2083
2084 return ret;
2085}
2086
2087/*
2088 * ocfs2_xattr_block_set()
2089 *
2090 * Set, replace or remove an extended attribute into external block.
2091 *
2092 */
2093static int ocfs2_xattr_block_set(struct inode *inode,
2094 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002095 struct ocfs2_xattr_search *xs,
2096 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002097{
2098 struct buffer_head *new_bh = NULL;
2099 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2100 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08002101 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002102 struct ocfs2_xattr_block *xblk = NULL;
2103 u16 suballoc_bit_start;
2104 u32 num_got;
2105 u64 first_blkno;
2106 int ret;
2107
2108 if (!xs->xattr_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002109 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
2110 xs->inode_bh,
Joel Becker84008972008-12-09 16:11:49 -08002111 OCFS2_JOURNAL_ACCESS_CREATE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002112 if (ret < 0) {
2113 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002114 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002115 }
2116
Tao Ma78f30c32008-11-12 08:27:00 +08002117 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002118 &suballoc_bit_start, &num_got,
2119 &first_blkno);
2120 if (ret < 0) {
2121 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002122 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002123 }
2124
2125 new_bh = sb_getblk(inode->i_sb, first_blkno);
Joel Becker8cb471e2009-02-10 20:00:41 -08002126 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002127
Joel Becker0cf2f762009-02-12 16:41:25 -08002128 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode),
2129 new_bh,
Joel Becker84008972008-12-09 16:11:49 -08002130 OCFS2_JOURNAL_ACCESS_CREATE);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002131 if (ret < 0) {
2132 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002133 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002134 }
2135
2136 /* Initialize ocfs2_xattr_block */
2137 xs->xattr_bh = new_bh;
2138 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2139 memset(xblk, 0, inode->i_sb->s_blocksize);
2140 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2141 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2142 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2143 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2144 xblk->xb_blkno = cpu_to_le64(first_blkno);
2145
2146 xs->header = &xblk->xb_attrs.xb_header;
2147 xs->base = (void *)xs->header;
2148 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2149 xs->here = xs->header->xh_entries;
2150
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002151 ret = ocfs2_journal_dirty(handle, new_bh);
2152 if (ret < 0) {
2153 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002154 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002155 }
2156 di->i_xattr_loc = cpu_to_le64(first_blkno);
Tao Ma85db90e2008-11-12 08:27:01 +08002157 ocfs2_journal_dirty(handle, xs->inode_bh);
Tao Ma01225592008-08-18 17:38:53 +08002158 } else
2159 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2160
2161 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2162 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002163 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2164 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002165 if (!ret || ret != -ENOSPC)
2166 goto end;
2167
Tao Ma78f30c32008-11-12 08:27:00 +08002168 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002169 if (ret)
2170 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002171 }
2172
Tao Ma78f30c32008-11-12 08:27:00 +08002173 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002174
2175end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002176
2177 return ret;
2178}
2179
Tao Ma78f30c32008-11-12 08:27:00 +08002180/* Check whether the new xattr can be inserted into the inode. */
2181static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2182 struct ocfs2_xattr_info *xi,
2183 struct ocfs2_xattr_search *xs)
2184{
2185 u64 value_size;
2186 struct ocfs2_xattr_entry *last;
2187 int free, i;
2188 size_t min_offs = xs->end - xs->base;
2189
2190 if (!xs->header)
2191 return 0;
2192
2193 last = xs->header->xh_entries;
2194
2195 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2196 size_t offs = le16_to_cpu(last->xe_name_offset);
2197 if (offs < min_offs)
2198 min_offs = offs;
2199 last += 1;
2200 }
2201
Tiger Yang4442f512009-02-20 11:11:50 +08002202 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP;
Tao Ma78f30c32008-11-12 08:27:00 +08002203 if (free < 0)
2204 return 0;
2205
2206 BUG_ON(!xs->not_found);
2207
2208 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2209 value_size = OCFS2_XATTR_ROOT_SIZE;
2210 else
2211 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2212
2213 if (free >= sizeof(struct ocfs2_xattr_entry) +
2214 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2215 return 1;
2216
2217 return 0;
2218}
2219
2220static int ocfs2_calc_xattr_set_need(struct inode *inode,
2221 struct ocfs2_dinode *di,
2222 struct ocfs2_xattr_info *xi,
2223 struct ocfs2_xattr_search *xis,
2224 struct ocfs2_xattr_search *xbs,
2225 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002226 int *meta_need,
2227 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002228{
2229 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002230 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002231 struct buffer_head *bh = NULL;
2232 struct ocfs2_xattr_block *xb = NULL;
2233 struct ocfs2_xattr_entry *xe = NULL;
2234 struct ocfs2_xattr_value_root *xv = NULL;
2235 char *base = NULL;
2236 int name_offset, name_len = 0;
2237 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2238 xi->value_len);
2239 u64 value_size;
2240
Tao Ma71d548a2008-12-05 06:20:54 +08002241 /*
2242 * Calculate the clusters we need to write.
2243 * No matter whether we replace an old one or add a new one,
2244 * we need this for writing.
2245 */
2246 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2247 credits += new_clusters *
2248 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2249
Tao Ma78f30c32008-11-12 08:27:00 +08002250 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002251 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2252
2253 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002254 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002255 credits += ocfs2_calc_extend_credits(inode->i_sb,
2256 &def_xv.xv.xr_list,
2257 new_clusters);
2258 }
Tao Ma78f30c32008-11-12 08:27:00 +08002259
2260 goto meta_guess;
2261 }
2262
2263 if (!xis->not_found) {
2264 xe = xis->here;
2265 name_offset = le16_to_cpu(xe->xe_name_offset);
2266 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2267 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002268 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002269 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002270 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002271 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2272 xe = xbs->here;
2273 name_offset = le16_to_cpu(xe->xe_name_offset);
2274 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2275 i = xbs->here - xbs->header->xh_entries;
2276 old_in_xb = 1;
2277
2278 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2279 ret = ocfs2_xattr_bucket_get_name_value(inode,
2280 bucket_xh(xbs->bucket),
2281 i, &block_off,
2282 &name_offset);
2283 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002284 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2285 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002286 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002287 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2288 }
2289 }
2290
2291 /*
2292 * delete a xattr doesn't need metadata and cluster allocation.
2293 * so just calculate the credits and return.
2294 *
2295 * The credits for removing the value tree will be extended
2296 * by ocfs2_remove_extent itself.
2297 */
2298 if (!xi->value) {
2299 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002300 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002301
2302 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002303 }
2304
2305 /* do cluster allocation guess first. */
2306 value_size = le64_to_cpu(xe->xe_value_size);
2307
2308 if (old_in_xb) {
2309 /*
2310 * In xattr set, we always try to set the xe in inode first,
2311 * so if it can be inserted into inode successfully, the old
2312 * one will be removed from the xattr block, and this xattr
2313 * will be inserted into inode as a new xattr in inode.
2314 */
2315 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2316 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002317 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002318 OCFS2_INODE_UPDATE_CREDITS;
2319 if (!ocfs2_xattr_is_local(xe))
2320 credits += ocfs2_calc_extend_credits(
2321 inode->i_sb,
2322 &def_xv.xv.xr_list,
2323 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002324 goto out;
2325 }
2326 }
2327
2328 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2329 /* the new values will be stored outside. */
2330 u32 old_clusters = 0;
2331
2332 if (!ocfs2_xattr_is_local(xe)) {
2333 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2334 value_size);
2335 xv = (struct ocfs2_xattr_value_root *)
2336 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002337 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002338 } else
2339 xv = &def_xv.xv;
2340
Tao Ma85db90e2008-11-12 08:27:01 +08002341 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002342 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002343 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002344 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002345 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2346 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002347 credits += ocfs2_calc_extend_credits(inode->i_sb,
2348 &xv->xr_list,
2349 new_clusters -
2350 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002351 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2352 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002353 }
2354 } else {
2355 /*
2356 * Now the new value will be stored inside. So if the new
2357 * value is smaller than the size of value root or the old
2358 * value, we don't need any allocation, otherwise we have
2359 * to guess metadata allocation.
2360 */
2361 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2362 (!ocfs2_xattr_is_local(xe) &&
2363 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2364 goto out;
2365 }
2366
2367meta_guess:
2368 /* calculate metadata allocation. */
2369 if (di->i_xattr_loc) {
2370 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002371 ret = ocfs2_read_xattr_block(inode,
2372 le64_to_cpu(di->i_xattr_loc),
2373 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002374 if (ret) {
2375 mlog_errno(ret);
2376 goto out;
2377 }
2378
2379 xb = (struct ocfs2_xattr_block *)bh->b_data;
2380 } else
2381 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2382
Tao Ma90cb5462008-12-05 06:20:56 +08002383 /*
2384 * If there is already an xattr tree, good, we can calculate
2385 * like other b-trees. Otherwise we may have the chance of
2386 * create a tree, the credit calculation is borrowed from
2387 * ocfs2_calc_extend_credits with root_el = NULL. And the
2388 * new tree will be cluster based, so no meta is needed.
2389 */
Tao Ma78f30c32008-11-12 08:27:00 +08002390 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2391 struct ocfs2_extent_list *el =
2392 &xb->xb_attrs.xb_root.xt_list;
2393 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002394 credits += ocfs2_calc_extend_credits(inode->i_sb,
2395 el, 1);
Tao Ma90cb5462008-12-05 06:20:56 +08002396 } else
2397 credits += OCFS2_SUBALLOC_ALLOC + 1;
Tao Ma78f30c32008-11-12 08:27:00 +08002398
2399 /*
2400 * This cluster will be used either for new bucket or for
2401 * new xattr block.
2402 * If the cluster size is the same as the bucket size, one
2403 * more is needed since we may need to extend the bucket
2404 * also.
2405 */
2406 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002407 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002408 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002409 OCFS2_SB(inode->i_sb)->s_clustersize) {
2410 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002411 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002412 }
2413 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002414 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002415 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2416 }
Tao Ma78f30c32008-11-12 08:27:00 +08002417out:
2418 if (clusters_need)
2419 *clusters_need = clusters_add;
2420 if (meta_need)
2421 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002422 if (credits_need)
2423 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002424 brelse(bh);
2425 return ret;
2426}
2427
2428static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2429 struct ocfs2_dinode *di,
2430 struct ocfs2_xattr_info *xi,
2431 struct ocfs2_xattr_search *xis,
2432 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002433 struct ocfs2_xattr_set_ctxt *ctxt,
2434 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002435{
2436 int clusters_add, meta_add, ret;
2437 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2438
2439 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2440
2441 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2442
2443 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002444 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002445 if (ret) {
2446 mlog_errno(ret);
2447 return ret;
2448 }
2449
Tao Ma85db90e2008-11-12 08:27:01 +08002450 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2451 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002452
2453 if (meta_add) {
2454 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2455 &ctxt->meta_ac);
2456 if (ret) {
2457 mlog_errno(ret);
2458 goto out;
2459 }
2460 }
2461
2462 if (clusters_add) {
2463 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2464 if (ret)
2465 mlog_errno(ret);
2466 }
2467out:
2468 if (ret) {
2469 if (ctxt->meta_ac) {
2470 ocfs2_free_alloc_context(ctxt->meta_ac);
2471 ctxt->meta_ac = NULL;
2472 }
2473
2474 /*
2475 * We cannot have an error and a non null ctxt->data_ac.
2476 */
2477 }
2478
2479 return ret;
2480}
2481
Tao Ma85db90e2008-11-12 08:27:01 +08002482static int __ocfs2_xattr_set_handle(struct inode *inode,
2483 struct ocfs2_dinode *di,
2484 struct ocfs2_xattr_info *xi,
2485 struct ocfs2_xattr_search *xis,
2486 struct ocfs2_xattr_search *xbs,
2487 struct ocfs2_xattr_set_ctxt *ctxt)
2488{
Tao Ma9f868f12008-11-19 16:48:42 +08002489 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002490
2491 if (!xi->value) {
2492 /* Remove existing extended attribute */
2493 if (!xis->not_found)
2494 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2495 else if (!xbs->not_found)
2496 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2497 } else {
2498 /* We always try to set extended attribute into inode first*/
2499 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2500 if (!ret && !xbs->not_found) {
2501 /*
2502 * If succeed and that extended attribute existing in
2503 * external block, then we will remove it.
2504 */
2505 xi->value = NULL;
2506 xi->value_len = 0;
2507
Tao Ma9f868f12008-11-19 16:48:42 +08002508 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002509 xis->not_found = -ENODATA;
2510 ret = ocfs2_calc_xattr_set_need(inode,
2511 di,
2512 xi,
2513 xis,
2514 xbs,
2515 NULL,
2516 NULL,
2517 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002518 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002519 if (ret) {
2520 mlog_errno(ret);
2521 goto out;
2522 }
2523
2524 ret = ocfs2_extend_trans(ctxt->handle, credits +
2525 ctxt->handle->h_buffer_credits);
2526 if (ret) {
2527 mlog_errno(ret);
2528 goto out;
2529 }
2530 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2531 } else if (ret == -ENOSPC) {
2532 if (di->i_xattr_loc && !xbs->xattr_bh) {
2533 ret = ocfs2_xattr_block_find(inode,
2534 xi->name_index,
2535 xi->name, xbs);
2536 if (ret)
2537 goto out;
2538
Tao Ma9f868f12008-11-19 16:48:42 +08002539 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002540 xis->not_found = -ENODATA;
2541 ret = ocfs2_calc_xattr_set_need(inode,
2542 di,
2543 xi,
2544 xis,
2545 xbs,
2546 NULL,
2547 NULL,
2548 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002549 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002550 if (ret) {
2551 mlog_errno(ret);
2552 goto out;
2553 }
2554
2555 ret = ocfs2_extend_trans(ctxt->handle, credits +
2556 ctxt->handle->h_buffer_credits);
2557 if (ret) {
2558 mlog_errno(ret);
2559 goto out;
2560 }
2561 }
2562 /*
2563 * If no space in inode, we will set extended attribute
2564 * into external block.
2565 */
2566 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2567 if (ret)
2568 goto out;
2569 if (!xis->not_found) {
2570 /*
2571 * If succeed and that extended attribute
2572 * existing in inode, we will remove it.
2573 */
2574 xi->value = NULL;
2575 xi->value_len = 0;
2576 xbs->not_found = -ENODATA;
2577 ret = ocfs2_calc_xattr_set_need(inode,
2578 di,
2579 xi,
2580 xis,
2581 xbs,
2582 NULL,
2583 NULL,
2584 &credits);
2585 if (ret) {
2586 mlog_errno(ret);
2587 goto out;
2588 }
2589
2590 ret = ocfs2_extend_trans(ctxt->handle, credits +
2591 ctxt->handle->h_buffer_credits);
2592 if (ret) {
2593 mlog_errno(ret);
2594 goto out;
2595 }
2596 ret = ocfs2_xattr_ibody_set(inode, xi,
2597 xis, ctxt);
2598 }
2599 }
2600 }
2601
Tao Ma4b3f6202008-12-05 06:20:55 +08002602 if (!ret) {
2603 /* Update inode ctime. */
Joel Becker0cf2f762009-02-12 16:41:25 -08002604 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
Tao Ma89a907a2009-02-17 04:39:28 +08002605 xis->inode_bh,
2606 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma4b3f6202008-12-05 06:20:55 +08002607 if (ret) {
2608 mlog_errno(ret);
2609 goto out;
2610 }
2611
2612 inode->i_ctime = CURRENT_TIME;
2613 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
2614 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
2615 ocfs2_journal_dirty(ctxt->handle, xis->inode_bh);
2616 }
Tao Ma85db90e2008-11-12 08:27:01 +08002617out:
2618 return ret;
2619}
2620
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002621/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002622 * This function only called duing creating inode
2623 * for init security/acl xattrs of the new inode.
Tiger Yang008aafa2008-12-09 16:43:08 +08002624 * All transanction credits have been reserved in mknod.
Tiger Yang6c3faba2008-11-14 11:16:03 +08002625 */
2626int ocfs2_xattr_set_handle(handle_t *handle,
2627 struct inode *inode,
2628 struct buffer_head *di_bh,
2629 int name_index,
2630 const char *name,
2631 const void *value,
2632 size_t value_len,
2633 int flags,
2634 struct ocfs2_alloc_context *meta_ac,
2635 struct ocfs2_alloc_context *data_ac)
2636{
2637 struct ocfs2_dinode *di;
2638 int ret;
2639
2640 struct ocfs2_xattr_info xi = {
2641 .name_index = name_index,
2642 .name = name,
2643 .value = value,
2644 .value_len = value_len,
2645 };
2646
2647 struct ocfs2_xattr_search xis = {
2648 .not_found = -ENODATA,
2649 };
2650
2651 struct ocfs2_xattr_search xbs = {
2652 .not_found = -ENODATA,
2653 };
2654
2655 struct ocfs2_xattr_set_ctxt ctxt = {
2656 .handle = handle,
2657 .meta_ac = meta_ac,
2658 .data_ac = data_ac,
2659 };
2660
2661 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2662 return -EOPNOTSUPP;
2663
Tiger Yang008aafa2008-12-09 16:43:08 +08002664 /*
2665 * In extreme situation, may need xattr bucket when
2666 * block size is too small. And we have already reserved
2667 * the credits for bucket in mknod.
2668 */
2669 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) {
2670 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2671 if (!xbs.bucket) {
2672 mlog_errno(-ENOMEM);
2673 return -ENOMEM;
2674 }
2675 }
2676
Tiger Yang6c3faba2008-11-14 11:16:03 +08002677 xis.inode_bh = xbs.inode_bh = di_bh;
2678 di = (struct ocfs2_dinode *)di_bh->b_data;
2679
2680 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2681
2682 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2683 if (ret)
2684 goto cleanup;
2685 if (xis.not_found) {
2686 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2687 if (ret)
2688 goto cleanup;
2689 }
2690
2691 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2692
2693cleanup:
2694 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2695 brelse(xbs.xattr_bh);
Tiger Yang008aafa2008-12-09 16:43:08 +08002696 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yang6c3faba2008-11-14 11:16:03 +08002697
2698 return ret;
2699}
2700
2701/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002702 * ocfs2_xattr_set()
2703 *
2704 * Set, replace or remove an extended attribute for this inode.
2705 * value is NULL to remove an existing extended attribute, else either
2706 * create or replace an extended attribute.
2707 */
2708int ocfs2_xattr_set(struct inode *inode,
2709 int name_index,
2710 const char *name,
2711 const void *value,
2712 size_t value_len,
2713 int flags)
2714{
2715 struct buffer_head *di_bh = NULL;
2716 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002717 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002718 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002719 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002720 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002721
2722 struct ocfs2_xattr_info xi = {
2723 .name_index = name_index,
2724 .name = name,
2725 .value = value,
2726 .value_len = value_len,
2727 };
2728
2729 struct ocfs2_xattr_search xis = {
2730 .not_found = -ENODATA,
2731 };
2732
2733 struct ocfs2_xattr_search xbs = {
2734 .not_found = -ENODATA,
2735 };
2736
Tiger Yang8154da32008-08-18 17:11:46 +08002737 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2738 return -EOPNOTSUPP;
2739
Joel Beckerba937122008-10-24 19:13:20 -07002740 /*
2741 * Only xbs will be used on indexed trees. xis doesn't need a
2742 * bucket.
2743 */
2744 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2745 if (!xbs.bucket) {
2746 mlog_errno(-ENOMEM);
2747 return -ENOMEM;
2748 }
2749
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002750 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2751 if (ret < 0) {
2752 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002753 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002754 }
2755 xis.inode_bh = xbs.inode_bh = di_bh;
2756 di = (struct ocfs2_dinode *)di_bh->b_data;
2757
2758 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2759 /*
2760 * Scan inode and external block to find the same name
2761 * extended attribute and collect search infomation.
2762 */
2763 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2764 if (ret)
2765 goto cleanup;
2766 if (xis.not_found) {
2767 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2768 if (ret)
2769 goto cleanup;
2770 }
2771
2772 if (xis.not_found && xbs.not_found) {
2773 ret = -ENODATA;
2774 if (flags & XATTR_REPLACE)
2775 goto cleanup;
2776 ret = 0;
2777 if (!value)
2778 goto cleanup;
2779 } else {
2780 ret = -EEXIST;
2781 if (flags & XATTR_CREATE)
2782 goto cleanup;
2783 }
2784
Tao Ma85db90e2008-11-12 08:27:01 +08002785
2786 mutex_lock(&tl_inode->i_mutex);
2787
2788 if (ocfs2_truncate_log_needs_flush(osb)) {
2789 ret = __ocfs2_flush_truncate_log(osb);
2790 if (ret < 0) {
2791 mutex_unlock(&tl_inode->i_mutex);
2792 mlog_errno(ret);
2793 goto cleanup;
2794 }
2795 }
2796 mutex_unlock(&tl_inode->i_mutex);
2797
2798 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2799 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002800 if (ret) {
2801 mlog_errno(ret);
2802 goto cleanup;
2803 }
2804
Tao Ma4b3f6202008-12-05 06:20:55 +08002805 /* we need to update inode's ctime field, so add credit for it. */
2806 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma85db90e2008-11-12 08:27:01 +08002807 ctxt.handle = ocfs2_start_trans(osb, credits);
2808 if (IS_ERR(ctxt.handle)) {
2809 ret = PTR_ERR(ctxt.handle);
2810 mlog_errno(ret);
2811 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002812 }
Tao Ma85db90e2008-11-12 08:27:01 +08002813
2814 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2815
2816 ocfs2_commit_trans(osb, ctxt.handle);
2817
Tao Ma78f30c32008-11-12 08:27:00 +08002818 if (ctxt.data_ac)
2819 ocfs2_free_alloc_context(ctxt.data_ac);
2820 if (ctxt.meta_ac)
2821 ocfs2_free_alloc_context(ctxt.meta_ac);
2822 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2823 ocfs2_schedule_truncate_log_flush(osb, 1);
2824 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002825cleanup:
2826 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2827 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002828cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002829 brelse(di_bh);
2830 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002831 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002832
2833 return ret;
2834}
2835
Tao Ma0c044f02008-08-18 17:38:50 +08002836/*
2837 * Find the xattr extent rec which may contains name_hash.
2838 * e_cpos will be the first name hash of the xattr rec.
2839 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2840 */
2841static int ocfs2_xattr_get_rec(struct inode *inode,
2842 u32 name_hash,
2843 u64 *p_blkno,
2844 u32 *e_cpos,
2845 u32 *num_clusters,
2846 struct ocfs2_extent_list *el)
2847{
2848 int ret = 0, i;
2849 struct buffer_head *eb_bh = NULL;
2850 struct ocfs2_extent_block *eb;
2851 struct ocfs2_extent_rec *rec = NULL;
2852 u64 e_blkno = 0;
2853
2854 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002855 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, name_hash,
2856 &eb_bh);
Tao Ma0c044f02008-08-18 17:38:50 +08002857 if (ret) {
2858 mlog_errno(ret);
2859 goto out;
2860 }
2861
2862 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2863 el = &eb->h_list;
2864
2865 if (el->l_tree_depth) {
2866 ocfs2_error(inode->i_sb,
2867 "Inode %lu has non zero tree depth in "
2868 "xattr tree block %llu\n", inode->i_ino,
2869 (unsigned long long)eb_bh->b_blocknr);
2870 ret = -EROFS;
2871 goto out;
2872 }
2873 }
2874
2875 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2876 rec = &el->l_recs[i];
2877
2878 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2879 e_blkno = le64_to_cpu(rec->e_blkno);
2880 break;
2881 }
2882 }
2883
2884 if (!e_blkno) {
2885 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2886 "record (%u, %u, 0) in xattr", inode->i_ino,
2887 le32_to_cpu(rec->e_cpos),
2888 ocfs2_rec_clusters(el, rec));
2889 ret = -EROFS;
2890 goto out;
2891 }
2892
2893 *p_blkno = le64_to_cpu(rec->e_blkno);
2894 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2895 if (e_cpos)
2896 *e_cpos = le32_to_cpu(rec->e_cpos);
2897out:
2898 brelse(eb_bh);
2899 return ret;
2900}
2901
2902typedef int (xattr_bucket_func)(struct inode *inode,
2903 struct ocfs2_xattr_bucket *bucket,
2904 void *para);
2905
Tao Ma589dc262008-08-18 17:38:51 +08002906static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002907 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002908 int name_index,
2909 const char *name,
2910 u32 name_hash,
2911 u16 *xe_index,
2912 int *found)
2913{
2914 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002915 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002916 size_t name_len = strlen(name);
2917 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002918 char *xe_name;
2919
2920 /*
2921 * We don't use binary search in the bucket because there
2922 * may be multiple entries with the same name hash.
2923 */
2924 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2925 xe = &xh->xh_entries[i];
2926
2927 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2928 continue;
2929 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2930 break;
2931
2932 cmp = name_index - ocfs2_xattr_get_type(xe);
2933 if (!cmp)
2934 cmp = name_len - xe->xe_name_len;
2935 if (cmp)
2936 continue;
2937
2938 ret = ocfs2_xattr_bucket_get_name_value(inode,
2939 xh,
2940 i,
2941 &block_off,
2942 &new_offset);
2943 if (ret) {
2944 mlog_errno(ret);
2945 break;
2946 }
2947
Joel Becker970e4932008-11-13 14:49:19 -08002948
Joel Beckere2356a32008-10-27 15:01:54 -07002949 xe_name = bucket_block(bucket, block_off) + new_offset;
2950 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002951 *xe_index = i;
2952 *found = 1;
2953 ret = 0;
2954 break;
2955 }
2956 }
2957
2958 return ret;
2959}
2960
2961/*
2962 * Find the specified xattr entry in a series of buckets.
2963 * This series start from p_blkno and last for num_clusters.
2964 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2965 * the num of the valid buckets.
2966 *
2967 * Return the buffer_head this xattr should reside in. And if the xattr's
2968 * hash is in the gap of 2 buckets, return the lower bucket.
2969 */
2970static int ocfs2_xattr_bucket_find(struct inode *inode,
2971 int name_index,
2972 const char *name,
2973 u32 name_hash,
2974 u64 p_blkno,
2975 u32 first_hash,
2976 u32 num_clusters,
2977 struct ocfs2_xattr_search *xs)
2978{
2979 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002980 struct ocfs2_xattr_header *xh = NULL;
2981 struct ocfs2_xattr_entry *xe = NULL;
2982 u16 index = 0;
2983 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2984 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002985 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002986 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002987 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002988
Joel Beckere2356a32008-10-27 15:01:54 -07002989 search = ocfs2_xattr_bucket_new(inode);
2990 if (!search) {
2991 ret = -ENOMEM;
2992 mlog_errno(ret);
2993 goto out;
2994 }
2995
2996 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002997 if (ret) {
2998 mlog_errno(ret);
2999 goto out;
3000 }
3001
Joel Beckere2356a32008-10-27 15:01:54 -07003002 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003003 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08003004 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07003005 ocfs2_xattr_bucket_relse(search);
3006
Tao Ma589dc262008-08-18 17:38:51 +08003007 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08003008 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07003009 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08003010 if (ret) {
3011 mlog_errno(ret);
3012 goto out;
3013 }
3014
Joel Beckere2356a32008-10-27 15:01:54 -07003015 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08003016 xe = &xh->xh_entries[0];
3017 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
3018 high_bucket = bucket - 1;
3019 continue;
3020 }
3021
3022 /*
3023 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08003024 * bucket is larger than the search one. for an empty
3025 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08003026 */
Tao Ma5a095612008-09-19 22:17:41 +08003027 if (xh->xh_count)
3028 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
3029
Tao Ma589dc262008-08-18 17:38:51 +08003030 last_hash = le32_to_cpu(xe->xe_name_hash);
3031
Joel Beckere2356a32008-10-27 15:01:54 -07003032 /* record lower_blkno which may be the insert place. */
3033 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08003034
3035 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
3036 low_bucket = bucket + 1;
3037 continue;
3038 }
3039
3040 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07003041 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08003042 name_index, name, name_hash,
3043 &index, &found);
3044 if (ret) {
3045 mlog_errno(ret);
3046 goto out;
3047 }
3048 break;
3049 }
3050
3051 /*
3052 * Record the bucket we have found.
3053 * When the xattr's hash value is in the gap of 2 buckets, we will
3054 * always set it to the previous bucket.
3055 */
Joel Beckere2356a32008-10-27 15:01:54 -07003056 if (!lower_blkno)
3057 lower_blkno = p_blkno;
3058
3059 /* This should be in cache - we just read it during the search */
3060 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
3061 if (ret) {
3062 mlog_errno(ret);
3063 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08003064 }
Tao Ma589dc262008-08-18 17:38:51 +08003065
Joel Beckerba937122008-10-24 19:13:20 -07003066 xs->header = bucket_xh(xs->bucket);
3067 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08003068 xs->end = xs->base + inode->i_sb->s_blocksize;
3069
3070 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08003071 xs->here = &xs->header->xh_entries[index];
3072 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07003073 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08003074 } else
3075 ret = -ENODATA;
3076
3077out:
Joel Beckere2356a32008-10-27 15:01:54 -07003078 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08003079 return ret;
3080}
3081
3082static int ocfs2_xattr_index_block_find(struct inode *inode,
3083 struct buffer_head *root_bh,
3084 int name_index,
3085 const char *name,
3086 struct ocfs2_xattr_search *xs)
3087{
3088 int ret;
3089 struct ocfs2_xattr_block *xb =
3090 (struct ocfs2_xattr_block *)root_bh->b_data;
3091 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3092 struct ocfs2_extent_list *el = &xb_root->xt_list;
3093 u64 p_blkno = 0;
3094 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08003095 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08003096
3097 if (le16_to_cpu(el->l_next_free_rec) == 0)
3098 return -ENODATA;
3099
3100 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3101 name, name_hash, name_index);
3102
3103 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3104 &num_clusters, el);
3105 if (ret) {
3106 mlog_errno(ret);
3107 goto out;
3108 }
3109
3110 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3111
3112 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003113 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3114 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003115
3116 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3117 p_blkno, first_hash, num_clusters, xs);
3118
3119out:
3120 return ret;
3121}
3122
Tao Ma0c044f02008-08-18 17:38:50 +08003123static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3124 u64 blkno,
3125 u32 clusters,
3126 xattr_bucket_func *func,
3127 void *para)
3128{
Joel Becker6dde41d2008-10-24 17:16:48 -07003129 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003130 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3131 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003132 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003133
Joel Beckerba937122008-10-24 19:13:20 -07003134 bucket = ocfs2_xattr_bucket_new(inode);
3135 if (!bucket) {
3136 mlog_errno(-ENOMEM);
3137 return -ENOMEM;
3138 }
Tao Ma0c044f02008-08-18 17:38:50 +08003139
3140 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003141 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003142
Joel Beckerba937122008-10-24 19:13:20 -07003143 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3144 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003145 if (ret) {
3146 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003147 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003148 }
3149
Tao Ma0c044f02008-08-18 17:38:50 +08003150 /*
3151 * The real bucket num in this series of blocks is stored
3152 * in the 1st bucket.
3153 */
3154 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003155 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003156
Mark Fashehde29c082008-10-29 14:45:30 -07003157 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3158 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003159 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003160 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003161 ret = func(inode, bucket, para);
Tao Maa46fa682009-05-04 05:18:09 +08003162 if (ret && ret != -ERANGE)
Tao Ma0c044f02008-08-18 17:38:50 +08003163 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003164 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003165 }
3166
Joel Beckerba937122008-10-24 19:13:20 -07003167 ocfs2_xattr_bucket_relse(bucket);
3168 if (ret)
3169 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003170 }
3171
Joel Beckerba937122008-10-24 19:13:20 -07003172 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003173 return ret;
3174}
3175
3176struct ocfs2_xattr_tree_list {
3177 char *buffer;
3178 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003179 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003180};
3181
3182static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3183 struct ocfs2_xattr_header *xh,
3184 int index,
3185 int *block_off,
3186 int *new_offset)
3187{
3188 u16 name_offset;
3189
3190 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3191 return -EINVAL;
3192
3193 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3194
3195 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3196 *new_offset = name_offset % inode->i_sb->s_blocksize;
3197
3198 return 0;
3199}
3200
3201static int ocfs2_list_xattr_bucket(struct inode *inode,
3202 struct ocfs2_xattr_bucket *bucket,
3203 void *para)
3204{
Tao Ma936b8832008-10-09 23:06:14 +08003205 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003206 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003207 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003208 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003209
Joel Becker3e632942008-10-24 17:04:49 -07003210 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3211 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003212 type = ocfs2_xattr_get_type(entry);
3213 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003214
Tao Ma936b8832008-10-09 23:06:14 +08003215 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08003216 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07003217 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003218 i,
3219 &block_off,
3220 &new_offset);
3221 if (ret)
3222 break;
Tao Ma936b8832008-10-09 23:06:14 +08003223
Joel Becker51def392008-10-24 16:57:21 -07003224 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003225 new_offset;
3226 ret = ocfs2_xattr_list_entry(xl->buffer,
3227 xl->buffer_size,
3228 &xl->result,
3229 prefix, name,
3230 entry->xe_name_len);
3231 if (ret)
3232 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003233 }
3234 }
3235
3236 return ret;
3237}
3238
3239static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3240 struct ocfs2_xattr_tree_root *xt,
3241 char *buffer,
3242 size_t buffer_size)
3243{
3244 struct ocfs2_extent_list *el = &xt->xt_list;
3245 int ret = 0;
3246 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3247 u64 p_blkno = 0;
3248 struct ocfs2_xattr_tree_list xl = {
3249 .buffer = buffer,
3250 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08003251 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08003252 };
3253
3254 if (le16_to_cpu(el->l_next_free_rec) == 0)
3255 return 0;
3256
3257 while (name_hash > 0) {
3258 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3259 &e_cpos, &num_clusters, el);
3260 if (ret) {
3261 mlog_errno(ret);
3262 goto out;
3263 }
3264
3265 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3266 ocfs2_list_xattr_bucket,
3267 &xl);
3268 if (ret) {
Tao Maa46fa682009-05-04 05:18:09 +08003269 if (ret != -ERANGE)
3270 mlog_errno(ret);
Tao Ma0c044f02008-08-18 17:38:50 +08003271 goto out;
3272 }
3273
3274 if (e_cpos == 0)
3275 break;
3276
3277 name_hash = e_cpos - 1;
3278 }
3279
Tao Ma936b8832008-10-09 23:06:14 +08003280 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003281out:
3282 return ret;
3283}
Tao Ma01225592008-08-18 17:38:53 +08003284
3285static int cmp_xe(const void *a, const void *b)
3286{
3287 const struct ocfs2_xattr_entry *l = a, *r = b;
3288 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3289 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3290
3291 if (l_hash > r_hash)
3292 return 1;
3293 if (l_hash < r_hash)
3294 return -1;
3295 return 0;
3296}
3297
3298static void swap_xe(void *a, void *b, int size)
3299{
3300 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3301
3302 tmp = *l;
3303 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3304 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3305}
3306
3307/*
3308 * When the ocfs2_xattr_block is filled up, new bucket will be created
3309 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003310 * The header goes at the start of the bucket, and the names+values are
3311 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003312 * Note: we need to sort the entries since they are not saved in order
3313 * in the ocfs2_xattr_block.
3314 */
3315static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3316 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003317 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003318{
3319 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003320 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003321 u16 offset, size, off_change;
3322 struct ocfs2_xattr_entry *xe;
3323 struct ocfs2_xattr_block *xb =
3324 (struct ocfs2_xattr_block *)xb_bh->b_data;
3325 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003326 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003327 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003328 char *src = xb_bh->b_data;
3329 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003330
3331 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3332 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003333 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003334
Joel Becker178eeac2008-10-27 15:18:29 -07003335 for (i = 0; i < blks; i++)
3336 memset(bucket_block(bucket, i), 0, blocksize);
3337
Tao Ma01225592008-08-18 17:38:53 +08003338 /*
3339 * Since the xe_name_offset is based on ocfs2_xattr_header,
3340 * there is a offset change corresponding to the change of
3341 * ocfs2_xattr_header's position.
3342 */
3343 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3344 xe = &xb_xh->xh_entries[count - 1];
3345 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3346 size = blocksize - offset;
3347
3348 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003349 memcpy(target + offset, src + offset, size);
3350
3351 /* Init new header now. */
3352 xh->xh_count = xb_xh->xh_count;
3353 xh->xh_num_buckets = cpu_to_le16(1);
3354 xh->xh_name_value_len = cpu_to_le16(size);
3355 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3356
3357 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003358 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003359 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3360 size = count * sizeof(struct ocfs2_xattr_entry);
3361 memcpy(target + offset, (char *)xb_xh + offset, size);
3362
3363 /* Change the xe offset for all the xe because of the move. */
3364 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3365 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3366 for (i = 0; i < count; i++)
3367 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3368
3369 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3370 offset, size, off_change);
3371
3372 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3373 cmp_xe, swap_xe);
3374}
3375
3376/*
3377 * After we move xattr from block to index btree, we have to
3378 * update ocfs2_xattr_search to the new xe and base.
3379 *
3380 * When the entry is in xattr block, xattr_bh indicates the storage place.
3381 * While if the entry is in index b-tree, "bucket" indicates the
3382 * real place of the xattr.
3383 */
Joel Becker178eeac2008-10-27 15:18:29 -07003384static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3385 struct ocfs2_xattr_search *xs,
3386 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003387{
Tao Ma01225592008-08-18 17:38:53 +08003388 char *buf = old_bh->b_data;
3389 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3390 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003391 int i;
Tao Ma01225592008-08-18 17:38:53 +08003392
Joel Beckerba937122008-10-24 19:13:20 -07003393 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003394 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003395 xs->end = xs->base + inode->i_sb->s_blocksize;
3396
Joel Becker178eeac2008-10-27 15:18:29 -07003397 if (xs->not_found)
3398 return;
Tao Ma01225592008-08-18 17:38:53 +08003399
Joel Becker178eeac2008-10-27 15:18:29 -07003400 i = xs->here - old_xh->xh_entries;
3401 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003402}
3403
3404static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003405 struct ocfs2_xattr_search *xs,
3406 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003407{
Tao Ma85db90e2008-11-12 08:27:01 +08003408 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003409 u32 bit_off, len;
3410 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003411 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003412 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3413 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003414 struct buffer_head *xb_bh = xs->xattr_bh;
3415 struct ocfs2_xattr_block *xb =
3416 (struct ocfs2_xattr_block *)xb_bh->b_data;
3417 struct ocfs2_xattr_tree_root *xr;
3418 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003419
3420 mlog(0, "create xattr index block for %llu\n",
3421 (unsigned long long)xb_bh->b_blocknr);
3422
3423 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003424 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003425
Tao Ma01225592008-08-18 17:38:53 +08003426 /*
3427 * XXX:
3428 * We can use this lock for now, and maybe move to a dedicated mutex
3429 * if performance becomes a problem later.
3430 */
3431 down_write(&oi->ip_alloc_sem);
3432
Joel Becker0cf2f762009-02-12 16:41:25 -08003433 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), xb_bh,
Joel Becker84008972008-12-09 16:11:49 -08003434 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003435 if (ret) {
3436 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003437 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003438 }
3439
Tao Ma78f30c32008-11-12 08:27:00 +08003440 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3441 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003442 if (ret) {
3443 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003444 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003445 }
3446
3447 /*
3448 * The bucket may spread in many blocks, and
3449 * we will only touch the 1st block and the last block
3450 * in the whole bucket(one for entry and one for data).
3451 */
3452 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3453
Mark Fashehde29c082008-10-29 14:45:30 -07003454 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3455 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003456
Joel Becker178eeac2008-10-27 15:18:29 -07003457 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003458 if (ret) {
3459 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003460 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003461 }
3462
Joel Becker178eeac2008-10-27 15:18:29 -07003463 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3464 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003465 if (ret) {
3466 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003467 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003468 }
Tao Ma01225592008-08-18 17:38:53 +08003469
Joel Becker178eeac2008-10-27 15:18:29 -07003470 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3471 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3472
3473 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3474
Tao Ma01225592008-08-18 17:38:53 +08003475 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3476 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3477 offsetof(struct ocfs2_xattr_block, xb_attrs));
3478
3479 xr = &xb->xb_attrs.xb_root;
3480 xr->xt_clusters = cpu_to_le32(1);
3481 xr->xt_last_eb_blk = 0;
3482 xr->xt_list.l_tree_depth = 0;
3483 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3484 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3485
3486 xr->xt_list.l_recs[0].e_cpos = 0;
3487 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3488 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3489
3490 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3491
Tao Ma85db90e2008-11-12 08:27:01 +08003492 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003493
Tao Ma85db90e2008-11-12 08:27:01 +08003494out:
Tao Ma01225592008-08-18 17:38:53 +08003495 up_write(&oi->ip_alloc_sem);
3496
Tao Ma01225592008-08-18 17:38:53 +08003497 return ret;
3498}
3499
3500static int cmp_xe_offset(const void *a, const void *b)
3501{
3502 const struct ocfs2_xattr_entry *l = a, *r = b;
3503 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3504 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3505
3506 if (l_name_offset < r_name_offset)
3507 return 1;
3508 if (l_name_offset > r_name_offset)
3509 return -1;
3510 return 0;
3511}
3512
3513/*
3514 * defrag a xattr bucket if we find that the bucket has some
3515 * holes beteen name/value pairs.
3516 * We will move all the name/value pairs to the end of the bucket
3517 * so that we can spare some space for insertion.
3518 */
3519static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003520 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003521 struct ocfs2_xattr_bucket *bucket)
3522{
3523 int ret, i;
3524 size_t end, offset, len, value_len;
3525 struct ocfs2_xattr_header *xh;
3526 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003527 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003528 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003529 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003530 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003531
3532 /*
3533 * In order to make the operation more efficient and generic,
3534 * we copy all the blocks into a contiguous memory and do the
3535 * defragment there, so if anything is error, we will not touch
3536 * the real block.
3537 */
3538 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3539 if (!bucket_buf) {
3540 ret = -EIO;
3541 goto out;
3542 }
3543
Joel Becker161d6f32008-10-27 15:25:18 -07003544 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003545 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3546 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003547
Tao Ma1c32a2f2008-11-06 08:10:47 +08003548 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003549 OCFS2_JOURNAL_ACCESS_WRITE);
3550 if (ret < 0) {
3551 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003552 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003553 }
3554
3555 xh = (struct ocfs2_xattr_header *)bucket_buf;
3556 entries = (char *)xh->xh_entries;
3557 xh_free_start = le16_to_cpu(xh->xh_free_start);
3558
3559 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3560 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003561 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3562 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003563
3564 /*
3565 * sort all the entries by their offset.
3566 * the largest will be the first, so that we can
3567 * move them to the end one by one.
3568 */
3569 sort(entries, le16_to_cpu(xh->xh_count),
3570 sizeof(struct ocfs2_xattr_entry),
3571 cmp_xe_offset, swap_xe);
3572
3573 /* Move all name/values to the end of the bucket. */
3574 xe = xh->xh_entries;
3575 end = OCFS2_XATTR_BUCKET_SIZE;
3576 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3577 offset = le16_to_cpu(xe->xe_name_offset);
3578 if (ocfs2_xattr_is_local(xe))
3579 value_len = OCFS2_XATTR_SIZE(
3580 le64_to_cpu(xe->xe_value_size));
3581 else
3582 value_len = OCFS2_XATTR_ROOT_SIZE;
3583 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3584
3585 /*
3586 * We must make sure that the name/value pair
3587 * exist in the same block. So adjust end to
3588 * the previous block end if needed.
3589 */
3590 if (((end - len) / blocksize !=
3591 (end - 1) / blocksize))
3592 end = end - end % blocksize;
3593
3594 if (end > offset + len) {
3595 memmove(bucket_buf + end - len,
3596 bucket_buf + offset, len);
3597 xe->xe_name_offset = cpu_to_le16(end - len);
3598 }
3599
3600 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3601 "bucket %llu\n", (unsigned long long)blkno);
3602
3603 end -= len;
3604 }
3605
3606 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3607 "bucket %llu\n", (unsigned long long)blkno);
3608
3609 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003610 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003611
3612 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3613 xh->xh_free_start = cpu_to_le16(end);
3614
3615 /* sort the entries by their name_hash. */
3616 sort(entries, le16_to_cpu(xh->xh_count),
3617 sizeof(struct ocfs2_xattr_entry),
3618 cmp_xe, swap_xe);
3619
3620 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003621 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3622 memcpy(bucket_block(bucket, i), buf, blocksize);
3623 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003624
Tao Ma01225592008-08-18 17:38:53 +08003625out:
Tao Ma01225592008-08-18 17:38:53 +08003626 kfree(bucket_buf);
3627 return ret;
3628}
3629
3630/*
Joel Beckerb5c03e72008-11-25 19:58:16 -08003631 * prev_blkno points to the start of an existing extent. new_blkno
3632 * points to a newly allocated extent. Because we know each of our
3633 * clusters contains more than bucket, we can easily split one cluster
3634 * at a bucket boundary. So we take the last cluster of the existing
3635 * extent and split it down the middle. We move the last half of the
3636 * buckets in the last cluster of the existing extent over to the new
3637 * extent.
Tao Ma01225592008-08-18 17:38:53 +08003638 *
Joel Beckerb5c03e72008-11-25 19:58:16 -08003639 * first_bh is the buffer at prev_blkno so we can update the existing
3640 * extent's bucket count. header_bh is the bucket were we were hoping
3641 * to insert our xattr. If the bucket move places the target in the new
3642 * extent, we'll update first_bh and header_bh after modifying the old
3643 * extent.
3644 *
3645 * first_hash will be set as the 1st xe's name_hash in the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003646 */
3647static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3648 handle_t *handle,
Joel Becker41cb8142008-11-26 14:25:21 -08003649 struct ocfs2_xattr_bucket *first,
3650 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08003651 u64 new_blkno,
Tao Ma01225592008-08-18 17:38:53 +08003652 u32 num_clusters,
3653 u32 *first_hash)
3654{
Joel Beckerc58b6032008-11-26 13:36:24 -08003655 int ret;
Joel Becker41cb8142008-11-26 14:25:21 -08003656 struct super_block *sb = inode->i_sb;
3657 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(sb);
3658 int num_buckets = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(sb));
Joel Beckerb5c03e72008-11-25 19:58:16 -08003659 int to_move = num_buckets / 2;
Joel Beckerc58b6032008-11-26 13:36:24 -08003660 u64 src_blkno;
Joel Becker41cb8142008-11-26 14:25:21 -08003661 u64 last_cluster_blkno = bucket_blkno(first) +
3662 ((num_clusters - 1) * ocfs2_clusters_to_blocks(sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08003663
Joel Becker41cb8142008-11-26 14:25:21 -08003664 BUG_ON(le16_to_cpu(bucket_xh(first)->xh_num_buckets) < num_buckets);
3665 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == OCFS2_SB(sb)->s_clustersize);
Tao Ma01225592008-08-18 17:38:53 +08003666
Tao Ma01225592008-08-18 17:38:53 +08003667 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Joel Beckerc58b6032008-11-26 13:36:24 -08003668 (unsigned long long)last_cluster_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003669
Joel Becker41cb8142008-11-26 14:25:21 -08003670 ret = ocfs2_mv_xattr_buckets(inode, handle, bucket_blkno(first),
Joel Beckerc58b6032008-11-26 13:36:24 -08003671 last_cluster_blkno, new_blkno,
3672 to_move, first_hash);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003673 if (ret) {
3674 mlog_errno(ret);
3675 goto out;
3676 }
3677
Joel Beckerc58b6032008-11-26 13:36:24 -08003678 /* This is the first bucket that got moved */
3679 src_blkno = last_cluster_blkno + (to_move * blks_per_bucket);
3680
Tao Ma01225592008-08-18 17:38:53 +08003681 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003682 * If the target bucket was part of the moved buckets, we need to
Joel Becker41cb8142008-11-26 14:25:21 -08003683 * update first and target.
Joel Beckerb5c03e72008-11-25 19:58:16 -08003684 */
Joel Becker41cb8142008-11-26 14:25:21 -08003685 if (bucket_blkno(target) >= src_blkno) {
Joel Beckerb5c03e72008-11-25 19:58:16 -08003686 /* Find the block for the new target bucket */
3687 src_blkno = new_blkno +
Joel Becker41cb8142008-11-26 14:25:21 -08003688 (bucket_blkno(target) - src_blkno);
3689
3690 ocfs2_xattr_bucket_relse(first);
3691 ocfs2_xattr_bucket_relse(target);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003692
3693 /*
Joel Beckerc58b6032008-11-26 13:36:24 -08003694 * These shouldn't fail - the buffers are in the
Joel Beckerb5c03e72008-11-25 19:58:16 -08003695 * journal from ocfs2_cp_xattr_bucket().
3696 */
Joel Becker41cb8142008-11-26 14:25:21 -08003697 ret = ocfs2_read_xattr_bucket(first, new_blkno);
Joel Beckerc58b6032008-11-26 13:36:24 -08003698 if (ret) {
3699 mlog_errno(ret);
3700 goto out;
3701 }
Joel Becker41cb8142008-11-26 14:25:21 -08003702 ret = ocfs2_read_xattr_bucket(target, src_blkno);
3703 if (ret)
Joel Beckerb5c03e72008-11-25 19:58:16 -08003704 mlog_errno(ret);
Joel Beckerb5c03e72008-11-25 19:58:16 -08003705
Joel Beckerb5c03e72008-11-25 19:58:16 -08003706 }
3707
Tao Ma01225592008-08-18 17:38:53 +08003708out:
Tao Ma01225592008-08-18 17:38:53 +08003709 return ret;
3710}
3711
Tao Ma01225592008-08-18 17:38:53 +08003712/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003713 * Find the suitable pos when we divide a bucket into 2.
3714 * We have to make sure the xattrs with the same hash value exist
3715 * in the same bucket.
3716 *
3717 * If this ocfs2_xattr_header covers more than one hash value, find a
3718 * place where the hash value changes. Try to find the most even split.
3719 * The most common case is that all entries have different hash values,
3720 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003721 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003722static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3723{
3724 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3725 int count = le16_to_cpu(xh->xh_count);
3726 int delta, middle = count / 2;
3727
3728 /*
3729 * We start at the middle. Each step gets farther away in both
3730 * directions. We therefore hit the change in hash value
3731 * nearest to the middle. Note that this loop does not execute for
3732 * count < 2.
3733 */
3734 for (delta = 0; delta < middle; delta++) {
3735 /* Let's check delta earlier than middle */
3736 if (cmp_xe(&entries[middle - delta - 1],
3737 &entries[middle - delta]))
3738 return middle - delta;
3739
3740 /* For even counts, don't walk off the end */
3741 if ((middle + delta + 1) == count)
3742 continue;
3743
3744 /* Now try delta past middle */
3745 if (cmp_xe(&entries[middle + delta],
3746 &entries[middle + delta + 1]))
3747 return middle + delta + 1;
3748 }
3749
3750 /* Every entry had the same hash */
3751 return count;
3752}
3753
3754/*
3755 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3756 * first_hash will record the 1st hash of the new bucket.
3757 *
3758 * Normally half of the xattrs will be moved. But we have to make
3759 * sure that the xattrs with the same hash value are stored in the
3760 * same bucket. If all the xattrs in this bucket have the same hash
3761 * value, the new bucket will be initialized as an empty one and the
3762 * first_hash will be initialized as (hash_value+1).
3763 */
3764static int ocfs2_divide_xattr_bucket(struct inode *inode,
3765 handle_t *handle,
3766 u64 blk,
3767 u64 new_blk,
3768 u32 *first_hash,
3769 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003770{
3771 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003772 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003773 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003774 struct ocfs2_xattr_header *xh;
3775 struct ocfs2_xattr_entry *xe;
3776 int blocksize = inode->i_sb->s_blocksize;
3777
Tao Ma80bcaf32008-10-27 06:06:24 +08003778 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003779 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003780
Joel Beckerba937122008-10-24 19:13:20 -07003781 s_bucket = ocfs2_xattr_bucket_new(inode);
3782 t_bucket = ocfs2_xattr_bucket_new(inode);
3783 if (!s_bucket || !t_bucket) {
3784 ret = -ENOMEM;
3785 mlog_errno(ret);
3786 goto out;
3787 }
Tao Ma01225592008-08-18 17:38:53 +08003788
Joel Beckerba937122008-10-24 19:13:20 -07003789 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003790 if (ret) {
3791 mlog_errno(ret);
3792 goto out;
3793 }
3794
Joel Beckerba937122008-10-24 19:13:20 -07003795 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003796 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003797 if (ret) {
3798 mlog_errno(ret);
3799 goto out;
3800 }
3801
Joel Becker784b8162008-10-24 17:33:40 -07003802 /*
3803 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3804 * there's no need to read it.
3805 */
Joel Beckerba937122008-10-24 19:13:20 -07003806 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003807 if (ret) {
3808 mlog_errno(ret);
3809 goto out;
3810 }
3811
Joel Becker2b656c12008-11-25 19:00:15 -08003812 /*
3813 * Hey, if we're overwriting t_bucket, what difference does
3814 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3815 * same part of ocfs2_cp_xattr_bucket().
3816 */
Joel Beckerba937122008-10-24 19:13:20 -07003817 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003818 new_bucket_head ?
3819 OCFS2_JOURNAL_ACCESS_CREATE :
3820 OCFS2_JOURNAL_ACCESS_WRITE);
3821 if (ret) {
3822 mlog_errno(ret);
3823 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003824 }
3825
Joel Beckerba937122008-10-24 19:13:20 -07003826 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003827 count = le16_to_cpu(xh->xh_count);
3828 start = ocfs2_xattr_find_divide_pos(xh);
3829
3830 if (start == count) {
3831 xe = &xh->xh_entries[start-1];
3832
3833 /*
3834 * initialized a new empty bucket here.
3835 * The hash value is set as one larger than
3836 * that of the last entry in the previous bucket.
3837 */
Joel Beckerba937122008-10-24 19:13:20 -07003838 for (i = 0; i < t_bucket->bu_blocks; i++)
3839 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003840
Joel Beckerba937122008-10-24 19:13:20 -07003841 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003842 xh->xh_free_start = cpu_to_le16(blocksize);
3843 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3844 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3845
3846 goto set_num_buckets;
3847 }
3848
Tao Ma01225592008-08-18 17:38:53 +08003849 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003850 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003851
3852 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003853 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003854
3855 /*
3856 * Calculate the total name/value len and xh_free_start for
3857 * the old bucket first.
3858 */
3859 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3860 name_value_len = 0;
3861 for (i = 0; i < start; i++) {
3862 xe = &xh->xh_entries[i];
3863 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3864 if (ocfs2_xattr_is_local(xe))
3865 xe_len +=
3866 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3867 else
3868 xe_len += OCFS2_XATTR_ROOT_SIZE;
3869 name_value_len += xe_len;
3870 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3871 name_offset = le16_to_cpu(xe->xe_name_offset);
3872 }
3873
3874 /*
3875 * Now begin the modification to the new bucket.
3876 *
3877 * In the new bucket, We just move the xattr entry to the beginning
3878 * and don't touch the name/value. So there will be some holes in the
3879 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3880 * called.
3881 */
3882 xe = &xh->xh_entries[start];
3883 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3884 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003885 (int)((char *)xe - (char *)xh),
3886 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003887 memmove((char *)xh->xh_entries, (char *)xe, len);
3888 xe = &xh->xh_entries[count - start];
3889 len = sizeof(struct ocfs2_xattr_entry) * start;
3890 memset((char *)xe, 0, len);
3891
3892 le16_add_cpu(&xh->xh_count, -start);
3893 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3894
3895 /* Calculate xh_free_start for the new bucket. */
3896 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3897 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3898 xe = &xh->xh_entries[i];
3899 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3900 if (ocfs2_xattr_is_local(xe))
3901 xe_len +=
3902 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3903 else
3904 xe_len += OCFS2_XATTR_ROOT_SIZE;
3905 if (le16_to_cpu(xe->xe_name_offset) <
3906 le16_to_cpu(xh->xh_free_start))
3907 xh->xh_free_start = xe->xe_name_offset;
3908 }
3909
Tao Ma80bcaf32008-10-27 06:06:24 +08003910set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003911 /* set xh->xh_num_buckets for the new xh. */
3912 if (new_bucket_head)
3913 xh->xh_num_buckets = cpu_to_le16(1);
3914 else
3915 xh->xh_num_buckets = 0;
3916
Joel Beckerba937122008-10-24 19:13:20 -07003917 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003918
3919 /* store the first_hash of the new bucket. */
3920 if (first_hash)
3921 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3922
3923 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003924 * Now only update the 1st block of the old bucket. If we
3925 * just added a new empty bucket, there is no need to modify
3926 * it.
Tao Ma01225592008-08-18 17:38:53 +08003927 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003928 if (start == count)
3929 goto out;
3930
Joel Beckerba937122008-10-24 19:13:20 -07003931 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003932 memset(&xh->xh_entries[start], 0,
3933 sizeof(struct ocfs2_xattr_entry) * (count - start));
3934 xh->xh_count = cpu_to_le16(start);
3935 xh->xh_free_start = cpu_to_le16(name_offset);
3936 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3937
Joel Beckerba937122008-10-24 19:13:20 -07003938 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003939
3940out:
Joel Beckerba937122008-10-24 19:13:20 -07003941 ocfs2_xattr_bucket_free(s_bucket);
3942 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003943
3944 return ret;
3945}
3946
3947/*
3948 * Copy xattr from one bucket to another bucket.
3949 *
3950 * The caller must make sure that the journal transaction
3951 * has enough space for journaling.
3952 */
3953static int ocfs2_cp_xattr_bucket(struct inode *inode,
3954 handle_t *handle,
3955 u64 s_blkno,
3956 u64 t_blkno,
3957 int t_is_new)
3958{
Joel Becker4980c6d2008-10-24 18:54:43 -07003959 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003960 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003961
3962 BUG_ON(s_blkno == t_blkno);
3963
3964 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003965 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3966 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003967
Joel Beckerba937122008-10-24 19:13:20 -07003968 s_bucket = ocfs2_xattr_bucket_new(inode);
3969 t_bucket = ocfs2_xattr_bucket_new(inode);
3970 if (!s_bucket || !t_bucket) {
3971 ret = -ENOMEM;
3972 mlog_errno(ret);
3973 goto out;
3974 }
Joel Becker92de1092008-11-25 17:06:40 -08003975
Joel Beckerba937122008-10-24 19:13:20 -07003976 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003977 if (ret)
3978 goto out;
3979
Joel Becker784b8162008-10-24 17:33:40 -07003980 /*
3981 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3982 * there's no need to read it.
3983 */
Joel Beckerba937122008-10-24 19:13:20 -07003984 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003985 if (ret)
3986 goto out;
3987
Joel Becker2b656c12008-11-25 19:00:15 -08003988 /*
3989 * Hey, if we're overwriting t_bucket, what difference does
3990 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
Joel Becker874d65a2008-11-26 13:02:18 -08003991 * cluster to fill, we came here from
3992 * ocfs2_mv_xattr_buckets(), and it is really new -
3993 * ACCESS_CREATE is required. But we also might have moved data
3994 * out of t_bucket before extending back into it.
3995 * ocfs2_add_new_xattr_bucket() can do this - its call to
3996 * ocfs2_add_new_xattr_cluster() may have created a new extent
Joel Becker2b656c12008-11-25 19:00:15 -08003997 * and copied out the end of the old extent. Then it re-extends
3998 * the old extent back to create space for new xattrs. That's
3999 * how we get here, and the bucket isn't really new.
4000 */
Joel Beckerba937122008-10-24 19:13:20 -07004001 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004002 t_is_new ?
4003 OCFS2_JOURNAL_ACCESS_CREATE :
4004 OCFS2_JOURNAL_ACCESS_WRITE);
4005 if (ret)
4006 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004007
Joel Beckerba937122008-10-24 19:13:20 -07004008 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
4009 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004010
4011out:
Joel Beckerba937122008-10-24 19:13:20 -07004012 ocfs2_xattr_bucket_free(t_bucket);
4013 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004014
4015 return ret;
4016}
4017
4018/*
Joel Becker874d65a2008-11-26 13:02:18 -08004019 * src_blk points to the start of an existing extent. last_blk points to
4020 * last cluster in that extent. to_blk points to a newly allocated
Joel Becker54ecb6b2008-11-26 13:18:31 -08004021 * extent. We copy the buckets from the cluster at last_blk to the new
4022 * extent. If start_bucket is non-zero, we skip that many buckets before
4023 * we start copying. The new extent's xh_num_buckets gets set to the
4024 * number of buckets we copied. The old extent's xh_num_buckets shrinks
4025 * by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08004026 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004027static int ocfs2_mv_xattr_buckets(struct inode *inode, handle_t *handle,
4028 u64 src_blk, u64 last_blk, u64 to_blk,
4029 unsigned int start_bucket,
4030 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004031{
4032 int i, ret, credits;
4033 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker15d60922008-11-25 18:36:42 -08004034 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004035 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08004036 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08004037
Joel Becker874d65a2008-11-26 13:02:18 -08004038 mlog(0, "mv xattrs from cluster %llu to %llu\n",
4039 (unsigned long long)last_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08004040
Joel Becker54ecb6b2008-11-26 13:18:31 -08004041 BUG_ON(start_bucket >= num_buckets);
4042 if (start_bucket) {
4043 num_buckets -= start_bucket;
4044 last_blk += (start_bucket * blks_per_bucket);
4045 }
4046
Joel Becker15d60922008-11-25 18:36:42 -08004047 /* The first bucket of the original extent */
4048 old_first = ocfs2_xattr_bucket_new(inode);
4049 /* The first bucket of the new extent */
4050 new_first = ocfs2_xattr_bucket_new(inode);
4051 if (!old_first || !new_first) {
4052 ret = -ENOMEM;
4053 mlog_errno(ret);
4054 goto out;
4055 }
4056
Joel Becker874d65a2008-11-26 13:02:18 -08004057 ret = ocfs2_read_xattr_bucket(old_first, src_blk);
Joel Becker15d60922008-11-25 18:36:42 -08004058 if (ret) {
4059 mlog_errno(ret);
4060 goto out;
4061 }
4062
Tao Ma01225592008-08-18 17:38:53 +08004063 /*
Joel Becker54ecb6b2008-11-26 13:18:31 -08004064 * We need to update the first bucket of the old extent and all
4065 * the buckets going to the new extent.
Tao Ma01225592008-08-18 17:38:53 +08004066 */
Joel Becker54ecb6b2008-11-26 13:18:31 -08004067 credits = ((num_buckets + 1) * blks_per_bucket) +
4068 handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004069 ret = ocfs2_extend_trans(handle, credits);
4070 if (ret) {
4071 mlog_errno(ret);
4072 goto out;
4073 }
4074
Joel Becker15d60922008-11-25 18:36:42 -08004075 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4076 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004077 if (ret) {
4078 mlog_errno(ret);
4079 goto out;
4080 }
4081
4082 for (i = 0; i < num_buckets; i++) {
4083 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker874d65a2008-11-26 13:02:18 -08004084 last_blk + (i * blks_per_bucket),
Joel Becker15d60922008-11-25 18:36:42 -08004085 to_blk + (i * blks_per_bucket),
4086 1);
Tao Ma01225592008-08-18 17:38:53 +08004087 if (ret) {
4088 mlog_errno(ret);
4089 goto out;
4090 }
Tao Ma01225592008-08-18 17:38:53 +08004091 }
4092
Joel Becker15d60922008-11-25 18:36:42 -08004093 /*
4094 * Get the new bucket ready before we dirty anything
4095 * (This actually shouldn't fail, because we already dirtied
4096 * it once in ocfs2_cp_xattr_bucket()).
4097 */
4098 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4099 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004100 mlog_errno(ret);
4101 goto out;
4102 }
Joel Becker15d60922008-11-25 18:36:42 -08004103 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4104 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004105 if (ret) {
4106 mlog_errno(ret);
4107 goto out;
4108 }
4109
Joel Becker15d60922008-11-25 18:36:42 -08004110 /* Now update the headers */
4111 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4112 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004113
Joel Becker15d60922008-11-25 18:36:42 -08004114 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4115 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004116
4117 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004118 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4119
Tao Ma01225592008-08-18 17:38:53 +08004120out:
Joel Becker15d60922008-11-25 18:36:42 -08004121 ocfs2_xattr_bucket_free(new_first);
4122 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004123 return ret;
4124}
4125
4126/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004127 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004128 * This function should only be called when bucket size == cluster size.
4129 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4130 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004131static int ocfs2_divide_xattr_cluster(struct inode *inode,
4132 handle_t *handle,
4133 u64 prev_blk,
4134 u64 new_blk,
4135 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004136{
4137 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004138 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004139
4140 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4141
4142 ret = ocfs2_extend_trans(handle, credits);
4143 if (ret) {
4144 mlog_errno(ret);
4145 return ret;
4146 }
4147
4148 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004149 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4150 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004151}
4152
4153/*
4154 * Move some xattrs from the old cluster to the new one since they are not
4155 * contiguous in ocfs2 xattr tree.
4156 *
4157 * new_blk starts a new separate cluster, and we will move some xattrs from
4158 * prev_blk to it. v_start will be set as the first name hash value in this
4159 * new cluster so that it can be used as e_cpos during tree insertion and
4160 * don't collide with our original b-tree operations. first_bh and header_bh
4161 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4162 * to extend the insert bucket.
4163 *
4164 * The problem is how much xattr should we move to the new one and when should
4165 * we update first_bh and header_bh?
4166 * 1. If cluster size > bucket size, that means the previous cluster has more
4167 * than 1 bucket, so just move half nums of bucket into the new cluster and
4168 * update the first_bh and header_bh if the insert bucket has been moved
4169 * to the new cluster.
4170 * 2. If cluster_size == bucket_size:
4171 * a) If the previous extent rec has more than one cluster and the insert
4172 * place isn't in the last cluster, copy the entire last cluster to the
4173 * new one. This time, we don't need to upate the first_bh and header_bh
4174 * since they will not be moved into the new cluster.
4175 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4176 * the new one. And we set the extend flag to zero if the insert place is
4177 * moved into the new allocated cluster since no extend is needed.
4178 */
4179static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4180 handle_t *handle,
Joel Becker012ee912008-11-26 14:43:31 -08004181 struct ocfs2_xattr_bucket *first,
4182 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004183 u64 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004184 u32 prev_clusters,
4185 u32 *v_start,
4186 int *extend)
4187{
Joel Becker92cf3ad2008-11-26 14:12:09 -08004188 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004189
4190 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Joel Becker012ee912008-11-26 14:43:31 -08004191 (unsigned long long)bucket_blkno(first), prev_clusters,
Mark Fashehde29c082008-10-29 14:45:30 -07004192 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004193
Joel Becker41cb8142008-11-26 14:25:21 -08004194 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) {
Tao Ma01225592008-08-18 17:38:53 +08004195 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4196 handle,
Joel Becker41cb8142008-11-26 14:25:21 -08004197 first, target,
Tao Ma01225592008-08-18 17:38:53 +08004198 new_blk,
Tao Ma01225592008-08-18 17:38:53 +08004199 prev_clusters,
4200 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004201 if (ret)
Joel Becker41cb8142008-11-26 14:25:21 -08004202 mlog_errno(ret);
Joel Becker41cb8142008-11-26 14:25:21 -08004203 } else {
Joel Becker92cf3ad2008-11-26 14:12:09 -08004204 /* The start of the last cluster in the first extent */
4205 u64 last_blk = bucket_blkno(first) +
4206 ((prev_clusters - 1) *
4207 ocfs2_clusters_to_blocks(inode->i_sb, 1));
Tao Ma01225592008-08-18 17:38:53 +08004208
Joel Becker012ee912008-11-26 14:43:31 -08004209 if (prev_clusters > 1 && bucket_blkno(target) != last_blk) {
Joel Becker874d65a2008-11-26 13:02:18 -08004210 ret = ocfs2_mv_xattr_buckets(inode, handle,
Joel Becker92cf3ad2008-11-26 14:12:09 -08004211 bucket_blkno(first),
Joel Becker54ecb6b2008-11-26 13:18:31 -08004212 last_blk, new_blk, 0,
Tao Ma01225592008-08-18 17:38:53 +08004213 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004214 if (ret)
4215 mlog_errno(ret);
4216 } else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004217 ret = ocfs2_divide_xattr_cluster(inode, handle,
4218 last_blk, new_blk,
4219 v_start);
Joel Becker012ee912008-11-26 14:43:31 -08004220 if (ret)
4221 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004222
Joel Becker92cf3ad2008-11-26 14:12:09 -08004223 if ((bucket_blkno(target) == last_blk) && extend)
Tao Ma01225592008-08-18 17:38:53 +08004224 *extend = 0;
4225 }
4226 }
4227
4228 return ret;
4229}
4230
4231/*
4232 * Add a new cluster for xattr storage.
4233 *
4234 * If the new cluster is contiguous with the previous one, it will be
4235 * appended to the same extent record, and num_clusters will be updated.
4236 * If not, we will insert a new extent for it and move some xattrs in
4237 * the last cluster into the new allocated one.
4238 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4239 * lose the benefits of hashing because we'll have to search large leaves.
4240 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4241 * if it's bigger).
4242 *
4243 * first_bh is the first block of the previous extent rec and header_bh
4244 * indicates the bucket we will insert the new xattrs. They will be updated
4245 * when the header_bh is moved into the new cluster.
4246 */
4247static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4248 struct buffer_head *root_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004249 struct ocfs2_xattr_bucket *first,
4250 struct ocfs2_xattr_bucket *target,
Tao Ma01225592008-08-18 17:38:53 +08004251 u32 *num_clusters,
4252 u32 prev_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004253 int *extend,
4254 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004255{
Tao Ma85db90e2008-11-12 08:27:01 +08004256 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004257 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4258 u32 prev_clusters = *num_clusters;
4259 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4260 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004261 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004262 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004263 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004264
4265 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4266 "previous xattr blkno = %llu\n",
4267 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Joel Beckered29c0c2008-11-26 15:08:44 -08004268 prev_cpos, (unsigned long long)bucket_blkno(first));
Tao Ma01225592008-08-18 17:38:53 +08004269
Joel Becker5e404e92009-02-13 03:54:22 -08004270 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004271
Joel Becker0cf2f762009-02-12 16:41:25 -08004272 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004273 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004274 if (ret < 0) {
4275 mlog_errno(ret);
4276 goto leave;
4277 }
4278
Tao Ma78f30c32008-11-12 08:27:00 +08004279 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004280 clusters_to_add, &bit_off, &num_bits);
4281 if (ret < 0) {
4282 if (ret != -ENOSPC)
4283 mlog_errno(ret);
4284 goto leave;
4285 }
4286
4287 BUG_ON(num_bits > clusters_to_add);
4288
4289 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4290 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4291 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4292
Joel Beckered29c0c2008-11-26 15:08:44 -08004293 if (bucket_blkno(first) + (prev_clusters * bpc) == block &&
Tao Ma01225592008-08-18 17:38:53 +08004294 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4295 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4296 /*
4297 * If this cluster is contiguous with the old one and
4298 * adding this new cluster, we don't surpass the limit of
4299 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4300 * initialized and used like other buckets in the previous
4301 * cluster.
4302 * So add it as a contiguous one. The caller will handle
4303 * its init process.
4304 */
4305 v_start = prev_cpos + prev_clusters;
4306 *num_clusters = prev_clusters + num_bits;
4307 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4308 num_bits);
4309 } else {
4310 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4311 handle,
Joel Becker012ee912008-11-26 14:43:31 -08004312 first,
4313 target,
Tao Ma01225592008-08-18 17:38:53 +08004314 block,
Tao Ma01225592008-08-18 17:38:53 +08004315 prev_clusters,
4316 &v_start,
4317 extend);
4318 if (ret) {
4319 mlog_errno(ret);
4320 goto leave;
4321 }
4322 }
4323
4324 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004325 num_bits, (unsigned long long)block, v_start);
Joel Beckercc79d8c2009-02-13 03:24:43 -08004326 ret = ocfs2_insert_extent(handle, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004327 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004328 if (ret < 0) {
4329 mlog_errno(ret);
4330 goto leave;
4331 }
4332
4333 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004334 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004335 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004336
4337leave:
Tao Ma01225592008-08-18 17:38:53 +08004338 return ret;
4339}
4340
4341/*
Joel Becker92de1092008-11-25 17:06:40 -08004342 * We are given an extent. 'first' is the bucket at the very front of
4343 * the extent. The extent has space for an additional bucket past
4344 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4345 * of the target bucket. We wish to shift every bucket past the target
4346 * down one, filling in that additional space. When we get back to the
4347 * target, we split the target between itself and the now-empty bucket
4348 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004349 */
4350static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004351 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004352 struct ocfs2_xattr_bucket *first,
4353 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004354 u32 num_clusters)
4355{
4356 int ret, credits;
4357 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4358 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004359 u64 end_blk;
4360 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004361
4362 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004363 "from %llu, len = %u\n", (unsigned long long)target_blk,
4364 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004365
Joel Becker92de1092008-11-25 17:06:40 -08004366 /* The extent must have room for an additional bucket */
4367 BUG_ON(new_bucket >=
4368 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004369
Joel Becker92de1092008-11-25 17:06:40 -08004370 /* end_blk points to the last existing bucket */
4371 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004372
4373 /*
Joel Becker92de1092008-11-25 17:06:40 -08004374 * end_blk is the start of the last existing bucket.
4375 * Thus, (end_blk - target_blk) covers the target bucket and
4376 * every bucket after it up to, but not including, the last
4377 * existing bucket. Then we add the last existing bucket, the
4378 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004379 */
Joel Becker92de1092008-11-25 17:06:40 -08004380 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004381 handle->h_buffer_credits;
4382 ret = ocfs2_extend_trans(handle, credits);
4383 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004384 mlog_errno(ret);
4385 goto out;
4386 }
4387
Joel Becker92de1092008-11-25 17:06:40 -08004388 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4389 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004390 if (ret) {
4391 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004392 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004393 }
4394
Joel Becker92de1092008-11-25 17:06:40 -08004395 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004396 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4397 end_blk + blk_per_bucket, 0);
4398 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004399 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004400 end_blk -= blk_per_bucket;
4401 }
4402
Joel Becker92de1092008-11-25 17:06:40 -08004403 /* Move half of the xattr in target_blkno to the next bucket. */
4404 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4405 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004406
Joel Becker92de1092008-11-25 17:06:40 -08004407 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4408 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004409
Tao Ma01225592008-08-18 17:38:53 +08004410out:
4411 return ret;
4412}
4413
4414/*
Joel Becker91f20332008-11-26 15:25:41 -08004415 * Add new xattr bucket in an extent record and adjust the buckets
4416 * accordingly. xb_bh is the ocfs2_xattr_block, and target is the
4417 * bucket we want to insert into.
Tao Ma01225592008-08-18 17:38:53 +08004418 *
Joel Becker91f20332008-11-26 15:25:41 -08004419 * In the easy case, we will move all the buckets after target down by
4420 * one. Half of target's xattrs will be moved to the next bucket.
4421 *
4422 * If current cluster is full, we'll allocate a new one. This may not
4423 * be contiguous. The underlying calls will make sure that there is
4424 * space for the insert, shifting buckets around if necessary.
4425 * 'target' may be moved by those calls.
Tao Ma01225592008-08-18 17:38:53 +08004426 */
4427static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4428 struct buffer_head *xb_bh,
Joel Becker91f20332008-11-26 15:25:41 -08004429 struct ocfs2_xattr_bucket *target,
Tao Ma78f30c32008-11-12 08:27:00 +08004430 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004431{
Tao Ma01225592008-08-18 17:38:53 +08004432 struct ocfs2_xattr_block *xb =
4433 (struct ocfs2_xattr_block *)xb_bh->b_data;
4434 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4435 struct ocfs2_extent_list *el = &xb_root->xt_list;
Joel Becker91f20332008-11-26 15:25:41 -08004436 u32 name_hash =
4437 le32_to_cpu(bucket_xh(target)->xh_entries[0].xe_name_hash);
Joel Beckered29c0c2008-11-26 15:08:44 -08004438 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08004439 int ret, num_buckets, extend = 1;
4440 u64 p_blkno;
4441 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004442 /* The bucket at the front of the extent */
Joel Becker91f20332008-11-26 15:25:41 -08004443 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004444
Joel Becker91f20332008-11-26 15:25:41 -08004445 mlog(0, "Add new xattr bucket starting from %llu\n",
4446 (unsigned long long)bucket_blkno(target));
Tao Ma01225592008-08-18 17:38:53 +08004447
Joel Beckered29c0c2008-11-26 15:08:44 -08004448 /* The first bucket of the original extent */
Joel Becker92de1092008-11-25 17:06:40 -08004449 first = ocfs2_xattr_bucket_new(inode);
Joel Becker91f20332008-11-26 15:25:41 -08004450 if (!first) {
Joel Becker92de1092008-11-25 17:06:40 -08004451 ret = -ENOMEM;
4452 mlog_errno(ret);
4453 goto out;
4454 }
4455
Tao Ma01225592008-08-18 17:38:53 +08004456 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4457 &num_clusters, el);
4458 if (ret) {
4459 mlog_errno(ret);
4460 goto out;
4461 }
4462
Joel Beckered29c0c2008-11-26 15:08:44 -08004463 ret = ocfs2_read_xattr_bucket(first, p_blkno);
4464 if (ret) {
4465 mlog_errno(ret);
4466 goto out;
4467 }
4468
Tao Ma01225592008-08-18 17:38:53 +08004469 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
Joel Beckered29c0c2008-11-26 15:08:44 -08004470 if (num_buckets == le16_to_cpu(bucket_xh(first)->xh_num_buckets)) {
4471 /*
4472 * This can move first+target if the target bucket moves
4473 * to the new extent.
4474 */
Tao Ma01225592008-08-18 17:38:53 +08004475 ret = ocfs2_add_new_xattr_cluster(inode,
4476 xb_bh,
Joel Beckered29c0c2008-11-26 15:08:44 -08004477 first,
4478 target,
Tao Ma01225592008-08-18 17:38:53 +08004479 &num_clusters,
4480 e_cpos,
Tao Ma78f30c32008-11-12 08:27:00 +08004481 &extend,
4482 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004483 if (ret) {
4484 mlog_errno(ret);
4485 goto out;
4486 }
4487 }
4488
Joel Becker92de1092008-11-25 17:06:40 -08004489 if (extend) {
Tao Ma01225592008-08-18 17:38:53 +08004490 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004491 ctxt->handle,
Joel Beckered29c0c2008-11-26 15:08:44 -08004492 first,
4493 bucket_blkno(target),
Tao Ma01225592008-08-18 17:38:53 +08004494 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004495 if (ret)
4496 mlog_errno(ret);
4497 }
4498
Tao Ma01225592008-08-18 17:38:53 +08004499out:
Joel Becker92de1092008-11-25 17:06:40 -08004500 ocfs2_xattr_bucket_free(first);
Joel Beckered29c0c2008-11-26 15:08:44 -08004501
Tao Ma01225592008-08-18 17:38:53 +08004502 return ret;
4503}
4504
4505static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4506 struct ocfs2_xattr_bucket *bucket,
4507 int offs)
4508{
4509 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4510
4511 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004512 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004513}
4514
4515/*
4516 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004517 *
4518 * Note: "local" indicates the real data's locality. So we can't
4519 * just its bucket locality by its length.
4520 */
4521static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4522 struct ocfs2_xattr_info *xi,
4523 struct ocfs2_xattr_search *xs,
4524 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004525 int local)
Tao Ma01225592008-08-18 17:38:53 +08004526{
4527 struct ocfs2_xattr_entry *last, *xe;
4528 int name_len = strlen(xi->name);
4529 struct ocfs2_xattr_header *xh = xs->header;
4530 u16 count = le16_to_cpu(xh->xh_count), start;
4531 size_t blocksize = inode->i_sb->s_blocksize;
4532 char *val;
4533 size_t offs, size, new_size;
4534
4535 last = &xh->xh_entries[count];
4536 if (!xs->not_found) {
4537 xe = xs->here;
4538 offs = le16_to_cpu(xe->xe_name_offset);
4539 if (ocfs2_xattr_is_local(xe))
4540 size = OCFS2_XATTR_SIZE(name_len) +
4541 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4542 else
4543 size = OCFS2_XATTR_SIZE(name_len) +
4544 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4545
4546 /*
4547 * If the new value will be stored outside, xi->value has been
4548 * initalized as an empty ocfs2_xattr_value_root, and the same
4549 * goes with xi->value_len, so we can set new_size safely here.
4550 * See ocfs2_xattr_set_in_bucket.
4551 */
4552 new_size = OCFS2_XATTR_SIZE(name_len) +
4553 OCFS2_XATTR_SIZE(xi->value_len);
4554
4555 le16_add_cpu(&xh->xh_name_value_len, -size);
4556 if (xi->value) {
4557 if (new_size > size)
4558 goto set_new_name_value;
4559
4560 /* Now replace the old value with new one. */
4561 if (local)
4562 xe->xe_value_size = cpu_to_le64(xi->value_len);
4563 else
4564 xe->xe_value_size = 0;
4565
4566 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004567 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004568 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4569 size - OCFS2_XATTR_SIZE(name_len));
4570 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4571 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4572 xi->value, xi->value_len);
4573
4574 le16_add_cpu(&xh->xh_name_value_len, new_size);
4575 ocfs2_xattr_set_local(xe, local);
4576 return;
4577 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004578 /*
4579 * Remove the old entry if there is more than one.
4580 * We don't remove the last entry so that we can
4581 * use it to indicate the hash value of the empty
4582 * bucket.
4583 */
Tao Ma01225592008-08-18 17:38:53 +08004584 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004585 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004586 if (xh->xh_count) {
4587 memmove(xe, xe + 1,
4588 (void *)last - (void *)xe);
4589 memset(last, 0,
4590 sizeof(struct ocfs2_xattr_entry));
4591 } else
4592 xh->xh_free_start =
4593 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4594
Tao Ma01225592008-08-18 17:38:53 +08004595 return;
4596 }
4597 } else {
4598 /* find a new entry for insert. */
4599 int low = 0, high = count - 1, tmp;
4600 struct ocfs2_xattr_entry *tmp_xe;
4601
Tao Ma5a095612008-09-19 22:17:41 +08004602 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004603 tmp = (low + high) / 2;
4604 tmp_xe = &xh->xh_entries[tmp];
4605
4606 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4607 low = tmp + 1;
4608 else if (name_hash <
4609 le32_to_cpu(tmp_xe->xe_name_hash))
4610 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004611 else {
4612 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004613 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004614 }
Tao Ma01225592008-08-18 17:38:53 +08004615 }
4616
4617 xe = &xh->xh_entries[low];
4618 if (low != count)
4619 memmove(xe + 1, xe, (void *)last - (void *)xe);
4620
4621 le16_add_cpu(&xh->xh_count, 1);
4622 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4623 xe->xe_name_hash = cpu_to_le32(name_hash);
4624 xe->xe_name_len = name_len;
4625 ocfs2_xattr_set_type(xe, xi->name_index);
4626 }
4627
4628set_new_name_value:
4629 /* Insert the new name+value. */
4630 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4631
4632 /*
4633 * We must make sure that the name/value pair
4634 * exists in the same block.
4635 */
4636 offs = le16_to_cpu(xh->xh_free_start);
4637 start = offs - size;
4638
4639 if (start >> inode->i_sb->s_blocksize_bits !=
4640 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4641 offs = offs - offs % blocksize;
4642 xh->xh_free_start = cpu_to_le16(offs);
4643 }
4644
Joel Beckerba937122008-10-24 19:13:20 -07004645 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004646 xe->xe_name_offset = cpu_to_le16(offs - size);
4647
4648 memset(val, 0, size);
4649 memcpy(val, xi->name, name_len);
4650 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4651
4652 xe->xe_value_size = cpu_to_le64(xi->value_len);
4653 ocfs2_xattr_set_local(xe, local);
4654 xs->here = xe;
4655 le16_add_cpu(&xh->xh_free_start, -size);
4656 le16_add_cpu(&xh->xh_name_value_len, size);
4657
4658 return;
4659}
4660
Tao Ma01225592008-08-18 17:38:53 +08004661/*
4662 * Set the xattr entry in the specified bucket.
4663 * The bucket is indicated by xs->bucket and it should have the enough
4664 * space for the xattr insertion.
4665 */
4666static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004667 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004668 struct ocfs2_xattr_info *xi,
4669 struct ocfs2_xattr_search *xs,
4670 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004671 int local)
Tao Ma01225592008-08-18 17:38:53 +08004672{
Joel Becker1224be02008-10-24 18:47:33 -07004673 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004674 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004675
Mark Fashehff1ec202008-08-19 10:54:29 -07004676 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4677 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004678 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004679
Joel Beckerba937122008-10-24 19:13:20 -07004680 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004681 blkno = bucket_blkno(xs->bucket);
4682 ocfs2_xattr_bucket_relse(xs->bucket);
4683 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004684 if (ret) {
4685 mlog_errno(ret);
4686 goto out;
4687 }
4688 }
4689
Joel Beckerba937122008-10-24 19:13:20 -07004690 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004691 OCFS2_JOURNAL_ACCESS_WRITE);
4692 if (ret < 0) {
4693 mlog_errno(ret);
4694 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004695 }
4696
Tao Ma5a095612008-09-19 22:17:41 +08004697 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004698 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004699
Tao Ma01225592008-08-18 17:38:53 +08004700out:
Tao Ma01225592008-08-18 17:38:53 +08004701 return ret;
4702}
4703
Tao Ma01225592008-08-18 17:38:53 +08004704/*
4705 * Truncate the specified xe_off entry in xattr bucket.
4706 * bucket is indicated by header_bh and len is the new length.
4707 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4708 *
4709 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4710 */
4711static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004712 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004713 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004714 int len,
4715 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004716{
4717 int ret, offset;
4718 u64 value_blk;
Tao Ma01225592008-08-18 17:38:53 +08004719 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004720 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004721 size_t blocksize = inode->i_sb->s_blocksize;
Joel Beckerb3e5d372008-12-09 15:01:04 -08004722 struct ocfs2_xattr_value_buf vb = {
4723 .vb_access = ocfs2_journal_access,
4724 };
Tao Ma01225592008-08-18 17:38:53 +08004725
4726 xe = &xh->xh_entries[xe_off];
4727
4728 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4729
4730 offset = le16_to_cpu(xe->xe_name_offset) +
4731 OCFS2_XATTR_SIZE(xe->xe_name_len);
4732
4733 value_blk = offset / blocksize;
4734
4735 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4736 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004737
Joel Beckerb3e5d372008-12-09 15:01:04 -08004738 vb.vb_bh = bucket->bu_bhs[value_blk];
4739 BUG_ON(!vb.vb_bh);
Tao Ma01225592008-08-18 17:38:53 +08004740
Joel Beckerb3e5d372008-12-09 15:01:04 -08004741 vb.vb_xv = (struct ocfs2_xattr_value_root *)
4742 (vb.vb_bh->b_data + offset % blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004743
Joel Becker548b0f22008-11-24 19:32:13 -08004744 /*
4745 * From here on out we have to dirty the bucket. The generic
4746 * value calls only modify one of the bucket's bhs, but we need
4747 * to send the bucket at once. So if they error, they *could* have
4748 * modified something. We have to assume they did, and dirty
4749 * the whole bucket. This leaves us in a consistent state.
4750 */
Tao Ma01225592008-08-18 17:38:53 +08004751 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004752 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Joel Beckerb3e5d372008-12-09 15:01:04 -08004753 ret = ocfs2_xattr_value_truncate(inode, &vb, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004754 if (ret) {
4755 mlog_errno(ret);
Tao Ma554e7f92009-01-08 08:21:43 +08004756 goto out;
4757 }
4758
4759 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4760 OCFS2_JOURNAL_ACCESS_WRITE);
4761 if (ret) {
4762 mlog_errno(ret);
4763 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004764 }
4765
Joel Becker548b0f22008-11-24 19:32:13 -08004766 xe->xe_value_size = cpu_to_le64(len);
4767
Joel Becker548b0f22008-11-24 19:32:13 -08004768 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004769
4770out:
Tao Ma01225592008-08-18 17:38:53 +08004771 return ret;
4772}
4773
4774static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004775 struct ocfs2_xattr_search *xs,
4776 int len,
4777 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004778{
4779 int ret, offset;
4780 struct ocfs2_xattr_entry *xe = xs->here;
4781 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4782
Joel Beckerba937122008-10-24 19:13:20 -07004783 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004784
4785 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08004786 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08004787 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004788 if (ret)
4789 mlog_errno(ret);
4790
4791 return ret;
4792}
4793
4794static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004795 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004796 struct ocfs2_xattr_search *xs,
4797 char *val,
4798 int value_len)
4799{
Tao Ma712e53e2009-03-12 08:37:34 +08004800 int ret, offset, block_off;
Tao Ma01225592008-08-18 17:38:53 +08004801 struct ocfs2_xattr_value_root *xv;
4802 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma712e53e2009-03-12 08:37:34 +08004803 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
4804 void *base;
Tao Ma01225592008-08-18 17:38:53 +08004805
4806 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4807
Tao Ma712e53e2009-03-12 08:37:34 +08004808 ret = ocfs2_xattr_bucket_get_name_value(inode, xh,
4809 xe - xh->xh_entries,
4810 &block_off,
4811 &offset);
4812 if (ret) {
4813 mlog_errno(ret);
4814 goto out;
4815 }
Tao Ma01225592008-08-18 17:38:53 +08004816
Tao Ma712e53e2009-03-12 08:37:34 +08004817 base = bucket_block(xs->bucket, block_off);
4818 xv = (struct ocfs2_xattr_value_root *)(base + offset +
4819 OCFS2_XATTR_SIZE(xe->xe_name_len));
Tao Ma01225592008-08-18 17:38:53 +08004820
Tao Ma712e53e2009-03-12 08:37:34 +08004821 ret = __ocfs2_xattr_set_value_outside(inode, handle,
4822 xv, val, value_len);
4823 if (ret)
4824 mlog_errno(ret);
4825out:
4826 return ret;
Tao Ma01225592008-08-18 17:38:53 +08004827}
4828
Tao Ma01225592008-08-18 17:38:53 +08004829static int ocfs2_rm_xattr_cluster(struct inode *inode,
4830 struct buffer_head *root_bh,
4831 u64 blkno,
4832 u32 cpos,
4833 u32 len)
4834{
4835 int ret;
4836 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4837 struct inode *tl_inode = osb->osb_tl_inode;
4838 handle_t *handle;
4839 struct ocfs2_xattr_block *xb =
4840 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004841 struct ocfs2_alloc_context *meta_ac = NULL;
4842 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004843 struct ocfs2_extent_tree et;
4844
Joel Becker5e404e92009-02-13 03:54:22 -08004845 ocfs2_init_xattr_tree_extent_tree(&et, INODE_CACHE(inode), root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004846
4847 ocfs2_init_dealloc_ctxt(&dealloc);
4848
4849 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4850 cpos, len, (unsigned long long)blkno);
4851
Joel Becker8cb471e2009-02-10 20:00:41 -08004852 ocfs2_remove_xattr_clusters_from_cache(INODE_CACHE(inode), blkno,
4853 len);
Tao Ma01225592008-08-18 17:38:53 +08004854
Joel Beckerf99b9b72008-08-20 19:36:33 -07004855 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004856 if (ret) {
4857 mlog_errno(ret);
4858 return ret;
4859 }
4860
4861 mutex_lock(&tl_inode->i_mutex);
4862
4863 if (ocfs2_truncate_log_needs_flush(osb)) {
4864 ret = __ocfs2_flush_truncate_log(osb);
4865 if (ret < 0) {
4866 mlog_errno(ret);
4867 goto out;
4868 }
4869 }
4870
Jan Karaa90714c2008-10-09 19:38:40 +02004871 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08004872 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004873 ret = -ENOMEM;
4874 mlog_errno(ret);
4875 goto out;
4876 }
4877
Joel Becker0cf2f762009-02-12 16:41:25 -08004878 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), root_bh,
Joel Becker84008972008-12-09 16:11:49 -08004879 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004880 if (ret) {
4881 mlog_errno(ret);
4882 goto out_commit;
4883 }
4884
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08004885 ret = ocfs2_remove_extent(handle, &et, cpos, len, meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004886 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004887 if (ret) {
4888 mlog_errno(ret);
4889 goto out_commit;
4890 }
4891
4892 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4893
4894 ret = ocfs2_journal_dirty(handle, root_bh);
4895 if (ret) {
4896 mlog_errno(ret);
4897 goto out_commit;
4898 }
4899
4900 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4901 if (ret)
4902 mlog_errno(ret);
4903
4904out_commit:
4905 ocfs2_commit_trans(osb, handle);
4906out:
4907 ocfs2_schedule_truncate_log_flush(osb, 1);
4908
4909 mutex_unlock(&tl_inode->i_mutex);
4910
4911 if (meta_ac)
4912 ocfs2_free_alloc_context(meta_ac);
4913
4914 ocfs2_run_deallocs(osb, &dealloc);
4915
4916 return ret;
4917}
4918
Tao Ma01225592008-08-18 17:38:53 +08004919static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004920 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004921 struct ocfs2_xattr_search *xs)
4922{
Joel Beckerba937122008-10-24 19:13:20 -07004923 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004924 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4925 le16_to_cpu(xh->xh_count) - 1];
4926 int ret = 0;
4927
Joel Beckerba937122008-10-24 19:13:20 -07004928 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004929 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004930 if (ret) {
4931 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004932 return;
Tao Ma01225592008-08-18 17:38:53 +08004933 }
4934
4935 /* Remove the old entry. */
4936 memmove(xs->here, xs->here + 1,
4937 (void *)last - (void *)xs->here);
4938 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4939 le16_add_cpu(&xh->xh_count, -1);
4940
Joel Beckerba937122008-10-24 19:13:20 -07004941 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004942}
4943
4944/*
4945 * Set the xattr name/value in the bucket specified in xs.
4946 *
4947 * As the new value in xi may be stored in the bucket or in an outside cluster,
4948 * we divide the whole process into 3 steps:
4949 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4950 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4951 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4952 * 4. If the clusters for the new outside value can't be allocated, we need
4953 * to free the xattr we allocated in set.
4954 */
4955static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4956 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004957 struct ocfs2_xattr_search *xs,
4958 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004959{
Tao Ma5a095612008-09-19 22:17:41 +08004960 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004961 size_t value_len;
4962 char *val = (char *)xi->value;
4963 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004964 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4965 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004966
4967 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4968 /*
4969 * We need to truncate the xattr storage first.
4970 *
4971 * If both the old and new value are stored to
4972 * outside block, we only need to truncate
4973 * the storage and then set the value outside.
4974 *
4975 * If the new value should be stored within block,
4976 * we should free all the outside block first and
4977 * the modification to the xattr block will be done
4978 * by following steps.
4979 */
4980 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4981 value_len = xi->value_len;
4982 else
4983 value_len = 0;
4984
4985 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004986 value_len,
4987 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004988 if (ret)
4989 goto out;
4990
4991 if (value_len)
4992 goto set_value_outside;
4993 }
4994
4995 value_len = xi->value_len;
4996 /* So we have to handle the inside block change now. */
4997 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4998 /*
4999 * If the new value will be stored outside of block,
5000 * initalize a new empty value root and insert it first.
5001 */
5002 local = 0;
5003 xi->value = &def_xv;
5004 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
5005 }
5006
Tao Ma85db90e2008-11-12 08:27:01 +08005007 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5008 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08005009 if (ret) {
5010 mlog_errno(ret);
5011 goto out;
5012 }
5013
Tao Ma5a095612008-09-19 22:17:41 +08005014 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5015 goto out;
Tao Ma01225592008-08-18 17:38:53 +08005016
Tao Ma5a095612008-09-19 22:17:41 +08005017 /* allocate the space now for the outside block storage. */
5018 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08005019 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08005020 if (ret) {
5021 mlog_errno(ret);
5022
5023 if (xs->not_found) {
5024 /*
5025 * We can't allocate enough clusters for outside
5026 * storage and we have allocated xattr already,
5027 * so need to remove it.
5028 */
Tao Ma85db90e2008-11-12 08:27:01 +08005029 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08005030 }
Tao Ma01225592008-08-18 17:38:53 +08005031 goto out;
5032 }
5033
5034set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08005035 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5036 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08005037out:
5038 return ret;
5039}
5040
Tao Ma80bcaf32008-10-27 06:06:24 +08005041/*
5042 * check whether the xattr bucket is filled up with the same hash value.
5043 * If we want to insert the xattr with the same hash, return -ENOSPC.
5044 * If we want to insert a xattr with different hash value, go ahead
5045 * and ocfs2_divide_xattr_bucket will handle this.
5046 */
Tao Ma01225592008-08-18 17:38:53 +08005047static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08005048 struct ocfs2_xattr_bucket *bucket,
5049 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08005050{
Joel Becker3e632942008-10-24 17:04:49 -07005051 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08005052 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
5053
5054 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
5055 return 0;
Tao Ma01225592008-08-18 17:38:53 +08005056
5057 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
5058 xh->xh_entries[0].xe_name_hash) {
5059 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
5060 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07005061 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08005062 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
5063 return -ENOSPC;
5064 }
5065
5066 return 0;
5067}
5068
5069static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5070 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08005071 struct ocfs2_xattr_search *xs,
5072 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08005073{
5074 struct ocfs2_xattr_header *xh;
5075 struct ocfs2_xattr_entry *xe;
5076 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07005077 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08005078 size_t value_size = 0, name_len = strlen(xi->name);
5079 size_t blocksize = inode->i_sb->s_blocksize;
5080 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005081
5082 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5083
5084try_again:
5085 xh = xs->header;
5086 count = le16_to_cpu(xh->xh_count);
5087 xh_free_start = le16_to_cpu(xh->xh_free_start);
5088 header_size = sizeof(struct ocfs2_xattr_header) +
5089 count * sizeof(struct ocfs2_xattr_entry);
Tiger Yang4442f512009-02-20 11:11:50 +08005090 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5091 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005092
5093 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5094 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005095 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005096 header_size);
5097
5098 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5099 value_size = OCFS2_XATTR_ROOT_SIZE;
5100 else if (xi->value)
5101 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5102
5103 if (xs->not_found)
5104 need = sizeof(struct ocfs2_xattr_entry) +
5105 OCFS2_XATTR_SIZE(name_len) + value_size;
5106 else {
5107 need = value_size + OCFS2_XATTR_SIZE(name_len);
5108
5109 /*
5110 * We only replace the old value if the new length is smaller
5111 * than the old one. Otherwise we will allocate new space in the
5112 * bucket to store it.
5113 */
5114 xe = xs->here;
5115 if (ocfs2_xattr_is_local(xe))
5116 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5117 else
5118 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5119
5120 if (old >= value_size)
5121 need = 0;
5122 }
5123
Tiger Yang4442f512009-02-20 11:11:50 +08005124 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005125 /*
5126 * We need to make sure the new name/value pair
5127 * can exist in the same block.
5128 */
5129 if (xh_free_start % blocksize < need)
5130 free -= xh_free_start % blocksize;
5131
5132 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5133 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5134 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005135 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005136 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5137 le16_to_cpu(xh->xh_name_value_len));
5138
Tao Ma976331d2008-11-12 08:26:57 +08005139 if (free < need ||
5140 (xs->not_found &&
5141 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005142 if (need <= max_free &&
5143 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5144 /*
5145 * We can create the space by defragment. Since only the
5146 * name/value will be moved, the xe shouldn't be changed
5147 * in xs.
5148 */
Tao Ma85db90e2008-11-12 08:27:01 +08005149 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5150 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005151 if (ret) {
5152 mlog_errno(ret);
5153 goto out;
5154 }
5155
5156 xh_free_start = le16_to_cpu(xh->xh_free_start);
Tiger Yang4442f512009-02-20 11:11:50 +08005157 free = xh_free_start - header_size
5158 - OCFS2_XATTR_HEADER_GAP;
Tao Ma01225592008-08-18 17:38:53 +08005159 if (xh_free_start % blocksize < need)
5160 free -= xh_free_start % blocksize;
5161
5162 if (free >= need)
5163 goto xattr_set;
5164
5165 mlog(0, "Can't get enough space for xattr insert by "
5166 "defragment. Need %u bytes, but we have %d, so "
5167 "allocate new bucket for it.\n", need, free);
5168 }
5169
5170 /*
5171 * We have to add new buckets or clusters and one
5172 * allocation should leave us enough space for insert.
5173 */
5174 BUG_ON(allocation);
5175
5176 /*
5177 * We do not allow for overlapping ranges between buckets. And
5178 * the maximum number of collisions we will allow for then is
5179 * one bucket's worth, so check it here whether we need to
5180 * add a new bucket for the insert.
5181 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005182 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005183 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005184 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005185 if (ret) {
5186 mlog_errno(ret);
5187 goto out;
5188 }
5189
5190 ret = ocfs2_add_new_xattr_bucket(inode,
5191 xs->xattr_bh,
Joel Becker91f20332008-11-26 15:25:41 -08005192 xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005193 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005194 if (ret) {
5195 mlog_errno(ret);
5196 goto out;
5197 }
5198
Joel Becker91f20332008-11-26 15:25:41 -08005199 /*
5200 * ocfs2_add_new_xattr_bucket() will have updated
5201 * xs->bucket if it moved, but it will not have updated
5202 * any of the other search fields. Thus, we drop it and
5203 * re-search. Everything should be cached, so it'll be
5204 * quick.
5205 */
Joel Beckerba937122008-10-24 19:13:20 -07005206 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005207 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5208 xi->name_index,
5209 xi->name, xs);
5210 if (ret && ret != -ENODATA)
5211 goto out;
5212 xs->not_found = ret;
5213 allocation = 1;
5214 goto try_again;
5215 }
5216
5217xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005218 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005219out:
5220 mlog_exit(ret);
5221 return ret;
5222}
Tao Maa3944252008-08-18 17:38:54 +08005223
5224static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5225 struct ocfs2_xattr_bucket *bucket,
5226 void *para)
5227{
5228 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07005229 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005230 u16 i;
5231 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005232 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5233 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005234 int credits = ocfs2_remove_extent_credits(osb->sb) +
5235 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5236
Tao Ma78f30c32008-11-12 08:27:00 +08005237
5238 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005239
5240 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5241 xe = &xh->xh_entries[i];
5242 if (ocfs2_xattr_is_local(xe))
5243 continue;
5244
Tao Ma88c3b062008-12-11 08:54:11 +08005245 ctxt.handle = ocfs2_start_trans(osb, credits);
5246 if (IS_ERR(ctxt.handle)) {
5247 ret = PTR_ERR(ctxt.handle);
5248 mlog_errno(ret);
5249 break;
5250 }
5251
Joel Becker548b0f22008-11-24 19:32:13 -08005252 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005253 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005254
5255 ocfs2_commit_trans(osb, ctxt.handle);
Tao Maa3944252008-08-18 17:38:54 +08005256 if (ret) {
5257 mlog_errno(ret);
5258 break;
5259 }
5260 }
5261
Tao Ma78f30c32008-11-12 08:27:00 +08005262 ocfs2_schedule_truncate_log_flush(osb, 1);
5263 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005264 return ret;
5265}
5266
5267static int ocfs2_delete_xattr_index_block(struct inode *inode,
5268 struct buffer_head *xb_bh)
5269{
5270 struct ocfs2_xattr_block *xb =
5271 (struct ocfs2_xattr_block *)xb_bh->b_data;
5272 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5273 int ret = 0;
5274 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5275 u64 p_blkno;
5276
5277 if (le16_to_cpu(el->l_next_free_rec) == 0)
5278 return 0;
5279
5280 while (name_hash > 0) {
5281 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5282 &e_cpos, &num_clusters, el);
5283 if (ret) {
5284 mlog_errno(ret);
5285 goto out;
5286 }
5287
5288 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5289 ocfs2_delete_xattr_in_bucket,
5290 NULL);
5291 if (ret) {
5292 mlog_errno(ret);
5293 goto out;
5294 }
5295
5296 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5297 p_blkno, e_cpos, num_clusters);
5298 if (ret) {
5299 mlog_errno(ret);
5300 break;
5301 }
5302
5303 if (e_cpos == 0)
5304 break;
5305
5306 name_hash = e_cpos - 1;
5307 }
5308
5309out:
5310 return ret;
5311}
Mark Fasheh99219ae2008-10-07 14:52:59 -07005312
5313/*
Tiger Yang923f7f32008-11-14 11:16:27 +08005314 * 'security' attributes support
5315 */
5316static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5317 size_t list_size, const char *name,
5318 size_t name_len)
5319{
5320 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5321 const size_t total_len = prefix_len + name_len + 1;
5322
5323 if (list && total_len <= list_size) {
5324 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5325 memcpy(list + prefix_len, name, name_len);
5326 list[prefix_len + name_len] = '\0';
5327 }
5328 return total_len;
5329}
5330
5331static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5332 void *buffer, size_t size)
5333{
5334 if (strcmp(name, "") == 0)
5335 return -EINVAL;
5336 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5337 buffer, size);
5338}
5339
5340static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5341 const void *value, size_t size, int flags)
5342{
5343 if (strcmp(name, "") == 0)
5344 return -EINVAL;
5345
5346 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5347 size, flags);
5348}
5349
Tiger Yang534eadd2008-11-14 11:16:41 +08005350int ocfs2_init_security_get(struct inode *inode,
5351 struct inode *dir,
5352 struct ocfs2_security_xattr_info *si)
5353{
Tiger Yang38d59ef2008-12-17 10:22:56 +08005354 /* check whether ocfs2 support feature xattr */
5355 if (!ocfs2_supports_xattr(OCFS2_SB(dir->i_sb)))
5356 return -EOPNOTSUPP;
Tiger Yang534eadd2008-11-14 11:16:41 +08005357 return security_inode_init_security(inode, dir, &si->name, &si->value,
5358 &si->value_len);
5359}
5360
5361int ocfs2_init_security_set(handle_t *handle,
5362 struct inode *inode,
5363 struct buffer_head *di_bh,
5364 struct ocfs2_security_xattr_info *si,
5365 struct ocfs2_alloc_context *xattr_ac,
5366 struct ocfs2_alloc_context *data_ac)
5367{
5368 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5369 OCFS2_XATTR_INDEX_SECURITY,
5370 si->name, si->value, si->value_len, 0,
5371 xattr_ac, data_ac);
5372}
5373
Tiger Yang923f7f32008-11-14 11:16:27 +08005374struct xattr_handler ocfs2_xattr_security_handler = {
5375 .prefix = XATTR_SECURITY_PREFIX,
5376 .list = ocfs2_xattr_security_list,
5377 .get = ocfs2_xattr_security_get,
5378 .set = ocfs2_xattr_security_set,
5379};
5380
5381/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07005382 * 'trusted' attributes support
5383 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005384static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5385 size_t list_size, const char *name,
5386 size_t name_len)
5387{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005388 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005389 const size_t total_len = prefix_len + name_len + 1;
5390
5391 if (list && total_len <= list_size) {
5392 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5393 memcpy(list + prefix_len, name, name_len);
5394 list[prefix_len + name_len] = '\0';
5395 }
5396 return total_len;
5397}
5398
5399static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5400 void *buffer, size_t size)
5401{
5402 if (strcmp(name, "") == 0)
5403 return -EINVAL;
5404 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5405 buffer, size);
5406}
5407
5408static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5409 const void *value, size_t size, int flags)
5410{
5411 if (strcmp(name, "") == 0)
5412 return -EINVAL;
5413
5414 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5415 size, flags);
5416}
5417
5418struct xattr_handler ocfs2_xattr_trusted_handler = {
5419 .prefix = XATTR_TRUSTED_PREFIX,
5420 .list = ocfs2_xattr_trusted_list,
5421 .get = ocfs2_xattr_trusted_get,
5422 .set = ocfs2_xattr_trusted_set,
5423};
5424
Mark Fasheh99219ae2008-10-07 14:52:59 -07005425/*
5426 * 'user' attributes support
5427 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005428static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5429 size_t list_size, const char *name,
5430 size_t name_len)
5431{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005432 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005433 const size_t total_len = prefix_len + name_len + 1;
5434 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5435
5436 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5437 return 0;
5438
5439 if (list && total_len <= list_size) {
5440 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5441 memcpy(list + prefix_len, name, name_len);
5442 list[prefix_len + name_len] = '\0';
5443 }
5444 return total_len;
5445}
5446
5447static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5448 void *buffer, size_t size)
5449{
5450 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5451
5452 if (strcmp(name, "") == 0)
5453 return -EINVAL;
5454 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5455 return -EOPNOTSUPP;
5456 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5457 buffer, size);
5458}
5459
5460static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5461 const void *value, size_t size, int flags)
5462{
5463 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5464
5465 if (strcmp(name, "") == 0)
5466 return -EINVAL;
5467 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5468 return -EOPNOTSUPP;
5469
5470 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5471 size, flags);
5472}
5473
5474struct xattr_handler ocfs2_xattr_user_handler = {
5475 .prefix = XATTR_USER_PREFIX,
5476 .list = ocfs2_xattr_user_list,
5477 .get = ocfs2_xattr_user_get,
5478 .set = ocfs2_xattr_user_set,
5479};