blob: 6480254fe396b1a274438f96cb796e7b499aaeea [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 Yangcf1d6c72008-08-18 17:11:00 +080038
Tao Maf56654c2008-08-18 17:38:48 +080039#define MLOG_MASK_PREFIX ML_XATTR
40#include <cluster/masklog.h>
41
42#include "ocfs2.h"
43#include "alloc.h"
44#include "dlmglue.h"
45#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080046#include "symlink.h"
47#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080048#include "inode.h"
49#include "journal.h"
50#include "ocfs2_fs.h"
51#include "suballoc.h"
52#include "uptodate.h"
53#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080054#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080055#include "xattr.h"
56
57
58struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
61};
62
Tao Ma0c044f02008-08-18 17:38:50 +080063struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070064 /* The inode these xattrs are associated with */
65 struct inode *bu_inode;
66
67 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070068 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070069
70 /* How many blocks make up one bucket for this filesystem */
71 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080072};
73
Tao Ma78f30c32008-11-12 08:27:00 +080074struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080075 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080076 struct ocfs2_alloc_context *meta_ac;
77 struct ocfs2_alloc_context *data_ac;
78 struct ocfs2_cached_dealloc_ctxt dealloc;
79};
80
Tiger Yangcf1d6c72008-08-18 17:11:00 +080081#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
82#define OCFS2_XATTR_INLINE_SIZE 80
83
84static struct ocfs2_xattr_def_value_root def_xv = {
85 .xv.xr_list.l_count = cpu_to_le16(1),
86};
87
88struct xattr_handler *ocfs2_xattr_handlers[] = {
89 &ocfs2_xattr_user_handler,
90 &ocfs2_xattr_trusted_handler,
91 NULL
92};
93
Tiger Yangc988fd02008-10-23 16:34:44 +080094static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +080095 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
96 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
97};
98
99struct ocfs2_xattr_info {
100 int name_index;
101 const char *name;
102 const void *value;
103 size_t value_len;
104};
105
106struct ocfs2_xattr_search {
107 struct buffer_head *inode_bh;
108 /*
109 * xattr_bh point to the block buffer head which has extended attribute
110 * when extended attribute in inode, xattr_bh is equal to inode_bh.
111 */
112 struct buffer_head *xattr_bh;
113 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700114 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800115 void *base;
116 void *end;
117 struct ocfs2_xattr_entry *here;
118 int not_found;
119};
120
Tao Ma589dc262008-08-18 17:38:51 +0800121static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
122 struct ocfs2_xattr_header *xh,
123 int index,
124 int *block_off,
125 int *new_offset);
126
Joel Becker54f443f2008-10-20 18:43:07 -0700127static int ocfs2_xattr_block_find(struct inode *inode,
128 int name_index,
129 const char *name,
130 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800131static int ocfs2_xattr_index_block_find(struct inode *inode,
132 struct buffer_head *root_bh,
133 int name_index,
134 const char *name,
135 struct ocfs2_xattr_search *xs);
136
Tao Ma0c044f02008-08-18 17:38:50 +0800137static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
138 struct ocfs2_xattr_tree_root *xt,
139 char *buffer,
140 size_t buffer_size);
141
Tao Ma01225592008-08-18 17:38:53 +0800142static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800143 struct ocfs2_xattr_search *xs,
144 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800145
146static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
147 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800148 struct ocfs2_xattr_search *xs,
149 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800150
Tao Maa3944252008-08-18 17:38:54 +0800151static int ocfs2_delete_xattr_index_block(struct inode *inode,
152 struct buffer_head *xb_bh);
153
Tiger Yang0030e002008-10-23 16:33:33 +0800154static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
155{
156 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
157}
158
159static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
160{
161 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
162}
163
164static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
165{
166 u16 len = sb->s_blocksize -
167 offsetof(struct ocfs2_xattr_header, xh_entries);
168
169 return len / sizeof(struct ocfs2_xattr_entry);
170}
171
Joel Becker9c7759a2008-10-24 16:21:03 -0700172#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700173#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700174#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700175
Joel Beckerba937122008-10-24 19:13:20 -0700176static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700177{
Joel Beckerba937122008-10-24 19:13:20 -0700178 struct ocfs2_xattr_bucket *bucket;
179 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700180
Joel Beckerba937122008-10-24 19:13:20 -0700181 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
182
183 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
184 if (bucket) {
185 bucket->bu_inode = inode;
186 bucket->bu_blocks = blks;
187 }
188
189 return bucket;
190}
191
192static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
193{
194 int i;
195
196 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700197 brelse(bucket->bu_bhs[i]);
198 bucket->bu_bhs[i] = NULL;
199 }
200}
201
Joel Beckerba937122008-10-24 19:13:20 -0700202static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
203{
204 if (bucket) {
205 ocfs2_xattr_bucket_relse(bucket);
206 bucket->bu_inode = NULL;
207 kfree(bucket);
208 }
209}
210
Joel Becker784b8162008-10-24 17:33:40 -0700211/*
212 * A bucket that has never been written to disk doesn't need to be
213 * read. We just need the buffer_heads. Don't call this for
214 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
215 * them fully.
216 */
Joel Beckerba937122008-10-24 19:13:20 -0700217static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700218 u64 xb_blkno)
219{
220 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700221
Joel Beckerba937122008-10-24 19:13:20 -0700222 for (i = 0; i < bucket->bu_blocks; i++) {
223 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
224 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700225 if (!bucket->bu_bhs[i]) {
226 rc = -EIO;
227 mlog_errno(rc);
228 break;
229 }
230
Tao Ma757055a2008-11-06 08:10:48 +0800231 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
232 bucket->bu_bhs[i]))
233 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
234 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700235 }
236
237 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700238 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700239 return rc;
240}
241
242/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700243static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700244 u64 xb_blkno)
245{
Joel Beckerba937122008-10-24 19:13:20 -0700246 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700247
Joel Beckerba937122008-10-24 19:13:20 -0700248 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
249 bucket->bu_blocks, bucket->bu_bhs, 0);
Joel Becker784b8162008-10-24 17:33:40 -0700250 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700251 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700252 return rc;
253}
254
Joel Becker1224be02008-10-24 18:47:33 -0700255static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700256 struct ocfs2_xattr_bucket *bucket,
257 int type)
258{
259 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700260
Joel Beckerba937122008-10-24 19:13:20 -0700261 for (i = 0; i < bucket->bu_blocks; i++) {
262 rc = ocfs2_journal_access(handle, bucket->bu_inode,
Joel Becker1224be02008-10-24 18:47:33 -0700263 bucket->bu_bhs[i], type);
264 if (rc) {
265 mlog_errno(rc);
266 break;
267 }
268 }
269
270 return rc;
271}
272
273static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700274 struct ocfs2_xattr_bucket *bucket)
275{
Joel Beckerba937122008-10-24 19:13:20 -0700276 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700277
Joel Beckerba937122008-10-24 19:13:20 -0700278 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700279 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
280}
281
Joel Beckerba937122008-10-24 19:13:20 -0700282static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700283 struct ocfs2_xattr_bucket *src)
284{
285 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700286 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700287
Joel Beckerba937122008-10-24 19:13:20 -0700288 BUG_ON(dest->bu_blocks != src->bu_blocks);
289 BUG_ON(dest->bu_inode != src->bu_inode);
290
291 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700292 memcpy(bucket_block(dest, i), bucket_block(src, i),
293 blocksize);
294 }
295}
Joel Becker1224be02008-10-24 18:47:33 -0700296
Tao Ma936b8832008-10-09 23:06:14 +0800297static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800298{
299 struct xattr_handler *handler = NULL;
300
301 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
302 handler = ocfs2_xattr_handler_map[name_index];
303
Tao Ma936b8832008-10-09 23:06:14 +0800304 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800305}
306
Mark Fasheh40daa162008-10-07 14:31:42 -0700307static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800308 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700309 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800310{
311 /* Get hash value of uuid from super block */
312 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
313 int i;
314
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800315 /* hash extended attribute name */
316 for (i = 0; i < name_len; i++) {
317 hash = (hash << OCFS2_HASH_SHIFT) ^
318 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
319 *name++;
320 }
321
322 return hash;
323}
324
325/*
326 * ocfs2_xattr_hash_entry()
327 *
328 * Compute the hash of an extended attribute.
329 */
330static void ocfs2_xattr_hash_entry(struct inode *inode,
331 struct ocfs2_xattr_header *header,
332 struct ocfs2_xattr_entry *entry)
333{
334 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800335 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800336
Tao Ma2057e5c2008-10-09 23:06:13 +0800337 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800338 entry->xe_name_hash = cpu_to_le32(hash);
339
340 return;
341}
Tao Maf56654c2008-08-18 17:38:48 +0800342
343static int ocfs2_xattr_extend_allocation(struct inode *inode,
344 u32 clusters_to_add,
345 struct buffer_head *xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800346 struct ocfs2_xattr_value_root *xv,
347 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800348{
349 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800350 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800351 enum ocfs2_alloc_restarted why;
352 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Maf56654c2008-08-18 17:38:48 +0800353 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700354 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800355
356 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
357
Joel Becker8d6220d2008-08-22 12:46:09 -0700358 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700359
Tao Maf56654c2008-08-18 17:38:48 +0800360 status = ocfs2_journal_access(handle, inode, xattr_bh,
361 OCFS2_JOURNAL_ACCESS_WRITE);
362 if (status < 0) {
363 mlog_errno(status);
364 goto leave;
365 }
366
367 prev_clusters = le32_to_cpu(xv->xr_clusters);
368 status = ocfs2_add_clusters_in_btree(osb,
369 inode,
370 &logical_start,
371 clusters_to_add,
372 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700373 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800374 handle,
Tao Ma78f30c32008-11-12 08:27:00 +0800375 ctxt->data_ac,
376 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700377 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800378 if (status < 0) {
379 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800380 goto leave;
381 }
382
383 status = ocfs2_journal_dirty(handle, xattr_bh);
384 if (status < 0) {
385 mlog_errno(status);
386 goto leave;
387 }
388
389 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
390
Tao Ma85db90e2008-11-12 08:27:01 +0800391 /*
392 * We should have already allocated enough space before the transaction,
393 * so no need to restart.
394 */
395 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800396
397leave:
Tao Maf56654c2008-08-18 17:38:48 +0800398
399 return status;
400}
401
402static int __ocfs2_remove_xattr_range(struct inode *inode,
403 struct buffer_head *root_bh,
404 struct ocfs2_xattr_value_root *xv,
405 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma78f30c32008-11-12 08:27:00 +0800406 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800407{
408 int ret;
409 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800410 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700411 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800412
Joel Becker8d6220d2008-08-22 12:46:09 -0700413 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700414
Tao Maf56654c2008-08-18 17:38:48 +0800415 ret = ocfs2_journal_access(handle, inode, root_bh,
416 OCFS2_JOURNAL_ACCESS_WRITE);
417 if (ret) {
418 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800419 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800420 }
421
Tao Ma78f30c32008-11-12 08:27:00 +0800422 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
423 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800424 if (ret) {
425 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800426 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800427 }
428
429 le32_add_cpu(&xv->xr_clusters, -len);
430
431 ret = ocfs2_journal_dirty(handle, root_bh);
432 if (ret) {
433 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800434 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800435 }
436
Tao Ma78f30c32008-11-12 08:27:00 +0800437 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800438 if (ret)
439 mlog_errno(ret);
440
Tao Maf56654c2008-08-18 17:38:48 +0800441out:
Tao Maf56654c2008-08-18 17:38:48 +0800442 return ret;
443}
444
445static int ocfs2_xattr_shrink_size(struct inode *inode,
446 u32 old_clusters,
447 u32 new_clusters,
448 struct buffer_head *root_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800449 struct ocfs2_xattr_value_root *xv,
450 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800451{
452 int ret = 0;
453 u32 trunc_len, cpos, phys_cpos, alloc_size;
454 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800455
456 if (old_clusters <= new_clusters)
457 return 0;
458
459 cpos = new_clusters;
460 trunc_len = old_clusters - new_clusters;
461 while (trunc_len) {
462 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
463 &alloc_size, &xv->xr_list);
464 if (ret) {
465 mlog_errno(ret);
466 goto out;
467 }
468
469 if (alloc_size > trunc_len)
470 alloc_size = trunc_len;
471
472 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
473 phys_cpos, alloc_size,
Tao Ma78f30c32008-11-12 08:27:00 +0800474 ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800475 if (ret) {
476 mlog_errno(ret);
477 goto out;
478 }
479
480 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
481 ocfs2_remove_xattr_clusters_from_cache(inode, block,
482 alloc_size);
483 cpos += alloc_size;
484 trunc_len -= alloc_size;
485 }
486
487out:
Tao Maf56654c2008-08-18 17:38:48 +0800488 return ret;
489}
490
491static int ocfs2_xattr_value_truncate(struct inode *inode,
492 struct buffer_head *root_bh,
493 struct ocfs2_xattr_value_root *xv,
Tao Ma78f30c32008-11-12 08:27:00 +0800494 int len,
495 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800496{
497 int ret;
498 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
499 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
500
501 if (new_clusters == old_clusters)
502 return 0;
503
504 if (new_clusters > old_clusters)
505 ret = ocfs2_xattr_extend_allocation(inode,
506 new_clusters - old_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800507 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800508 else
509 ret = ocfs2_xattr_shrink_size(inode,
510 old_clusters, new_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800511 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800512
513 return ret;
514}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800515
Tao Ma936b8832008-10-09 23:06:14 +0800516static int ocfs2_xattr_list_entry(char *buffer, size_t size,
517 size_t *result, const char *prefix,
518 const char *name, int name_len)
519{
520 char *p = buffer + *result;
521 int prefix_len = strlen(prefix);
522 int total_len = prefix_len + name_len + 1;
523
524 *result += total_len;
525
526 /* we are just looking for how big our buffer needs to be */
527 if (!size)
528 return 0;
529
530 if (*result > size)
531 return -ERANGE;
532
533 memcpy(p, prefix, prefix_len);
534 memcpy(p + prefix_len, name, name_len);
535 p[prefix_len + name_len] = '\0';
536
537 return 0;
538}
539
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800540static int ocfs2_xattr_list_entries(struct inode *inode,
541 struct ocfs2_xattr_header *header,
542 char *buffer, size_t buffer_size)
543{
Tao Ma936b8832008-10-09 23:06:14 +0800544 size_t result = 0;
545 int i, type, ret;
546 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800547
548 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
549 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800550 type = ocfs2_xattr_get_type(entry);
551 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800552
Tao Ma936b8832008-10-09 23:06:14 +0800553 if (prefix) {
554 name = (const char *)header +
555 le16_to_cpu(entry->xe_name_offset);
556
557 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
558 &result, prefix, name,
559 entry->xe_name_len);
560 if (ret)
561 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800562 }
563 }
564
Tao Ma936b8832008-10-09 23:06:14 +0800565 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800566}
567
568static int ocfs2_xattr_ibody_list(struct inode *inode,
569 struct ocfs2_dinode *di,
570 char *buffer,
571 size_t buffer_size)
572{
573 struct ocfs2_xattr_header *header = NULL;
574 struct ocfs2_inode_info *oi = OCFS2_I(inode);
575 int ret = 0;
576
577 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
578 return ret;
579
580 header = (struct ocfs2_xattr_header *)
581 ((void *)di + inode->i_sb->s_blocksize -
582 le16_to_cpu(di->i_xattr_inline_size));
583
584 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
585
586 return ret;
587}
588
589static int ocfs2_xattr_block_list(struct inode *inode,
590 struct ocfs2_dinode *di,
591 char *buffer,
592 size_t buffer_size)
593{
594 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800595 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800596 int ret = 0;
597
598 if (!di->i_xattr_loc)
599 return ret;
600
Joel Becker0fcaa562008-10-09 17:20:31 -0700601 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800602 if (ret < 0) {
603 mlog_errno(ret);
604 return ret;
605 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800606
Tao Ma0c044f02008-08-18 17:38:50 +0800607 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Joel Beckerf6087fb2008-10-20 18:20:43 -0700608 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
609 ret = -EIO;
610 goto cleanup;
611 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800612
Tao Ma0c044f02008-08-18 17:38:50 +0800613 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
614 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
615 ret = ocfs2_xattr_list_entries(inode, header,
616 buffer, buffer_size);
617 } else {
618 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
619 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
620 buffer, buffer_size);
621 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800622cleanup:
623 brelse(blk_bh);
624
625 return ret;
626}
627
628ssize_t ocfs2_listxattr(struct dentry *dentry,
629 char *buffer,
630 size_t size)
631{
632 int ret = 0, i_ret = 0, b_ret = 0;
633 struct buffer_head *di_bh = NULL;
634 struct ocfs2_dinode *di = NULL;
635 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
636
Tiger Yang8154da32008-08-18 17:11:46 +0800637 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
638 return -EOPNOTSUPP;
639
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800640 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
641 return ret;
642
643 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
644 if (ret < 0) {
645 mlog_errno(ret);
646 return ret;
647 }
648
649 di = (struct ocfs2_dinode *)di_bh->b_data;
650
651 down_read(&oi->ip_xattr_sem);
652 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
653 if (i_ret < 0)
654 b_ret = 0;
655 else {
656 if (buffer) {
657 buffer += i_ret;
658 size -= i_ret;
659 }
660 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
661 buffer, size);
662 if (b_ret < 0)
663 i_ret = 0;
664 }
665 up_read(&oi->ip_xattr_sem);
666 ocfs2_inode_unlock(dentry->d_inode, 0);
667
668 brelse(di_bh);
669
670 return i_ret + b_ret;
671}
672
673static int ocfs2_xattr_find_entry(int name_index,
674 const char *name,
675 struct ocfs2_xattr_search *xs)
676{
677 struct ocfs2_xattr_entry *entry;
678 size_t name_len;
679 int i, cmp = 1;
680
681 if (name == NULL)
682 return -EINVAL;
683
684 name_len = strlen(name);
685 entry = xs->here;
686 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
687 cmp = name_index - ocfs2_xattr_get_type(entry);
688 if (!cmp)
689 cmp = name_len - entry->xe_name_len;
690 if (!cmp)
691 cmp = memcmp(name, (xs->base +
692 le16_to_cpu(entry->xe_name_offset)),
693 name_len);
694 if (cmp == 0)
695 break;
696 entry += 1;
697 }
698 xs->here = entry;
699
700 return cmp ? -ENODATA : 0;
701}
702
703static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800704 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800705 void *buffer,
706 size_t len)
707{
708 u32 cpos, p_cluster, num_clusters, bpc, clusters;
709 u64 blkno;
710 int i, ret = 0;
711 size_t cplen, blocksize;
712 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800713 struct ocfs2_extent_list *el;
714
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800715 el = &xv->xr_list;
716 clusters = le32_to_cpu(xv->xr_clusters);
717 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
718 blocksize = inode->i_sb->s_blocksize;
719
720 cpos = 0;
721 while (cpos < clusters) {
722 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
723 &num_clusters, el);
724 if (ret) {
725 mlog_errno(ret);
726 goto out;
727 }
728
729 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
730 /* Copy ocfs2_xattr_value */
731 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa562008-10-09 17:20:31 -0700732 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800733 if (ret) {
734 mlog_errno(ret);
735 goto out;
736 }
737
738 cplen = len >= blocksize ? blocksize : len;
739 memcpy(buffer, bh->b_data, cplen);
740 len -= cplen;
741 buffer += cplen;
742
743 brelse(bh);
744 bh = NULL;
745 if (len == 0)
746 break;
747 }
748 cpos += num_clusters;
749 }
750out:
751 return ret;
752}
753
754static int ocfs2_xattr_ibody_get(struct inode *inode,
755 int name_index,
756 const char *name,
757 void *buffer,
758 size_t buffer_size,
759 struct ocfs2_xattr_search *xs)
760{
761 struct ocfs2_inode_info *oi = OCFS2_I(inode);
762 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800763 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800764 size_t size;
765 int ret = 0;
766
767 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
768 return -ENODATA;
769
770 xs->end = (void *)di + inode->i_sb->s_blocksize;
771 xs->header = (struct ocfs2_xattr_header *)
772 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
773 xs->base = (void *)xs->header;
774 xs->here = xs->header->xh_entries;
775
776 ret = ocfs2_xattr_find_entry(name_index, name, xs);
777 if (ret)
778 return ret;
779 size = le64_to_cpu(xs->here->xe_value_size);
780 if (buffer) {
781 if (size > buffer_size)
782 return -ERANGE;
783 if (ocfs2_xattr_is_local(xs->here)) {
784 memcpy(buffer, (void *)xs->base +
785 le16_to_cpu(xs->here->xe_name_offset) +
786 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
787 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800788 xv = (struct ocfs2_xattr_value_root *)
789 (xs->base + le16_to_cpu(
790 xs->here->xe_name_offset) +
791 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
792 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800793 buffer, size);
794 if (ret < 0) {
795 mlog_errno(ret);
796 return ret;
797 }
798 }
799 }
800
801 return size;
802}
803
804static int ocfs2_xattr_block_get(struct inode *inode,
805 int name_index,
806 const char *name,
807 void *buffer,
808 size_t buffer_size,
809 struct ocfs2_xattr_search *xs)
810{
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800811 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +0800812 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800813 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +0800814 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800815
Joel Beckerba937122008-10-24 19:13:20 -0700816 xs->bucket = ocfs2_xattr_bucket_new(inode);
817 if (!xs->bucket) {
818 ret = -ENOMEM;
819 mlog_errno(ret);
820 goto cleanup;
821 }
Tao Ma589dc262008-08-18 17:38:51 +0800822
Joel Becker54f443f2008-10-20 18:43:07 -0700823 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
824 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800825 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800826 goto cleanup;
827 }
828
Tiger Yang6c1e1832008-11-02 19:04:21 +0800829 if (xs->not_found) {
830 ret = -ENODATA;
831 goto cleanup;
832 }
833
Joel Becker54f443f2008-10-20 18:43:07 -0700834 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800835 size = le64_to_cpu(xs->here->xe_value_size);
836 if (buffer) {
837 ret = -ERANGE;
838 if (size > buffer_size)
839 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +0800840
841 name_offset = le16_to_cpu(xs->here->xe_name_offset);
842 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
843 i = xs->here - xs->header->xh_entries;
844
845 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
846 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -0700847 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +0800848 i,
849 &block_off,
850 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -0700851 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +0800852 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800853 if (ocfs2_xattr_is_local(xs->here)) {
854 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +0800855 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800856 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800857 xv = (struct ocfs2_xattr_value_root *)
858 (xs->base + name_offset + name_len);
859 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800860 buffer, size);
861 if (ret < 0) {
862 mlog_errno(ret);
863 goto cleanup;
864 }
865 }
866 }
867 ret = size;
868cleanup:
Joel Beckerba937122008-10-24 19:13:20 -0700869 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800870
Joel Becker54f443f2008-10-20 18:43:07 -0700871 brelse(xs->xattr_bh);
872 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800873 return ret;
874}
875
876/* ocfs2_xattr_get()
877 *
878 * Copy an extended attribute into the buffer provided.
879 * Buffer is NULL to compute the size of buffer required.
880 */
Tiger Yang0030e002008-10-23 16:33:33 +0800881static int ocfs2_xattr_get(struct inode *inode,
882 int name_index,
883 const char *name,
884 void *buffer,
885 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800886{
887 int ret;
888 struct ocfs2_dinode *di = NULL;
889 struct buffer_head *di_bh = NULL;
890 struct ocfs2_inode_info *oi = OCFS2_I(inode);
891 struct ocfs2_xattr_search xis = {
892 .not_found = -ENODATA,
893 };
894 struct ocfs2_xattr_search xbs = {
895 .not_found = -ENODATA,
896 };
897
Tiger Yang8154da32008-08-18 17:11:46 +0800898 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
899 return -EOPNOTSUPP;
900
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800901 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
902 ret = -ENODATA;
903
904 ret = ocfs2_inode_lock(inode, &di_bh, 0);
905 if (ret < 0) {
906 mlog_errno(ret);
907 return ret;
908 }
909 xis.inode_bh = xbs.inode_bh = di_bh;
910 di = (struct ocfs2_dinode *)di_bh->b_data;
911
912 down_read(&oi->ip_xattr_sem);
913 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
914 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +0800915 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800916 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
917 buffer_size, &xbs);
918 up_read(&oi->ip_xattr_sem);
919 ocfs2_inode_unlock(inode, 0);
920
921 brelse(di_bh);
922
923 return ret;
924}
925
926static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +0800927 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800928 struct ocfs2_xattr_value_root *xv,
929 const void *value,
930 int value_len)
931{
932 int ret = 0, i, cp_len, credits;
933 u16 blocksize = inode->i_sb->s_blocksize;
934 u32 p_cluster, num_clusters;
935 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
936 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
937 u64 blkno;
938 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800939
940 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
941
Tao Ma85db90e2008-11-12 08:27:01 +0800942 /*
943 * In __ocfs2_xattr_set_value_outside has already been dirtied,
944 * so we don't need to worry about whether ocfs2_extend_trans
945 * will create a new transactio for us or not.
946 */
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800947 credits = clusters * bpc;
Tao Ma85db90e2008-11-12 08:27:01 +0800948 ret = ocfs2_extend_trans(handle, credits);
949 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800950 mlog_errno(ret);
951 goto out;
952 }
953
954 while (cpos < clusters) {
955 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
956 &num_clusters, &xv->xr_list);
957 if (ret) {
958 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800959 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800960 }
961
962 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
963
964 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa562008-10-09 17:20:31 -0700965 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800966 if (ret) {
967 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800968 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800969 }
970
971 ret = ocfs2_journal_access(handle,
972 inode,
973 bh,
974 OCFS2_JOURNAL_ACCESS_WRITE);
975 if (ret < 0) {
976 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800977 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800978 }
979
980 cp_len = value_len > blocksize ? blocksize : value_len;
981 memcpy(bh->b_data, value, cp_len);
982 value_len -= cp_len;
983 value += cp_len;
984 if (cp_len < blocksize)
985 memset(bh->b_data + cp_len, 0,
986 blocksize - cp_len);
987
988 ret = ocfs2_journal_dirty(handle, bh);
989 if (ret < 0) {
990 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800991 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800992 }
993 brelse(bh);
994 bh = NULL;
995
996 /*
997 * XXX: do we need to empty all the following
998 * blocks in this cluster?
999 */
1000 if (!value_len)
1001 break;
1002 }
1003 cpos += num_clusters;
1004 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001005out:
1006 brelse(bh);
1007
1008 return ret;
1009}
1010
1011static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001012 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001013 struct ocfs2_xattr_info *xi,
1014 struct ocfs2_xattr_search *xs,
1015 size_t offs)
1016{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001017 int ret = 0;
1018 size_t name_len = strlen(xi->name);
1019 void *val = xs->base + offs;
1020 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1021
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001022 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1023 OCFS2_JOURNAL_ACCESS_WRITE);
1024 if (ret) {
1025 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001026 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001027 }
1028 /* Decrease xattr count */
1029 le16_add_cpu(&xs->header->xh_count, -1);
1030 /* Remove the xattr entry and tree root which has already be set*/
1031 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1032 memset(val, 0, size);
1033
1034 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1035 if (ret < 0)
1036 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001037out:
1038 return ret;
1039}
1040
1041static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001042 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001043 struct ocfs2_xattr_info *xi,
1044 struct ocfs2_xattr_search *xs,
1045 size_t offs)
1046{
Tao Ma85db90e2008-11-12 08:27:01 +08001047 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001048
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001049 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1050 OCFS2_JOURNAL_ACCESS_WRITE);
1051 if (ret) {
1052 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001053 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001054 }
1055
1056 xs->here->xe_name_offset = cpu_to_le16(offs);
1057 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1058 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1059 ocfs2_xattr_set_local(xs->here, 1);
1060 else
1061 ocfs2_xattr_set_local(xs->here, 0);
1062 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1063
1064 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1065 if (ret < 0)
1066 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001067out:
1068 return ret;
1069}
1070
1071/*
1072 * ocfs2_xattr_set_value_outside()
1073 *
1074 * Set large size value in B tree.
1075 */
1076static int ocfs2_xattr_set_value_outside(struct inode *inode,
1077 struct ocfs2_xattr_info *xi,
1078 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001079 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001080 size_t offs)
1081{
1082 size_t name_len = strlen(xi->name);
1083 void *val = xs->base + offs;
1084 struct ocfs2_xattr_value_root *xv = NULL;
1085 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1086 int ret = 0;
1087
1088 memset(val, 0, size);
1089 memcpy(val, xi->name, name_len);
1090 xv = (struct ocfs2_xattr_value_root *)
1091 (val + OCFS2_XATTR_SIZE(name_len));
1092 xv->xr_clusters = 0;
1093 xv->xr_last_eb_blk = 0;
1094 xv->xr_list.l_tree_depth = 0;
1095 xv->xr_list.l_count = cpu_to_le16(1);
1096 xv->xr_list.l_next_free_rec = 0;
1097
1098 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001099 xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001100 if (ret < 0) {
1101 mlog_errno(ret);
1102 return ret;
1103 }
Tao Ma85db90e2008-11-12 08:27:01 +08001104 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001105 if (ret < 0) {
1106 mlog_errno(ret);
1107 return ret;
1108 }
Tao Ma85db90e2008-11-12 08:27:01 +08001109 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1110 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001111 if (ret < 0)
1112 mlog_errno(ret);
1113
1114 return ret;
1115}
1116
1117/*
1118 * ocfs2_xattr_set_entry_local()
1119 *
1120 * Set, replace or remove extended attribute in local.
1121 */
1122static void ocfs2_xattr_set_entry_local(struct inode *inode,
1123 struct ocfs2_xattr_info *xi,
1124 struct ocfs2_xattr_search *xs,
1125 struct ocfs2_xattr_entry *last,
1126 size_t min_offs)
1127{
1128 size_t name_len = strlen(xi->name);
1129 int i;
1130
1131 if (xi->value && xs->not_found) {
1132 /* Insert the new xattr entry. */
1133 le16_add_cpu(&xs->header->xh_count, 1);
1134 ocfs2_xattr_set_type(last, xi->name_index);
1135 ocfs2_xattr_set_local(last, 1);
1136 last->xe_name_len = name_len;
1137 } else {
1138 void *first_val;
1139 void *val;
1140 size_t offs, size;
1141
1142 first_val = xs->base + min_offs;
1143 offs = le16_to_cpu(xs->here->xe_name_offset);
1144 val = xs->base + offs;
1145
1146 if (le64_to_cpu(xs->here->xe_value_size) >
1147 OCFS2_XATTR_INLINE_SIZE)
1148 size = OCFS2_XATTR_SIZE(name_len) +
1149 OCFS2_XATTR_ROOT_SIZE;
1150 else
1151 size = OCFS2_XATTR_SIZE(name_len) +
1152 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1153
1154 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1155 OCFS2_XATTR_SIZE(xi->value_len)) {
1156 /* The old and the new value have the
1157 same size. Just replace the value. */
1158 ocfs2_xattr_set_local(xs->here, 1);
1159 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1160 /* Clear value bytes. */
1161 memset(val + OCFS2_XATTR_SIZE(name_len),
1162 0,
1163 OCFS2_XATTR_SIZE(xi->value_len));
1164 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1165 xi->value,
1166 xi->value_len);
1167 return;
1168 }
1169 /* Remove the old name+value. */
1170 memmove(first_val + size, first_val, val - first_val);
1171 memset(first_val, 0, size);
1172 xs->here->xe_name_hash = 0;
1173 xs->here->xe_name_offset = 0;
1174 ocfs2_xattr_set_local(xs->here, 1);
1175 xs->here->xe_value_size = 0;
1176
1177 min_offs += size;
1178
1179 /* Adjust all value offsets. */
1180 last = xs->header->xh_entries;
1181 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1182 size_t o = le16_to_cpu(last->xe_name_offset);
1183
1184 if (o < offs)
1185 last->xe_name_offset = cpu_to_le16(o + size);
1186 last += 1;
1187 }
1188
1189 if (!xi->value) {
1190 /* Remove the old entry. */
1191 last -= 1;
1192 memmove(xs->here, xs->here + 1,
1193 (void *)last - (void *)xs->here);
1194 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1195 le16_add_cpu(&xs->header->xh_count, -1);
1196 }
1197 }
1198 if (xi->value) {
1199 /* Insert the new name+value. */
1200 size_t size = OCFS2_XATTR_SIZE(name_len) +
1201 OCFS2_XATTR_SIZE(xi->value_len);
1202 void *val = xs->base + min_offs - size;
1203
1204 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1205 memset(val, 0, size);
1206 memcpy(val, xi->name, name_len);
1207 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1208 xi->value,
1209 xi->value_len);
1210 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1211 ocfs2_xattr_set_local(xs->here, 1);
1212 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1213 }
1214
1215 return;
1216}
1217
1218/*
1219 * ocfs2_xattr_set_entry()
1220 *
1221 * Set extended attribute entry into inode or block.
1222 *
1223 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1224 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1225 * then set value in B tree with set_value_outside().
1226 */
1227static int ocfs2_xattr_set_entry(struct inode *inode,
1228 struct ocfs2_xattr_info *xi,
1229 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001230 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001231 int flag)
1232{
1233 struct ocfs2_xattr_entry *last;
1234 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1235 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1236 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1237 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001238 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001239 int free, i, ret;
1240 struct ocfs2_xattr_info xi_l = {
1241 .name_index = xi->name_index,
1242 .name = xi->name,
1243 .value = xi->value,
1244 .value_len = xi->value_len,
1245 };
1246
1247 /* Compute min_offs, last and free space. */
1248 last = xs->header->xh_entries;
1249
1250 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1251 size_t offs = le16_to_cpu(last->xe_name_offset);
1252 if (offs < min_offs)
1253 min_offs = offs;
1254 last += 1;
1255 }
1256
1257 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1258 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001259 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001260
1261 if (!xs->not_found) {
1262 size_t size = 0;
1263 if (ocfs2_xattr_is_local(xs->here))
1264 size = OCFS2_XATTR_SIZE(name_len) +
1265 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1266 else
1267 size = OCFS2_XATTR_SIZE(name_len) +
1268 OCFS2_XATTR_ROOT_SIZE;
1269 free += (size + sizeof(struct ocfs2_xattr_entry));
1270 }
1271 /* Check free space in inode or block */
1272 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1273 if (free < sizeof(struct ocfs2_xattr_entry) +
1274 OCFS2_XATTR_SIZE(name_len) +
1275 OCFS2_XATTR_ROOT_SIZE) {
1276 ret = -ENOSPC;
1277 goto out;
1278 }
1279 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1280 xi_l.value = (void *)&def_xv;
1281 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1282 } else if (xi->value) {
1283 if (free < sizeof(struct ocfs2_xattr_entry) +
1284 OCFS2_XATTR_SIZE(name_len) +
1285 OCFS2_XATTR_SIZE(xi->value_len)) {
1286 ret = -ENOSPC;
1287 goto out;
1288 }
1289 }
1290
1291 if (!xs->not_found) {
1292 /* For existing extended attribute */
1293 size_t size = OCFS2_XATTR_SIZE(name_len) +
1294 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1295 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1296 void *val = xs->base + offs;
1297
1298 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1299 /* Replace existing local xattr with tree root */
1300 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001301 ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001302 if (ret < 0)
1303 mlog_errno(ret);
1304 goto out;
1305 } else if (!ocfs2_xattr_is_local(xs->here)) {
1306 /* For existing xattr which has value outside */
1307 struct ocfs2_xattr_value_root *xv = NULL;
1308 xv = (struct ocfs2_xattr_value_root *)(val +
1309 OCFS2_XATTR_SIZE(name_len));
1310
1311 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1312 /*
1313 * If new value need set outside also,
1314 * first truncate old value to new value,
1315 * then set new value with set_value_outside().
1316 */
1317 ret = ocfs2_xattr_value_truncate(inode,
1318 xs->xattr_bh,
1319 xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001320 xi->value_len,
1321 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001322 if (ret < 0) {
1323 mlog_errno(ret);
1324 goto out;
1325 }
1326
Tao Ma85db90e2008-11-12 08:27:01 +08001327 ret = ocfs2_xattr_update_entry(inode,
1328 handle,
1329 xi,
1330 xs,
1331 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001332 if (ret < 0) {
1333 mlog_errno(ret);
1334 goto out;
1335 }
1336
Tao Ma85db90e2008-11-12 08:27:01 +08001337 ret = __ocfs2_xattr_set_value_outside(inode,
1338 handle,
1339 xv,
1340 xi->value,
1341 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001342 if (ret < 0)
1343 mlog_errno(ret);
1344 goto out;
1345 } else {
1346 /*
1347 * If new value need set in local,
1348 * just trucate old value to zero.
1349 */
1350 ret = ocfs2_xattr_value_truncate(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001351 xs->xattr_bh,
1352 xv,
1353 0,
1354 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001355 if (ret < 0)
1356 mlog_errno(ret);
1357 }
1358 }
1359 }
1360
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001361 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1362 OCFS2_JOURNAL_ACCESS_WRITE);
1363 if (ret) {
1364 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001365 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001366 }
1367
1368 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001369 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1370 OCFS2_JOURNAL_ACCESS_WRITE);
1371 if (ret) {
1372 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001373 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001374 }
1375 }
1376
1377 /*
1378 * Set value in local, include set tree root in local.
1379 * This is the first step for value size >INLINE_SIZE.
1380 */
1381 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1382
1383 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1384 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1385 if (ret < 0) {
1386 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001387 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001388 }
1389 }
1390
1391 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1392 (flag & OCFS2_INLINE_XATTR_FL)) {
1393 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1394 unsigned int xattrsize = osb->s_xattr_inline_size;
1395
1396 /*
1397 * Adjust extent record count or inline data size
1398 * to reserve space for extended attribute.
1399 */
1400 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1401 struct ocfs2_inline_data *idata = &di->id2.i_data;
1402 le16_add_cpu(&idata->id_count, -xattrsize);
1403 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1404 struct ocfs2_extent_list *el = &di->id2.i_list;
1405 le16_add_cpu(&el->l_count, -(xattrsize /
1406 sizeof(struct ocfs2_extent_rec)));
1407 }
1408 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1409 }
1410 /* Update xattr flag */
1411 spin_lock(&oi->ip_lock);
1412 oi->ip_dyn_features |= flag;
1413 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1414 spin_unlock(&oi->ip_lock);
1415 /* Update inode ctime */
1416 inode->i_ctime = CURRENT_TIME;
1417 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1418 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1419
1420 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1421 if (ret < 0)
1422 mlog_errno(ret);
1423
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001424 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1425 /*
1426 * Set value outside in B tree.
1427 * This is the second step for value size > INLINE_SIZE.
1428 */
1429 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Tao Ma78f30c32008-11-12 08:27:00 +08001430 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001431 if (ret < 0) {
1432 int ret2;
1433
1434 mlog_errno(ret);
1435 /*
1436 * If set value outside failed, we have to clean
1437 * the junk tree root we have already set in local.
1438 */
Tao Ma85db90e2008-11-12 08:27:01 +08001439 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1440 xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001441 if (ret2 < 0)
1442 mlog_errno(ret2);
1443 }
1444 }
1445out:
1446 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001447}
1448
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001449static int ocfs2_remove_value_outside(struct inode*inode,
1450 struct buffer_head *bh,
1451 struct ocfs2_xattr_header *header)
1452{
1453 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001454 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1455 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1456
1457 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001458
Tao Ma85db90e2008-11-12 08:27:01 +08001459 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1460 if (IS_ERR(ctxt.handle)) {
1461 ret = PTR_ERR(ctxt.handle);
1462 mlog_errno(ret);
1463 goto out;
1464 }
1465
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001466 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1467 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1468
1469 if (!ocfs2_xattr_is_local(entry)) {
1470 struct ocfs2_xattr_value_root *xv;
1471 void *val;
1472
1473 val = (void *)header +
1474 le16_to_cpu(entry->xe_name_offset);
1475 xv = (struct ocfs2_xattr_value_root *)
1476 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Tao Ma78f30c32008-11-12 08:27:00 +08001477 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1478 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001479 if (ret < 0) {
1480 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001481 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001482 }
1483 }
1484 }
1485
Tao Ma85db90e2008-11-12 08:27:01 +08001486 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001487 ocfs2_schedule_truncate_log_flush(osb, 1);
1488 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001489out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001490 return ret;
1491}
1492
1493static int ocfs2_xattr_ibody_remove(struct inode *inode,
1494 struct buffer_head *di_bh)
1495{
1496
1497 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1498 struct ocfs2_xattr_header *header;
1499 int ret;
1500
1501 header = (struct ocfs2_xattr_header *)
1502 ((void *)di + inode->i_sb->s_blocksize -
1503 le16_to_cpu(di->i_xattr_inline_size));
1504
1505 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1506
1507 return ret;
1508}
1509
1510static int ocfs2_xattr_block_remove(struct inode *inode,
1511 struct buffer_head *blk_bh)
1512{
1513 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001514 int ret = 0;
1515
1516 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001517 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1518 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1519 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1520 } else
1521 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001522
1523 return ret;
1524}
1525
Tao Ma08413892008-08-29 09:00:19 +08001526static int ocfs2_xattr_free_block(struct inode *inode,
1527 u64 block)
1528{
1529 struct inode *xb_alloc_inode;
1530 struct buffer_head *xb_alloc_bh = NULL;
1531 struct buffer_head *blk_bh = NULL;
1532 struct ocfs2_xattr_block *xb;
1533 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1534 handle_t *handle;
1535 int ret = 0;
1536 u64 blk, bg_blkno;
1537 u16 bit;
1538
Joel Becker0fcaa562008-10-09 17:20:31 -07001539 ret = ocfs2_read_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001540 if (ret < 0) {
1541 mlog_errno(ret);
1542 goto out;
1543 }
1544
Joel Beckerf6087fb2008-10-20 18:20:43 -07001545 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1546 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1547 ret = -EIO;
Tao Ma08413892008-08-29 09:00:19 +08001548 goto out;
1549 }
1550
1551 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1552 if (ret < 0) {
1553 mlog_errno(ret);
1554 goto out;
1555 }
1556
Tao Ma08413892008-08-29 09:00:19 +08001557 blk = le64_to_cpu(xb->xb_blkno);
1558 bit = le16_to_cpu(xb->xb_suballoc_bit);
1559 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1560
1561 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1562 EXTENT_ALLOC_SYSTEM_INODE,
1563 le16_to_cpu(xb->xb_suballoc_slot));
1564 if (!xb_alloc_inode) {
1565 ret = -ENOMEM;
1566 mlog_errno(ret);
1567 goto out;
1568 }
1569 mutex_lock(&xb_alloc_inode->i_mutex);
1570
1571 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1572 if (ret < 0) {
1573 mlog_errno(ret);
1574 goto out_mutex;
1575 }
1576
1577 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1578 if (IS_ERR(handle)) {
1579 ret = PTR_ERR(handle);
1580 mlog_errno(ret);
1581 goto out_unlock;
1582 }
1583
1584 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1585 bit, bg_blkno, 1);
1586 if (ret < 0)
1587 mlog_errno(ret);
1588
1589 ocfs2_commit_trans(osb, handle);
1590out_unlock:
1591 ocfs2_inode_unlock(xb_alloc_inode, 1);
1592 brelse(xb_alloc_bh);
1593out_mutex:
1594 mutex_unlock(&xb_alloc_inode->i_mutex);
1595 iput(xb_alloc_inode);
1596out:
1597 brelse(blk_bh);
1598 return ret;
1599}
1600
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001601/*
1602 * ocfs2_xattr_remove()
1603 *
1604 * Free extended attribute resources associated with this inode.
1605 */
1606int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1607{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001608 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1609 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1610 handle_t *handle;
1611 int ret;
1612
Tiger Yang8154da32008-08-18 17:11:46 +08001613 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1614 return 0;
1615
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001616 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1617 return 0;
1618
1619 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1620 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1621 if (ret < 0) {
1622 mlog_errno(ret);
1623 goto out;
1624 }
1625 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001626
Tao Ma08413892008-08-29 09:00:19 +08001627 if (di->i_xattr_loc) {
1628 ret = ocfs2_xattr_free_block(inode,
1629 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001630 if (ret < 0) {
1631 mlog_errno(ret);
1632 goto out;
1633 }
1634 }
1635
1636 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1637 OCFS2_INODE_UPDATE_CREDITS);
1638 if (IS_ERR(handle)) {
1639 ret = PTR_ERR(handle);
1640 mlog_errno(ret);
1641 goto out;
1642 }
1643 ret = ocfs2_journal_access(handle, inode, di_bh,
1644 OCFS2_JOURNAL_ACCESS_WRITE);
1645 if (ret) {
1646 mlog_errno(ret);
1647 goto out_commit;
1648 }
1649
Tao Ma08413892008-08-29 09:00:19 +08001650 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001651
1652 spin_lock(&oi->ip_lock);
1653 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1654 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1655 spin_unlock(&oi->ip_lock);
1656
1657 ret = ocfs2_journal_dirty(handle, di_bh);
1658 if (ret < 0)
1659 mlog_errno(ret);
1660out_commit:
1661 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1662out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001663 return ret;
1664}
1665
1666static int ocfs2_xattr_has_space_inline(struct inode *inode,
1667 struct ocfs2_dinode *di)
1668{
1669 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1670 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1671 int free;
1672
1673 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1674 return 0;
1675
1676 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1677 struct ocfs2_inline_data *idata = &di->id2.i_data;
1678 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1679 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1680 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1681 le64_to_cpu(di->i_size);
1682 } else {
1683 struct ocfs2_extent_list *el = &di->id2.i_list;
1684 free = (le16_to_cpu(el->l_count) -
1685 le16_to_cpu(el->l_next_free_rec)) *
1686 sizeof(struct ocfs2_extent_rec);
1687 }
1688 if (free >= xattrsize)
1689 return 1;
1690
1691 return 0;
1692}
1693
1694/*
1695 * ocfs2_xattr_ibody_find()
1696 *
1697 * Find extended attribute in inode block and
1698 * fill search info into struct ocfs2_xattr_search.
1699 */
1700static int ocfs2_xattr_ibody_find(struct inode *inode,
1701 int name_index,
1702 const char *name,
1703 struct ocfs2_xattr_search *xs)
1704{
1705 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1706 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1707 int ret;
1708 int has_space = 0;
1709
1710 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1711 return 0;
1712
1713 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1714 down_read(&oi->ip_alloc_sem);
1715 has_space = ocfs2_xattr_has_space_inline(inode, di);
1716 up_read(&oi->ip_alloc_sem);
1717 if (!has_space)
1718 return 0;
1719 }
1720
1721 xs->xattr_bh = xs->inode_bh;
1722 xs->end = (void *)di + inode->i_sb->s_blocksize;
1723 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1724 xs->header = (struct ocfs2_xattr_header *)
1725 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1726 else
1727 xs->header = (struct ocfs2_xattr_header *)
1728 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1729 xs->base = (void *)xs->header;
1730 xs->here = xs->header->xh_entries;
1731
1732 /* Find the named attribute. */
1733 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1734 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1735 if (ret && ret != -ENODATA)
1736 return ret;
1737 xs->not_found = ret;
1738 }
1739
1740 return 0;
1741}
1742
1743/*
1744 * ocfs2_xattr_ibody_set()
1745 *
1746 * Set, replace or remove an extended attribute into inode block.
1747 *
1748 */
1749static int ocfs2_xattr_ibody_set(struct inode *inode,
1750 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08001751 struct ocfs2_xattr_search *xs,
1752 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001753{
1754 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1755 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1756 int ret;
1757
1758 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1759 return -ENOSPC;
1760
1761 down_write(&oi->ip_alloc_sem);
1762 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1763 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1764 ret = -ENOSPC;
1765 goto out;
1766 }
1767 }
1768
Tao Ma78f30c32008-11-12 08:27:00 +08001769 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001770 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1771out:
1772 up_write(&oi->ip_alloc_sem);
1773
1774 return ret;
1775}
1776
1777/*
1778 * ocfs2_xattr_block_find()
1779 *
1780 * Find extended attribute in external block and
1781 * fill search info into struct ocfs2_xattr_search.
1782 */
1783static int ocfs2_xattr_block_find(struct inode *inode,
1784 int name_index,
1785 const char *name,
1786 struct ocfs2_xattr_search *xs)
1787{
1788 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1789 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08001790 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001791 int ret = 0;
1792
1793 if (!di->i_xattr_loc)
1794 return ret;
1795
Joel Becker0fcaa562008-10-09 17:20:31 -07001796 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001797 if (ret < 0) {
1798 mlog_errno(ret);
1799 return ret;
1800 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07001801
1802 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1803 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1804 ret = -EIO;
1805 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001806 }
1807
1808 xs->xattr_bh = blk_bh;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001809
Tao Ma589dc262008-08-18 17:38:51 +08001810 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1811 xs->header = &xb->xb_attrs.xb_header;
1812 xs->base = (void *)xs->header;
1813 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1814 xs->here = xs->header->xh_entries;
1815
1816 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1817 } else
1818 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1819 name_index,
1820 name, xs);
1821
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001822 if (ret && ret != -ENODATA) {
1823 xs->xattr_bh = NULL;
1824 goto cleanup;
1825 }
1826 xs->not_found = ret;
1827 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001828cleanup:
1829 brelse(blk_bh);
1830
1831 return ret;
1832}
1833
1834/*
1835 * ocfs2_xattr_block_set()
1836 *
1837 * Set, replace or remove an extended attribute into external block.
1838 *
1839 */
1840static int ocfs2_xattr_block_set(struct inode *inode,
1841 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08001842 struct ocfs2_xattr_search *xs,
1843 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001844{
1845 struct buffer_head *new_bh = NULL;
1846 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1847 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08001848 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001849 struct ocfs2_xattr_block *xblk = NULL;
1850 u16 suballoc_bit_start;
1851 u32 num_got;
1852 u64 first_blkno;
1853 int ret;
1854
1855 if (!xs->xattr_bh) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001856 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1857 OCFS2_JOURNAL_ACCESS_CREATE);
1858 if (ret < 0) {
1859 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001860 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001861 }
1862
Tao Ma78f30c32008-11-12 08:27:00 +08001863 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001864 &suballoc_bit_start, &num_got,
1865 &first_blkno);
1866 if (ret < 0) {
1867 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001868 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001869 }
1870
1871 new_bh = sb_getblk(inode->i_sb, first_blkno);
1872 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1873
1874 ret = ocfs2_journal_access(handle, inode, new_bh,
1875 OCFS2_JOURNAL_ACCESS_CREATE);
1876 if (ret < 0) {
1877 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001878 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001879 }
1880
1881 /* Initialize ocfs2_xattr_block */
1882 xs->xattr_bh = new_bh;
1883 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1884 memset(xblk, 0, inode->i_sb->s_blocksize);
1885 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1886 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1887 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1888 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1889 xblk->xb_blkno = cpu_to_le64(first_blkno);
1890
1891 xs->header = &xblk->xb_attrs.xb_header;
1892 xs->base = (void *)xs->header;
1893 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1894 xs->here = xs->header->xh_entries;
1895
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001896 ret = ocfs2_journal_dirty(handle, new_bh);
1897 if (ret < 0) {
1898 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001899 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001900 }
1901 di->i_xattr_loc = cpu_to_le64(first_blkno);
Tao Ma85db90e2008-11-12 08:27:01 +08001902 ocfs2_journal_dirty(handle, xs->inode_bh);
Tao Ma01225592008-08-18 17:38:53 +08001903 } else
1904 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1905
1906 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1907 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08001908 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
1909 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08001910 if (!ret || ret != -ENOSPC)
1911 goto end;
1912
Tao Ma78f30c32008-11-12 08:27:00 +08001913 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08001914 if (ret)
1915 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001916 }
1917
Tao Ma78f30c32008-11-12 08:27:00 +08001918 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08001919
1920end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001921
1922 return ret;
1923}
1924
Tao Ma78f30c32008-11-12 08:27:00 +08001925/* Check whether the new xattr can be inserted into the inode. */
1926static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
1927 struct ocfs2_xattr_info *xi,
1928 struct ocfs2_xattr_search *xs)
1929{
1930 u64 value_size;
1931 struct ocfs2_xattr_entry *last;
1932 int free, i;
1933 size_t min_offs = xs->end - xs->base;
1934
1935 if (!xs->header)
1936 return 0;
1937
1938 last = xs->header->xh_entries;
1939
1940 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
1941 size_t offs = le16_to_cpu(last->xe_name_offset);
1942 if (offs < min_offs)
1943 min_offs = offs;
1944 last += 1;
1945 }
1946
1947 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1948 if (free < 0)
1949 return 0;
1950
1951 BUG_ON(!xs->not_found);
1952
1953 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
1954 value_size = OCFS2_XATTR_ROOT_SIZE;
1955 else
1956 value_size = OCFS2_XATTR_SIZE(xi->value_len);
1957
1958 if (free >= sizeof(struct ocfs2_xattr_entry) +
1959 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
1960 return 1;
1961
1962 return 0;
1963}
1964
1965static int ocfs2_calc_xattr_set_need(struct inode *inode,
1966 struct ocfs2_dinode *di,
1967 struct ocfs2_xattr_info *xi,
1968 struct ocfs2_xattr_search *xis,
1969 struct ocfs2_xattr_search *xbs,
1970 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08001971 int *meta_need,
1972 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08001973{
1974 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001975 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08001976 struct buffer_head *bh = NULL;
1977 struct ocfs2_xattr_block *xb = NULL;
1978 struct ocfs2_xattr_entry *xe = NULL;
1979 struct ocfs2_xattr_value_root *xv = NULL;
1980 char *base = NULL;
1981 int name_offset, name_len = 0;
1982 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
1983 xi->value_len);
1984 u64 value_size;
1985
Tao Ma78f30c32008-11-12 08:27:00 +08001986 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08001987 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1988
1989 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08001990 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08001991 credits += ocfs2_calc_extend_credits(inode->i_sb,
1992 &def_xv.xv.xr_list,
1993 new_clusters);
1994 }
Tao Ma78f30c32008-11-12 08:27:00 +08001995
1996 goto meta_guess;
1997 }
1998
1999 if (!xis->not_found) {
2000 xe = xis->here;
2001 name_offset = le16_to_cpu(xe->xe_name_offset);
2002 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2003 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002004 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002005 } else {
2006 int i, block_off;
2007 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2008 xe = xbs->here;
2009 name_offset = le16_to_cpu(xe->xe_name_offset);
2010 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2011 i = xbs->here - xbs->header->xh_entries;
2012 old_in_xb = 1;
2013
2014 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2015 ret = ocfs2_xattr_bucket_get_name_value(inode,
2016 bucket_xh(xbs->bucket),
2017 i, &block_off,
2018 &name_offset);
2019 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002020 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2021 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002022 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002023 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2024 }
2025 }
2026
2027 /*
2028 * delete a xattr doesn't need metadata and cluster allocation.
2029 * so just calculate the credits and return.
2030 *
2031 * The credits for removing the value tree will be extended
2032 * by ocfs2_remove_extent itself.
2033 */
2034 if (!xi->value) {
2035 if (!ocfs2_xattr_is_local(xe))
2036 credits += OCFS2_REMOVE_EXTENT_CREDITS;
2037
2038 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002039 }
2040
2041 /* do cluster allocation guess first. */
2042 value_size = le64_to_cpu(xe->xe_value_size);
2043
2044 if (old_in_xb) {
2045 /*
2046 * In xattr set, we always try to set the xe in inode first,
2047 * so if it can be inserted into inode successfully, the old
2048 * one will be removed from the xattr block, and this xattr
2049 * will be inserted into inode as a new xattr in inode.
2050 */
2051 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2052 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002053 credits += OCFS2_REMOVE_EXTENT_CREDITS +
2054 OCFS2_INODE_UPDATE_CREDITS;
2055 if (!ocfs2_xattr_is_local(xe))
2056 credits += ocfs2_calc_extend_credits(
2057 inode->i_sb,
2058 &def_xv.xv.xr_list,
2059 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002060 goto out;
2061 }
2062 }
2063
2064 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2065 /* the new values will be stored outside. */
2066 u32 old_clusters = 0;
2067
2068 if (!ocfs2_xattr_is_local(xe)) {
2069 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2070 value_size);
2071 xv = (struct ocfs2_xattr_value_root *)
2072 (base + name_offset + name_len);
2073 } else
2074 xv = &def_xv.xv;
2075
Tao Ma85db90e2008-11-12 08:27:01 +08002076 if (old_clusters >= new_clusters) {
2077 credits += OCFS2_REMOVE_EXTENT_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002078 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002079 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002080 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2081 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002082 credits += ocfs2_calc_extend_credits(inode->i_sb,
2083 &xv->xr_list,
2084 new_clusters -
2085 old_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002086 goto out;
2087 }
2088 } else {
2089 /*
2090 * Now the new value will be stored inside. So if the new
2091 * value is smaller than the size of value root or the old
2092 * value, we don't need any allocation, otherwise we have
2093 * to guess metadata allocation.
2094 */
2095 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2096 (!ocfs2_xattr_is_local(xe) &&
2097 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2098 goto out;
2099 }
2100
2101meta_guess:
2102 /* calculate metadata allocation. */
2103 if (di->i_xattr_loc) {
2104 if (!xbs->xattr_bh) {
2105 ret = ocfs2_read_block(inode,
2106 le64_to_cpu(di->i_xattr_loc),
2107 &bh);
2108 if (ret) {
2109 mlog_errno(ret);
2110 goto out;
2111 }
2112
2113 xb = (struct ocfs2_xattr_block *)bh->b_data;
2114 } else
2115 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2116
2117 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2118 struct ocfs2_extent_list *el =
2119 &xb->xb_attrs.xb_root.xt_list;
2120 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002121 credits += ocfs2_calc_extend_credits(inode->i_sb,
2122 el, 1);
Tao Ma78f30c32008-11-12 08:27:00 +08002123 }
2124
2125 /*
2126 * This cluster will be used either for new bucket or for
2127 * new xattr block.
2128 * If the cluster size is the same as the bucket size, one
2129 * more is needed since we may need to extend the bucket
2130 * also.
2131 */
2132 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002133 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002134 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002135 OCFS2_SB(inode->i_sb)->s_clustersize) {
2136 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002137 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002138 }
2139 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002140 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002141 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2142 }
Tao Ma78f30c32008-11-12 08:27:00 +08002143out:
2144 if (clusters_need)
2145 *clusters_need = clusters_add;
2146 if (meta_need)
2147 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002148 if (credits_need)
2149 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002150 brelse(bh);
2151 return ret;
2152}
2153
2154static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2155 struct ocfs2_dinode *di,
2156 struct ocfs2_xattr_info *xi,
2157 struct ocfs2_xattr_search *xis,
2158 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002159 struct ocfs2_xattr_set_ctxt *ctxt,
2160 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002161{
2162 int clusters_add, meta_add, ret;
2163 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2164
2165 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2166
2167 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2168
2169 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002170 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002171 if (ret) {
2172 mlog_errno(ret);
2173 return ret;
2174 }
2175
Tao Ma85db90e2008-11-12 08:27:01 +08002176 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2177 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002178
2179 if (meta_add) {
2180 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2181 &ctxt->meta_ac);
2182 if (ret) {
2183 mlog_errno(ret);
2184 goto out;
2185 }
2186 }
2187
2188 if (clusters_add) {
2189 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2190 if (ret)
2191 mlog_errno(ret);
2192 }
2193out:
2194 if (ret) {
2195 if (ctxt->meta_ac) {
2196 ocfs2_free_alloc_context(ctxt->meta_ac);
2197 ctxt->meta_ac = NULL;
2198 }
2199
2200 /*
2201 * We cannot have an error and a non null ctxt->data_ac.
2202 */
2203 }
2204
2205 return ret;
2206}
2207
Tao Ma85db90e2008-11-12 08:27:01 +08002208static int __ocfs2_xattr_set_handle(struct inode *inode,
2209 struct ocfs2_dinode *di,
2210 struct ocfs2_xattr_info *xi,
2211 struct ocfs2_xattr_search *xis,
2212 struct ocfs2_xattr_search *xbs,
2213 struct ocfs2_xattr_set_ctxt *ctxt)
2214{
2215 int ret = 0, credits;
2216
2217 if (!xi->value) {
2218 /* Remove existing extended attribute */
2219 if (!xis->not_found)
2220 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2221 else if (!xbs->not_found)
2222 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2223 } else {
2224 /* We always try to set extended attribute into inode first*/
2225 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2226 if (!ret && !xbs->not_found) {
2227 /*
2228 * If succeed and that extended attribute existing in
2229 * external block, then we will remove it.
2230 */
2231 xi->value = NULL;
2232 xi->value_len = 0;
2233
2234 xis->not_found = -ENODATA;
2235 ret = ocfs2_calc_xattr_set_need(inode,
2236 di,
2237 xi,
2238 xis,
2239 xbs,
2240 NULL,
2241 NULL,
2242 &credits);
2243 if (ret) {
2244 mlog_errno(ret);
2245 goto out;
2246 }
2247
2248 ret = ocfs2_extend_trans(ctxt->handle, credits +
2249 ctxt->handle->h_buffer_credits);
2250 if (ret) {
2251 mlog_errno(ret);
2252 goto out;
2253 }
2254 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2255 } else if (ret == -ENOSPC) {
2256 if (di->i_xattr_loc && !xbs->xattr_bh) {
2257 ret = ocfs2_xattr_block_find(inode,
2258 xi->name_index,
2259 xi->name, xbs);
2260 if (ret)
2261 goto out;
2262
2263 xis->not_found = -ENODATA;
2264 ret = ocfs2_calc_xattr_set_need(inode,
2265 di,
2266 xi,
2267 xis,
2268 xbs,
2269 NULL,
2270 NULL,
2271 &credits);
2272 if (ret) {
2273 mlog_errno(ret);
2274 goto out;
2275 }
2276
2277 ret = ocfs2_extend_trans(ctxt->handle, credits +
2278 ctxt->handle->h_buffer_credits);
2279 if (ret) {
2280 mlog_errno(ret);
2281 goto out;
2282 }
2283 }
2284 /*
2285 * If no space in inode, we will set extended attribute
2286 * into external block.
2287 */
2288 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2289 if (ret)
2290 goto out;
2291 if (!xis->not_found) {
2292 /*
2293 * If succeed and that extended attribute
2294 * existing in inode, we will remove it.
2295 */
2296 xi->value = NULL;
2297 xi->value_len = 0;
2298 xbs->not_found = -ENODATA;
2299 ret = ocfs2_calc_xattr_set_need(inode,
2300 di,
2301 xi,
2302 xis,
2303 xbs,
2304 NULL,
2305 NULL,
2306 &credits);
2307 if (ret) {
2308 mlog_errno(ret);
2309 goto out;
2310 }
2311
2312 ret = ocfs2_extend_trans(ctxt->handle, credits +
2313 ctxt->handle->h_buffer_credits);
2314 if (ret) {
2315 mlog_errno(ret);
2316 goto out;
2317 }
2318 ret = ocfs2_xattr_ibody_set(inode, xi,
2319 xis, ctxt);
2320 }
2321 }
2322 }
2323
2324out:
2325 return ret;
2326}
2327
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002328/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002329 * This function only called duing creating inode
2330 * for init security/acl xattrs of the new inode.
2331 * The xattrs could be put into ibody or extent block,
2332 * xattr bucket would not be use in this case.
2333 * transanction credits also be reserved in here.
2334 */
2335int ocfs2_xattr_set_handle(handle_t *handle,
2336 struct inode *inode,
2337 struct buffer_head *di_bh,
2338 int name_index,
2339 const char *name,
2340 const void *value,
2341 size_t value_len,
2342 int flags,
2343 struct ocfs2_alloc_context *meta_ac,
2344 struct ocfs2_alloc_context *data_ac)
2345{
2346 struct ocfs2_dinode *di;
2347 int ret;
2348
2349 struct ocfs2_xattr_info xi = {
2350 .name_index = name_index,
2351 .name = name,
2352 .value = value,
2353 .value_len = value_len,
2354 };
2355
2356 struct ocfs2_xattr_search xis = {
2357 .not_found = -ENODATA,
2358 };
2359
2360 struct ocfs2_xattr_search xbs = {
2361 .not_found = -ENODATA,
2362 };
2363
2364 struct ocfs2_xattr_set_ctxt ctxt = {
2365 .handle = handle,
2366 .meta_ac = meta_ac,
2367 .data_ac = data_ac,
2368 };
2369
2370 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2371 return -EOPNOTSUPP;
2372
2373 xis.inode_bh = xbs.inode_bh = di_bh;
2374 di = (struct ocfs2_dinode *)di_bh->b_data;
2375
2376 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2377
2378 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2379 if (ret)
2380 goto cleanup;
2381 if (xis.not_found) {
2382 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2383 if (ret)
2384 goto cleanup;
2385 }
2386
2387 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2388
2389cleanup:
2390 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2391 brelse(xbs.xattr_bh);
2392
2393 return ret;
2394}
2395
2396/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002397 * ocfs2_xattr_set()
2398 *
2399 * Set, replace or remove an extended attribute for this inode.
2400 * value is NULL to remove an existing extended attribute, else either
2401 * create or replace an extended attribute.
2402 */
2403int ocfs2_xattr_set(struct inode *inode,
2404 int name_index,
2405 const char *name,
2406 const void *value,
2407 size_t value_len,
2408 int flags)
2409{
2410 struct buffer_head *di_bh = NULL;
2411 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002412 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002413 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002414 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002415 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002416
2417 struct ocfs2_xattr_info xi = {
2418 .name_index = name_index,
2419 .name = name,
2420 .value = value,
2421 .value_len = value_len,
2422 };
2423
2424 struct ocfs2_xattr_search xis = {
2425 .not_found = -ENODATA,
2426 };
2427
2428 struct ocfs2_xattr_search xbs = {
2429 .not_found = -ENODATA,
2430 };
2431
Tiger Yang8154da32008-08-18 17:11:46 +08002432 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2433 return -EOPNOTSUPP;
2434
Joel Beckerba937122008-10-24 19:13:20 -07002435 /*
2436 * Only xbs will be used on indexed trees. xis doesn't need a
2437 * bucket.
2438 */
2439 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2440 if (!xbs.bucket) {
2441 mlog_errno(-ENOMEM);
2442 return -ENOMEM;
2443 }
2444
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002445 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2446 if (ret < 0) {
2447 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002448 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002449 }
2450 xis.inode_bh = xbs.inode_bh = di_bh;
2451 di = (struct ocfs2_dinode *)di_bh->b_data;
2452
2453 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2454 /*
2455 * Scan inode and external block to find the same name
2456 * extended attribute and collect search infomation.
2457 */
2458 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2459 if (ret)
2460 goto cleanup;
2461 if (xis.not_found) {
2462 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2463 if (ret)
2464 goto cleanup;
2465 }
2466
2467 if (xis.not_found && xbs.not_found) {
2468 ret = -ENODATA;
2469 if (flags & XATTR_REPLACE)
2470 goto cleanup;
2471 ret = 0;
2472 if (!value)
2473 goto cleanup;
2474 } else {
2475 ret = -EEXIST;
2476 if (flags & XATTR_CREATE)
2477 goto cleanup;
2478 }
2479
Tao Ma85db90e2008-11-12 08:27:01 +08002480
2481 mutex_lock(&tl_inode->i_mutex);
2482
2483 if (ocfs2_truncate_log_needs_flush(osb)) {
2484 ret = __ocfs2_flush_truncate_log(osb);
2485 if (ret < 0) {
2486 mutex_unlock(&tl_inode->i_mutex);
2487 mlog_errno(ret);
2488 goto cleanup;
2489 }
2490 }
2491 mutex_unlock(&tl_inode->i_mutex);
2492
2493 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2494 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002495 if (ret) {
2496 mlog_errno(ret);
2497 goto cleanup;
2498 }
2499
Tao Ma85db90e2008-11-12 08:27:01 +08002500 ctxt.handle = ocfs2_start_trans(osb, credits);
2501 if (IS_ERR(ctxt.handle)) {
2502 ret = PTR_ERR(ctxt.handle);
2503 mlog_errno(ret);
2504 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002505 }
Tao Ma85db90e2008-11-12 08:27:01 +08002506
2507 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2508
2509 ocfs2_commit_trans(osb, ctxt.handle);
2510
Tao Ma78f30c32008-11-12 08:27:00 +08002511 if (ctxt.data_ac)
2512 ocfs2_free_alloc_context(ctxt.data_ac);
2513 if (ctxt.meta_ac)
2514 ocfs2_free_alloc_context(ctxt.meta_ac);
2515 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2516 ocfs2_schedule_truncate_log_flush(osb, 1);
2517 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002518cleanup:
2519 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2520 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002521cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002522 brelse(di_bh);
2523 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002524 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002525
2526 return ret;
2527}
2528
Tao Ma0c044f02008-08-18 17:38:50 +08002529/*
2530 * Find the xattr extent rec which may contains name_hash.
2531 * e_cpos will be the first name hash of the xattr rec.
2532 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2533 */
2534static int ocfs2_xattr_get_rec(struct inode *inode,
2535 u32 name_hash,
2536 u64 *p_blkno,
2537 u32 *e_cpos,
2538 u32 *num_clusters,
2539 struct ocfs2_extent_list *el)
2540{
2541 int ret = 0, i;
2542 struct buffer_head *eb_bh = NULL;
2543 struct ocfs2_extent_block *eb;
2544 struct ocfs2_extent_rec *rec = NULL;
2545 u64 e_blkno = 0;
2546
2547 if (el->l_tree_depth) {
2548 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2549 if (ret) {
2550 mlog_errno(ret);
2551 goto out;
2552 }
2553
2554 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2555 el = &eb->h_list;
2556
2557 if (el->l_tree_depth) {
2558 ocfs2_error(inode->i_sb,
2559 "Inode %lu has non zero tree depth in "
2560 "xattr tree block %llu\n", inode->i_ino,
2561 (unsigned long long)eb_bh->b_blocknr);
2562 ret = -EROFS;
2563 goto out;
2564 }
2565 }
2566
2567 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2568 rec = &el->l_recs[i];
2569
2570 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2571 e_blkno = le64_to_cpu(rec->e_blkno);
2572 break;
2573 }
2574 }
2575
2576 if (!e_blkno) {
2577 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2578 "record (%u, %u, 0) in xattr", inode->i_ino,
2579 le32_to_cpu(rec->e_cpos),
2580 ocfs2_rec_clusters(el, rec));
2581 ret = -EROFS;
2582 goto out;
2583 }
2584
2585 *p_blkno = le64_to_cpu(rec->e_blkno);
2586 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2587 if (e_cpos)
2588 *e_cpos = le32_to_cpu(rec->e_cpos);
2589out:
2590 brelse(eb_bh);
2591 return ret;
2592}
2593
2594typedef int (xattr_bucket_func)(struct inode *inode,
2595 struct ocfs2_xattr_bucket *bucket,
2596 void *para);
2597
Tao Ma589dc262008-08-18 17:38:51 +08002598static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002599 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002600 int name_index,
2601 const char *name,
2602 u32 name_hash,
2603 u16 *xe_index,
2604 int *found)
2605{
2606 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002607 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002608 size_t name_len = strlen(name);
2609 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002610 char *xe_name;
2611
2612 /*
2613 * We don't use binary search in the bucket because there
2614 * may be multiple entries with the same name hash.
2615 */
2616 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2617 xe = &xh->xh_entries[i];
2618
2619 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2620 continue;
2621 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2622 break;
2623
2624 cmp = name_index - ocfs2_xattr_get_type(xe);
2625 if (!cmp)
2626 cmp = name_len - xe->xe_name_len;
2627 if (cmp)
2628 continue;
2629
2630 ret = ocfs2_xattr_bucket_get_name_value(inode,
2631 xh,
2632 i,
2633 &block_off,
2634 &new_offset);
2635 if (ret) {
2636 mlog_errno(ret);
2637 break;
2638 }
2639
Joel Beckere2356a32008-10-27 15:01:54 -07002640 xe_name = bucket_block(bucket, block_off) + new_offset;
2641 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002642 *xe_index = i;
2643 *found = 1;
2644 ret = 0;
2645 break;
2646 }
2647 }
2648
2649 return ret;
2650}
2651
2652/*
2653 * Find the specified xattr entry in a series of buckets.
2654 * This series start from p_blkno and last for num_clusters.
2655 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2656 * the num of the valid buckets.
2657 *
2658 * Return the buffer_head this xattr should reside in. And if the xattr's
2659 * hash is in the gap of 2 buckets, return the lower bucket.
2660 */
2661static int ocfs2_xattr_bucket_find(struct inode *inode,
2662 int name_index,
2663 const char *name,
2664 u32 name_hash,
2665 u64 p_blkno,
2666 u32 first_hash,
2667 u32 num_clusters,
2668 struct ocfs2_xattr_search *xs)
2669{
2670 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002671 struct ocfs2_xattr_header *xh = NULL;
2672 struct ocfs2_xattr_entry *xe = NULL;
2673 u16 index = 0;
2674 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2675 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002676 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002677 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002678 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002679
Joel Beckere2356a32008-10-27 15:01:54 -07002680 search = ocfs2_xattr_bucket_new(inode);
2681 if (!search) {
2682 ret = -ENOMEM;
2683 mlog_errno(ret);
2684 goto out;
2685 }
2686
2687 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002688 if (ret) {
2689 mlog_errno(ret);
2690 goto out;
2691 }
2692
Joel Beckere2356a32008-10-27 15:01:54 -07002693 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002694 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002695 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002696 ocfs2_xattr_bucket_relse(search);
2697
Tao Ma589dc262008-08-18 17:38:51 +08002698 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002699 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002700 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002701 if (ret) {
2702 mlog_errno(ret);
2703 goto out;
2704 }
2705
Joel Beckere2356a32008-10-27 15:01:54 -07002706 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002707 xe = &xh->xh_entries[0];
2708 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2709 high_bucket = bucket - 1;
2710 continue;
2711 }
2712
2713 /*
2714 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08002715 * bucket is larger than the search one. for an empty
2716 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002717 */
Tao Ma5a095612008-09-19 22:17:41 +08002718 if (xh->xh_count)
2719 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2720
Tao Ma589dc262008-08-18 17:38:51 +08002721 last_hash = le32_to_cpu(xe->xe_name_hash);
2722
Joel Beckere2356a32008-10-27 15:01:54 -07002723 /* record lower_blkno which may be the insert place. */
2724 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002725
2726 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2727 low_bucket = bucket + 1;
2728 continue;
2729 }
2730
2731 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002732 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002733 name_index, name, name_hash,
2734 &index, &found);
2735 if (ret) {
2736 mlog_errno(ret);
2737 goto out;
2738 }
2739 break;
2740 }
2741
2742 /*
2743 * Record the bucket we have found.
2744 * When the xattr's hash value is in the gap of 2 buckets, we will
2745 * always set it to the previous bucket.
2746 */
Joel Beckere2356a32008-10-27 15:01:54 -07002747 if (!lower_blkno)
2748 lower_blkno = p_blkno;
2749
2750 /* This should be in cache - we just read it during the search */
2751 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2752 if (ret) {
2753 mlog_errno(ret);
2754 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08002755 }
Tao Ma589dc262008-08-18 17:38:51 +08002756
Joel Beckerba937122008-10-24 19:13:20 -07002757 xs->header = bucket_xh(xs->bucket);
2758 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002759 xs->end = xs->base + inode->i_sb->s_blocksize;
2760
2761 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08002762 xs->here = &xs->header->xh_entries[index];
2763 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07002764 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08002765 } else
2766 ret = -ENODATA;
2767
2768out:
Joel Beckere2356a32008-10-27 15:01:54 -07002769 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08002770 return ret;
2771}
2772
2773static int ocfs2_xattr_index_block_find(struct inode *inode,
2774 struct buffer_head *root_bh,
2775 int name_index,
2776 const char *name,
2777 struct ocfs2_xattr_search *xs)
2778{
2779 int ret;
2780 struct ocfs2_xattr_block *xb =
2781 (struct ocfs2_xattr_block *)root_bh->b_data;
2782 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2783 struct ocfs2_extent_list *el = &xb_root->xt_list;
2784 u64 p_blkno = 0;
2785 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08002786 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08002787
2788 if (le16_to_cpu(el->l_next_free_rec) == 0)
2789 return -ENODATA;
2790
2791 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2792 name, name_hash, name_index);
2793
2794 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2795 &num_clusters, el);
2796 if (ret) {
2797 mlog_errno(ret);
2798 goto out;
2799 }
2800
2801 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2802
2803 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07002804 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2805 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08002806
2807 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2808 p_blkno, first_hash, num_clusters, xs);
2809
2810out:
2811 return ret;
2812}
2813
Tao Ma0c044f02008-08-18 17:38:50 +08002814static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2815 u64 blkno,
2816 u32 clusters,
2817 xattr_bucket_func *func,
2818 void *para)
2819{
Joel Becker6dde41d2008-10-24 17:16:48 -07002820 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08002821 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2822 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07002823 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08002824
Joel Beckerba937122008-10-24 19:13:20 -07002825 bucket = ocfs2_xattr_bucket_new(inode);
2826 if (!bucket) {
2827 mlog_errno(-ENOMEM);
2828 return -ENOMEM;
2829 }
Tao Ma0c044f02008-08-18 17:38:50 +08002830
2831 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07002832 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002833
Joel Beckerba937122008-10-24 19:13:20 -07002834 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
2835 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002836 if (ret) {
2837 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002838 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002839 }
2840
Tao Ma0c044f02008-08-18 17:38:50 +08002841 /*
2842 * The real bucket num in this series of blocks is stored
2843 * in the 1st bucket.
2844 */
2845 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07002846 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08002847
Mark Fashehde29c082008-10-29 14:45:30 -07002848 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2849 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07002850 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08002851 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07002852 ret = func(inode, bucket, para);
2853 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08002854 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002855 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08002856 }
2857
Joel Beckerba937122008-10-24 19:13:20 -07002858 ocfs2_xattr_bucket_relse(bucket);
2859 if (ret)
2860 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002861 }
2862
Joel Beckerba937122008-10-24 19:13:20 -07002863 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08002864 return ret;
2865}
2866
2867struct ocfs2_xattr_tree_list {
2868 char *buffer;
2869 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08002870 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08002871};
2872
2873static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2874 struct ocfs2_xattr_header *xh,
2875 int index,
2876 int *block_off,
2877 int *new_offset)
2878{
2879 u16 name_offset;
2880
2881 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2882 return -EINVAL;
2883
2884 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2885
2886 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2887 *new_offset = name_offset % inode->i_sb->s_blocksize;
2888
2889 return 0;
2890}
2891
2892static int ocfs2_list_xattr_bucket(struct inode *inode,
2893 struct ocfs2_xattr_bucket *bucket,
2894 void *para)
2895{
Tao Ma936b8832008-10-09 23:06:14 +08002896 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08002897 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08002898 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08002899 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08002900
Joel Becker3e632942008-10-24 17:04:49 -07002901 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
2902 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08002903 type = ocfs2_xattr_get_type(entry);
2904 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08002905
Tao Ma936b8832008-10-09 23:06:14 +08002906 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08002907 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07002908 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08002909 i,
2910 &block_off,
2911 &new_offset);
2912 if (ret)
2913 break;
Tao Ma936b8832008-10-09 23:06:14 +08002914
Joel Becker51def392008-10-24 16:57:21 -07002915 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08002916 new_offset;
2917 ret = ocfs2_xattr_list_entry(xl->buffer,
2918 xl->buffer_size,
2919 &xl->result,
2920 prefix, name,
2921 entry->xe_name_len);
2922 if (ret)
2923 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002924 }
2925 }
2926
2927 return ret;
2928}
2929
2930static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2931 struct ocfs2_xattr_tree_root *xt,
2932 char *buffer,
2933 size_t buffer_size)
2934{
2935 struct ocfs2_extent_list *el = &xt->xt_list;
2936 int ret = 0;
2937 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2938 u64 p_blkno = 0;
2939 struct ocfs2_xattr_tree_list xl = {
2940 .buffer = buffer,
2941 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08002942 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08002943 };
2944
2945 if (le16_to_cpu(el->l_next_free_rec) == 0)
2946 return 0;
2947
2948 while (name_hash > 0) {
2949 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2950 &e_cpos, &num_clusters, el);
2951 if (ret) {
2952 mlog_errno(ret);
2953 goto out;
2954 }
2955
2956 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2957 ocfs2_list_xattr_bucket,
2958 &xl);
2959 if (ret) {
2960 mlog_errno(ret);
2961 goto out;
2962 }
2963
2964 if (e_cpos == 0)
2965 break;
2966
2967 name_hash = e_cpos - 1;
2968 }
2969
Tao Ma936b8832008-10-09 23:06:14 +08002970 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08002971out:
2972 return ret;
2973}
Tao Ma01225592008-08-18 17:38:53 +08002974
2975static int cmp_xe(const void *a, const void *b)
2976{
2977 const struct ocfs2_xattr_entry *l = a, *r = b;
2978 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2979 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2980
2981 if (l_hash > r_hash)
2982 return 1;
2983 if (l_hash < r_hash)
2984 return -1;
2985 return 0;
2986}
2987
2988static void swap_xe(void *a, void *b, int size)
2989{
2990 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2991
2992 tmp = *l;
2993 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2994 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2995}
2996
2997/*
2998 * When the ocfs2_xattr_block is filled up, new bucket will be created
2999 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003000 * The header goes at the start of the bucket, and the names+values are
3001 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003002 * Note: we need to sort the entries since they are not saved in order
3003 * in the ocfs2_xattr_block.
3004 */
3005static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3006 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003007 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003008{
3009 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003010 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003011 u16 offset, size, off_change;
3012 struct ocfs2_xattr_entry *xe;
3013 struct ocfs2_xattr_block *xb =
3014 (struct ocfs2_xattr_block *)xb_bh->b_data;
3015 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003016 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003017 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003018 char *src = xb_bh->b_data;
3019 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003020
3021 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3022 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003023 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003024
Joel Becker178eeac2008-10-27 15:18:29 -07003025 for (i = 0; i < blks; i++)
3026 memset(bucket_block(bucket, i), 0, blocksize);
3027
Tao Ma01225592008-08-18 17:38:53 +08003028 /*
3029 * Since the xe_name_offset is based on ocfs2_xattr_header,
3030 * there is a offset change corresponding to the change of
3031 * ocfs2_xattr_header's position.
3032 */
3033 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3034 xe = &xb_xh->xh_entries[count - 1];
3035 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3036 size = blocksize - offset;
3037
3038 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003039 memcpy(target + offset, src + offset, size);
3040
3041 /* Init new header now. */
3042 xh->xh_count = xb_xh->xh_count;
3043 xh->xh_num_buckets = cpu_to_le16(1);
3044 xh->xh_name_value_len = cpu_to_le16(size);
3045 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3046
3047 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003048 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003049 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3050 size = count * sizeof(struct ocfs2_xattr_entry);
3051 memcpy(target + offset, (char *)xb_xh + offset, size);
3052
3053 /* Change the xe offset for all the xe because of the move. */
3054 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3055 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3056 for (i = 0; i < count; i++)
3057 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3058
3059 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3060 offset, size, off_change);
3061
3062 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3063 cmp_xe, swap_xe);
3064}
3065
3066/*
3067 * After we move xattr from block to index btree, we have to
3068 * update ocfs2_xattr_search to the new xe and base.
3069 *
3070 * When the entry is in xattr block, xattr_bh indicates the storage place.
3071 * While if the entry is in index b-tree, "bucket" indicates the
3072 * real place of the xattr.
3073 */
Joel Becker178eeac2008-10-27 15:18:29 -07003074static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3075 struct ocfs2_xattr_search *xs,
3076 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003077{
Tao Ma01225592008-08-18 17:38:53 +08003078 char *buf = old_bh->b_data;
3079 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3080 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003081 int i;
Tao Ma01225592008-08-18 17:38:53 +08003082
Joel Beckerba937122008-10-24 19:13:20 -07003083 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003084 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003085 xs->end = xs->base + inode->i_sb->s_blocksize;
3086
Joel Becker178eeac2008-10-27 15:18:29 -07003087 if (xs->not_found)
3088 return;
Tao Ma01225592008-08-18 17:38:53 +08003089
Joel Becker178eeac2008-10-27 15:18:29 -07003090 i = xs->here - old_xh->xh_entries;
3091 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003092}
3093
3094static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003095 struct ocfs2_xattr_search *xs,
3096 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003097{
Tao Ma85db90e2008-11-12 08:27:01 +08003098 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003099 u32 bit_off, len;
3100 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003101 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003102 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3103 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003104 struct buffer_head *xb_bh = xs->xattr_bh;
3105 struct ocfs2_xattr_block *xb =
3106 (struct ocfs2_xattr_block *)xb_bh->b_data;
3107 struct ocfs2_xattr_tree_root *xr;
3108 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003109
3110 mlog(0, "create xattr index block for %llu\n",
3111 (unsigned long long)xb_bh->b_blocknr);
3112
3113 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003114 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003115
Tao Ma01225592008-08-18 17:38:53 +08003116 /*
3117 * XXX:
3118 * We can use this lock for now, and maybe move to a dedicated mutex
3119 * if performance becomes a problem later.
3120 */
3121 down_write(&oi->ip_alloc_sem);
3122
Tao Ma01225592008-08-18 17:38:53 +08003123 ret = ocfs2_journal_access(handle, inode, xb_bh,
3124 OCFS2_JOURNAL_ACCESS_WRITE);
3125 if (ret) {
3126 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003127 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003128 }
3129
Tao Ma78f30c32008-11-12 08:27:00 +08003130 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3131 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003132 if (ret) {
3133 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003134 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003135 }
3136
3137 /*
3138 * The bucket may spread in many blocks, and
3139 * we will only touch the 1st block and the last block
3140 * in the whole bucket(one for entry and one for data).
3141 */
3142 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3143
Mark Fashehde29c082008-10-29 14:45:30 -07003144 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3145 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003146
Joel Becker178eeac2008-10-27 15:18:29 -07003147 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003148 if (ret) {
3149 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003150 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003151 }
3152
Joel Becker178eeac2008-10-27 15:18:29 -07003153 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3154 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003155 if (ret) {
3156 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003157 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003158 }
Tao Ma01225592008-08-18 17:38:53 +08003159
Joel Becker178eeac2008-10-27 15:18:29 -07003160 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3161 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3162
3163 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3164
Tao Ma01225592008-08-18 17:38:53 +08003165 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3166 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3167 offsetof(struct ocfs2_xattr_block, xb_attrs));
3168
3169 xr = &xb->xb_attrs.xb_root;
3170 xr->xt_clusters = cpu_to_le32(1);
3171 xr->xt_last_eb_blk = 0;
3172 xr->xt_list.l_tree_depth = 0;
3173 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3174 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3175
3176 xr->xt_list.l_recs[0].e_cpos = 0;
3177 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3178 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3179
3180 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3181
Tao Ma85db90e2008-11-12 08:27:01 +08003182 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003183
Tao Ma85db90e2008-11-12 08:27:01 +08003184out:
Tao Ma01225592008-08-18 17:38:53 +08003185 up_write(&oi->ip_alloc_sem);
3186
Tao Ma01225592008-08-18 17:38:53 +08003187 return ret;
3188}
3189
3190static int cmp_xe_offset(const void *a, const void *b)
3191{
3192 const struct ocfs2_xattr_entry *l = a, *r = b;
3193 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3194 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3195
3196 if (l_name_offset < r_name_offset)
3197 return 1;
3198 if (l_name_offset > r_name_offset)
3199 return -1;
3200 return 0;
3201}
3202
3203/*
3204 * defrag a xattr bucket if we find that the bucket has some
3205 * holes beteen name/value pairs.
3206 * We will move all the name/value pairs to the end of the bucket
3207 * so that we can spare some space for insertion.
3208 */
3209static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003210 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003211 struct ocfs2_xattr_bucket *bucket)
3212{
3213 int ret, i;
3214 size_t end, offset, len, value_len;
3215 struct ocfs2_xattr_header *xh;
3216 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003217 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003218 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003219 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003220 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003221
3222 /*
3223 * In order to make the operation more efficient and generic,
3224 * we copy all the blocks into a contiguous memory and do the
3225 * defragment there, so if anything is error, we will not touch
3226 * the real block.
3227 */
3228 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3229 if (!bucket_buf) {
3230 ret = -EIO;
3231 goto out;
3232 }
3233
Joel Becker161d6f32008-10-27 15:25:18 -07003234 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003235 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3236 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003237
Tao Ma1c32a2f2008-11-06 08:10:47 +08003238 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003239 OCFS2_JOURNAL_ACCESS_WRITE);
3240 if (ret < 0) {
3241 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003242 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003243 }
3244
3245 xh = (struct ocfs2_xattr_header *)bucket_buf;
3246 entries = (char *)xh->xh_entries;
3247 xh_free_start = le16_to_cpu(xh->xh_free_start);
3248
3249 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3250 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003251 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3252 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003253
3254 /*
3255 * sort all the entries by their offset.
3256 * the largest will be the first, so that we can
3257 * move them to the end one by one.
3258 */
3259 sort(entries, le16_to_cpu(xh->xh_count),
3260 sizeof(struct ocfs2_xattr_entry),
3261 cmp_xe_offset, swap_xe);
3262
3263 /* Move all name/values to the end of the bucket. */
3264 xe = xh->xh_entries;
3265 end = OCFS2_XATTR_BUCKET_SIZE;
3266 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3267 offset = le16_to_cpu(xe->xe_name_offset);
3268 if (ocfs2_xattr_is_local(xe))
3269 value_len = OCFS2_XATTR_SIZE(
3270 le64_to_cpu(xe->xe_value_size));
3271 else
3272 value_len = OCFS2_XATTR_ROOT_SIZE;
3273 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3274
3275 /*
3276 * We must make sure that the name/value pair
3277 * exist in the same block. So adjust end to
3278 * the previous block end if needed.
3279 */
3280 if (((end - len) / blocksize !=
3281 (end - 1) / blocksize))
3282 end = end - end % blocksize;
3283
3284 if (end > offset + len) {
3285 memmove(bucket_buf + end - len,
3286 bucket_buf + offset, len);
3287 xe->xe_name_offset = cpu_to_le16(end - len);
3288 }
3289
3290 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3291 "bucket %llu\n", (unsigned long long)blkno);
3292
3293 end -= len;
3294 }
3295
3296 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3297 "bucket %llu\n", (unsigned long long)blkno);
3298
3299 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003300 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003301
3302 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3303 xh->xh_free_start = cpu_to_le16(end);
3304
3305 /* sort the entries by their name_hash. */
3306 sort(entries, le16_to_cpu(xh->xh_count),
3307 sizeof(struct ocfs2_xattr_entry),
3308 cmp_xe, swap_xe);
3309
3310 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003311 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3312 memcpy(bucket_block(bucket, i), buf, blocksize);
3313 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003314
Tao Ma01225592008-08-18 17:38:53 +08003315out:
Tao Ma01225592008-08-18 17:38:53 +08003316 kfree(bucket_buf);
3317 return ret;
3318}
3319
3320/*
3321 * Move half nums of the xattr bucket in the previous cluster to this new
3322 * cluster. We only touch the last cluster of the previous extend record.
3323 *
3324 * first_bh is the first buffer_head of a series of bucket in the same
3325 * extent rec and header_bh is the header of one bucket in this cluster.
3326 * They will be updated if we move the data header_bh contains to the new
3327 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3328 */
3329static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3330 handle_t *handle,
3331 struct buffer_head **first_bh,
3332 struct buffer_head **header_bh,
3333 u64 new_blkno,
3334 u64 prev_blkno,
3335 u32 num_clusters,
3336 u32 *first_hash)
3337{
3338 int i, ret, credits;
3339 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3340 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3341 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3342 int blocksize = inode->i_sb->s_blocksize;
3343 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3344 struct ocfs2_xattr_header *new_xh;
3345 struct ocfs2_xattr_header *xh =
3346 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3347
3348 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3349 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3350
3351 prev_bh = *first_bh;
3352 get_bh(prev_bh);
3353 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3354
3355 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3356
3357 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003358 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003359
3360 /*
3361 * We need to update the 1st half of the new cluster and
3362 * 1 more for the update of the 1st bucket of the previous
3363 * extent record.
3364 */
Tao Ma85db90e2008-11-12 08:27:01 +08003365 credits = bpc / 2 + 1 + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003366 ret = ocfs2_extend_trans(handle, credits);
3367 if (ret) {
3368 mlog_errno(ret);
3369 goto out;
3370 }
3371
3372 ret = ocfs2_journal_access(handle, inode, prev_bh,
3373 OCFS2_JOURNAL_ACCESS_WRITE);
3374 if (ret) {
3375 mlog_errno(ret);
3376 goto out;
3377 }
3378
3379 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3380 old_bh = new_bh = NULL;
3381 new_bh = sb_getblk(inode->i_sb, new_blkno);
3382 if (!new_bh) {
3383 ret = -EIO;
3384 mlog_errno(ret);
3385 goto out;
3386 }
3387
3388 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3389
3390 ret = ocfs2_journal_access(handle, inode, new_bh,
3391 OCFS2_JOURNAL_ACCESS_CREATE);
3392 if (ret < 0) {
3393 mlog_errno(ret);
3394 brelse(new_bh);
3395 goto out;
3396 }
3397
Joel Becker0fcaa562008-10-09 17:20:31 -07003398 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
Tao Ma01225592008-08-18 17:38:53 +08003399 if (ret < 0) {
3400 mlog_errno(ret);
3401 brelse(new_bh);
3402 goto out;
3403 }
3404
3405 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3406
3407 if (i == 0) {
3408 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3409 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3410
3411 if (first_hash)
3412 *first_hash = le32_to_cpu(
3413 new_xh->xh_entries[0].xe_name_hash);
3414 new_first_bh = new_bh;
3415 get_bh(new_first_bh);
3416 }
3417
3418 ocfs2_journal_dirty(handle, new_bh);
3419
3420 if (*header_bh == old_bh) {
3421 brelse(*header_bh);
3422 *header_bh = new_bh;
3423 get_bh(*header_bh);
3424
3425 brelse(*first_bh);
3426 *first_bh = new_first_bh;
3427 get_bh(*first_bh);
3428 }
3429 brelse(new_bh);
3430 brelse(old_bh);
3431 }
3432
3433 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3434
3435 ocfs2_journal_dirty(handle, prev_bh);
3436out:
3437 brelse(prev_bh);
3438 brelse(new_first_bh);
3439 return ret;
3440}
3441
Tao Ma01225592008-08-18 17:38:53 +08003442/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003443 * Find the suitable pos when we divide a bucket into 2.
3444 * We have to make sure the xattrs with the same hash value exist
3445 * in the same bucket.
3446 *
3447 * If this ocfs2_xattr_header covers more than one hash value, find a
3448 * place where the hash value changes. Try to find the most even split.
3449 * The most common case is that all entries have different hash values,
3450 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003451 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003452static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3453{
3454 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3455 int count = le16_to_cpu(xh->xh_count);
3456 int delta, middle = count / 2;
3457
3458 /*
3459 * We start at the middle. Each step gets farther away in both
3460 * directions. We therefore hit the change in hash value
3461 * nearest to the middle. Note that this loop does not execute for
3462 * count < 2.
3463 */
3464 for (delta = 0; delta < middle; delta++) {
3465 /* Let's check delta earlier than middle */
3466 if (cmp_xe(&entries[middle - delta - 1],
3467 &entries[middle - delta]))
3468 return middle - delta;
3469
3470 /* For even counts, don't walk off the end */
3471 if ((middle + delta + 1) == count)
3472 continue;
3473
3474 /* Now try delta past middle */
3475 if (cmp_xe(&entries[middle + delta],
3476 &entries[middle + delta + 1]))
3477 return middle + delta + 1;
3478 }
3479
3480 /* Every entry had the same hash */
3481 return count;
3482}
3483
3484/*
3485 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3486 * first_hash will record the 1st hash of the new bucket.
3487 *
3488 * Normally half of the xattrs will be moved. But we have to make
3489 * sure that the xattrs with the same hash value are stored in the
3490 * same bucket. If all the xattrs in this bucket have the same hash
3491 * value, the new bucket will be initialized as an empty one and the
3492 * first_hash will be initialized as (hash_value+1).
3493 */
3494static int ocfs2_divide_xattr_bucket(struct inode *inode,
3495 handle_t *handle,
3496 u64 blk,
3497 u64 new_blk,
3498 u32 *first_hash,
3499 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003500{
3501 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003502 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003503 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003504 struct ocfs2_xattr_header *xh;
3505 struct ocfs2_xattr_entry *xe;
3506 int blocksize = inode->i_sb->s_blocksize;
3507
Tao Ma80bcaf32008-10-27 06:06:24 +08003508 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003509 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003510
Joel Beckerba937122008-10-24 19:13:20 -07003511 s_bucket = ocfs2_xattr_bucket_new(inode);
3512 t_bucket = ocfs2_xattr_bucket_new(inode);
3513 if (!s_bucket || !t_bucket) {
3514 ret = -ENOMEM;
3515 mlog_errno(ret);
3516 goto out;
3517 }
Tao Ma01225592008-08-18 17:38:53 +08003518
Joel Beckerba937122008-10-24 19:13:20 -07003519 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003520 if (ret) {
3521 mlog_errno(ret);
3522 goto out;
3523 }
3524
Joel Beckerba937122008-10-24 19:13:20 -07003525 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003526 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003527 if (ret) {
3528 mlog_errno(ret);
3529 goto out;
3530 }
3531
Joel Becker784b8162008-10-24 17:33:40 -07003532 /*
3533 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3534 * there's no need to read it.
3535 */
Joel Beckerba937122008-10-24 19:13:20 -07003536 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003537 if (ret) {
3538 mlog_errno(ret);
3539 goto out;
3540 }
3541
Joel Beckerba937122008-10-24 19:13:20 -07003542 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003543 new_bucket_head ?
3544 OCFS2_JOURNAL_ACCESS_CREATE :
3545 OCFS2_JOURNAL_ACCESS_WRITE);
3546 if (ret) {
3547 mlog_errno(ret);
3548 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003549 }
3550
Joel Beckerba937122008-10-24 19:13:20 -07003551 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003552 count = le16_to_cpu(xh->xh_count);
3553 start = ocfs2_xattr_find_divide_pos(xh);
3554
3555 if (start == count) {
3556 xe = &xh->xh_entries[start-1];
3557
3558 /*
3559 * initialized a new empty bucket here.
3560 * The hash value is set as one larger than
3561 * that of the last entry in the previous bucket.
3562 */
Joel Beckerba937122008-10-24 19:13:20 -07003563 for (i = 0; i < t_bucket->bu_blocks; i++)
3564 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003565
Joel Beckerba937122008-10-24 19:13:20 -07003566 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003567 xh->xh_free_start = cpu_to_le16(blocksize);
3568 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3569 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3570
3571 goto set_num_buckets;
3572 }
3573
Tao Ma01225592008-08-18 17:38:53 +08003574 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003575 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003576
3577 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003578 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003579
3580 /*
3581 * Calculate the total name/value len and xh_free_start for
3582 * the old bucket first.
3583 */
3584 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3585 name_value_len = 0;
3586 for (i = 0; i < start; i++) {
3587 xe = &xh->xh_entries[i];
3588 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3589 if (ocfs2_xattr_is_local(xe))
3590 xe_len +=
3591 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3592 else
3593 xe_len += OCFS2_XATTR_ROOT_SIZE;
3594 name_value_len += xe_len;
3595 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3596 name_offset = le16_to_cpu(xe->xe_name_offset);
3597 }
3598
3599 /*
3600 * Now begin the modification to the new bucket.
3601 *
3602 * In the new bucket, We just move the xattr entry to the beginning
3603 * and don't touch the name/value. So there will be some holes in the
3604 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3605 * called.
3606 */
3607 xe = &xh->xh_entries[start];
3608 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3609 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003610 (int)((char *)xe - (char *)xh),
3611 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003612 memmove((char *)xh->xh_entries, (char *)xe, len);
3613 xe = &xh->xh_entries[count - start];
3614 len = sizeof(struct ocfs2_xattr_entry) * start;
3615 memset((char *)xe, 0, len);
3616
3617 le16_add_cpu(&xh->xh_count, -start);
3618 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3619
3620 /* Calculate xh_free_start for the new bucket. */
3621 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3622 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3623 xe = &xh->xh_entries[i];
3624 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3625 if (ocfs2_xattr_is_local(xe))
3626 xe_len +=
3627 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3628 else
3629 xe_len += OCFS2_XATTR_ROOT_SIZE;
3630 if (le16_to_cpu(xe->xe_name_offset) <
3631 le16_to_cpu(xh->xh_free_start))
3632 xh->xh_free_start = xe->xe_name_offset;
3633 }
3634
Tao Ma80bcaf32008-10-27 06:06:24 +08003635set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003636 /* set xh->xh_num_buckets for the new xh. */
3637 if (new_bucket_head)
3638 xh->xh_num_buckets = cpu_to_le16(1);
3639 else
3640 xh->xh_num_buckets = 0;
3641
Joel Beckerba937122008-10-24 19:13:20 -07003642 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003643
3644 /* store the first_hash of the new bucket. */
3645 if (first_hash)
3646 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3647
3648 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003649 * Now only update the 1st block of the old bucket. If we
3650 * just added a new empty bucket, there is no need to modify
3651 * it.
Tao Ma01225592008-08-18 17:38:53 +08003652 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003653 if (start == count)
3654 goto out;
3655
Joel Beckerba937122008-10-24 19:13:20 -07003656 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003657 memset(&xh->xh_entries[start], 0,
3658 sizeof(struct ocfs2_xattr_entry) * (count - start));
3659 xh->xh_count = cpu_to_le16(start);
3660 xh->xh_free_start = cpu_to_le16(name_offset);
3661 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3662
Joel Beckerba937122008-10-24 19:13:20 -07003663 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003664
3665out:
Joel Beckerba937122008-10-24 19:13:20 -07003666 ocfs2_xattr_bucket_free(s_bucket);
3667 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003668
3669 return ret;
3670}
3671
3672/*
3673 * Copy xattr from one bucket to another bucket.
3674 *
3675 * The caller must make sure that the journal transaction
3676 * has enough space for journaling.
3677 */
3678static int ocfs2_cp_xattr_bucket(struct inode *inode,
3679 handle_t *handle,
3680 u64 s_blkno,
3681 u64 t_blkno,
3682 int t_is_new)
3683{
Joel Becker4980c6d2008-10-24 18:54:43 -07003684 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003685 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003686
3687 BUG_ON(s_blkno == t_blkno);
3688
3689 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003690 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3691 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003692
Joel Beckerba937122008-10-24 19:13:20 -07003693 s_bucket = ocfs2_xattr_bucket_new(inode);
3694 t_bucket = ocfs2_xattr_bucket_new(inode);
3695 if (!s_bucket || !t_bucket) {
3696 ret = -ENOMEM;
3697 mlog_errno(ret);
3698 goto out;
3699 }
3700
3701 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003702 if (ret)
3703 goto out;
3704
Joel Becker784b8162008-10-24 17:33:40 -07003705 /*
3706 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3707 * there's no need to read it.
3708 */
Joel Beckerba937122008-10-24 19:13:20 -07003709 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003710 if (ret)
3711 goto out;
3712
Joel Beckerba937122008-10-24 19:13:20 -07003713 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003714 t_is_new ?
3715 OCFS2_JOURNAL_ACCESS_CREATE :
3716 OCFS2_JOURNAL_ACCESS_WRITE);
3717 if (ret)
3718 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003719
Joel Beckerba937122008-10-24 19:13:20 -07003720 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3721 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003722
3723out:
Joel Beckerba937122008-10-24 19:13:20 -07003724 ocfs2_xattr_bucket_free(t_bucket);
3725 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003726
3727 return ret;
3728}
3729
3730/*
3731 * Copy one xattr cluster from src_blk to to_blk.
3732 * The to_blk will become the first bucket header of the cluster, so its
3733 * xh_num_buckets will be initialized as the bucket num in the cluster.
3734 */
3735static int ocfs2_cp_xattr_cluster(struct inode *inode,
3736 handle_t *handle,
3737 struct buffer_head *first_bh,
3738 u64 src_blk,
3739 u64 to_blk,
3740 u32 *first_hash)
3741{
3742 int i, ret, credits;
3743 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3744 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3745 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3746 struct buffer_head *bh = NULL;
3747 struct ocfs2_xattr_header *xh;
3748 u64 to_blk_start = to_blk;
3749
Mark Fashehde29c082008-10-29 14:45:30 -07003750 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3751 (unsigned long long)src_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003752
3753 /*
3754 * We need to update the new cluster and 1 more for the update of
3755 * the 1st bucket of the previous extent rec.
3756 */
Tao Ma85db90e2008-11-12 08:27:01 +08003757 credits = bpc + 1 + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003758 ret = ocfs2_extend_trans(handle, credits);
3759 if (ret) {
3760 mlog_errno(ret);
3761 goto out;
3762 }
3763
3764 ret = ocfs2_journal_access(handle, inode, first_bh,
3765 OCFS2_JOURNAL_ACCESS_WRITE);
3766 if (ret) {
3767 mlog_errno(ret);
3768 goto out;
3769 }
3770
3771 for (i = 0; i < num_buckets; i++) {
3772 ret = ocfs2_cp_xattr_bucket(inode, handle,
3773 src_blk, to_blk, 1);
3774 if (ret) {
3775 mlog_errno(ret);
3776 goto out;
3777 }
3778
3779 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3780 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3781 }
3782
3783 /* update the old bucket header. */
3784 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3785 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3786
3787 ocfs2_journal_dirty(handle, first_bh);
3788
3789 /* update the new bucket header. */
Joel Becker0fcaa562008-10-09 17:20:31 -07003790 ret = ocfs2_read_block(inode, to_blk_start, &bh);
Tao Ma01225592008-08-18 17:38:53 +08003791 if (ret < 0) {
3792 mlog_errno(ret);
3793 goto out;
3794 }
3795
3796 ret = ocfs2_journal_access(handle, inode, bh,
3797 OCFS2_JOURNAL_ACCESS_WRITE);
3798 if (ret) {
3799 mlog_errno(ret);
3800 goto out;
3801 }
3802
3803 xh = (struct ocfs2_xattr_header *)bh->b_data;
3804 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3805
3806 ocfs2_journal_dirty(handle, bh);
3807
3808 if (first_hash)
3809 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3810out:
3811 brelse(bh);
3812 return ret;
3813}
3814
3815/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003816 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08003817 * This function should only be called when bucket size == cluster size.
3818 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3819 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003820static int ocfs2_divide_xattr_cluster(struct inode *inode,
3821 handle_t *handle,
3822 u64 prev_blk,
3823 u64 new_blk,
3824 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003825{
3826 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003827 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003828
3829 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3830
3831 ret = ocfs2_extend_trans(handle, credits);
3832 if (ret) {
3833 mlog_errno(ret);
3834 return ret;
3835 }
3836
3837 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08003838 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3839 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08003840}
3841
3842/*
3843 * Move some xattrs from the old cluster to the new one since they are not
3844 * contiguous in ocfs2 xattr tree.
3845 *
3846 * new_blk starts a new separate cluster, and we will move some xattrs from
3847 * prev_blk to it. v_start will be set as the first name hash value in this
3848 * new cluster so that it can be used as e_cpos during tree insertion and
3849 * don't collide with our original b-tree operations. first_bh and header_bh
3850 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3851 * to extend the insert bucket.
3852 *
3853 * The problem is how much xattr should we move to the new one and when should
3854 * we update first_bh and header_bh?
3855 * 1. If cluster size > bucket size, that means the previous cluster has more
3856 * than 1 bucket, so just move half nums of bucket into the new cluster and
3857 * update the first_bh and header_bh if the insert bucket has been moved
3858 * to the new cluster.
3859 * 2. If cluster_size == bucket_size:
3860 * a) If the previous extent rec has more than one cluster and the insert
3861 * place isn't in the last cluster, copy the entire last cluster to the
3862 * new one. This time, we don't need to upate the first_bh and header_bh
3863 * since they will not be moved into the new cluster.
3864 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3865 * the new one. And we set the extend flag to zero if the insert place is
3866 * moved into the new allocated cluster since no extend is needed.
3867 */
3868static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3869 handle_t *handle,
3870 struct buffer_head **first_bh,
3871 struct buffer_head **header_bh,
3872 u64 new_blk,
3873 u64 prev_blk,
3874 u32 prev_clusters,
3875 u32 *v_start,
3876 int *extend)
3877{
3878 int ret = 0;
3879 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3880
3881 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003882 (unsigned long long)prev_blk, prev_clusters,
3883 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003884
3885 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3886 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3887 handle,
3888 first_bh,
3889 header_bh,
3890 new_blk,
3891 prev_blk,
3892 prev_clusters,
3893 v_start);
3894 else {
3895 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3896
3897 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3898 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3899 last_blk, new_blk,
3900 v_start);
3901 else {
Tao Ma80bcaf32008-10-27 06:06:24 +08003902 ret = ocfs2_divide_xattr_cluster(inode, handle,
3903 last_blk, new_blk,
3904 v_start);
Tao Ma01225592008-08-18 17:38:53 +08003905
3906 if ((*header_bh)->b_blocknr == last_blk && extend)
3907 *extend = 0;
3908 }
3909 }
3910
3911 return ret;
3912}
3913
3914/*
3915 * Add a new cluster for xattr storage.
3916 *
3917 * If the new cluster is contiguous with the previous one, it will be
3918 * appended to the same extent record, and num_clusters will be updated.
3919 * If not, we will insert a new extent for it and move some xattrs in
3920 * the last cluster into the new allocated one.
3921 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3922 * lose the benefits of hashing because we'll have to search large leaves.
3923 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3924 * if it's bigger).
3925 *
3926 * first_bh is the first block of the previous extent rec and header_bh
3927 * indicates the bucket we will insert the new xattrs. They will be updated
3928 * when the header_bh is moved into the new cluster.
3929 */
3930static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3931 struct buffer_head *root_bh,
3932 struct buffer_head **first_bh,
3933 struct buffer_head **header_bh,
3934 u32 *num_clusters,
3935 u32 prev_cpos,
3936 u64 prev_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08003937 int *extend,
3938 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003939{
Tao Ma85db90e2008-11-12 08:27:01 +08003940 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003941 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3942 u32 prev_clusters = *num_clusters;
3943 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3944 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08003945 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003946 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003947 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08003948
3949 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3950 "previous xattr blkno = %llu\n",
3951 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehde29c082008-10-29 14:45:30 -07003952 prev_cpos, (unsigned long long)prev_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003953
Joel Becker8d6220d2008-08-22 12:46:09 -07003954 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003955
Tao Ma01225592008-08-18 17:38:53 +08003956 ret = ocfs2_journal_access(handle, inode, root_bh,
3957 OCFS2_JOURNAL_ACCESS_WRITE);
3958 if (ret < 0) {
3959 mlog_errno(ret);
3960 goto leave;
3961 }
3962
Tao Ma78f30c32008-11-12 08:27:00 +08003963 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08003964 clusters_to_add, &bit_off, &num_bits);
3965 if (ret < 0) {
3966 if (ret != -ENOSPC)
3967 mlog_errno(ret);
3968 goto leave;
3969 }
3970
3971 BUG_ON(num_bits > clusters_to_add);
3972
3973 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3974 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3975 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3976
3977 if (prev_blkno + prev_clusters * bpc == block &&
3978 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3979 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3980 /*
3981 * If this cluster is contiguous with the old one and
3982 * adding this new cluster, we don't surpass the limit of
3983 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3984 * initialized and used like other buckets in the previous
3985 * cluster.
3986 * So add it as a contiguous one. The caller will handle
3987 * its init process.
3988 */
3989 v_start = prev_cpos + prev_clusters;
3990 *num_clusters = prev_clusters + num_bits;
3991 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3992 num_bits);
3993 } else {
3994 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3995 handle,
3996 first_bh,
3997 header_bh,
3998 block,
3999 prev_blkno,
4000 prev_clusters,
4001 &v_start,
4002 extend);
4003 if (ret) {
4004 mlog_errno(ret);
4005 goto leave;
4006 }
4007 }
4008
4009 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004010 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004011 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004012 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004013 if (ret < 0) {
4014 mlog_errno(ret);
4015 goto leave;
4016 }
4017
4018 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004019 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004020 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004021
4022leave:
Tao Ma01225592008-08-18 17:38:53 +08004023 return ret;
4024}
4025
4026/*
4027 * Extend a new xattr bucket and move xattrs to the end one by one until
4028 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
4029 */
4030static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004031 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004032 struct buffer_head *first_bh,
4033 struct buffer_head *start_bh,
4034 u32 num_clusters)
4035{
4036 int ret, credits;
4037 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4038 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4039 u64 start_blk = start_bh->b_blocknr, end_blk;
4040 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
Tao Ma01225592008-08-18 17:38:53 +08004041 struct ocfs2_xattr_header *first_xh =
4042 (struct ocfs2_xattr_header *)first_bh->b_data;
4043 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
4044
4045 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Mark Fashehde29c082008-10-29 14:45:30 -07004046 "from %llu, len = %u\n", (unsigned long long)start_blk,
Tao Ma01225592008-08-18 17:38:53 +08004047 (unsigned long long)first_bh->b_blocknr, num_clusters);
4048
4049 BUG_ON(bucket >= num_buckets);
4050
4051 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
4052
4053 /*
4054 * We will touch all the buckets after the start_bh(include it).
Joel Becker1224be02008-10-24 18:47:33 -07004055 * Then we add one more bucket.
Tao Ma01225592008-08-18 17:38:53 +08004056 */
Tao Ma85db90e2008-11-12 08:27:01 +08004057 credits = end_blk - start_blk + 3 * blk_per_bucket + 1 +
4058 handle->h_buffer_credits;
4059 ret = ocfs2_extend_trans(handle, credits);
4060 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004061 mlog_errno(ret);
4062 goto out;
4063 }
4064
4065 ret = ocfs2_journal_access(handle, inode, first_bh,
4066 OCFS2_JOURNAL_ACCESS_WRITE);
4067 if (ret) {
4068 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004069 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004070 }
4071
4072 while (end_blk != start_blk) {
4073 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4074 end_blk + blk_per_bucket, 0);
4075 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004076 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004077 end_blk -= blk_per_bucket;
4078 }
4079
4080 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004081 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
4082 start_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004083
4084 le16_add_cpu(&first_xh->xh_num_buckets, 1);
4085 ocfs2_journal_dirty(handle, first_bh);
4086
Tao Ma01225592008-08-18 17:38:53 +08004087out:
4088 return ret;
4089}
4090
4091/*
4092 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4093 * xb_bh is the ocfs2_xattr_block.
4094 * We will move all the buckets starting from header_bh to the next place. As
4095 * for this one, half num of its xattrs will be moved to the next one.
4096 *
4097 * We will allocate a new cluster if current cluster is full and adjust
4098 * header_bh and first_bh if the insert place is moved to the new cluster.
4099 */
4100static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4101 struct buffer_head *xb_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08004102 struct buffer_head *header_bh,
4103 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004104{
4105 struct ocfs2_xattr_header *first_xh = NULL;
4106 struct buffer_head *first_bh = NULL;
4107 struct ocfs2_xattr_block *xb =
4108 (struct ocfs2_xattr_block *)xb_bh->b_data;
4109 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4110 struct ocfs2_extent_list *el = &xb_root->xt_list;
4111 struct ocfs2_xattr_header *xh =
4112 (struct ocfs2_xattr_header *)header_bh->b_data;
4113 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4114 struct super_block *sb = inode->i_sb;
4115 struct ocfs2_super *osb = OCFS2_SB(sb);
4116 int ret, num_buckets, extend = 1;
4117 u64 p_blkno;
4118 u32 e_cpos, num_clusters;
4119
4120 mlog(0, "Add new xattr bucket starting form %llu\n",
4121 (unsigned long long)header_bh->b_blocknr);
4122
4123 /*
4124 * Add refrence for header_bh here because it may be
4125 * changed in ocfs2_add_new_xattr_cluster and we need
4126 * to free it in the end.
4127 */
4128 get_bh(header_bh);
4129
4130 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4131 &num_clusters, el);
4132 if (ret) {
4133 mlog_errno(ret);
4134 goto out;
4135 }
4136
Joel Becker0fcaa562008-10-09 17:20:31 -07004137 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
Tao Ma01225592008-08-18 17:38:53 +08004138 if (ret) {
4139 mlog_errno(ret);
4140 goto out;
4141 }
4142
4143 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4144 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4145
4146 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4147 ret = ocfs2_add_new_xattr_cluster(inode,
4148 xb_bh,
4149 &first_bh,
4150 &header_bh,
4151 &num_clusters,
4152 e_cpos,
4153 p_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08004154 &extend,
4155 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004156 if (ret) {
4157 mlog_errno(ret);
4158 goto out;
4159 }
4160 }
4161
4162 if (extend)
4163 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004164 ctxt->handle,
Tao Ma01225592008-08-18 17:38:53 +08004165 first_bh,
4166 header_bh,
4167 num_clusters);
4168 if (ret)
4169 mlog_errno(ret);
4170out:
4171 brelse(first_bh);
4172 brelse(header_bh);
4173 return ret;
4174}
4175
4176static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4177 struct ocfs2_xattr_bucket *bucket,
4178 int offs)
4179{
4180 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4181
4182 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004183 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004184}
4185
4186/*
4187 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004188 *
4189 * Note: "local" indicates the real data's locality. So we can't
4190 * just its bucket locality by its length.
4191 */
4192static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4193 struct ocfs2_xattr_info *xi,
4194 struct ocfs2_xattr_search *xs,
4195 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004196 int local)
Tao Ma01225592008-08-18 17:38:53 +08004197{
4198 struct ocfs2_xattr_entry *last, *xe;
4199 int name_len = strlen(xi->name);
4200 struct ocfs2_xattr_header *xh = xs->header;
4201 u16 count = le16_to_cpu(xh->xh_count), start;
4202 size_t blocksize = inode->i_sb->s_blocksize;
4203 char *val;
4204 size_t offs, size, new_size;
4205
4206 last = &xh->xh_entries[count];
4207 if (!xs->not_found) {
4208 xe = xs->here;
4209 offs = le16_to_cpu(xe->xe_name_offset);
4210 if (ocfs2_xattr_is_local(xe))
4211 size = OCFS2_XATTR_SIZE(name_len) +
4212 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4213 else
4214 size = OCFS2_XATTR_SIZE(name_len) +
4215 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4216
4217 /*
4218 * If the new value will be stored outside, xi->value has been
4219 * initalized as an empty ocfs2_xattr_value_root, and the same
4220 * goes with xi->value_len, so we can set new_size safely here.
4221 * See ocfs2_xattr_set_in_bucket.
4222 */
4223 new_size = OCFS2_XATTR_SIZE(name_len) +
4224 OCFS2_XATTR_SIZE(xi->value_len);
4225
4226 le16_add_cpu(&xh->xh_name_value_len, -size);
4227 if (xi->value) {
4228 if (new_size > size)
4229 goto set_new_name_value;
4230
4231 /* Now replace the old value with new one. */
4232 if (local)
4233 xe->xe_value_size = cpu_to_le64(xi->value_len);
4234 else
4235 xe->xe_value_size = 0;
4236
4237 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004238 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004239 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4240 size - OCFS2_XATTR_SIZE(name_len));
4241 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4242 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4243 xi->value, xi->value_len);
4244
4245 le16_add_cpu(&xh->xh_name_value_len, new_size);
4246 ocfs2_xattr_set_local(xe, local);
4247 return;
4248 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004249 /*
4250 * Remove the old entry if there is more than one.
4251 * We don't remove the last entry so that we can
4252 * use it to indicate the hash value of the empty
4253 * bucket.
4254 */
Tao Ma01225592008-08-18 17:38:53 +08004255 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004256 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004257 if (xh->xh_count) {
4258 memmove(xe, xe + 1,
4259 (void *)last - (void *)xe);
4260 memset(last, 0,
4261 sizeof(struct ocfs2_xattr_entry));
4262 } else
4263 xh->xh_free_start =
4264 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4265
Tao Ma01225592008-08-18 17:38:53 +08004266 return;
4267 }
4268 } else {
4269 /* find a new entry for insert. */
4270 int low = 0, high = count - 1, tmp;
4271 struct ocfs2_xattr_entry *tmp_xe;
4272
Tao Ma5a095612008-09-19 22:17:41 +08004273 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004274 tmp = (low + high) / 2;
4275 tmp_xe = &xh->xh_entries[tmp];
4276
4277 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4278 low = tmp + 1;
4279 else if (name_hash <
4280 le32_to_cpu(tmp_xe->xe_name_hash))
4281 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004282 else {
4283 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004284 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004285 }
Tao Ma01225592008-08-18 17:38:53 +08004286 }
4287
4288 xe = &xh->xh_entries[low];
4289 if (low != count)
4290 memmove(xe + 1, xe, (void *)last - (void *)xe);
4291
4292 le16_add_cpu(&xh->xh_count, 1);
4293 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4294 xe->xe_name_hash = cpu_to_le32(name_hash);
4295 xe->xe_name_len = name_len;
4296 ocfs2_xattr_set_type(xe, xi->name_index);
4297 }
4298
4299set_new_name_value:
4300 /* Insert the new name+value. */
4301 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4302
4303 /*
4304 * We must make sure that the name/value pair
4305 * exists in the same block.
4306 */
4307 offs = le16_to_cpu(xh->xh_free_start);
4308 start = offs - size;
4309
4310 if (start >> inode->i_sb->s_blocksize_bits !=
4311 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4312 offs = offs - offs % blocksize;
4313 xh->xh_free_start = cpu_to_le16(offs);
4314 }
4315
Joel Beckerba937122008-10-24 19:13:20 -07004316 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004317 xe->xe_name_offset = cpu_to_le16(offs - size);
4318
4319 memset(val, 0, size);
4320 memcpy(val, xi->name, name_len);
4321 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4322
4323 xe->xe_value_size = cpu_to_le64(xi->value_len);
4324 ocfs2_xattr_set_local(xe, local);
4325 xs->here = xe;
4326 le16_add_cpu(&xh->xh_free_start, -size);
4327 le16_add_cpu(&xh->xh_name_value_len, size);
4328
4329 return;
4330}
4331
Tao Ma01225592008-08-18 17:38:53 +08004332/*
4333 * Set the xattr entry in the specified bucket.
4334 * The bucket is indicated by xs->bucket and it should have the enough
4335 * space for the xattr insertion.
4336 */
4337static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004338 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004339 struct ocfs2_xattr_info *xi,
4340 struct ocfs2_xattr_search *xs,
4341 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004342 int local)
Tao Ma01225592008-08-18 17:38:53 +08004343{
Joel Becker1224be02008-10-24 18:47:33 -07004344 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004345 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004346
Mark Fashehff1ec202008-08-19 10:54:29 -07004347 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4348 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004349 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004350
Joel Beckerba937122008-10-24 19:13:20 -07004351 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004352 blkno = bucket_blkno(xs->bucket);
4353 ocfs2_xattr_bucket_relse(xs->bucket);
4354 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004355 if (ret) {
4356 mlog_errno(ret);
4357 goto out;
4358 }
4359 }
4360
Joel Beckerba937122008-10-24 19:13:20 -07004361 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004362 OCFS2_JOURNAL_ACCESS_WRITE);
4363 if (ret < 0) {
4364 mlog_errno(ret);
4365 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004366 }
4367
Tao Ma5a095612008-09-19 22:17:41 +08004368 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004369 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004370
Tao Ma01225592008-08-18 17:38:53 +08004371out:
Tao Ma01225592008-08-18 17:38:53 +08004372 return ret;
4373}
4374
4375static int ocfs2_xattr_value_update_size(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004376 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004377 struct buffer_head *xe_bh,
4378 struct ocfs2_xattr_entry *xe,
4379 u64 new_size)
4380{
4381 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004382
4383 ret = ocfs2_journal_access(handle, inode, xe_bh,
4384 OCFS2_JOURNAL_ACCESS_WRITE);
4385 if (ret < 0) {
4386 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004387 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004388 }
4389
4390 xe->xe_value_size = cpu_to_le64(new_size);
4391
4392 ret = ocfs2_journal_dirty(handle, xe_bh);
4393 if (ret < 0)
4394 mlog_errno(ret);
4395
Tao Ma01225592008-08-18 17:38:53 +08004396out:
4397 return ret;
4398}
4399
4400/*
4401 * Truncate the specified xe_off entry in xattr bucket.
4402 * bucket is indicated by header_bh and len is the new length.
4403 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4404 *
4405 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4406 */
4407static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4408 struct buffer_head *header_bh,
4409 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004410 int len,
4411 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004412{
4413 int ret, offset;
4414 u64 value_blk;
4415 struct buffer_head *value_bh = NULL;
4416 struct ocfs2_xattr_value_root *xv;
4417 struct ocfs2_xattr_entry *xe;
4418 struct ocfs2_xattr_header *xh =
4419 (struct ocfs2_xattr_header *)header_bh->b_data;
4420 size_t blocksize = inode->i_sb->s_blocksize;
4421
4422 xe = &xh->xh_entries[xe_off];
4423
4424 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4425
4426 offset = le16_to_cpu(xe->xe_name_offset) +
4427 OCFS2_XATTR_SIZE(xe->xe_name_len);
4428
4429 value_blk = offset / blocksize;
4430
4431 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4432 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4433 value_blk += header_bh->b_blocknr;
4434
Joel Becker0fcaa562008-10-09 17:20:31 -07004435 ret = ocfs2_read_block(inode, value_blk, &value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004436 if (ret) {
4437 mlog_errno(ret);
4438 goto out;
4439 }
4440
4441 xv = (struct ocfs2_xattr_value_root *)
4442 (value_bh->b_data + offset % blocksize);
4443
4444 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4445 xe_off, (unsigned long long)header_bh->b_blocknr, len);
Tao Ma78f30c32008-11-12 08:27:00 +08004446 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004447 if (ret) {
4448 mlog_errno(ret);
4449 goto out;
4450 }
4451
Tao Ma85db90e2008-11-12 08:27:01 +08004452 ret = ocfs2_xattr_value_update_size(inode, ctxt->handle,
4453 header_bh, xe, len);
Tao Ma01225592008-08-18 17:38:53 +08004454 if (ret) {
4455 mlog_errno(ret);
4456 goto out;
4457 }
4458
4459out:
4460 brelse(value_bh);
4461 return ret;
4462}
4463
4464static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004465 struct ocfs2_xattr_search *xs,
4466 int len,
4467 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004468{
4469 int ret, offset;
4470 struct ocfs2_xattr_entry *xe = xs->here;
4471 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4472
Joel Beckerba937122008-10-24 19:13:20 -07004473 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004474
4475 offset = xe - xh->xh_entries;
Joel Beckerba937122008-10-24 19:13:20 -07004476 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
Tao Ma78f30c32008-11-12 08:27:00 +08004477 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004478 if (ret)
4479 mlog_errno(ret);
4480
4481 return ret;
4482}
4483
4484static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004485 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004486 struct ocfs2_xattr_search *xs,
4487 char *val,
4488 int value_len)
4489{
4490 int offset;
4491 struct ocfs2_xattr_value_root *xv;
4492 struct ocfs2_xattr_entry *xe = xs->here;
4493
4494 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4495
4496 offset = le16_to_cpu(xe->xe_name_offset) +
4497 OCFS2_XATTR_SIZE(xe->xe_name_len);
4498
4499 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4500
Tao Ma85db90e2008-11-12 08:27:01 +08004501 return __ocfs2_xattr_set_value_outside(inode, handle,
4502 xv, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004503}
4504
Tao Ma01225592008-08-18 17:38:53 +08004505static int ocfs2_rm_xattr_cluster(struct inode *inode,
4506 struct buffer_head *root_bh,
4507 u64 blkno,
4508 u32 cpos,
4509 u32 len)
4510{
4511 int ret;
4512 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4513 struct inode *tl_inode = osb->osb_tl_inode;
4514 handle_t *handle;
4515 struct ocfs2_xattr_block *xb =
4516 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004517 struct ocfs2_alloc_context *meta_ac = NULL;
4518 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004519 struct ocfs2_extent_tree et;
4520
Joel Becker8d6220d2008-08-22 12:46:09 -07004521 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004522
4523 ocfs2_init_dealloc_ctxt(&dealloc);
4524
4525 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4526 cpos, len, (unsigned long long)blkno);
4527
4528 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4529
Joel Beckerf99b9b72008-08-20 19:36:33 -07004530 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004531 if (ret) {
4532 mlog_errno(ret);
4533 return ret;
4534 }
4535
4536 mutex_lock(&tl_inode->i_mutex);
4537
4538 if (ocfs2_truncate_log_needs_flush(osb)) {
4539 ret = __ocfs2_flush_truncate_log(osb);
4540 if (ret < 0) {
4541 mlog_errno(ret);
4542 goto out;
4543 }
4544 }
4545
4546 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
Tao Mad3264792008-10-24 07:57:28 +08004547 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004548 ret = -ENOMEM;
4549 mlog_errno(ret);
4550 goto out;
4551 }
4552
4553 ret = ocfs2_journal_access(handle, inode, root_bh,
4554 OCFS2_JOURNAL_ACCESS_WRITE);
4555 if (ret) {
4556 mlog_errno(ret);
4557 goto out_commit;
4558 }
4559
Joel Beckerf99b9b72008-08-20 19:36:33 -07004560 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4561 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004562 if (ret) {
4563 mlog_errno(ret);
4564 goto out_commit;
4565 }
4566
4567 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4568
4569 ret = ocfs2_journal_dirty(handle, root_bh);
4570 if (ret) {
4571 mlog_errno(ret);
4572 goto out_commit;
4573 }
4574
4575 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4576 if (ret)
4577 mlog_errno(ret);
4578
4579out_commit:
4580 ocfs2_commit_trans(osb, handle);
4581out:
4582 ocfs2_schedule_truncate_log_flush(osb, 1);
4583
4584 mutex_unlock(&tl_inode->i_mutex);
4585
4586 if (meta_ac)
4587 ocfs2_free_alloc_context(meta_ac);
4588
4589 ocfs2_run_deallocs(osb, &dealloc);
4590
4591 return ret;
4592}
4593
Tao Ma01225592008-08-18 17:38:53 +08004594static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004595 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004596 struct ocfs2_xattr_search *xs)
4597{
Joel Beckerba937122008-10-24 19:13:20 -07004598 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004599 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4600 le16_to_cpu(xh->xh_count) - 1];
4601 int ret = 0;
4602
Joel Beckerba937122008-10-24 19:13:20 -07004603 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004604 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004605 if (ret) {
4606 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004607 return;
Tao Ma01225592008-08-18 17:38:53 +08004608 }
4609
4610 /* Remove the old entry. */
4611 memmove(xs->here, xs->here + 1,
4612 (void *)last - (void *)xs->here);
4613 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4614 le16_add_cpu(&xh->xh_count, -1);
4615
Joel Beckerba937122008-10-24 19:13:20 -07004616 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004617}
4618
4619/*
4620 * Set the xattr name/value in the bucket specified in xs.
4621 *
4622 * As the new value in xi may be stored in the bucket or in an outside cluster,
4623 * we divide the whole process into 3 steps:
4624 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4625 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4626 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4627 * 4. If the clusters for the new outside value can't be allocated, we need
4628 * to free the xattr we allocated in set.
4629 */
4630static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4631 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004632 struct ocfs2_xattr_search *xs,
4633 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004634{
Tao Ma5a095612008-09-19 22:17:41 +08004635 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004636 size_t value_len;
4637 char *val = (char *)xi->value;
4638 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004639 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4640 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004641
4642 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4643 /*
4644 * We need to truncate the xattr storage first.
4645 *
4646 * If both the old and new value are stored to
4647 * outside block, we only need to truncate
4648 * the storage and then set the value outside.
4649 *
4650 * If the new value should be stored within block,
4651 * we should free all the outside block first and
4652 * the modification to the xattr block will be done
4653 * by following steps.
4654 */
4655 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4656 value_len = xi->value_len;
4657 else
4658 value_len = 0;
4659
4660 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004661 value_len,
4662 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004663 if (ret)
4664 goto out;
4665
4666 if (value_len)
4667 goto set_value_outside;
4668 }
4669
4670 value_len = xi->value_len;
4671 /* So we have to handle the inside block change now. */
4672 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4673 /*
4674 * If the new value will be stored outside of block,
4675 * initalize a new empty value root and insert it first.
4676 */
4677 local = 0;
4678 xi->value = &def_xv;
4679 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4680 }
4681
Tao Ma85db90e2008-11-12 08:27:01 +08004682 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4683 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004684 if (ret) {
4685 mlog_errno(ret);
4686 goto out;
4687 }
4688
Tao Ma5a095612008-09-19 22:17:41 +08004689 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4690 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004691
Tao Ma5a095612008-09-19 22:17:41 +08004692 /* allocate the space now for the outside block storage. */
4693 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004694 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08004695 if (ret) {
4696 mlog_errno(ret);
4697
4698 if (xs->not_found) {
4699 /*
4700 * We can't allocate enough clusters for outside
4701 * storage and we have allocated xattr already,
4702 * so need to remove it.
4703 */
Tao Ma85db90e2008-11-12 08:27:01 +08004704 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08004705 }
Tao Ma01225592008-08-18 17:38:53 +08004706 goto out;
4707 }
4708
4709set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08004710 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4711 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004712out:
4713 return ret;
4714}
4715
Tao Ma80bcaf32008-10-27 06:06:24 +08004716/*
4717 * check whether the xattr bucket is filled up with the same hash value.
4718 * If we want to insert the xattr with the same hash, return -ENOSPC.
4719 * If we want to insert a xattr with different hash value, go ahead
4720 * and ocfs2_divide_xattr_bucket will handle this.
4721 */
Tao Ma01225592008-08-18 17:38:53 +08004722static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004723 struct ocfs2_xattr_bucket *bucket,
4724 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004725{
Joel Becker3e632942008-10-24 17:04:49 -07004726 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004727 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4728
4729 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4730 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004731
4732 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4733 xh->xh_entries[0].xe_name_hash) {
4734 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4735 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004736 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004737 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4738 return -ENOSPC;
4739 }
4740
4741 return 0;
4742}
4743
4744static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4745 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004746 struct ocfs2_xattr_search *xs,
4747 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004748{
4749 struct ocfs2_xattr_header *xh;
4750 struct ocfs2_xattr_entry *xe;
4751 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07004752 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08004753 size_t value_size = 0, name_len = strlen(xi->name);
4754 size_t blocksize = inode->i_sb->s_blocksize;
4755 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08004756
4757 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4758
4759try_again:
4760 xh = xs->header;
4761 count = le16_to_cpu(xh->xh_count);
4762 xh_free_start = le16_to_cpu(xh->xh_free_start);
4763 header_size = sizeof(struct ocfs2_xattr_header) +
4764 count * sizeof(struct ocfs2_xattr_entry);
4765 max_free = OCFS2_XATTR_BUCKET_SIZE -
4766 le16_to_cpu(xh->xh_name_value_len) - header_size;
4767
4768 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4769 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07004770 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004771 header_size);
4772
4773 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4774 value_size = OCFS2_XATTR_ROOT_SIZE;
4775 else if (xi->value)
4776 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4777
4778 if (xs->not_found)
4779 need = sizeof(struct ocfs2_xattr_entry) +
4780 OCFS2_XATTR_SIZE(name_len) + value_size;
4781 else {
4782 need = value_size + OCFS2_XATTR_SIZE(name_len);
4783
4784 /*
4785 * We only replace the old value if the new length is smaller
4786 * than the old one. Otherwise we will allocate new space in the
4787 * bucket to store it.
4788 */
4789 xe = xs->here;
4790 if (ocfs2_xattr_is_local(xe))
4791 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4792 else
4793 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4794
4795 if (old >= value_size)
4796 need = 0;
4797 }
4798
4799 free = xh_free_start - header_size;
4800 /*
4801 * We need to make sure the new name/value pair
4802 * can exist in the same block.
4803 */
4804 if (xh_free_start % blocksize < need)
4805 free -= xh_free_start % blocksize;
4806
4807 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4808 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4809 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07004810 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004811 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4812 le16_to_cpu(xh->xh_name_value_len));
4813
Tao Ma976331d2008-11-12 08:26:57 +08004814 if (free < need ||
4815 (xs->not_found &&
4816 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08004817 if (need <= max_free &&
4818 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4819 /*
4820 * We can create the space by defragment. Since only the
4821 * name/value will be moved, the xe shouldn't be changed
4822 * in xs.
4823 */
Tao Ma85db90e2008-11-12 08:27:01 +08004824 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
4825 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004826 if (ret) {
4827 mlog_errno(ret);
4828 goto out;
4829 }
4830
4831 xh_free_start = le16_to_cpu(xh->xh_free_start);
4832 free = xh_free_start - header_size;
4833 if (xh_free_start % blocksize < need)
4834 free -= xh_free_start % blocksize;
4835
4836 if (free >= need)
4837 goto xattr_set;
4838
4839 mlog(0, "Can't get enough space for xattr insert by "
4840 "defragment. Need %u bytes, but we have %d, so "
4841 "allocate new bucket for it.\n", need, free);
4842 }
4843
4844 /*
4845 * We have to add new buckets or clusters and one
4846 * allocation should leave us enough space for insert.
4847 */
4848 BUG_ON(allocation);
4849
4850 /*
4851 * We do not allow for overlapping ranges between buckets. And
4852 * the maximum number of collisions we will allow for then is
4853 * one bucket's worth, so check it here whether we need to
4854 * add a new bucket for the insert.
4855 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004856 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004857 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08004858 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08004859 if (ret) {
4860 mlog_errno(ret);
4861 goto out;
4862 }
4863
4864 ret = ocfs2_add_new_xattr_bucket(inode,
4865 xs->xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08004866 xs->bucket->bu_bhs[0],
4867 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004868 if (ret) {
4869 mlog_errno(ret);
4870 goto out;
4871 }
4872
Joel Beckerba937122008-10-24 19:13:20 -07004873 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004874
4875 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4876 xi->name_index,
4877 xi->name, xs);
4878 if (ret && ret != -ENODATA)
4879 goto out;
4880 xs->not_found = ret;
4881 allocation = 1;
4882 goto try_again;
4883 }
4884
4885xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08004886 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004887out:
4888 mlog_exit(ret);
4889 return ret;
4890}
Tao Maa3944252008-08-18 17:38:54 +08004891
4892static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4893 struct ocfs2_xattr_bucket *bucket,
4894 void *para)
4895{
4896 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07004897 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08004898 u16 i;
4899 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08004900 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4901 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
4902
4903 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08004904
Tao Ma85db90e2008-11-12 08:27:01 +08004905 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4906 if (IS_ERR(ctxt.handle)) {
4907 ret = PTR_ERR(ctxt.handle);
4908 mlog_errno(ret);
4909 goto out;
4910 }
4911
Tao Maa3944252008-08-18 17:38:54 +08004912 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4913 xe = &xh->xh_entries[i];
4914 if (ocfs2_xattr_is_local(xe))
4915 continue;
4916
4917 ret = ocfs2_xattr_bucket_value_truncate(inode,
Joel Becker4ac60322008-10-18 19:11:42 -07004918 bucket->bu_bhs[0],
Tao Ma78f30c32008-11-12 08:27:00 +08004919 i, 0, &ctxt);
Tao Maa3944252008-08-18 17:38:54 +08004920 if (ret) {
4921 mlog_errno(ret);
4922 break;
4923 }
4924 }
4925
Tao Ma85db90e2008-11-12 08:27:01 +08004926 ret = ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08004927 ocfs2_schedule_truncate_log_flush(osb, 1);
4928 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08004929out:
Tao Maa3944252008-08-18 17:38:54 +08004930 return ret;
4931}
4932
4933static int ocfs2_delete_xattr_index_block(struct inode *inode,
4934 struct buffer_head *xb_bh)
4935{
4936 struct ocfs2_xattr_block *xb =
4937 (struct ocfs2_xattr_block *)xb_bh->b_data;
4938 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4939 int ret = 0;
4940 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4941 u64 p_blkno;
4942
4943 if (le16_to_cpu(el->l_next_free_rec) == 0)
4944 return 0;
4945
4946 while (name_hash > 0) {
4947 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4948 &e_cpos, &num_clusters, el);
4949 if (ret) {
4950 mlog_errno(ret);
4951 goto out;
4952 }
4953
4954 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4955 ocfs2_delete_xattr_in_bucket,
4956 NULL);
4957 if (ret) {
4958 mlog_errno(ret);
4959 goto out;
4960 }
4961
4962 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4963 p_blkno, e_cpos, num_clusters);
4964 if (ret) {
4965 mlog_errno(ret);
4966 break;
4967 }
4968
4969 if (e_cpos == 0)
4970 break;
4971
4972 name_hash = e_cpos - 1;
4973 }
4974
4975out:
4976 return ret;
4977}
Mark Fasheh99219ae2008-10-07 14:52:59 -07004978
4979/*
4980 * 'trusted' attributes support
4981 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07004982static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4983 size_t list_size, const char *name,
4984 size_t name_len)
4985{
Tiger Yangceb1eba2008-10-23 16:34:13 +08004986 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07004987 const size_t total_len = prefix_len + name_len + 1;
4988
4989 if (list && total_len <= list_size) {
4990 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4991 memcpy(list + prefix_len, name, name_len);
4992 list[prefix_len + name_len] = '\0';
4993 }
4994 return total_len;
4995}
4996
4997static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4998 void *buffer, size_t size)
4999{
5000 if (strcmp(name, "") == 0)
5001 return -EINVAL;
5002 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5003 buffer, size);
5004}
5005
5006static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5007 const void *value, size_t size, int flags)
5008{
5009 if (strcmp(name, "") == 0)
5010 return -EINVAL;
5011
5012 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5013 size, flags);
5014}
5015
5016struct xattr_handler ocfs2_xattr_trusted_handler = {
5017 .prefix = XATTR_TRUSTED_PREFIX,
5018 .list = ocfs2_xattr_trusted_list,
5019 .get = ocfs2_xattr_trusted_get,
5020 .set = ocfs2_xattr_trusted_set,
5021};
5022
Mark Fasheh99219ae2008-10-07 14:52:59 -07005023/*
5024 * 'user' attributes support
5025 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005026static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5027 size_t list_size, const char *name,
5028 size_t name_len)
5029{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005030 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005031 const size_t total_len = prefix_len + name_len + 1;
5032 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5033
5034 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5035 return 0;
5036
5037 if (list && total_len <= list_size) {
5038 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5039 memcpy(list + prefix_len, name, name_len);
5040 list[prefix_len + name_len] = '\0';
5041 }
5042 return total_len;
5043}
5044
5045static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5046 void *buffer, size_t size)
5047{
5048 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5049
5050 if (strcmp(name, "") == 0)
5051 return -EINVAL;
5052 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5053 return -EOPNOTSUPP;
5054 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5055 buffer, size);
5056}
5057
5058static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5059 const void *value, size_t size, int flags)
5060{
5061 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5062
5063 if (strcmp(name, "") == 0)
5064 return -EINVAL;
5065 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5066 return -EOPNOTSUPP;
5067
5068 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5069 size, flags);
5070}
5071
5072struct xattr_handler ocfs2_xattr_user_handler = {
5073 .prefix = XATTR_USER_PREFIX,
5074 .list = ocfs2_xattr_user_list,
5075 .get = ocfs2_xattr_user_get,
5076 .set = ocfs2_xattr_user_set,
5077};