blob: 7a9089255a875652d32fdae6cd8dc6443f302cf4 [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/*
2329 * ocfs2_xattr_set()
2330 *
2331 * Set, replace or remove an extended attribute for this inode.
2332 * value is NULL to remove an existing extended attribute, else either
2333 * create or replace an extended attribute.
2334 */
2335int ocfs2_xattr_set(struct inode *inode,
2336 int name_index,
2337 const char *name,
2338 const void *value,
2339 size_t value_len,
2340 int flags)
2341{
2342 struct buffer_head *di_bh = NULL;
2343 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002344 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002345 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002346 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002347 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002348
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
Tiger Yang8154da32008-08-18 17:11:46 +08002364 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2365 return -EOPNOTSUPP;
2366
Joel Beckerba937122008-10-24 19:13:20 -07002367 /*
2368 * Only xbs will be used on indexed trees. xis doesn't need a
2369 * bucket.
2370 */
2371 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2372 if (!xbs.bucket) {
2373 mlog_errno(-ENOMEM);
2374 return -ENOMEM;
2375 }
2376
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002377 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2378 if (ret < 0) {
2379 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002380 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002381 }
2382 xis.inode_bh = xbs.inode_bh = di_bh;
2383 di = (struct ocfs2_dinode *)di_bh->b_data;
2384
2385 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2386 /*
2387 * Scan inode and external block to find the same name
2388 * extended attribute and collect search infomation.
2389 */
2390 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2391 if (ret)
2392 goto cleanup;
2393 if (xis.not_found) {
2394 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2395 if (ret)
2396 goto cleanup;
2397 }
2398
2399 if (xis.not_found && xbs.not_found) {
2400 ret = -ENODATA;
2401 if (flags & XATTR_REPLACE)
2402 goto cleanup;
2403 ret = 0;
2404 if (!value)
2405 goto cleanup;
2406 } else {
2407 ret = -EEXIST;
2408 if (flags & XATTR_CREATE)
2409 goto cleanup;
2410 }
2411
Tao Ma85db90e2008-11-12 08:27:01 +08002412
2413 mutex_lock(&tl_inode->i_mutex);
2414
2415 if (ocfs2_truncate_log_needs_flush(osb)) {
2416 ret = __ocfs2_flush_truncate_log(osb);
2417 if (ret < 0) {
2418 mutex_unlock(&tl_inode->i_mutex);
2419 mlog_errno(ret);
2420 goto cleanup;
2421 }
2422 }
2423 mutex_unlock(&tl_inode->i_mutex);
2424
2425 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2426 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002427 if (ret) {
2428 mlog_errno(ret);
2429 goto cleanup;
2430 }
2431
Tao Ma85db90e2008-11-12 08:27:01 +08002432 ctxt.handle = ocfs2_start_trans(osb, credits);
2433 if (IS_ERR(ctxt.handle)) {
2434 ret = PTR_ERR(ctxt.handle);
2435 mlog_errno(ret);
2436 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002437 }
Tao Ma85db90e2008-11-12 08:27:01 +08002438
2439 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2440
2441 ocfs2_commit_trans(osb, ctxt.handle);
2442
Tao Ma78f30c32008-11-12 08:27:00 +08002443 if (ctxt.data_ac)
2444 ocfs2_free_alloc_context(ctxt.data_ac);
2445 if (ctxt.meta_ac)
2446 ocfs2_free_alloc_context(ctxt.meta_ac);
2447 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2448 ocfs2_schedule_truncate_log_flush(osb, 1);
2449 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002450cleanup:
2451 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2452 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002453cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002454 brelse(di_bh);
2455 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002456 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002457
2458 return ret;
2459}
2460
Tao Ma0c044f02008-08-18 17:38:50 +08002461/*
2462 * Find the xattr extent rec which may contains name_hash.
2463 * e_cpos will be the first name hash of the xattr rec.
2464 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2465 */
2466static int ocfs2_xattr_get_rec(struct inode *inode,
2467 u32 name_hash,
2468 u64 *p_blkno,
2469 u32 *e_cpos,
2470 u32 *num_clusters,
2471 struct ocfs2_extent_list *el)
2472{
2473 int ret = 0, i;
2474 struct buffer_head *eb_bh = NULL;
2475 struct ocfs2_extent_block *eb;
2476 struct ocfs2_extent_rec *rec = NULL;
2477 u64 e_blkno = 0;
2478
2479 if (el->l_tree_depth) {
2480 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2481 if (ret) {
2482 mlog_errno(ret);
2483 goto out;
2484 }
2485
2486 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2487 el = &eb->h_list;
2488
2489 if (el->l_tree_depth) {
2490 ocfs2_error(inode->i_sb,
2491 "Inode %lu has non zero tree depth in "
2492 "xattr tree block %llu\n", inode->i_ino,
2493 (unsigned long long)eb_bh->b_blocknr);
2494 ret = -EROFS;
2495 goto out;
2496 }
2497 }
2498
2499 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2500 rec = &el->l_recs[i];
2501
2502 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2503 e_blkno = le64_to_cpu(rec->e_blkno);
2504 break;
2505 }
2506 }
2507
2508 if (!e_blkno) {
2509 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2510 "record (%u, %u, 0) in xattr", inode->i_ino,
2511 le32_to_cpu(rec->e_cpos),
2512 ocfs2_rec_clusters(el, rec));
2513 ret = -EROFS;
2514 goto out;
2515 }
2516
2517 *p_blkno = le64_to_cpu(rec->e_blkno);
2518 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2519 if (e_cpos)
2520 *e_cpos = le32_to_cpu(rec->e_cpos);
2521out:
2522 brelse(eb_bh);
2523 return ret;
2524}
2525
2526typedef int (xattr_bucket_func)(struct inode *inode,
2527 struct ocfs2_xattr_bucket *bucket,
2528 void *para);
2529
Tao Ma589dc262008-08-18 17:38:51 +08002530static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002531 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002532 int name_index,
2533 const char *name,
2534 u32 name_hash,
2535 u16 *xe_index,
2536 int *found)
2537{
2538 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002539 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002540 size_t name_len = strlen(name);
2541 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002542 char *xe_name;
2543
2544 /*
2545 * We don't use binary search in the bucket because there
2546 * may be multiple entries with the same name hash.
2547 */
2548 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2549 xe = &xh->xh_entries[i];
2550
2551 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2552 continue;
2553 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2554 break;
2555
2556 cmp = name_index - ocfs2_xattr_get_type(xe);
2557 if (!cmp)
2558 cmp = name_len - xe->xe_name_len;
2559 if (cmp)
2560 continue;
2561
2562 ret = ocfs2_xattr_bucket_get_name_value(inode,
2563 xh,
2564 i,
2565 &block_off,
2566 &new_offset);
2567 if (ret) {
2568 mlog_errno(ret);
2569 break;
2570 }
2571
Joel Beckere2356a32008-10-27 15:01:54 -07002572 xe_name = bucket_block(bucket, block_off) + new_offset;
2573 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002574 *xe_index = i;
2575 *found = 1;
2576 ret = 0;
2577 break;
2578 }
2579 }
2580
2581 return ret;
2582}
2583
2584/*
2585 * Find the specified xattr entry in a series of buckets.
2586 * This series start from p_blkno and last for num_clusters.
2587 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2588 * the num of the valid buckets.
2589 *
2590 * Return the buffer_head this xattr should reside in. And if the xattr's
2591 * hash is in the gap of 2 buckets, return the lower bucket.
2592 */
2593static int ocfs2_xattr_bucket_find(struct inode *inode,
2594 int name_index,
2595 const char *name,
2596 u32 name_hash,
2597 u64 p_blkno,
2598 u32 first_hash,
2599 u32 num_clusters,
2600 struct ocfs2_xattr_search *xs)
2601{
2602 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002603 struct ocfs2_xattr_header *xh = NULL;
2604 struct ocfs2_xattr_entry *xe = NULL;
2605 u16 index = 0;
2606 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2607 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002608 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002609 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002610 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002611
Joel Beckere2356a32008-10-27 15:01:54 -07002612 search = ocfs2_xattr_bucket_new(inode);
2613 if (!search) {
2614 ret = -ENOMEM;
2615 mlog_errno(ret);
2616 goto out;
2617 }
2618
2619 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002620 if (ret) {
2621 mlog_errno(ret);
2622 goto out;
2623 }
2624
Joel Beckere2356a32008-10-27 15:01:54 -07002625 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002626 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002627 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002628 ocfs2_xattr_bucket_relse(search);
2629
Tao Ma589dc262008-08-18 17:38:51 +08002630 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002631 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002632 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002633 if (ret) {
2634 mlog_errno(ret);
2635 goto out;
2636 }
2637
Joel Beckere2356a32008-10-27 15:01:54 -07002638 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002639 xe = &xh->xh_entries[0];
2640 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2641 high_bucket = bucket - 1;
2642 continue;
2643 }
2644
2645 /*
2646 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08002647 * bucket is larger than the search one. for an empty
2648 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002649 */
Tao Ma5a095612008-09-19 22:17:41 +08002650 if (xh->xh_count)
2651 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2652
Tao Ma589dc262008-08-18 17:38:51 +08002653 last_hash = le32_to_cpu(xe->xe_name_hash);
2654
Joel Beckere2356a32008-10-27 15:01:54 -07002655 /* record lower_blkno which may be the insert place. */
2656 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002657
2658 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2659 low_bucket = bucket + 1;
2660 continue;
2661 }
2662
2663 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002664 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002665 name_index, name, name_hash,
2666 &index, &found);
2667 if (ret) {
2668 mlog_errno(ret);
2669 goto out;
2670 }
2671 break;
2672 }
2673
2674 /*
2675 * Record the bucket we have found.
2676 * When the xattr's hash value is in the gap of 2 buckets, we will
2677 * always set it to the previous bucket.
2678 */
Joel Beckere2356a32008-10-27 15:01:54 -07002679 if (!lower_blkno)
2680 lower_blkno = p_blkno;
2681
2682 /* This should be in cache - we just read it during the search */
2683 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2684 if (ret) {
2685 mlog_errno(ret);
2686 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08002687 }
Tao Ma589dc262008-08-18 17:38:51 +08002688
Joel Beckerba937122008-10-24 19:13:20 -07002689 xs->header = bucket_xh(xs->bucket);
2690 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002691 xs->end = xs->base + inode->i_sb->s_blocksize;
2692
2693 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08002694 xs->here = &xs->header->xh_entries[index];
2695 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07002696 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08002697 } else
2698 ret = -ENODATA;
2699
2700out:
Joel Beckere2356a32008-10-27 15:01:54 -07002701 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08002702 return ret;
2703}
2704
2705static int ocfs2_xattr_index_block_find(struct inode *inode,
2706 struct buffer_head *root_bh,
2707 int name_index,
2708 const char *name,
2709 struct ocfs2_xattr_search *xs)
2710{
2711 int ret;
2712 struct ocfs2_xattr_block *xb =
2713 (struct ocfs2_xattr_block *)root_bh->b_data;
2714 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2715 struct ocfs2_extent_list *el = &xb_root->xt_list;
2716 u64 p_blkno = 0;
2717 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08002718 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08002719
2720 if (le16_to_cpu(el->l_next_free_rec) == 0)
2721 return -ENODATA;
2722
2723 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2724 name, name_hash, name_index);
2725
2726 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2727 &num_clusters, el);
2728 if (ret) {
2729 mlog_errno(ret);
2730 goto out;
2731 }
2732
2733 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2734
2735 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07002736 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2737 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08002738
2739 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2740 p_blkno, first_hash, num_clusters, xs);
2741
2742out:
2743 return ret;
2744}
2745
Tao Ma0c044f02008-08-18 17:38:50 +08002746static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2747 u64 blkno,
2748 u32 clusters,
2749 xattr_bucket_func *func,
2750 void *para)
2751{
Joel Becker6dde41d2008-10-24 17:16:48 -07002752 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08002753 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2754 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07002755 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08002756
Joel Beckerba937122008-10-24 19:13:20 -07002757 bucket = ocfs2_xattr_bucket_new(inode);
2758 if (!bucket) {
2759 mlog_errno(-ENOMEM);
2760 return -ENOMEM;
2761 }
Tao Ma0c044f02008-08-18 17:38:50 +08002762
2763 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07002764 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002765
Joel Beckerba937122008-10-24 19:13:20 -07002766 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
2767 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002768 if (ret) {
2769 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002770 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002771 }
2772
Tao Ma0c044f02008-08-18 17:38:50 +08002773 /*
2774 * The real bucket num in this series of blocks is stored
2775 * in the 1st bucket.
2776 */
2777 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07002778 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08002779
Mark Fashehde29c082008-10-29 14:45:30 -07002780 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2781 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07002782 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08002783 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07002784 ret = func(inode, bucket, para);
2785 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08002786 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002787 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08002788 }
2789
Joel Beckerba937122008-10-24 19:13:20 -07002790 ocfs2_xattr_bucket_relse(bucket);
2791 if (ret)
2792 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002793 }
2794
Joel Beckerba937122008-10-24 19:13:20 -07002795 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08002796 return ret;
2797}
2798
2799struct ocfs2_xattr_tree_list {
2800 char *buffer;
2801 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08002802 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08002803};
2804
2805static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2806 struct ocfs2_xattr_header *xh,
2807 int index,
2808 int *block_off,
2809 int *new_offset)
2810{
2811 u16 name_offset;
2812
2813 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2814 return -EINVAL;
2815
2816 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2817
2818 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2819 *new_offset = name_offset % inode->i_sb->s_blocksize;
2820
2821 return 0;
2822}
2823
2824static int ocfs2_list_xattr_bucket(struct inode *inode,
2825 struct ocfs2_xattr_bucket *bucket,
2826 void *para)
2827{
Tao Ma936b8832008-10-09 23:06:14 +08002828 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08002829 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08002830 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08002831 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08002832
Joel Becker3e632942008-10-24 17:04:49 -07002833 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
2834 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08002835 type = ocfs2_xattr_get_type(entry);
2836 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08002837
Tao Ma936b8832008-10-09 23:06:14 +08002838 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08002839 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07002840 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08002841 i,
2842 &block_off,
2843 &new_offset);
2844 if (ret)
2845 break;
Tao Ma936b8832008-10-09 23:06:14 +08002846
Joel Becker51def392008-10-24 16:57:21 -07002847 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08002848 new_offset;
2849 ret = ocfs2_xattr_list_entry(xl->buffer,
2850 xl->buffer_size,
2851 &xl->result,
2852 prefix, name,
2853 entry->xe_name_len);
2854 if (ret)
2855 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002856 }
2857 }
2858
2859 return ret;
2860}
2861
2862static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2863 struct ocfs2_xattr_tree_root *xt,
2864 char *buffer,
2865 size_t buffer_size)
2866{
2867 struct ocfs2_extent_list *el = &xt->xt_list;
2868 int ret = 0;
2869 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2870 u64 p_blkno = 0;
2871 struct ocfs2_xattr_tree_list xl = {
2872 .buffer = buffer,
2873 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08002874 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08002875 };
2876
2877 if (le16_to_cpu(el->l_next_free_rec) == 0)
2878 return 0;
2879
2880 while (name_hash > 0) {
2881 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2882 &e_cpos, &num_clusters, el);
2883 if (ret) {
2884 mlog_errno(ret);
2885 goto out;
2886 }
2887
2888 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2889 ocfs2_list_xattr_bucket,
2890 &xl);
2891 if (ret) {
2892 mlog_errno(ret);
2893 goto out;
2894 }
2895
2896 if (e_cpos == 0)
2897 break;
2898
2899 name_hash = e_cpos - 1;
2900 }
2901
Tao Ma936b8832008-10-09 23:06:14 +08002902 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08002903out:
2904 return ret;
2905}
Tao Ma01225592008-08-18 17:38:53 +08002906
2907static int cmp_xe(const void *a, const void *b)
2908{
2909 const struct ocfs2_xattr_entry *l = a, *r = b;
2910 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2911 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2912
2913 if (l_hash > r_hash)
2914 return 1;
2915 if (l_hash < r_hash)
2916 return -1;
2917 return 0;
2918}
2919
2920static void swap_xe(void *a, void *b, int size)
2921{
2922 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2923
2924 tmp = *l;
2925 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2926 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2927}
2928
2929/*
2930 * When the ocfs2_xattr_block is filled up, new bucket will be created
2931 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07002932 * The header goes at the start of the bucket, and the names+values are
2933 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08002934 * Note: we need to sort the entries since they are not saved in order
2935 * in the ocfs2_xattr_block.
2936 */
2937static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2938 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07002939 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08002940{
2941 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07002942 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08002943 u16 offset, size, off_change;
2944 struct ocfs2_xattr_entry *xe;
2945 struct ocfs2_xattr_block *xb =
2946 (struct ocfs2_xattr_block *)xb_bh->b_data;
2947 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07002948 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08002949 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07002950 char *src = xb_bh->b_data;
2951 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08002952
2953 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2954 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07002955 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08002956
Joel Becker178eeac2008-10-27 15:18:29 -07002957 for (i = 0; i < blks; i++)
2958 memset(bucket_block(bucket, i), 0, blocksize);
2959
Tao Ma01225592008-08-18 17:38:53 +08002960 /*
2961 * Since the xe_name_offset is based on ocfs2_xattr_header,
2962 * there is a offset change corresponding to the change of
2963 * ocfs2_xattr_header's position.
2964 */
2965 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2966 xe = &xb_xh->xh_entries[count - 1];
2967 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2968 size = blocksize - offset;
2969
2970 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08002971 memcpy(target + offset, src + offset, size);
2972
2973 /* Init new header now. */
2974 xh->xh_count = xb_xh->xh_count;
2975 xh->xh_num_buckets = cpu_to_le16(1);
2976 xh->xh_name_value_len = cpu_to_le16(size);
2977 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2978
2979 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07002980 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08002981 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2982 size = count * sizeof(struct ocfs2_xattr_entry);
2983 memcpy(target + offset, (char *)xb_xh + offset, size);
2984
2985 /* Change the xe offset for all the xe because of the move. */
2986 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2987 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2988 for (i = 0; i < count; i++)
2989 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2990
2991 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2992 offset, size, off_change);
2993
2994 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2995 cmp_xe, swap_xe);
2996}
2997
2998/*
2999 * After we move xattr from block to index btree, we have to
3000 * update ocfs2_xattr_search to the new xe and base.
3001 *
3002 * When the entry is in xattr block, xattr_bh indicates the storage place.
3003 * While if the entry is in index b-tree, "bucket" indicates the
3004 * real place of the xattr.
3005 */
Joel Becker178eeac2008-10-27 15:18:29 -07003006static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3007 struct ocfs2_xattr_search *xs,
3008 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003009{
Tao Ma01225592008-08-18 17:38:53 +08003010 char *buf = old_bh->b_data;
3011 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3012 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003013 int i;
Tao Ma01225592008-08-18 17:38:53 +08003014
Joel Beckerba937122008-10-24 19:13:20 -07003015 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003016 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003017 xs->end = xs->base + inode->i_sb->s_blocksize;
3018
Joel Becker178eeac2008-10-27 15:18:29 -07003019 if (xs->not_found)
3020 return;
Tao Ma01225592008-08-18 17:38:53 +08003021
Joel Becker178eeac2008-10-27 15:18:29 -07003022 i = xs->here - old_xh->xh_entries;
3023 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003024}
3025
3026static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003027 struct ocfs2_xattr_search *xs,
3028 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003029{
Tao Ma85db90e2008-11-12 08:27:01 +08003030 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003031 u32 bit_off, len;
3032 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003033 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003034 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3035 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003036 struct buffer_head *xb_bh = xs->xattr_bh;
3037 struct ocfs2_xattr_block *xb =
3038 (struct ocfs2_xattr_block *)xb_bh->b_data;
3039 struct ocfs2_xattr_tree_root *xr;
3040 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003041
3042 mlog(0, "create xattr index block for %llu\n",
3043 (unsigned long long)xb_bh->b_blocknr);
3044
3045 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003046 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003047
Tao Ma01225592008-08-18 17:38:53 +08003048 /*
3049 * XXX:
3050 * We can use this lock for now, and maybe move to a dedicated mutex
3051 * if performance becomes a problem later.
3052 */
3053 down_write(&oi->ip_alloc_sem);
3054
Tao Ma01225592008-08-18 17:38:53 +08003055 ret = ocfs2_journal_access(handle, inode, xb_bh,
3056 OCFS2_JOURNAL_ACCESS_WRITE);
3057 if (ret) {
3058 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003059 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003060 }
3061
Tao Ma78f30c32008-11-12 08:27:00 +08003062 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3063 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003064 if (ret) {
3065 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003066 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003067 }
3068
3069 /*
3070 * The bucket may spread in many blocks, and
3071 * we will only touch the 1st block and the last block
3072 * in the whole bucket(one for entry and one for data).
3073 */
3074 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3075
Mark Fashehde29c082008-10-29 14:45:30 -07003076 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3077 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003078
Joel Becker178eeac2008-10-27 15:18:29 -07003079 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003080 if (ret) {
3081 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003082 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003083 }
3084
Joel Becker178eeac2008-10-27 15:18:29 -07003085 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3086 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003087 if (ret) {
3088 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003089 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003090 }
Tao Ma01225592008-08-18 17:38:53 +08003091
Joel Becker178eeac2008-10-27 15:18:29 -07003092 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3093 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3094
3095 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3096
Tao Ma01225592008-08-18 17:38:53 +08003097 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3098 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3099 offsetof(struct ocfs2_xattr_block, xb_attrs));
3100
3101 xr = &xb->xb_attrs.xb_root;
3102 xr->xt_clusters = cpu_to_le32(1);
3103 xr->xt_last_eb_blk = 0;
3104 xr->xt_list.l_tree_depth = 0;
3105 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3106 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3107
3108 xr->xt_list.l_recs[0].e_cpos = 0;
3109 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3110 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3111
3112 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3113
Tao Ma85db90e2008-11-12 08:27:01 +08003114 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003115
Tao Ma85db90e2008-11-12 08:27:01 +08003116out:
Tao Ma01225592008-08-18 17:38:53 +08003117 up_write(&oi->ip_alloc_sem);
3118
Tao Ma01225592008-08-18 17:38:53 +08003119 return ret;
3120}
3121
3122static int cmp_xe_offset(const void *a, const void *b)
3123{
3124 const struct ocfs2_xattr_entry *l = a, *r = b;
3125 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3126 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3127
3128 if (l_name_offset < r_name_offset)
3129 return 1;
3130 if (l_name_offset > r_name_offset)
3131 return -1;
3132 return 0;
3133}
3134
3135/*
3136 * defrag a xattr bucket if we find that the bucket has some
3137 * holes beteen name/value pairs.
3138 * We will move all the name/value pairs to the end of the bucket
3139 * so that we can spare some space for insertion.
3140 */
3141static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003142 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003143 struct ocfs2_xattr_bucket *bucket)
3144{
3145 int ret, i;
3146 size_t end, offset, len, value_len;
3147 struct ocfs2_xattr_header *xh;
3148 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003149 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003150 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003151 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003152 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003153
3154 /*
3155 * In order to make the operation more efficient and generic,
3156 * we copy all the blocks into a contiguous memory and do the
3157 * defragment there, so if anything is error, we will not touch
3158 * the real block.
3159 */
3160 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3161 if (!bucket_buf) {
3162 ret = -EIO;
3163 goto out;
3164 }
3165
Joel Becker161d6f32008-10-27 15:25:18 -07003166 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003167 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3168 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003169
Tao Ma1c32a2f2008-11-06 08:10:47 +08003170 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003171 OCFS2_JOURNAL_ACCESS_WRITE);
3172 if (ret < 0) {
3173 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003174 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003175 }
3176
3177 xh = (struct ocfs2_xattr_header *)bucket_buf;
3178 entries = (char *)xh->xh_entries;
3179 xh_free_start = le16_to_cpu(xh->xh_free_start);
3180
3181 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3182 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003183 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3184 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003185
3186 /*
3187 * sort all the entries by their offset.
3188 * the largest will be the first, so that we can
3189 * move them to the end one by one.
3190 */
3191 sort(entries, le16_to_cpu(xh->xh_count),
3192 sizeof(struct ocfs2_xattr_entry),
3193 cmp_xe_offset, swap_xe);
3194
3195 /* Move all name/values to the end of the bucket. */
3196 xe = xh->xh_entries;
3197 end = OCFS2_XATTR_BUCKET_SIZE;
3198 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3199 offset = le16_to_cpu(xe->xe_name_offset);
3200 if (ocfs2_xattr_is_local(xe))
3201 value_len = OCFS2_XATTR_SIZE(
3202 le64_to_cpu(xe->xe_value_size));
3203 else
3204 value_len = OCFS2_XATTR_ROOT_SIZE;
3205 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3206
3207 /*
3208 * We must make sure that the name/value pair
3209 * exist in the same block. So adjust end to
3210 * the previous block end if needed.
3211 */
3212 if (((end - len) / blocksize !=
3213 (end - 1) / blocksize))
3214 end = end - end % blocksize;
3215
3216 if (end > offset + len) {
3217 memmove(bucket_buf + end - len,
3218 bucket_buf + offset, len);
3219 xe->xe_name_offset = cpu_to_le16(end - len);
3220 }
3221
3222 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3223 "bucket %llu\n", (unsigned long long)blkno);
3224
3225 end -= len;
3226 }
3227
3228 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3229 "bucket %llu\n", (unsigned long long)blkno);
3230
3231 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003232 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003233
3234 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3235 xh->xh_free_start = cpu_to_le16(end);
3236
3237 /* sort the entries by their name_hash. */
3238 sort(entries, le16_to_cpu(xh->xh_count),
3239 sizeof(struct ocfs2_xattr_entry),
3240 cmp_xe, swap_xe);
3241
3242 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003243 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3244 memcpy(bucket_block(bucket, i), buf, blocksize);
3245 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003246
Tao Ma01225592008-08-18 17:38:53 +08003247out:
Tao Ma01225592008-08-18 17:38:53 +08003248 kfree(bucket_buf);
3249 return ret;
3250}
3251
3252/*
3253 * Move half nums of the xattr bucket in the previous cluster to this new
3254 * cluster. We only touch the last cluster of the previous extend record.
3255 *
3256 * first_bh is the first buffer_head of a series of bucket in the same
3257 * extent rec and header_bh is the header of one bucket in this cluster.
3258 * They will be updated if we move the data header_bh contains to the new
3259 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3260 */
3261static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3262 handle_t *handle,
3263 struct buffer_head **first_bh,
3264 struct buffer_head **header_bh,
3265 u64 new_blkno,
3266 u64 prev_blkno,
3267 u32 num_clusters,
3268 u32 *first_hash)
3269{
3270 int i, ret, credits;
3271 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3272 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3273 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3274 int blocksize = inode->i_sb->s_blocksize;
3275 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3276 struct ocfs2_xattr_header *new_xh;
3277 struct ocfs2_xattr_header *xh =
3278 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3279
3280 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3281 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3282
3283 prev_bh = *first_bh;
3284 get_bh(prev_bh);
3285 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3286
3287 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3288
3289 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003290 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003291
3292 /*
3293 * We need to update the 1st half of the new cluster and
3294 * 1 more for the update of the 1st bucket of the previous
3295 * extent record.
3296 */
Tao Ma85db90e2008-11-12 08:27:01 +08003297 credits = bpc / 2 + 1 + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003298 ret = ocfs2_extend_trans(handle, credits);
3299 if (ret) {
3300 mlog_errno(ret);
3301 goto out;
3302 }
3303
3304 ret = ocfs2_journal_access(handle, inode, prev_bh,
3305 OCFS2_JOURNAL_ACCESS_WRITE);
3306 if (ret) {
3307 mlog_errno(ret);
3308 goto out;
3309 }
3310
3311 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3312 old_bh = new_bh = NULL;
3313 new_bh = sb_getblk(inode->i_sb, new_blkno);
3314 if (!new_bh) {
3315 ret = -EIO;
3316 mlog_errno(ret);
3317 goto out;
3318 }
3319
3320 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3321
3322 ret = ocfs2_journal_access(handle, inode, new_bh,
3323 OCFS2_JOURNAL_ACCESS_CREATE);
3324 if (ret < 0) {
3325 mlog_errno(ret);
3326 brelse(new_bh);
3327 goto out;
3328 }
3329
Joel Becker0fcaa562008-10-09 17:20:31 -07003330 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
Tao Ma01225592008-08-18 17:38:53 +08003331 if (ret < 0) {
3332 mlog_errno(ret);
3333 brelse(new_bh);
3334 goto out;
3335 }
3336
3337 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3338
3339 if (i == 0) {
3340 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3341 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3342
3343 if (first_hash)
3344 *first_hash = le32_to_cpu(
3345 new_xh->xh_entries[0].xe_name_hash);
3346 new_first_bh = new_bh;
3347 get_bh(new_first_bh);
3348 }
3349
3350 ocfs2_journal_dirty(handle, new_bh);
3351
3352 if (*header_bh == old_bh) {
3353 brelse(*header_bh);
3354 *header_bh = new_bh;
3355 get_bh(*header_bh);
3356
3357 brelse(*first_bh);
3358 *first_bh = new_first_bh;
3359 get_bh(*first_bh);
3360 }
3361 brelse(new_bh);
3362 brelse(old_bh);
3363 }
3364
3365 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3366
3367 ocfs2_journal_dirty(handle, prev_bh);
3368out:
3369 brelse(prev_bh);
3370 brelse(new_first_bh);
3371 return ret;
3372}
3373
Tao Ma01225592008-08-18 17:38:53 +08003374/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003375 * Find the suitable pos when we divide a bucket into 2.
3376 * We have to make sure the xattrs with the same hash value exist
3377 * in the same bucket.
3378 *
3379 * If this ocfs2_xattr_header covers more than one hash value, find a
3380 * place where the hash value changes. Try to find the most even split.
3381 * The most common case is that all entries have different hash values,
3382 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003383 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003384static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3385{
3386 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3387 int count = le16_to_cpu(xh->xh_count);
3388 int delta, middle = count / 2;
3389
3390 /*
3391 * We start at the middle. Each step gets farther away in both
3392 * directions. We therefore hit the change in hash value
3393 * nearest to the middle. Note that this loop does not execute for
3394 * count < 2.
3395 */
3396 for (delta = 0; delta < middle; delta++) {
3397 /* Let's check delta earlier than middle */
3398 if (cmp_xe(&entries[middle - delta - 1],
3399 &entries[middle - delta]))
3400 return middle - delta;
3401
3402 /* For even counts, don't walk off the end */
3403 if ((middle + delta + 1) == count)
3404 continue;
3405
3406 /* Now try delta past middle */
3407 if (cmp_xe(&entries[middle + delta],
3408 &entries[middle + delta + 1]))
3409 return middle + delta + 1;
3410 }
3411
3412 /* Every entry had the same hash */
3413 return count;
3414}
3415
3416/*
3417 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3418 * first_hash will record the 1st hash of the new bucket.
3419 *
3420 * Normally half of the xattrs will be moved. But we have to make
3421 * sure that the xattrs with the same hash value are stored in the
3422 * same bucket. If all the xattrs in this bucket have the same hash
3423 * value, the new bucket will be initialized as an empty one and the
3424 * first_hash will be initialized as (hash_value+1).
3425 */
3426static int ocfs2_divide_xattr_bucket(struct inode *inode,
3427 handle_t *handle,
3428 u64 blk,
3429 u64 new_blk,
3430 u32 *first_hash,
3431 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003432{
3433 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003434 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003435 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003436 struct ocfs2_xattr_header *xh;
3437 struct ocfs2_xattr_entry *xe;
3438 int blocksize = inode->i_sb->s_blocksize;
3439
Tao Ma80bcaf32008-10-27 06:06:24 +08003440 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003441 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003442
Joel Beckerba937122008-10-24 19:13:20 -07003443 s_bucket = ocfs2_xattr_bucket_new(inode);
3444 t_bucket = ocfs2_xattr_bucket_new(inode);
3445 if (!s_bucket || !t_bucket) {
3446 ret = -ENOMEM;
3447 mlog_errno(ret);
3448 goto out;
3449 }
Tao Ma01225592008-08-18 17:38:53 +08003450
Joel Beckerba937122008-10-24 19:13:20 -07003451 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003452 if (ret) {
3453 mlog_errno(ret);
3454 goto out;
3455 }
3456
Joel Beckerba937122008-10-24 19:13:20 -07003457 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003458 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003459 if (ret) {
3460 mlog_errno(ret);
3461 goto out;
3462 }
3463
Joel Becker784b8162008-10-24 17:33:40 -07003464 /*
3465 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3466 * there's no need to read it.
3467 */
Joel Beckerba937122008-10-24 19:13:20 -07003468 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003469 if (ret) {
3470 mlog_errno(ret);
3471 goto out;
3472 }
3473
Joel Beckerba937122008-10-24 19:13:20 -07003474 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003475 new_bucket_head ?
3476 OCFS2_JOURNAL_ACCESS_CREATE :
3477 OCFS2_JOURNAL_ACCESS_WRITE);
3478 if (ret) {
3479 mlog_errno(ret);
3480 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003481 }
3482
Joel Beckerba937122008-10-24 19:13:20 -07003483 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003484 count = le16_to_cpu(xh->xh_count);
3485 start = ocfs2_xattr_find_divide_pos(xh);
3486
3487 if (start == count) {
3488 xe = &xh->xh_entries[start-1];
3489
3490 /*
3491 * initialized a new empty bucket here.
3492 * The hash value is set as one larger than
3493 * that of the last entry in the previous bucket.
3494 */
Joel Beckerba937122008-10-24 19:13:20 -07003495 for (i = 0; i < t_bucket->bu_blocks; i++)
3496 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003497
Joel Beckerba937122008-10-24 19:13:20 -07003498 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003499 xh->xh_free_start = cpu_to_le16(blocksize);
3500 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3501 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3502
3503 goto set_num_buckets;
3504 }
3505
Tao Ma01225592008-08-18 17:38:53 +08003506 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003507 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003508
3509 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003510 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003511
3512 /*
3513 * Calculate the total name/value len and xh_free_start for
3514 * the old bucket first.
3515 */
3516 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3517 name_value_len = 0;
3518 for (i = 0; i < start; i++) {
3519 xe = &xh->xh_entries[i];
3520 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3521 if (ocfs2_xattr_is_local(xe))
3522 xe_len +=
3523 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3524 else
3525 xe_len += OCFS2_XATTR_ROOT_SIZE;
3526 name_value_len += xe_len;
3527 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3528 name_offset = le16_to_cpu(xe->xe_name_offset);
3529 }
3530
3531 /*
3532 * Now begin the modification to the new bucket.
3533 *
3534 * In the new bucket, We just move the xattr entry to the beginning
3535 * and don't touch the name/value. So there will be some holes in the
3536 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3537 * called.
3538 */
3539 xe = &xh->xh_entries[start];
3540 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3541 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003542 (int)((char *)xe - (char *)xh),
3543 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003544 memmove((char *)xh->xh_entries, (char *)xe, len);
3545 xe = &xh->xh_entries[count - start];
3546 len = sizeof(struct ocfs2_xattr_entry) * start;
3547 memset((char *)xe, 0, len);
3548
3549 le16_add_cpu(&xh->xh_count, -start);
3550 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3551
3552 /* Calculate xh_free_start for the new bucket. */
3553 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3554 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3555 xe = &xh->xh_entries[i];
3556 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3557 if (ocfs2_xattr_is_local(xe))
3558 xe_len +=
3559 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3560 else
3561 xe_len += OCFS2_XATTR_ROOT_SIZE;
3562 if (le16_to_cpu(xe->xe_name_offset) <
3563 le16_to_cpu(xh->xh_free_start))
3564 xh->xh_free_start = xe->xe_name_offset;
3565 }
3566
Tao Ma80bcaf32008-10-27 06:06:24 +08003567set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003568 /* set xh->xh_num_buckets for the new xh. */
3569 if (new_bucket_head)
3570 xh->xh_num_buckets = cpu_to_le16(1);
3571 else
3572 xh->xh_num_buckets = 0;
3573
Joel Beckerba937122008-10-24 19:13:20 -07003574 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003575
3576 /* store the first_hash of the new bucket. */
3577 if (first_hash)
3578 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3579
3580 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003581 * Now only update the 1st block of the old bucket. If we
3582 * just added a new empty bucket, there is no need to modify
3583 * it.
Tao Ma01225592008-08-18 17:38:53 +08003584 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003585 if (start == count)
3586 goto out;
3587
Joel Beckerba937122008-10-24 19:13:20 -07003588 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003589 memset(&xh->xh_entries[start], 0,
3590 sizeof(struct ocfs2_xattr_entry) * (count - start));
3591 xh->xh_count = cpu_to_le16(start);
3592 xh->xh_free_start = cpu_to_le16(name_offset);
3593 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3594
Joel Beckerba937122008-10-24 19:13:20 -07003595 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003596
3597out:
Joel Beckerba937122008-10-24 19:13:20 -07003598 ocfs2_xattr_bucket_free(s_bucket);
3599 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003600
3601 return ret;
3602}
3603
3604/*
3605 * Copy xattr from one bucket to another bucket.
3606 *
3607 * The caller must make sure that the journal transaction
3608 * has enough space for journaling.
3609 */
3610static int ocfs2_cp_xattr_bucket(struct inode *inode,
3611 handle_t *handle,
3612 u64 s_blkno,
3613 u64 t_blkno,
3614 int t_is_new)
3615{
Joel Becker4980c6d2008-10-24 18:54:43 -07003616 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003617 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003618
3619 BUG_ON(s_blkno == t_blkno);
3620
3621 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003622 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3623 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003624
Joel Beckerba937122008-10-24 19:13:20 -07003625 s_bucket = ocfs2_xattr_bucket_new(inode);
3626 t_bucket = ocfs2_xattr_bucket_new(inode);
3627 if (!s_bucket || !t_bucket) {
3628 ret = -ENOMEM;
3629 mlog_errno(ret);
3630 goto out;
3631 }
3632
3633 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003634 if (ret)
3635 goto out;
3636
Joel Becker784b8162008-10-24 17:33:40 -07003637 /*
3638 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3639 * there's no need to read it.
3640 */
Joel Beckerba937122008-10-24 19:13:20 -07003641 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003642 if (ret)
3643 goto out;
3644
Joel Beckerba937122008-10-24 19:13:20 -07003645 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003646 t_is_new ?
3647 OCFS2_JOURNAL_ACCESS_CREATE :
3648 OCFS2_JOURNAL_ACCESS_WRITE);
3649 if (ret)
3650 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003651
Joel Beckerba937122008-10-24 19:13:20 -07003652 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3653 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003654
3655out:
Joel Beckerba937122008-10-24 19:13:20 -07003656 ocfs2_xattr_bucket_free(t_bucket);
3657 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003658
3659 return ret;
3660}
3661
3662/*
3663 * Copy one xattr cluster from src_blk to to_blk.
3664 * The to_blk will become the first bucket header of the cluster, so its
3665 * xh_num_buckets will be initialized as the bucket num in the cluster.
3666 */
3667static int ocfs2_cp_xattr_cluster(struct inode *inode,
3668 handle_t *handle,
3669 struct buffer_head *first_bh,
3670 u64 src_blk,
3671 u64 to_blk,
3672 u32 *first_hash)
3673{
3674 int i, ret, credits;
3675 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3676 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3677 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3678 struct buffer_head *bh = NULL;
3679 struct ocfs2_xattr_header *xh;
3680 u64 to_blk_start = to_blk;
3681
Mark Fashehde29c082008-10-29 14:45:30 -07003682 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3683 (unsigned long long)src_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003684
3685 /*
3686 * We need to update the new cluster and 1 more for the update of
3687 * the 1st bucket of the previous extent rec.
3688 */
Tao Ma85db90e2008-11-12 08:27:01 +08003689 credits = bpc + 1 + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003690 ret = ocfs2_extend_trans(handle, credits);
3691 if (ret) {
3692 mlog_errno(ret);
3693 goto out;
3694 }
3695
3696 ret = ocfs2_journal_access(handle, inode, first_bh,
3697 OCFS2_JOURNAL_ACCESS_WRITE);
3698 if (ret) {
3699 mlog_errno(ret);
3700 goto out;
3701 }
3702
3703 for (i = 0; i < num_buckets; i++) {
3704 ret = ocfs2_cp_xattr_bucket(inode, handle,
3705 src_blk, to_blk, 1);
3706 if (ret) {
3707 mlog_errno(ret);
3708 goto out;
3709 }
3710
3711 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3712 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3713 }
3714
3715 /* update the old bucket header. */
3716 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3717 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3718
3719 ocfs2_journal_dirty(handle, first_bh);
3720
3721 /* update the new bucket header. */
Joel Becker0fcaa562008-10-09 17:20:31 -07003722 ret = ocfs2_read_block(inode, to_blk_start, &bh);
Tao Ma01225592008-08-18 17:38:53 +08003723 if (ret < 0) {
3724 mlog_errno(ret);
3725 goto out;
3726 }
3727
3728 ret = ocfs2_journal_access(handle, inode, bh,
3729 OCFS2_JOURNAL_ACCESS_WRITE);
3730 if (ret) {
3731 mlog_errno(ret);
3732 goto out;
3733 }
3734
3735 xh = (struct ocfs2_xattr_header *)bh->b_data;
3736 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3737
3738 ocfs2_journal_dirty(handle, bh);
3739
3740 if (first_hash)
3741 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3742out:
3743 brelse(bh);
3744 return ret;
3745}
3746
3747/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003748 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08003749 * This function should only be called when bucket size == cluster size.
3750 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3751 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003752static int ocfs2_divide_xattr_cluster(struct inode *inode,
3753 handle_t *handle,
3754 u64 prev_blk,
3755 u64 new_blk,
3756 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003757{
3758 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08003759 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003760
3761 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3762
3763 ret = ocfs2_extend_trans(handle, credits);
3764 if (ret) {
3765 mlog_errno(ret);
3766 return ret;
3767 }
3768
3769 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08003770 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3771 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08003772}
3773
3774/*
3775 * Move some xattrs from the old cluster to the new one since they are not
3776 * contiguous in ocfs2 xattr tree.
3777 *
3778 * new_blk starts a new separate cluster, and we will move some xattrs from
3779 * prev_blk to it. v_start will be set as the first name hash value in this
3780 * new cluster so that it can be used as e_cpos during tree insertion and
3781 * don't collide with our original b-tree operations. first_bh and header_bh
3782 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3783 * to extend the insert bucket.
3784 *
3785 * The problem is how much xattr should we move to the new one and when should
3786 * we update first_bh and header_bh?
3787 * 1. If cluster size > bucket size, that means the previous cluster has more
3788 * than 1 bucket, so just move half nums of bucket into the new cluster and
3789 * update the first_bh and header_bh if the insert bucket has been moved
3790 * to the new cluster.
3791 * 2. If cluster_size == bucket_size:
3792 * a) If the previous extent rec has more than one cluster and the insert
3793 * place isn't in the last cluster, copy the entire last cluster to the
3794 * new one. This time, we don't need to upate the first_bh and header_bh
3795 * since they will not be moved into the new cluster.
3796 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3797 * the new one. And we set the extend flag to zero if the insert place is
3798 * moved into the new allocated cluster since no extend is needed.
3799 */
3800static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3801 handle_t *handle,
3802 struct buffer_head **first_bh,
3803 struct buffer_head **header_bh,
3804 u64 new_blk,
3805 u64 prev_blk,
3806 u32 prev_clusters,
3807 u32 *v_start,
3808 int *extend)
3809{
3810 int ret = 0;
3811 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3812
3813 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003814 (unsigned long long)prev_blk, prev_clusters,
3815 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003816
3817 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3818 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3819 handle,
3820 first_bh,
3821 header_bh,
3822 new_blk,
3823 prev_blk,
3824 prev_clusters,
3825 v_start);
3826 else {
3827 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3828
3829 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3830 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3831 last_blk, new_blk,
3832 v_start);
3833 else {
Tao Ma80bcaf32008-10-27 06:06:24 +08003834 ret = ocfs2_divide_xattr_cluster(inode, handle,
3835 last_blk, new_blk,
3836 v_start);
Tao Ma01225592008-08-18 17:38:53 +08003837
3838 if ((*header_bh)->b_blocknr == last_blk && extend)
3839 *extend = 0;
3840 }
3841 }
3842
3843 return ret;
3844}
3845
3846/*
3847 * Add a new cluster for xattr storage.
3848 *
3849 * If the new cluster is contiguous with the previous one, it will be
3850 * appended to the same extent record, and num_clusters will be updated.
3851 * If not, we will insert a new extent for it and move some xattrs in
3852 * the last cluster into the new allocated one.
3853 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3854 * lose the benefits of hashing because we'll have to search large leaves.
3855 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3856 * if it's bigger).
3857 *
3858 * first_bh is the first block of the previous extent rec and header_bh
3859 * indicates the bucket we will insert the new xattrs. They will be updated
3860 * when the header_bh is moved into the new cluster.
3861 */
3862static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3863 struct buffer_head *root_bh,
3864 struct buffer_head **first_bh,
3865 struct buffer_head **header_bh,
3866 u32 *num_clusters,
3867 u32 prev_cpos,
3868 u64 prev_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08003869 int *extend,
3870 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003871{
Tao Ma85db90e2008-11-12 08:27:01 +08003872 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003873 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3874 u32 prev_clusters = *num_clusters;
3875 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3876 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08003877 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003878 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003879 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08003880
3881 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3882 "previous xattr blkno = %llu\n",
3883 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehde29c082008-10-29 14:45:30 -07003884 prev_cpos, (unsigned long long)prev_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003885
Joel Becker8d6220d2008-08-22 12:46:09 -07003886 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003887
Tao Ma01225592008-08-18 17:38:53 +08003888 ret = ocfs2_journal_access(handle, inode, root_bh,
3889 OCFS2_JOURNAL_ACCESS_WRITE);
3890 if (ret < 0) {
3891 mlog_errno(ret);
3892 goto leave;
3893 }
3894
Tao Ma78f30c32008-11-12 08:27:00 +08003895 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08003896 clusters_to_add, &bit_off, &num_bits);
3897 if (ret < 0) {
3898 if (ret != -ENOSPC)
3899 mlog_errno(ret);
3900 goto leave;
3901 }
3902
3903 BUG_ON(num_bits > clusters_to_add);
3904
3905 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3906 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3907 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3908
3909 if (prev_blkno + prev_clusters * bpc == block &&
3910 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3911 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3912 /*
3913 * If this cluster is contiguous with the old one and
3914 * adding this new cluster, we don't surpass the limit of
3915 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3916 * initialized and used like other buckets in the previous
3917 * cluster.
3918 * So add it as a contiguous one. The caller will handle
3919 * its init process.
3920 */
3921 v_start = prev_cpos + prev_clusters;
3922 *num_clusters = prev_clusters + num_bits;
3923 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3924 num_bits);
3925 } else {
3926 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3927 handle,
3928 first_bh,
3929 header_bh,
3930 block,
3931 prev_blkno,
3932 prev_clusters,
3933 &v_start,
3934 extend);
3935 if (ret) {
3936 mlog_errno(ret);
3937 goto leave;
3938 }
3939 }
3940
3941 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003942 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003943 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08003944 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08003945 if (ret < 0) {
3946 mlog_errno(ret);
3947 goto leave;
3948 }
3949
3950 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08003951 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08003952 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08003953
3954leave:
Tao Ma01225592008-08-18 17:38:53 +08003955 return ret;
3956}
3957
3958/*
3959 * Extend a new xattr bucket and move xattrs to the end one by one until
3960 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3961 */
3962static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003963 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003964 struct buffer_head *first_bh,
3965 struct buffer_head *start_bh,
3966 u32 num_clusters)
3967{
3968 int ret, credits;
3969 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3970 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3971 u64 start_blk = start_bh->b_blocknr, end_blk;
3972 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
Tao Ma01225592008-08-18 17:38:53 +08003973 struct ocfs2_xattr_header *first_xh =
3974 (struct ocfs2_xattr_header *)first_bh->b_data;
3975 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3976
3977 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Mark Fashehde29c082008-10-29 14:45:30 -07003978 "from %llu, len = %u\n", (unsigned long long)start_blk,
Tao Ma01225592008-08-18 17:38:53 +08003979 (unsigned long long)first_bh->b_blocknr, num_clusters);
3980
3981 BUG_ON(bucket >= num_buckets);
3982
3983 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3984
3985 /*
3986 * We will touch all the buckets after the start_bh(include it).
Joel Becker1224be02008-10-24 18:47:33 -07003987 * Then we add one more bucket.
Tao Ma01225592008-08-18 17:38:53 +08003988 */
Tao Ma85db90e2008-11-12 08:27:01 +08003989 credits = end_blk - start_blk + 3 * blk_per_bucket + 1 +
3990 handle->h_buffer_credits;
3991 ret = ocfs2_extend_trans(handle, credits);
3992 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08003993 mlog_errno(ret);
3994 goto out;
3995 }
3996
3997 ret = ocfs2_journal_access(handle, inode, first_bh,
3998 OCFS2_JOURNAL_ACCESS_WRITE);
3999 if (ret) {
4000 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004001 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004002 }
4003
4004 while (end_blk != start_blk) {
4005 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4006 end_blk + blk_per_bucket, 0);
4007 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004008 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004009 end_blk -= blk_per_bucket;
4010 }
4011
4012 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004013 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
4014 start_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004015
4016 le16_add_cpu(&first_xh->xh_num_buckets, 1);
4017 ocfs2_journal_dirty(handle, first_bh);
4018
Tao Ma01225592008-08-18 17:38:53 +08004019out:
4020 return ret;
4021}
4022
4023/*
4024 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4025 * xb_bh is the ocfs2_xattr_block.
4026 * We will move all the buckets starting from header_bh to the next place. As
4027 * for this one, half num of its xattrs will be moved to the next one.
4028 *
4029 * We will allocate a new cluster if current cluster is full and adjust
4030 * header_bh and first_bh if the insert place is moved to the new cluster.
4031 */
4032static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4033 struct buffer_head *xb_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08004034 struct buffer_head *header_bh,
4035 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004036{
4037 struct ocfs2_xattr_header *first_xh = NULL;
4038 struct buffer_head *first_bh = NULL;
4039 struct ocfs2_xattr_block *xb =
4040 (struct ocfs2_xattr_block *)xb_bh->b_data;
4041 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4042 struct ocfs2_extent_list *el = &xb_root->xt_list;
4043 struct ocfs2_xattr_header *xh =
4044 (struct ocfs2_xattr_header *)header_bh->b_data;
4045 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4046 struct super_block *sb = inode->i_sb;
4047 struct ocfs2_super *osb = OCFS2_SB(sb);
4048 int ret, num_buckets, extend = 1;
4049 u64 p_blkno;
4050 u32 e_cpos, num_clusters;
4051
4052 mlog(0, "Add new xattr bucket starting form %llu\n",
4053 (unsigned long long)header_bh->b_blocknr);
4054
4055 /*
4056 * Add refrence for header_bh here because it may be
4057 * changed in ocfs2_add_new_xattr_cluster and we need
4058 * to free it in the end.
4059 */
4060 get_bh(header_bh);
4061
4062 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4063 &num_clusters, el);
4064 if (ret) {
4065 mlog_errno(ret);
4066 goto out;
4067 }
4068
Joel Becker0fcaa562008-10-09 17:20:31 -07004069 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
Tao Ma01225592008-08-18 17:38:53 +08004070 if (ret) {
4071 mlog_errno(ret);
4072 goto out;
4073 }
4074
4075 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4076 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4077
4078 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4079 ret = ocfs2_add_new_xattr_cluster(inode,
4080 xb_bh,
4081 &first_bh,
4082 &header_bh,
4083 &num_clusters,
4084 e_cpos,
4085 p_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08004086 &extend,
4087 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004088 if (ret) {
4089 mlog_errno(ret);
4090 goto out;
4091 }
4092 }
4093
4094 if (extend)
4095 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004096 ctxt->handle,
Tao Ma01225592008-08-18 17:38:53 +08004097 first_bh,
4098 header_bh,
4099 num_clusters);
4100 if (ret)
4101 mlog_errno(ret);
4102out:
4103 brelse(first_bh);
4104 brelse(header_bh);
4105 return ret;
4106}
4107
4108static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4109 struct ocfs2_xattr_bucket *bucket,
4110 int offs)
4111{
4112 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4113
4114 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004115 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004116}
4117
4118/*
4119 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004120 *
4121 * Note: "local" indicates the real data's locality. So we can't
4122 * just its bucket locality by its length.
4123 */
4124static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4125 struct ocfs2_xattr_info *xi,
4126 struct ocfs2_xattr_search *xs,
4127 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004128 int local)
Tao Ma01225592008-08-18 17:38:53 +08004129{
4130 struct ocfs2_xattr_entry *last, *xe;
4131 int name_len = strlen(xi->name);
4132 struct ocfs2_xattr_header *xh = xs->header;
4133 u16 count = le16_to_cpu(xh->xh_count), start;
4134 size_t blocksize = inode->i_sb->s_blocksize;
4135 char *val;
4136 size_t offs, size, new_size;
4137
4138 last = &xh->xh_entries[count];
4139 if (!xs->not_found) {
4140 xe = xs->here;
4141 offs = le16_to_cpu(xe->xe_name_offset);
4142 if (ocfs2_xattr_is_local(xe))
4143 size = OCFS2_XATTR_SIZE(name_len) +
4144 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4145 else
4146 size = OCFS2_XATTR_SIZE(name_len) +
4147 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4148
4149 /*
4150 * If the new value will be stored outside, xi->value has been
4151 * initalized as an empty ocfs2_xattr_value_root, and the same
4152 * goes with xi->value_len, so we can set new_size safely here.
4153 * See ocfs2_xattr_set_in_bucket.
4154 */
4155 new_size = OCFS2_XATTR_SIZE(name_len) +
4156 OCFS2_XATTR_SIZE(xi->value_len);
4157
4158 le16_add_cpu(&xh->xh_name_value_len, -size);
4159 if (xi->value) {
4160 if (new_size > size)
4161 goto set_new_name_value;
4162
4163 /* Now replace the old value with new one. */
4164 if (local)
4165 xe->xe_value_size = cpu_to_le64(xi->value_len);
4166 else
4167 xe->xe_value_size = 0;
4168
4169 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004170 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004171 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4172 size - OCFS2_XATTR_SIZE(name_len));
4173 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4174 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4175 xi->value, xi->value_len);
4176
4177 le16_add_cpu(&xh->xh_name_value_len, new_size);
4178 ocfs2_xattr_set_local(xe, local);
4179 return;
4180 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004181 /*
4182 * Remove the old entry if there is more than one.
4183 * We don't remove the last entry so that we can
4184 * use it to indicate the hash value of the empty
4185 * bucket.
4186 */
Tao Ma01225592008-08-18 17:38:53 +08004187 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004188 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004189 if (xh->xh_count) {
4190 memmove(xe, xe + 1,
4191 (void *)last - (void *)xe);
4192 memset(last, 0,
4193 sizeof(struct ocfs2_xattr_entry));
4194 } else
4195 xh->xh_free_start =
4196 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4197
Tao Ma01225592008-08-18 17:38:53 +08004198 return;
4199 }
4200 } else {
4201 /* find a new entry for insert. */
4202 int low = 0, high = count - 1, tmp;
4203 struct ocfs2_xattr_entry *tmp_xe;
4204
Tao Ma5a095612008-09-19 22:17:41 +08004205 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004206 tmp = (low + high) / 2;
4207 tmp_xe = &xh->xh_entries[tmp];
4208
4209 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4210 low = tmp + 1;
4211 else if (name_hash <
4212 le32_to_cpu(tmp_xe->xe_name_hash))
4213 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004214 else {
4215 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004216 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004217 }
Tao Ma01225592008-08-18 17:38:53 +08004218 }
4219
4220 xe = &xh->xh_entries[low];
4221 if (low != count)
4222 memmove(xe + 1, xe, (void *)last - (void *)xe);
4223
4224 le16_add_cpu(&xh->xh_count, 1);
4225 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4226 xe->xe_name_hash = cpu_to_le32(name_hash);
4227 xe->xe_name_len = name_len;
4228 ocfs2_xattr_set_type(xe, xi->name_index);
4229 }
4230
4231set_new_name_value:
4232 /* Insert the new name+value. */
4233 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4234
4235 /*
4236 * We must make sure that the name/value pair
4237 * exists in the same block.
4238 */
4239 offs = le16_to_cpu(xh->xh_free_start);
4240 start = offs - size;
4241
4242 if (start >> inode->i_sb->s_blocksize_bits !=
4243 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4244 offs = offs - offs % blocksize;
4245 xh->xh_free_start = cpu_to_le16(offs);
4246 }
4247
Joel Beckerba937122008-10-24 19:13:20 -07004248 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004249 xe->xe_name_offset = cpu_to_le16(offs - size);
4250
4251 memset(val, 0, size);
4252 memcpy(val, xi->name, name_len);
4253 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4254
4255 xe->xe_value_size = cpu_to_le64(xi->value_len);
4256 ocfs2_xattr_set_local(xe, local);
4257 xs->here = xe;
4258 le16_add_cpu(&xh->xh_free_start, -size);
4259 le16_add_cpu(&xh->xh_name_value_len, size);
4260
4261 return;
4262}
4263
Tao Ma01225592008-08-18 17:38:53 +08004264/*
4265 * Set the xattr entry in the specified bucket.
4266 * The bucket is indicated by xs->bucket and it should have the enough
4267 * space for the xattr insertion.
4268 */
4269static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004270 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004271 struct ocfs2_xattr_info *xi,
4272 struct ocfs2_xattr_search *xs,
4273 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004274 int local)
Tao Ma01225592008-08-18 17:38:53 +08004275{
Joel Becker1224be02008-10-24 18:47:33 -07004276 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004277 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004278
Mark Fashehff1ec202008-08-19 10:54:29 -07004279 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4280 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004281 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004282
Joel Beckerba937122008-10-24 19:13:20 -07004283 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004284 blkno = bucket_blkno(xs->bucket);
4285 ocfs2_xattr_bucket_relse(xs->bucket);
4286 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004287 if (ret) {
4288 mlog_errno(ret);
4289 goto out;
4290 }
4291 }
4292
Joel Beckerba937122008-10-24 19:13:20 -07004293 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004294 OCFS2_JOURNAL_ACCESS_WRITE);
4295 if (ret < 0) {
4296 mlog_errno(ret);
4297 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004298 }
4299
Tao Ma5a095612008-09-19 22:17:41 +08004300 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004301 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004302
Tao Ma01225592008-08-18 17:38:53 +08004303out:
Tao Ma01225592008-08-18 17:38:53 +08004304 return ret;
4305}
4306
4307static int ocfs2_xattr_value_update_size(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004308 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004309 struct buffer_head *xe_bh,
4310 struct ocfs2_xattr_entry *xe,
4311 u64 new_size)
4312{
4313 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004314
4315 ret = ocfs2_journal_access(handle, inode, xe_bh,
4316 OCFS2_JOURNAL_ACCESS_WRITE);
4317 if (ret < 0) {
4318 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004319 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004320 }
4321
4322 xe->xe_value_size = cpu_to_le64(new_size);
4323
4324 ret = ocfs2_journal_dirty(handle, xe_bh);
4325 if (ret < 0)
4326 mlog_errno(ret);
4327
Tao Ma01225592008-08-18 17:38:53 +08004328out:
4329 return ret;
4330}
4331
4332/*
4333 * Truncate the specified xe_off entry in xattr bucket.
4334 * bucket is indicated by header_bh and len is the new length.
4335 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4336 *
4337 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4338 */
4339static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4340 struct buffer_head *header_bh,
4341 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004342 int len,
4343 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004344{
4345 int ret, offset;
4346 u64 value_blk;
4347 struct buffer_head *value_bh = NULL;
4348 struct ocfs2_xattr_value_root *xv;
4349 struct ocfs2_xattr_entry *xe;
4350 struct ocfs2_xattr_header *xh =
4351 (struct ocfs2_xattr_header *)header_bh->b_data;
4352 size_t blocksize = inode->i_sb->s_blocksize;
4353
4354 xe = &xh->xh_entries[xe_off];
4355
4356 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4357
4358 offset = le16_to_cpu(xe->xe_name_offset) +
4359 OCFS2_XATTR_SIZE(xe->xe_name_len);
4360
4361 value_blk = offset / blocksize;
4362
4363 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4364 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4365 value_blk += header_bh->b_blocknr;
4366
Joel Becker0fcaa562008-10-09 17:20:31 -07004367 ret = ocfs2_read_block(inode, value_blk, &value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004368 if (ret) {
4369 mlog_errno(ret);
4370 goto out;
4371 }
4372
4373 xv = (struct ocfs2_xattr_value_root *)
4374 (value_bh->b_data + offset % blocksize);
4375
4376 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4377 xe_off, (unsigned long long)header_bh->b_blocknr, len);
Tao Ma78f30c32008-11-12 08:27:00 +08004378 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004379 if (ret) {
4380 mlog_errno(ret);
4381 goto out;
4382 }
4383
Tao Ma85db90e2008-11-12 08:27:01 +08004384 ret = ocfs2_xattr_value_update_size(inode, ctxt->handle,
4385 header_bh, xe, len);
Tao Ma01225592008-08-18 17:38:53 +08004386 if (ret) {
4387 mlog_errno(ret);
4388 goto out;
4389 }
4390
4391out:
4392 brelse(value_bh);
4393 return ret;
4394}
4395
4396static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004397 struct ocfs2_xattr_search *xs,
4398 int len,
4399 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004400{
4401 int ret, offset;
4402 struct ocfs2_xattr_entry *xe = xs->here;
4403 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4404
Joel Beckerba937122008-10-24 19:13:20 -07004405 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004406
4407 offset = xe - xh->xh_entries;
Joel Beckerba937122008-10-24 19:13:20 -07004408 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
Tao Ma78f30c32008-11-12 08:27:00 +08004409 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004410 if (ret)
4411 mlog_errno(ret);
4412
4413 return ret;
4414}
4415
4416static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004417 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004418 struct ocfs2_xattr_search *xs,
4419 char *val,
4420 int value_len)
4421{
4422 int offset;
4423 struct ocfs2_xattr_value_root *xv;
4424 struct ocfs2_xattr_entry *xe = xs->here;
4425
4426 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4427
4428 offset = le16_to_cpu(xe->xe_name_offset) +
4429 OCFS2_XATTR_SIZE(xe->xe_name_len);
4430
4431 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4432
Tao Ma85db90e2008-11-12 08:27:01 +08004433 return __ocfs2_xattr_set_value_outside(inode, handle,
4434 xv, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004435}
4436
Tao Ma01225592008-08-18 17:38:53 +08004437static int ocfs2_rm_xattr_cluster(struct inode *inode,
4438 struct buffer_head *root_bh,
4439 u64 blkno,
4440 u32 cpos,
4441 u32 len)
4442{
4443 int ret;
4444 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4445 struct inode *tl_inode = osb->osb_tl_inode;
4446 handle_t *handle;
4447 struct ocfs2_xattr_block *xb =
4448 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004449 struct ocfs2_alloc_context *meta_ac = NULL;
4450 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004451 struct ocfs2_extent_tree et;
4452
Joel Becker8d6220d2008-08-22 12:46:09 -07004453 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004454
4455 ocfs2_init_dealloc_ctxt(&dealloc);
4456
4457 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4458 cpos, len, (unsigned long long)blkno);
4459
4460 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4461
Joel Beckerf99b9b72008-08-20 19:36:33 -07004462 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004463 if (ret) {
4464 mlog_errno(ret);
4465 return ret;
4466 }
4467
4468 mutex_lock(&tl_inode->i_mutex);
4469
4470 if (ocfs2_truncate_log_needs_flush(osb)) {
4471 ret = __ocfs2_flush_truncate_log(osb);
4472 if (ret < 0) {
4473 mlog_errno(ret);
4474 goto out;
4475 }
4476 }
4477
4478 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
Tao Mad3264792008-10-24 07:57:28 +08004479 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004480 ret = -ENOMEM;
4481 mlog_errno(ret);
4482 goto out;
4483 }
4484
4485 ret = ocfs2_journal_access(handle, inode, root_bh,
4486 OCFS2_JOURNAL_ACCESS_WRITE);
4487 if (ret) {
4488 mlog_errno(ret);
4489 goto out_commit;
4490 }
4491
Joel Beckerf99b9b72008-08-20 19:36:33 -07004492 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4493 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004494 if (ret) {
4495 mlog_errno(ret);
4496 goto out_commit;
4497 }
4498
4499 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4500
4501 ret = ocfs2_journal_dirty(handle, root_bh);
4502 if (ret) {
4503 mlog_errno(ret);
4504 goto out_commit;
4505 }
4506
4507 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4508 if (ret)
4509 mlog_errno(ret);
4510
4511out_commit:
4512 ocfs2_commit_trans(osb, handle);
4513out:
4514 ocfs2_schedule_truncate_log_flush(osb, 1);
4515
4516 mutex_unlock(&tl_inode->i_mutex);
4517
4518 if (meta_ac)
4519 ocfs2_free_alloc_context(meta_ac);
4520
4521 ocfs2_run_deallocs(osb, &dealloc);
4522
4523 return ret;
4524}
4525
Tao Ma01225592008-08-18 17:38:53 +08004526static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004527 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004528 struct ocfs2_xattr_search *xs)
4529{
Joel Beckerba937122008-10-24 19:13:20 -07004530 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004531 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4532 le16_to_cpu(xh->xh_count) - 1];
4533 int ret = 0;
4534
Joel Beckerba937122008-10-24 19:13:20 -07004535 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004536 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004537 if (ret) {
4538 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004539 return;
Tao Ma01225592008-08-18 17:38:53 +08004540 }
4541
4542 /* Remove the old entry. */
4543 memmove(xs->here, xs->here + 1,
4544 (void *)last - (void *)xs->here);
4545 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4546 le16_add_cpu(&xh->xh_count, -1);
4547
Joel Beckerba937122008-10-24 19:13:20 -07004548 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004549}
4550
4551/*
4552 * Set the xattr name/value in the bucket specified in xs.
4553 *
4554 * As the new value in xi may be stored in the bucket or in an outside cluster,
4555 * we divide the whole process into 3 steps:
4556 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4557 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4558 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4559 * 4. If the clusters for the new outside value can't be allocated, we need
4560 * to free the xattr we allocated in set.
4561 */
4562static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4563 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004564 struct ocfs2_xattr_search *xs,
4565 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004566{
Tao Ma5a095612008-09-19 22:17:41 +08004567 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004568 size_t value_len;
4569 char *val = (char *)xi->value;
4570 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004571 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4572 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004573
4574 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4575 /*
4576 * We need to truncate the xattr storage first.
4577 *
4578 * If both the old and new value are stored to
4579 * outside block, we only need to truncate
4580 * the storage and then set the value outside.
4581 *
4582 * If the new value should be stored within block,
4583 * we should free all the outside block first and
4584 * the modification to the xattr block will be done
4585 * by following steps.
4586 */
4587 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4588 value_len = xi->value_len;
4589 else
4590 value_len = 0;
4591
4592 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004593 value_len,
4594 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004595 if (ret)
4596 goto out;
4597
4598 if (value_len)
4599 goto set_value_outside;
4600 }
4601
4602 value_len = xi->value_len;
4603 /* So we have to handle the inside block change now. */
4604 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4605 /*
4606 * If the new value will be stored outside of block,
4607 * initalize a new empty value root and insert it first.
4608 */
4609 local = 0;
4610 xi->value = &def_xv;
4611 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4612 }
4613
Tao Ma85db90e2008-11-12 08:27:01 +08004614 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4615 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004616 if (ret) {
4617 mlog_errno(ret);
4618 goto out;
4619 }
4620
Tao Ma5a095612008-09-19 22:17:41 +08004621 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4622 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004623
Tao Ma5a095612008-09-19 22:17:41 +08004624 /* allocate the space now for the outside block storage. */
4625 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004626 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08004627 if (ret) {
4628 mlog_errno(ret);
4629
4630 if (xs->not_found) {
4631 /*
4632 * We can't allocate enough clusters for outside
4633 * storage and we have allocated xattr already,
4634 * so need to remove it.
4635 */
Tao Ma85db90e2008-11-12 08:27:01 +08004636 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08004637 }
Tao Ma01225592008-08-18 17:38:53 +08004638 goto out;
4639 }
4640
4641set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08004642 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4643 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004644out:
4645 return ret;
4646}
4647
Tao Ma80bcaf32008-10-27 06:06:24 +08004648/*
4649 * check whether the xattr bucket is filled up with the same hash value.
4650 * If we want to insert the xattr with the same hash, return -ENOSPC.
4651 * If we want to insert a xattr with different hash value, go ahead
4652 * and ocfs2_divide_xattr_bucket will handle this.
4653 */
Tao Ma01225592008-08-18 17:38:53 +08004654static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004655 struct ocfs2_xattr_bucket *bucket,
4656 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004657{
Joel Becker3e632942008-10-24 17:04:49 -07004658 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004659 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4660
4661 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4662 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004663
4664 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4665 xh->xh_entries[0].xe_name_hash) {
4666 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4667 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004668 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004669 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4670 return -ENOSPC;
4671 }
4672
4673 return 0;
4674}
4675
4676static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4677 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004678 struct ocfs2_xattr_search *xs,
4679 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004680{
4681 struct ocfs2_xattr_header *xh;
4682 struct ocfs2_xattr_entry *xe;
4683 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07004684 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08004685 size_t value_size = 0, name_len = strlen(xi->name);
4686 size_t blocksize = inode->i_sb->s_blocksize;
4687 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08004688
4689 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4690
4691try_again:
4692 xh = xs->header;
4693 count = le16_to_cpu(xh->xh_count);
4694 xh_free_start = le16_to_cpu(xh->xh_free_start);
4695 header_size = sizeof(struct ocfs2_xattr_header) +
4696 count * sizeof(struct ocfs2_xattr_entry);
4697 max_free = OCFS2_XATTR_BUCKET_SIZE -
4698 le16_to_cpu(xh->xh_name_value_len) - header_size;
4699
4700 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4701 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07004702 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004703 header_size);
4704
4705 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4706 value_size = OCFS2_XATTR_ROOT_SIZE;
4707 else if (xi->value)
4708 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4709
4710 if (xs->not_found)
4711 need = sizeof(struct ocfs2_xattr_entry) +
4712 OCFS2_XATTR_SIZE(name_len) + value_size;
4713 else {
4714 need = value_size + OCFS2_XATTR_SIZE(name_len);
4715
4716 /*
4717 * We only replace the old value if the new length is smaller
4718 * than the old one. Otherwise we will allocate new space in the
4719 * bucket to store it.
4720 */
4721 xe = xs->here;
4722 if (ocfs2_xattr_is_local(xe))
4723 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4724 else
4725 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4726
4727 if (old >= value_size)
4728 need = 0;
4729 }
4730
4731 free = xh_free_start - header_size;
4732 /*
4733 * We need to make sure the new name/value pair
4734 * can exist in the same block.
4735 */
4736 if (xh_free_start % blocksize < need)
4737 free -= xh_free_start % blocksize;
4738
4739 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4740 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4741 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07004742 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004743 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4744 le16_to_cpu(xh->xh_name_value_len));
4745
Tao Ma976331d2008-11-12 08:26:57 +08004746 if (free < need ||
4747 (xs->not_found &&
4748 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08004749 if (need <= max_free &&
4750 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4751 /*
4752 * We can create the space by defragment. Since only the
4753 * name/value will be moved, the xe shouldn't be changed
4754 * in xs.
4755 */
Tao Ma85db90e2008-11-12 08:27:01 +08004756 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
4757 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004758 if (ret) {
4759 mlog_errno(ret);
4760 goto out;
4761 }
4762
4763 xh_free_start = le16_to_cpu(xh->xh_free_start);
4764 free = xh_free_start - header_size;
4765 if (xh_free_start % blocksize < need)
4766 free -= xh_free_start % blocksize;
4767
4768 if (free >= need)
4769 goto xattr_set;
4770
4771 mlog(0, "Can't get enough space for xattr insert by "
4772 "defragment. Need %u bytes, but we have %d, so "
4773 "allocate new bucket for it.\n", need, free);
4774 }
4775
4776 /*
4777 * We have to add new buckets or clusters and one
4778 * allocation should leave us enough space for insert.
4779 */
4780 BUG_ON(allocation);
4781
4782 /*
4783 * We do not allow for overlapping ranges between buckets. And
4784 * the maximum number of collisions we will allow for then is
4785 * one bucket's worth, so check it here whether we need to
4786 * add a new bucket for the insert.
4787 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004788 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004789 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08004790 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08004791 if (ret) {
4792 mlog_errno(ret);
4793 goto out;
4794 }
4795
4796 ret = ocfs2_add_new_xattr_bucket(inode,
4797 xs->xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08004798 xs->bucket->bu_bhs[0],
4799 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004800 if (ret) {
4801 mlog_errno(ret);
4802 goto out;
4803 }
4804
Joel Beckerba937122008-10-24 19:13:20 -07004805 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004806
4807 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4808 xi->name_index,
4809 xi->name, xs);
4810 if (ret && ret != -ENODATA)
4811 goto out;
4812 xs->not_found = ret;
4813 allocation = 1;
4814 goto try_again;
4815 }
4816
4817xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08004818 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004819out:
4820 mlog_exit(ret);
4821 return ret;
4822}
Tao Maa3944252008-08-18 17:38:54 +08004823
4824static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4825 struct ocfs2_xattr_bucket *bucket,
4826 void *para)
4827{
4828 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07004829 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08004830 u16 i;
4831 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08004832 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4833 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
4834
4835 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08004836
Tao Ma85db90e2008-11-12 08:27:01 +08004837 ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4838 if (IS_ERR(ctxt.handle)) {
4839 ret = PTR_ERR(ctxt.handle);
4840 mlog_errno(ret);
4841 goto out;
4842 }
4843
Tao Maa3944252008-08-18 17:38:54 +08004844 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4845 xe = &xh->xh_entries[i];
4846 if (ocfs2_xattr_is_local(xe))
4847 continue;
4848
4849 ret = ocfs2_xattr_bucket_value_truncate(inode,
Joel Becker4ac60322008-10-18 19:11:42 -07004850 bucket->bu_bhs[0],
Tao Ma78f30c32008-11-12 08:27:00 +08004851 i, 0, &ctxt);
Tao Maa3944252008-08-18 17:38:54 +08004852 if (ret) {
4853 mlog_errno(ret);
4854 break;
4855 }
4856 }
4857
Tao Ma85db90e2008-11-12 08:27:01 +08004858 ret = ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08004859 ocfs2_schedule_truncate_log_flush(osb, 1);
4860 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08004861out:
Tao Maa3944252008-08-18 17:38:54 +08004862 return ret;
4863}
4864
4865static int ocfs2_delete_xattr_index_block(struct inode *inode,
4866 struct buffer_head *xb_bh)
4867{
4868 struct ocfs2_xattr_block *xb =
4869 (struct ocfs2_xattr_block *)xb_bh->b_data;
4870 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4871 int ret = 0;
4872 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4873 u64 p_blkno;
4874
4875 if (le16_to_cpu(el->l_next_free_rec) == 0)
4876 return 0;
4877
4878 while (name_hash > 0) {
4879 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4880 &e_cpos, &num_clusters, el);
4881 if (ret) {
4882 mlog_errno(ret);
4883 goto out;
4884 }
4885
4886 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4887 ocfs2_delete_xattr_in_bucket,
4888 NULL);
4889 if (ret) {
4890 mlog_errno(ret);
4891 goto out;
4892 }
4893
4894 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4895 p_blkno, e_cpos, num_clusters);
4896 if (ret) {
4897 mlog_errno(ret);
4898 break;
4899 }
4900
4901 if (e_cpos == 0)
4902 break;
4903
4904 name_hash = e_cpos - 1;
4905 }
4906
4907out:
4908 return ret;
4909}
Mark Fasheh99219ae2008-10-07 14:52:59 -07004910
4911/*
4912 * 'trusted' attributes support
4913 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07004914static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4915 size_t list_size, const char *name,
4916 size_t name_len)
4917{
Tiger Yangceb1eba2008-10-23 16:34:13 +08004918 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07004919 const size_t total_len = prefix_len + name_len + 1;
4920
4921 if (list && total_len <= list_size) {
4922 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4923 memcpy(list + prefix_len, name, name_len);
4924 list[prefix_len + name_len] = '\0';
4925 }
4926 return total_len;
4927}
4928
4929static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4930 void *buffer, size_t size)
4931{
4932 if (strcmp(name, "") == 0)
4933 return -EINVAL;
4934 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4935 buffer, size);
4936}
4937
4938static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4939 const void *value, size_t size, int flags)
4940{
4941 if (strcmp(name, "") == 0)
4942 return -EINVAL;
4943
4944 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4945 size, flags);
4946}
4947
4948struct xattr_handler ocfs2_xattr_trusted_handler = {
4949 .prefix = XATTR_TRUSTED_PREFIX,
4950 .list = ocfs2_xattr_trusted_list,
4951 .get = ocfs2_xattr_trusted_get,
4952 .set = ocfs2_xattr_trusted_set,
4953};
4954
Mark Fasheh99219ae2008-10-07 14:52:59 -07004955/*
4956 * 'user' attributes support
4957 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07004958static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4959 size_t list_size, const char *name,
4960 size_t name_len)
4961{
Tiger Yangceb1eba2008-10-23 16:34:13 +08004962 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07004963 const size_t total_len = prefix_len + name_len + 1;
4964 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4965
4966 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4967 return 0;
4968
4969 if (list && total_len <= list_size) {
4970 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4971 memcpy(list + prefix_len, name, name_len);
4972 list[prefix_len + name_len] = '\0';
4973 }
4974 return total_len;
4975}
4976
4977static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4978 void *buffer, size_t size)
4979{
4980 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4981
4982 if (strcmp(name, "") == 0)
4983 return -EINVAL;
4984 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4985 return -EOPNOTSUPP;
4986 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4987 buffer, size);
4988}
4989
4990static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4991 const void *value, size_t size, int flags)
4992{
4993 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4994
4995 if (strcmp(name, "") == 0)
4996 return -EINVAL;
4997 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4998 return -EOPNOTSUPP;
4999
5000 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5001 size, flags);
5002}
5003
5004struct xattr_handler ocfs2_xattr_user_handler = {
5005 .prefix = XATTR_USER_PREFIX,
5006 .list = ocfs2_xattr_user_list,
5007 .get = ocfs2_xattr_user_get,
5008 .set = ocfs2_xattr_user_set,
5009};