blob: 5efcf4e85d7cf68870d14c8f3bfed11efca4b952 [file] [log] [blame]
Tao Maf56654c2008-08-18 17:38:48 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
Tiger Yangc3cb6822008-10-23 16:33:03 +08006 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
Tao Maf56654c2008-08-18 17:38:48 +08007 *
Tiger Yangcf1d6c72008-08-18 17:11:00 +08008 * CREDITS:
Tiger Yangc3cb6822008-10-23 16:33:03 +08009 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080011 *
Tao Maf56654c2008-08-18 17:38:48 +080012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
Tiger Yangc3cb6822008-10-23 16:33:03 +080014 * License version 2 as published by the Free Software Foundation.
Tao Maf56654c2008-08-18 17:38:48 +080015 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
Tao Maf56654c2008-08-18 17:38:48 +080020 */
21
Tiger Yangcf1d6c72008-08-18 17:11:00 +080022#include <linux/capability.h>
23#include <linux/fs.h>
24#include <linux/types.h>
25#include <linux/slab.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h>
28#include <linux/uio.h>
29#include <linux/sched.h>
30#include <linux/splice.h>
31#include <linux/mount.h>
32#include <linux/writeback.h>
33#include <linux/falloc.h>
Tao Ma01225592008-08-18 17:38:53 +080034#include <linux/sort.h>
Mark Fasheh99219ae2008-10-07 14:52:59 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
Tiger Yang923f7f32008-11-14 11:16:27 +080038#include <linux/security.h>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080039
Tao Maf56654c2008-08-18 17:38:48 +080040#define MLOG_MASK_PREFIX ML_XATTR
41#include <cluster/masklog.h>
42
43#include "ocfs2.h"
44#include "alloc.h"
45#include "dlmglue.h"
46#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080047#include "symlink.h"
48#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080049#include "inode.h"
50#include "journal.h"
51#include "ocfs2_fs.h"
52#include "suballoc.h"
53#include "uptodate.h"
54#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080055#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080056#include "xattr.h"
57
58
59struct ocfs2_xattr_def_value_root {
60 struct ocfs2_xattr_value_root xv;
61 struct ocfs2_extent_rec er;
62};
63
Tao Ma0c044f02008-08-18 17:38:50 +080064struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070065 /* The inode these xattrs are associated with */
66 struct inode *bu_inode;
67
68 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070069 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070070
71 /* How many blocks make up one bucket for this filesystem */
72 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080073};
74
Tao Ma78f30c32008-11-12 08:27:00 +080075struct ocfs2_xattr_set_ctxt {
Tao Ma85db90e2008-11-12 08:27:01 +080076 handle_t *handle;
Tao Ma78f30c32008-11-12 08:27:00 +080077 struct ocfs2_alloc_context *meta_ac;
78 struct ocfs2_alloc_context *data_ac;
79 struct ocfs2_cached_dealloc_ctxt dealloc;
80};
81
Tiger Yangcf1d6c72008-08-18 17:11:00 +080082#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
83#define OCFS2_XATTR_INLINE_SIZE 80
Tiger Yang534eadd2008-11-14 11:16:41 +080084#define OCFS2_XATTR_FREE_IN_IBODY (OCFS2_MIN_XATTR_INLINE_SIZE \
85 - sizeof(struct ocfs2_xattr_header) \
86 - sizeof(__u32))
Tiger Yang89c38bd2008-11-14 11:17:41 +080087#define OCFS2_XATTR_FREE_IN_BLOCK(ptr) ((ptr)->i_sb->s_blocksize \
88 - sizeof(struct ocfs2_xattr_block) \
89 - sizeof(struct ocfs2_xattr_header) \
90 - sizeof(__u32))
Tiger Yangcf1d6c72008-08-18 17:11:00 +080091
92static struct ocfs2_xattr_def_value_root def_xv = {
93 .xv.xr_list.l_count = cpu_to_le16(1),
94};
95
96struct xattr_handler *ocfs2_xattr_handlers[] = {
97 &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +080098#ifdef CONFIG_OCFS2_FS_POSIX_ACL
99 &ocfs2_xattr_acl_access_handler,
100 &ocfs2_xattr_acl_default_handler,
101#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800102 &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800103 &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800104 NULL
105};
106
Tiger Yangc988fd02008-10-23 16:34:44 +0800107static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800108 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
Tiger Yang929fb012008-11-14 11:17:04 +0800109#ifdef CONFIG_OCFS2_FS_POSIX_ACL
110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
111 = &ocfs2_xattr_acl_access_handler,
112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
113 = &ocfs2_xattr_acl_default_handler,
114#endif
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800115 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
Tiger Yang923f7f32008-11-14 11:16:27 +0800116 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800117};
118
119struct ocfs2_xattr_info {
120 int name_index;
121 const char *name;
122 const void *value;
123 size_t value_len;
124};
125
126struct ocfs2_xattr_search {
127 struct buffer_head *inode_bh;
128 /*
129 * xattr_bh point to the block buffer head which has extended attribute
130 * when extended attribute in inode, xattr_bh is equal to inode_bh.
131 */
132 struct buffer_head *xattr_bh;
133 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700134 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800135 void *base;
136 void *end;
137 struct ocfs2_xattr_entry *here;
138 int not_found;
139};
140
Tao Ma589dc262008-08-18 17:38:51 +0800141static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
142 struct ocfs2_xattr_header *xh,
143 int index,
144 int *block_off,
145 int *new_offset);
146
Joel Becker54f443f2008-10-20 18:43:07 -0700147static int ocfs2_xattr_block_find(struct inode *inode,
148 int name_index,
149 const char *name,
150 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800151static int ocfs2_xattr_index_block_find(struct inode *inode,
152 struct buffer_head *root_bh,
153 int name_index,
154 const char *name,
155 struct ocfs2_xattr_search *xs);
156
Tao Ma0c044f02008-08-18 17:38:50 +0800157static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
158 struct ocfs2_xattr_tree_root *xt,
159 char *buffer,
160 size_t buffer_size);
161
Tao Ma01225592008-08-18 17:38:53 +0800162static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +0800163 struct ocfs2_xattr_search *xs,
164 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800165
166static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
167 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +0800168 struct ocfs2_xattr_search *xs,
169 struct ocfs2_xattr_set_ctxt *ctxt);
Tao Ma01225592008-08-18 17:38:53 +0800170
Tao Maa3944252008-08-18 17:38:54 +0800171static int ocfs2_delete_xattr_index_block(struct inode *inode,
172 struct buffer_head *xb_bh);
173
Tiger Yang0030e002008-10-23 16:33:33 +0800174static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
175{
176 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
177}
178
179static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
180{
181 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
182}
183
184static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
185{
186 u16 len = sb->s_blocksize -
187 offsetof(struct ocfs2_xattr_header, xh_entries);
188
189 return len / sizeof(struct ocfs2_xattr_entry);
190}
191
Joel Becker9c7759a2008-10-24 16:21:03 -0700192#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700193#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700194#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700195
Joel Beckerba937122008-10-24 19:13:20 -0700196static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700197{
Joel Beckerba937122008-10-24 19:13:20 -0700198 struct ocfs2_xattr_bucket *bucket;
199 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700200
Joel Beckerba937122008-10-24 19:13:20 -0700201 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
202
203 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
204 if (bucket) {
205 bucket->bu_inode = inode;
206 bucket->bu_blocks = blks;
207 }
208
209 return bucket;
210}
211
212static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
213{
214 int i;
215
216 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700217 brelse(bucket->bu_bhs[i]);
218 bucket->bu_bhs[i] = NULL;
219 }
220}
221
Joel Beckerba937122008-10-24 19:13:20 -0700222static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
223{
224 if (bucket) {
225 ocfs2_xattr_bucket_relse(bucket);
226 bucket->bu_inode = NULL;
227 kfree(bucket);
228 }
229}
230
Joel Becker784b8162008-10-24 17:33:40 -0700231/*
232 * A bucket that has never been written to disk doesn't need to be
233 * read. We just need the buffer_heads. Don't call this for
234 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
235 * them fully.
236 */
Joel Beckerba937122008-10-24 19:13:20 -0700237static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700238 u64 xb_blkno)
239{
240 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700241
Joel Beckerba937122008-10-24 19:13:20 -0700242 for (i = 0; i < bucket->bu_blocks; i++) {
243 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
244 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700245 if (!bucket->bu_bhs[i]) {
246 rc = -EIO;
247 mlog_errno(rc);
248 break;
249 }
250
Tao Ma757055a2008-11-06 08:10:48 +0800251 if (!ocfs2_buffer_uptodate(bucket->bu_inode,
252 bucket->bu_bhs[i]))
253 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
254 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700255 }
256
257 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700258 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700259 return rc;
260}
261
262/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700263static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700264 u64 xb_blkno)
265{
Joel Beckerba937122008-10-24 19:13:20 -0700266 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700267
Joel Beckerba937122008-10-24 19:13:20 -0700268 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
Joel Becker970e4932008-11-13 14:49:19 -0800269 bucket->bu_blocks, bucket->bu_bhs, 0,
270 NULL);
Joel Becker784b8162008-10-24 17:33:40 -0700271 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700272 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700273 return rc;
274}
275
Joel Becker1224be02008-10-24 18:47:33 -0700276static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700277 struct ocfs2_xattr_bucket *bucket,
278 int type)
279{
280 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700281
Joel Beckerba937122008-10-24 19:13:20 -0700282 for (i = 0; i < bucket->bu_blocks; i++) {
283 rc = ocfs2_journal_access(handle, bucket->bu_inode,
Joel Becker1224be02008-10-24 18:47:33 -0700284 bucket->bu_bhs[i], type);
285 if (rc) {
286 mlog_errno(rc);
287 break;
288 }
289 }
290
291 return rc;
292}
293
294static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700295 struct ocfs2_xattr_bucket *bucket)
296{
Joel Beckerba937122008-10-24 19:13:20 -0700297 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700298
Joel Beckerba937122008-10-24 19:13:20 -0700299 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700300 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
301}
302
Joel Beckerba937122008-10-24 19:13:20 -0700303static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700304 struct ocfs2_xattr_bucket *src)
305{
306 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700307 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700308
Joel Beckerba937122008-10-24 19:13:20 -0700309 BUG_ON(dest->bu_blocks != src->bu_blocks);
310 BUG_ON(dest->bu_inode != src->bu_inode);
311
312 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700313 memcpy(bucket_block(dest, i), bucket_block(src, i),
314 blocksize);
315 }
316}
Joel Becker1224be02008-10-24 18:47:33 -0700317
Joel Becker4ae1d692008-11-13 14:49:18 -0800318static int ocfs2_validate_xattr_block(struct super_block *sb,
319 struct buffer_head *bh)
320{
321 struct ocfs2_xattr_block *xb =
322 (struct ocfs2_xattr_block *)bh->b_data;
323
324 mlog(0, "Validating xattr block %llu\n",
325 (unsigned long long)bh->b_blocknr);
326
327 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
328 ocfs2_error(sb,
329 "Extended attribute block #%llu has bad "
330 "signature %.*s",
331 (unsigned long long)bh->b_blocknr, 7,
332 xb->xb_signature);
333 return -EINVAL;
334 }
335
336 if (le64_to_cpu(xb->xb_blkno) != bh->b_blocknr) {
337 ocfs2_error(sb,
338 "Extended attribute block #%llu has an "
339 "invalid xb_blkno of %llu",
340 (unsigned long long)bh->b_blocknr,
341 (unsigned long long)le64_to_cpu(xb->xb_blkno));
342 return -EINVAL;
343 }
344
345 if (le32_to_cpu(xb->xb_fs_generation) != OCFS2_SB(sb)->fs_generation) {
346 ocfs2_error(sb,
347 "Extended attribute block #%llu has an invalid "
348 "xb_fs_generation of #%u",
349 (unsigned long long)bh->b_blocknr,
350 le32_to_cpu(xb->xb_fs_generation));
351 return -EINVAL;
352 }
353
354 return 0;
355}
356
357static int ocfs2_read_xattr_block(struct inode *inode, u64 xb_blkno,
358 struct buffer_head **bh)
359{
360 int rc;
361 struct buffer_head *tmp = *bh;
362
Joel Becker970e4932008-11-13 14:49:19 -0800363 rc = ocfs2_read_block(inode, xb_blkno, &tmp,
364 ocfs2_validate_xattr_block);
Joel Becker4ae1d692008-11-13 14:49:18 -0800365
366 /* If ocfs2_read_block() got us a new bh, pass it up. */
367 if (!rc && !*bh)
368 *bh = tmp;
369
370 return rc;
371}
372
Tao Ma936b8832008-10-09 23:06:14 +0800373static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800374{
375 struct xattr_handler *handler = NULL;
376
377 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
378 handler = ocfs2_xattr_handler_map[name_index];
379
Tao Ma936b8832008-10-09 23:06:14 +0800380 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800381}
382
Mark Fasheh40daa162008-10-07 14:31:42 -0700383static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800384 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700385 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800386{
387 /* Get hash value of uuid from super block */
388 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
389 int i;
390
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800391 /* hash extended attribute name */
392 for (i = 0; i < name_len; i++) {
393 hash = (hash << OCFS2_HASH_SHIFT) ^
394 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
395 *name++;
396 }
397
398 return hash;
399}
400
401/*
402 * ocfs2_xattr_hash_entry()
403 *
404 * Compute the hash of an extended attribute.
405 */
406static void ocfs2_xattr_hash_entry(struct inode *inode,
407 struct ocfs2_xattr_header *header,
408 struct ocfs2_xattr_entry *entry)
409{
410 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800411 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800412
Tao Ma2057e5c2008-10-09 23:06:13 +0800413 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800414 entry->xe_name_hash = cpu_to_le32(hash);
415
416 return;
417}
Tao Maf56654c2008-08-18 17:38:48 +0800418
Tiger Yang534eadd2008-11-14 11:16:41 +0800419static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
420{
421 int size = 0;
422
423 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
424 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
425 else
426 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
427 size += sizeof(struct ocfs2_xattr_entry);
428
429 return size;
430}
431
432int ocfs2_calc_security_init(struct inode *dir,
433 struct ocfs2_security_xattr_info *si,
434 int *want_clusters,
435 int *xattr_credits,
436 struct ocfs2_alloc_context **xattr_ac)
437{
438 int ret = 0;
439 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
440 int s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
441 si->value_len);
442
443 /*
444 * The max space of security xattr taken inline is
445 * 256(name) + 80(value) + 16(entry) = 352 bytes,
446 * So reserve one metadata block for it is ok.
447 */
448 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
449 s_size > OCFS2_XATTR_FREE_IN_IBODY) {
450 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
451 if (ret) {
452 mlog_errno(ret);
453 return ret;
454 }
455 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
456 }
457
458 /* reserve clusters for xattr value which will be set in B tree*/
459 if (si->value_len > OCFS2_XATTR_INLINE_SIZE)
460 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
461 si->value_len);
462 return ret;
463}
464
Tiger Yang89c38bd2008-11-14 11:17:41 +0800465int ocfs2_calc_xattr_init(struct inode *dir,
466 struct buffer_head *dir_bh,
467 int mode,
468 struct ocfs2_security_xattr_info *si,
469 int *want_clusters,
470 int *xattr_credits,
471 struct ocfs2_alloc_context **xattr_ac)
472{
473 int ret = 0;
474 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
475 int s_size = 0;
476 int a_size = 0;
477 int acl_len = 0;
478
479 if (si->enable)
480 s_size = ocfs2_xattr_entry_real_size(strlen(si->name),
481 si->value_len);
482
483 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
484 acl_len = ocfs2_xattr_get_nolock(dir, dir_bh,
485 OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT,
486 "", NULL, 0);
487 if (acl_len > 0) {
488 a_size = ocfs2_xattr_entry_real_size(0, acl_len);
489 if (S_ISDIR(mode))
490 a_size <<= 1;
491 } else if (acl_len != 0 && acl_len != -ENODATA) {
492 mlog_errno(ret);
493 return ret;
494 }
495 }
496
497 if (!(s_size + a_size))
498 return ret;
499
500 /*
501 * The max space of security xattr taken inline is
502 * 256(name) + 80(value) + 16(entry) = 352 bytes,
503 * The max space of acl xattr taken inline is
504 * 80(value) + 16(entry) * 2(if directory) = 192 bytes,
505 * when blocksize = 512, may reserve one more cluser for
506 * xattr bucket, otherwise reserve one metadata block
507 * for them is ok.
508 */
509 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE ||
510 (s_size + a_size) > OCFS2_XATTR_FREE_IN_IBODY) {
511 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, xattr_ac);
512 if (ret) {
513 mlog_errno(ret);
514 return ret;
515 }
516 *xattr_credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
517 }
518
519 if (dir->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE &&
520 (s_size + a_size) > OCFS2_XATTR_FREE_IN_BLOCK(dir)) {
521 *want_clusters += 1;
522 *xattr_credits += ocfs2_blocks_per_xattr_bucket(dir->i_sb);
523 }
524
525 /* reserve clusters for xattr value which will be set in B tree*/
526 if (si->enable && si->value_len > OCFS2_XATTR_INLINE_SIZE)
527 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
528 si->value_len);
529 if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL &&
530 acl_len > OCFS2_XATTR_INLINE_SIZE) {
531 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb, acl_len);
532 if (S_ISDIR(mode))
533 *want_clusters += ocfs2_clusters_for_bytes(dir->i_sb,
534 acl_len);
535 }
536
537 return ret;
538}
539
Tao Maf56654c2008-08-18 17:38:48 +0800540static int ocfs2_xattr_extend_allocation(struct inode *inode,
541 u32 clusters_to_add,
542 struct buffer_head *xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800543 struct ocfs2_xattr_value_root *xv,
544 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800545{
546 int status = 0;
Tao Ma85db90e2008-11-12 08:27:01 +0800547 handle_t *handle = ctxt->handle;
Tao Maf56654c2008-08-18 17:38:48 +0800548 enum ocfs2_alloc_restarted why;
549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Maf56654c2008-08-18 17:38:48 +0800550 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700551 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800552
553 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
554
Joel Becker8d6220d2008-08-22 12:46:09 -0700555 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700556
Tao Maf56654c2008-08-18 17:38:48 +0800557 status = ocfs2_journal_access(handle, inode, xattr_bh,
558 OCFS2_JOURNAL_ACCESS_WRITE);
559 if (status < 0) {
560 mlog_errno(status);
561 goto leave;
562 }
563
564 prev_clusters = le32_to_cpu(xv->xr_clusters);
565 status = ocfs2_add_clusters_in_btree(osb,
566 inode,
567 &logical_start,
568 clusters_to_add,
569 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700570 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800571 handle,
Tao Ma78f30c32008-11-12 08:27:00 +0800572 ctxt->data_ac,
573 ctxt->meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700574 &why);
Tao Ma85db90e2008-11-12 08:27:01 +0800575 if (status < 0) {
576 mlog_errno(status);
Tao Maf56654c2008-08-18 17:38:48 +0800577 goto leave;
578 }
579
580 status = ocfs2_journal_dirty(handle, xattr_bh);
581 if (status < 0) {
582 mlog_errno(status);
583 goto leave;
584 }
585
586 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
587
Tao Ma85db90e2008-11-12 08:27:01 +0800588 /*
589 * We should have already allocated enough space before the transaction,
590 * so no need to restart.
591 */
592 BUG_ON(why != RESTART_NONE || clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800593
594leave:
Tao Maf56654c2008-08-18 17:38:48 +0800595
596 return status;
597}
598
599static int __ocfs2_remove_xattr_range(struct inode *inode,
600 struct buffer_head *root_bh,
601 struct ocfs2_xattr_value_root *xv,
602 u32 cpos, u32 phys_cpos, u32 len,
Tao Ma78f30c32008-11-12 08:27:00 +0800603 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800604{
605 int ret;
606 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
Tao Ma85db90e2008-11-12 08:27:01 +0800607 handle_t *handle = ctxt->handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700608 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800609
Joel Becker8d6220d2008-08-22 12:46:09 -0700610 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700611
Tao Maf56654c2008-08-18 17:38:48 +0800612 ret = ocfs2_journal_access(handle, inode, root_bh,
613 OCFS2_JOURNAL_ACCESS_WRITE);
614 if (ret) {
615 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800616 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800617 }
618
Tao Ma78f30c32008-11-12 08:27:00 +0800619 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, ctxt->meta_ac,
620 &ctxt->dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800621 if (ret) {
622 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800623 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800624 }
625
626 le32_add_cpu(&xv->xr_clusters, -len);
627
628 ret = ocfs2_journal_dirty(handle, root_bh);
629 if (ret) {
630 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +0800631 goto out;
Tao Maf56654c2008-08-18 17:38:48 +0800632 }
633
Tao Ma78f30c32008-11-12 08:27:00 +0800634 ret = ocfs2_cache_cluster_dealloc(&ctxt->dealloc, phys_blkno, len);
Tao Maf56654c2008-08-18 17:38:48 +0800635 if (ret)
636 mlog_errno(ret);
637
Tao Maf56654c2008-08-18 17:38:48 +0800638out:
Tao Maf56654c2008-08-18 17:38:48 +0800639 return ret;
640}
641
642static int ocfs2_xattr_shrink_size(struct inode *inode,
643 u32 old_clusters,
644 u32 new_clusters,
645 struct buffer_head *root_bh,
Tao Ma78f30c32008-11-12 08:27:00 +0800646 struct ocfs2_xattr_value_root *xv,
647 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800648{
649 int ret = 0;
650 u32 trunc_len, cpos, phys_cpos, alloc_size;
651 u64 block;
Tao Maf56654c2008-08-18 17:38:48 +0800652
653 if (old_clusters <= new_clusters)
654 return 0;
655
656 cpos = new_clusters;
657 trunc_len = old_clusters - new_clusters;
658 while (trunc_len) {
659 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
660 &alloc_size, &xv->xr_list);
661 if (ret) {
662 mlog_errno(ret);
663 goto out;
664 }
665
666 if (alloc_size > trunc_len)
667 alloc_size = trunc_len;
668
669 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
670 phys_cpos, alloc_size,
Tao Ma78f30c32008-11-12 08:27:00 +0800671 ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800672 if (ret) {
673 mlog_errno(ret);
674 goto out;
675 }
676
677 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
678 ocfs2_remove_xattr_clusters_from_cache(inode, block,
679 alloc_size);
680 cpos += alloc_size;
681 trunc_len -= alloc_size;
682 }
683
684out:
Tao Maf56654c2008-08-18 17:38:48 +0800685 return ret;
686}
687
688static int ocfs2_xattr_value_truncate(struct inode *inode,
689 struct buffer_head *root_bh,
690 struct ocfs2_xattr_value_root *xv,
Tao Ma78f30c32008-11-12 08:27:00 +0800691 int len,
692 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Maf56654c2008-08-18 17:38:48 +0800693{
694 int ret;
695 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
696 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
697
698 if (new_clusters == old_clusters)
699 return 0;
700
701 if (new_clusters > old_clusters)
702 ret = ocfs2_xattr_extend_allocation(inode,
703 new_clusters - old_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800704 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800705 else
706 ret = ocfs2_xattr_shrink_size(inode,
707 old_clusters, new_clusters,
Tao Ma78f30c32008-11-12 08:27:00 +0800708 root_bh, xv, ctxt);
Tao Maf56654c2008-08-18 17:38:48 +0800709
710 return ret;
711}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800712
Tao Ma936b8832008-10-09 23:06:14 +0800713static int ocfs2_xattr_list_entry(char *buffer, size_t size,
714 size_t *result, const char *prefix,
715 const char *name, int name_len)
716{
717 char *p = buffer + *result;
718 int prefix_len = strlen(prefix);
719 int total_len = prefix_len + name_len + 1;
720
721 *result += total_len;
722
723 /* we are just looking for how big our buffer needs to be */
724 if (!size)
725 return 0;
726
727 if (*result > size)
728 return -ERANGE;
729
730 memcpy(p, prefix, prefix_len);
731 memcpy(p + prefix_len, name, name_len);
732 p[prefix_len + name_len] = '\0';
733
734 return 0;
735}
736
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800737static int ocfs2_xattr_list_entries(struct inode *inode,
738 struct ocfs2_xattr_header *header,
739 char *buffer, size_t buffer_size)
740{
Tao Ma936b8832008-10-09 23:06:14 +0800741 size_t result = 0;
742 int i, type, ret;
743 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800744
745 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
746 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800747 type = ocfs2_xattr_get_type(entry);
748 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800749
Tao Ma936b8832008-10-09 23:06:14 +0800750 if (prefix) {
751 name = (const char *)header +
752 le16_to_cpu(entry->xe_name_offset);
753
754 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
755 &result, prefix, name,
756 entry->xe_name_len);
757 if (ret)
758 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800759 }
760 }
761
Tao Ma936b8832008-10-09 23:06:14 +0800762 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800763}
764
765static int ocfs2_xattr_ibody_list(struct inode *inode,
766 struct ocfs2_dinode *di,
767 char *buffer,
768 size_t buffer_size)
769{
770 struct ocfs2_xattr_header *header = NULL;
771 struct ocfs2_inode_info *oi = OCFS2_I(inode);
772 int ret = 0;
773
774 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
775 return ret;
776
777 header = (struct ocfs2_xattr_header *)
778 ((void *)di + inode->i_sb->s_blocksize -
779 le16_to_cpu(di->i_xattr_inline_size));
780
781 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
782
783 return ret;
784}
785
786static int ocfs2_xattr_block_list(struct inode *inode,
787 struct ocfs2_dinode *di,
788 char *buffer,
789 size_t buffer_size)
790{
791 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800792 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800793 int ret = 0;
794
795 if (!di->i_xattr_loc)
796 return ret;
797
Joel Becker4ae1d692008-11-13 14:49:18 -0800798 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
799 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800800 if (ret < 0) {
801 mlog_errno(ret);
802 return ret;
803 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800804
Tao Ma0c044f02008-08-18 17:38:50 +0800805 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma0c044f02008-08-18 17:38:50 +0800806 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
807 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
808 ret = ocfs2_xattr_list_entries(inode, header,
809 buffer, buffer_size);
810 } else {
811 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
812 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
813 buffer, buffer_size);
814 }
Joel Becker4ae1d692008-11-13 14:49:18 -0800815
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800816 brelse(blk_bh);
817
818 return ret;
819}
820
821ssize_t ocfs2_listxattr(struct dentry *dentry,
822 char *buffer,
823 size_t size)
824{
825 int ret = 0, i_ret = 0, b_ret = 0;
826 struct buffer_head *di_bh = NULL;
827 struct ocfs2_dinode *di = NULL;
828 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
829
Tiger Yang8154da32008-08-18 17:11:46 +0800830 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
831 return -EOPNOTSUPP;
832
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800833 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
834 return ret;
835
836 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
837 if (ret < 0) {
838 mlog_errno(ret);
839 return ret;
840 }
841
842 di = (struct ocfs2_dinode *)di_bh->b_data;
843
844 down_read(&oi->ip_xattr_sem);
845 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
846 if (i_ret < 0)
847 b_ret = 0;
848 else {
849 if (buffer) {
850 buffer += i_ret;
851 size -= i_ret;
852 }
853 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
854 buffer, size);
855 if (b_ret < 0)
856 i_ret = 0;
857 }
858 up_read(&oi->ip_xattr_sem);
859 ocfs2_inode_unlock(dentry->d_inode, 0);
860
861 brelse(di_bh);
862
863 return i_ret + b_ret;
864}
865
866static int ocfs2_xattr_find_entry(int name_index,
867 const char *name,
868 struct ocfs2_xattr_search *xs)
869{
870 struct ocfs2_xattr_entry *entry;
871 size_t name_len;
872 int i, cmp = 1;
873
874 if (name == NULL)
875 return -EINVAL;
876
877 name_len = strlen(name);
878 entry = xs->here;
879 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
880 cmp = name_index - ocfs2_xattr_get_type(entry);
881 if (!cmp)
882 cmp = name_len - entry->xe_name_len;
883 if (!cmp)
884 cmp = memcmp(name, (xs->base +
885 le16_to_cpu(entry->xe_name_offset)),
886 name_len);
887 if (cmp == 0)
888 break;
889 entry += 1;
890 }
891 xs->here = entry;
892
893 return cmp ? -ENODATA : 0;
894}
895
896static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800897 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800898 void *buffer,
899 size_t len)
900{
901 u32 cpos, p_cluster, num_clusters, bpc, clusters;
902 u64 blkno;
903 int i, ret = 0;
904 size_t cplen, blocksize;
905 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800906 struct ocfs2_extent_list *el;
907
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800908 el = &xv->xr_list;
909 clusters = le32_to_cpu(xv->xr_clusters);
910 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
911 blocksize = inode->i_sb->s_blocksize;
912
913 cpos = 0;
914 while (cpos < clusters) {
915 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
916 &num_clusters, el);
917 if (ret) {
918 mlog_errno(ret);
919 goto out;
920 }
921
922 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
923 /* Copy ocfs2_xattr_value */
924 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -0800925 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800926 if (ret) {
927 mlog_errno(ret);
928 goto out;
929 }
930
931 cplen = len >= blocksize ? blocksize : len;
932 memcpy(buffer, bh->b_data, cplen);
933 len -= cplen;
934 buffer += cplen;
935
936 brelse(bh);
937 bh = NULL;
938 if (len == 0)
939 break;
940 }
941 cpos += num_clusters;
942 }
943out:
944 return ret;
945}
946
947static int ocfs2_xattr_ibody_get(struct inode *inode,
948 int name_index,
949 const char *name,
950 void *buffer,
951 size_t buffer_size,
952 struct ocfs2_xattr_search *xs)
953{
954 struct ocfs2_inode_info *oi = OCFS2_I(inode);
955 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800956 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800957 size_t size;
958 int ret = 0;
959
960 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
961 return -ENODATA;
962
963 xs->end = (void *)di + inode->i_sb->s_blocksize;
964 xs->header = (struct ocfs2_xattr_header *)
965 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
966 xs->base = (void *)xs->header;
967 xs->here = xs->header->xh_entries;
968
969 ret = ocfs2_xattr_find_entry(name_index, name, xs);
970 if (ret)
971 return ret;
972 size = le64_to_cpu(xs->here->xe_value_size);
973 if (buffer) {
974 if (size > buffer_size)
975 return -ERANGE;
976 if (ocfs2_xattr_is_local(xs->here)) {
977 memcpy(buffer, (void *)xs->base +
978 le16_to_cpu(xs->here->xe_name_offset) +
979 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
980 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800981 xv = (struct ocfs2_xattr_value_root *)
982 (xs->base + le16_to_cpu(
983 xs->here->xe_name_offset) +
984 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
985 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800986 buffer, size);
987 if (ret < 0) {
988 mlog_errno(ret);
989 return ret;
990 }
991 }
992 }
993
994 return size;
995}
996
997static int ocfs2_xattr_block_get(struct inode *inode,
998 int name_index,
999 const char *name,
1000 void *buffer,
1001 size_t buffer_size,
1002 struct ocfs2_xattr_search *xs)
1003{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001004 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +08001005 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001006 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +08001007 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001008
Joel Beckerba937122008-10-24 19:13:20 -07001009 xs->bucket = ocfs2_xattr_bucket_new(inode);
1010 if (!xs->bucket) {
1011 ret = -ENOMEM;
1012 mlog_errno(ret);
1013 goto cleanup;
1014 }
Tao Ma589dc262008-08-18 17:38:51 +08001015
Joel Becker54f443f2008-10-20 18:43:07 -07001016 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
1017 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001018 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001019 goto cleanup;
1020 }
1021
Tiger Yang6c1e1832008-11-02 19:04:21 +08001022 if (xs->not_found) {
1023 ret = -ENODATA;
1024 goto cleanup;
1025 }
1026
Joel Becker54f443f2008-10-20 18:43:07 -07001027 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001028 size = le64_to_cpu(xs->here->xe_value_size);
1029 if (buffer) {
1030 ret = -ERANGE;
1031 if (size > buffer_size)
1032 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +08001033
1034 name_offset = le16_to_cpu(xs->here->xe_name_offset);
1035 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
1036 i = xs->here - xs->header->xh_entries;
1037
1038 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
1039 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -07001040 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +08001041 i,
1042 &block_off,
1043 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -07001044 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +08001045 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001046 if (ocfs2_xattr_is_local(xs->here)) {
1047 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +08001048 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001049 } else {
Tao Ma589dc262008-08-18 17:38:51 +08001050 xv = (struct ocfs2_xattr_value_root *)
1051 (xs->base + name_offset + name_len);
1052 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001053 buffer, size);
1054 if (ret < 0) {
1055 mlog_errno(ret);
1056 goto cleanup;
1057 }
1058 }
1059 }
1060 ret = size;
1061cleanup:
Joel Beckerba937122008-10-24 19:13:20 -07001062 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001063
Joel Becker54f443f2008-10-20 18:43:07 -07001064 brelse(xs->xattr_bh);
1065 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001066 return ret;
1067}
1068
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001069int ocfs2_xattr_get_nolock(struct inode *inode,
1070 struct buffer_head *di_bh,
Tiger Yang0030e002008-10-23 16:33:33 +08001071 int name_index,
1072 const char *name,
1073 void *buffer,
1074 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001075{
1076 int ret;
1077 struct ocfs2_dinode *di = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001078 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1079 struct ocfs2_xattr_search xis = {
1080 .not_found = -ENODATA,
1081 };
1082 struct ocfs2_xattr_search xbs = {
1083 .not_found = -ENODATA,
1084 };
1085
Tiger Yang8154da32008-08-18 17:11:46 +08001086 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1087 return -EOPNOTSUPP;
1088
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001089 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1090 ret = -ENODATA;
1091
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001092 xis.inode_bh = xbs.inode_bh = di_bh;
1093 di = (struct ocfs2_dinode *)di_bh->b_data;
1094
1095 down_read(&oi->ip_xattr_sem);
1096 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
1097 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001098 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001099 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1100 buffer_size, &xbs);
1101 up_read(&oi->ip_xattr_sem);
Tiger Yang4e3e9d02008-11-14 11:16:53 +08001102
1103 return ret;
1104}
1105
1106/* ocfs2_xattr_get()
1107 *
1108 * Copy an extended attribute into the buffer provided.
1109 * Buffer is NULL to compute the size of buffer required.
1110 */
1111static int ocfs2_xattr_get(struct inode *inode,
1112 int name_index,
1113 const char *name,
1114 void *buffer,
1115 size_t buffer_size)
1116{
1117 int ret;
1118 struct buffer_head *di_bh = NULL;
1119
1120 ret = ocfs2_inode_lock(inode, &di_bh, 0);
1121 if (ret < 0) {
1122 mlog_errno(ret);
1123 return ret;
1124 }
1125 ret = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
1126 name, buffer, buffer_size);
1127
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001128 ocfs2_inode_unlock(inode, 0);
1129
1130 brelse(di_bh);
1131
1132 return ret;
1133}
1134
1135static int __ocfs2_xattr_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001136 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001137 struct ocfs2_xattr_value_root *xv,
1138 const void *value,
1139 int value_len)
1140{
1141 int ret = 0, i, cp_len, credits;
1142 u16 blocksize = inode->i_sb->s_blocksize;
1143 u32 p_cluster, num_clusters;
1144 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1145 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1146 u64 blkno;
1147 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001148
1149 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1150
Tao Ma85db90e2008-11-12 08:27:01 +08001151 /*
1152 * In __ocfs2_xattr_set_value_outside has already been dirtied,
1153 * so we don't need to worry about whether ocfs2_extend_trans
1154 * will create a new transactio for us or not.
1155 */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001156 credits = clusters * bpc;
Tao Ma85db90e2008-11-12 08:27:01 +08001157 ret = ocfs2_extend_trans(handle, credits);
1158 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001159 mlog_errno(ret);
1160 goto out;
1161 }
1162
1163 while (cpos < clusters) {
1164 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1165 &num_clusters, &xv->xr_list);
1166 if (ret) {
1167 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001168 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001169 }
1170
1171 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1172
1173 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker970e4932008-11-13 14:49:19 -08001174 ret = ocfs2_read_block(inode, blkno, &bh, NULL);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001175 if (ret) {
1176 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001177 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001178 }
1179
1180 ret = ocfs2_journal_access(handle,
1181 inode,
1182 bh,
1183 OCFS2_JOURNAL_ACCESS_WRITE);
1184 if (ret < 0) {
1185 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001186 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001187 }
1188
1189 cp_len = value_len > blocksize ? blocksize : value_len;
1190 memcpy(bh->b_data, value, cp_len);
1191 value_len -= cp_len;
1192 value += cp_len;
1193 if (cp_len < blocksize)
1194 memset(bh->b_data + cp_len, 0,
1195 blocksize - cp_len);
1196
1197 ret = ocfs2_journal_dirty(handle, bh);
1198 if (ret < 0) {
1199 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001200 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001201 }
1202 brelse(bh);
1203 bh = NULL;
1204
1205 /*
1206 * XXX: do we need to empty all the following
1207 * blocks in this cluster?
1208 */
1209 if (!value_len)
1210 break;
1211 }
1212 cpos += num_clusters;
1213 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001214out:
1215 brelse(bh);
1216
1217 return ret;
1218}
1219
1220static int ocfs2_xattr_cleanup(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001221 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001222 struct ocfs2_xattr_info *xi,
1223 struct ocfs2_xattr_search *xs,
1224 size_t offs)
1225{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001226 int ret = 0;
1227 size_t name_len = strlen(xi->name);
1228 void *val = xs->base + offs;
1229 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1230
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001231 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1232 OCFS2_JOURNAL_ACCESS_WRITE);
1233 if (ret) {
1234 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001235 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001236 }
1237 /* Decrease xattr count */
1238 le16_add_cpu(&xs->header->xh_count, -1);
1239 /* Remove the xattr entry and tree root which has already be set*/
1240 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1241 memset(val, 0, size);
1242
1243 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1244 if (ret < 0)
1245 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001246out:
1247 return ret;
1248}
1249
1250static int ocfs2_xattr_update_entry(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001251 handle_t *handle,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001252 struct ocfs2_xattr_info *xi,
1253 struct ocfs2_xattr_search *xs,
1254 size_t offs)
1255{
Tao Ma85db90e2008-11-12 08:27:01 +08001256 int ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001257
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001258 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1259 OCFS2_JOURNAL_ACCESS_WRITE);
1260 if (ret) {
1261 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001262 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001263 }
1264
1265 xs->here->xe_name_offset = cpu_to_le16(offs);
1266 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1267 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1268 ocfs2_xattr_set_local(xs->here, 1);
1269 else
1270 ocfs2_xattr_set_local(xs->here, 0);
1271 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1272
1273 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1274 if (ret < 0)
1275 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001276out:
1277 return ret;
1278}
1279
1280/*
1281 * ocfs2_xattr_set_value_outside()
1282 *
1283 * Set large size value in B tree.
1284 */
1285static int ocfs2_xattr_set_value_outside(struct inode *inode,
1286 struct ocfs2_xattr_info *xi,
1287 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001288 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001289 size_t offs)
1290{
1291 size_t name_len = strlen(xi->name);
1292 void *val = xs->base + offs;
1293 struct ocfs2_xattr_value_root *xv = NULL;
1294 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1295 int ret = 0;
1296
1297 memset(val, 0, size);
1298 memcpy(val, xi->name, name_len);
1299 xv = (struct ocfs2_xattr_value_root *)
1300 (val + OCFS2_XATTR_SIZE(name_len));
1301 xv->xr_clusters = 0;
1302 xv->xr_last_eb_blk = 0;
1303 xv->xr_list.l_tree_depth = 0;
1304 xv->xr_list.l_count = cpu_to_le16(1);
1305 xv->xr_list.l_next_free_rec = 0;
1306
1307 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001308 xi->value_len, ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001309 if (ret < 0) {
1310 mlog_errno(ret);
1311 return ret;
1312 }
Tao Ma85db90e2008-11-12 08:27:01 +08001313 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001314 if (ret < 0) {
1315 mlog_errno(ret);
1316 return ret;
1317 }
Tao Ma85db90e2008-11-12 08:27:01 +08001318 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, xv,
1319 xi->value, xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001320 if (ret < 0)
1321 mlog_errno(ret);
1322
1323 return ret;
1324}
1325
1326/*
1327 * ocfs2_xattr_set_entry_local()
1328 *
1329 * Set, replace or remove extended attribute in local.
1330 */
1331static void ocfs2_xattr_set_entry_local(struct inode *inode,
1332 struct ocfs2_xattr_info *xi,
1333 struct ocfs2_xattr_search *xs,
1334 struct ocfs2_xattr_entry *last,
1335 size_t min_offs)
1336{
1337 size_t name_len = strlen(xi->name);
1338 int i;
1339
1340 if (xi->value && xs->not_found) {
1341 /* Insert the new xattr entry. */
1342 le16_add_cpu(&xs->header->xh_count, 1);
1343 ocfs2_xattr_set_type(last, xi->name_index);
1344 ocfs2_xattr_set_local(last, 1);
1345 last->xe_name_len = name_len;
1346 } else {
1347 void *first_val;
1348 void *val;
1349 size_t offs, size;
1350
1351 first_val = xs->base + min_offs;
1352 offs = le16_to_cpu(xs->here->xe_name_offset);
1353 val = xs->base + offs;
1354
1355 if (le64_to_cpu(xs->here->xe_value_size) >
1356 OCFS2_XATTR_INLINE_SIZE)
1357 size = OCFS2_XATTR_SIZE(name_len) +
1358 OCFS2_XATTR_ROOT_SIZE;
1359 else
1360 size = OCFS2_XATTR_SIZE(name_len) +
1361 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1362
1363 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1364 OCFS2_XATTR_SIZE(xi->value_len)) {
1365 /* The old and the new value have the
1366 same size. Just replace the value. */
1367 ocfs2_xattr_set_local(xs->here, 1);
1368 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1369 /* Clear value bytes. */
1370 memset(val + OCFS2_XATTR_SIZE(name_len),
1371 0,
1372 OCFS2_XATTR_SIZE(xi->value_len));
1373 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1374 xi->value,
1375 xi->value_len);
1376 return;
1377 }
1378 /* Remove the old name+value. */
1379 memmove(first_val + size, first_val, val - first_val);
1380 memset(first_val, 0, size);
1381 xs->here->xe_name_hash = 0;
1382 xs->here->xe_name_offset = 0;
1383 ocfs2_xattr_set_local(xs->here, 1);
1384 xs->here->xe_value_size = 0;
1385
1386 min_offs += size;
1387
1388 /* Adjust all value offsets. */
1389 last = xs->header->xh_entries;
1390 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1391 size_t o = le16_to_cpu(last->xe_name_offset);
1392
1393 if (o < offs)
1394 last->xe_name_offset = cpu_to_le16(o + size);
1395 last += 1;
1396 }
1397
1398 if (!xi->value) {
1399 /* Remove the old entry. */
1400 last -= 1;
1401 memmove(xs->here, xs->here + 1,
1402 (void *)last - (void *)xs->here);
1403 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1404 le16_add_cpu(&xs->header->xh_count, -1);
1405 }
1406 }
1407 if (xi->value) {
1408 /* Insert the new name+value. */
1409 size_t size = OCFS2_XATTR_SIZE(name_len) +
1410 OCFS2_XATTR_SIZE(xi->value_len);
1411 void *val = xs->base + min_offs - size;
1412
1413 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1414 memset(val, 0, size);
1415 memcpy(val, xi->name, name_len);
1416 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1417 xi->value,
1418 xi->value_len);
1419 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1420 ocfs2_xattr_set_local(xs->here, 1);
1421 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1422 }
1423
1424 return;
1425}
1426
1427/*
1428 * ocfs2_xattr_set_entry()
1429 *
1430 * Set extended attribute entry into inode or block.
1431 *
1432 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1433 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1434 * then set value in B tree with set_value_outside().
1435 */
1436static int ocfs2_xattr_set_entry(struct inode *inode,
1437 struct ocfs2_xattr_info *xi,
1438 struct ocfs2_xattr_search *xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001439 struct ocfs2_xattr_set_ctxt *ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001440 int flag)
1441{
1442 struct ocfs2_xattr_entry *last;
1443 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1444 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1445 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1446 size_t size_l = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08001447 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001448 int free, i, ret;
1449 struct ocfs2_xattr_info xi_l = {
1450 .name_index = xi->name_index,
1451 .name = xi->name,
1452 .value = xi->value,
1453 .value_len = xi->value_len,
1454 };
1455
1456 /* Compute min_offs, last and free space. */
1457 last = xs->header->xh_entries;
1458
1459 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1460 size_t offs = le16_to_cpu(last->xe_name_offset);
1461 if (offs < min_offs)
1462 min_offs = offs;
1463 last += 1;
1464 }
1465
1466 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1467 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001468 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001469
1470 if (!xs->not_found) {
1471 size_t size = 0;
1472 if (ocfs2_xattr_is_local(xs->here))
1473 size = OCFS2_XATTR_SIZE(name_len) +
1474 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1475 else
1476 size = OCFS2_XATTR_SIZE(name_len) +
1477 OCFS2_XATTR_ROOT_SIZE;
1478 free += (size + sizeof(struct ocfs2_xattr_entry));
1479 }
1480 /* Check free space in inode or block */
1481 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1482 if (free < sizeof(struct ocfs2_xattr_entry) +
1483 OCFS2_XATTR_SIZE(name_len) +
1484 OCFS2_XATTR_ROOT_SIZE) {
1485 ret = -ENOSPC;
1486 goto out;
1487 }
1488 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1489 xi_l.value = (void *)&def_xv;
1490 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1491 } else if (xi->value) {
1492 if (free < sizeof(struct ocfs2_xattr_entry) +
1493 OCFS2_XATTR_SIZE(name_len) +
1494 OCFS2_XATTR_SIZE(xi->value_len)) {
1495 ret = -ENOSPC;
1496 goto out;
1497 }
1498 }
1499
1500 if (!xs->not_found) {
1501 /* For existing extended attribute */
1502 size_t size = OCFS2_XATTR_SIZE(name_len) +
1503 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1504 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1505 void *val = xs->base + offs;
1506
1507 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1508 /* Replace existing local xattr with tree root */
1509 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08001510 ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001511 if (ret < 0)
1512 mlog_errno(ret);
1513 goto out;
1514 } else if (!ocfs2_xattr_is_local(xs->here)) {
1515 /* For existing xattr which has value outside */
1516 struct ocfs2_xattr_value_root *xv = NULL;
1517 xv = (struct ocfs2_xattr_value_root *)(val +
1518 OCFS2_XATTR_SIZE(name_len));
1519
1520 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1521 /*
1522 * If new value need set outside also,
1523 * first truncate old value to new value,
1524 * then set new value with set_value_outside().
1525 */
1526 ret = ocfs2_xattr_value_truncate(inode,
1527 xs->xattr_bh,
1528 xv,
Tao Ma78f30c32008-11-12 08:27:00 +08001529 xi->value_len,
1530 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001531 if (ret < 0) {
1532 mlog_errno(ret);
1533 goto out;
1534 }
1535
Tao Ma85db90e2008-11-12 08:27:01 +08001536 ret = ocfs2_xattr_update_entry(inode,
1537 handle,
1538 xi,
1539 xs,
1540 offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001541 if (ret < 0) {
1542 mlog_errno(ret);
1543 goto out;
1544 }
1545
Tao Ma85db90e2008-11-12 08:27:01 +08001546 ret = __ocfs2_xattr_set_value_outside(inode,
1547 handle,
1548 xv,
1549 xi->value,
1550 xi->value_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001551 if (ret < 0)
1552 mlog_errno(ret);
1553 goto out;
1554 } else {
1555 /*
1556 * If new value need set in local,
1557 * just trucate old value to zero.
1558 */
1559 ret = ocfs2_xattr_value_truncate(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08001560 xs->xattr_bh,
1561 xv,
1562 0,
1563 ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001564 if (ret < 0)
1565 mlog_errno(ret);
1566 }
1567 }
1568 }
1569
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001570 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1571 OCFS2_JOURNAL_ACCESS_WRITE);
1572 if (ret) {
1573 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001574 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001575 }
1576
1577 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001578 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1579 OCFS2_JOURNAL_ACCESS_WRITE);
1580 if (ret) {
1581 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001582 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001583 }
1584 }
1585
1586 /*
1587 * Set value in local, include set tree root in local.
1588 * This is the first step for value size >INLINE_SIZE.
1589 */
1590 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1591
1592 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1593 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1594 if (ret < 0) {
1595 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08001596 goto out;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001597 }
1598 }
1599
1600 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1601 (flag & OCFS2_INLINE_XATTR_FL)) {
1602 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1603 unsigned int xattrsize = osb->s_xattr_inline_size;
1604
1605 /*
1606 * Adjust extent record count or inline data size
1607 * to reserve space for extended attribute.
1608 */
1609 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1610 struct ocfs2_inline_data *idata = &di->id2.i_data;
1611 le16_add_cpu(&idata->id_count, -xattrsize);
1612 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1613 struct ocfs2_extent_list *el = &di->id2.i_list;
1614 le16_add_cpu(&el->l_count, -(xattrsize /
1615 sizeof(struct ocfs2_extent_rec)));
1616 }
1617 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1618 }
1619 /* Update xattr flag */
1620 spin_lock(&oi->ip_lock);
1621 oi->ip_dyn_features |= flag;
1622 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1623 spin_unlock(&oi->ip_lock);
1624 /* Update inode ctime */
1625 inode->i_ctime = CURRENT_TIME;
1626 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1627 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1628
1629 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1630 if (ret < 0)
1631 mlog_errno(ret);
1632
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001633 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1634 /*
1635 * Set value outside in B tree.
1636 * This is the second step for value size > INLINE_SIZE.
1637 */
1638 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
Tao Ma78f30c32008-11-12 08:27:00 +08001639 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001640 if (ret < 0) {
1641 int ret2;
1642
1643 mlog_errno(ret);
1644 /*
1645 * If set value outside failed, we have to clean
1646 * the junk tree root we have already set in local.
1647 */
Tao Ma85db90e2008-11-12 08:27:01 +08001648 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1649 xi, xs, offs);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001650 if (ret2 < 0)
1651 mlog_errno(ret2);
1652 }
1653 }
1654out:
1655 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001656}
1657
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001658static int ocfs2_remove_value_outside(struct inode*inode,
1659 struct buffer_head *bh,
1660 struct ocfs2_xattr_header *header)
1661{
1662 int ret = 0, i;
Tao Ma78f30c32008-11-12 08:27:00 +08001663 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1664 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
1665
1666 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001667
Jan Karaa90714c2008-10-09 19:38:40 +02001668 ctxt.handle = ocfs2_start_trans(osb,
1669 ocfs2_remove_extent_credits(osb->sb));
Tao Ma85db90e2008-11-12 08:27:01 +08001670 if (IS_ERR(ctxt.handle)) {
1671 ret = PTR_ERR(ctxt.handle);
1672 mlog_errno(ret);
1673 goto out;
1674 }
1675
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001676 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1677 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1678
1679 if (!ocfs2_xattr_is_local(entry)) {
1680 struct ocfs2_xattr_value_root *xv;
1681 void *val;
1682
1683 val = (void *)header +
1684 le16_to_cpu(entry->xe_name_offset);
1685 xv = (struct ocfs2_xattr_value_root *)
1686 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
Tao Ma78f30c32008-11-12 08:27:00 +08001687 ret = ocfs2_xattr_value_truncate(inode, bh, xv,
1688 0, &ctxt);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001689 if (ret < 0) {
1690 mlog_errno(ret);
Tao Ma78f30c32008-11-12 08:27:00 +08001691 break;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001692 }
1693 }
1694 }
1695
Tao Ma85db90e2008-11-12 08:27:01 +08001696 ocfs2_commit_trans(osb, ctxt.handle);
Tao Ma78f30c32008-11-12 08:27:00 +08001697 ocfs2_schedule_truncate_log_flush(osb, 1);
1698 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Ma85db90e2008-11-12 08:27:01 +08001699out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001700 return ret;
1701}
1702
1703static int ocfs2_xattr_ibody_remove(struct inode *inode,
1704 struct buffer_head *di_bh)
1705{
1706
1707 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1708 struct ocfs2_xattr_header *header;
1709 int ret;
1710
1711 header = (struct ocfs2_xattr_header *)
1712 ((void *)di + inode->i_sb->s_blocksize -
1713 le16_to_cpu(di->i_xattr_inline_size));
1714
1715 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1716
1717 return ret;
1718}
1719
1720static int ocfs2_xattr_block_remove(struct inode *inode,
1721 struct buffer_head *blk_bh)
1722{
1723 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001724 int ret = 0;
1725
1726 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001727 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1728 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1729 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1730 } else
1731 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001732
1733 return ret;
1734}
1735
Tao Ma08413892008-08-29 09:00:19 +08001736static int ocfs2_xattr_free_block(struct inode *inode,
1737 u64 block)
1738{
1739 struct inode *xb_alloc_inode;
1740 struct buffer_head *xb_alloc_bh = NULL;
1741 struct buffer_head *blk_bh = NULL;
1742 struct ocfs2_xattr_block *xb;
1743 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1744 handle_t *handle;
1745 int ret = 0;
1746 u64 blk, bg_blkno;
1747 u16 bit;
1748
Joel Becker4ae1d692008-11-13 14:49:18 -08001749 ret = ocfs2_read_xattr_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001750 if (ret < 0) {
1751 mlog_errno(ret);
1752 goto out;
1753 }
1754
Tao Ma08413892008-08-29 09:00:19 +08001755 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1756 if (ret < 0) {
1757 mlog_errno(ret);
1758 goto out;
1759 }
1760
Joel Becker4ae1d692008-11-13 14:49:18 -08001761 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Ma08413892008-08-29 09:00:19 +08001762 blk = le64_to_cpu(xb->xb_blkno);
1763 bit = le16_to_cpu(xb->xb_suballoc_bit);
1764 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1765
1766 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1767 EXTENT_ALLOC_SYSTEM_INODE,
1768 le16_to_cpu(xb->xb_suballoc_slot));
1769 if (!xb_alloc_inode) {
1770 ret = -ENOMEM;
1771 mlog_errno(ret);
1772 goto out;
1773 }
1774 mutex_lock(&xb_alloc_inode->i_mutex);
1775
1776 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1777 if (ret < 0) {
1778 mlog_errno(ret);
1779 goto out_mutex;
1780 }
1781
1782 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1783 if (IS_ERR(handle)) {
1784 ret = PTR_ERR(handle);
1785 mlog_errno(ret);
1786 goto out_unlock;
1787 }
1788
1789 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1790 bit, bg_blkno, 1);
1791 if (ret < 0)
1792 mlog_errno(ret);
1793
1794 ocfs2_commit_trans(osb, handle);
1795out_unlock:
1796 ocfs2_inode_unlock(xb_alloc_inode, 1);
1797 brelse(xb_alloc_bh);
1798out_mutex:
1799 mutex_unlock(&xb_alloc_inode->i_mutex);
1800 iput(xb_alloc_inode);
1801out:
1802 brelse(blk_bh);
1803 return ret;
1804}
1805
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001806/*
1807 * ocfs2_xattr_remove()
1808 *
1809 * Free extended attribute resources associated with this inode.
1810 */
1811int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1812{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001813 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1814 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1815 handle_t *handle;
1816 int ret;
1817
Tiger Yang8154da32008-08-18 17:11:46 +08001818 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1819 return 0;
1820
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001821 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1822 return 0;
1823
1824 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1825 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1826 if (ret < 0) {
1827 mlog_errno(ret);
1828 goto out;
1829 }
1830 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001831
Tao Ma08413892008-08-29 09:00:19 +08001832 if (di->i_xattr_loc) {
1833 ret = ocfs2_xattr_free_block(inode,
1834 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001835 if (ret < 0) {
1836 mlog_errno(ret);
1837 goto out;
1838 }
1839 }
1840
1841 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1842 OCFS2_INODE_UPDATE_CREDITS);
1843 if (IS_ERR(handle)) {
1844 ret = PTR_ERR(handle);
1845 mlog_errno(ret);
1846 goto out;
1847 }
1848 ret = ocfs2_journal_access(handle, inode, di_bh,
1849 OCFS2_JOURNAL_ACCESS_WRITE);
1850 if (ret) {
1851 mlog_errno(ret);
1852 goto out_commit;
1853 }
1854
Tao Ma08413892008-08-29 09:00:19 +08001855 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001856
1857 spin_lock(&oi->ip_lock);
1858 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1859 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1860 spin_unlock(&oi->ip_lock);
1861
1862 ret = ocfs2_journal_dirty(handle, di_bh);
1863 if (ret < 0)
1864 mlog_errno(ret);
1865out_commit:
1866 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1867out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001868 return ret;
1869}
1870
1871static int ocfs2_xattr_has_space_inline(struct inode *inode,
1872 struct ocfs2_dinode *di)
1873{
1874 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1875 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1876 int free;
1877
1878 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1879 return 0;
1880
1881 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1882 struct ocfs2_inline_data *idata = &di->id2.i_data;
1883 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1884 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1885 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1886 le64_to_cpu(di->i_size);
1887 } else {
1888 struct ocfs2_extent_list *el = &di->id2.i_list;
1889 free = (le16_to_cpu(el->l_count) -
1890 le16_to_cpu(el->l_next_free_rec)) *
1891 sizeof(struct ocfs2_extent_rec);
1892 }
1893 if (free >= xattrsize)
1894 return 1;
1895
1896 return 0;
1897}
1898
1899/*
1900 * ocfs2_xattr_ibody_find()
1901 *
1902 * Find extended attribute in inode block and
1903 * fill search info into struct ocfs2_xattr_search.
1904 */
1905static int ocfs2_xattr_ibody_find(struct inode *inode,
1906 int name_index,
1907 const char *name,
1908 struct ocfs2_xattr_search *xs)
1909{
1910 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1911 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1912 int ret;
1913 int has_space = 0;
1914
1915 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1916 return 0;
1917
1918 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1919 down_read(&oi->ip_alloc_sem);
1920 has_space = ocfs2_xattr_has_space_inline(inode, di);
1921 up_read(&oi->ip_alloc_sem);
1922 if (!has_space)
1923 return 0;
1924 }
1925
1926 xs->xattr_bh = xs->inode_bh;
1927 xs->end = (void *)di + inode->i_sb->s_blocksize;
1928 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1929 xs->header = (struct ocfs2_xattr_header *)
1930 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1931 else
1932 xs->header = (struct ocfs2_xattr_header *)
1933 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1934 xs->base = (void *)xs->header;
1935 xs->here = xs->header->xh_entries;
1936
1937 /* Find the named attribute. */
1938 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1939 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1940 if (ret && ret != -ENODATA)
1941 return ret;
1942 xs->not_found = ret;
1943 }
1944
1945 return 0;
1946}
1947
1948/*
1949 * ocfs2_xattr_ibody_set()
1950 *
1951 * Set, replace or remove an extended attribute into inode block.
1952 *
1953 */
1954static int ocfs2_xattr_ibody_set(struct inode *inode,
1955 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08001956 struct ocfs2_xattr_search *xs,
1957 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001958{
1959 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1960 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1961 int ret;
1962
1963 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1964 return -ENOSPC;
1965
1966 down_write(&oi->ip_alloc_sem);
1967 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1968 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1969 ret = -ENOSPC;
1970 goto out;
1971 }
1972 }
1973
Tao Ma78f30c32008-11-12 08:27:00 +08001974 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001975 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1976out:
1977 up_write(&oi->ip_alloc_sem);
1978
1979 return ret;
1980}
1981
1982/*
1983 * ocfs2_xattr_block_find()
1984 *
1985 * Find extended attribute in external block and
1986 * fill search info into struct ocfs2_xattr_search.
1987 */
1988static int ocfs2_xattr_block_find(struct inode *inode,
1989 int name_index,
1990 const char *name,
1991 struct ocfs2_xattr_search *xs)
1992{
1993 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1994 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08001995 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001996 int ret = 0;
1997
1998 if (!di->i_xattr_loc)
1999 return ret;
2000
Joel Becker4ae1d692008-11-13 14:49:18 -08002001 ret = ocfs2_read_xattr_block(inode, le64_to_cpu(di->i_xattr_loc),
2002 &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002003 if (ret < 0) {
2004 mlog_errno(ret);
2005 return ret;
2006 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07002007
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002008 xs->xattr_bh = blk_bh;
Joel Becker4ae1d692008-11-13 14:49:18 -08002009 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002010
Tao Ma589dc262008-08-18 17:38:51 +08002011 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
2012 xs->header = &xb->xb_attrs.xb_header;
2013 xs->base = (void *)xs->header;
2014 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
2015 xs->here = xs->header->xh_entries;
2016
2017 ret = ocfs2_xattr_find_entry(name_index, name, xs);
2018 } else
2019 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
2020 name_index,
2021 name, xs);
2022
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002023 if (ret && ret != -ENODATA) {
2024 xs->xattr_bh = NULL;
2025 goto cleanup;
2026 }
2027 xs->not_found = ret;
2028 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002029cleanup:
2030 brelse(blk_bh);
2031
2032 return ret;
2033}
2034
2035/*
2036 * ocfs2_xattr_block_set()
2037 *
2038 * Set, replace or remove an extended attribute into external block.
2039 *
2040 */
2041static int ocfs2_xattr_block_set(struct inode *inode,
2042 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08002043 struct ocfs2_xattr_search *xs,
2044 struct ocfs2_xattr_set_ctxt *ctxt)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002045{
2046 struct buffer_head *new_bh = NULL;
2047 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2048 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma85db90e2008-11-12 08:27:01 +08002049 handle_t *handle = ctxt->handle;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002050 struct ocfs2_xattr_block *xblk = NULL;
2051 u16 suballoc_bit_start;
2052 u32 num_got;
2053 u64 first_blkno;
2054 int ret;
2055
2056 if (!xs->xattr_bh) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002057 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
2058 OCFS2_JOURNAL_ACCESS_CREATE);
2059 if (ret < 0) {
2060 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002061 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002062 }
2063
Tao Ma78f30c32008-11-12 08:27:00 +08002064 ret = ocfs2_claim_metadata(osb, handle, ctxt->meta_ac, 1,
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002065 &suballoc_bit_start, &num_got,
2066 &first_blkno);
2067 if (ret < 0) {
2068 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002069 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002070 }
2071
2072 new_bh = sb_getblk(inode->i_sb, first_blkno);
2073 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2074
2075 ret = ocfs2_journal_access(handle, inode, new_bh,
2076 OCFS2_JOURNAL_ACCESS_CREATE);
2077 if (ret < 0) {
2078 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002079 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002080 }
2081
2082 /* Initialize ocfs2_xattr_block */
2083 xs->xattr_bh = new_bh;
2084 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2085 memset(xblk, 0, inode->i_sb->s_blocksize);
2086 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2087 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2088 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2089 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2090 xblk->xb_blkno = cpu_to_le64(first_blkno);
2091
2092 xs->header = &xblk->xb_attrs.xb_header;
2093 xs->base = (void *)xs->header;
2094 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2095 xs->here = xs->header->xh_entries;
2096
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002097 ret = ocfs2_journal_dirty(handle, new_bh);
2098 if (ret < 0) {
2099 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08002100 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002101 }
2102 di->i_xattr_loc = cpu_to_le64(first_blkno);
Tao Ma85db90e2008-11-12 08:27:01 +08002103 ocfs2_journal_dirty(handle, xs->inode_bh);
Tao Ma01225592008-08-18 17:38:53 +08002104 } else
2105 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2106
2107 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2108 /* Set extended attribute into external block */
Tao Ma78f30c32008-11-12 08:27:00 +08002109 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt,
2110 OCFS2_HAS_XATTR_FL);
Tao Ma01225592008-08-18 17:38:53 +08002111 if (!ret || ret != -ENOSPC)
2112 goto end;
2113
Tao Ma78f30c32008-11-12 08:27:00 +08002114 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002115 if (ret)
2116 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002117 }
2118
Tao Ma78f30c32008-11-12 08:27:00 +08002119 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08002120
2121end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002122
2123 return ret;
2124}
2125
Tao Ma78f30c32008-11-12 08:27:00 +08002126/* Check whether the new xattr can be inserted into the inode. */
2127static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2128 struct ocfs2_xattr_info *xi,
2129 struct ocfs2_xattr_search *xs)
2130{
2131 u64 value_size;
2132 struct ocfs2_xattr_entry *last;
2133 int free, i;
2134 size_t min_offs = xs->end - xs->base;
2135
2136 if (!xs->header)
2137 return 0;
2138
2139 last = xs->header->xh_entries;
2140
2141 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
2142 size_t offs = le16_to_cpu(last->xe_name_offset);
2143 if (offs < min_offs)
2144 min_offs = offs;
2145 last += 1;
2146 }
2147
2148 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
2149 if (free < 0)
2150 return 0;
2151
2152 BUG_ON(!xs->not_found);
2153
2154 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2155 value_size = OCFS2_XATTR_ROOT_SIZE;
2156 else
2157 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2158
2159 if (free >= sizeof(struct ocfs2_xattr_entry) +
2160 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2161 return 1;
2162
2163 return 0;
2164}
2165
2166static int ocfs2_calc_xattr_set_need(struct inode *inode,
2167 struct ocfs2_dinode *di,
2168 struct ocfs2_xattr_info *xi,
2169 struct ocfs2_xattr_search *xis,
2170 struct ocfs2_xattr_search *xbs,
2171 int *clusters_need,
Tao Ma85db90e2008-11-12 08:27:01 +08002172 int *meta_need,
2173 int *credits_need)
Tao Ma78f30c32008-11-12 08:27:00 +08002174{
2175 int ret = 0, old_in_xb = 0;
Tao Ma85db90e2008-11-12 08:27:01 +08002176 int clusters_add = 0, meta_add = 0, credits = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002177 struct buffer_head *bh = NULL;
2178 struct ocfs2_xattr_block *xb = NULL;
2179 struct ocfs2_xattr_entry *xe = NULL;
2180 struct ocfs2_xattr_value_root *xv = NULL;
2181 char *base = NULL;
2182 int name_offset, name_len = 0;
2183 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2184 xi->value_len);
2185 u64 value_size;
2186
Tao Ma78f30c32008-11-12 08:27:00 +08002187 if (xis->not_found && xbs->not_found) {
Tao Ma85db90e2008-11-12 08:27:01 +08002188 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2189
2190 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
Tao Ma78f30c32008-11-12 08:27:00 +08002191 clusters_add += new_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002192 credits += ocfs2_calc_extend_credits(inode->i_sb,
2193 &def_xv.xv.xr_list,
2194 new_clusters);
2195 }
Tao Ma78f30c32008-11-12 08:27:00 +08002196
2197 goto meta_guess;
2198 }
2199
2200 if (!xis->not_found) {
2201 xe = xis->here;
2202 name_offset = le16_to_cpu(xe->xe_name_offset);
2203 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2204 base = xis->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002205 credits += OCFS2_INODE_UPDATE_CREDITS;
Tao Ma78f30c32008-11-12 08:27:00 +08002206 } else {
Joel Becker970e4932008-11-13 14:49:19 -08002207 int i, block_off = 0;
Tao Ma78f30c32008-11-12 08:27:00 +08002208 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2209 xe = xbs->here;
2210 name_offset = le16_to_cpu(xe->xe_name_offset);
2211 name_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
2212 i = xbs->here - xbs->header->xh_entries;
2213 old_in_xb = 1;
2214
2215 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2216 ret = ocfs2_xattr_bucket_get_name_value(inode,
2217 bucket_xh(xbs->bucket),
2218 i, &block_off,
2219 &name_offset);
2220 base = bucket_block(xbs->bucket, block_off);
Tao Ma85db90e2008-11-12 08:27:01 +08002221 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2222 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002223 base = xbs->base;
Tao Ma85db90e2008-11-12 08:27:01 +08002224 credits += OCFS2_XATTR_BLOCK_UPDATE_CREDITS;
2225 }
2226 }
2227
2228 /*
2229 * delete a xattr doesn't need metadata and cluster allocation.
2230 * so just calculate the credits and return.
2231 *
2232 * The credits for removing the value tree will be extended
2233 * by ocfs2_remove_extent itself.
2234 */
2235 if (!xi->value) {
2236 if (!ocfs2_xattr_is_local(xe))
Jan Karaa90714c2008-10-09 19:38:40 +02002237 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002238
2239 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002240 }
2241
2242 /* do cluster allocation guess first. */
2243 value_size = le64_to_cpu(xe->xe_value_size);
2244
2245 if (old_in_xb) {
2246 /*
2247 * In xattr set, we always try to set the xe in inode first,
2248 * so if it can be inserted into inode successfully, the old
2249 * one will be removed from the xattr block, and this xattr
2250 * will be inserted into inode as a new xattr in inode.
2251 */
2252 if (ocfs2_xattr_can_be_in_inode(inode, xi, xis)) {
2253 clusters_add += new_clusters;
Jan Karaa90714c2008-10-09 19:38:40 +02002254 credits += ocfs2_remove_extent_credits(inode->i_sb) +
Tao Ma85db90e2008-11-12 08:27:01 +08002255 OCFS2_INODE_UPDATE_CREDITS;
2256 if (!ocfs2_xattr_is_local(xe))
2257 credits += ocfs2_calc_extend_credits(
2258 inode->i_sb,
2259 &def_xv.xv.xr_list,
2260 new_clusters);
Tao Ma78f30c32008-11-12 08:27:00 +08002261 goto out;
2262 }
2263 }
2264
2265 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
2266 /* the new values will be stored outside. */
2267 u32 old_clusters = 0;
2268
2269 if (!ocfs2_xattr_is_local(xe)) {
2270 old_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2271 value_size);
2272 xv = (struct ocfs2_xattr_value_root *)
2273 (base + name_offset + name_len);
Tao Ma97aff522008-11-19 16:48:41 +08002274 value_size = OCFS2_XATTR_ROOT_SIZE;
Tao Ma78f30c32008-11-12 08:27:00 +08002275 } else
2276 xv = &def_xv.xv;
2277
Tao Ma85db90e2008-11-12 08:27:01 +08002278 if (old_clusters >= new_clusters) {
Jan Karaa90714c2008-10-09 19:38:40 +02002279 credits += ocfs2_remove_extent_credits(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002280 goto out;
Tao Ma85db90e2008-11-12 08:27:01 +08002281 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002282 meta_add += ocfs2_extend_meta_needed(&xv->xr_list);
2283 clusters_add += new_clusters - old_clusters;
Tao Ma85db90e2008-11-12 08:27:01 +08002284 credits += ocfs2_calc_extend_credits(inode->i_sb,
2285 &xv->xr_list,
2286 new_clusters -
2287 old_clusters);
Tao Ma97aff522008-11-19 16:48:41 +08002288 if (value_size >= OCFS2_XATTR_ROOT_SIZE)
2289 goto out;
Tao Ma78f30c32008-11-12 08:27:00 +08002290 }
2291 } else {
2292 /*
2293 * Now the new value will be stored inside. So if the new
2294 * value is smaller than the size of value root or the old
2295 * value, we don't need any allocation, otherwise we have
2296 * to guess metadata allocation.
2297 */
2298 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) ||
2299 (!ocfs2_xattr_is_local(xe) &&
2300 OCFS2_XATTR_ROOT_SIZE >= xi->value_len))
2301 goto out;
2302 }
2303
2304meta_guess:
2305 /* calculate metadata allocation. */
2306 if (di->i_xattr_loc) {
2307 if (!xbs->xattr_bh) {
Joel Becker4ae1d692008-11-13 14:49:18 -08002308 ret = ocfs2_read_xattr_block(inode,
2309 le64_to_cpu(di->i_xattr_loc),
2310 &bh);
Tao Ma78f30c32008-11-12 08:27:00 +08002311 if (ret) {
2312 mlog_errno(ret);
2313 goto out;
2314 }
2315
2316 xb = (struct ocfs2_xattr_block *)bh->b_data;
2317 } else
2318 xb = (struct ocfs2_xattr_block *)xbs->xattr_bh->b_data;
2319
2320 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
2321 struct ocfs2_extent_list *el =
2322 &xb->xb_attrs.xb_root.xt_list;
2323 meta_add += ocfs2_extend_meta_needed(el);
Tao Ma85db90e2008-11-12 08:27:01 +08002324 credits += ocfs2_calc_extend_credits(inode->i_sb,
2325 el, 1);
Tao Ma78f30c32008-11-12 08:27:00 +08002326 }
2327
2328 /*
2329 * This cluster will be used either for new bucket or for
2330 * new xattr block.
2331 * If the cluster size is the same as the bucket size, one
2332 * more is needed since we may need to extend the bucket
2333 * also.
2334 */
2335 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002336 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002337 if (OCFS2_XATTR_BUCKET_SIZE ==
Tao Ma85db90e2008-11-12 08:27:01 +08002338 OCFS2_SB(inode->i_sb)->s_clustersize) {
2339 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma78f30c32008-11-12 08:27:00 +08002340 clusters_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002341 }
2342 } else {
Tao Ma78f30c32008-11-12 08:27:00 +08002343 meta_add += 1;
Tao Ma85db90e2008-11-12 08:27:01 +08002344 credits += OCFS2_XATTR_BLOCK_CREATE_CREDITS;
2345 }
Tao Ma78f30c32008-11-12 08:27:00 +08002346out:
2347 if (clusters_need)
2348 *clusters_need = clusters_add;
2349 if (meta_need)
2350 *meta_need = meta_add;
Tao Ma85db90e2008-11-12 08:27:01 +08002351 if (credits_need)
2352 *credits_need = credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002353 brelse(bh);
2354 return ret;
2355}
2356
2357static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2358 struct ocfs2_dinode *di,
2359 struct ocfs2_xattr_info *xi,
2360 struct ocfs2_xattr_search *xis,
2361 struct ocfs2_xattr_search *xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002362 struct ocfs2_xattr_set_ctxt *ctxt,
2363 int *credits)
Tao Ma78f30c32008-11-12 08:27:00 +08002364{
2365 int clusters_add, meta_add, ret;
2366 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2367
2368 memset(ctxt, 0, sizeof(struct ocfs2_xattr_set_ctxt));
2369
2370 ocfs2_init_dealloc_ctxt(&ctxt->dealloc);
2371
2372 ret = ocfs2_calc_xattr_set_need(inode, di, xi, xis, xbs,
Tao Ma85db90e2008-11-12 08:27:01 +08002373 &clusters_add, &meta_add, credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002374 if (ret) {
2375 mlog_errno(ret);
2376 return ret;
2377 }
2378
Tao Ma85db90e2008-11-12 08:27:01 +08002379 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2380 "credits = %d\n", xi->name, meta_add, clusters_add, *credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002381
2382 if (meta_add) {
2383 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
2384 &ctxt->meta_ac);
2385 if (ret) {
2386 mlog_errno(ret);
2387 goto out;
2388 }
2389 }
2390
2391 if (clusters_add) {
2392 ret = ocfs2_reserve_clusters(osb, clusters_add, &ctxt->data_ac);
2393 if (ret)
2394 mlog_errno(ret);
2395 }
2396out:
2397 if (ret) {
2398 if (ctxt->meta_ac) {
2399 ocfs2_free_alloc_context(ctxt->meta_ac);
2400 ctxt->meta_ac = NULL;
2401 }
2402
2403 /*
2404 * We cannot have an error and a non null ctxt->data_ac.
2405 */
2406 }
2407
2408 return ret;
2409}
2410
Tao Ma85db90e2008-11-12 08:27:01 +08002411static int __ocfs2_xattr_set_handle(struct inode *inode,
2412 struct ocfs2_dinode *di,
2413 struct ocfs2_xattr_info *xi,
2414 struct ocfs2_xattr_search *xis,
2415 struct ocfs2_xattr_search *xbs,
2416 struct ocfs2_xattr_set_ctxt *ctxt)
2417{
Tao Ma9f868f12008-11-19 16:48:42 +08002418 int ret = 0, credits, old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002419
2420 if (!xi->value) {
2421 /* Remove existing extended attribute */
2422 if (!xis->not_found)
2423 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2424 else if (!xbs->not_found)
2425 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2426 } else {
2427 /* We always try to set extended attribute into inode first*/
2428 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
2429 if (!ret && !xbs->not_found) {
2430 /*
2431 * If succeed and that extended attribute existing in
2432 * external block, then we will remove it.
2433 */
2434 xi->value = NULL;
2435 xi->value_len = 0;
2436
Tao Ma9f868f12008-11-19 16:48:42 +08002437 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002438 xis->not_found = -ENODATA;
2439 ret = ocfs2_calc_xattr_set_need(inode,
2440 di,
2441 xi,
2442 xis,
2443 xbs,
2444 NULL,
2445 NULL,
2446 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002447 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002448 if (ret) {
2449 mlog_errno(ret);
2450 goto out;
2451 }
2452
2453 ret = ocfs2_extend_trans(ctxt->handle, credits +
2454 ctxt->handle->h_buffer_credits);
2455 if (ret) {
2456 mlog_errno(ret);
2457 goto out;
2458 }
2459 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2460 } else if (ret == -ENOSPC) {
2461 if (di->i_xattr_loc && !xbs->xattr_bh) {
2462 ret = ocfs2_xattr_block_find(inode,
2463 xi->name_index,
2464 xi->name, xbs);
2465 if (ret)
2466 goto out;
2467
Tao Ma9f868f12008-11-19 16:48:42 +08002468 old_found = xis->not_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002469 xis->not_found = -ENODATA;
2470 ret = ocfs2_calc_xattr_set_need(inode,
2471 di,
2472 xi,
2473 xis,
2474 xbs,
2475 NULL,
2476 NULL,
2477 &credits);
Tao Ma9f868f12008-11-19 16:48:42 +08002478 xis->not_found = old_found;
Tao Ma85db90e2008-11-12 08:27:01 +08002479 if (ret) {
2480 mlog_errno(ret);
2481 goto out;
2482 }
2483
2484 ret = ocfs2_extend_trans(ctxt->handle, credits +
2485 ctxt->handle->h_buffer_credits);
2486 if (ret) {
2487 mlog_errno(ret);
2488 goto out;
2489 }
2490 }
2491 /*
2492 * If no space in inode, we will set extended attribute
2493 * into external block.
2494 */
2495 ret = ocfs2_xattr_block_set(inode, xi, xbs, ctxt);
2496 if (ret)
2497 goto out;
2498 if (!xis->not_found) {
2499 /*
2500 * If succeed and that extended attribute
2501 * existing in inode, we will remove it.
2502 */
2503 xi->value = NULL;
2504 xi->value_len = 0;
2505 xbs->not_found = -ENODATA;
2506 ret = ocfs2_calc_xattr_set_need(inode,
2507 di,
2508 xi,
2509 xis,
2510 xbs,
2511 NULL,
2512 NULL,
2513 &credits);
2514 if (ret) {
2515 mlog_errno(ret);
2516 goto out;
2517 }
2518
2519 ret = ocfs2_extend_trans(ctxt->handle, credits +
2520 ctxt->handle->h_buffer_credits);
2521 if (ret) {
2522 mlog_errno(ret);
2523 goto out;
2524 }
2525 ret = ocfs2_xattr_ibody_set(inode, xi,
2526 xis, ctxt);
2527 }
2528 }
2529 }
2530
2531out:
2532 return ret;
2533}
2534
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002535/*
Tiger Yang6c3faba2008-11-14 11:16:03 +08002536 * This function only called duing creating inode
2537 * for init security/acl xattrs of the new inode.
2538 * The xattrs could be put into ibody or extent block,
2539 * xattr bucket would not be use in this case.
2540 * transanction credits also be reserved in here.
2541 */
2542int ocfs2_xattr_set_handle(handle_t *handle,
2543 struct inode *inode,
2544 struct buffer_head *di_bh,
2545 int name_index,
2546 const char *name,
2547 const void *value,
2548 size_t value_len,
2549 int flags,
2550 struct ocfs2_alloc_context *meta_ac,
2551 struct ocfs2_alloc_context *data_ac)
2552{
2553 struct ocfs2_dinode *di;
2554 int ret;
2555
2556 struct ocfs2_xattr_info xi = {
2557 .name_index = name_index,
2558 .name = name,
2559 .value = value,
2560 .value_len = value_len,
2561 };
2562
2563 struct ocfs2_xattr_search xis = {
2564 .not_found = -ENODATA,
2565 };
2566
2567 struct ocfs2_xattr_search xbs = {
2568 .not_found = -ENODATA,
2569 };
2570
2571 struct ocfs2_xattr_set_ctxt ctxt = {
2572 .handle = handle,
2573 .meta_ac = meta_ac,
2574 .data_ac = data_ac,
2575 };
2576
2577 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2578 return -EOPNOTSUPP;
2579
2580 xis.inode_bh = xbs.inode_bh = di_bh;
2581 di = (struct ocfs2_dinode *)di_bh->b_data;
2582
2583 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2584
2585 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2586 if (ret)
2587 goto cleanup;
2588 if (xis.not_found) {
2589 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2590 if (ret)
2591 goto cleanup;
2592 }
2593
2594 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2595
2596cleanup:
2597 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2598 brelse(xbs.xattr_bh);
2599
2600 return ret;
2601}
2602
2603/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002604 * ocfs2_xattr_set()
2605 *
2606 * Set, replace or remove an extended attribute for this inode.
2607 * value is NULL to remove an existing extended attribute, else either
2608 * create or replace an extended attribute.
2609 */
2610int ocfs2_xattr_set(struct inode *inode,
2611 int name_index,
2612 const char *name,
2613 const void *value,
2614 size_t value_len,
2615 int flags)
2616{
2617 struct buffer_head *di_bh = NULL;
2618 struct ocfs2_dinode *di;
Tao Ma85db90e2008-11-12 08:27:01 +08002619 int ret, credits;
Tao Ma78f30c32008-11-12 08:27:00 +08002620 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08002621 struct inode *tl_inode = osb->osb_tl_inode;
Tao Ma78f30c32008-11-12 08:27:00 +08002622 struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, };
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002623
2624 struct ocfs2_xattr_info xi = {
2625 .name_index = name_index,
2626 .name = name,
2627 .value = value,
2628 .value_len = value_len,
2629 };
2630
2631 struct ocfs2_xattr_search xis = {
2632 .not_found = -ENODATA,
2633 };
2634
2635 struct ocfs2_xattr_search xbs = {
2636 .not_found = -ENODATA,
2637 };
2638
Tiger Yang8154da32008-08-18 17:11:46 +08002639 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2640 return -EOPNOTSUPP;
2641
Joel Beckerba937122008-10-24 19:13:20 -07002642 /*
2643 * Only xbs will be used on indexed trees. xis doesn't need a
2644 * bucket.
2645 */
2646 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2647 if (!xbs.bucket) {
2648 mlog_errno(-ENOMEM);
2649 return -ENOMEM;
2650 }
2651
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002652 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2653 if (ret < 0) {
2654 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002655 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002656 }
2657 xis.inode_bh = xbs.inode_bh = di_bh;
2658 di = (struct ocfs2_dinode *)di_bh->b_data;
2659
2660 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2661 /*
2662 * Scan inode and external block to find the same name
2663 * extended attribute and collect search infomation.
2664 */
2665 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2666 if (ret)
2667 goto cleanup;
2668 if (xis.not_found) {
2669 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2670 if (ret)
2671 goto cleanup;
2672 }
2673
2674 if (xis.not_found && xbs.not_found) {
2675 ret = -ENODATA;
2676 if (flags & XATTR_REPLACE)
2677 goto cleanup;
2678 ret = 0;
2679 if (!value)
2680 goto cleanup;
2681 } else {
2682 ret = -EEXIST;
2683 if (flags & XATTR_CREATE)
2684 goto cleanup;
2685 }
2686
Tao Ma85db90e2008-11-12 08:27:01 +08002687
2688 mutex_lock(&tl_inode->i_mutex);
2689
2690 if (ocfs2_truncate_log_needs_flush(osb)) {
2691 ret = __ocfs2_flush_truncate_log(osb);
2692 if (ret < 0) {
2693 mutex_unlock(&tl_inode->i_mutex);
2694 mlog_errno(ret);
2695 goto cleanup;
2696 }
2697 }
2698 mutex_unlock(&tl_inode->i_mutex);
2699
2700 ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis,
2701 &xbs, &ctxt, &credits);
Tao Ma78f30c32008-11-12 08:27:00 +08002702 if (ret) {
2703 mlog_errno(ret);
2704 goto cleanup;
2705 }
2706
Tao Ma85db90e2008-11-12 08:27:01 +08002707 ctxt.handle = ocfs2_start_trans(osb, credits);
2708 if (IS_ERR(ctxt.handle)) {
2709 ret = PTR_ERR(ctxt.handle);
2710 mlog_errno(ret);
2711 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002712 }
Tao Ma85db90e2008-11-12 08:27:01 +08002713
2714 ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt);
2715
2716 ocfs2_commit_trans(osb, ctxt.handle);
2717
Tao Ma78f30c32008-11-12 08:27:00 +08002718 if (ctxt.data_ac)
2719 ocfs2_free_alloc_context(ctxt.data_ac);
2720 if (ctxt.meta_ac)
2721 ocfs2_free_alloc_context(ctxt.meta_ac);
2722 if (ocfs2_dealloc_has_cluster(&ctxt.dealloc))
2723 ocfs2_schedule_truncate_log_flush(osb, 1);
2724 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002725cleanup:
2726 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2727 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002728cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002729 brelse(di_bh);
2730 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002731 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002732
2733 return ret;
2734}
2735
Tao Ma0c044f02008-08-18 17:38:50 +08002736/*
2737 * Find the xattr extent rec which may contains name_hash.
2738 * e_cpos will be the first name hash of the xattr rec.
2739 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2740 */
2741static int ocfs2_xattr_get_rec(struct inode *inode,
2742 u32 name_hash,
2743 u64 *p_blkno,
2744 u32 *e_cpos,
2745 u32 *num_clusters,
2746 struct ocfs2_extent_list *el)
2747{
2748 int ret = 0, i;
2749 struct buffer_head *eb_bh = NULL;
2750 struct ocfs2_extent_block *eb;
2751 struct ocfs2_extent_rec *rec = NULL;
2752 u64 e_blkno = 0;
2753
2754 if (el->l_tree_depth) {
2755 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2756 if (ret) {
2757 mlog_errno(ret);
2758 goto out;
2759 }
2760
2761 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2762 el = &eb->h_list;
2763
2764 if (el->l_tree_depth) {
2765 ocfs2_error(inode->i_sb,
2766 "Inode %lu has non zero tree depth in "
2767 "xattr tree block %llu\n", inode->i_ino,
2768 (unsigned long long)eb_bh->b_blocknr);
2769 ret = -EROFS;
2770 goto out;
2771 }
2772 }
2773
2774 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2775 rec = &el->l_recs[i];
2776
2777 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2778 e_blkno = le64_to_cpu(rec->e_blkno);
2779 break;
2780 }
2781 }
2782
2783 if (!e_blkno) {
2784 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2785 "record (%u, %u, 0) in xattr", inode->i_ino,
2786 le32_to_cpu(rec->e_cpos),
2787 ocfs2_rec_clusters(el, rec));
2788 ret = -EROFS;
2789 goto out;
2790 }
2791
2792 *p_blkno = le64_to_cpu(rec->e_blkno);
2793 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2794 if (e_cpos)
2795 *e_cpos = le32_to_cpu(rec->e_cpos);
2796out:
2797 brelse(eb_bh);
2798 return ret;
2799}
2800
2801typedef int (xattr_bucket_func)(struct inode *inode,
2802 struct ocfs2_xattr_bucket *bucket,
2803 void *para);
2804
Tao Ma589dc262008-08-18 17:38:51 +08002805static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002806 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002807 int name_index,
2808 const char *name,
2809 u32 name_hash,
2810 u16 *xe_index,
2811 int *found)
2812{
2813 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002814 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002815 size_t name_len = strlen(name);
2816 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002817 char *xe_name;
2818
2819 /*
2820 * We don't use binary search in the bucket because there
2821 * may be multiple entries with the same name hash.
2822 */
2823 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2824 xe = &xh->xh_entries[i];
2825
2826 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2827 continue;
2828 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2829 break;
2830
2831 cmp = name_index - ocfs2_xattr_get_type(xe);
2832 if (!cmp)
2833 cmp = name_len - xe->xe_name_len;
2834 if (cmp)
2835 continue;
2836
2837 ret = ocfs2_xattr_bucket_get_name_value(inode,
2838 xh,
2839 i,
2840 &block_off,
2841 &new_offset);
2842 if (ret) {
2843 mlog_errno(ret);
2844 break;
2845 }
2846
Joel Becker970e4932008-11-13 14:49:19 -08002847
Joel Beckere2356a32008-10-27 15:01:54 -07002848 xe_name = bucket_block(bucket, block_off) + new_offset;
2849 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002850 *xe_index = i;
2851 *found = 1;
2852 ret = 0;
2853 break;
2854 }
2855 }
2856
2857 return ret;
2858}
2859
2860/*
2861 * Find the specified xattr entry in a series of buckets.
2862 * This series start from p_blkno and last for num_clusters.
2863 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2864 * the num of the valid buckets.
2865 *
2866 * Return the buffer_head this xattr should reside in. And if the xattr's
2867 * hash is in the gap of 2 buckets, return the lower bucket.
2868 */
2869static int ocfs2_xattr_bucket_find(struct inode *inode,
2870 int name_index,
2871 const char *name,
2872 u32 name_hash,
2873 u64 p_blkno,
2874 u32 first_hash,
2875 u32 num_clusters,
2876 struct ocfs2_xattr_search *xs)
2877{
2878 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002879 struct ocfs2_xattr_header *xh = NULL;
2880 struct ocfs2_xattr_entry *xe = NULL;
2881 u16 index = 0;
2882 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2883 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002884 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002885 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002886 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002887
Joel Beckere2356a32008-10-27 15:01:54 -07002888 search = ocfs2_xattr_bucket_new(inode);
2889 if (!search) {
2890 ret = -ENOMEM;
2891 mlog_errno(ret);
2892 goto out;
2893 }
2894
2895 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002896 if (ret) {
2897 mlog_errno(ret);
2898 goto out;
2899 }
2900
Joel Beckere2356a32008-10-27 15:01:54 -07002901 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002902 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002903 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002904 ocfs2_xattr_bucket_relse(search);
2905
Tao Ma589dc262008-08-18 17:38:51 +08002906 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002907 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002908 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002909 if (ret) {
2910 mlog_errno(ret);
2911 goto out;
2912 }
2913
Joel Beckere2356a32008-10-27 15:01:54 -07002914 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002915 xe = &xh->xh_entries[0];
2916 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2917 high_bucket = bucket - 1;
2918 continue;
2919 }
2920
2921 /*
2922 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08002923 * bucket is larger than the search one. for an empty
2924 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002925 */
Tao Ma5a095612008-09-19 22:17:41 +08002926 if (xh->xh_count)
2927 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2928
Tao Ma589dc262008-08-18 17:38:51 +08002929 last_hash = le32_to_cpu(xe->xe_name_hash);
2930
Joel Beckere2356a32008-10-27 15:01:54 -07002931 /* record lower_blkno which may be the insert place. */
2932 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002933
2934 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2935 low_bucket = bucket + 1;
2936 continue;
2937 }
2938
2939 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002940 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002941 name_index, name, name_hash,
2942 &index, &found);
2943 if (ret) {
2944 mlog_errno(ret);
2945 goto out;
2946 }
2947 break;
2948 }
2949
2950 /*
2951 * Record the bucket we have found.
2952 * When the xattr's hash value is in the gap of 2 buckets, we will
2953 * always set it to the previous bucket.
2954 */
Joel Beckere2356a32008-10-27 15:01:54 -07002955 if (!lower_blkno)
2956 lower_blkno = p_blkno;
2957
2958 /* This should be in cache - we just read it during the search */
2959 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2960 if (ret) {
2961 mlog_errno(ret);
2962 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08002963 }
Tao Ma589dc262008-08-18 17:38:51 +08002964
Joel Beckerba937122008-10-24 19:13:20 -07002965 xs->header = bucket_xh(xs->bucket);
2966 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002967 xs->end = xs->base + inode->i_sb->s_blocksize;
2968
2969 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08002970 xs->here = &xs->header->xh_entries[index];
2971 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07002972 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08002973 } else
2974 ret = -ENODATA;
2975
2976out:
Joel Beckere2356a32008-10-27 15:01:54 -07002977 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08002978 return ret;
2979}
2980
2981static int ocfs2_xattr_index_block_find(struct inode *inode,
2982 struct buffer_head *root_bh,
2983 int name_index,
2984 const char *name,
2985 struct ocfs2_xattr_search *xs)
2986{
2987 int ret;
2988 struct ocfs2_xattr_block *xb =
2989 (struct ocfs2_xattr_block *)root_bh->b_data;
2990 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2991 struct ocfs2_extent_list *el = &xb_root->xt_list;
2992 u64 p_blkno = 0;
2993 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08002994 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08002995
2996 if (le16_to_cpu(el->l_next_free_rec) == 0)
2997 return -ENODATA;
2998
2999 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
3000 name, name_hash, name_index);
3001
3002 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
3003 &num_clusters, el);
3004 if (ret) {
3005 mlog_errno(ret);
3006 goto out;
3007 }
3008
3009 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
3010
3011 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07003012 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
3013 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08003014
3015 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
3016 p_blkno, first_hash, num_clusters, xs);
3017
3018out:
3019 return ret;
3020}
3021
Tao Ma0c044f02008-08-18 17:38:50 +08003022static int ocfs2_iterate_xattr_buckets(struct inode *inode,
3023 u64 blkno,
3024 u32 clusters,
3025 xattr_bucket_func *func,
3026 void *para)
3027{
Joel Becker6dde41d2008-10-24 17:16:48 -07003028 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08003029 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
3030 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07003031 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08003032
Joel Beckerba937122008-10-24 19:13:20 -07003033 bucket = ocfs2_xattr_bucket_new(inode);
3034 if (!bucket) {
3035 mlog_errno(-ENOMEM);
3036 return -ENOMEM;
3037 }
Tao Ma0c044f02008-08-18 17:38:50 +08003038
3039 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003040 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003041
Joel Beckerba937122008-10-24 19:13:20 -07003042 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
3043 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08003044 if (ret) {
3045 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003046 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003047 }
3048
Tao Ma0c044f02008-08-18 17:38:50 +08003049 /*
3050 * The real bucket num in this series of blocks is stored
3051 * in the 1st bucket.
3052 */
3053 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07003054 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08003055
Mark Fashehde29c082008-10-29 14:45:30 -07003056 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
3057 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07003058 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08003059 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07003060 ret = func(inode, bucket, para);
3061 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08003062 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07003063 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08003064 }
3065
Joel Beckerba937122008-10-24 19:13:20 -07003066 ocfs2_xattr_bucket_relse(bucket);
3067 if (ret)
3068 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003069 }
3070
Joel Beckerba937122008-10-24 19:13:20 -07003071 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08003072 return ret;
3073}
3074
3075struct ocfs2_xattr_tree_list {
3076 char *buffer;
3077 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08003078 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08003079};
3080
3081static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
3082 struct ocfs2_xattr_header *xh,
3083 int index,
3084 int *block_off,
3085 int *new_offset)
3086{
3087 u16 name_offset;
3088
3089 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
3090 return -EINVAL;
3091
3092 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
3093
3094 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
3095 *new_offset = name_offset % inode->i_sb->s_blocksize;
3096
3097 return 0;
3098}
3099
3100static int ocfs2_list_xattr_bucket(struct inode *inode,
3101 struct ocfs2_xattr_bucket *bucket,
3102 void *para)
3103{
Tao Ma936b8832008-10-09 23:06:14 +08003104 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08003105 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08003106 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08003107 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08003108
Joel Becker3e632942008-10-24 17:04:49 -07003109 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
3110 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08003111 type = ocfs2_xattr_get_type(entry);
3112 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08003113
Tao Ma936b8832008-10-09 23:06:14 +08003114 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08003115 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07003116 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08003117 i,
3118 &block_off,
3119 &new_offset);
3120 if (ret)
3121 break;
Tao Ma936b8832008-10-09 23:06:14 +08003122
Joel Becker51def392008-10-24 16:57:21 -07003123 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08003124 new_offset;
3125 ret = ocfs2_xattr_list_entry(xl->buffer,
3126 xl->buffer_size,
3127 &xl->result,
3128 prefix, name,
3129 entry->xe_name_len);
3130 if (ret)
3131 break;
Tao Ma0c044f02008-08-18 17:38:50 +08003132 }
3133 }
3134
3135 return ret;
3136}
3137
3138static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
3139 struct ocfs2_xattr_tree_root *xt,
3140 char *buffer,
3141 size_t buffer_size)
3142{
3143 struct ocfs2_extent_list *el = &xt->xt_list;
3144 int ret = 0;
3145 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
3146 u64 p_blkno = 0;
3147 struct ocfs2_xattr_tree_list xl = {
3148 .buffer = buffer,
3149 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08003150 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08003151 };
3152
3153 if (le16_to_cpu(el->l_next_free_rec) == 0)
3154 return 0;
3155
3156 while (name_hash > 0) {
3157 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
3158 &e_cpos, &num_clusters, el);
3159 if (ret) {
3160 mlog_errno(ret);
3161 goto out;
3162 }
3163
3164 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
3165 ocfs2_list_xattr_bucket,
3166 &xl);
3167 if (ret) {
3168 mlog_errno(ret);
3169 goto out;
3170 }
3171
3172 if (e_cpos == 0)
3173 break;
3174
3175 name_hash = e_cpos - 1;
3176 }
3177
Tao Ma936b8832008-10-09 23:06:14 +08003178 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08003179out:
3180 return ret;
3181}
Tao Ma01225592008-08-18 17:38:53 +08003182
3183static int cmp_xe(const void *a, const void *b)
3184{
3185 const struct ocfs2_xattr_entry *l = a, *r = b;
3186 u32 l_hash = le32_to_cpu(l->xe_name_hash);
3187 u32 r_hash = le32_to_cpu(r->xe_name_hash);
3188
3189 if (l_hash > r_hash)
3190 return 1;
3191 if (l_hash < r_hash)
3192 return -1;
3193 return 0;
3194}
3195
3196static void swap_xe(void *a, void *b, int size)
3197{
3198 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
3199
3200 tmp = *l;
3201 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
3202 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
3203}
3204
3205/*
3206 * When the ocfs2_xattr_block is filled up, new bucket will be created
3207 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07003208 * The header goes at the start of the bucket, and the names+values are
3209 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08003210 * Note: we need to sort the entries since they are not saved in order
3211 * in the ocfs2_xattr_block.
3212 */
3213static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
3214 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07003215 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08003216{
3217 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07003218 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003219 u16 offset, size, off_change;
3220 struct ocfs2_xattr_entry *xe;
3221 struct ocfs2_xattr_block *xb =
3222 (struct ocfs2_xattr_block *)xb_bh->b_data;
3223 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003224 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003225 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07003226 char *src = xb_bh->b_data;
3227 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08003228
3229 mlog(0, "cp xattr from block %llu to bucket %llu\n",
3230 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07003231 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08003232
Joel Becker178eeac2008-10-27 15:18:29 -07003233 for (i = 0; i < blks; i++)
3234 memset(bucket_block(bucket, i), 0, blocksize);
3235
Tao Ma01225592008-08-18 17:38:53 +08003236 /*
3237 * Since the xe_name_offset is based on ocfs2_xattr_header,
3238 * there is a offset change corresponding to the change of
3239 * ocfs2_xattr_header's position.
3240 */
3241 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3242 xe = &xb_xh->xh_entries[count - 1];
3243 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
3244 size = blocksize - offset;
3245
3246 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08003247 memcpy(target + offset, src + offset, size);
3248
3249 /* Init new header now. */
3250 xh->xh_count = xb_xh->xh_count;
3251 xh->xh_num_buckets = cpu_to_le16(1);
3252 xh->xh_name_value_len = cpu_to_le16(size);
3253 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
3254
3255 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07003256 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003257 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
3258 size = count * sizeof(struct ocfs2_xattr_entry);
3259 memcpy(target + offset, (char *)xb_xh + offset, size);
3260
3261 /* Change the xe offset for all the xe because of the move. */
3262 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
3263 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
3264 for (i = 0; i < count; i++)
3265 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
3266
3267 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
3268 offset, size, off_change);
3269
3270 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
3271 cmp_xe, swap_xe);
3272}
3273
3274/*
3275 * After we move xattr from block to index btree, we have to
3276 * update ocfs2_xattr_search to the new xe and base.
3277 *
3278 * When the entry is in xattr block, xattr_bh indicates the storage place.
3279 * While if the entry is in index b-tree, "bucket" indicates the
3280 * real place of the xattr.
3281 */
Joel Becker178eeac2008-10-27 15:18:29 -07003282static void ocfs2_xattr_update_xattr_search(struct inode *inode,
3283 struct ocfs2_xattr_search *xs,
3284 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08003285{
Tao Ma01225592008-08-18 17:38:53 +08003286 char *buf = old_bh->b_data;
3287 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
3288 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07003289 int i;
Tao Ma01225592008-08-18 17:38:53 +08003290
Joel Beckerba937122008-10-24 19:13:20 -07003291 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07003292 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08003293 xs->end = xs->base + inode->i_sb->s_blocksize;
3294
Joel Becker178eeac2008-10-27 15:18:29 -07003295 if (xs->not_found)
3296 return;
Tao Ma01225592008-08-18 17:38:53 +08003297
Joel Becker178eeac2008-10-27 15:18:29 -07003298 i = xs->here - old_xh->xh_entries;
3299 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08003300}
3301
3302static int ocfs2_xattr_create_index_block(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08003303 struct ocfs2_xattr_search *xs,
3304 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08003305{
Tao Ma85db90e2008-11-12 08:27:01 +08003306 int ret;
Tao Ma01225592008-08-18 17:38:53 +08003307 u32 bit_off, len;
3308 u64 blkno;
Tao Ma85db90e2008-11-12 08:27:01 +08003309 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08003310 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3311 struct ocfs2_inode_info *oi = OCFS2_I(inode);
Tao Ma01225592008-08-18 17:38:53 +08003312 struct buffer_head *xb_bh = xs->xattr_bh;
3313 struct ocfs2_xattr_block *xb =
3314 (struct ocfs2_xattr_block *)xb_bh->b_data;
3315 struct ocfs2_xattr_tree_root *xr;
3316 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08003317
3318 mlog(0, "create xattr index block for %llu\n",
3319 (unsigned long long)xb_bh->b_blocknr);
3320
3321 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07003322 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08003323
Tao Ma01225592008-08-18 17:38:53 +08003324 /*
3325 * XXX:
3326 * We can use this lock for now, and maybe move to a dedicated mutex
3327 * if performance becomes a problem later.
3328 */
3329 down_write(&oi->ip_alloc_sem);
3330
Tao Ma01225592008-08-18 17:38:53 +08003331 ret = ocfs2_journal_access(handle, inode, xb_bh,
3332 OCFS2_JOURNAL_ACCESS_WRITE);
3333 if (ret) {
3334 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003335 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003336 }
3337
Tao Ma78f30c32008-11-12 08:27:00 +08003338 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac,
3339 1, 1, &bit_off, &len);
Tao Ma01225592008-08-18 17:38:53 +08003340 if (ret) {
3341 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003342 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003343 }
3344
3345 /*
3346 * The bucket may spread in many blocks, and
3347 * we will only touch the 1st block and the last block
3348 * in the whole bucket(one for entry and one for data).
3349 */
3350 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
3351
Mark Fashehde29c082008-10-29 14:45:30 -07003352 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
3353 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08003354
Joel Becker178eeac2008-10-27 15:18:29 -07003355 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08003356 if (ret) {
3357 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003358 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003359 }
3360
Joel Becker178eeac2008-10-27 15:18:29 -07003361 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
3362 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07003363 if (ret) {
3364 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003365 goto out;
Joel Beckerbd60bd32008-10-20 18:25:56 -07003366 }
Tao Ma01225592008-08-18 17:38:53 +08003367
Joel Becker178eeac2008-10-27 15:18:29 -07003368 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
3369 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
3370
3371 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
3372
Tao Ma01225592008-08-18 17:38:53 +08003373 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
3374 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
3375 offsetof(struct ocfs2_xattr_block, xb_attrs));
3376
3377 xr = &xb->xb_attrs.xb_root;
3378 xr->xt_clusters = cpu_to_le32(1);
3379 xr->xt_last_eb_blk = 0;
3380 xr->xt_list.l_tree_depth = 0;
3381 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
3382 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
3383
3384 xr->xt_list.l_recs[0].e_cpos = 0;
3385 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
3386 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
3387
3388 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
3389
Tao Ma85db90e2008-11-12 08:27:01 +08003390 ocfs2_journal_dirty(handle, xb_bh);
Tao Ma01225592008-08-18 17:38:53 +08003391
Tao Ma85db90e2008-11-12 08:27:01 +08003392out:
Tao Ma01225592008-08-18 17:38:53 +08003393 up_write(&oi->ip_alloc_sem);
3394
Tao Ma01225592008-08-18 17:38:53 +08003395 return ret;
3396}
3397
3398static int cmp_xe_offset(const void *a, const void *b)
3399{
3400 const struct ocfs2_xattr_entry *l = a, *r = b;
3401 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
3402 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
3403
3404 if (l_name_offset < r_name_offset)
3405 return 1;
3406 if (l_name_offset > r_name_offset)
3407 return -1;
3408 return 0;
3409}
3410
3411/*
3412 * defrag a xattr bucket if we find that the bucket has some
3413 * holes beteen name/value pairs.
3414 * We will move all the name/value pairs to the end of the bucket
3415 * so that we can spare some space for insertion.
3416 */
3417static int ocfs2_defrag_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08003418 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08003419 struct ocfs2_xattr_bucket *bucket)
3420{
3421 int ret, i;
3422 size_t end, offset, len, value_len;
3423 struct ocfs2_xattr_header *xh;
3424 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07003425 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08003426 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08003427 size_t blocksize = inode->i_sb->s_blocksize;
Tao Ma01225592008-08-18 17:38:53 +08003428 struct ocfs2_xattr_entry *xe;
Tao Ma01225592008-08-18 17:38:53 +08003429
3430 /*
3431 * In order to make the operation more efficient and generic,
3432 * we copy all the blocks into a contiguous memory and do the
3433 * defragment there, so if anything is error, we will not touch
3434 * the real block.
3435 */
3436 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
3437 if (!bucket_buf) {
3438 ret = -EIO;
3439 goto out;
3440 }
3441
Joel Becker161d6f32008-10-27 15:25:18 -07003442 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003443 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3444 memcpy(buf, bucket_block(bucket, i), blocksize);
Joel Becker161d6f32008-10-27 15:25:18 -07003445
Tao Ma1c32a2f2008-11-06 08:10:47 +08003446 ret = ocfs2_xattr_bucket_journal_access(handle, bucket,
Joel Becker161d6f32008-10-27 15:25:18 -07003447 OCFS2_JOURNAL_ACCESS_WRITE);
3448 if (ret < 0) {
3449 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08003450 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003451 }
3452
3453 xh = (struct ocfs2_xattr_header *)bucket_buf;
3454 entries = (char *)xh->xh_entries;
3455 xh_free_start = le16_to_cpu(xh->xh_free_start);
3456
3457 mlog(0, "adjust xattr bucket in %llu, count = %u, "
3458 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003459 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
3460 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08003461
3462 /*
3463 * sort all the entries by their offset.
3464 * the largest will be the first, so that we can
3465 * move them to the end one by one.
3466 */
3467 sort(entries, le16_to_cpu(xh->xh_count),
3468 sizeof(struct ocfs2_xattr_entry),
3469 cmp_xe_offset, swap_xe);
3470
3471 /* Move all name/values to the end of the bucket. */
3472 xe = xh->xh_entries;
3473 end = OCFS2_XATTR_BUCKET_SIZE;
3474 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3475 offset = le16_to_cpu(xe->xe_name_offset);
3476 if (ocfs2_xattr_is_local(xe))
3477 value_len = OCFS2_XATTR_SIZE(
3478 le64_to_cpu(xe->xe_value_size));
3479 else
3480 value_len = OCFS2_XATTR_ROOT_SIZE;
3481 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3482
3483 /*
3484 * We must make sure that the name/value pair
3485 * exist in the same block. So adjust end to
3486 * the previous block end if needed.
3487 */
3488 if (((end - len) / blocksize !=
3489 (end - 1) / blocksize))
3490 end = end - end % blocksize;
3491
3492 if (end > offset + len) {
3493 memmove(bucket_buf + end - len,
3494 bucket_buf + offset, len);
3495 xe->xe_name_offset = cpu_to_le16(end - len);
3496 }
3497
3498 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
3499 "bucket %llu\n", (unsigned long long)blkno);
3500
3501 end -= len;
3502 }
3503
3504 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
3505 "bucket %llu\n", (unsigned long long)blkno);
3506
3507 if (xh_free_start == end)
Tao Ma85db90e2008-11-12 08:27:01 +08003508 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003509
3510 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3511 xh->xh_free_start = cpu_to_le16(end);
3512
3513 /* sort the entries by their name_hash. */
3514 sort(entries, le16_to_cpu(xh->xh_count),
3515 sizeof(struct ocfs2_xattr_entry),
3516 cmp_xe, swap_xe);
3517
3518 buf = bucket_buf;
Tao Ma1c32a2f2008-11-06 08:10:47 +08003519 for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize)
3520 memcpy(bucket_block(bucket, i), buf, blocksize);
3521 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08003522
Tao Ma01225592008-08-18 17:38:53 +08003523out:
Tao Ma01225592008-08-18 17:38:53 +08003524 kfree(bucket_buf);
3525 return ret;
3526}
3527
3528/*
3529 * Move half nums of the xattr bucket in the previous cluster to this new
3530 * cluster. We only touch the last cluster of the previous extend record.
3531 *
3532 * first_bh is the first buffer_head of a series of bucket in the same
3533 * extent rec and header_bh is the header of one bucket in this cluster.
3534 * They will be updated if we move the data header_bh contains to the new
3535 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3536 */
3537static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3538 handle_t *handle,
3539 struct buffer_head **first_bh,
3540 struct buffer_head **header_bh,
3541 u64 new_blkno,
3542 u64 prev_blkno,
3543 u32 num_clusters,
3544 u32 *first_hash)
3545{
3546 int i, ret, credits;
3547 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3548 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3549 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3550 int blocksize = inode->i_sb->s_blocksize;
3551 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3552 struct ocfs2_xattr_header *new_xh;
3553 struct ocfs2_xattr_header *xh =
3554 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3555
3556 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3557 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3558
3559 prev_bh = *first_bh;
3560 get_bh(prev_bh);
3561 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3562
3563 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3564
3565 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003566 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003567
3568 /*
3569 * We need to update the 1st half of the new cluster and
3570 * 1 more for the update of the 1st bucket of the previous
3571 * extent record.
3572 */
Tao Ma85db90e2008-11-12 08:27:01 +08003573 credits = bpc / 2 + 1 + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003574 ret = ocfs2_extend_trans(handle, credits);
3575 if (ret) {
3576 mlog_errno(ret);
3577 goto out;
3578 }
3579
3580 ret = ocfs2_journal_access(handle, inode, prev_bh,
3581 OCFS2_JOURNAL_ACCESS_WRITE);
3582 if (ret) {
3583 mlog_errno(ret);
3584 goto out;
3585 }
3586
3587 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3588 old_bh = new_bh = NULL;
3589 new_bh = sb_getblk(inode->i_sb, new_blkno);
3590 if (!new_bh) {
3591 ret = -EIO;
3592 mlog_errno(ret);
3593 goto out;
3594 }
3595
3596 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3597
3598 ret = ocfs2_journal_access(handle, inode, new_bh,
3599 OCFS2_JOURNAL_ACCESS_CREATE);
3600 if (ret < 0) {
3601 mlog_errno(ret);
3602 brelse(new_bh);
3603 goto out;
3604 }
3605
Joel Becker970e4932008-11-13 14:49:19 -08003606 ret = ocfs2_read_block(inode, prev_blkno, &old_bh, NULL);
Tao Ma01225592008-08-18 17:38:53 +08003607 if (ret < 0) {
3608 mlog_errno(ret);
3609 brelse(new_bh);
3610 goto out;
3611 }
3612
3613 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3614
3615 if (i == 0) {
3616 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3617 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3618
3619 if (first_hash)
3620 *first_hash = le32_to_cpu(
3621 new_xh->xh_entries[0].xe_name_hash);
3622 new_first_bh = new_bh;
3623 get_bh(new_first_bh);
3624 }
3625
3626 ocfs2_journal_dirty(handle, new_bh);
3627
3628 if (*header_bh == old_bh) {
3629 brelse(*header_bh);
3630 *header_bh = new_bh;
3631 get_bh(*header_bh);
3632
3633 brelse(*first_bh);
3634 *first_bh = new_first_bh;
3635 get_bh(*first_bh);
3636 }
3637 brelse(new_bh);
3638 brelse(old_bh);
3639 }
3640
3641 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3642
3643 ocfs2_journal_dirty(handle, prev_bh);
3644out:
3645 brelse(prev_bh);
3646 brelse(new_first_bh);
3647 return ret;
3648}
3649
Tao Ma01225592008-08-18 17:38:53 +08003650/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003651 * Find the suitable pos when we divide a bucket into 2.
3652 * We have to make sure the xattrs with the same hash value exist
3653 * in the same bucket.
3654 *
3655 * If this ocfs2_xattr_header covers more than one hash value, find a
3656 * place where the hash value changes. Try to find the most even split.
3657 * The most common case is that all entries have different hash values,
3658 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003659 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003660static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3661{
3662 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3663 int count = le16_to_cpu(xh->xh_count);
3664 int delta, middle = count / 2;
3665
3666 /*
3667 * We start at the middle. Each step gets farther away in both
3668 * directions. We therefore hit the change in hash value
3669 * nearest to the middle. Note that this loop does not execute for
3670 * count < 2.
3671 */
3672 for (delta = 0; delta < middle; delta++) {
3673 /* Let's check delta earlier than middle */
3674 if (cmp_xe(&entries[middle - delta - 1],
3675 &entries[middle - delta]))
3676 return middle - delta;
3677
3678 /* For even counts, don't walk off the end */
3679 if ((middle + delta + 1) == count)
3680 continue;
3681
3682 /* Now try delta past middle */
3683 if (cmp_xe(&entries[middle + delta],
3684 &entries[middle + delta + 1]))
3685 return middle + delta + 1;
3686 }
3687
3688 /* Every entry had the same hash */
3689 return count;
3690}
3691
3692/*
3693 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3694 * first_hash will record the 1st hash of the new bucket.
3695 *
3696 * Normally half of the xattrs will be moved. But we have to make
3697 * sure that the xattrs with the same hash value are stored in the
3698 * same bucket. If all the xattrs in this bucket have the same hash
3699 * value, the new bucket will be initialized as an empty one and the
3700 * first_hash will be initialized as (hash_value+1).
3701 */
3702static int ocfs2_divide_xattr_bucket(struct inode *inode,
3703 handle_t *handle,
3704 u64 blk,
3705 u64 new_blk,
3706 u32 *first_hash,
3707 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003708{
3709 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003710 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003711 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003712 struct ocfs2_xattr_header *xh;
3713 struct ocfs2_xattr_entry *xe;
3714 int blocksize = inode->i_sb->s_blocksize;
3715
Tao Ma80bcaf32008-10-27 06:06:24 +08003716 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003717 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003718
Joel Beckerba937122008-10-24 19:13:20 -07003719 s_bucket = ocfs2_xattr_bucket_new(inode);
3720 t_bucket = ocfs2_xattr_bucket_new(inode);
3721 if (!s_bucket || !t_bucket) {
3722 ret = -ENOMEM;
3723 mlog_errno(ret);
3724 goto out;
3725 }
Tao Ma01225592008-08-18 17:38:53 +08003726
Joel Beckerba937122008-10-24 19:13:20 -07003727 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003728 if (ret) {
3729 mlog_errno(ret);
3730 goto out;
3731 }
3732
Joel Beckerba937122008-10-24 19:13:20 -07003733 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003734 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003735 if (ret) {
3736 mlog_errno(ret);
3737 goto out;
3738 }
3739
Joel Becker784b8162008-10-24 17:33:40 -07003740 /*
3741 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3742 * there's no need to read it.
3743 */
Joel Beckerba937122008-10-24 19:13:20 -07003744 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003745 if (ret) {
3746 mlog_errno(ret);
3747 goto out;
3748 }
3749
Joel Becker2b656c12008-11-25 19:00:15 -08003750 /*
3751 * Hey, if we're overwriting t_bucket, what difference does
3752 * ACCESS_CREATE vs ACCESS_WRITE make? See the comment in the
3753 * same part of ocfs2_cp_xattr_bucket().
3754 */
Joel Beckerba937122008-10-24 19:13:20 -07003755 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003756 new_bucket_head ?
3757 OCFS2_JOURNAL_ACCESS_CREATE :
3758 OCFS2_JOURNAL_ACCESS_WRITE);
3759 if (ret) {
3760 mlog_errno(ret);
3761 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003762 }
3763
Joel Beckerba937122008-10-24 19:13:20 -07003764 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003765 count = le16_to_cpu(xh->xh_count);
3766 start = ocfs2_xattr_find_divide_pos(xh);
3767
3768 if (start == count) {
3769 xe = &xh->xh_entries[start-1];
3770
3771 /*
3772 * initialized a new empty bucket here.
3773 * The hash value is set as one larger than
3774 * that of the last entry in the previous bucket.
3775 */
Joel Beckerba937122008-10-24 19:13:20 -07003776 for (i = 0; i < t_bucket->bu_blocks; i++)
3777 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003778
Joel Beckerba937122008-10-24 19:13:20 -07003779 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003780 xh->xh_free_start = cpu_to_le16(blocksize);
3781 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3782 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3783
3784 goto set_num_buckets;
3785 }
3786
Tao Ma01225592008-08-18 17:38:53 +08003787 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003788 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003789
3790 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003791 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003792
3793 /*
3794 * Calculate the total name/value len and xh_free_start for
3795 * the old bucket first.
3796 */
3797 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3798 name_value_len = 0;
3799 for (i = 0; i < start; i++) {
3800 xe = &xh->xh_entries[i];
3801 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3802 if (ocfs2_xattr_is_local(xe))
3803 xe_len +=
3804 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3805 else
3806 xe_len += OCFS2_XATTR_ROOT_SIZE;
3807 name_value_len += xe_len;
3808 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3809 name_offset = le16_to_cpu(xe->xe_name_offset);
3810 }
3811
3812 /*
3813 * Now begin the modification to the new bucket.
3814 *
3815 * In the new bucket, We just move the xattr entry to the beginning
3816 * and don't touch the name/value. So there will be some holes in the
3817 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3818 * called.
3819 */
3820 xe = &xh->xh_entries[start];
3821 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3822 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003823 (int)((char *)xe - (char *)xh),
3824 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003825 memmove((char *)xh->xh_entries, (char *)xe, len);
3826 xe = &xh->xh_entries[count - start];
3827 len = sizeof(struct ocfs2_xattr_entry) * start;
3828 memset((char *)xe, 0, len);
3829
3830 le16_add_cpu(&xh->xh_count, -start);
3831 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3832
3833 /* Calculate xh_free_start for the new bucket. */
3834 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3835 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3836 xe = &xh->xh_entries[i];
3837 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3838 if (ocfs2_xattr_is_local(xe))
3839 xe_len +=
3840 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3841 else
3842 xe_len += OCFS2_XATTR_ROOT_SIZE;
3843 if (le16_to_cpu(xe->xe_name_offset) <
3844 le16_to_cpu(xh->xh_free_start))
3845 xh->xh_free_start = xe->xe_name_offset;
3846 }
3847
Tao Ma80bcaf32008-10-27 06:06:24 +08003848set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003849 /* set xh->xh_num_buckets for the new xh. */
3850 if (new_bucket_head)
3851 xh->xh_num_buckets = cpu_to_le16(1);
3852 else
3853 xh->xh_num_buckets = 0;
3854
Joel Beckerba937122008-10-24 19:13:20 -07003855 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003856
3857 /* store the first_hash of the new bucket. */
3858 if (first_hash)
3859 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3860
3861 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003862 * Now only update the 1st block of the old bucket. If we
3863 * just added a new empty bucket, there is no need to modify
3864 * it.
Tao Ma01225592008-08-18 17:38:53 +08003865 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003866 if (start == count)
3867 goto out;
3868
Joel Beckerba937122008-10-24 19:13:20 -07003869 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003870 memset(&xh->xh_entries[start], 0,
3871 sizeof(struct ocfs2_xattr_entry) * (count - start));
3872 xh->xh_count = cpu_to_le16(start);
3873 xh->xh_free_start = cpu_to_le16(name_offset);
3874 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3875
Joel Beckerba937122008-10-24 19:13:20 -07003876 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003877
3878out:
Joel Beckerba937122008-10-24 19:13:20 -07003879 ocfs2_xattr_bucket_free(s_bucket);
3880 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003881
3882 return ret;
3883}
3884
3885/*
3886 * Copy xattr from one bucket to another bucket.
3887 *
3888 * The caller must make sure that the journal transaction
3889 * has enough space for journaling.
3890 */
3891static int ocfs2_cp_xattr_bucket(struct inode *inode,
3892 handle_t *handle,
3893 u64 s_blkno,
3894 u64 t_blkno,
3895 int t_is_new)
3896{
Joel Becker4980c6d2008-10-24 18:54:43 -07003897 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003898 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003899
3900 BUG_ON(s_blkno == t_blkno);
3901
3902 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003903 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3904 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003905
Joel Beckerba937122008-10-24 19:13:20 -07003906 s_bucket = ocfs2_xattr_bucket_new(inode);
3907 t_bucket = ocfs2_xattr_bucket_new(inode);
3908 if (!s_bucket || !t_bucket) {
3909 ret = -ENOMEM;
3910 mlog_errno(ret);
3911 goto out;
3912 }
Joel Becker92de1092008-11-25 17:06:40 -08003913
Joel Beckerba937122008-10-24 19:13:20 -07003914 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003915 if (ret)
3916 goto out;
3917
Joel Becker784b8162008-10-24 17:33:40 -07003918 /*
3919 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3920 * there's no need to read it.
3921 */
Joel Beckerba937122008-10-24 19:13:20 -07003922 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003923 if (ret)
3924 goto out;
3925
Joel Becker2b656c12008-11-25 19:00:15 -08003926 /*
3927 * Hey, if we're overwriting t_bucket, what difference does
3928 * ACCESS_CREATE vs ACCESS_WRITE make? Well, if we allocated a new
3929 * cluster to fill, we came here from ocfs2_cp_xattr_cluster(), and
3930 * it is really new - ACCESS_CREATE is required. But we also
3931 * might have moved data out of t_bucket before extending back
3932 * into it. ocfs2_add_new_xattr_bucket() can do this - its call
3933 * to ocfs2_add_new_xattr_cluster() may have created a new extent
3934 * and copied out the end of the old extent. Then it re-extends
3935 * the old extent back to create space for new xattrs. That's
3936 * how we get here, and the bucket isn't really new.
3937 */
Joel Beckerba937122008-10-24 19:13:20 -07003938 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003939 t_is_new ?
3940 OCFS2_JOURNAL_ACCESS_CREATE :
3941 OCFS2_JOURNAL_ACCESS_WRITE);
3942 if (ret)
3943 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003944
Joel Beckerba937122008-10-24 19:13:20 -07003945 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3946 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003947
3948out:
Joel Beckerba937122008-10-24 19:13:20 -07003949 ocfs2_xattr_bucket_free(t_bucket);
3950 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003951
3952 return ret;
3953}
3954
3955/*
Joel Becker15d60922008-11-25 18:36:42 -08003956 * src_blk points to the last cluster of an existing extent. to_blk
3957 * points to a newly allocated extent. We copy the cluster over to the
3958 * new extent, initializing its xh_num_buckets. The old extent's
3959 * xh_num_buckets shrinks by the same amount.
Tao Ma01225592008-08-18 17:38:53 +08003960 */
3961static int ocfs2_cp_xattr_cluster(struct inode *inode,
3962 handle_t *handle,
3963 struct buffer_head *first_bh,
3964 u64 src_blk,
3965 u64 to_blk,
3966 u32 *first_hash)
3967{
3968 int i, ret, credits;
3969 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3970 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
Joel Becker15d60922008-11-25 18:36:42 -08003971 int blks_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08003972 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
Joel Becker15d60922008-11-25 18:36:42 -08003973 struct ocfs2_xattr_bucket *old_first, *new_first;
Tao Ma01225592008-08-18 17:38:53 +08003974
Mark Fashehde29c082008-10-29 14:45:30 -07003975 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3976 (unsigned long long)src_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003977
Joel Becker15d60922008-11-25 18:36:42 -08003978 /* The first bucket of the original extent */
3979 old_first = ocfs2_xattr_bucket_new(inode);
3980 /* The first bucket of the new extent */
3981 new_first = ocfs2_xattr_bucket_new(inode);
3982 if (!old_first || !new_first) {
3983 ret = -ENOMEM;
3984 mlog_errno(ret);
3985 goto out;
3986 }
3987
3988 ret = ocfs2_read_xattr_bucket(old_first, first_bh->b_blocknr);
3989 if (ret) {
3990 mlog_errno(ret);
3991 goto out;
3992 }
3993
Tao Ma01225592008-08-18 17:38:53 +08003994 /*
Joel Becker15d60922008-11-25 18:36:42 -08003995 * We need to update the first bucket of the old extent and the
3996 * entire first cluster of the new extent.
Tao Ma01225592008-08-18 17:38:53 +08003997 */
Joel Becker15d60922008-11-25 18:36:42 -08003998 credits = blks_per_bucket + bpc + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08003999 ret = ocfs2_extend_trans(handle, credits);
4000 if (ret) {
4001 mlog_errno(ret);
4002 goto out;
4003 }
4004
Joel Becker15d60922008-11-25 18:36:42 -08004005 ret = ocfs2_xattr_bucket_journal_access(handle, old_first,
4006 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004007 if (ret) {
4008 mlog_errno(ret);
4009 goto out;
4010 }
4011
4012 for (i = 0; i < num_buckets; i++) {
4013 ret = ocfs2_cp_xattr_bucket(inode, handle,
Joel Becker15d60922008-11-25 18:36:42 -08004014 src_blk + (i * blks_per_bucket),
4015 to_blk + (i * blks_per_bucket),
4016 1);
Tao Ma01225592008-08-18 17:38:53 +08004017 if (ret) {
4018 mlog_errno(ret);
4019 goto out;
4020 }
Tao Ma01225592008-08-18 17:38:53 +08004021 }
4022
Joel Becker15d60922008-11-25 18:36:42 -08004023 /*
4024 * Get the new bucket ready before we dirty anything
4025 * (This actually shouldn't fail, because we already dirtied
4026 * it once in ocfs2_cp_xattr_bucket()).
4027 */
4028 ret = ocfs2_read_xattr_bucket(new_first, to_blk);
4029 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004030 mlog_errno(ret);
4031 goto out;
4032 }
Joel Becker15d60922008-11-25 18:36:42 -08004033 ret = ocfs2_xattr_bucket_journal_access(handle, new_first,
4034 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004035 if (ret) {
4036 mlog_errno(ret);
4037 goto out;
4038 }
4039
Joel Becker15d60922008-11-25 18:36:42 -08004040 /* Now update the headers */
4041 le16_add_cpu(&bucket_xh(old_first)->xh_num_buckets, -num_buckets);
4042 ocfs2_xattr_bucket_journal_dirty(handle, old_first);
Tao Ma01225592008-08-18 17:38:53 +08004043
Joel Becker15d60922008-11-25 18:36:42 -08004044 bucket_xh(new_first)->xh_num_buckets = cpu_to_le16(num_buckets);
4045 ocfs2_xattr_bucket_journal_dirty(handle, new_first);
Tao Ma01225592008-08-18 17:38:53 +08004046
4047 if (first_hash)
Joel Becker15d60922008-11-25 18:36:42 -08004048 *first_hash = le32_to_cpu(bucket_xh(new_first)->xh_entries[0].xe_name_hash);
4049
Tao Ma01225592008-08-18 17:38:53 +08004050out:
Joel Becker15d60922008-11-25 18:36:42 -08004051 ocfs2_xattr_bucket_free(new_first);
4052 ocfs2_xattr_bucket_free(old_first);
Tao Ma01225592008-08-18 17:38:53 +08004053 return ret;
4054}
4055
4056/*
Tao Ma80bcaf32008-10-27 06:06:24 +08004057 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08004058 * This function should only be called when bucket size == cluster size.
4059 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
4060 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004061static int ocfs2_divide_xattr_cluster(struct inode *inode,
4062 handle_t *handle,
4063 u64 prev_blk,
4064 u64 new_blk,
4065 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08004066{
4067 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma85db90e2008-11-12 08:27:01 +08004068 int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits;
Tao Ma01225592008-08-18 17:38:53 +08004069
4070 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
4071
4072 ret = ocfs2_extend_trans(handle, credits);
4073 if (ret) {
4074 mlog_errno(ret);
4075 return ret;
4076 }
4077
4078 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08004079 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
4080 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08004081}
4082
4083/*
4084 * Move some xattrs from the old cluster to the new one since they are not
4085 * contiguous in ocfs2 xattr tree.
4086 *
4087 * new_blk starts a new separate cluster, and we will move some xattrs from
4088 * prev_blk to it. v_start will be set as the first name hash value in this
4089 * new cluster so that it can be used as e_cpos during tree insertion and
4090 * don't collide with our original b-tree operations. first_bh and header_bh
4091 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
4092 * to extend the insert bucket.
4093 *
4094 * The problem is how much xattr should we move to the new one and when should
4095 * we update first_bh and header_bh?
4096 * 1. If cluster size > bucket size, that means the previous cluster has more
4097 * than 1 bucket, so just move half nums of bucket into the new cluster and
4098 * update the first_bh and header_bh if the insert bucket has been moved
4099 * to the new cluster.
4100 * 2. If cluster_size == bucket_size:
4101 * a) If the previous extent rec has more than one cluster and the insert
4102 * place isn't in the last cluster, copy the entire last cluster to the
4103 * new one. This time, we don't need to upate the first_bh and header_bh
4104 * since they will not be moved into the new cluster.
4105 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
4106 * the new one. And we set the extend flag to zero if the insert place is
4107 * moved into the new allocated cluster since no extend is needed.
4108 */
4109static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
4110 handle_t *handle,
4111 struct buffer_head **first_bh,
4112 struct buffer_head **header_bh,
4113 u64 new_blk,
4114 u64 prev_blk,
4115 u32 prev_clusters,
4116 u32 *v_start,
4117 int *extend)
4118{
4119 int ret = 0;
4120 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4121
4122 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004123 (unsigned long long)prev_blk, prev_clusters,
4124 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08004125
4126 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
4127 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
4128 handle,
4129 first_bh,
4130 header_bh,
4131 new_blk,
4132 prev_blk,
4133 prev_clusters,
4134 v_start);
4135 else {
4136 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
4137
4138 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
4139 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
4140 last_blk, new_blk,
4141 v_start);
4142 else {
Tao Ma80bcaf32008-10-27 06:06:24 +08004143 ret = ocfs2_divide_xattr_cluster(inode, handle,
4144 last_blk, new_blk,
4145 v_start);
Tao Ma01225592008-08-18 17:38:53 +08004146
4147 if ((*header_bh)->b_blocknr == last_blk && extend)
4148 *extend = 0;
4149 }
4150 }
4151
4152 return ret;
4153}
4154
4155/*
4156 * Add a new cluster for xattr storage.
4157 *
4158 * If the new cluster is contiguous with the previous one, it will be
4159 * appended to the same extent record, and num_clusters will be updated.
4160 * If not, we will insert a new extent for it and move some xattrs in
4161 * the last cluster into the new allocated one.
4162 * We also need to limit the maximum size of a btree leaf, otherwise we'll
4163 * lose the benefits of hashing because we'll have to search large leaves.
4164 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
4165 * if it's bigger).
4166 *
4167 * first_bh is the first block of the previous extent rec and header_bh
4168 * indicates the bucket we will insert the new xattrs. They will be updated
4169 * when the header_bh is moved into the new cluster.
4170 */
4171static int ocfs2_add_new_xattr_cluster(struct inode *inode,
4172 struct buffer_head *root_bh,
4173 struct buffer_head **first_bh,
4174 struct buffer_head **header_bh,
4175 u32 *num_clusters,
4176 u32 prev_cpos,
4177 u64 prev_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08004178 int *extend,
4179 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004180{
Tao Ma85db90e2008-11-12 08:27:01 +08004181 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004182 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
4183 u32 prev_clusters = *num_clusters;
4184 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
4185 u64 block;
Tao Ma85db90e2008-11-12 08:27:01 +08004186 handle_t *handle = ctxt->handle;
Tao Ma01225592008-08-18 17:38:53 +08004187 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004188 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08004189
4190 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
4191 "previous xattr blkno = %llu\n",
4192 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehde29c082008-10-29 14:45:30 -07004193 prev_cpos, (unsigned long long)prev_blkno);
Tao Ma01225592008-08-18 17:38:53 +08004194
Joel Becker8d6220d2008-08-22 12:46:09 -07004195 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004196
Tao Ma01225592008-08-18 17:38:53 +08004197 ret = ocfs2_journal_access(handle, inode, root_bh,
4198 OCFS2_JOURNAL_ACCESS_WRITE);
4199 if (ret < 0) {
4200 mlog_errno(ret);
4201 goto leave;
4202 }
4203
Tao Ma78f30c32008-11-12 08:27:00 +08004204 ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1,
Tao Ma01225592008-08-18 17:38:53 +08004205 clusters_to_add, &bit_off, &num_bits);
4206 if (ret < 0) {
4207 if (ret != -ENOSPC)
4208 mlog_errno(ret);
4209 goto leave;
4210 }
4211
4212 BUG_ON(num_bits > clusters_to_add);
4213
4214 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4215 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
4216 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
4217
4218 if (prev_blkno + prev_clusters * bpc == block &&
4219 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
4220 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
4221 /*
4222 * If this cluster is contiguous with the old one and
4223 * adding this new cluster, we don't surpass the limit of
4224 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
4225 * initialized and used like other buckets in the previous
4226 * cluster.
4227 * So add it as a contiguous one. The caller will handle
4228 * its init process.
4229 */
4230 v_start = prev_cpos + prev_clusters;
4231 *num_clusters = prev_clusters + num_bits;
4232 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
4233 num_bits);
4234 } else {
4235 ret = ocfs2_adjust_xattr_cross_cluster(inode,
4236 handle,
4237 first_bh,
4238 header_bh,
4239 block,
4240 prev_blkno,
4241 prev_clusters,
4242 &v_start,
4243 extend);
4244 if (ret) {
4245 mlog_errno(ret);
4246 goto leave;
4247 }
4248 }
4249
4250 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07004251 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004252 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
Tao Ma78f30c32008-11-12 08:27:00 +08004253 num_bits, 0, ctxt->meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004254 if (ret < 0) {
4255 mlog_errno(ret);
4256 goto leave;
4257 }
4258
4259 ret = ocfs2_journal_dirty(handle, root_bh);
Tao Ma85db90e2008-11-12 08:27:01 +08004260 if (ret < 0)
Tao Ma01225592008-08-18 17:38:53 +08004261 mlog_errno(ret);
Tao Ma01225592008-08-18 17:38:53 +08004262
4263leave:
Tao Ma01225592008-08-18 17:38:53 +08004264 return ret;
4265}
4266
4267/*
Joel Becker92de1092008-11-25 17:06:40 -08004268 * We are given an extent. 'first' is the bucket at the very front of
4269 * the extent. The extent has space for an additional bucket past
4270 * bucket_xh(first)->xh_num_buckets. 'target_blkno' is the block number
4271 * of the target bucket. We wish to shift every bucket past the target
4272 * down one, filling in that additional space. When we get back to the
4273 * target, we split the target between itself and the now-empty bucket
4274 * at target+1 (aka, target_blkno + blks_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004275 */
4276static int ocfs2_extend_xattr_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004277 handle_t *handle,
Joel Becker92de1092008-11-25 17:06:40 -08004278 struct ocfs2_xattr_bucket *first,
4279 u64 target_blk,
Tao Ma01225592008-08-18 17:38:53 +08004280 u32 num_clusters)
4281{
4282 int ret, credits;
4283 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4284 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker92de1092008-11-25 17:06:40 -08004285 u64 end_blk;
4286 u16 new_bucket = le16_to_cpu(bucket_xh(first)->xh_num_buckets);
Tao Ma01225592008-08-18 17:38:53 +08004287
4288 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Joel Becker92de1092008-11-25 17:06:40 -08004289 "from %llu, len = %u\n", (unsigned long long)target_blk,
4290 (unsigned long long)bucket_blkno(first), num_clusters);
Tao Ma01225592008-08-18 17:38:53 +08004291
Joel Becker92de1092008-11-25 17:06:40 -08004292 /* The extent must have room for an additional bucket */
4293 BUG_ON(new_bucket >=
4294 (num_clusters * ocfs2_xattr_buckets_per_cluster(osb)));
Tao Ma01225592008-08-18 17:38:53 +08004295
Joel Becker92de1092008-11-25 17:06:40 -08004296 /* end_blk points to the last existing bucket */
4297 end_blk = bucket_blkno(first) + ((new_bucket - 1) * blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004298
4299 /*
Joel Becker92de1092008-11-25 17:06:40 -08004300 * end_blk is the start of the last existing bucket.
4301 * Thus, (end_blk - target_blk) covers the target bucket and
4302 * every bucket after it up to, but not including, the last
4303 * existing bucket. Then we add the last existing bucket, the
4304 * new bucket, and the first bucket (3 * blk_per_bucket).
Tao Ma01225592008-08-18 17:38:53 +08004305 */
Joel Becker92de1092008-11-25 17:06:40 -08004306 credits = (end_blk - target_blk) + (3 * blk_per_bucket) +
Tao Ma85db90e2008-11-12 08:27:01 +08004307 handle->h_buffer_credits;
4308 ret = ocfs2_extend_trans(handle, credits);
4309 if (ret) {
Tao Ma01225592008-08-18 17:38:53 +08004310 mlog_errno(ret);
4311 goto out;
4312 }
4313
Joel Becker92de1092008-11-25 17:06:40 -08004314 ret = ocfs2_xattr_bucket_journal_access(handle, first,
4315 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004316 if (ret) {
4317 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004318 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004319 }
4320
Joel Becker92de1092008-11-25 17:06:40 -08004321 while (end_blk != target_blk) {
Tao Ma01225592008-08-18 17:38:53 +08004322 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
4323 end_blk + blk_per_bucket, 0);
4324 if (ret)
Tao Ma85db90e2008-11-12 08:27:01 +08004325 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004326 end_blk -= blk_per_bucket;
4327 }
4328
Joel Becker92de1092008-11-25 17:06:40 -08004329 /* Move half of the xattr in target_blkno to the next bucket. */
4330 ret = ocfs2_divide_xattr_bucket(inode, handle, target_blk,
4331 target_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08004332
Joel Becker92de1092008-11-25 17:06:40 -08004333 le16_add_cpu(&bucket_xh(first)->xh_num_buckets, 1);
4334 ocfs2_xattr_bucket_journal_dirty(handle, first);
Tao Ma01225592008-08-18 17:38:53 +08004335
Tao Ma01225592008-08-18 17:38:53 +08004336out:
4337 return ret;
4338}
4339
4340/*
4341 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
4342 * xb_bh is the ocfs2_xattr_block.
4343 * We will move all the buckets starting from header_bh to the next place. As
4344 * for this one, half num of its xattrs will be moved to the next one.
4345 *
4346 * We will allocate a new cluster if current cluster is full and adjust
4347 * header_bh and first_bh if the insert place is moved to the new cluster.
4348 */
4349static int ocfs2_add_new_xattr_bucket(struct inode *inode,
4350 struct buffer_head *xb_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08004351 struct buffer_head *header_bh,
4352 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004353{
4354 struct ocfs2_xattr_header *first_xh = NULL;
4355 struct buffer_head *first_bh = NULL;
4356 struct ocfs2_xattr_block *xb =
4357 (struct ocfs2_xattr_block *)xb_bh->b_data;
4358 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
4359 struct ocfs2_extent_list *el = &xb_root->xt_list;
4360 struct ocfs2_xattr_header *xh =
4361 (struct ocfs2_xattr_header *)header_bh->b_data;
4362 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
4363 struct super_block *sb = inode->i_sb;
4364 struct ocfs2_super *osb = OCFS2_SB(sb);
4365 int ret, num_buckets, extend = 1;
4366 u64 p_blkno;
4367 u32 e_cpos, num_clusters;
Joel Becker92de1092008-11-25 17:06:40 -08004368 /* The bucket at the front of the extent */
4369 struct ocfs2_xattr_bucket *first;
Tao Ma01225592008-08-18 17:38:53 +08004370
4371 mlog(0, "Add new xattr bucket starting form %llu\n",
4372 (unsigned long long)header_bh->b_blocknr);
4373
Joel Becker92de1092008-11-25 17:06:40 -08004374 first = ocfs2_xattr_bucket_new(inode);
4375 if (!first) {
4376 ret = -ENOMEM;
4377 mlog_errno(ret);
4378 goto out;
4379 }
4380
Tao Ma01225592008-08-18 17:38:53 +08004381 /*
4382 * Add refrence for header_bh here because it may be
4383 * changed in ocfs2_add_new_xattr_cluster and we need
4384 * to free it in the end.
4385 */
4386 get_bh(header_bh);
4387
4388 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
4389 &num_clusters, el);
4390 if (ret) {
4391 mlog_errno(ret);
4392 goto out;
4393 }
4394
Joel Becker970e4932008-11-13 14:49:19 -08004395 ret = ocfs2_read_block(inode, p_blkno, &first_bh, NULL);
Tao Ma01225592008-08-18 17:38:53 +08004396 if (ret) {
4397 mlog_errno(ret);
4398 goto out;
4399 }
4400
4401 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
4402 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4403
4404 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
4405 ret = ocfs2_add_new_xattr_cluster(inode,
4406 xb_bh,
4407 &first_bh,
4408 &header_bh,
4409 &num_clusters,
4410 e_cpos,
4411 p_blkno,
Tao Ma78f30c32008-11-12 08:27:00 +08004412 &extend,
4413 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004414 if (ret) {
4415 mlog_errno(ret);
4416 goto out;
4417 }
4418 }
4419
Joel Becker92de1092008-11-25 17:06:40 -08004420 if (extend) {
4421 /* These bucket reads should be cached */
4422 ret = ocfs2_read_xattr_bucket(first, first_bh->b_blocknr);
4423 if (ret) {
4424 mlog_errno(ret);
4425 goto out;
4426 }
Tao Ma01225592008-08-18 17:38:53 +08004427 ret = ocfs2_extend_xattr_bucket(inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004428 ctxt->handle,
Joel Becker92de1092008-11-25 17:06:40 -08004429 first, header_bh->b_blocknr,
Tao Ma01225592008-08-18 17:38:53 +08004430 num_clusters);
Joel Becker92de1092008-11-25 17:06:40 -08004431 if (ret)
4432 mlog_errno(ret);
4433 }
4434
Tao Ma01225592008-08-18 17:38:53 +08004435out:
4436 brelse(first_bh);
4437 brelse(header_bh);
Joel Becker92de1092008-11-25 17:06:40 -08004438 ocfs2_xattr_bucket_free(first);
Tao Ma01225592008-08-18 17:38:53 +08004439 return ret;
4440}
4441
4442static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4443 struct ocfs2_xattr_bucket *bucket,
4444 int offs)
4445{
4446 int block_off = offs >> inode->i_sb->s_blocksize_bits;
4447
4448 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07004449 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08004450}
4451
4452/*
4453 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08004454 *
4455 * Note: "local" indicates the real data's locality. So we can't
4456 * just its bucket locality by its length.
4457 */
4458static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4459 struct ocfs2_xattr_info *xi,
4460 struct ocfs2_xattr_search *xs,
4461 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004462 int local)
Tao Ma01225592008-08-18 17:38:53 +08004463{
4464 struct ocfs2_xattr_entry *last, *xe;
4465 int name_len = strlen(xi->name);
4466 struct ocfs2_xattr_header *xh = xs->header;
4467 u16 count = le16_to_cpu(xh->xh_count), start;
4468 size_t blocksize = inode->i_sb->s_blocksize;
4469 char *val;
4470 size_t offs, size, new_size;
4471
4472 last = &xh->xh_entries[count];
4473 if (!xs->not_found) {
4474 xe = xs->here;
4475 offs = le16_to_cpu(xe->xe_name_offset);
4476 if (ocfs2_xattr_is_local(xe))
4477 size = OCFS2_XATTR_SIZE(name_len) +
4478 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4479 else
4480 size = OCFS2_XATTR_SIZE(name_len) +
4481 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4482
4483 /*
4484 * If the new value will be stored outside, xi->value has been
4485 * initalized as an empty ocfs2_xattr_value_root, and the same
4486 * goes with xi->value_len, so we can set new_size safely here.
4487 * See ocfs2_xattr_set_in_bucket.
4488 */
4489 new_size = OCFS2_XATTR_SIZE(name_len) +
4490 OCFS2_XATTR_SIZE(xi->value_len);
4491
4492 le16_add_cpu(&xh->xh_name_value_len, -size);
4493 if (xi->value) {
4494 if (new_size > size)
4495 goto set_new_name_value;
4496
4497 /* Now replace the old value with new one. */
4498 if (local)
4499 xe->xe_value_size = cpu_to_le64(xi->value_len);
4500 else
4501 xe->xe_value_size = 0;
4502
4503 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004504 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08004505 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4506 size - OCFS2_XATTR_SIZE(name_len));
4507 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4508 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4509 xi->value, xi->value_len);
4510
4511 le16_add_cpu(&xh->xh_name_value_len, new_size);
4512 ocfs2_xattr_set_local(xe, local);
4513 return;
4514 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004515 /*
4516 * Remove the old entry if there is more than one.
4517 * We don't remove the last entry so that we can
4518 * use it to indicate the hash value of the empty
4519 * bucket.
4520 */
Tao Ma01225592008-08-18 17:38:53 +08004521 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004522 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004523 if (xh->xh_count) {
4524 memmove(xe, xe + 1,
4525 (void *)last - (void *)xe);
4526 memset(last, 0,
4527 sizeof(struct ocfs2_xattr_entry));
4528 } else
4529 xh->xh_free_start =
4530 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4531
Tao Ma01225592008-08-18 17:38:53 +08004532 return;
4533 }
4534 } else {
4535 /* find a new entry for insert. */
4536 int low = 0, high = count - 1, tmp;
4537 struct ocfs2_xattr_entry *tmp_xe;
4538
Tao Ma5a095612008-09-19 22:17:41 +08004539 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004540 tmp = (low + high) / 2;
4541 tmp_xe = &xh->xh_entries[tmp];
4542
4543 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4544 low = tmp + 1;
4545 else if (name_hash <
4546 le32_to_cpu(tmp_xe->xe_name_hash))
4547 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004548 else {
4549 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004550 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004551 }
Tao Ma01225592008-08-18 17:38:53 +08004552 }
4553
4554 xe = &xh->xh_entries[low];
4555 if (low != count)
4556 memmove(xe + 1, xe, (void *)last - (void *)xe);
4557
4558 le16_add_cpu(&xh->xh_count, 1);
4559 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4560 xe->xe_name_hash = cpu_to_le32(name_hash);
4561 xe->xe_name_len = name_len;
4562 ocfs2_xattr_set_type(xe, xi->name_index);
4563 }
4564
4565set_new_name_value:
4566 /* Insert the new name+value. */
4567 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4568
4569 /*
4570 * We must make sure that the name/value pair
4571 * exists in the same block.
4572 */
4573 offs = le16_to_cpu(xh->xh_free_start);
4574 start = offs - size;
4575
4576 if (start >> inode->i_sb->s_blocksize_bits !=
4577 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4578 offs = offs - offs % blocksize;
4579 xh->xh_free_start = cpu_to_le16(offs);
4580 }
4581
Joel Beckerba937122008-10-24 19:13:20 -07004582 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004583 xe->xe_name_offset = cpu_to_le16(offs - size);
4584
4585 memset(val, 0, size);
4586 memcpy(val, xi->name, name_len);
4587 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4588
4589 xe->xe_value_size = cpu_to_le64(xi->value_len);
4590 ocfs2_xattr_set_local(xe, local);
4591 xs->here = xe;
4592 le16_add_cpu(&xh->xh_free_start, -size);
4593 le16_add_cpu(&xh->xh_name_value_len, size);
4594
4595 return;
4596}
4597
Tao Ma01225592008-08-18 17:38:53 +08004598/*
4599 * Set the xattr entry in the specified bucket.
4600 * The bucket is indicated by xs->bucket and it should have the enough
4601 * space for the xattr insertion.
4602 */
4603static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004604 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004605 struct ocfs2_xattr_info *xi,
4606 struct ocfs2_xattr_search *xs,
4607 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004608 int local)
Tao Ma01225592008-08-18 17:38:53 +08004609{
Joel Becker1224be02008-10-24 18:47:33 -07004610 int ret;
Joel Becker02dbf382008-10-27 18:07:45 -07004611 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004612
Mark Fashehff1ec202008-08-19 10:54:29 -07004613 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4614 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004615 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004616
Joel Beckerba937122008-10-24 19:13:20 -07004617 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004618 blkno = bucket_blkno(xs->bucket);
4619 ocfs2_xattr_bucket_relse(xs->bucket);
4620 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004621 if (ret) {
4622 mlog_errno(ret);
4623 goto out;
4624 }
4625 }
4626
Joel Beckerba937122008-10-24 19:13:20 -07004627 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004628 OCFS2_JOURNAL_ACCESS_WRITE);
4629 if (ret < 0) {
4630 mlog_errno(ret);
4631 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004632 }
4633
Tao Ma5a095612008-09-19 22:17:41 +08004634 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004635 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004636
Tao Ma01225592008-08-18 17:38:53 +08004637out:
Tao Ma01225592008-08-18 17:38:53 +08004638 return ret;
4639}
4640
Tao Ma01225592008-08-18 17:38:53 +08004641/*
4642 * Truncate the specified xe_off entry in xattr bucket.
4643 * bucket is indicated by header_bh and len is the new length.
4644 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4645 *
4646 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4647 */
4648static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
Joel Becker548b0f22008-11-24 19:32:13 -08004649 struct ocfs2_xattr_bucket *bucket,
Tao Ma01225592008-08-18 17:38:53 +08004650 int xe_off,
Tao Ma78f30c32008-11-12 08:27:00 +08004651 int len,
4652 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004653{
4654 int ret, offset;
4655 u64 value_blk;
4656 struct buffer_head *value_bh = NULL;
4657 struct ocfs2_xattr_value_root *xv;
4658 struct ocfs2_xattr_entry *xe;
Joel Becker548b0f22008-11-24 19:32:13 -08004659 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08004660 size_t blocksize = inode->i_sb->s_blocksize;
4661
4662 xe = &xh->xh_entries[xe_off];
4663
4664 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4665
4666 offset = le16_to_cpu(xe->xe_name_offset) +
4667 OCFS2_XATTR_SIZE(xe->xe_name_len);
4668
4669 value_blk = offset / blocksize;
4670
4671 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4672 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
Tao Ma01225592008-08-18 17:38:53 +08004673
Joel Becker548b0f22008-11-24 19:32:13 -08004674 value_bh = bucket->bu_bhs[value_blk];
4675 BUG_ON(!value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004676
4677 xv = (struct ocfs2_xattr_value_root *)
4678 (value_bh->b_data + offset % blocksize);
4679
Joel Becker548b0f22008-11-24 19:32:13 -08004680 ret = ocfs2_xattr_bucket_journal_access(ctxt->handle, bucket,
4681 OCFS2_JOURNAL_ACCESS_WRITE);
4682 if (ret) {
4683 mlog_errno(ret);
4684 goto out;
4685 }
4686
4687 /*
4688 * From here on out we have to dirty the bucket. The generic
4689 * value calls only modify one of the bucket's bhs, but we need
4690 * to send the bucket at once. So if they error, they *could* have
4691 * modified something. We have to assume they did, and dirty
4692 * the whole bucket. This leaves us in a consistent state.
4693 */
Tao Ma01225592008-08-18 17:38:53 +08004694 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
Joel Becker548b0f22008-11-24 19:32:13 -08004695 xe_off, (unsigned long long)bucket_blkno(bucket), len);
Tao Ma78f30c32008-11-12 08:27:00 +08004696 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004697 if (ret) {
4698 mlog_errno(ret);
Joel Becker548b0f22008-11-24 19:32:13 -08004699 goto out_dirty;
Tao Ma01225592008-08-18 17:38:53 +08004700 }
4701
Joel Becker548b0f22008-11-24 19:32:13 -08004702 xe->xe_value_size = cpu_to_le64(len);
4703
4704out_dirty:
4705 ocfs2_xattr_bucket_journal_dirty(ctxt->handle, bucket);
Tao Ma01225592008-08-18 17:38:53 +08004706
4707out:
Tao Ma01225592008-08-18 17:38:53 +08004708 return ret;
4709}
4710
4711static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
Tao Ma78f30c32008-11-12 08:27:00 +08004712 struct ocfs2_xattr_search *xs,
4713 int len,
4714 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004715{
4716 int ret, offset;
4717 struct ocfs2_xattr_entry *xe = xs->here;
4718 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4719
Joel Beckerba937122008-10-24 19:13:20 -07004720 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004721
4722 offset = xe - xh->xh_entries;
Joel Becker548b0f22008-11-24 19:32:13 -08004723 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08004724 offset, len, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004725 if (ret)
4726 mlog_errno(ret);
4727
4728 return ret;
4729}
4730
4731static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004732 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004733 struct ocfs2_xattr_search *xs,
4734 char *val,
4735 int value_len)
4736{
4737 int offset;
4738 struct ocfs2_xattr_value_root *xv;
4739 struct ocfs2_xattr_entry *xe = xs->here;
4740
4741 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4742
4743 offset = le16_to_cpu(xe->xe_name_offset) +
4744 OCFS2_XATTR_SIZE(xe->xe_name_len);
4745
4746 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4747
Tao Ma85db90e2008-11-12 08:27:01 +08004748 return __ocfs2_xattr_set_value_outside(inode, handle,
4749 xv, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004750}
4751
Tao Ma01225592008-08-18 17:38:53 +08004752static int ocfs2_rm_xattr_cluster(struct inode *inode,
4753 struct buffer_head *root_bh,
4754 u64 blkno,
4755 u32 cpos,
4756 u32 len)
4757{
4758 int ret;
4759 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4760 struct inode *tl_inode = osb->osb_tl_inode;
4761 handle_t *handle;
4762 struct ocfs2_xattr_block *xb =
4763 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004764 struct ocfs2_alloc_context *meta_ac = NULL;
4765 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004766 struct ocfs2_extent_tree et;
4767
Joel Becker8d6220d2008-08-22 12:46:09 -07004768 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004769
4770 ocfs2_init_dealloc_ctxt(&dealloc);
4771
4772 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4773 cpos, len, (unsigned long long)blkno);
4774
4775 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4776
Joel Beckerf99b9b72008-08-20 19:36:33 -07004777 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004778 if (ret) {
4779 mlog_errno(ret);
4780 return ret;
4781 }
4782
4783 mutex_lock(&tl_inode->i_mutex);
4784
4785 if (ocfs2_truncate_log_needs_flush(osb)) {
4786 ret = __ocfs2_flush_truncate_log(osb);
4787 if (ret < 0) {
4788 mlog_errno(ret);
4789 goto out;
4790 }
4791 }
4792
Jan Karaa90714c2008-10-09 19:38:40 +02004793 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Tao Mad3264792008-10-24 07:57:28 +08004794 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004795 ret = -ENOMEM;
4796 mlog_errno(ret);
4797 goto out;
4798 }
4799
4800 ret = ocfs2_journal_access(handle, inode, root_bh,
4801 OCFS2_JOURNAL_ACCESS_WRITE);
4802 if (ret) {
4803 mlog_errno(ret);
4804 goto out_commit;
4805 }
4806
Joel Beckerf99b9b72008-08-20 19:36:33 -07004807 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4808 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004809 if (ret) {
4810 mlog_errno(ret);
4811 goto out_commit;
4812 }
4813
4814 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4815
4816 ret = ocfs2_journal_dirty(handle, root_bh);
4817 if (ret) {
4818 mlog_errno(ret);
4819 goto out_commit;
4820 }
4821
4822 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4823 if (ret)
4824 mlog_errno(ret);
4825
4826out_commit:
4827 ocfs2_commit_trans(osb, handle);
4828out:
4829 ocfs2_schedule_truncate_log_flush(osb, 1);
4830
4831 mutex_unlock(&tl_inode->i_mutex);
4832
4833 if (meta_ac)
4834 ocfs2_free_alloc_context(meta_ac);
4835
4836 ocfs2_run_deallocs(osb, &dealloc);
4837
4838 return ret;
4839}
4840
Tao Ma01225592008-08-18 17:38:53 +08004841static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
Tao Ma85db90e2008-11-12 08:27:01 +08004842 handle_t *handle,
Tao Ma01225592008-08-18 17:38:53 +08004843 struct ocfs2_xattr_search *xs)
4844{
Joel Beckerba937122008-10-24 19:13:20 -07004845 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004846 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4847 le16_to_cpu(xh->xh_count) - 1];
4848 int ret = 0;
4849
Joel Beckerba937122008-10-24 19:13:20 -07004850 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004851 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004852 if (ret) {
4853 mlog_errno(ret);
Tao Ma85db90e2008-11-12 08:27:01 +08004854 return;
Tao Ma01225592008-08-18 17:38:53 +08004855 }
4856
4857 /* Remove the old entry. */
4858 memmove(xs->here, xs->here + 1,
4859 (void *)last - (void *)xs->here);
4860 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4861 le16_add_cpu(&xh->xh_count, -1);
4862
Joel Beckerba937122008-10-24 19:13:20 -07004863 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004864}
4865
4866/*
4867 * Set the xattr name/value in the bucket specified in xs.
4868 *
4869 * As the new value in xi may be stored in the bucket or in an outside cluster,
4870 * we divide the whole process into 3 steps:
4871 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4872 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4873 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4874 * 4. If the clusters for the new outside value can't be allocated, we need
4875 * to free the xattr we allocated in set.
4876 */
4877static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4878 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004879 struct ocfs2_xattr_search *xs,
4880 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004881{
Tao Ma5a095612008-09-19 22:17:41 +08004882 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004883 size_t value_len;
4884 char *val = (char *)xi->value;
4885 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004886 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4887 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004888
4889 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4890 /*
4891 * We need to truncate the xattr storage first.
4892 *
4893 * If both the old and new value are stored to
4894 * outside block, we only need to truncate
4895 * the storage and then set the value outside.
4896 *
4897 * If the new value should be stored within block,
4898 * we should free all the outside block first and
4899 * the modification to the xattr block will be done
4900 * by following steps.
4901 */
4902 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4903 value_len = xi->value_len;
4904 else
4905 value_len = 0;
4906
4907 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004908 value_len,
4909 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08004910 if (ret)
4911 goto out;
4912
4913 if (value_len)
4914 goto set_value_outside;
4915 }
4916
4917 value_len = xi->value_len;
4918 /* So we have to handle the inside block change now. */
4919 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4920 /*
4921 * If the new value will be stored outside of block,
4922 * initalize a new empty value root and insert it first.
4923 */
4924 local = 0;
4925 xi->value = &def_xv;
4926 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4927 }
4928
Tao Ma85db90e2008-11-12 08:27:01 +08004929 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
4930 name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004931 if (ret) {
4932 mlog_errno(ret);
4933 goto out;
4934 }
4935
Tao Ma5a095612008-09-19 22:17:41 +08004936 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4937 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004938
Tao Ma5a095612008-09-19 22:17:41 +08004939 /* allocate the space now for the outside block storage. */
4940 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
Tao Ma78f30c32008-11-12 08:27:00 +08004941 value_len, ctxt);
Tao Ma5a095612008-09-19 22:17:41 +08004942 if (ret) {
4943 mlog_errno(ret);
4944
4945 if (xs->not_found) {
4946 /*
4947 * We can't allocate enough clusters for outside
4948 * storage and we have allocated xattr already,
4949 * so need to remove it.
4950 */
Tao Ma85db90e2008-11-12 08:27:01 +08004951 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
Tao Ma01225592008-08-18 17:38:53 +08004952 }
Tao Ma01225592008-08-18 17:38:53 +08004953 goto out;
4954 }
4955
4956set_value_outside:
Tao Ma85db90e2008-11-12 08:27:01 +08004957 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
4958 xs, val, value_len);
Tao Ma01225592008-08-18 17:38:53 +08004959out:
4960 return ret;
4961}
4962
Tao Ma80bcaf32008-10-27 06:06:24 +08004963/*
4964 * check whether the xattr bucket is filled up with the same hash value.
4965 * If we want to insert the xattr with the same hash, return -ENOSPC.
4966 * If we want to insert a xattr with different hash value, go ahead
4967 * and ocfs2_divide_xattr_bucket will handle this.
4968 */
Tao Ma01225592008-08-18 17:38:53 +08004969static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004970 struct ocfs2_xattr_bucket *bucket,
4971 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004972{
Joel Becker3e632942008-10-24 17:04:49 -07004973 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004974 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4975
4976 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4977 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004978
4979 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4980 xh->xh_entries[0].xe_name_hash) {
4981 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4982 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004983 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004984 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4985 return -ENOSPC;
4986 }
4987
4988 return 0;
4989}
4990
4991static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4992 struct ocfs2_xattr_info *xi,
Tao Ma78f30c32008-11-12 08:27:00 +08004993 struct ocfs2_xattr_search *xs,
4994 struct ocfs2_xattr_set_ctxt *ctxt)
Tao Ma01225592008-08-18 17:38:53 +08004995{
4996 struct ocfs2_xattr_header *xh;
4997 struct ocfs2_xattr_entry *xe;
4998 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07004999 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08005000 size_t value_size = 0, name_len = strlen(xi->name);
5001 size_t blocksize = inode->i_sb->s_blocksize;
5002 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08005003
5004 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5005
5006try_again:
5007 xh = xs->header;
5008 count = le16_to_cpu(xh->xh_count);
5009 xh_free_start = le16_to_cpu(xh->xh_free_start);
5010 header_size = sizeof(struct ocfs2_xattr_header) +
5011 count * sizeof(struct ocfs2_xattr_entry);
5012 max_free = OCFS2_XATTR_BUCKET_SIZE -
5013 le16_to_cpu(xh->xh_name_value_len) - header_size;
5014
5015 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5016 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07005017 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005018 header_size);
5019
5020 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5021 value_size = OCFS2_XATTR_ROOT_SIZE;
5022 else if (xi->value)
5023 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5024
5025 if (xs->not_found)
5026 need = sizeof(struct ocfs2_xattr_entry) +
5027 OCFS2_XATTR_SIZE(name_len) + value_size;
5028 else {
5029 need = value_size + OCFS2_XATTR_SIZE(name_len);
5030
5031 /*
5032 * We only replace the old value if the new length is smaller
5033 * than the old one. Otherwise we will allocate new space in the
5034 * bucket to store it.
5035 */
5036 xe = xs->here;
5037 if (ocfs2_xattr_is_local(xe))
5038 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5039 else
5040 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5041
5042 if (old >= value_size)
5043 need = 0;
5044 }
5045
5046 free = xh_free_start - header_size;
5047 /*
5048 * We need to make sure the new name/value pair
5049 * can exist in the same block.
5050 */
5051 if (xh_free_start % blocksize < need)
5052 free -= xh_free_start % blocksize;
5053
5054 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5055 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5056 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07005057 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08005058 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5059 le16_to_cpu(xh->xh_name_value_len));
5060
Tao Ma976331d2008-11-12 08:26:57 +08005061 if (free < need ||
5062 (xs->not_found &&
5063 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
Tao Ma01225592008-08-18 17:38:53 +08005064 if (need <= max_free &&
5065 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5066 /*
5067 * We can create the space by defragment. Since only the
5068 * name/value will be moved, the xe shouldn't be changed
5069 * in xs.
5070 */
Tao Ma85db90e2008-11-12 08:27:01 +08005071 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5072 xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005073 if (ret) {
5074 mlog_errno(ret);
5075 goto out;
5076 }
5077
5078 xh_free_start = le16_to_cpu(xh->xh_free_start);
5079 free = xh_free_start - header_size;
5080 if (xh_free_start % blocksize < need)
5081 free -= xh_free_start % blocksize;
5082
5083 if (free >= need)
5084 goto xattr_set;
5085
5086 mlog(0, "Can't get enough space for xattr insert by "
5087 "defragment. Need %u bytes, but we have %d, so "
5088 "allocate new bucket for it.\n", need, free);
5089 }
5090
5091 /*
5092 * We have to add new buckets or clusters and one
5093 * allocation should leave us enough space for insert.
5094 */
5095 BUG_ON(allocation);
5096
5097 /*
5098 * We do not allow for overlapping ranges between buckets. And
5099 * the maximum number of collisions we will allow for then is
5100 * one bucket's worth, so check it here whether we need to
5101 * add a new bucket for the insert.
5102 */
Tao Ma80bcaf32008-10-27 06:06:24 +08005103 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07005104 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08005105 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08005106 if (ret) {
5107 mlog_errno(ret);
5108 goto out;
5109 }
5110
5111 ret = ocfs2_add_new_xattr_bucket(inode,
5112 xs->xattr_bh,
Tao Ma78f30c32008-11-12 08:27:00 +08005113 xs->bucket->bu_bhs[0],
5114 ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005115 if (ret) {
5116 mlog_errno(ret);
5117 goto out;
5118 }
5119
Joel Beckerba937122008-10-24 19:13:20 -07005120 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08005121
5122 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5123 xi->name_index,
5124 xi->name, xs);
5125 if (ret && ret != -ENODATA)
5126 goto out;
5127 xs->not_found = ret;
5128 allocation = 1;
5129 goto try_again;
5130 }
5131
5132xattr_set:
Tao Ma78f30c32008-11-12 08:27:00 +08005133 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt);
Tao Ma01225592008-08-18 17:38:53 +08005134out:
5135 mlog_exit(ret);
5136 return ret;
5137}
Tao Maa3944252008-08-18 17:38:54 +08005138
5139static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
5140 struct ocfs2_xattr_bucket *bucket,
5141 void *para)
5142{
5143 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07005144 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08005145 u16 i;
5146 struct ocfs2_xattr_entry *xe;
Tao Ma78f30c32008-11-12 08:27:00 +08005147 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5148 struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,};
Joel Becker548b0f22008-11-24 19:32:13 -08005149 int credits = ocfs2_remove_extent_credits(osb->sb) +
5150 ocfs2_blocks_per_xattr_bucket(inode->i_sb);
5151
Tao Ma78f30c32008-11-12 08:27:00 +08005152
5153 ocfs2_init_dealloc_ctxt(&ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005154
5155 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
5156 xe = &xh->xh_entries[i];
5157 if (ocfs2_xattr_is_local(xe))
5158 continue;
5159
Tao Ma88c3b062008-12-11 08:54:11 +08005160 ctxt.handle = ocfs2_start_trans(osb, credits);
5161 if (IS_ERR(ctxt.handle)) {
5162 ret = PTR_ERR(ctxt.handle);
5163 mlog_errno(ret);
5164 break;
5165 }
5166
Joel Becker548b0f22008-11-24 19:32:13 -08005167 ret = ocfs2_xattr_bucket_value_truncate(inode, bucket,
Tao Ma78f30c32008-11-12 08:27:00 +08005168 i, 0, &ctxt);
Tao Ma88c3b062008-12-11 08:54:11 +08005169
5170 ocfs2_commit_trans(osb, ctxt.handle);
Tao Maa3944252008-08-18 17:38:54 +08005171 if (ret) {
5172 mlog_errno(ret);
5173 break;
5174 }
5175 }
5176
Tao Ma78f30c32008-11-12 08:27:00 +08005177 ocfs2_schedule_truncate_log_flush(osb, 1);
5178 ocfs2_run_deallocs(osb, &ctxt.dealloc);
Tao Maa3944252008-08-18 17:38:54 +08005179 return ret;
5180}
5181
5182static int ocfs2_delete_xattr_index_block(struct inode *inode,
5183 struct buffer_head *xb_bh)
5184{
5185 struct ocfs2_xattr_block *xb =
5186 (struct ocfs2_xattr_block *)xb_bh->b_data;
5187 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
5188 int ret = 0;
5189 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
5190 u64 p_blkno;
5191
5192 if (le16_to_cpu(el->l_next_free_rec) == 0)
5193 return 0;
5194
5195 while (name_hash > 0) {
5196 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
5197 &e_cpos, &num_clusters, el);
5198 if (ret) {
5199 mlog_errno(ret);
5200 goto out;
5201 }
5202
5203 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
5204 ocfs2_delete_xattr_in_bucket,
5205 NULL);
5206 if (ret) {
5207 mlog_errno(ret);
5208 goto out;
5209 }
5210
5211 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
5212 p_blkno, e_cpos, num_clusters);
5213 if (ret) {
5214 mlog_errno(ret);
5215 break;
5216 }
5217
5218 if (e_cpos == 0)
5219 break;
5220
5221 name_hash = e_cpos - 1;
5222 }
5223
5224out:
5225 return ret;
5226}
Mark Fasheh99219ae2008-10-07 14:52:59 -07005227
5228/*
Tiger Yang923f7f32008-11-14 11:16:27 +08005229 * 'security' attributes support
5230 */
5231static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
5232 size_t list_size, const char *name,
5233 size_t name_len)
5234{
5235 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
5236 const size_t total_len = prefix_len + name_len + 1;
5237
5238 if (list && total_len <= list_size) {
5239 memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
5240 memcpy(list + prefix_len, name, name_len);
5241 list[prefix_len + name_len] = '\0';
5242 }
5243 return total_len;
5244}
5245
5246static int ocfs2_xattr_security_get(struct inode *inode, const char *name,
5247 void *buffer, size_t size)
5248{
5249 if (strcmp(name, "") == 0)
5250 return -EINVAL;
5251 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name,
5252 buffer, size);
5253}
5254
5255static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
5256 const void *value, size_t size, int flags)
5257{
5258 if (strcmp(name, "") == 0)
5259 return -EINVAL;
5260
5261 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value,
5262 size, flags);
5263}
5264
Tiger Yang534eadd2008-11-14 11:16:41 +08005265int ocfs2_init_security_get(struct inode *inode,
5266 struct inode *dir,
5267 struct ocfs2_security_xattr_info *si)
5268{
5269 return security_inode_init_security(inode, dir, &si->name, &si->value,
5270 &si->value_len);
5271}
5272
5273int ocfs2_init_security_set(handle_t *handle,
5274 struct inode *inode,
5275 struct buffer_head *di_bh,
5276 struct ocfs2_security_xattr_info *si,
5277 struct ocfs2_alloc_context *xattr_ac,
5278 struct ocfs2_alloc_context *data_ac)
5279{
5280 return ocfs2_xattr_set_handle(handle, inode, di_bh,
5281 OCFS2_XATTR_INDEX_SECURITY,
5282 si->name, si->value, si->value_len, 0,
5283 xattr_ac, data_ac);
5284}
5285
Tiger Yang923f7f32008-11-14 11:16:27 +08005286struct xattr_handler ocfs2_xattr_security_handler = {
5287 .prefix = XATTR_SECURITY_PREFIX,
5288 .list = ocfs2_xattr_security_list,
5289 .get = ocfs2_xattr_security_get,
5290 .set = ocfs2_xattr_security_set,
5291};
5292
5293/*
Mark Fasheh99219ae2008-10-07 14:52:59 -07005294 * 'trusted' attributes support
5295 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005296static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
5297 size_t list_size, const char *name,
5298 size_t name_len)
5299{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005300 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005301 const size_t total_len = prefix_len + name_len + 1;
5302
5303 if (list && total_len <= list_size) {
5304 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
5305 memcpy(list + prefix_len, name, name_len);
5306 list[prefix_len + name_len] = '\0';
5307 }
5308 return total_len;
5309}
5310
5311static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
5312 void *buffer, size_t size)
5313{
5314 if (strcmp(name, "") == 0)
5315 return -EINVAL;
5316 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
5317 buffer, size);
5318}
5319
5320static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
5321 const void *value, size_t size, int flags)
5322{
5323 if (strcmp(name, "") == 0)
5324 return -EINVAL;
5325
5326 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
5327 size, flags);
5328}
5329
5330struct xattr_handler ocfs2_xattr_trusted_handler = {
5331 .prefix = XATTR_TRUSTED_PREFIX,
5332 .list = ocfs2_xattr_trusted_list,
5333 .get = ocfs2_xattr_trusted_get,
5334 .set = ocfs2_xattr_trusted_set,
5335};
5336
Mark Fasheh99219ae2008-10-07 14:52:59 -07005337/*
5338 * 'user' attributes support
5339 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07005340static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
5341 size_t list_size, const char *name,
5342 size_t name_len)
5343{
Tiger Yangceb1eba2008-10-23 16:34:13 +08005344 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07005345 const size_t total_len = prefix_len + name_len + 1;
5346 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5347
5348 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5349 return 0;
5350
5351 if (list && total_len <= list_size) {
5352 memcpy(list, XATTR_USER_PREFIX, prefix_len);
5353 memcpy(list + prefix_len, name, name_len);
5354 list[prefix_len + name_len] = '\0';
5355 }
5356 return total_len;
5357}
5358
5359static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
5360 void *buffer, size_t size)
5361{
5362 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5363
5364 if (strcmp(name, "") == 0)
5365 return -EINVAL;
5366 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5367 return -EOPNOTSUPP;
5368 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
5369 buffer, size);
5370}
5371
5372static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
5373 const void *value, size_t size, int flags)
5374{
5375 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5376
5377 if (strcmp(name, "") == 0)
5378 return -EINVAL;
5379 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
5380 return -EOPNOTSUPP;
5381
5382 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
5383 size, flags);
5384}
5385
5386struct xattr_handler ocfs2_xattr_user_handler = {
5387 .prefix = XATTR_USER_PREFIX,
5388 .list = ocfs2_xattr_user_list,
5389 .get = ocfs2_xattr_user_get,
5390 .set = ocfs2_xattr_user_set,
5391};