blob: 2e273c2cb83103803bbb86bf29ebbe2628eed822 [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"
45#include "dlmglue.h"
46#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080047#include "symlink.h"
48#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080049#include "inode.h"
50#include "journal.h"
51#include "ocfs2_fs.h"
52#include "suballoc.h"
53#include "uptodate.h"
54#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080055#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080056#include "xattr.h"
57
58
59struct ocfs2_xattr_def_value_root {
60 struct ocfs2_xattr_value_root xv;
61 struct ocfs2_extent_rec er;
62};
63
Tao Ma0c044f02008-08-18 17:38:50 +080064struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070065 /* The inode these xattrs are associated with */
66 struct inode *bu_inode;
67
68 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070069 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070070
71 /* How many blocks make up one bucket for this filesystem */
72 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080073};
74
Tao Ma78f30c32008-11-12 08:27:00 +080075struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080076 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080077 struct ocfs2_alloc_context *meta_ac;
78 struct ocfs2_alloc_context *data_ac;
79 struct ocfs2_cached_dealloc_ctxt dealloc;
80};
81
Tiger Yangcf1d6c72008-08-18 17:11:00 +080082#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
83#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang534eadd2008-11-14 11:16:41 +080084#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
85 - sizeof(struct ocfs2_xattr_header) \
86 - sizeof(__u32))
Tiger Yangcf1d6c72008-08-18 17:11:00 +080087
88static struct ocfs2_xattr_def_value_root def_xv = {
89 .xv.xr_list.l_count = cpu_to_le16(1),
90};
91
92struct xattr_handler *ocfs2_xattr_handlers[] = {
93 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +080094#ifdef CONFIG_OCFS2_FS_POSIX_ACL
95 &ocfs2_xattr_acl_access_handler,
96 &ocfs2_xattr_acl_default_handler,
97#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +080098 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +080099 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800100 NULL
101};
102
Tiger Yangc988fd02008-10-23 16:34:44 +0800103static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800104 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800105#ifdef CONFIG_OCFS2_FS_POSIX_ACL
106 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
107 = &ocfs2_xattr_acl_access_handler,
108 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
109 = &ocfs2_xattr_acl_default_handler,
110#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800111 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800112 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800113};
114
115struct ocfs2_xattr_info {
116 int name_index;
117 const char *name;
118 const void *value;
119 size_t value_len;
120};
121
122struct ocfs2_xattr_search {
123 struct buffer_head *inode_bh;
124 /*
125 * xattr_bh point to the block buffer head which has extended attribute
126 * when extended attribute in inode, xattr_bh is equal to inode_bh.
127 */
128 struct buffer_head *xattr_bh;
129 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700130 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800131 void *base;
132 void *end;
133 struct ocfs2_xattr_entry *here;
134 int not_found;
135};
136
Tao Ma589dc262008-08-18 17:38:51 +0800137static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
138 struct ocfs2_xattr_header *xh,
139 int index,
140 int *block_off,
141 int *new_offset);
142
Joel Becker54f443f2008-10-20 18:43:07 -0700143static int ocfs2_xattr_block_find(struct inode *inode,
144 int name_index,
145 const char *name,
146 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800147static int ocfs2_xattr_index_block_find(struct inode *inode,
148 struct buffer_head *root_bh,
149 int name_index,
150 const char *name,
151 struct ocfs2_xattr_search *xs);
152
Tao Ma0c044f02008-08-18 17:38:50 +0800153static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
154 struct ocfs2_xattr_tree_root *xt,
155 char *buffer,
156 size_t buffer_size);
157
Tao Ma01225592008-08-18 17:38:53 +0800158static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800159 struct ocfs2_xattr_search *xs,
160 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800161
162static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
163 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800164 struct ocfs2_xattr_search *xs,
165 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800166
Tao Maa3944252008-08-18 17:38:54 +0800167static int ocfs2_delete_xattr_index_block(struct inode *inode,
168 struct buffer_head *xb_bh);
169
Tiger Yang0030e002008-10-23 16:33:33 +0800170static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
171{
172 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
173}
174
175static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
176{
177 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
178}
179
180static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
181{
182 u16 len = sb->s_blocksize -
183 offsetof(struct ocfs2_xattr_header, xh_entries);
184
185 return len / sizeof(struct ocfs2_xattr_entry);
186}
187
Joel Becker9c7759a2008-10-24 16:21:03 -0700188#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700189#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700190#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700191
Joel Beckerba937122008-10-24 19:13:20 -0700192static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700193{
Joel Beckerba937122008-10-24 19:13:20 -0700194 struct ocfs2_xattr_bucket *bucket;
195 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700196
Joel Beckerba937122008-10-24 19:13:20 -0700197 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
198
199 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
200 if (bucket) {
201 bucket->bu_inode = inode;
202 bucket->bu_blocks = blks;
203 }
204
205 return bucket;
206}
207
208static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
209{
210 int i;
211
212 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700213 brelse(bucket->bu_bhs[i]);
214 bucket->bu_bhs[i] = NULL;
215 }
216}
217
Joel Beckerba937122008-10-24 19:13:20 -0700218static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
219{
220 if (bucket) {
221 ocfs2_xattr_bucket_relse(bucket);
222 bucket->bu_inode = NULL;
223 kfree(bucket);
224 }
225}
226
Joel Becker784b8162008-10-24 17:33:40 -0700227/*
228 * A bucket that has never been written to disk doesn't need to be
229 * read. We just need the buffer_heads. Don't call this for
230 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
231 * them fully.
232 */
Joel Beckerba937122008-10-24 19:13:20 -0700233static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700234 u64 xb_blkno)
235{
236 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700237
Joel Beckerba937122008-10-24 19:13:20 -0700238 for (i = 0; i < bucket->bu_blocks; i++) {
239 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
240 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700241 if (!bucket->bu_bhs[i]) {
242 rc = -EIO;
243 mlog_errno(rc);
244 break;
245 }
246
Tao Ma757055a2008-11-06 08:10:48 +0800247 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
248 bucket->bu_bhs[i]))
249 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
250 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700251 }
252
253 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700254 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700255 return rc;
256}
257
258/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700259static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700260 u64 xb_blkno)
261{
Joel Beckerba937122008-10-24 19:13:20 -0700262 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700263
Joel Beckerba937122008-10-24 19:13:20 -0700264 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
265 bucket->bu_blocks, bucket->bu_bhs, 0);
Joel Becker784b8162008-10-24 17:33:40 -0700266 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700267 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700268 return rc;
269}
270
Joel Becker1224be02008-10-24 18:47:33 -0700271static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700272 struct ocfs2_xattr_bucket *bucket,
273 int type)
274{
275 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700276
Joel Beckerba937122008-10-24 19:13:20 -0700277 for (i = 0; i < bucket->bu_blocks; i++) {
278 rc = ocfs2_journal_access(handle, bucket->bu_inode,
Joel Becker1224be02008-10-24 18:47:33 -0700279 bucket->bu_bhs[i], type);
280 if (rc) {
281 mlog_errno(rc);
282 break;
283 }
284 }
285
286 return rc;
287}
288
289static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700290 struct ocfs2_xattr_bucket *bucket)
291{
Joel Beckerba937122008-10-24 19:13:20 -0700292 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700293
Joel Beckerba937122008-10-24 19:13:20 -0700294 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700295 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
296}
297
Joel Beckerba937122008-10-24 19:13:20 -0700298static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700299 struct ocfs2_xattr_bucket *src)
300{
301 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700302 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700303
Joel Beckerba937122008-10-24 19:13:20 -0700304 BUG_ON(dest->bu_blocks != src->bu_blocks);
305 BUG_ON(dest->bu_inode != src->bu_inode);
306
307 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700308 memcpy(bucket_block(dest, i), bucket_block(src, i),
309 blocksize);
310 }
311}
Joel Becker1224be02008-10-24 18:47:33 -0700312
Tao Ma936b8832008-10-09 23:06:14 +0800313static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800314{
315 struct xattr_handler *handler = NULL;
316
317 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
318 handler = ocfs2_xattr_handler_map[name_index];
319
Tao Ma936b8832008-10-09 23:06:14 +0800320 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800321}
322
Mark Fasheh40daa162008-10-07 14:31:42 -0700323static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800324 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700325 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800326{
327 /* Get hash value of uuid from super block */
328 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
329 int i;
330
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800331 /* hash extended attribute name */
332 for (i = 0; i < name_len; i++) {
333 hash = (hash << OCFS2_HASH_SHIFT) ^
334 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
335 *name++;
336 }
337
338 return hash;
339}
340
341/*
342 * ocfs2_xattr_hash_entry()
343 *
344 * Compute the hash of an extended attribute.
345 */
346static void ocfs2_xattr_hash_entry(struct inode *inode,
347 struct ocfs2_xattr_header *header,
348 struct ocfs2_xattr_entry *entry)
349{
350 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800351 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800352
Tao Ma2057e5c2008-10-09 23:06:13 +0800353 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800354 entry->xe_name_hash = cpu_to_le32(hash);
355
356 return;
357}
Tao Maf56654c2008-08-18 17:38:48 +0800358
Tiger Yang534eadd2008-11-14 11:16:41 +0800359static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
360{
361 int size = 0;
362
363 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
364 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
365 else
366 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
367 size += sizeof(struct ocfs2_xattr_entry);
368
369 return size;
370}
371
372int ocfs2_calc_security_init(struct inode *dir,
373 struct ocfs2_security_xattr_info *si,
374 int *want_clusters,
375 int *xattr_credits,
376 struct ocfs2_alloc_context **xattr_ac)
377{
378 int ret = 0;
379 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
380 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
381 si->value_len);
382
383 /*
384 * The max space of security xattr taken inline is
385 * 256(name) + 80(value) + 16(entry) = 352 bytes,
386 * So reserve one metadata block for it is ok.
387 */
388 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
389 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
390 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
391 if (ret) {
392 mlog_errno(ret);
393 return ret;
394 }
395 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
396 }
397
398 /* reserve clusters for xattr value which will be set in B tree*/
399 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
400 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
401 si->value_len);
402 return ret;
403}
404
Tao Maf56654c2008-08-18 17:38:48 +0800405static int ocfs2_xattr_extend_allocation(struct inode *inode,
406 u32 clusters_to_add,
407 struct buffer_head *xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800408 struct ocfs2_xattr_value_root *xv,
409 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800410{
411 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800412 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800413 enum ocfs2_alloc_restarted why;
414 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Maf56654c2008-08-18 17:38:48 +0800415 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700416 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800417
418 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
419
Joel Becker8d6220d2008-08-22 12:46:09 -0700420 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700421
Tao Maf56654c2008-08-18 17:38:48 +0800422 status = ocfs2_journal_access(handle, inode, xattr_bh,
423 OCFS2_JOURNAL_ACCESS_WRITE);
424 if (status < 0) {
425 mlog_errno(status);
426 goto leave;
427 }
428
429 prev_clusters = le32_to_cpu(xv->xr_clusters);
430 status = ocfs2_add_clusters_in_btree(osb,
431 inode,
432 &logical_start,
433 clusters_to_add,
434 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700435 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800436 handle,
Tao Ma78f30c32008-11-12 08:27:00 +0800437 ctxt->data_ac,
438 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700439 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800440 if (status < 0) {
441 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800442 goto leave;
443 }
444
445 status = ocfs2_journal_dirty(handle, xattr_bh);
446 if (status < 0) {
447 mlog_errno(status);
448 goto leave;
449 }
450
451 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
452
Tao Ma85db90e2008-11-12 08:27:01 +0800453 /*
454 * We should have already allocated enough space before the transaction,
455 * so no need to restart.
456 */
457 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800458
459leave:
Tao Maf56654c2008-08-18 17:38:48 +0800460
461 return status;
462}
463
464static int __ocfs2_remove_xattr_range(struct inode *inode,
465 struct buffer_head *root_bh,
466 struct ocfs2_xattr_value_root *xv,
467 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma78f30c32008-11-12 08:27:00 +0800468 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800469{
470 int ret;
471 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800472 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700473 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800474
Joel Becker8d6220d2008-08-22 12:46:09 -0700475 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700476
Tao Maf56654c2008-08-18 17:38:48 +0800477 ret = ocfs2_journal_access(handle, inode, root_bh,
478 OCFS2_JOURNAL_ACCESS_WRITE);
479 if (ret) {
480 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800481 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800482 }
483
Tao Ma78f30c32008-11-12 08:27:00 +0800484 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
485 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800486 if (ret) {
487 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800488 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800489 }
490
491 le32_add_cpu(&xv->xr_clusters, -len);
492
493 ret = ocfs2_journal_dirty(handle, root_bh);
494 if (ret) {
495 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800496 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800497 }
498
Tao Ma78f30c32008-11-12 08:27:00 +0800499 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800500 if (ret)
501 mlog_errno(ret);
502
Tao Maf56654c2008-08-18 17:38:48 +0800503out:
Tao Maf56654c2008-08-18 17:38:48 +0800504 return ret;
505}
506
507static int ocfs2_xattr_shrink_size(struct inode *inode,
508 u32 old_clusters,
509 u32 new_clusters,
510 struct buffer_head *root_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800511 struct ocfs2_xattr_value_root *xv,
512 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800513{
514 int ret = 0;
515 u32 trunc_len, cpos, phys_cpos, alloc_size;
516 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800517
518 if (old_clusters <= new_clusters)
519 return 0;
520
521 cpos = new_clusters;
522 trunc_len = old_clusters - new_clusters;
523 while (trunc_len) {
524 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
525 &alloc_size, &xv->xr_list);
526 if (ret) {
527 mlog_errno(ret);
528 goto out;
529 }
530
531 if (alloc_size > trunc_len)
532 alloc_size = trunc_len;
533
534 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
535 phys_cpos, alloc_size,
Tao Ma78f30c32008-11-12 08:27:00 +0800536 ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800537 if (ret) {
538 mlog_errno(ret);
539 goto out;
540 }
541
542 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
543 ocfs2_remove_xattr_clusters_from_cache(inode, block,
544 alloc_size);
545 cpos += alloc_size;
546 trunc_len -= alloc_size;
547 }
548
549out:
Tao Maf56654c2008-08-18 17:38:48 +0800550 return ret;
551}
552
553static int ocfs2_xattr_value_truncate(struct inode *inode,
554 struct buffer_head *root_bh,
555 struct ocfs2_xattr_value_root *xv,
Tao Ma78f30c32008-11-12 08:27:00 +0800556 int len,
557 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800558{
559 int ret;
560 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
561 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
562
563 if (new_clusters == old_clusters)
564 return 0;
565
566 if (new_clusters > old_clusters)
567 ret = ocfs2_xattr_extend_allocation(inode,
568 new_clusters - old_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800569 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800570 else
571 ret = ocfs2_xattr_shrink_size(inode,
572 old_clusters, new_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800573 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800574
575 return ret;
576}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800577
Tao Ma936b8832008-10-09 23:06:14 +0800578static int ocfs2_xattr_list_entry(char *buffer, size_t size,
579 size_t *result, const char *prefix,
580 const char *name, int name_len)
581{
582 char *p = buffer + *result;
583 int prefix_len = strlen(prefix);
584 int total_len = prefix_len + name_len + 1;
585
586 *result += total_len;
587
588 /* we are just looking for how big our buffer needs to be */
589 if (!size)
590 return 0;
591
592 if (*result > size)
593 return -ERANGE;
594
595 memcpy(p, prefix, prefix_len);
596 memcpy(p + prefix_len, name, name_len);
597 p[prefix_len + name_len] = '\0';
598
599 return 0;
600}
601
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800602static int ocfs2_xattr_list_entries(struct inode *inode,
603 struct ocfs2_xattr_header *header,
604 char *buffer, size_t buffer_size)
605{
Tao Ma936b8832008-10-09 23:06:14 +0800606 size_t result = 0;
607 int i, type, ret;
608 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800609
610 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
611 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800612 type = ocfs2_xattr_get_type(entry);
613 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800614
Tao Ma936b8832008-10-09 23:06:14 +0800615 if (prefix) {
616 name = (const char *)header +
617 le16_to_cpu(entry->xe_name_offset);
618
619 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
620 &result, prefix, name,
621 entry->xe_name_len);
622 if (ret)
623 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800624 }
625 }
626
Tao Ma936b8832008-10-09 23:06:14 +0800627 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800628}
629
630static int ocfs2_xattr_ibody_list(struct inode *inode,
631 struct ocfs2_dinode *di,
632 char *buffer,
633 size_t buffer_size)
634{
635 struct ocfs2_xattr_header *header = NULL;
636 struct ocfs2_inode_info *oi = OCFS2_I(inode);
637 int ret = 0;
638
639 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
640 return ret;
641
642 header = (struct ocfs2_xattr_header *)
643 ((void *)di + inode->i_sb->s_blocksize -
644 le16_to_cpu(di->i_xattr_inline_size));
645
646 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
647
648 return ret;
649}
650
651static int ocfs2_xattr_block_list(struct inode *inode,
652 struct ocfs2_dinode *di,
653 char *buffer,
654 size_t buffer_size)
655{
656 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800657 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800658 int ret = 0;
659
660 if (!di->i_xattr_loc)
661 return ret;
662
Joel Becker0fcaa562008-10-09 17:20:31 -0700663 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800664 if (ret < 0) {
665 mlog_errno(ret);
666 return ret;
667 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800668
Tao Ma0c044f02008-08-18 17:38:50 +0800669 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Joel Beckerf6087fb2008-10-20 18:20:43 -0700670 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
671 ret = -EIO;
672 goto cleanup;
673 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800674
Tao Ma0c044f02008-08-18 17:38:50 +0800675 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
676 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
677 ret = ocfs2_xattr_list_entries(inode, header,
678 buffer, buffer_size);
679 } else {
680 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
681 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
682 buffer, buffer_size);
683 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800684cleanup:
685 brelse(blk_bh);
686
687 return ret;
688}
689
690ssize_t ocfs2_listxattr(struct dentry *dentry,
691 char *buffer,
692 size_t size)
693{
694 int ret = 0, i_ret = 0, b_ret = 0;
695 struct buffer_head *di_bh = NULL;
696 struct ocfs2_dinode *di = NULL;
697 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
698
Tiger Yang8154da32008-08-18 17:11:46 +0800699 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
700 return -EOPNOTSUPP;
701
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800702 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
703 return ret;
704
705 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
706 if (ret < 0) {
707 mlog_errno(ret);
708 return ret;
709 }
710
711 di = (struct ocfs2_dinode *)di_bh->b_data;
712
713 down_read(&oi->ip_xattr_sem);
714 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
715 if (i_ret < 0)
716 b_ret = 0;
717 else {
718 if (buffer) {
719 buffer += i_ret;
720 size -= i_ret;
721 }
722 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
723 buffer, size);
724 if (b_ret < 0)
725 i_ret = 0;
726 }
727 up_read(&oi->ip_xattr_sem);
728 ocfs2_inode_unlock(dentry->d_inode, 0);
729
730 brelse(di_bh);
731
732 return i_ret + b_ret;
733}
734
735static int ocfs2_xattr_find_entry(int name_index,
736 const char *name,
737 struct ocfs2_xattr_search *xs)
738{
739 struct ocfs2_xattr_entry *entry;
740 size_t name_len;
741 int i, cmp = 1;
742
743 if (name == NULL)
744 return -EINVAL;
745
746 name_len = strlen(name);
747 entry = xs->here;
748 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
749 cmp = name_index - ocfs2_xattr_get_type(entry);
750 if (!cmp)
751 cmp = name_len - entry->xe_name_len;
752 if (!cmp)
753 cmp = memcmp(name, (xs->base +
754 le16_to_cpu(entry->xe_name_offset)),
755 name_len);
756 if (cmp == 0)
757 break;
758 entry += 1;
759 }
760 xs->here = entry;
761
762 return cmp ? -ENODATA : 0;
763}
764
765static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800766 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800767 void *buffer,
768 size_t len)
769{
770 u32 cpos, p_cluster, num_clusters, bpc, clusters;
771 u64 blkno;
772 int i, ret = 0;
773 size_t cplen, blocksize;
774 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800775 struct ocfs2_extent_list *el;
776
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800777 el = &xv->xr_list;
778 clusters = le32_to_cpu(xv->xr_clusters);
779 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
780 blocksize = inode->i_sb->s_blocksize;
781
782 cpos = 0;
783 while (cpos < clusters) {
784 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
785 &num_clusters, el);
786 if (ret) {
787 mlog_errno(ret);
788 goto out;
789 }
790
791 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
792 /* Copy ocfs2_xattr_value */
793 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa562008-10-09 17:20:31 -0700794 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800795 if (ret) {
796 mlog_errno(ret);
797 goto out;
798 }
799
800 cplen = len >= blocksize ? blocksize : len;
801 memcpy(buffer, bh->b_data, cplen);
802 len -= cplen;
803 buffer += cplen;
804
805 brelse(bh);
806 bh = NULL;
807 if (len == 0)
808 break;
809 }
810 cpos += num_clusters;
811 }
812out:
813 return ret;
814}
815
816static int ocfs2_xattr_ibody_get(struct inode *inode,
817 int name_index,
818 const char *name,
819 void *buffer,
820 size_t buffer_size,
821 struct ocfs2_xattr_search *xs)
822{
823 struct ocfs2_inode_info *oi = OCFS2_I(inode);
824 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800825 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800826 size_t size;
827 int ret = 0;
828
829 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
830 return -ENODATA;
831
832 xs->end = (void *)di + inode->i_sb->s_blocksize;
833 xs->header = (struct ocfs2_xattr_header *)
834 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
835 xs->base = (void *)xs->header;
836 xs->here = xs->header->xh_entries;
837
838 ret = ocfs2_xattr_find_entry(name_index, name, xs);
839 if (ret)
840 return ret;
841 size = le64_to_cpu(xs->here->xe_value_size);
842 if (buffer) {
843 if (size > buffer_size)
844 return -ERANGE;
845 if (ocfs2_xattr_is_local(xs->here)) {
846 memcpy(buffer, (void *)xs->base +
847 le16_to_cpu(xs->here->xe_name_offset) +
848 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
849 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800850 xv = (struct ocfs2_xattr_value_root *)
851 (xs->base + le16_to_cpu(
852 xs->here->xe_name_offset) +
853 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
854 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800855 buffer, size);
856 if (ret < 0) {
857 mlog_errno(ret);
858 return ret;
859 }
860 }
861 }
862
863 return size;
864}
865
866static int ocfs2_xattr_block_get(struct inode *inode,
867 int name_index,
868 const char *name,
869 void *buffer,
870 size_t buffer_size,
871 struct ocfs2_xattr_search *xs)
872{
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800873 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +0800874 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800875 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +0800876 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800877
Joel Beckerba937122008-10-24 19:13:20 -0700878 xs->bucket = ocfs2_xattr_bucket_new(inode);
879 if (!xs->bucket) {
880 ret = -ENOMEM;
881 mlog_errno(ret);
882 goto cleanup;
883 }
Tao Ma589dc262008-08-18 17:38:51 +0800884
Joel Becker54f443f2008-10-20 18:43:07 -0700885 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
886 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800887 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800888 goto cleanup;
889 }
890
Tiger Yang6c1e1832008-11-02 19:04:21 +0800891 if (xs->not_found) {
892 ret = -ENODATA;
893 goto cleanup;
894 }
895
Joel Becker54f443f2008-10-20 18:43:07 -0700896 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800897 size = le64_to_cpu(xs->here->xe_value_size);
898 if (buffer) {
899 ret = -ERANGE;
900 if (size > buffer_size)
901 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +0800902
903 name_offset = le16_to_cpu(xs->here->xe_name_offset);
904 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
905 i = xs->here - xs->header->xh_entries;
906
907 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
908 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -0700909 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +0800910 i,
911 &block_off,
912 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -0700913 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +0800914 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800915 if (ocfs2_xattr_is_local(xs->here)) {
916 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +0800917 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800918 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800919 xv = (struct ocfs2_xattr_value_root *)
920 (xs->base + name_offset + name_len);
921 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800922 buffer, size);
923 if (ret < 0) {
924 mlog_errno(ret);
925 goto cleanup;
926 }
927 }
928 }
929 ret = size;
930cleanup:
Joel Beckerba937122008-10-24 19:13:20 -0700931 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800932
Joel Becker54f443f2008-10-20 18:43:07 -0700933 brelse(xs->xattr_bh);
934 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800935 return ret;
936}
937
Tiger Yang4e3e9d02008-11-14 11:16:53 +0800938int ocfs2_xattr_get_nolock(struct inode *inode,
939 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +0800940 int name_index,
941 const char *name,
942 void *buffer,
943 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800944{
945 int ret;
946 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800947 struct ocfs2_inode_info *oi = OCFS2_I(inode);
948 struct ocfs2_xattr_search xis = {
949 .not_found = -ENODATA,
950 };
951 struct ocfs2_xattr_search xbs = {
952 .not_found = -ENODATA,
953 };
954
Tiger Yang8154da32008-08-18 17:11:46 +0800955 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
956 return -EOPNOTSUPP;
957
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800958 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
959 ret = -ENODATA;
960
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800961 xis.inode_bh = xbs.inode_bh = di_bh;
962 di = (struct ocfs2_dinode *)di_bh->b_data;
963
964 down_read(&oi->ip_xattr_sem);
965 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
966 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +0800967 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800968 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
969 buffer_size, &xbs);
970 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +0800971
972 return ret;
973}
974
975/* ocfs2_xattr_get()
976 *
977 * Copy an extended attribute into the buffer provided.
978 * Buffer is NULL to compute the size of buffer required.
979 */
980static int ocfs2_xattr_get(struct inode *inode,
981 int name_index,
982 const char *name,
983 void *buffer,
984 size_t buffer_size)
985{
986 int ret;
987 struct buffer_head *di_bh = NULL;
988
989 ret = ocfs2_inode_lock(inode, &di_bh, 0);
990 if (ret < 0) {
991 mlog_errno(ret);
992 return ret;
993 }
994 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
995 name, buffer, buffer_size);
996
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800997 ocfs2_inode_unlock(inode, 0);
998
999 brelse(di_bh);
1000
1001 return ret;
1002}
1003
1004static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001005 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001006 struct ocfs2_xattr_value_root *xv,
1007 const void *value,
1008 int value_len)
1009{
1010 int ret = 0, i, cp_len, credits;
1011 u16 blocksize = inode->i_sb->s_blocksize;
1012 u32 p_cluster, num_clusters;
1013 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1014 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1015 u64 blkno;
1016 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001017
1018 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1019
Tao Ma85db90e2008-11-12 08:27:01 +08001020 /*
1021 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1022 * so we don't need to worry about whether ocfs2_extend_trans
1023 * will create a new transactio for us or not.
1024 */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001025 credits = clusters * bpc;
Tao Ma85db90e2008-11-12 08:27:01 +08001026 ret = ocfs2_extend_trans(handle, credits);
1027 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001028 mlog_errno(ret);
1029 goto out;
1030 }
1031
1032 while (cpos < clusters) {
1033 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1034 &num_clusters, &xv->xr_list);
1035 if (ret) {
1036 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001037 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001038 }
1039
1040 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1041
1042 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa562008-10-09 17:20:31 -07001043 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001044 if (ret) {
1045 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001046 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001047 }
1048
1049 ret = ocfs2_journal_access(handle,
1050 inode,
1051 bh,
1052 OCFS2_JOURNAL_ACCESS_WRITE);
1053 if (ret < 0) {
1054 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001055 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001056 }
1057
1058 cp_len = value_len > blocksize ? blocksize : value_len;
1059 memcpy(bh->b_data, value, cp_len);
1060 value_len -= cp_len;
1061 value += cp_len;
1062 if (cp_len < blocksize)
1063 memset(bh->b_data + cp_len, 0,
1064 blocksize - cp_len);
1065
1066 ret = ocfs2_journal_dirty(handle, bh);
1067 if (ret < 0) {
1068 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001069 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001070 }
1071 brelse(bh);
1072 bh = NULL;
1073
1074 /*
1075 * XXX: do we need to empty all the following
1076 * blocks in this cluster?
1077 */
1078 if (!value_len)
1079 break;
1080 }
1081 cpos += num_clusters;
1082 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001083out:
1084 brelse(bh);
1085
1086 return ret;
1087}
1088
1089static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001090 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001091 struct ocfs2_xattr_info *xi,
1092 struct ocfs2_xattr_search *xs,
1093 size_t offs)
1094{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001095 int ret = 0;
1096 size_t name_len = strlen(xi->name);
1097 void *val = xs->base + offs;
1098 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1099
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001100 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1101 OCFS2_JOURNAL_ACCESS_WRITE);
1102 if (ret) {
1103 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001104 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001105 }
1106 /* Decrease xattr count */
1107 le16_add_cpu(&xs->header->xh_count, -1);
1108 /* Remove the xattr entry and tree root which has already be set*/
1109 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1110 memset(val, 0, size);
1111
1112 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1113 if (ret < 0)
1114 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001115out:
1116 return ret;
1117}
1118
1119static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001120 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001121 struct ocfs2_xattr_info *xi,
1122 struct ocfs2_xattr_search *xs,
1123 size_t offs)
1124{
Tao Ma85db90e2008-11-12 08:27:01 +08001125 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001126
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001127 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1128 OCFS2_JOURNAL_ACCESS_WRITE);
1129 if (ret) {
1130 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001131 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001132 }
1133
1134 xs->here->xe_name_offset = cpu_to_le16(offs);
1135 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1136 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1137 ocfs2_xattr_set_local(xs->here, 1);
1138 else
1139 ocfs2_xattr_set_local(xs->here, 0);
1140 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1141
1142 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1143 if (ret < 0)
1144 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001145out:
1146 return ret;
1147}
1148
1149/*
1150 * ocfs2_xattr_set_value_outside()
1151 *
1152 * Set large size value in B tree.
1153 */
1154static int ocfs2_xattr_set_value_outside(struct inode *inode,
1155 struct ocfs2_xattr_info *xi,
1156 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001157 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001158 size_t offs)
1159{
1160 size_t name_len = strlen(xi->name);
1161 void *val = xs->base + offs;
1162 struct ocfs2_xattr_value_root *xv = NULL;
1163 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1164 int ret = 0;
1165
1166 memset(val, 0, size);
1167 memcpy(val, xi->name, name_len);
1168 xv = (struct ocfs2_xattr_value_root *)
1169 (val + OCFS2_XATTR_SIZE(name_len));
1170 xv->xr_clusters = 0;
1171 xv->xr_last_eb_blk = 0;
1172 xv->xr_list.l_tree_depth = 0;
1173 xv->xr_list.l_count = cpu_to_le16(1);
1174 xv->xr_list.l_next_free_rec = 0;
1175
1176 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001177 xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001178 if (ret < 0) {
1179 mlog_errno(ret);
1180 return ret;
1181 }
Tao Ma85db90e2008-11-12 08:27:01 +08001182 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001183 if (ret < 0) {
1184 mlog_errno(ret);
1185 return ret;
1186 }
Tao Ma85db90e2008-11-12 08:27:01 +08001187 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1188 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001189 if (ret < 0)
1190 mlog_errno(ret);
1191
1192 return ret;
1193}
1194
1195/*
1196 * ocfs2_xattr_set_entry_local()
1197 *
1198 * Set, replace or remove extended attribute in local.
1199 */
1200static void ocfs2_xattr_set_entry_local(struct inode *inode,
1201 struct ocfs2_xattr_info *xi,
1202 struct ocfs2_xattr_search *xs,
1203 struct ocfs2_xattr_entry *last,
1204 size_t min_offs)
1205{
1206 size_t name_len = strlen(xi->name);
1207 int i;
1208
1209 if (xi->value && xs->not_found) {
1210 /* Insert the new xattr entry. */
1211 le16_add_cpu(&xs->header->xh_count, 1);
1212 ocfs2_xattr_set_type(last, xi->name_index);
1213 ocfs2_xattr_set_local(last, 1);
1214 last->xe_name_len = name_len;
1215 } else {
1216 void *first_val;
1217 void *val;
1218 size_t offs, size;
1219
1220 first_val = xs->base + min_offs;
1221 offs = le16_to_cpu(xs->here->xe_name_offset);
1222 val = xs->base + offs;
1223
1224 if (le64_to_cpu(xs->here->xe_value_size) >
1225 OCFS2_XATTR_INLINE_SIZE)
1226 size = OCFS2_XATTR_SIZE(name_len) +
1227 OCFS2_XATTR_ROOT_SIZE;
1228 else
1229 size = OCFS2_XATTR_SIZE(name_len) +
1230 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1231
1232 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1233 OCFS2_XATTR_SIZE(xi->value_len)) {
1234 /* The old and the new value have the
1235 same size. Just replace the value. */
1236 ocfs2_xattr_set_local(xs->here, 1);
1237 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1238 /* Clear value bytes. */
1239 memset(val + OCFS2_XATTR_SIZE(name_len),
1240 0,
1241 OCFS2_XATTR_SIZE(xi->value_len));
1242 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1243 xi->value,
1244 xi->value_len);
1245 return;
1246 }
1247 /* Remove the old name+value. */
1248 memmove(first_val + size, first_val, val - first_val);
1249 memset(first_val, 0, size);
1250 xs->here->xe_name_hash = 0;
1251 xs->here->xe_name_offset = 0;
1252 ocfs2_xattr_set_local(xs->here, 1);
1253 xs->here->xe_value_size = 0;
1254
1255 min_offs += size;
1256
1257 /* Adjust all value offsets. */
1258 last = xs->header->xh_entries;
1259 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1260 size_t o = le16_to_cpu(last->xe_name_offset);
1261
1262 if (o < offs)
1263 last->xe_name_offset = cpu_to_le16(o + size);
1264 last += 1;
1265 }
1266
1267 if (!xi->value) {
1268 /* Remove the old entry. */
1269 last -= 1;
1270 memmove(xs->here, xs->here + 1,
1271 (void *)last - (void *)xs->here);
1272 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1273 le16_add_cpu(&xs->header->xh_count, -1);
1274 }
1275 }
1276 if (xi->value) {
1277 /* Insert the new name+value. */
1278 size_t size = OCFS2_XATTR_SIZE(name_len) +
1279 OCFS2_XATTR_SIZE(xi->value_len);
1280 void *val = xs->base + min_offs - size;
1281
1282 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1283 memset(val, 0, size);
1284 memcpy(val, xi->name, name_len);
1285 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1286 xi->value,
1287 xi->value_len);
1288 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1289 ocfs2_xattr_set_local(xs->here, 1);
1290 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1291 }
1292
1293 return;
1294}
1295
1296/*
1297 * ocfs2_xattr_set_entry()
1298 *
1299 * Set extended attribute entry into inode or block.
1300 *
1301 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1302 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1303 * then set value in B tree with set_value_outside().
1304 */
1305static int ocfs2_xattr_set_entry(struct inode *inode,
1306 struct ocfs2_xattr_info *xi,
1307 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001308 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001309 int flag)
1310{
1311 struct ocfs2_xattr_entry *last;
1312 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1313 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1314 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1315 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001316 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001317 int free, i, ret;
1318 struct ocfs2_xattr_info xi_l = {
1319 .name_index = xi->name_index,
1320 .name = xi->name,
1321 .value = xi->value,
1322 .value_len = xi->value_len,
1323 };
1324
1325 /* Compute min_offs, last and free space. */
1326 last = xs->header->xh_entries;
1327
1328 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1329 size_t offs = le16_to_cpu(last->xe_name_offset);
1330 if (offs < min_offs)
1331 min_offs = offs;
1332 last += 1;
1333 }
1334
1335 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1336 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001337 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001338
1339 if (!xs->not_found) {
1340 size_t size = 0;
1341 if (ocfs2_xattr_is_local(xs->here))
1342 size = OCFS2_XATTR_SIZE(name_len) +
1343 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1344 else
1345 size = OCFS2_XATTR_SIZE(name_len) +
1346 OCFS2_XATTR_ROOT_SIZE;
1347 free += (size + sizeof(struct ocfs2_xattr_entry));
1348 }
1349 /* Check free space in inode or block */
1350 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1351 if (free < sizeof(struct ocfs2_xattr_entry) +
1352 OCFS2_XATTR_SIZE(name_len) +
1353 OCFS2_XATTR_ROOT_SIZE) {
1354 ret = -ENOSPC;
1355 goto out;
1356 }
1357 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1358 xi_l.value = (void *)&def_xv;
1359 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1360 } else if (xi->value) {
1361 if (free < sizeof(struct ocfs2_xattr_entry) +
1362 OCFS2_XATTR_SIZE(name_len) +
1363 OCFS2_XATTR_SIZE(xi->value_len)) {
1364 ret = -ENOSPC;
1365 goto out;
1366 }
1367 }
1368
1369 if (!xs->not_found) {
1370 /* For existing extended attribute */
1371 size_t size = OCFS2_XATTR_SIZE(name_len) +
1372 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1373 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1374 void *val = xs->base + offs;
1375
1376 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1377 /* Replace existing local xattr with tree root */
1378 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001379 ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001380 if (ret < 0)
1381 mlog_errno(ret);
1382 goto out;
1383 } else if (!ocfs2_xattr_is_local(xs->here)) {
1384 /* For existing xattr which has value outside */
1385 struct ocfs2_xattr_value_root *xv = NULL;
1386 xv = (struct ocfs2_xattr_value_root *)(val +
1387 OCFS2_XATTR_SIZE(name_len));
1388
1389 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1390 /*
1391 * If new value need set outside also,
1392 * first truncate old value to new value,
1393 * then set new value with set_value_outside().
1394 */
1395 ret = ocfs2_xattr_value_truncate(inode,
1396 xs->xattr_bh,
1397 xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001398 xi->value_len,
1399 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001400 if (ret < 0) {
1401 mlog_errno(ret);
1402 goto out;
1403 }
1404
Tao Ma85db90e2008-11-12 08:27:01 +08001405 ret = ocfs2_xattr_update_entry(inode,
1406 handle,
1407 xi,
1408 xs,
1409 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001410 if (ret < 0) {
1411 mlog_errno(ret);
1412 goto out;
1413 }
1414
Tao Ma85db90e2008-11-12 08:27:01 +08001415 ret = __ocfs2_xattr_set_value_outside(inode,
1416 handle,
1417 xv,
1418 xi->value,
1419 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001420 if (ret < 0)
1421 mlog_errno(ret);
1422 goto out;
1423 } else {
1424 /*
1425 * If new value need set in local,
1426 * just trucate old value to zero.
1427 */
1428 ret = ocfs2_xattr_value_truncate(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001429 xs->xattr_bh,
1430 xv,
1431 0,
1432 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001433 if (ret < 0)
1434 mlog_errno(ret);
1435 }
1436 }
1437 }
1438
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001439 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1440 OCFS2_JOURNAL_ACCESS_WRITE);
1441 if (ret) {
1442 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001443 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001444 }
1445
1446 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001447 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1448 OCFS2_JOURNAL_ACCESS_WRITE);
1449 if (ret) {
1450 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001451 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001452 }
1453 }
1454
1455 /*
1456 * Set value in local, include set tree root in local.
1457 * This is the first step for value size >INLINE_SIZE.
1458 */
1459 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1460
1461 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1462 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1463 if (ret < 0) {
1464 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001465 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001466 }
1467 }
1468
1469 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1470 (flag & OCFS2_INLINE_XATTR_FL)) {
1471 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1472 unsigned int xattrsize = osb->s_xattr_inline_size;
1473
1474 /*
1475 * Adjust extent record count or inline data size
1476 * to reserve space for extended attribute.
1477 */
1478 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1479 struct ocfs2_inline_data *idata = &di->id2.i_data;
1480 le16_add_cpu(&idata->id_count, -xattrsize);
1481 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1482 struct ocfs2_extent_list *el = &di->id2.i_list;
1483 le16_add_cpu(&el->l_count, -(xattrsize /
1484 sizeof(struct ocfs2_extent_rec)));
1485 }
1486 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1487 }
1488 /* Update xattr flag */
1489 spin_lock(&oi->ip_lock);
1490 oi->ip_dyn_features |= flag;
1491 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1492 spin_unlock(&oi->ip_lock);
1493 /* Update inode ctime */
1494 inode->i_ctime = CURRENT_TIME;
1495 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1496 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1497
1498 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1499 if (ret < 0)
1500 mlog_errno(ret);
1501
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001502 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1503 /*
1504 * Set value outside in B tree.
1505 * This is the second step for value size > INLINE_SIZE.
1506 */
1507 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Tao Ma78f30c32008-11-12 08:27:00 +08001508 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001509 if (ret < 0) {
1510 int ret2;
1511
1512 mlog_errno(ret);
1513 /*
1514 * If set value outside failed, we have to clean
1515 * the junk tree root we have already set in local.
1516 */
Tao Ma85db90e2008-11-12 08:27:01 +08001517 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1518 xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001519 if (ret2 < 0)
1520 mlog_errno(ret2);
1521 }
1522 }
1523out:
1524 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001525}
1526
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001527static int ocfs2_remove_value_outside(struct inode*inode,
1528 struct buffer_head *bh,
1529 struct ocfs2_xattr_header *header)
1530{
1531 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001532 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1533 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1534
1535 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001536
Tao Ma85db90e2008-11-12 08:27:01 +08001537 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1538 if (IS_ERR(ctxt.handle)) {
1539 ret = PTR_ERR(ctxt.handle);
1540 mlog_errno(ret);
1541 goto out;
1542 }
1543
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001544 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1545 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1546
1547 if (!ocfs2_xattr_is_local(entry)) {
1548 struct ocfs2_xattr_value_root *xv;
1549 void *val;
1550
1551 val = (void *)header +
1552 le16_to_cpu(entry->xe_name_offset);
1553 xv = (struct ocfs2_xattr_value_root *)
1554 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Tao Ma78f30c32008-11-12 08:27:00 +08001555 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1556 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001557 if (ret < 0) {
1558 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001559 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001560 }
1561 }
1562 }
1563
Tao Ma85db90e2008-11-12 08:27:01 +08001564 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001565 ocfs2_schedule_truncate_log_flush(osb, 1);
1566 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001567out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001568 return ret;
1569}
1570
1571static int ocfs2_xattr_ibody_remove(struct inode *inode,
1572 struct buffer_head *di_bh)
1573{
1574
1575 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1576 struct ocfs2_xattr_header *header;
1577 int ret;
1578
1579 header = (struct ocfs2_xattr_header *)
1580 ((void *)di + inode->i_sb->s_blocksize -
1581 le16_to_cpu(di->i_xattr_inline_size));
1582
1583 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1584
1585 return ret;
1586}
1587
1588static int ocfs2_xattr_block_remove(struct inode *inode,
1589 struct buffer_head *blk_bh)
1590{
1591 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001592 int ret = 0;
1593
1594 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001595 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1596 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1597 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1598 } else
1599 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001600
1601 return ret;
1602}
1603
Tao Ma08413892008-08-29 09:00:19 +08001604static int ocfs2_xattr_free_block(struct inode *inode,
1605 u64 block)
1606{
1607 struct inode *xb_alloc_inode;
1608 struct buffer_head *xb_alloc_bh = NULL;
1609 struct buffer_head *blk_bh = NULL;
1610 struct ocfs2_xattr_block *xb;
1611 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1612 handle_t *handle;
1613 int ret = 0;
1614 u64 blk, bg_blkno;
1615 u16 bit;
1616
Joel Becker0fcaa562008-10-09 17:20:31 -07001617 ret = ocfs2_read_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001618 if (ret < 0) {
1619 mlog_errno(ret);
1620 goto out;
1621 }
1622
Joel Beckerf6087fb2008-10-20 18:20:43 -07001623 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1624 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1625 ret = -EIO;
Tao Ma08413892008-08-29 09:00:19 +08001626 goto out;
1627 }
1628
1629 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1630 if (ret < 0) {
1631 mlog_errno(ret);
1632 goto out;
1633 }
1634
Tao Ma08413892008-08-29 09:00:19 +08001635 blk = le64_to_cpu(xb->xb_blkno);
1636 bit = le16_to_cpu(xb->xb_suballoc_bit);
1637 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1638
1639 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1640 EXTENT_ALLOC_SYSTEM_INODE,
1641 le16_to_cpu(xb->xb_suballoc_slot));
1642 if (!xb_alloc_inode) {
1643 ret = -ENOMEM;
1644 mlog_errno(ret);
1645 goto out;
1646 }
1647 mutex_lock(&xb_alloc_inode->i_mutex);
1648
1649 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1650 if (ret < 0) {
1651 mlog_errno(ret);
1652 goto out_mutex;
1653 }
1654
1655 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1656 if (IS_ERR(handle)) {
1657 ret = PTR_ERR(handle);
1658 mlog_errno(ret);
1659 goto out_unlock;
1660 }
1661
1662 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1663 bit, bg_blkno, 1);
1664 if (ret < 0)
1665 mlog_errno(ret);
1666
1667 ocfs2_commit_trans(osb, handle);
1668out_unlock:
1669 ocfs2_inode_unlock(xb_alloc_inode, 1);
1670 brelse(xb_alloc_bh);
1671out_mutex:
1672 mutex_unlock(&xb_alloc_inode->i_mutex);
1673 iput(xb_alloc_inode);
1674out:
1675 brelse(blk_bh);
1676 return ret;
1677}
1678
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001679/*
1680 * ocfs2_xattr_remove()
1681 *
1682 * Free extended attribute resources associated with this inode.
1683 */
1684int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1685{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001686 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1687 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1688 handle_t *handle;
1689 int ret;
1690
Tiger Yang8154da32008-08-18 17:11:46 +08001691 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1692 return 0;
1693
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001694 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1695 return 0;
1696
1697 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1698 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1699 if (ret < 0) {
1700 mlog_errno(ret);
1701 goto out;
1702 }
1703 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001704
Tao Ma08413892008-08-29 09:00:19 +08001705 if (di->i_xattr_loc) {
1706 ret = ocfs2_xattr_free_block(inode,
1707 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001708 if (ret < 0) {
1709 mlog_errno(ret);
1710 goto out;
1711 }
1712 }
1713
1714 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1715 OCFS2_INODE_UPDATE_CREDITS);
1716 if (IS_ERR(handle)) {
1717 ret = PTR_ERR(handle);
1718 mlog_errno(ret);
1719 goto out;
1720 }
1721 ret = ocfs2_journal_access(handle, inode, di_bh,
1722 OCFS2_JOURNAL_ACCESS_WRITE);
1723 if (ret) {
1724 mlog_errno(ret);
1725 goto out_commit;
1726 }
1727
Tao Ma08413892008-08-29 09:00:19 +08001728 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001729
1730 spin_lock(&oi->ip_lock);
1731 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1732 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1733 spin_unlock(&oi->ip_lock);
1734
1735 ret = ocfs2_journal_dirty(handle, di_bh);
1736 if (ret < 0)
1737 mlog_errno(ret);
1738out_commit:
1739 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1740out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001741 return ret;
1742}
1743
1744static int ocfs2_xattr_has_space_inline(struct inode *inode,
1745 struct ocfs2_dinode *di)
1746{
1747 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1748 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1749 int free;
1750
1751 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1752 return 0;
1753
1754 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1755 struct ocfs2_inline_data *idata = &di->id2.i_data;
1756 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1757 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1758 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1759 le64_to_cpu(di->i_size);
1760 } else {
1761 struct ocfs2_extent_list *el = &di->id2.i_list;
1762 free = (le16_to_cpu(el->l_count) -
1763 le16_to_cpu(el->l_next_free_rec)) *
1764 sizeof(struct ocfs2_extent_rec);
1765 }
1766 if (free >= xattrsize)
1767 return 1;
1768
1769 return 0;
1770}
1771
1772/*
1773 * ocfs2_xattr_ibody_find()
1774 *
1775 * Find extended attribute in inode block and
1776 * fill search info into struct ocfs2_xattr_search.
1777 */
1778static int ocfs2_xattr_ibody_find(struct inode *inode,
1779 int name_index,
1780 const char *name,
1781 struct ocfs2_xattr_search *xs)
1782{
1783 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1784 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1785 int ret;
1786 int has_space = 0;
1787
1788 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1789 return 0;
1790
1791 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1792 down_read(&oi->ip_alloc_sem);
1793 has_space = ocfs2_xattr_has_space_inline(inode, di);
1794 up_read(&oi->ip_alloc_sem);
1795 if (!has_space)
1796 return 0;
1797 }
1798
1799 xs->xattr_bh = xs->inode_bh;
1800 xs->end = (void *)di + inode->i_sb->s_blocksize;
1801 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1802 xs->header = (struct ocfs2_xattr_header *)
1803 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1804 else
1805 xs->header = (struct ocfs2_xattr_header *)
1806 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1807 xs->base = (void *)xs->header;
1808 xs->here = xs->header->xh_entries;
1809
1810 /* Find the named attribute. */
1811 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1812 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1813 if (ret && ret != -ENODATA)
1814 return ret;
1815 xs->not_found = ret;
1816 }
1817
1818 return 0;
1819}
1820
1821/*
1822 * ocfs2_xattr_ibody_set()
1823 *
1824 * Set, replace or remove an extended attribute into inode block.
1825 *
1826 */
1827static int ocfs2_xattr_ibody_set(struct inode *inode,
1828 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08001829 struct ocfs2_xattr_search *xs,
1830 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001831{
1832 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1833 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1834 int ret;
1835
1836 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1837 return -ENOSPC;
1838
1839 down_write(&oi->ip_alloc_sem);
1840 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1841 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1842 ret = -ENOSPC;
1843 goto out;
1844 }
1845 }
1846
Tao Ma78f30c32008-11-12 08:27:00 +08001847 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001848 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1849out:
1850 up_write(&oi->ip_alloc_sem);
1851
1852 return ret;
1853}
1854
1855/*
1856 * ocfs2_xattr_block_find()
1857 *
1858 * Find extended attribute in external block and
1859 * fill search info into struct ocfs2_xattr_search.
1860 */
1861static int ocfs2_xattr_block_find(struct inode *inode,
1862 int name_index,
1863 const char *name,
1864 struct ocfs2_xattr_search *xs)
1865{
1866 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1867 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08001868 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001869 int ret = 0;
1870
1871 if (!di->i_xattr_loc)
1872 return ret;
1873
Joel Becker0fcaa562008-10-09 17:20:31 -07001874 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001875 if (ret < 0) {
1876 mlog_errno(ret);
1877 return ret;
1878 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07001879
1880 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1881 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1882 ret = -EIO;
1883 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001884 }
1885
1886 xs->xattr_bh = blk_bh;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001887
Tao Ma589dc262008-08-18 17:38:51 +08001888 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1889 xs->header = &xb->xb_attrs.xb_header;
1890 xs->base = (void *)xs->header;
1891 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1892 xs->here = xs->header->xh_entries;
1893
1894 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1895 } else
1896 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1897 name_index,
1898 name, xs);
1899
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001900 if (ret && ret != -ENODATA) {
1901 xs->xattr_bh = NULL;
1902 goto cleanup;
1903 }
1904 xs->not_found = ret;
1905 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001906cleanup:
1907 brelse(blk_bh);
1908
1909 return ret;
1910}
1911
1912/*
1913 * ocfs2_xattr_block_set()
1914 *
1915 * Set, replace or remove an extended attribute into external block.
1916 *
1917 */
1918static int ocfs2_xattr_block_set(struct inode *inode,
1919 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08001920 struct ocfs2_xattr_search *xs,
1921 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001922{
1923 struct buffer_head *new_bh = NULL;
1924 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1925 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08001926 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001927 struct ocfs2_xattr_block *xblk = NULL;
1928 u16 suballoc_bit_start;
1929 u32 num_got;
1930 u64 first_blkno;
1931 int ret;
1932
1933 if (!xs->xattr_bh) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001934 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1935 OCFS2_JOURNAL_ACCESS_CREATE);
1936 if (ret < 0) {
1937 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001938 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001939 }
1940
Tao Ma78f30c32008-11-12 08:27:00 +08001941 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001942 &suballoc_bit_start, &num_got,
1943 &first_blkno);
1944 if (ret < 0) {
1945 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001946 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001947 }
1948
1949 new_bh = sb_getblk(inode->i_sb, first_blkno);
1950 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1951
1952 ret = ocfs2_journal_access(handle, inode, new_bh,
1953 OCFS2_JOURNAL_ACCESS_CREATE);
1954 if (ret < 0) {
1955 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001956 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001957 }
1958
1959 /* Initialize ocfs2_xattr_block */
1960 xs->xattr_bh = new_bh;
1961 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1962 memset(xblk, 0, inode->i_sb->s_blocksize);
1963 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1964 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1965 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1966 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1967 xblk->xb_blkno = cpu_to_le64(first_blkno);
1968
1969 xs->header = &xblk->xb_attrs.xb_header;
1970 xs->base = (void *)xs->header;
1971 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1972 xs->here = xs->header->xh_entries;
1973
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001974 ret = ocfs2_journal_dirty(handle, new_bh);
1975 if (ret < 0) {
1976 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001977 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001978 }
1979 di->i_xattr_loc = cpu_to_le64(first_blkno);
Tao Ma85db90e2008-11-12 08:27:01 +08001980 ocfs2_journal_dirty(handle, xs->inode_bh);
Tao Ma01225592008-08-18 17:38:53 +08001981 } else
1982 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1983
1984 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1985 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08001986 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
1987 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08001988 if (!ret || ret != -ENOSPC)
1989 goto end;
1990
Tao Ma78f30c32008-11-12 08:27:00 +08001991 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08001992 if (ret)
1993 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001994 }
1995
Tao Ma78f30c32008-11-12 08:27:00 +08001996 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08001997
1998end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001999
2000 return ret;
2001}
2002
Tao Ma78f30c32008-11-12 08:27:00 +08002003/* Check whether the new xattr can be inserted into the inode. */
2004static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2005 struct ocfs2_xattr_info *xi,
2006 struct ocfs2_xattr_search *xs)
2007{
2008 u64 value_size;
2009 struct ocfs2_xattr_entry *last;
2010 int free, i;
2011 size_t min_offs = xs->end - xs->base;
2012
2013 if (!xs->header)
2014 return 0;
2015
2016 last = xs->header->xh_entries;
2017
2018 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2019 size_t offs = le16_to_cpu(last->xe_name_offset);
2020 if (offs < min_offs)
2021 min_offs = offs;
2022 last += 1;
2023 }
2024
2025 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2026 if (free < 0)
2027 return 0;
2028
2029 BUG_ON(!xs->not_found);
2030
2031 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2032 value_size = OCFS2_XATTR_ROOT_SIZE;
2033 else
2034 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2035
2036 if (free >= sizeof(struct ocfs2_xattr_entry) +
2037 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2038 return 1;
2039
2040 return 0;
2041}
2042
2043static int ocfs2_calc_xattr_set_need(struct inode *inode,
2044 struct ocfs2_dinode *di,
2045 struct ocfs2_xattr_info *xi,
2046 struct ocfs2_xattr_search *xis,
2047 struct ocfs2_xattr_search *xbs,
2048 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002049 int *meta_need,
2050 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002051{
2052 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002053 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002054 struct buffer_head *bh = NULL;
2055 struct ocfs2_xattr_block *xb = NULL;
2056 struct ocfs2_xattr_entry *xe = NULL;
2057 struct ocfs2_xattr_value_root *xv = NULL;
2058 char *base = NULL;
2059 int name_offset, name_len = 0;
2060 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2061 xi->value_len);
2062 u64 value_size;
2063
Tao Ma78f30c32008-11-12 08:27:00 +08002064 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002065 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2066
2067 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002068 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002069 credits += ocfs2_calc_extend_credits(inode->i_sb,
2070 &def_xv.xv.xr_list,
2071 new_clusters);
2072 }
Tao Ma78f30c32008-11-12 08:27:00 +08002073
2074 goto meta_guess;
2075 }
2076
2077 if (!xis->not_found) {
2078 xe = xis->here;
2079 name_offset = le16_to_cpu(xe->xe_name_offset);
2080 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2081 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002082 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002083 } else {
2084 int i, block_off;
2085 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2086 xe = xbs->here;
2087 name_offset = le16_to_cpu(xe->xe_name_offset);
2088 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2089 i = xbs->here - xbs->header->xh_entries;
2090 old_in_xb = 1;
2091
2092 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2093 ret = ocfs2_xattr_bucket_get_name_value(inode,
2094 bucket_xh(xbs->bucket),
2095 i, &block_off,
2096 &name_offset);
2097 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002098 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2099 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002100 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002101 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2102 }
2103 }
2104
2105 /*
2106 * delete a xattr doesn't need metadata and cluster allocation.
2107 * so just calculate the credits and return.
2108 *
2109 * The credits for removing the value tree will be extended
2110 * by ocfs2_remove_extent itself.
2111 */
2112 if (!xi->value) {
2113 if (!ocfs2_xattr_is_local(xe))
2114 credits += OCFS2_REMOVE_EXTENT_CREDITS;
2115
2116 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002117 }
2118
2119 /* do cluster allocation guess first. */
2120 value_size = le64_to_cpu(xe->xe_value_size);
2121
2122 if (old_in_xb) {
2123 /*
2124 * In xattr set, we always try to set the xe in inode first,
2125 * so if it can be inserted into inode successfully, the old
2126 * one will be removed from the xattr block, and this xattr
2127 * will be inserted into inode as a new xattr in inode.
2128 */
2129 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2130 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002131 credits += OCFS2_REMOVE_EXTENT_CREDITS +
2132 OCFS2_INODE_UPDATE_CREDITS;
2133 if (!ocfs2_xattr_is_local(xe))
2134 credits += ocfs2_calc_extend_credits(
2135 inode->i_sb,
2136 &def_xv.xv.xr_list,
2137 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002138 goto out;
2139 }
2140 }
2141
2142 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2143 /* the new values will be stored outside. */
2144 u32 old_clusters = 0;
2145
2146 if (!ocfs2_xattr_is_local(xe)) {
2147 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2148 value_size);
2149 xv = (struct ocfs2_xattr_value_root *)
2150 (base + name_offset + name_len);
2151 } else
2152 xv = &def_xv.xv;
2153
Tao Ma85db90e2008-11-12 08:27:01 +08002154 if (old_clusters >= new_clusters) {
2155 credits += OCFS2_REMOVE_EXTENT_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002156 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002157 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002158 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2159 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002160 credits += ocfs2_calc_extend_credits(inode->i_sb,
2161 &xv->xr_list,
2162 new_clusters -
2163 old_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002164 goto out;
2165 }
2166 } else {
2167 /*
2168 * Now the new value will be stored inside. So if the new
2169 * value is smaller than the size of value root or the old
2170 * value, we don't need any allocation, otherwise we have
2171 * to guess metadata allocation.
2172 */
2173 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2174 (!ocfs2_xattr_is_local(xe) &&
2175 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2176 goto out;
2177 }
2178
2179meta_guess:
2180 /* calculate metadata allocation. */
2181 if (di->i_xattr_loc) {
2182 if (!xbs->xattr_bh) {
2183 ret = ocfs2_read_block(inode,
2184 le64_to_cpu(di->i_xattr_loc),
2185 &bh);
2186 if (ret) {
2187 mlog_errno(ret);
2188 goto out;
2189 }
2190
2191 xb = (struct ocfs2_xattr_block *)bh->b_data;
2192 } else
2193 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2194
2195 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2196 struct ocfs2_extent_list *el =
2197 &xb->xb_attrs.xb_root.xt_list;
2198 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002199 credits += ocfs2_calc_extend_credits(inode->i_sb,
2200 el, 1);
Tao Ma78f30c32008-11-12 08:27:00 +08002201 }
2202
2203 /*
2204 * This cluster will be used either for new bucket or for
2205 * new xattr block.
2206 * If the cluster size is the same as the bucket size, one
2207 * more is needed since we may need to extend the bucket
2208 * also.
2209 */
2210 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002211 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002212 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002213 OCFS2_SB(inode->i_sb)->s_clustersize) {
2214 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002215 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002216 }
2217 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002218 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002219 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2220 }
Tao Ma78f30c32008-11-12 08:27:00 +08002221out:
2222 if (clusters_need)
2223 *clusters_need = clusters_add;
2224 if (meta_need)
2225 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002226 if (credits_need)
2227 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002228 brelse(bh);
2229 return ret;
2230}
2231
2232static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2233 struct ocfs2_dinode *di,
2234 struct ocfs2_xattr_info *xi,
2235 struct ocfs2_xattr_search *xis,
2236 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002237 struct ocfs2_xattr_set_ctxt *ctxt,
2238 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002239{
2240 int clusters_add, meta_add, ret;
2241 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2242
2243 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2244
2245 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2246
2247 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002248 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002249 if (ret) {
2250 mlog_errno(ret);
2251 return ret;
2252 }
2253
Tao Ma85db90e2008-11-12 08:27:01 +08002254 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2255 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002256
2257 if (meta_add) {
2258 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2259 &ctxt->meta_ac);
2260 if (ret) {
2261 mlog_errno(ret);
2262 goto out;
2263 }
2264 }
2265
2266 if (clusters_add) {
2267 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2268 if (ret)
2269 mlog_errno(ret);
2270 }
2271out:
2272 if (ret) {
2273 if (ctxt->meta_ac) {
2274 ocfs2_free_alloc_context(ctxt->meta_ac);
2275 ctxt->meta_ac = NULL;
2276 }
2277
2278 /*
2279 * We cannot have an error and a non null ctxt->data_ac.
2280 */
2281 }
2282
2283 return ret;
2284}
2285
Tao Ma85db90e2008-11-12 08:27:01 +08002286static int __ocfs2_xattr_set_handle(struct inode *inode,
2287 struct ocfs2_dinode *di,
2288 struct ocfs2_xattr_info *xi,
2289 struct ocfs2_xattr_search *xis,
2290 struct ocfs2_xattr_search *xbs,
2291 struct ocfs2_xattr_set_ctxt *ctxt)
2292{
2293 int ret = 0, credits;
2294
2295 if (!xi->value) {
2296 /* Remove existing extended attribute */
2297 if (!xis->not_found)
2298 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2299 else if (!xbs->not_found)
2300 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2301 } else {
2302 /* We always try to set extended attribute into inode first*/
2303 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2304 if (!ret && !xbs->not_found) {
2305 /*
2306 * If succeed and that extended attribute existing in
2307 * external block, then we will remove it.
2308 */
2309 xi->value = NULL;
2310 xi->value_len = 0;
2311
2312 xis->not_found = -ENODATA;
2313 ret = ocfs2_calc_xattr_set_need(inode,
2314 di,
2315 xi,
2316 xis,
2317 xbs,
2318 NULL,
2319 NULL,
2320 &credits);
2321 if (ret) {
2322 mlog_errno(ret);
2323 goto out;
2324 }
2325
2326 ret = ocfs2_extend_trans(ctxt->handle, credits +
2327 ctxt->handle->h_buffer_credits);
2328 if (ret) {
2329 mlog_errno(ret);
2330 goto out;
2331 }
2332 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2333 } else if (ret == -ENOSPC) {
2334 if (di->i_xattr_loc && !xbs->xattr_bh) {
2335 ret = ocfs2_xattr_block_find(inode,
2336 xi->name_index,
2337 xi->name, xbs);
2338 if (ret)
2339 goto out;
2340
2341 xis->not_found = -ENODATA;
2342 ret = ocfs2_calc_xattr_set_need(inode,
2343 di,
2344 xi,
2345 xis,
2346 xbs,
2347 NULL,
2348 NULL,
2349 &credits);
2350 if (ret) {
2351 mlog_errno(ret);
2352 goto out;
2353 }
2354
2355 ret = ocfs2_extend_trans(ctxt->handle, credits +
2356 ctxt->handle->h_buffer_credits);
2357 if (ret) {
2358 mlog_errno(ret);
2359 goto out;
2360 }
2361 }
2362 /*
2363 * If no space in inode, we will set extended attribute
2364 * into external block.
2365 */
2366 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2367 if (ret)
2368 goto out;
2369 if (!xis->not_found) {
2370 /*
2371 * If succeed and that extended attribute
2372 * existing in inode, we will remove it.
2373 */
2374 xi->value = NULL;
2375 xi->value_len = 0;
2376 xbs->not_found = -ENODATA;
2377 ret = ocfs2_calc_xattr_set_need(inode,
2378 di,
2379 xi,
2380 xis,
2381 xbs,
2382 NULL,
2383 NULL,
2384 &credits);
2385 if (ret) {
2386 mlog_errno(ret);
2387 goto out;
2388 }
2389
2390 ret = ocfs2_extend_trans(ctxt->handle, credits +
2391 ctxt->handle->h_buffer_credits);
2392 if (ret) {
2393 mlog_errno(ret);
2394 goto out;
2395 }
2396 ret = ocfs2_xattr_ibody_set(inode, xi,
2397 xis, ctxt);
2398 }
2399 }
2400 }
2401
2402out:
2403 return ret;
2404}
2405
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002406/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002407 * This function only called duing creating inode
2408 * for init security/acl xattrs of the new inode.
2409 * The xattrs could be put into ibody or extent block,
2410 * xattr bucket would not be use in this case.
2411 * transanction credits also be reserved in here.
2412 */
2413int ocfs2_xattr_set_handle(handle_t *handle,
2414 struct inode *inode,
2415 struct buffer_head *di_bh,
2416 int name_index,
2417 const char *name,
2418 const void *value,
2419 size_t value_len,
2420 int flags,
2421 struct ocfs2_alloc_context *meta_ac,
2422 struct ocfs2_alloc_context *data_ac)
2423{
2424 struct ocfs2_dinode *di;
2425 int ret;
2426
2427 struct ocfs2_xattr_info xi = {
2428 .name_index = name_index,
2429 .name = name,
2430 .value = value,
2431 .value_len = value_len,
2432 };
2433
2434 struct ocfs2_xattr_search xis = {
2435 .not_found = -ENODATA,
2436 };
2437
2438 struct ocfs2_xattr_search xbs = {
2439 .not_found = -ENODATA,
2440 };
2441
2442 struct ocfs2_xattr_set_ctxt ctxt = {
2443 .handle = handle,
2444 .meta_ac = meta_ac,
2445 .data_ac = data_ac,
2446 };
2447
2448 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2449 return -EOPNOTSUPP;
2450
2451 xis.inode_bh = xbs.inode_bh = di_bh;
2452 di = (struct ocfs2_dinode *)di_bh->b_data;
2453
2454 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2455
2456 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2457 if (ret)
2458 goto cleanup;
2459 if (xis.not_found) {
2460 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2461 if (ret)
2462 goto cleanup;
2463 }
2464
2465 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2466
2467cleanup:
2468 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2469 brelse(xbs.xattr_bh);
2470
2471 return ret;
2472}
2473
2474/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002475 * ocfs2_xattr_set()
2476 *
2477 * Set, replace or remove an extended attribute for this inode.
2478 * value is NULL to remove an existing extended attribute, else either
2479 * create or replace an extended attribute.
2480 */
2481int ocfs2_xattr_set(struct inode *inode,
2482 int name_index,
2483 const char *name,
2484 const void *value,
2485 size_t value_len,
2486 int flags)
2487{
2488 struct buffer_head *di_bh = NULL;
2489 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002490 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002491 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002492 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002493 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002494
2495 struct ocfs2_xattr_info xi = {
2496 .name_index = name_index,
2497 .name = name,
2498 .value = value,
2499 .value_len = value_len,
2500 };
2501
2502 struct ocfs2_xattr_search xis = {
2503 .not_found = -ENODATA,
2504 };
2505
2506 struct ocfs2_xattr_search xbs = {
2507 .not_found = -ENODATA,
2508 };
2509
Tiger Yang8154da32008-08-18 17:11:46 +08002510 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2511 return -EOPNOTSUPP;
2512
Joel Beckerba937122008-10-24 19:13:20 -07002513 /*
2514 * Only xbs will be used on indexed trees. xis doesn't need a
2515 * bucket.
2516 */
2517 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2518 if (!xbs.bucket) {
2519 mlog_errno(-ENOMEM);
2520 return -ENOMEM;
2521 }
2522
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002523 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2524 if (ret < 0) {
2525 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002526 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002527 }
2528 xis.inode_bh = xbs.inode_bh = di_bh;
2529 di = (struct ocfs2_dinode *)di_bh->b_data;
2530
2531 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2532 /*
2533 * Scan inode and external block to find the same name
2534 * extended attribute and collect search infomation.
2535 */
2536 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2537 if (ret)
2538 goto cleanup;
2539 if (xis.not_found) {
2540 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2541 if (ret)
2542 goto cleanup;
2543 }
2544
2545 if (xis.not_found && xbs.not_found) {
2546 ret = -ENODATA;
2547 if (flags & XATTR_REPLACE)
2548 goto cleanup;
2549 ret = 0;
2550 if (!value)
2551 goto cleanup;
2552 } else {
2553 ret = -EEXIST;
2554 if (flags & XATTR_CREATE)
2555 goto cleanup;
2556 }
2557
Tao Ma85db90e2008-11-12 08:27:01 +08002558
2559 mutex_lock(&tl_inode->i_mutex);
2560
2561 if (ocfs2_truncate_log_needs_flush(osb)) {
2562 ret = __ocfs2_flush_truncate_log(osb);
2563 if (ret < 0) {
2564 mutex_unlock(&tl_inode->i_mutex);
2565 mlog_errno(ret);
2566 goto cleanup;
2567 }
2568 }
2569 mutex_unlock(&tl_inode->i_mutex);
2570
2571 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2572 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002573 if (ret) {
2574 mlog_errno(ret);
2575 goto cleanup;
2576 }
2577
Tao Ma85db90e2008-11-12 08:27:01 +08002578 ctxt.handle = ocfs2_start_trans(osb, credits);
2579 if (IS_ERR(ctxt.handle)) {
2580 ret = PTR_ERR(ctxt.handle);
2581 mlog_errno(ret);
2582 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002583 }
Tao Ma85db90e2008-11-12 08:27:01 +08002584
2585 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2586
2587 ocfs2_commit_trans(osb, ctxt.handle);
2588
Tao Ma78f30c32008-11-12 08:27:00 +08002589 if (ctxt.data_ac)
2590 ocfs2_free_alloc_context(ctxt.data_ac);
2591 if (ctxt.meta_ac)
2592 ocfs2_free_alloc_context(ctxt.meta_ac);
2593 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2594 ocfs2_schedule_truncate_log_flush(osb, 1);
2595 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002596cleanup:
2597 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2598 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002599cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002600 brelse(di_bh);
2601 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002602 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002603
2604 return ret;
2605}
2606
Tao Ma0c044f02008-08-18 17:38:50 +08002607/*
2608 * Find the xattr extent rec which may contains name_hash.
2609 * e_cpos will be the first name hash of the xattr rec.
2610 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2611 */
2612static int ocfs2_xattr_get_rec(struct inode *inode,
2613 u32 name_hash,
2614 u64 *p_blkno,
2615 u32 *e_cpos,
2616 u32 *num_clusters,
2617 struct ocfs2_extent_list *el)
2618{
2619 int ret = 0, i;
2620 struct buffer_head *eb_bh = NULL;
2621 struct ocfs2_extent_block *eb;
2622 struct ocfs2_extent_rec *rec = NULL;
2623 u64 e_blkno = 0;
2624
2625 if (el->l_tree_depth) {
2626 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2627 if (ret) {
2628 mlog_errno(ret);
2629 goto out;
2630 }
2631
2632 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2633 el = &eb->h_list;
2634
2635 if (el->l_tree_depth) {
2636 ocfs2_error(inode->i_sb,
2637 "Inode %lu has non zero tree depth in "
2638 "xattr tree block %llu\n", inode->i_ino,
2639 (unsigned long long)eb_bh->b_blocknr);
2640 ret = -EROFS;
2641 goto out;
2642 }
2643 }
2644
2645 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2646 rec = &el->l_recs[i];
2647
2648 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2649 e_blkno = le64_to_cpu(rec->e_blkno);
2650 break;
2651 }
2652 }
2653
2654 if (!e_blkno) {
2655 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2656 "record (%u, %u, 0) in xattr", inode->i_ino,
2657 le32_to_cpu(rec->e_cpos),
2658 ocfs2_rec_clusters(el, rec));
2659 ret = -EROFS;
2660 goto out;
2661 }
2662
2663 *p_blkno = le64_to_cpu(rec->e_blkno);
2664 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2665 if (e_cpos)
2666 *e_cpos = le32_to_cpu(rec->e_cpos);
2667out:
2668 brelse(eb_bh);
2669 return ret;
2670}
2671
2672typedef int (xattr_bucket_func)(struct inode *inode,
2673 struct ocfs2_xattr_bucket *bucket,
2674 void *para);
2675
Tao Ma589dc262008-08-18 17:38:51 +08002676static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002677 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002678 int name_index,
2679 const char *name,
2680 u32 name_hash,
2681 u16 *xe_index,
2682 int *found)
2683{
2684 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002685 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002686 size_t name_len = strlen(name);
2687 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002688 char *xe_name;
2689
2690 /*
2691 * We don't use binary search in the bucket because there
2692 * may be multiple entries with the same name hash.
2693 */
2694 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2695 xe = &xh->xh_entries[i];
2696
2697 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2698 continue;
2699 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2700 break;
2701
2702 cmp = name_index - ocfs2_xattr_get_type(xe);
2703 if (!cmp)
2704 cmp = name_len - xe->xe_name_len;
2705 if (cmp)
2706 continue;
2707
2708 ret = ocfs2_xattr_bucket_get_name_value(inode,
2709 xh,
2710 i,
2711 &block_off,
2712 &new_offset);
2713 if (ret) {
2714 mlog_errno(ret);
2715 break;
2716 }
2717
Joel Beckere2356a32008-10-27 15:01:54 -07002718 xe_name = bucket_block(bucket, block_off) + new_offset;
2719 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002720 *xe_index = i;
2721 *found = 1;
2722 ret = 0;
2723 break;
2724 }
2725 }
2726
2727 return ret;
2728}
2729
2730/*
2731 * Find the specified xattr entry in a series of buckets.
2732 * This series start from p_blkno and last for num_clusters.
2733 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2734 * the num of the valid buckets.
2735 *
2736 * Return the buffer_head this xattr should reside in. And if the xattr's
2737 * hash is in the gap of 2 buckets, return the lower bucket.
2738 */
2739static int ocfs2_xattr_bucket_find(struct inode *inode,
2740 int name_index,
2741 const char *name,
2742 u32 name_hash,
2743 u64 p_blkno,
2744 u32 first_hash,
2745 u32 num_clusters,
2746 struct ocfs2_xattr_search *xs)
2747{
2748 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002749 struct ocfs2_xattr_header *xh = NULL;
2750 struct ocfs2_xattr_entry *xe = NULL;
2751 u16 index = 0;
2752 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2753 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002754 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002755 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002756 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002757
Joel Beckere2356a32008-10-27 15:01:54 -07002758 search = ocfs2_xattr_bucket_new(inode);
2759 if (!search) {
2760 ret = -ENOMEM;
2761 mlog_errno(ret);
2762 goto out;
2763 }
2764
2765 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002766 if (ret) {
2767 mlog_errno(ret);
2768 goto out;
2769 }
2770
Joel Beckere2356a32008-10-27 15:01:54 -07002771 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002772 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002773 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002774 ocfs2_xattr_bucket_relse(search);
2775
Tao Ma589dc262008-08-18 17:38:51 +08002776 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002777 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002778 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002779 if (ret) {
2780 mlog_errno(ret);
2781 goto out;
2782 }
2783
Joel Beckere2356a32008-10-27 15:01:54 -07002784 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002785 xe = &xh->xh_entries[0];
2786 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2787 high_bucket = bucket - 1;
2788 continue;
2789 }
2790
2791 /*
2792 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08002793 * bucket is larger than the search one. for an empty
2794 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002795 */
Tao Ma5a095612008-09-19 22:17:41 +08002796 if (xh->xh_count)
2797 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2798
Tao Ma589dc262008-08-18 17:38:51 +08002799 last_hash = le32_to_cpu(xe->xe_name_hash);
2800
Joel Beckere2356a32008-10-27 15:01:54 -07002801 /* record lower_blkno which may be the insert place. */
2802 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002803
2804 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2805 low_bucket = bucket + 1;
2806 continue;
2807 }
2808
2809 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002810 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002811 name_index, name, name_hash,
2812 &index, &found);
2813 if (ret) {
2814 mlog_errno(ret);
2815 goto out;
2816 }
2817 break;
2818 }
2819
2820 /*
2821 * Record the bucket we have found.
2822 * When the xattr's hash value is in the gap of 2 buckets, we will
2823 * always set it to the previous bucket.
2824 */
Joel Beckere2356a32008-10-27 15:01:54 -07002825 if (!lower_blkno)
2826 lower_blkno = p_blkno;
2827
2828 /* This should be in cache - we just read it during the search */
2829 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2830 if (ret) {
2831 mlog_errno(ret);
2832 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08002833 }
Tao Ma589dc262008-08-18 17:38:51 +08002834
Joel Beckerba937122008-10-24 19:13:20 -07002835 xs->header = bucket_xh(xs->bucket);
2836 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002837 xs->end = xs->base + inode->i_sb->s_blocksize;
2838
2839 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08002840 xs->here = &xs->header->xh_entries[index];
2841 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07002842 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08002843 } else
2844 ret = -ENODATA;
2845
2846out:
Joel Beckere2356a32008-10-27 15:01:54 -07002847 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08002848 return ret;
2849}
2850
2851static int ocfs2_xattr_index_block_find(struct inode *inode,
2852 struct buffer_head *root_bh,
2853 int name_index,
2854 const char *name,
2855 struct ocfs2_xattr_search *xs)
2856{
2857 int ret;
2858 struct ocfs2_xattr_block *xb =
2859 (struct ocfs2_xattr_block *)root_bh->b_data;
2860 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2861 struct ocfs2_extent_list *el = &xb_root->xt_list;
2862 u64 p_blkno = 0;
2863 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08002864 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08002865
2866 if (le16_to_cpu(el->l_next_free_rec) == 0)
2867 return -ENODATA;
2868
2869 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2870 name, name_hash, name_index);
2871
2872 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2873 &num_clusters, el);
2874 if (ret) {
2875 mlog_errno(ret);
2876 goto out;
2877 }
2878
2879 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2880
2881 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07002882 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2883 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08002884
2885 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2886 p_blkno, first_hash, num_clusters, xs);
2887
2888out:
2889 return ret;
2890}
2891
Tao Ma0c044f02008-08-18 17:38:50 +08002892static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2893 u64 blkno,
2894 u32 clusters,
2895 xattr_bucket_func *func,
2896 void *para)
2897{
Joel Becker6dde41d2008-10-24 17:16:48 -07002898 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08002899 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2900 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07002901 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08002902
Joel Beckerba937122008-10-24 19:13:20 -07002903 bucket = ocfs2_xattr_bucket_new(inode);
2904 if (!bucket) {
2905 mlog_errno(-ENOMEM);
2906 return -ENOMEM;
2907 }
Tao Ma0c044f02008-08-18 17:38:50 +08002908
2909 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07002910 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002911
Joel Beckerba937122008-10-24 19:13:20 -07002912 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
2913 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002914 if (ret) {
2915 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002916 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002917 }
2918
Tao Ma0c044f02008-08-18 17:38:50 +08002919 /*
2920 * The real bucket num in this series of blocks is stored
2921 * in the 1st bucket.
2922 */
2923 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07002924 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08002925
Mark Fashehde29c082008-10-29 14:45:30 -07002926 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2927 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07002928 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08002929 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07002930 ret = func(inode, bucket, para);
2931 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08002932 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002933 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08002934 }
2935
Joel Beckerba937122008-10-24 19:13:20 -07002936 ocfs2_xattr_bucket_relse(bucket);
2937 if (ret)
2938 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002939 }
2940
Joel Beckerba937122008-10-24 19:13:20 -07002941 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08002942 return ret;
2943}
2944
2945struct ocfs2_xattr_tree_list {
2946 char *buffer;
2947 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08002948 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08002949};
2950
2951static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2952 struct ocfs2_xattr_header *xh,
2953 int index,
2954 int *block_off,
2955 int *new_offset)
2956{
2957 u16 name_offset;
2958
2959 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2960 return -EINVAL;
2961
2962 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2963
2964 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2965 *new_offset = name_offset % inode->i_sb->s_blocksize;
2966
2967 return 0;
2968}
2969
2970static int ocfs2_list_xattr_bucket(struct inode *inode,
2971 struct ocfs2_xattr_bucket *bucket,
2972 void *para)
2973{
Tao Ma936b8832008-10-09 23:06:14 +08002974 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08002975 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08002976 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08002977 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08002978
Joel Becker3e632942008-10-24 17:04:49 -07002979 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
2980 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08002981 type = ocfs2_xattr_get_type(entry);
2982 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08002983
Tao Ma936b8832008-10-09 23:06:14 +08002984 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08002985 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07002986 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08002987 i,
2988 &block_off,
2989 &new_offset);
2990 if (ret)
2991 break;
Tao Ma936b8832008-10-09 23:06:14 +08002992
Joel Becker51def392008-10-24 16:57:21 -07002993 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08002994 new_offset;
2995 ret = ocfs2_xattr_list_entry(xl->buffer,
2996 xl->buffer_size,
2997 &xl->result,
2998 prefix, name,
2999 entry->xe_name_len);
3000 if (ret)
3001 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003002 }
3003 }
3004
3005 return ret;
3006}
3007
3008static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3009 struct ocfs2_xattr_tree_root *xt,
3010 char *buffer,
3011 size_t buffer_size)
3012{
3013 struct ocfs2_extent_list *el = &xt->xt_list;
3014 int ret = 0;
3015 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3016 u64 p_blkno = 0;
3017 struct ocfs2_xattr_tree_list xl = {
3018 .buffer = buffer,
3019 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08003020 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08003021 };
3022
3023 if (le16_to_cpu(el->l_next_free_rec) == 0)
3024 return 0;
3025
3026 while (name_hash > 0) {
3027 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3028 &e_cpos, &num_clusters, el);
3029 if (ret) {
3030 mlog_errno(ret);
3031 goto out;
3032 }
3033
3034 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3035 ocfs2_list_xattr_bucket,
3036 &xl);
3037 if (ret) {
3038 mlog_errno(ret);
3039 goto out;
3040 }
3041
3042 if (e_cpos == 0)
3043 break;
3044
3045 name_hash = e_cpos - 1;
3046 }
3047
Tao Ma936b8832008-10-09 23:06:14 +08003048 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003049out:
3050 return ret;
3051}
Tao Ma01225592008-08-18 17:38:53 +08003052
3053static int cmp_xe(const void *a, const void *b)
3054{
3055 const struct ocfs2_xattr_entry *l = a, *r = b;
3056 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3057 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3058
3059 if (l_hash > r_hash)
3060 return 1;
3061 if (l_hash < r_hash)
3062 return -1;
3063 return 0;
3064}
3065
3066static void swap_xe(void *a, void *b, int size)
3067{
3068 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3069
3070 tmp = *l;
3071 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3072 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3073}
3074
3075/*
3076 * When the ocfs2_xattr_block is filled up, new bucket will be created
3077 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003078 * The header goes at the start of the bucket, and the names+values are
3079 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003080 * Note: we need to sort the entries since they are not saved in order
3081 * in the ocfs2_xattr_block.
3082 */
3083static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3084 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003085 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003086{
3087 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003088 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003089 u16 offset, size, off_change;
3090 struct ocfs2_xattr_entry *xe;
3091 struct ocfs2_xattr_block *xb =
3092 (struct ocfs2_xattr_block *)xb_bh->b_data;
3093 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003094 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003095 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003096 char *src = xb_bh->b_data;
3097 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003098
3099 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3100 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003101 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003102
Joel Becker178eeac2008-10-27 15:18:29 -07003103 for (i = 0; i < blks; i++)
3104 memset(bucket_block(bucket, i), 0, blocksize);
3105
Tao Ma01225592008-08-18 17:38:53 +08003106 /*
3107 * Since the xe_name_offset is based on ocfs2_xattr_header,
3108 * there is a offset change corresponding to the change of
3109 * ocfs2_xattr_header's position.
3110 */
3111 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3112 xe = &xb_xh->xh_entries[count - 1];
3113 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3114 size = blocksize - offset;
3115
3116 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003117 memcpy(target + offset, src + offset, size);
3118
3119 /* Init new header now. */
3120 xh->xh_count = xb_xh->xh_count;
3121 xh->xh_num_buckets = cpu_to_le16(1);
3122 xh->xh_name_value_len = cpu_to_le16(size);
3123 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3124
3125 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003126 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003127 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3128 size = count * sizeof(struct ocfs2_xattr_entry);
3129 memcpy(target + offset, (char *)xb_xh + offset, size);
3130
3131 /* Change the xe offset for all the xe because of the move. */
3132 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3133 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3134 for (i = 0; i < count; i++)
3135 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3136
3137 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3138 offset, size, off_change);
3139
3140 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3141 cmp_xe, swap_xe);
3142}
3143
3144/*
3145 * After we move xattr from block to index btree, we have to
3146 * update ocfs2_xattr_search to the new xe and base.
3147 *
3148 * When the entry is in xattr block, xattr_bh indicates the storage place.
3149 * While if the entry is in index b-tree, "bucket" indicates the
3150 * real place of the xattr.
3151 */
Joel Becker178eeac2008-10-27 15:18:29 -07003152static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3153 struct ocfs2_xattr_search *xs,
3154 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003155{
Tao Ma01225592008-08-18 17:38:53 +08003156 char *buf = old_bh->b_data;
3157 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3158 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003159 int i;
Tao Ma01225592008-08-18 17:38:53 +08003160
Joel Beckerba937122008-10-24 19:13:20 -07003161 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003162 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003163 xs->end = xs->base + inode->i_sb->s_blocksize;
3164
Joel Becker178eeac2008-10-27 15:18:29 -07003165 if (xs->not_found)
3166 return;
Tao Ma01225592008-08-18 17:38:53 +08003167
Joel Becker178eeac2008-10-27 15:18:29 -07003168 i = xs->here - old_xh->xh_entries;
3169 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003170}
3171
3172static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003173 struct ocfs2_xattr_search *xs,
3174 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003175{
Tao Ma85db90e2008-11-12 08:27:01 +08003176 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003177 u32 bit_off, len;
3178 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003179 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003180 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3181 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003182 struct buffer_head *xb_bh = xs->xattr_bh;
3183 struct ocfs2_xattr_block *xb =
3184 (struct ocfs2_xattr_block *)xb_bh->b_data;
3185 struct ocfs2_xattr_tree_root *xr;
3186 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003187
3188 mlog(0, "create xattr index block for %llu\n",
3189 (unsigned long long)xb_bh->b_blocknr);
3190
3191 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003192 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003193
Tao Ma01225592008-08-18 17:38:53 +08003194 /*
3195 * XXX:
3196 * We can use this lock for now, and maybe move to a dedicated mutex
3197 * if performance becomes a problem later.
3198 */
3199 down_write(&oi->ip_alloc_sem);
3200
Tao Ma01225592008-08-18 17:38:53 +08003201 ret = ocfs2_journal_access(handle, inode, xb_bh,
3202 OCFS2_JOURNAL_ACCESS_WRITE);
3203 if (ret) {
3204 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003205 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003206 }
3207
Tao Ma78f30c32008-11-12 08:27:00 +08003208 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3209 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003210 if (ret) {
3211 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003212 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003213 }
3214
3215 /*
3216 * The bucket may spread in many blocks, and
3217 * we will only touch the 1st block and the last block
3218 * in the whole bucket(one for entry and one for data).
3219 */
3220 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3221
Mark Fashehde29c082008-10-29 14:45:30 -07003222 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3223 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003224
Joel Becker178eeac2008-10-27 15:18:29 -07003225 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003226 if (ret) {
3227 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003228 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003229 }
3230
Joel Becker178eeac2008-10-27 15:18:29 -07003231 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3232 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003233 if (ret) {
3234 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003235 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003236 }
Tao Ma01225592008-08-18 17:38:53 +08003237
Joel Becker178eeac2008-10-27 15:18:29 -07003238 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3239 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3240
3241 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3242
Tao Ma01225592008-08-18 17:38:53 +08003243 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3244 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3245 offsetof(struct ocfs2_xattr_block, xb_attrs));
3246
3247 xr = &xb->xb_attrs.xb_root;
3248 xr->xt_clusters = cpu_to_le32(1);
3249 xr->xt_last_eb_blk = 0;
3250 xr->xt_list.l_tree_depth = 0;
3251 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3252 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3253
3254 xr->xt_list.l_recs[0].e_cpos = 0;
3255 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3256 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3257
3258 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3259
Tao Ma85db90e2008-11-12 08:27:01 +08003260 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003261
Tao Ma85db90e2008-11-12 08:27:01 +08003262out:
Tao Ma01225592008-08-18 17:38:53 +08003263 up_write(&oi->ip_alloc_sem);
3264
Tao Ma01225592008-08-18 17:38:53 +08003265 return ret;
3266}
3267
3268static int cmp_xe_offset(const void *a, const void *b)
3269{
3270 const struct ocfs2_xattr_entry *l = a, *r = b;
3271 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3272 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3273
3274 if (l_name_offset < r_name_offset)
3275 return 1;
3276 if (l_name_offset > r_name_offset)
3277 return -1;
3278 return 0;
3279}
3280
3281/*
3282 * defrag a xattr bucket if we find that the bucket has some
3283 * holes beteen name/value pairs.
3284 * We will move all the name/value pairs to the end of the bucket
3285 * so that we can spare some space for insertion.
3286 */
3287static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003288 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003289 struct ocfs2_xattr_bucket *bucket)
3290{
3291 int ret, i;
3292 size_t end, offset, len, value_len;
3293 struct ocfs2_xattr_header *xh;
3294 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003295 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003296 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003297 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003298 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003299
3300 /*
3301 * In order to make the operation more efficient and generic,
3302 * we copy all the blocks into a contiguous memory and do the
3303 * defragment there, so if anything is error, we will not touch
3304 * the real block.
3305 */
3306 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3307 if (!bucket_buf) {
3308 ret = -EIO;
3309 goto out;
3310 }
3311
Joel Becker161d6f32008-10-27 15:25:18 -07003312 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003313 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3314 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003315
Tao Ma1c32a2f2008-11-06 08:10:47 +08003316 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003317 OCFS2_JOURNAL_ACCESS_WRITE);
3318 if (ret < 0) {
3319 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003320 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003321 }
3322
3323 xh = (struct ocfs2_xattr_header *)bucket_buf;
3324 entries = (char *)xh->xh_entries;
3325 xh_free_start = le16_to_cpu(xh->xh_free_start);
3326
3327 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3328 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003329 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3330 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003331
3332 /*
3333 * sort all the entries by their offset.
3334 * the largest will be the first, so that we can
3335 * move them to the end one by one.
3336 */
3337 sort(entries, le16_to_cpu(xh->xh_count),
3338 sizeof(struct ocfs2_xattr_entry),
3339 cmp_xe_offset, swap_xe);
3340
3341 /* Move all name/values to the end of the bucket. */
3342 xe = xh->xh_entries;
3343 end = OCFS2_XATTR_BUCKET_SIZE;
3344 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3345 offset = le16_to_cpu(xe->xe_name_offset);
3346 if (ocfs2_xattr_is_local(xe))
3347 value_len = OCFS2_XATTR_SIZE(
3348 le64_to_cpu(xe->xe_value_size));
3349 else
3350 value_len = OCFS2_XATTR_ROOT_SIZE;
3351 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3352
3353 /*
3354 * We must make sure that the name/value pair
3355 * exist in the same block. So adjust end to
3356 * the previous block end if needed.
3357 */
3358 if (((end - len) / blocksize !=
3359 (end - 1) / blocksize))
3360 end = end - end % blocksize;
3361
3362 if (end > offset + len) {
3363 memmove(bucket_buf + end - len,
3364 bucket_buf + offset, len);
3365 xe->xe_name_offset = cpu_to_le16(end - len);
3366 }
3367
3368 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3369 "bucket %llu\n", (unsigned long long)blkno);
3370
3371 end -= len;
3372 }
3373
3374 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3375 "bucket %llu\n", (unsigned long long)blkno);
3376
3377 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003378 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003379
3380 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3381 xh->xh_free_start = cpu_to_le16(end);
3382
3383 /* sort the entries by their name_hash. */
3384 sort(entries, le16_to_cpu(xh->xh_count),
3385 sizeof(struct ocfs2_xattr_entry),
3386 cmp_xe, swap_xe);
3387
3388 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003389 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3390 memcpy(bucket_block(bucket, i), buf, blocksize);
3391 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003392
Tao Ma01225592008-08-18 17:38:53 +08003393out:
Tao Ma01225592008-08-18 17:38:53 +08003394 kfree(bucket_buf);
3395 return ret;
3396}
3397
3398/*
3399 * Move half nums of the xattr bucket in the previous cluster to this new
3400 * cluster. We only touch the last cluster of the previous extend record.
3401 *
3402 * first_bh is the first buffer_head of a series of bucket in the same
3403 * extent rec and header_bh is the header of one bucket in this cluster.
3404 * They will be updated if we move the data header_bh contains to the new
3405 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3406 */
3407static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3408 handle_t *handle,
3409 struct buffer_head **first_bh,
3410 struct buffer_head **header_bh,
3411 u64 new_blkno,
3412 u64 prev_blkno,
3413 u32 num_clusters,
3414 u32 *first_hash)
3415{
3416 int i, ret, credits;
3417 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3418 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3419 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3420 int blocksize = inode->i_sb->s_blocksize;
3421 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3422 struct ocfs2_xattr_header *new_xh;
3423 struct ocfs2_xattr_header *xh =
3424 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3425
3426 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3427 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3428
3429 prev_bh = *first_bh;
3430 get_bh(prev_bh);
3431 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3432
3433 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3434
3435 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003436 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003437
3438 /*
3439 * We need to update the 1st half of the new cluster and
3440 * 1 more for the update of the 1st bucket of the previous
3441 * extent record.
3442 */
Tao Ma85db90e2008-11-12 08:27:01 +08003443 credits = bpc / 2 + 1 + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003444 ret = ocfs2_extend_trans(handle, credits);
3445 if (ret) {
3446 mlog_errno(ret);
3447 goto out;
3448 }
3449
3450 ret = ocfs2_journal_access(handle, inode, prev_bh,
3451 OCFS2_JOURNAL_ACCESS_WRITE);
3452 if (ret) {
3453 mlog_errno(ret);
3454 goto out;
3455 }
3456
3457 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3458 old_bh = new_bh = NULL;
3459 new_bh = sb_getblk(inode->i_sb, new_blkno);
3460 if (!new_bh) {
3461 ret = -EIO;
3462 mlog_errno(ret);
3463 goto out;
3464 }
3465
3466 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3467
3468 ret = ocfs2_journal_access(handle, inode, new_bh,
3469 OCFS2_JOURNAL_ACCESS_CREATE);
3470 if (ret < 0) {
3471 mlog_errno(ret);
3472 brelse(new_bh);
3473 goto out;
3474 }
3475
Joel Becker0fcaa562008-10-09 17:20:31 -07003476 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
Tao Ma01225592008-08-18 17:38:53 +08003477 if (ret < 0) {
3478 mlog_errno(ret);
3479 brelse(new_bh);
3480 goto out;
3481 }
3482
3483 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3484
3485 if (i == 0) {
3486 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3487 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3488
3489 if (first_hash)
3490 *first_hash = le32_to_cpu(
3491 new_xh->xh_entries[0].xe_name_hash);
3492 new_first_bh = new_bh;
3493 get_bh(new_first_bh);
3494 }
3495
3496 ocfs2_journal_dirty(handle, new_bh);
3497
3498 if (*header_bh == old_bh) {
3499 brelse(*header_bh);
3500 *header_bh = new_bh;
3501 get_bh(*header_bh);
3502
3503 brelse(*first_bh);
3504 *first_bh = new_first_bh;
3505 get_bh(*first_bh);
3506 }
3507 brelse(new_bh);
3508 brelse(old_bh);
3509 }
3510
3511 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3512
3513 ocfs2_journal_dirty(handle, prev_bh);
3514out:
3515 brelse(prev_bh);
3516 brelse(new_first_bh);
3517 return ret;
3518}
3519
Tao Ma01225592008-08-18 17:38:53 +08003520/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003521 * Find the suitable pos when we divide a bucket into 2.
3522 * We have to make sure the xattrs with the same hash value exist
3523 * in the same bucket.
3524 *
3525 * If this ocfs2_xattr_header covers more than one hash value, find a
3526 * place where the hash value changes. Try to find the most even split.
3527 * The most common case is that all entries have different hash values,
3528 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003529 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003530static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3531{
3532 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3533 int count = le16_to_cpu(xh->xh_count);
3534 int delta, middle = count / 2;
3535
3536 /*
3537 * We start at the middle. Each step gets farther away in both
3538 * directions. We therefore hit the change in hash value
3539 * nearest to the middle. Note that this loop does not execute for
3540 * count < 2.
3541 */
3542 for (delta = 0; delta < middle; delta++) {
3543 /* Let's check delta earlier than middle */
3544 if (cmp_xe(&entries[middle - delta - 1],
3545 &entries[middle - delta]))
3546 return middle - delta;
3547
3548 /* For even counts, don't walk off the end */
3549 if ((middle + delta + 1) == count)
3550 continue;
3551
3552 /* Now try delta past middle */
3553 if (cmp_xe(&entries[middle + delta],
3554 &entries[middle + delta + 1]))
3555 return middle + delta + 1;
3556 }
3557
3558 /* Every entry had the same hash */
3559 return count;
3560}
3561
3562/*
3563 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3564 * first_hash will record the 1st hash of the new bucket.
3565 *
3566 * Normally half of the xattrs will be moved. But we have to make
3567 * sure that the xattrs with the same hash value are stored in the
3568 * same bucket. If all the xattrs in this bucket have the same hash
3569 * value, the new bucket will be initialized as an empty one and the
3570 * first_hash will be initialized as (hash_value+1).
3571 */
3572static int ocfs2_divide_xattr_bucket(struct inode *inode,
3573 handle_t *handle,
3574 u64 blk,
3575 u64 new_blk,
3576 u32 *first_hash,
3577 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003578{
3579 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003580 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003581 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003582 struct ocfs2_xattr_header *xh;
3583 struct ocfs2_xattr_entry *xe;
3584 int blocksize = inode->i_sb->s_blocksize;
3585
Tao Ma80bcaf32008-10-27 06:06:24 +08003586 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003587 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003588
Joel Beckerba937122008-10-24 19:13:20 -07003589 s_bucket = ocfs2_xattr_bucket_new(inode);
3590 t_bucket = ocfs2_xattr_bucket_new(inode);
3591 if (!s_bucket || !t_bucket) {
3592 ret = -ENOMEM;
3593 mlog_errno(ret);
3594 goto out;
3595 }
Tao Ma01225592008-08-18 17:38:53 +08003596
Joel Beckerba937122008-10-24 19:13:20 -07003597 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003598 if (ret) {
3599 mlog_errno(ret);
3600 goto out;
3601 }
3602
Joel Beckerba937122008-10-24 19:13:20 -07003603 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003604 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003605 if (ret) {
3606 mlog_errno(ret);
3607 goto out;
3608 }
3609
Joel Becker784b8162008-10-24 17:33:40 -07003610 /*
3611 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3612 * there's no need to read it.
3613 */
Joel Beckerba937122008-10-24 19:13:20 -07003614 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003615 if (ret) {
3616 mlog_errno(ret);
3617 goto out;
3618 }
3619
Joel Beckerba937122008-10-24 19:13:20 -07003620 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003621 new_bucket_head ?
3622 OCFS2_JOURNAL_ACCESS_CREATE :
3623 OCFS2_JOURNAL_ACCESS_WRITE);
3624 if (ret) {
3625 mlog_errno(ret);
3626 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003627 }
3628
Joel Beckerba937122008-10-24 19:13:20 -07003629 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003630 count = le16_to_cpu(xh->xh_count);
3631 start = ocfs2_xattr_find_divide_pos(xh);
3632
3633 if (start == count) {
3634 xe = &xh->xh_entries[start-1];
3635
3636 /*
3637 * initialized a new empty bucket here.
3638 * The hash value is set as one larger than
3639 * that of the last entry in the previous bucket.
3640 */
Joel Beckerba937122008-10-24 19:13:20 -07003641 for (i = 0; i < t_bucket->bu_blocks; i++)
3642 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003643
Joel Beckerba937122008-10-24 19:13:20 -07003644 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003645 xh->xh_free_start = cpu_to_le16(blocksize);
3646 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3647 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3648
3649 goto set_num_buckets;
3650 }
3651
Tao Ma01225592008-08-18 17:38:53 +08003652 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003653 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003654
3655 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003656 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003657
3658 /*
3659 * Calculate the total name/value len and xh_free_start for
3660 * the old bucket first.
3661 */
3662 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3663 name_value_len = 0;
3664 for (i = 0; i < start; i++) {
3665 xe = &xh->xh_entries[i];
3666 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3667 if (ocfs2_xattr_is_local(xe))
3668 xe_len +=
3669 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3670 else
3671 xe_len += OCFS2_XATTR_ROOT_SIZE;
3672 name_value_len += xe_len;
3673 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3674 name_offset = le16_to_cpu(xe->xe_name_offset);
3675 }
3676
3677 /*
3678 * Now begin the modification to the new bucket.
3679 *
3680 * In the new bucket, We just move the xattr entry to the beginning
3681 * and don't touch the name/value. So there will be some holes in the
3682 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3683 * called.
3684 */
3685 xe = &xh->xh_entries[start];
3686 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3687 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003688 (int)((char *)xe - (char *)xh),
3689 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003690 memmove((char *)xh->xh_entries, (char *)xe, len);
3691 xe = &xh->xh_entries[count - start];
3692 len = sizeof(struct ocfs2_xattr_entry) * start;
3693 memset((char *)xe, 0, len);
3694
3695 le16_add_cpu(&xh->xh_count, -start);
3696 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3697
3698 /* Calculate xh_free_start for the new bucket. */
3699 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3700 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3701 xe = &xh->xh_entries[i];
3702 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3703 if (ocfs2_xattr_is_local(xe))
3704 xe_len +=
3705 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3706 else
3707 xe_len += OCFS2_XATTR_ROOT_SIZE;
3708 if (le16_to_cpu(xe->xe_name_offset) <
3709 le16_to_cpu(xh->xh_free_start))
3710 xh->xh_free_start = xe->xe_name_offset;
3711 }
3712
Tao Ma80bcaf32008-10-27 06:06:24 +08003713set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003714 /* set xh->xh_num_buckets for the new xh. */
3715 if (new_bucket_head)
3716 xh->xh_num_buckets = cpu_to_le16(1);
3717 else
3718 xh->xh_num_buckets = 0;
3719
Joel Beckerba937122008-10-24 19:13:20 -07003720 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003721
3722 /* store the first_hash of the new bucket. */
3723 if (first_hash)
3724 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3725
3726 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003727 * Now only update the 1st block of the old bucket. If we
3728 * just added a new empty bucket, there is no need to modify
3729 * it.
Tao Ma01225592008-08-18 17:38:53 +08003730 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003731 if (start == count)
3732 goto out;
3733
Joel Beckerba937122008-10-24 19:13:20 -07003734 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003735 memset(&xh->xh_entries[start], 0,
3736 sizeof(struct ocfs2_xattr_entry) * (count - start));
3737 xh->xh_count = cpu_to_le16(start);
3738 xh->xh_free_start = cpu_to_le16(name_offset);
3739 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3740
Joel Beckerba937122008-10-24 19:13:20 -07003741 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003742
3743out:
Joel Beckerba937122008-10-24 19:13:20 -07003744 ocfs2_xattr_bucket_free(s_bucket);
3745 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003746
3747 return ret;
3748}
3749
3750/*
3751 * Copy xattr from one bucket to another bucket.
3752 *
3753 * The caller must make sure that the journal transaction
3754 * has enough space for journaling.
3755 */
3756static int ocfs2_cp_xattr_bucket(struct inode *inode,
3757 handle_t *handle,
3758 u64 s_blkno,
3759 u64 t_blkno,
3760 int t_is_new)
3761{
Joel Becker4980c6d2008-10-24 18:54:43 -07003762 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003763 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003764
3765 BUG_ON(s_blkno == t_blkno);
3766
3767 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003768 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3769 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003770
Joel Beckerba937122008-10-24 19:13:20 -07003771 s_bucket = ocfs2_xattr_bucket_new(inode);
3772 t_bucket = ocfs2_xattr_bucket_new(inode);
3773 if (!s_bucket || !t_bucket) {
3774 ret = -ENOMEM;
3775 mlog_errno(ret);
3776 goto out;
3777 }
3778
3779 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003780 if (ret)
3781 goto out;
3782
Joel Becker784b8162008-10-24 17:33:40 -07003783 /*
3784 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3785 * there's no need to read it.
3786 */
Joel Beckerba937122008-10-24 19:13:20 -07003787 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003788 if (ret)
3789 goto out;
3790
Joel Beckerba937122008-10-24 19:13:20 -07003791 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003792 t_is_new ?
3793 OCFS2_JOURNAL_ACCESS_CREATE :
3794 OCFS2_JOURNAL_ACCESS_WRITE);
3795 if (ret)
3796 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003797
Joel Beckerba937122008-10-24 19:13:20 -07003798 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3799 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003800
3801out:
Joel Beckerba937122008-10-24 19:13:20 -07003802 ocfs2_xattr_bucket_free(t_bucket);
3803 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003804
3805 return ret;
3806}
3807
3808/*
3809 * Copy one xattr cluster from src_blk to to_blk.
3810 * The to_blk will become the first bucket header of the cluster, so its
3811 * xh_num_buckets will be initialized as the bucket num in the cluster.
3812 */
3813static int ocfs2_cp_xattr_cluster(struct inode *inode,
3814 handle_t *handle,
3815 struct buffer_head *first_bh,
3816 u64 src_blk,
3817 u64 to_blk,
3818 u32 *first_hash)
3819{
3820 int i, ret, credits;
3821 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3822 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3823 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3824 struct buffer_head *bh = NULL;
3825 struct ocfs2_xattr_header *xh;
3826 u64 to_blk_start = to_blk;
3827
Mark Fashehde29c082008-10-29 14:45:30 -07003828 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3829 (unsigned long long)src_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003830
3831 /*
3832 * We need to update the new cluster and 1 more for the update of
3833 * the 1st bucket of the previous extent rec.
3834 */
Tao Ma85db90e2008-11-12 08:27:01 +08003835 credits = bpc + 1 + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003836 ret = ocfs2_extend_trans(handle, credits);
3837 if (ret) {
3838 mlog_errno(ret);
3839 goto out;
3840 }
3841
3842 ret = ocfs2_journal_access(handle, inode, first_bh,
3843 OCFS2_JOURNAL_ACCESS_WRITE);
3844 if (ret) {
3845 mlog_errno(ret);
3846 goto out;
3847 }
3848
3849 for (i = 0; i < num_buckets; i++) {
3850 ret = ocfs2_cp_xattr_bucket(inode, handle,
3851 src_blk, to_blk, 1);
3852 if (ret) {
3853 mlog_errno(ret);
3854 goto out;
3855 }
3856
3857 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3858 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3859 }
3860
3861 /* update the old bucket header. */
3862 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3863 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3864
3865 ocfs2_journal_dirty(handle, first_bh);
3866
3867 /* update the new bucket header. */
Joel Becker0fcaa562008-10-09 17:20:31 -07003868 ret = ocfs2_read_block(inode, to_blk_start, &bh);
Tao Ma01225592008-08-18 17:38:53 +08003869 if (ret < 0) {
3870 mlog_errno(ret);
3871 goto out;
3872 }
3873
3874 ret = ocfs2_journal_access(handle, inode, bh,
3875 OCFS2_JOURNAL_ACCESS_WRITE);
3876 if (ret) {
3877 mlog_errno(ret);
3878 goto out;
3879 }
3880
3881 xh = (struct ocfs2_xattr_header *)bh->b_data;
3882 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3883
3884 ocfs2_journal_dirty(handle, bh);
3885
3886 if (first_hash)
3887 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3888out:
3889 brelse(bh);
3890 return ret;
3891}
3892
3893/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003894 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08003895 * This function should only be called when bucket size == cluster size.
3896 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3897 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003898static int ocfs2_divide_xattr_cluster(struct inode *inode,
3899 handle_t *handle,
3900 u64 prev_blk,
3901 u64 new_blk,
3902 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003903{
3904 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003905 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003906
3907 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3908
3909 ret = ocfs2_extend_trans(handle, credits);
3910 if (ret) {
3911 mlog_errno(ret);
3912 return ret;
3913 }
3914
3915 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08003916 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3917 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08003918}
3919
3920/*
3921 * Move some xattrs from the old cluster to the new one since they are not
3922 * contiguous in ocfs2 xattr tree.
3923 *
3924 * new_blk starts a new separate cluster, and we will move some xattrs from
3925 * prev_blk to it. v_start will be set as the first name hash value in this
3926 * new cluster so that it can be used as e_cpos during tree insertion and
3927 * don't collide with our original b-tree operations. first_bh and header_bh
3928 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3929 * to extend the insert bucket.
3930 *
3931 * The problem is how much xattr should we move to the new one and when should
3932 * we update first_bh and header_bh?
3933 * 1. If cluster size > bucket size, that means the previous cluster has more
3934 * than 1 bucket, so just move half nums of bucket into the new cluster and
3935 * update the first_bh and header_bh if the insert bucket has been moved
3936 * to the new cluster.
3937 * 2. If cluster_size == bucket_size:
3938 * a) If the previous extent rec has more than one cluster and the insert
3939 * place isn't in the last cluster, copy the entire last cluster to the
3940 * new one. This time, we don't need to upate the first_bh and header_bh
3941 * since they will not be moved into the new cluster.
3942 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3943 * the new one. And we set the extend flag to zero if the insert place is
3944 * moved into the new allocated cluster since no extend is needed.
3945 */
3946static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3947 handle_t *handle,
3948 struct buffer_head **first_bh,
3949 struct buffer_head **header_bh,
3950 u64 new_blk,
3951 u64 prev_blk,
3952 u32 prev_clusters,
3953 u32 *v_start,
3954 int *extend)
3955{
3956 int ret = 0;
3957 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3958
3959 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003960 (unsigned long long)prev_blk, prev_clusters,
3961 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003962
3963 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3964 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3965 handle,
3966 first_bh,
3967 header_bh,
3968 new_blk,
3969 prev_blk,
3970 prev_clusters,
3971 v_start);
3972 else {
3973 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3974
3975 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3976 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3977 last_blk, new_blk,
3978 v_start);
3979 else {
Tao Ma80bcaf32008-10-27 06:06:24 +08003980 ret = ocfs2_divide_xattr_cluster(inode, handle,
3981 last_blk, new_blk,
3982 v_start);
Tao Ma01225592008-08-18 17:38:53 +08003983
3984 if ((*header_bh)->b_blocknr == last_blk && extend)
3985 *extend = 0;
3986 }
3987 }
3988
3989 return ret;
3990}
3991
3992/*
3993 * Add a new cluster for xattr storage.
3994 *
3995 * If the new cluster is contiguous with the previous one, it will be
3996 * appended to the same extent record, and num_clusters will be updated.
3997 * If not, we will insert a new extent for it and move some xattrs in
3998 * the last cluster into the new allocated one.
3999 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4000 * lose the benefits of hashing because we'll have to search large leaves.
4001 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4002 * if it's bigger).
4003 *
4004 * first_bh is the first block of the previous extent rec and header_bh
4005 * indicates the bucket we will insert the new xattrs. They will be updated
4006 * when the header_bh is moved into the new cluster.
4007 */
4008static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4009 struct buffer_head *root_bh,
4010 struct buffer_head **first_bh,
4011 struct buffer_head **header_bh,
4012 u32 *num_clusters,
4013 u32 prev_cpos,
4014 u64 prev_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08004015 int *extend,
4016 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004017{
Tao Ma85db90e2008-11-12 08:27:01 +08004018 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004019 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4020 u32 prev_clusters = *num_clusters;
4021 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4022 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004023 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004024 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004025 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004026
4027 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4028 "previous xattr blkno = %llu\n",
4029 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehde29c082008-10-29 14:45:30 -07004030 prev_cpos, (unsigned long long)prev_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004031
Joel Becker8d6220d2008-08-22 12:46:09 -07004032 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004033
Tao Ma01225592008-08-18 17:38:53 +08004034 ret = ocfs2_journal_access(handle, inode, root_bh,
4035 OCFS2_JOURNAL_ACCESS_WRITE);
4036 if (ret < 0) {
4037 mlog_errno(ret);
4038 goto leave;
4039 }
4040
Tao Ma78f30c32008-11-12 08:27:00 +08004041 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004042 clusters_to_add, &bit_off, &num_bits);
4043 if (ret < 0) {
4044 if (ret != -ENOSPC)
4045 mlog_errno(ret);
4046 goto leave;
4047 }
4048
4049 BUG_ON(num_bits > clusters_to_add);
4050
4051 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4052 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4053 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4054
4055 if (prev_blkno + prev_clusters * bpc == block &&
4056 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4057 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4058 /*
4059 * If this cluster is contiguous with the old one and
4060 * adding this new cluster, we don't surpass the limit of
4061 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4062 * initialized and used like other buckets in the previous
4063 * cluster.
4064 * So add it as a contiguous one. The caller will handle
4065 * its init process.
4066 */
4067 v_start = prev_cpos + prev_clusters;
4068 *num_clusters = prev_clusters + num_bits;
4069 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4070 num_bits);
4071 } else {
4072 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4073 handle,
4074 first_bh,
4075 header_bh,
4076 block,
4077 prev_blkno,
4078 prev_clusters,
4079 &v_start,
4080 extend);
4081 if (ret) {
4082 mlog_errno(ret);
4083 goto leave;
4084 }
4085 }
4086
4087 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004088 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004089 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004090 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004091 if (ret < 0) {
4092 mlog_errno(ret);
4093 goto leave;
4094 }
4095
4096 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004097 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004098 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004099
4100leave:
Tao Ma01225592008-08-18 17:38:53 +08004101 return ret;
4102}
4103
4104/*
4105 * Extend a new xattr bucket and move xattrs to the end one by one until
4106 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
4107 */
4108static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004109 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004110 struct buffer_head *first_bh,
4111 struct buffer_head *start_bh,
4112 u32 num_clusters)
4113{
4114 int ret, credits;
4115 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4116 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4117 u64 start_blk = start_bh->b_blocknr, end_blk;
4118 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
Tao Ma01225592008-08-18 17:38:53 +08004119 struct ocfs2_xattr_header *first_xh =
4120 (struct ocfs2_xattr_header *)first_bh->b_data;
4121 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
4122
4123 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Mark Fashehde29c082008-10-29 14:45:30 -07004124 "from %llu, len = %u\n", (unsigned long long)start_blk,
Tao Ma01225592008-08-18 17:38:53 +08004125 (unsigned long long)first_bh->b_blocknr, num_clusters);
4126
4127 BUG_ON(bucket >= num_buckets);
4128
4129 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
4130
4131 /*
4132 * We will touch all the buckets after the start_bh(include it).
Joel Becker1224be02008-10-24 18:47:33 -07004133 * Then we add one more bucket.
Tao Ma01225592008-08-18 17:38:53 +08004134 */
Tao Ma85db90e2008-11-12 08:27:01 +08004135 credits = end_blk - start_blk + 3 * blk_per_bucket + 1 +
4136 handle->h_buffer_credits;
4137 ret = ocfs2_extend_trans(handle, credits);
4138 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004139 mlog_errno(ret);
4140 goto out;
4141 }
4142
4143 ret = ocfs2_journal_access(handle, inode, first_bh,
4144 OCFS2_JOURNAL_ACCESS_WRITE);
4145 if (ret) {
4146 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004147 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004148 }
4149
4150 while (end_blk != start_blk) {
4151 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4152 end_blk + blk_per_bucket, 0);
4153 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004154 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004155 end_blk -= blk_per_bucket;
4156 }
4157
4158 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004159 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
4160 start_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004161
4162 le16_add_cpu(&first_xh->xh_num_buckets, 1);
4163 ocfs2_journal_dirty(handle, first_bh);
4164
Tao Ma01225592008-08-18 17:38:53 +08004165out:
4166 return ret;
4167}
4168
4169/*
4170 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4171 * xb_bh is the ocfs2_xattr_block.
4172 * We will move all the buckets starting from header_bh to the next place. As
4173 * for this one, half num of its xattrs will be moved to the next one.
4174 *
4175 * We will allocate a new cluster if current cluster is full and adjust
4176 * header_bh and first_bh if the insert place is moved to the new cluster.
4177 */
4178static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4179 struct buffer_head *xb_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08004180 struct buffer_head *header_bh,
4181 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004182{
4183 struct ocfs2_xattr_header *first_xh = NULL;
4184 struct buffer_head *first_bh = NULL;
4185 struct ocfs2_xattr_block *xb =
4186 (struct ocfs2_xattr_block *)xb_bh->b_data;
4187 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4188 struct ocfs2_extent_list *el = &xb_root->xt_list;
4189 struct ocfs2_xattr_header *xh =
4190 (struct ocfs2_xattr_header *)header_bh->b_data;
4191 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4192 struct super_block *sb = inode->i_sb;
4193 struct ocfs2_super *osb = OCFS2_SB(sb);
4194 int ret, num_buckets, extend = 1;
4195 u64 p_blkno;
4196 u32 e_cpos, num_clusters;
4197
4198 mlog(0, "Add new xattr bucket starting form %llu\n",
4199 (unsigned long long)header_bh->b_blocknr);
4200
4201 /*
4202 * Add refrence for header_bh here because it may be
4203 * changed in ocfs2_add_new_xattr_cluster and we need
4204 * to free it in the end.
4205 */
4206 get_bh(header_bh);
4207
4208 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4209 &num_clusters, el);
4210 if (ret) {
4211 mlog_errno(ret);
4212 goto out;
4213 }
4214
Joel Becker0fcaa562008-10-09 17:20:31 -07004215 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
Tao Ma01225592008-08-18 17:38:53 +08004216 if (ret) {
4217 mlog_errno(ret);
4218 goto out;
4219 }
4220
4221 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4222 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4223
4224 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4225 ret = ocfs2_add_new_xattr_cluster(inode,
4226 xb_bh,
4227 &first_bh,
4228 &header_bh,
4229 &num_clusters,
4230 e_cpos,
4231 p_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08004232 &extend,
4233 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004234 if (ret) {
4235 mlog_errno(ret);
4236 goto out;
4237 }
4238 }
4239
4240 if (extend)
4241 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004242 ctxt->handle,
Tao Ma01225592008-08-18 17:38:53 +08004243 first_bh,
4244 header_bh,
4245 num_clusters);
4246 if (ret)
4247 mlog_errno(ret);
4248out:
4249 brelse(first_bh);
4250 brelse(header_bh);
4251 return ret;
4252}
4253
4254static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4255 struct ocfs2_xattr_bucket *bucket,
4256 int offs)
4257{
4258 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4259
4260 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004261 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004262}
4263
4264/*
4265 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004266 *
4267 * Note: "local" indicates the real data's locality. So we can't
4268 * just its bucket locality by its length.
4269 */
4270static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4271 struct ocfs2_xattr_info *xi,
4272 struct ocfs2_xattr_search *xs,
4273 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004274 int local)
Tao Ma01225592008-08-18 17:38:53 +08004275{
4276 struct ocfs2_xattr_entry *last, *xe;
4277 int name_len = strlen(xi->name);
4278 struct ocfs2_xattr_header *xh = xs->header;
4279 u16 count = le16_to_cpu(xh->xh_count), start;
4280 size_t blocksize = inode->i_sb->s_blocksize;
4281 char *val;
4282 size_t offs, size, new_size;
4283
4284 last = &xh->xh_entries[count];
4285 if (!xs->not_found) {
4286 xe = xs->here;
4287 offs = le16_to_cpu(xe->xe_name_offset);
4288 if (ocfs2_xattr_is_local(xe))
4289 size = OCFS2_XATTR_SIZE(name_len) +
4290 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4291 else
4292 size = OCFS2_XATTR_SIZE(name_len) +
4293 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4294
4295 /*
4296 * If the new value will be stored outside, xi->value has been
4297 * initalized as an empty ocfs2_xattr_value_root, and the same
4298 * goes with xi->value_len, so we can set new_size safely here.
4299 * See ocfs2_xattr_set_in_bucket.
4300 */
4301 new_size = OCFS2_XATTR_SIZE(name_len) +
4302 OCFS2_XATTR_SIZE(xi->value_len);
4303
4304 le16_add_cpu(&xh->xh_name_value_len, -size);
4305 if (xi->value) {
4306 if (new_size > size)
4307 goto set_new_name_value;
4308
4309 /* Now replace the old value with new one. */
4310 if (local)
4311 xe->xe_value_size = cpu_to_le64(xi->value_len);
4312 else
4313 xe->xe_value_size = 0;
4314
4315 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004316 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004317 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4318 size - OCFS2_XATTR_SIZE(name_len));
4319 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4320 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4321 xi->value, xi->value_len);
4322
4323 le16_add_cpu(&xh->xh_name_value_len, new_size);
4324 ocfs2_xattr_set_local(xe, local);
4325 return;
4326 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004327 /*
4328 * Remove the old entry if there is more than one.
4329 * We don't remove the last entry so that we can
4330 * use it to indicate the hash value of the empty
4331 * bucket.
4332 */
Tao Ma01225592008-08-18 17:38:53 +08004333 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004334 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004335 if (xh->xh_count) {
4336 memmove(xe, xe + 1,
4337 (void *)last - (void *)xe);
4338 memset(last, 0,
4339 sizeof(struct ocfs2_xattr_entry));
4340 } else
4341 xh->xh_free_start =
4342 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4343
Tao Ma01225592008-08-18 17:38:53 +08004344 return;
4345 }
4346 } else {
4347 /* find a new entry for insert. */
4348 int low = 0, high = count - 1, tmp;
4349 struct ocfs2_xattr_entry *tmp_xe;
4350
Tao Ma5a095612008-09-19 22:17:41 +08004351 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004352 tmp = (low + high) / 2;
4353 tmp_xe = &xh->xh_entries[tmp];
4354
4355 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4356 low = tmp + 1;
4357 else if (name_hash <
4358 le32_to_cpu(tmp_xe->xe_name_hash))
4359 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004360 else {
4361 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004362 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004363 }
Tao Ma01225592008-08-18 17:38:53 +08004364 }
4365
4366 xe = &xh->xh_entries[low];
4367 if (low != count)
4368 memmove(xe + 1, xe, (void *)last - (void *)xe);
4369
4370 le16_add_cpu(&xh->xh_count, 1);
4371 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4372 xe->xe_name_hash = cpu_to_le32(name_hash);
4373 xe->xe_name_len = name_len;
4374 ocfs2_xattr_set_type(xe, xi->name_index);
4375 }
4376
4377set_new_name_value:
4378 /* Insert the new name+value. */
4379 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4380
4381 /*
4382 * We must make sure that the name/value pair
4383 * exists in the same block.
4384 */
4385 offs = le16_to_cpu(xh->xh_free_start);
4386 start = offs - size;
4387
4388 if (start >> inode->i_sb->s_blocksize_bits !=
4389 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4390 offs = offs - offs % blocksize;
4391 xh->xh_free_start = cpu_to_le16(offs);
4392 }
4393
Joel Beckerba937122008-10-24 19:13:20 -07004394 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004395 xe->xe_name_offset = cpu_to_le16(offs - size);
4396
4397 memset(val, 0, size);
4398 memcpy(val, xi->name, name_len);
4399 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4400
4401 xe->xe_value_size = cpu_to_le64(xi->value_len);
4402 ocfs2_xattr_set_local(xe, local);
4403 xs->here = xe;
4404 le16_add_cpu(&xh->xh_free_start, -size);
4405 le16_add_cpu(&xh->xh_name_value_len, size);
4406
4407 return;
4408}
4409
Tao Ma01225592008-08-18 17:38:53 +08004410/*
4411 * Set the xattr entry in the specified bucket.
4412 * The bucket is indicated by xs->bucket and it should have the enough
4413 * space for the xattr insertion.
4414 */
4415static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004416 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004417 struct ocfs2_xattr_info *xi,
4418 struct ocfs2_xattr_search *xs,
4419 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004420 int local)
Tao Ma01225592008-08-18 17:38:53 +08004421{
Joel Becker1224be02008-10-24 18:47:33 -07004422 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004423 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004424
Mark Fashehff1ec202008-08-19 10:54:29 -07004425 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4426 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004427 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004428
Joel Beckerba937122008-10-24 19:13:20 -07004429 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004430 blkno = bucket_blkno(xs->bucket);
4431 ocfs2_xattr_bucket_relse(xs->bucket);
4432 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004433 if (ret) {
4434 mlog_errno(ret);
4435 goto out;
4436 }
4437 }
4438
Joel Beckerba937122008-10-24 19:13:20 -07004439 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004440 OCFS2_JOURNAL_ACCESS_WRITE);
4441 if (ret < 0) {
4442 mlog_errno(ret);
4443 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004444 }
4445
Tao Ma5a095612008-09-19 22:17:41 +08004446 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004447 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004448
Tao Ma01225592008-08-18 17:38:53 +08004449out:
Tao Ma01225592008-08-18 17:38:53 +08004450 return ret;
4451}
4452
4453static int ocfs2_xattr_value_update_size(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004454 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004455 struct buffer_head *xe_bh,
4456 struct ocfs2_xattr_entry *xe,
4457 u64 new_size)
4458{
4459 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004460
4461 ret = ocfs2_journal_access(handle, inode, xe_bh,
4462 OCFS2_JOURNAL_ACCESS_WRITE);
4463 if (ret < 0) {
4464 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004465 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004466 }
4467
4468 xe->xe_value_size = cpu_to_le64(new_size);
4469
4470 ret = ocfs2_journal_dirty(handle, xe_bh);
4471 if (ret < 0)
4472 mlog_errno(ret);
4473
Tao Ma01225592008-08-18 17:38:53 +08004474out:
4475 return ret;
4476}
4477
4478/*
4479 * Truncate the specified xe_off entry in xattr bucket.
4480 * bucket is indicated by header_bh and len is the new length.
4481 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4482 *
4483 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4484 */
4485static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4486 struct buffer_head *header_bh,
4487 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004488 int len,
4489 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004490{
4491 int ret, offset;
4492 u64 value_blk;
4493 struct buffer_head *value_bh = NULL;
4494 struct ocfs2_xattr_value_root *xv;
4495 struct ocfs2_xattr_entry *xe;
4496 struct ocfs2_xattr_header *xh =
4497 (struct ocfs2_xattr_header *)header_bh->b_data;
4498 size_t blocksize = inode->i_sb->s_blocksize;
4499
4500 xe = &xh->xh_entries[xe_off];
4501
4502 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4503
4504 offset = le16_to_cpu(xe->xe_name_offset) +
4505 OCFS2_XATTR_SIZE(xe->xe_name_len);
4506
4507 value_blk = offset / blocksize;
4508
4509 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4510 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4511 value_blk += header_bh->b_blocknr;
4512
Joel Becker0fcaa562008-10-09 17:20:31 -07004513 ret = ocfs2_read_block(inode, value_blk, &value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004514 if (ret) {
4515 mlog_errno(ret);
4516 goto out;
4517 }
4518
4519 xv = (struct ocfs2_xattr_value_root *)
4520 (value_bh->b_data + offset % blocksize);
4521
4522 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4523 xe_off, (unsigned long long)header_bh->b_blocknr, len);
Tao Ma78f30c32008-11-12 08:27:00 +08004524 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004525 if (ret) {
4526 mlog_errno(ret);
4527 goto out;
4528 }
4529
Tao Ma85db90e2008-11-12 08:27:01 +08004530 ret = ocfs2_xattr_value_update_size(inode, ctxt->handle,
4531 header_bh, xe, len);
Tao Ma01225592008-08-18 17:38:53 +08004532 if (ret) {
4533 mlog_errno(ret);
4534 goto out;
4535 }
4536
4537out:
4538 brelse(value_bh);
4539 return ret;
4540}
4541
4542static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004543 struct ocfs2_xattr_search *xs,
4544 int len,
4545 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004546{
4547 int ret, offset;
4548 struct ocfs2_xattr_entry *xe = xs->here;
4549 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4550
Joel Beckerba937122008-10-24 19:13:20 -07004551 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004552
4553 offset = xe - xh->xh_entries;
Joel Beckerba937122008-10-24 19:13:20 -07004554 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
Tao Ma78f30c32008-11-12 08:27:00 +08004555 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004556 if (ret)
4557 mlog_errno(ret);
4558
4559 return ret;
4560}
4561
4562static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004563 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004564 struct ocfs2_xattr_search *xs,
4565 char *val,
4566 int value_len)
4567{
4568 int offset;
4569 struct ocfs2_xattr_value_root *xv;
4570 struct ocfs2_xattr_entry *xe = xs->here;
4571
4572 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4573
4574 offset = le16_to_cpu(xe->xe_name_offset) +
4575 OCFS2_XATTR_SIZE(xe->xe_name_len);
4576
4577 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4578
Tao Ma85db90e2008-11-12 08:27:01 +08004579 return __ocfs2_xattr_set_value_outside(inode, handle,
4580 xv, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004581}
4582
Tao Ma01225592008-08-18 17:38:53 +08004583static int ocfs2_rm_xattr_cluster(struct inode *inode,
4584 struct buffer_head *root_bh,
4585 u64 blkno,
4586 u32 cpos,
4587 u32 len)
4588{
4589 int ret;
4590 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4591 struct inode *tl_inode = osb->osb_tl_inode;
4592 handle_t *handle;
4593 struct ocfs2_xattr_block *xb =
4594 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004595 struct ocfs2_alloc_context *meta_ac = NULL;
4596 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004597 struct ocfs2_extent_tree et;
4598
Joel Becker8d6220d2008-08-22 12:46:09 -07004599 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004600
4601 ocfs2_init_dealloc_ctxt(&dealloc);
4602
4603 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4604 cpos, len, (unsigned long long)blkno);
4605
4606 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4607
Joel Beckerf99b9b72008-08-20 19:36:33 -07004608 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004609 if (ret) {
4610 mlog_errno(ret);
4611 return ret;
4612 }
4613
4614 mutex_lock(&tl_inode->i_mutex);
4615
4616 if (ocfs2_truncate_log_needs_flush(osb)) {
4617 ret = __ocfs2_flush_truncate_log(osb);
4618 if (ret < 0) {
4619 mlog_errno(ret);
4620 goto out;
4621 }
4622 }
4623
4624 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
Tao Mad3264792008-10-24 07:57:28 +08004625 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004626 ret = -ENOMEM;
4627 mlog_errno(ret);
4628 goto out;
4629 }
4630
4631 ret = ocfs2_journal_access(handle, inode, root_bh,
4632 OCFS2_JOURNAL_ACCESS_WRITE);
4633 if (ret) {
4634 mlog_errno(ret);
4635 goto out_commit;
4636 }
4637
Joel Beckerf99b9b72008-08-20 19:36:33 -07004638 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4639 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004640 if (ret) {
4641 mlog_errno(ret);
4642 goto out_commit;
4643 }
4644
4645 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4646
4647 ret = ocfs2_journal_dirty(handle, root_bh);
4648 if (ret) {
4649 mlog_errno(ret);
4650 goto out_commit;
4651 }
4652
4653 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4654 if (ret)
4655 mlog_errno(ret);
4656
4657out_commit:
4658 ocfs2_commit_trans(osb, handle);
4659out:
4660 ocfs2_schedule_truncate_log_flush(osb, 1);
4661
4662 mutex_unlock(&tl_inode->i_mutex);
4663
4664 if (meta_ac)
4665 ocfs2_free_alloc_context(meta_ac);
4666
4667 ocfs2_run_deallocs(osb, &dealloc);
4668
4669 return ret;
4670}
4671
Tao Ma01225592008-08-18 17:38:53 +08004672static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004673 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004674 struct ocfs2_xattr_search *xs)
4675{
Joel Beckerba937122008-10-24 19:13:20 -07004676 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004677 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4678 le16_to_cpu(xh->xh_count) - 1];
4679 int ret = 0;
4680
Joel Beckerba937122008-10-24 19:13:20 -07004681 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004682 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004683 if (ret) {
4684 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004685 return;
Tao Ma01225592008-08-18 17:38:53 +08004686 }
4687
4688 /* Remove the old entry. */
4689 memmove(xs->here, xs->here + 1,
4690 (void *)last - (void *)xs->here);
4691 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4692 le16_add_cpu(&xh->xh_count, -1);
4693
Joel Beckerba937122008-10-24 19:13:20 -07004694 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004695}
4696
4697/*
4698 * Set the xattr name/value in the bucket specified in xs.
4699 *
4700 * As the new value in xi may be stored in the bucket or in an outside cluster,
4701 * we divide the whole process into 3 steps:
4702 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4703 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4704 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4705 * 4. If the clusters for the new outside value can't be allocated, we need
4706 * to free the xattr we allocated in set.
4707 */
4708static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4709 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004710 struct ocfs2_xattr_search *xs,
4711 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004712{
Tao Ma5a095612008-09-19 22:17:41 +08004713 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004714 size_t value_len;
4715 char *val = (char *)xi->value;
4716 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004717 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4718 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004719
4720 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4721 /*
4722 * We need to truncate the xattr storage first.
4723 *
4724 * If both the old and new value are stored to
4725 * outside block, we only need to truncate
4726 * the storage and then set the value outside.
4727 *
4728 * If the new value should be stored within block,
4729 * we should free all the outside block first and
4730 * the modification to the xattr block will be done
4731 * by following steps.
4732 */
4733 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4734 value_len = xi->value_len;
4735 else
4736 value_len = 0;
4737
4738 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004739 value_len,
4740 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004741 if (ret)
4742 goto out;
4743
4744 if (value_len)
4745 goto set_value_outside;
4746 }
4747
4748 value_len = xi->value_len;
4749 /* So we have to handle the inside block change now. */
4750 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4751 /*
4752 * If the new value will be stored outside of block,
4753 * initalize a new empty value root and insert it first.
4754 */
4755 local = 0;
4756 xi->value = &def_xv;
4757 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4758 }
4759
Tao Ma85db90e2008-11-12 08:27:01 +08004760 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4761 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004762 if (ret) {
4763 mlog_errno(ret);
4764 goto out;
4765 }
4766
Tao Ma5a095612008-09-19 22:17:41 +08004767 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4768 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004769
Tao Ma5a095612008-09-19 22:17:41 +08004770 /* allocate the space now for the outside block storage. */
4771 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004772 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08004773 if (ret) {
4774 mlog_errno(ret);
4775
4776 if (xs->not_found) {
4777 /*
4778 * We can't allocate enough clusters for outside
4779 * storage and we have allocated xattr already,
4780 * so need to remove it.
4781 */
Tao Ma85db90e2008-11-12 08:27:01 +08004782 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08004783 }
Tao Ma01225592008-08-18 17:38:53 +08004784 goto out;
4785 }
4786
4787set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08004788 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4789 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004790out:
4791 return ret;
4792}
4793
Tao Ma80bcaf32008-10-27 06:06:24 +08004794/*
4795 * check whether the xattr bucket is filled up with the same hash value.
4796 * If we want to insert the xattr with the same hash, return -ENOSPC.
4797 * If we want to insert a xattr with different hash value, go ahead
4798 * and ocfs2_divide_xattr_bucket will handle this.
4799 */
Tao Ma01225592008-08-18 17:38:53 +08004800static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004801 struct ocfs2_xattr_bucket *bucket,
4802 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004803{
Joel Becker3e632942008-10-24 17:04:49 -07004804 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004805 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4806
4807 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4808 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004809
4810 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4811 xh->xh_entries[0].xe_name_hash) {
4812 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4813 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004814 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004815 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4816 return -ENOSPC;
4817 }
4818
4819 return 0;
4820}
4821
4822static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4823 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004824 struct ocfs2_xattr_search *xs,
4825 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004826{
4827 struct ocfs2_xattr_header *xh;
4828 struct ocfs2_xattr_entry *xe;
4829 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07004830 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08004831 size_t value_size = 0, name_len = strlen(xi->name);
4832 size_t blocksize = inode->i_sb->s_blocksize;
4833 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08004834
4835 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4836
4837try_again:
4838 xh = xs->header;
4839 count = le16_to_cpu(xh->xh_count);
4840 xh_free_start = le16_to_cpu(xh->xh_free_start);
4841 header_size = sizeof(struct ocfs2_xattr_header) +
4842 count * sizeof(struct ocfs2_xattr_entry);
4843 max_free = OCFS2_XATTR_BUCKET_SIZE -
4844 le16_to_cpu(xh->xh_name_value_len) - header_size;
4845
4846 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4847 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07004848 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004849 header_size);
4850
4851 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4852 value_size = OCFS2_XATTR_ROOT_SIZE;
4853 else if (xi->value)
4854 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4855
4856 if (xs->not_found)
4857 need = sizeof(struct ocfs2_xattr_entry) +
4858 OCFS2_XATTR_SIZE(name_len) + value_size;
4859 else {
4860 need = value_size + OCFS2_XATTR_SIZE(name_len);
4861
4862 /*
4863 * We only replace the old value if the new length is smaller
4864 * than the old one. Otherwise we will allocate new space in the
4865 * bucket to store it.
4866 */
4867 xe = xs->here;
4868 if (ocfs2_xattr_is_local(xe))
4869 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4870 else
4871 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4872
4873 if (old >= value_size)
4874 need = 0;
4875 }
4876
4877 free = xh_free_start - header_size;
4878 /*
4879 * We need to make sure the new name/value pair
4880 * can exist in the same block.
4881 */
4882 if (xh_free_start % blocksize < need)
4883 free -= xh_free_start % blocksize;
4884
4885 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4886 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4887 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07004888 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004889 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4890 le16_to_cpu(xh->xh_name_value_len));
4891
Tao Ma976331d2008-11-12 08:26:57 +08004892 if (free < need ||
4893 (xs->not_found &&
4894 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08004895 if (need <= max_free &&
4896 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4897 /*
4898 * We can create the space by defragment. Since only the
4899 * name/value will be moved, the xe shouldn't be changed
4900 * in xs.
4901 */
Tao Ma85db90e2008-11-12 08:27:01 +08004902 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
4903 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004904 if (ret) {
4905 mlog_errno(ret);
4906 goto out;
4907 }
4908
4909 xh_free_start = le16_to_cpu(xh->xh_free_start);
4910 free = xh_free_start - header_size;
4911 if (xh_free_start % blocksize < need)
4912 free -= xh_free_start % blocksize;
4913
4914 if (free >= need)
4915 goto xattr_set;
4916
4917 mlog(0, "Can't get enough space for xattr insert by "
4918 "defragment. Need %u bytes, but we have %d, so "
4919 "allocate new bucket for it.\n", need, free);
4920 }
4921
4922 /*
4923 * We have to add new buckets or clusters and one
4924 * allocation should leave us enough space for insert.
4925 */
4926 BUG_ON(allocation);
4927
4928 /*
4929 * We do not allow for overlapping ranges between buckets. And
4930 * the maximum number of collisions we will allow for then is
4931 * one bucket's worth, so check it here whether we need to
4932 * add a new bucket for the insert.
4933 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004934 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004935 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08004936 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08004937 if (ret) {
4938 mlog_errno(ret);
4939 goto out;
4940 }
4941
4942 ret = ocfs2_add_new_xattr_bucket(inode,
4943 xs->xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08004944 xs->bucket->bu_bhs[0],
4945 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004946 if (ret) {
4947 mlog_errno(ret);
4948 goto out;
4949 }
4950
Joel Beckerba937122008-10-24 19:13:20 -07004951 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004952
4953 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4954 xi->name_index,
4955 xi->name, xs);
4956 if (ret && ret != -ENODATA)
4957 goto out;
4958 xs->not_found = ret;
4959 allocation = 1;
4960 goto try_again;
4961 }
4962
4963xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08004964 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004965out:
4966 mlog_exit(ret);
4967 return ret;
4968}
Tao Maa3944252008-08-18 17:38:54 +08004969
4970static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4971 struct ocfs2_xattr_bucket *bucket,
4972 void *para)
4973{
4974 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07004975 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08004976 u16 i;
4977 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08004978 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4979 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
4980
4981 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08004982
Tao Ma85db90e2008-11-12 08:27:01 +08004983 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4984 if (IS_ERR(ctxt.handle)) {
4985 ret = PTR_ERR(ctxt.handle);
4986 mlog_errno(ret);
4987 goto out;
4988 }
4989
Tao Maa3944252008-08-18 17:38:54 +08004990 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4991 xe = &xh->xh_entries[i];
4992 if (ocfs2_xattr_is_local(xe))
4993 continue;
4994
4995 ret = ocfs2_xattr_bucket_value_truncate(inode,
Joel Becker4ac60322008-10-18 19:11:42 -07004996 bucket->bu_bhs[0],
Tao Ma78f30c32008-11-12 08:27:00 +08004997 i, 0, &ctxt);
Tao Maa3944252008-08-18 17:38:54 +08004998 if (ret) {
4999 mlog_errno(ret);
5000 break;
5001 }
5002 }
5003
Tao Ma85db90e2008-11-12 08:27:01 +08005004 ret = ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08005005 ocfs2_schedule_truncate_log_flush(osb, 1);
5006 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08005007out:
Tao Maa3944252008-08-18 17:38:54 +08005008 return ret;
5009}
5010
5011static int ocfs2_delete_xattr_index_block(struct inode *inode,
5012 struct buffer_head *xb_bh)
5013{
5014 struct ocfs2_xattr_block *xb =
5015 (struct ocfs2_xattr_block *)xb_bh->b_data;
5016 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5017 int ret = 0;
5018 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5019 u64 p_blkno;
5020
5021 if (le16_to_cpu(el->l_next_free_rec) == 0)
5022 return 0;
5023
5024 while (name_hash > 0) {
5025 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5026 &e_cpos, &num_clusters, el);
5027 if (ret) {
5028 mlog_errno(ret);
5029 goto out;
5030 }
5031
5032 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5033 ocfs2_delete_xattr_in_bucket,
5034 NULL);
5035 if (ret) {
5036 mlog_errno(ret);
5037 goto out;
5038 }
5039
5040 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5041 p_blkno, e_cpos, num_clusters);
5042 if (ret) {
5043 mlog_errno(ret);
5044 break;
5045 }
5046
5047 if (e_cpos == 0)
5048 break;
5049
5050 name_hash = e_cpos - 1;
5051 }
5052
5053out:
5054 return ret;
5055}
Mark Fasheh99219ae2008-10-07 14:52:59 -07005056
5057/*
Tiger Yang923f7f32008-11-14 11:16:27 +08005058 * 'security' attributes support
5059 */
5060static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5061 size_t list_size, const char *name,
5062 size_t name_len)
5063{
5064 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5065 const size_t total_len = prefix_len + name_len + 1;
5066
5067 if (list && total_len <= list_size) {
5068 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5069 memcpy(list + prefix_len, name, name_len);
5070 list[prefix_len + name_len] = '\0';
5071 }
5072 return total_len;
5073}
5074
5075static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5076 void *buffer, size_t size)
5077{
5078 if (strcmp(name, "") == 0)
5079 return -EINVAL;
5080 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5081 buffer, size);
5082}
5083
5084static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5085 const void *value, size_t size, int flags)
5086{
5087 if (strcmp(name, "") == 0)
5088 return -EINVAL;
5089
5090 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5091 size, flags);
5092}
5093
Tiger Yang534eadd2008-11-14 11:16:41 +08005094int ocfs2_init_security_get(struct inode *inode,
5095 struct inode *dir,
5096 struct ocfs2_security_xattr_info *si)
5097{
5098 return security_inode_init_security(inode, dir, &si->name, &si->value,
5099 &si->value_len);
5100}
5101
5102int ocfs2_init_security_set(handle_t *handle,
5103 struct inode *inode,
5104 struct buffer_head *di_bh,
5105 struct ocfs2_security_xattr_info *si,
5106 struct ocfs2_alloc_context *xattr_ac,
5107 struct ocfs2_alloc_context *data_ac)
5108{
5109 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5110 OCFS2_XATTR_INDEX_SECURITY,
5111 si->name, si->value, si->value_len, 0,
5112 xattr_ac, data_ac);
5113}
5114
Tiger Yang923f7f32008-11-14 11:16:27 +08005115struct xattr_handler ocfs2_xattr_security_handler = {
5116 .prefix = XATTR_SECURITY_PREFIX,
5117 .list = ocfs2_xattr_security_list,
5118 .get = ocfs2_xattr_security_get,
5119 .set = ocfs2_xattr_security_set,
5120};
5121
5122/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07005123 * 'trusted' attributes support
5124 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005125static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5126 size_t list_size, const char *name,
5127 size_t name_len)
5128{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005129 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005130 const size_t total_len = prefix_len + name_len + 1;
5131
5132 if (list && total_len <= list_size) {
5133 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5134 memcpy(list + prefix_len, name, name_len);
5135 list[prefix_len + name_len] = '\0';
5136 }
5137 return total_len;
5138}
5139
5140static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5141 void *buffer, size_t size)
5142{
5143 if (strcmp(name, "") == 0)
5144 return -EINVAL;
5145 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5146 buffer, size);
5147}
5148
5149static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5150 const void *value, size_t size, int flags)
5151{
5152 if (strcmp(name, "") == 0)
5153 return -EINVAL;
5154
5155 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5156 size, flags);
5157}
5158
5159struct xattr_handler ocfs2_xattr_trusted_handler = {
5160 .prefix = XATTR_TRUSTED_PREFIX,
5161 .list = ocfs2_xattr_trusted_list,
5162 .get = ocfs2_xattr_trusted_get,
5163 .set = ocfs2_xattr_trusted_set,
5164};
5165
Mark Fasheh99219ae2008-10-07 14:52:59 -07005166/*
5167 * 'user' attributes support
5168 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005169static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5170 size_t list_size, const char *name,
5171 size_t name_len)
5172{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005173 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005174 const size_t total_len = prefix_len + name_len + 1;
5175 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5176
5177 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5178 return 0;
5179
5180 if (list && total_len <= list_size) {
5181 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5182 memcpy(list + prefix_len, name, name_len);
5183 list[prefix_len + name_len] = '\0';
5184 }
5185 return total_len;
5186}
5187
5188static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5189 void *buffer, size_t size)
5190{
5191 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5192
5193 if (strcmp(name, "") == 0)
5194 return -EINVAL;
5195 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5196 return -EOPNOTSUPP;
5197 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5198 buffer, size);
5199}
5200
5201static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5202 const void *value, size_t size, int flags)
5203{
5204 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5205
5206 if (strcmp(name, "") == 0)
5207 return -EINVAL;
5208 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5209 return -EOPNOTSUPP;
5210
5211 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5212 size, flags);
5213}
5214
5215struct xattr_handler ocfs2_xattr_user_handler = {
5216 .prefix = XATTR_USER_PREFIX,
5217 .list = ocfs2_xattr_user_list,
5218 .get = ocfs2_xattr_user_get,
5219 .set = ocfs2_xattr_user_set,
5220};