]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/patches/suse-2.6.27.25/patches.suse/ocfs2-Enable-xattr-set-in-index-btree.patch
Updated xen patches taken from suse.
[people/pmueller/ipfire-2.x.git] / src / patches / suse-2.6.27.25 / patches.suse / ocfs2-Enable-xattr-set-in-index-btree.patch
1 From: Tao Ma <tao.ma@oracle.com>
2 Subject: [PATCH 14/16] ocfs2: Enable xattr set in index btree
3 Patch-mainline: 2.6.28?
4 References: FATE302067
5
6 Where the previous patches added the ability of list/get xattr in buckets
7 for ocfs2, this patch enables ocfs2 to store large numbers of EAs.
8
9 The original design doc is written by Mark Fasheh, and it can be found in
10 http://oss.oracle.com/osswiki/OCFS2/DesignDocs/IndexedEATrees. I only had to
11 make small modifications to it.
12
13 First, because the bucket size is 4K, a new field named xh_free_start is added
14 in ocfs2_xattr_header to indicate the next valid name/value offset in a bucket.
15 It is used when we store new EA name/value. With this field, we can find the
16 place more quickly and what's more, we don't need to sort the name/value every
17 time to let the last entry indicate the next unused space. This makes the
18 insert operation more efficient for blocksizes smaller than 4k.
19
20 Because of the new xh_free_start, another field named as xh_name_value_len is
21 also added in ocfs2_xattr_header. It records the total length of all the
22 name/values in the bucket. We need this so that we can check it and defragment
23 the bucket if there is not enough contiguous free space.
24
25 An xattr insertion looks like this:
26 1. xattr_index_block_find: find the right bucket by the name_hash, say bucketA.
27 2. check whether there is enough space in bucketA. If yes, insert it directly
28 and modify xh_free_start and xh_name_value_len accordingly. If not, check
29 xh_name_value_len to see whether we can store this by defragment the bucket.
30 If yes, defragment it and go on insertion.
31 3. If defragement doesn't work, check whether there is new empty bucket in
32 the clusters within this extent record. If yes, init the new bucket and move
33 all the buckets after bucketA one by one to the next bucket. Move half of the
34 entries in bucketA to the next bucket and go on insertion.
35 4. If there is no new bucket, grow the extent tree.
36
37 As for xattr deletion, we will delete an xattr bucket when all it's xattrs
38 are removed and move all the buckets after it to the previous one. When all
39 the xattr buckets in an extend record are freed, free this extend records
40 from ocfs2_xattr_tree.
41
42 Signed-off-by: Tao Ma <tao.ma@oracle.com>
43 Signed-off-by: Mark Fasheh <mfasheh@suse.com>
44 ---
45 fs/ocfs2/xattr.c | 2267 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
46 fs/ocfs2/xattr.h | 8 +
47 2 files changed, 2273 insertions(+), 2 deletions(-)
48
49 diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
50 index a5ca066..408553c 100644
51 --- a/fs/ocfs2/xattr.c
52 +++ b/fs/ocfs2/xattr.c
53 @@ -36,6 +36,7 @@
54 #include <linux/mount.h>
55 #include <linux/writeback.h>
56 #include <linux/falloc.h>
57 +#include <linux/sort.h>
58
59 #define MLOG_MASK_PREFIX ML_XATTR
60 #include <cluster/masklog.h>
61 @@ -139,6 +140,13 @@ static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
62 char *buffer,
63 size_t buffer_size);
64
65 +static int ocfs2_xattr_create_index_block(struct inode *inode,
66 + struct ocfs2_xattr_search *xs);
67 +
68 +static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
69 + struct ocfs2_xattr_info *xi,
70 + struct ocfs2_xattr_search *xs);
71 +
72 static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
73 {
74 struct xattr_handler *handler = NULL;
75 @@ -1784,6 +1792,52 @@ cleanup:
76 }
77
78 /*
79 + * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
80 + * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
81 + * re-initialized.
82 + */
83 +static int ocfs2_restore_xattr_block(struct inode *inode,
84 + struct ocfs2_xattr_search *xs)
85 +{
86 + int ret;
87 + handle_t *handle;
88 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
89 + struct ocfs2_xattr_block *xb =
90 + (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
91 + struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
92 + u16 xb_flags = le16_to_cpu(xb->xb_flags);
93 +
94 + BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
95 + le16_to_cpu(el->l_next_free_rec) != 0);
96 +
97 + handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
98 + if (IS_ERR(handle)) {
99 + ret = PTR_ERR(handle);
100 + handle = NULL;
101 + goto out;
102 + }
103 +
104 + ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
105 + OCFS2_JOURNAL_ACCESS_WRITE);
106 + if (ret < 0) {
107 + mlog_errno(ret);
108 + goto out_commit;
109 + }
110 +
111 + memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
112 + offsetof(struct ocfs2_xattr_block, xb_attrs));
113 +
114 + xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
115 +
116 + ocfs2_journal_dirty(handle, xs->xattr_bh);
117 +
118 +out_commit:
119 + ocfs2_commit_trans(osb, handle);
120 +out:
121 + return ret;
122 +}
123 +
124 +/*
125 * ocfs2_xattr_block_set()
126 *
127 * Set, replace or remove an extended attribute into external block.
128 @@ -1878,10 +1932,25 @@ out:
129 ocfs2_free_alloc_context(meta_ac);
130 if (ret < 0)
131 return ret;
132 + } else
133 + xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
134 +
135 + if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
136 + /* Set extended attribute into external block */
137 + ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
138 + if (!ret || ret != -ENOSPC)
139 + goto end;
140 +
141 + ret = ocfs2_xattr_create_index_block(inode, xs);
142 + if (ret)
143 + goto end;
144 }
145
146 - /* Set extended attribute into external block */
147 - ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
148 + ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
149 + if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
150 + ret = ocfs2_restore_xattr_block(inode, xs);
151 +
152 +end:
153
154 return ret;
155 }
156 @@ -1903,6 +1972,7 @@ int ocfs2_xattr_set(struct inode *inode,
157 struct buffer_head *di_bh = NULL;
158 struct ocfs2_dinode *di;
159 int ret;
160 + u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
161
162 struct ocfs2_xattr_info xi = {
163 .name_index = name_index,
164 @@ -2001,6 +2071,8 @@ cleanup:
165 ocfs2_inode_unlock(inode, 1);
166 brelse(di_bh);
167 brelse(xbs.xattr_bh);
168 + for (i = 0; i < blk_per_bucket; i++)
169 + brelse(xbs.bucket.bhs[i]);
170
171 return ret;
172 }
173 @@ -2491,3 +2563,2194 @@ static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
174 out:
175 return ret;
176 }
177 +
178 +static int cmp_xe(const void *a, const void *b)
179 +{
180 + const struct ocfs2_xattr_entry *l = a, *r = b;
181 + u32 l_hash = le32_to_cpu(l->xe_name_hash);
182 + u32 r_hash = le32_to_cpu(r->xe_name_hash);
183 +
184 + if (l_hash > r_hash)
185 + return 1;
186 + if (l_hash < r_hash)
187 + return -1;
188 + return 0;
189 +}
190 +
191 +static void swap_xe(void *a, void *b, int size)
192 +{
193 + struct ocfs2_xattr_entry *l = a, *r = b, tmp;
194 +
195 + tmp = *l;
196 + memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
197 + memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
198 +}
199 +
200 +/*
201 + * When the ocfs2_xattr_block is filled up, new bucket will be created
202 + * and all the xattr entries will be moved to the new bucket.
203 + * Note: we need to sort the entries since they are not saved in order
204 + * in the ocfs2_xattr_block.
205 + */
206 +static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
207 + struct buffer_head *xb_bh,
208 + struct buffer_head *xh_bh,
209 + struct buffer_head *data_bh)
210 +{
211 + int i, blocksize = inode->i_sb->s_blocksize;
212 + u16 offset, size, off_change;
213 + struct ocfs2_xattr_entry *xe;
214 + struct ocfs2_xattr_block *xb =
215 + (struct ocfs2_xattr_block *)xb_bh->b_data;
216 + struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
217 + struct ocfs2_xattr_header *xh =
218 + (struct ocfs2_xattr_header *)xh_bh->b_data;
219 + u16 count = le16_to_cpu(xb_xh->xh_count);
220 + char *target = xh_bh->b_data, *src = xb_bh->b_data;
221 +
222 + mlog(0, "cp xattr from block %llu to bucket %llu\n",
223 + (unsigned long long)xb_bh->b_blocknr,
224 + (unsigned long long)xh_bh->b_blocknr);
225 +
226 + memset(xh_bh->b_data, 0, blocksize);
227 + if (data_bh)
228 + memset(data_bh->b_data, 0, blocksize);
229 + /*
230 + * Since the xe_name_offset is based on ocfs2_xattr_header,
231 + * there is a offset change corresponding to the change of
232 + * ocfs2_xattr_header's position.
233 + */
234 + off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
235 + xe = &xb_xh->xh_entries[count - 1];
236 + offset = le16_to_cpu(xe->xe_name_offset) + off_change;
237 + size = blocksize - offset;
238 +
239 + /* copy all the names and values. */
240 + if (data_bh)
241 + target = data_bh->b_data;
242 + memcpy(target + offset, src + offset, size);
243 +
244 + /* Init new header now. */
245 + xh->xh_count = xb_xh->xh_count;
246 + xh->xh_num_buckets = cpu_to_le16(1);
247 + xh->xh_name_value_len = cpu_to_le16(size);
248 + xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
249 +
250 + /* copy all the entries. */
251 + target = xh_bh->b_data;
252 + offset = offsetof(struct ocfs2_xattr_header, xh_entries);
253 + size = count * sizeof(struct ocfs2_xattr_entry);
254 + memcpy(target + offset, (char *)xb_xh + offset, size);
255 +
256 + /* Change the xe offset for all the xe because of the move. */
257 + off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
258 + offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
259 + for (i = 0; i < count; i++)
260 + le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
261 +
262 + mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
263 + offset, size, off_change);
264 +
265 + sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
266 + cmp_xe, swap_xe);
267 +}
268 +
269 +/*
270 + * After we move xattr from block to index btree, we have to
271 + * update ocfs2_xattr_search to the new xe and base.
272 + *
273 + * When the entry is in xattr block, xattr_bh indicates the storage place.
274 + * While if the entry is in index b-tree, "bucket" indicates the
275 + * real place of the xattr.
276 + */
277 +static int ocfs2_xattr_update_xattr_search(struct inode *inode,
278 + struct ocfs2_xattr_search *xs,
279 + struct buffer_head *old_bh,
280 + struct buffer_head *new_bh)
281 +{
282 + int ret = 0;
283 + char *buf = old_bh->b_data;
284 + struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
285 + struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
286 + int i, blocksize = inode->i_sb->s_blocksize;
287 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
288 +
289 + xs->bucket.bhs[0] = new_bh;
290 + get_bh(new_bh);
291 + xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
292 + xs->header = xs->bucket.xh;
293 +
294 + xs->base = new_bh->b_data;
295 + xs->end = xs->base + inode->i_sb->s_blocksize;
296 +
297 + if (!xs->not_found) {
298 + if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
299 + ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
300 + xs->bucket.bhs[0]->b_blocknr + 1,
301 + blk_per_bucket - 1, &xs->bucket.bhs[1],
302 + OCFS2_BH_CACHED, inode);
303 + if (ret) {
304 + mlog_errno(ret);
305 + return ret;
306 + }
307 +
308 + i = xs->here - old_xh->xh_entries;
309 + xs->here = &xs->header->xh_entries[i];
310 + }
311 + }
312 +
313 + return ret;
314 +}
315 +
316 +static int ocfs2_xattr_create_index_block(struct inode *inode,
317 + struct ocfs2_xattr_search *xs)
318 +{
319 + int ret, credits = OCFS2_SUBALLOC_ALLOC;
320 + u32 bit_off, len;
321 + u64 blkno;
322 + handle_t *handle;
323 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
324 + struct ocfs2_inode_info *oi = OCFS2_I(inode);
325 + struct ocfs2_alloc_context *data_ac;
326 + struct buffer_head *xh_bh = NULL, *data_bh = NULL;
327 + struct buffer_head *xb_bh = xs->xattr_bh;
328 + struct ocfs2_xattr_block *xb =
329 + (struct ocfs2_xattr_block *)xb_bh->b_data;
330 + struct ocfs2_xattr_tree_root *xr;
331 + u16 xb_flags = le16_to_cpu(xb->xb_flags);
332 + u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
333 +
334 + mlog(0, "create xattr index block for %llu\n",
335 + (unsigned long long)xb_bh->b_blocknr);
336 +
337 + BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
338 +
339 + ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
340 + if (ret) {
341 + mlog_errno(ret);
342 + goto out;
343 + }
344 +
345 + /*
346 + * XXX:
347 + * We can use this lock for now, and maybe move to a dedicated mutex
348 + * if performance becomes a problem later.
349 + */
350 + down_write(&oi->ip_alloc_sem);
351 +
352 + /*
353 + * 3 more credits, one for xattr block update, one for the 1st block
354 + * of the new xattr bucket and one for the value/data.
355 + */
356 + credits += 3;
357 + handle = ocfs2_start_trans(osb, credits);
358 + if (IS_ERR(handle)) {
359 + ret = PTR_ERR(handle);
360 + mlog_errno(ret);
361 + goto out_sem;
362 + }
363 +
364 + ret = ocfs2_journal_access(handle, inode, xb_bh,
365 + OCFS2_JOURNAL_ACCESS_WRITE);
366 + if (ret) {
367 + mlog_errno(ret);
368 + goto out_commit;
369 + }
370 +
371 + ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
372 + if (ret) {
373 + mlog_errno(ret);
374 + goto out_commit;
375 + }
376 +
377 + /*
378 + * The bucket may spread in many blocks, and
379 + * we will only touch the 1st block and the last block
380 + * in the whole bucket(one for entry and one for data).
381 + */
382 + blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
383 +
384 + mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
385 +
386 + xh_bh = sb_getblk(inode->i_sb, blkno);
387 + if (!xh_bh) {
388 + ret = -EIO;
389 + mlog_errno(ret);
390 + goto out_commit;
391 + }
392 +
393 + ocfs2_set_new_buffer_uptodate(inode, xh_bh);
394 +
395 + ret = ocfs2_journal_access(handle, inode, xh_bh,
396 + OCFS2_JOURNAL_ACCESS_CREATE);
397 + if (ret) {
398 + mlog_errno(ret);
399 + goto out_commit;
400 + }
401 +
402 + if (bpb > 1) {
403 + data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
404 + if (!data_bh) {
405 + ret = -EIO;
406 + mlog_errno(ret);
407 + goto out_commit;
408 + }
409 +
410 + ocfs2_set_new_buffer_uptodate(inode, data_bh);
411 +
412 + ret = ocfs2_journal_access(handle, inode, data_bh,
413 + OCFS2_JOURNAL_ACCESS_CREATE);
414 + if (ret) {
415 + mlog_errno(ret);
416 + goto out_commit;
417 + }
418 + }
419 +
420 + ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
421 +
422 + ocfs2_journal_dirty(handle, xh_bh);
423 + if (data_bh)
424 + ocfs2_journal_dirty(handle, data_bh);
425 +
426 + ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
427 +
428 + /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
429 + memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
430 + offsetof(struct ocfs2_xattr_block, xb_attrs));
431 +
432 + xr = &xb->xb_attrs.xb_root;
433 + xr->xt_clusters = cpu_to_le32(1);
434 + xr->xt_last_eb_blk = 0;
435 + xr->xt_list.l_tree_depth = 0;
436 + xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
437 + xr->xt_list.l_next_free_rec = cpu_to_le16(1);
438 +
439 + xr->xt_list.l_recs[0].e_cpos = 0;
440 + xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
441 + xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
442 +
443 + xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
444 +
445 + ret = ocfs2_journal_dirty(handle, xb_bh);
446 + if (ret) {
447 + mlog_errno(ret);
448 + goto out_commit;
449 + }
450 +
451 +out_commit:
452 + ocfs2_commit_trans(osb, handle);
453 +
454 +out_sem:
455 + up_write(&oi->ip_alloc_sem);
456 +
457 +out:
458 + if (data_ac)
459 + ocfs2_free_alloc_context(data_ac);
460 +
461 + brelse(xh_bh);
462 + brelse(data_bh);
463 +
464 + return ret;
465 +}
466 +
467 +static int cmp_xe_offset(const void *a, const void *b)
468 +{
469 + const struct ocfs2_xattr_entry *l = a, *r = b;
470 + u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
471 + u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
472 +
473 + if (l_name_offset < r_name_offset)
474 + return 1;
475 + if (l_name_offset > r_name_offset)
476 + return -1;
477 + return 0;
478 +}
479 +
480 +/*
481 + * defrag a xattr bucket if we find that the bucket has some
482 + * holes beteen name/value pairs.
483 + * We will move all the name/value pairs to the end of the bucket
484 + * so that we can spare some space for insertion.
485 + */
486 +static int ocfs2_defrag_xattr_bucket(struct inode *inode,
487 + struct ocfs2_xattr_bucket *bucket)
488 +{
489 + int ret, i;
490 + size_t end, offset, len, value_len;
491 + struct ocfs2_xattr_header *xh;
492 + char *entries, *buf, *bucket_buf = NULL;
493 + u64 blkno = bucket->bhs[0]->b_blocknr;
494 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
495 + u16 xh_free_start;
496 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
497 + size_t blocksize = inode->i_sb->s_blocksize;
498 + handle_t *handle;
499 + struct buffer_head **bhs;
500 + struct ocfs2_xattr_entry *xe;
501 +
502 + bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
503 + GFP_NOFS);
504 + if (!bhs)
505 + return -ENOMEM;
506 +
507 + ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
508 + OCFS2_BH_CACHED, inode);
509 + if (ret)
510 + goto out;
511 +
512 + /*
513 + * In order to make the operation more efficient and generic,
514 + * we copy all the blocks into a contiguous memory and do the
515 + * defragment there, so if anything is error, we will not touch
516 + * the real block.
517 + */
518 + bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
519 + if (!bucket_buf) {
520 + ret = -EIO;
521 + goto out;
522 + }
523 +
524 + buf = bucket_buf;
525 + for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
526 + memcpy(buf, bhs[i]->b_data, blocksize);
527 +
528 + handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
529 + if (IS_ERR(handle)) {
530 + ret = PTR_ERR(handle);
531 + handle = NULL;
532 + mlog_errno(ret);
533 + goto out;
534 + }
535 +
536 + for (i = 0; i < blk_per_bucket; i++) {
537 + ret = ocfs2_journal_access(handle, inode, bhs[i],
538 + OCFS2_JOURNAL_ACCESS_WRITE);
539 + if (ret < 0) {
540 + mlog_errno(ret);
541 + goto commit;
542 + }
543 + }
544 +
545 + xh = (struct ocfs2_xattr_header *)bucket_buf;
546 + entries = (char *)xh->xh_entries;
547 + xh_free_start = le16_to_cpu(xh->xh_free_start);
548 +
549 + mlog(0, "adjust xattr bucket in %llu, count = %u, "
550 + "xh_free_start = %u, xh_name_value_len = %u.\n",
551 + blkno, le16_to_cpu(xh->xh_count), xh_free_start,
552 + le16_to_cpu(xh->xh_name_value_len));
553 +
554 + /*
555 + * sort all the entries by their offset.
556 + * the largest will be the first, so that we can
557 + * move them to the end one by one.
558 + */
559 + sort(entries, le16_to_cpu(xh->xh_count),
560 + sizeof(struct ocfs2_xattr_entry),
561 + cmp_xe_offset, swap_xe);
562 +
563 + /* Move all name/values to the end of the bucket. */
564 + xe = xh->xh_entries;
565 + end = OCFS2_XATTR_BUCKET_SIZE;
566 + for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
567 + offset = le16_to_cpu(xe->xe_name_offset);
568 + if (ocfs2_xattr_is_local(xe))
569 + value_len = OCFS2_XATTR_SIZE(
570 + le64_to_cpu(xe->xe_value_size));
571 + else
572 + value_len = OCFS2_XATTR_ROOT_SIZE;
573 + len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
574 +
575 + /*
576 + * We must make sure that the name/value pair
577 + * exist in the same block. So adjust end to
578 + * the previous block end if needed.
579 + */
580 + if (((end - len) / blocksize !=
581 + (end - 1) / blocksize))
582 + end = end - end % blocksize;
583 +
584 + if (end > offset + len) {
585 + memmove(bucket_buf + end - len,
586 + bucket_buf + offset, len);
587 + xe->xe_name_offset = cpu_to_le16(end - len);
588 + }
589 +
590 + mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
591 + "bucket %llu\n", (unsigned long long)blkno);
592 +
593 + end -= len;
594 + }
595 +
596 + mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
597 + "bucket %llu\n", (unsigned long long)blkno);
598 +
599 + if (xh_free_start == end)
600 + goto commit;
601 +
602 + memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
603 + xh->xh_free_start = cpu_to_le16(end);
604 +
605 + /* sort the entries by their name_hash. */
606 + sort(entries, le16_to_cpu(xh->xh_count),
607 + sizeof(struct ocfs2_xattr_entry),
608 + cmp_xe, swap_xe);
609 +
610 + buf = bucket_buf;
611 + for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
612 + memcpy(bhs[i]->b_data, buf, blocksize);
613 + ocfs2_journal_dirty(handle, bhs[i]);
614 + }
615 +
616 +commit:
617 + ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
618 +out:
619 +
620 + if (bhs) {
621 + for (i = 0; i < blk_per_bucket; i++)
622 + brelse(bhs[i]);
623 + }
624 + kfree(bhs);
625 +
626 + kfree(bucket_buf);
627 + return ret;
628 +}
629 +
630 +/*
631 + * Move half nums of the xattr bucket in the previous cluster to this new
632 + * cluster. We only touch the last cluster of the previous extend record.
633 + *
634 + * first_bh is the first buffer_head of a series of bucket in the same
635 + * extent rec and header_bh is the header of one bucket in this cluster.
636 + * They will be updated if we move the data header_bh contains to the new
637 + * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
638 + */
639 +static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
640 + handle_t *handle,
641 + struct buffer_head **first_bh,
642 + struct buffer_head **header_bh,
643 + u64 new_blkno,
644 + u64 prev_blkno,
645 + u32 num_clusters,
646 + u32 *first_hash)
647 +{
648 + int i, ret, credits;
649 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
650 + int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
651 + int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
652 + int blocksize = inode->i_sb->s_blocksize;
653 + struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
654 + struct ocfs2_xattr_header *new_xh;
655 + struct ocfs2_xattr_header *xh =
656 + (struct ocfs2_xattr_header *)((*first_bh)->b_data);
657 +
658 + BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
659 + BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
660 +
661 + prev_bh = *first_bh;
662 + get_bh(prev_bh);
663 + xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
664 +
665 + prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
666 +
667 + mlog(0, "move half of xattrs in cluster %llu to %llu\n",
668 + prev_blkno, new_blkno);
669 +
670 + /*
671 + * We need to update the 1st half of the new cluster and
672 + * 1 more for the update of the 1st bucket of the previous
673 + * extent record.
674 + */
675 + credits = bpc / 2 + 1;
676 + ret = ocfs2_extend_trans(handle, credits);
677 + if (ret) {
678 + mlog_errno(ret);
679 + goto out;
680 + }
681 +
682 + ret = ocfs2_journal_access(handle, inode, prev_bh,
683 + OCFS2_JOURNAL_ACCESS_WRITE);
684 + if (ret) {
685 + mlog_errno(ret);
686 + goto out;
687 + }
688 +
689 + for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
690 + old_bh = new_bh = NULL;
691 + new_bh = sb_getblk(inode->i_sb, new_blkno);
692 + if (!new_bh) {
693 + ret = -EIO;
694 + mlog_errno(ret);
695 + goto out;
696 + }
697 +
698 + ocfs2_set_new_buffer_uptodate(inode, new_bh);
699 +
700 + ret = ocfs2_journal_access(handle, inode, new_bh,
701 + OCFS2_JOURNAL_ACCESS_CREATE);
702 + if (ret < 0) {
703 + mlog_errno(ret);
704 + brelse(new_bh);
705 + goto out;
706 + }
707 +
708 + ret = ocfs2_read_block(osb, prev_blkno,
709 + &old_bh, OCFS2_BH_CACHED, inode);
710 + if (ret < 0) {
711 + mlog_errno(ret);
712 + brelse(new_bh);
713 + goto out;
714 + }
715 +
716 + memcpy(new_bh->b_data, old_bh->b_data, blocksize);
717 +
718 + if (i == 0) {
719 + new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
720 + new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
721 +
722 + if (first_hash)
723 + *first_hash = le32_to_cpu(
724 + new_xh->xh_entries[0].xe_name_hash);
725 + new_first_bh = new_bh;
726 + get_bh(new_first_bh);
727 + }
728 +
729 + ocfs2_journal_dirty(handle, new_bh);
730 +
731 + if (*header_bh == old_bh) {
732 + brelse(*header_bh);
733 + *header_bh = new_bh;
734 + get_bh(*header_bh);
735 +
736 + brelse(*first_bh);
737 + *first_bh = new_first_bh;
738 + get_bh(*first_bh);
739 + }
740 + brelse(new_bh);
741 + brelse(old_bh);
742 + }
743 +
744 + le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
745 +
746 + ocfs2_journal_dirty(handle, prev_bh);
747 +out:
748 + brelse(prev_bh);
749 + brelse(new_first_bh);
750 + return ret;
751 +}
752 +
753 +static int ocfs2_read_xattr_bucket(struct inode *inode,
754 + u64 blkno,
755 + struct buffer_head **bhs,
756 + int new)
757 +{
758 + int ret = 0;
759 + u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
760 +
761 + if (!new)
762 + return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
763 + blk_per_bucket, bhs,
764 + OCFS2_BH_CACHED, inode);
765 +
766 + for (i = 0; i < blk_per_bucket; i++) {
767 + bhs[i] = sb_getblk(inode->i_sb, blkno + i);
768 + if (bhs[i] == NULL) {
769 + ret = -EIO;
770 + mlog_errno(ret);
771 + break;
772 + }
773 + ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
774 + }
775 +
776 + return ret;
777 +}
778 +
779 +/*
780 + * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
781 + * first_hash will record the 1st hash of the new bucket.
782 + */
783 +static int ocfs2_half_xattr_bucket(struct inode *inode,
784 + handle_t *handle,
785 + u64 blk,
786 + u64 new_blk,
787 + u32 *first_hash,
788 + int new_bucket_head)
789 +{
790 + int ret, i;
791 + u16 count, start, len, name_value_len, xe_len, name_offset;
792 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
793 + struct buffer_head **s_bhs, **t_bhs = NULL;
794 + struct ocfs2_xattr_header *xh;
795 + struct ocfs2_xattr_entry *xe;
796 + int blocksize = inode->i_sb->s_blocksize;
797 +
798 + mlog(0, "move half of xattrs from bucket %llu to %llu\n",
799 + blk, new_blk);
800 +
801 + s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
802 + if (!s_bhs)
803 + return -ENOMEM;
804 +
805 + ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
806 + if (ret) {
807 + mlog_errno(ret);
808 + goto out;
809 + }
810 +
811 + ret = ocfs2_journal_access(handle, inode, s_bhs[0],
812 + OCFS2_JOURNAL_ACCESS_WRITE);
813 + if (ret) {
814 + mlog_errno(ret);
815 + goto out;
816 + }
817 +
818 + t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
819 + if (!t_bhs) {
820 + ret = -ENOMEM;
821 + goto out;
822 + }
823 +
824 + ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
825 + if (ret) {
826 + mlog_errno(ret);
827 + goto out;
828 + }
829 +
830 + for (i = 0; i < blk_per_bucket; i++) {
831 + ret = ocfs2_journal_access(handle, inode, t_bhs[i],
832 + OCFS2_JOURNAL_ACCESS_CREATE);
833 + if (ret) {
834 + mlog_errno(ret);
835 + goto out;
836 + }
837 + }
838 +
839 + /* copy the whole bucket to the new first. */
840 + for (i = 0; i < blk_per_bucket; i++)
841 + memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
842 +
843 + /* update the new bucket. */
844 + xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
845 + count = le16_to_cpu(xh->xh_count);
846 + start = count / 2;
847 +
848 + /*
849 + * Calculate the total name/value len and xh_free_start for
850 + * the old bucket first.
851 + */
852 + name_offset = OCFS2_XATTR_BUCKET_SIZE;
853 + name_value_len = 0;
854 + for (i = 0; i < start; i++) {
855 + xe = &xh->xh_entries[i];
856 + xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
857 + if (ocfs2_xattr_is_local(xe))
858 + xe_len +=
859 + OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
860 + else
861 + xe_len += OCFS2_XATTR_ROOT_SIZE;
862 + name_value_len += xe_len;
863 + if (le16_to_cpu(xe->xe_name_offset) < name_offset)
864 + name_offset = le16_to_cpu(xe->xe_name_offset);
865 + }
866 +
867 + /*
868 + * Now begin the modification to the new bucket.
869 + *
870 + * In the new bucket, We just move the xattr entry to the beginning
871 + * and don't touch the name/value. So there will be some holes in the
872 + * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
873 + * called.
874 + */
875 + xe = &xh->xh_entries[start];
876 + len = sizeof(struct ocfs2_xattr_entry) * (count - start);
877 + mlog(0, "mv xattr entry len %d from %d to %d\n", len,
878 + (char *)xe - (char *)xh, (char *)xh->xh_entries - (char *)xh);
879 + memmove((char *)xh->xh_entries, (char *)xe, len);
880 + xe = &xh->xh_entries[count - start];
881 + len = sizeof(struct ocfs2_xattr_entry) * start;
882 + memset((char *)xe, 0, len);
883 +
884 + le16_add_cpu(&xh->xh_count, -start);
885 + le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
886 +
887 + /* Calculate xh_free_start for the new bucket. */
888 + xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
889 + for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
890 + xe = &xh->xh_entries[i];
891 + xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
892 + if (ocfs2_xattr_is_local(xe))
893 + xe_len +=
894 + OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
895 + else
896 + xe_len += OCFS2_XATTR_ROOT_SIZE;
897 + if (le16_to_cpu(xe->xe_name_offset) <
898 + le16_to_cpu(xh->xh_free_start))
899 + xh->xh_free_start = xe->xe_name_offset;
900 + }
901 +
902 + /* set xh->xh_num_buckets for the new xh. */
903 + if (new_bucket_head)
904 + xh->xh_num_buckets = cpu_to_le16(1);
905 + else
906 + xh->xh_num_buckets = 0;
907 +
908 + for (i = 0; i < blk_per_bucket; i++) {
909 + ocfs2_journal_dirty(handle, t_bhs[i]);
910 + if (ret)
911 + mlog_errno(ret);
912 + }
913 +
914 + /* store the first_hash of the new bucket. */
915 + if (first_hash)
916 + *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
917 +
918 + /*
919 + * Now only update the 1st block of the old bucket.
920 + * Please note that the entry has been sorted already above.
921 + */
922 + xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
923 + memset(&xh->xh_entries[start], 0,
924 + sizeof(struct ocfs2_xattr_entry) * (count - start));
925 + xh->xh_count = cpu_to_le16(start);
926 + xh->xh_free_start = cpu_to_le16(name_offset);
927 + xh->xh_name_value_len = cpu_to_le16(name_value_len);
928 +
929 + ocfs2_journal_dirty(handle, s_bhs[0]);
930 + if (ret)
931 + mlog_errno(ret);
932 +
933 +out:
934 + if (s_bhs) {
935 + for (i = 0; i < blk_per_bucket; i++)
936 + brelse(s_bhs[i]);
937 + }
938 + kfree(s_bhs);
939 +
940 + if (t_bhs) {
941 + for (i = 0; i < blk_per_bucket; i++)
942 + brelse(t_bhs[i]);
943 + }
944 + kfree(t_bhs);
945 +
946 + return ret;
947 +}
948 +
949 +/*
950 + * Copy xattr from one bucket to another bucket.
951 + *
952 + * The caller must make sure that the journal transaction
953 + * has enough space for journaling.
954 + */
955 +static int ocfs2_cp_xattr_bucket(struct inode *inode,
956 + handle_t *handle,
957 + u64 s_blkno,
958 + u64 t_blkno,
959 + int t_is_new)
960 +{
961 + int ret, i;
962 + int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
963 + int blocksize = inode->i_sb->s_blocksize;
964 + struct buffer_head **s_bhs, **t_bhs = NULL;
965 +
966 + BUG_ON(s_blkno == t_blkno);
967 +
968 + mlog(0, "cp bucket %llu to %llu, target is %d\n",
969 + s_blkno, t_blkno, t_is_new);
970 +
971 + s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
972 + GFP_NOFS);
973 + if (!s_bhs)
974 + return -ENOMEM;
975 +
976 + ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
977 + if (ret)
978 + goto out;
979 +
980 + t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
981 + GFP_NOFS);
982 + if (!t_bhs) {
983 + ret = -ENOMEM;
984 + goto out;
985 + }
986 +
987 + ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
988 + if (ret)
989 + goto out;
990 +
991 + for (i = 0; i < blk_per_bucket; i++) {
992 + ret = ocfs2_journal_access(handle, inode, t_bhs[i],
993 + OCFS2_JOURNAL_ACCESS_WRITE);
994 + if (ret)
995 + goto out;
996 + }
997 +
998 + for (i = 0; i < blk_per_bucket; i++) {
999 + memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
1000 + ocfs2_journal_dirty(handle, t_bhs[i]);
1001 + }
1002 +
1003 +out:
1004 + if (s_bhs) {
1005 + for (i = 0; i < blk_per_bucket; i++)
1006 + brelse(s_bhs[i]);
1007 + }
1008 + kfree(s_bhs);
1009 +
1010 + if (t_bhs) {
1011 + for (i = 0; i < blk_per_bucket; i++)
1012 + brelse(t_bhs[i]);
1013 + }
1014 + kfree(t_bhs);
1015 +
1016 + return ret;
1017 +}
1018 +
1019 +/*
1020 + * Copy one xattr cluster from src_blk to to_blk.
1021 + * The to_blk will become the first bucket header of the cluster, so its
1022 + * xh_num_buckets will be initialized as the bucket num in the cluster.
1023 + */
1024 +static int ocfs2_cp_xattr_cluster(struct inode *inode,
1025 + handle_t *handle,
1026 + struct buffer_head *first_bh,
1027 + u64 src_blk,
1028 + u64 to_blk,
1029 + u32 *first_hash)
1030 +{
1031 + int i, ret, credits;
1032 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1033 + int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1034 + int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
1035 + struct buffer_head *bh = NULL;
1036 + struct ocfs2_xattr_header *xh;
1037 + u64 to_blk_start = to_blk;
1038 +
1039 + mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
1040 +
1041 + /*
1042 + * We need to update the new cluster and 1 more for the update of
1043 + * the 1st bucket of the previous extent rec.
1044 + */
1045 + credits = bpc + 1;
1046 + ret = ocfs2_extend_trans(handle, credits);
1047 + if (ret) {
1048 + mlog_errno(ret);
1049 + goto out;
1050 + }
1051 +
1052 + ret = ocfs2_journal_access(handle, inode, first_bh,
1053 + OCFS2_JOURNAL_ACCESS_WRITE);
1054 + if (ret) {
1055 + mlog_errno(ret);
1056 + goto out;
1057 + }
1058 +
1059 + for (i = 0; i < num_buckets; i++) {
1060 + ret = ocfs2_cp_xattr_bucket(inode, handle,
1061 + src_blk, to_blk, 1);
1062 + if (ret) {
1063 + mlog_errno(ret);
1064 + goto out;
1065 + }
1066 +
1067 + src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1068 + to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1069 + }
1070 +
1071 + /* update the old bucket header. */
1072 + xh = (struct ocfs2_xattr_header *)first_bh->b_data;
1073 + le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
1074 +
1075 + ocfs2_journal_dirty(handle, first_bh);
1076 +
1077 + /* update the new bucket header. */
1078 + ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
1079 + if (ret < 0) {
1080 + mlog_errno(ret);
1081 + goto out;
1082 + }
1083 +
1084 + ret = ocfs2_journal_access(handle, inode, bh,
1085 + OCFS2_JOURNAL_ACCESS_WRITE);
1086 + if (ret) {
1087 + mlog_errno(ret);
1088 + goto out;
1089 + }
1090 +
1091 + xh = (struct ocfs2_xattr_header *)bh->b_data;
1092 + xh->xh_num_buckets = cpu_to_le16(num_buckets);
1093 +
1094 + ocfs2_journal_dirty(handle, bh);
1095 +
1096 + if (first_hash)
1097 + *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
1098 +out:
1099 + brelse(bh);
1100 + return ret;
1101 +}
1102 +
1103 +/*
1104 + * Move half of the xattrs in this cluster to the new cluster.
1105 + * This function should only be called when bucket size == cluster size.
1106 + * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
1107 + */
1108 +static int ocfs2_half_xattr_cluster(struct inode *inode,
1109 + handle_t *handle,
1110 + u64 prev_blk,
1111 + u64 new_blk,
1112 + u32 *first_hash)
1113 +{
1114 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1115 + int ret, credits = 2 * blk_per_bucket;
1116 +
1117 + BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
1118 +
1119 + ret = ocfs2_extend_trans(handle, credits);
1120 + if (ret) {
1121 + mlog_errno(ret);
1122 + return ret;
1123 + }
1124 +
1125 + /* Move half of the xattr in start_blk to the next bucket. */
1126 + return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
1127 + new_blk, first_hash, 1);
1128 +}
1129 +
1130 +/*
1131 + * Move some xattrs from the old cluster to the new one since they are not
1132 + * contiguous in ocfs2 xattr tree.
1133 + *
1134 + * new_blk starts a new separate cluster, and we will move some xattrs from
1135 + * prev_blk to it. v_start will be set as the first name hash value in this
1136 + * new cluster so that it can be used as e_cpos during tree insertion and
1137 + * don't collide with our original b-tree operations. first_bh and header_bh
1138 + * will also be updated since they will be used in ocfs2_extend_xattr_bucket
1139 + * to extend the insert bucket.
1140 + *
1141 + * The problem is how much xattr should we move to the new one and when should
1142 + * we update first_bh and header_bh?
1143 + * 1. If cluster size > bucket size, that means the previous cluster has more
1144 + * than 1 bucket, so just move half nums of bucket into the new cluster and
1145 + * update the first_bh and header_bh if the insert bucket has been moved
1146 + * to the new cluster.
1147 + * 2. If cluster_size == bucket_size:
1148 + * a) If the previous extent rec has more than one cluster and the insert
1149 + * place isn't in the last cluster, copy the entire last cluster to the
1150 + * new one. This time, we don't need to upate the first_bh and header_bh
1151 + * since they will not be moved into the new cluster.
1152 + * b) Otherwise, move the bottom half of the xattrs in the last cluster into
1153 + * the new one. And we set the extend flag to zero if the insert place is
1154 + * moved into the new allocated cluster since no extend is needed.
1155 + */
1156 +static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
1157 + handle_t *handle,
1158 + struct buffer_head **first_bh,
1159 + struct buffer_head **header_bh,
1160 + u64 new_blk,
1161 + u64 prev_blk,
1162 + u32 prev_clusters,
1163 + u32 *v_start,
1164 + int *extend)
1165 +{
1166 + int ret = 0;
1167 + int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1168 +
1169 + mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
1170 + prev_blk, prev_clusters, new_blk);
1171 +
1172 + if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
1173 + ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
1174 + handle,
1175 + first_bh,
1176 + header_bh,
1177 + new_blk,
1178 + prev_blk,
1179 + prev_clusters,
1180 + v_start);
1181 + else {
1182 + u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
1183 +
1184 + if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
1185 + ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
1186 + last_blk, new_blk,
1187 + v_start);
1188 + else {
1189 + ret = ocfs2_half_xattr_cluster(inode, handle,
1190 + last_blk, new_blk,
1191 + v_start);
1192 +
1193 + if ((*header_bh)->b_blocknr == last_blk && extend)
1194 + *extend = 0;
1195 + }
1196 + }
1197 +
1198 + return ret;
1199 +}
1200 +
1201 +/*
1202 + * Add a new cluster for xattr storage.
1203 + *
1204 + * If the new cluster is contiguous with the previous one, it will be
1205 + * appended to the same extent record, and num_clusters will be updated.
1206 + * If not, we will insert a new extent for it and move some xattrs in
1207 + * the last cluster into the new allocated one.
1208 + * We also need to limit the maximum size of a btree leaf, otherwise we'll
1209 + * lose the benefits of hashing because we'll have to search large leaves.
1210 + * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
1211 + * if it's bigger).
1212 + *
1213 + * first_bh is the first block of the previous extent rec and header_bh
1214 + * indicates the bucket we will insert the new xattrs. They will be updated
1215 + * when the header_bh is moved into the new cluster.
1216 + */
1217 +static int ocfs2_add_new_xattr_cluster(struct inode *inode,
1218 + struct buffer_head *root_bh,
1219 + struct buffer_head **first_bh,
1220 + struct buffer_head **header_bh,
1221 + u32 *num_clusters,
1222 + u32 prev_cpos,
1223 + u64 prev_blkno,
1224 + int *extend)
1225 +{
1226 + int ret, credits;
1227 + u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1228 + u32 prev_clusters = *num_clusters;
1229 + u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
1230 + u64 block;
1231 + handle_t *handle = NULL;
1232 + struct ocfs2_alloc_context *data_ac = NULL;
1233 + struct ocfs2_alloc_context *meta_ac = NULL;
1234 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1235 + struct ocfs2_xattr_block *xb =
1236 + (struct ocfs2_xattr_block *)root_bh->b_data;
1237 + struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
1238 + struct ocfs2_extent_list *root_el = &xb_root->xt_list;
1239 + enum ocfs2_extent_tree_type type = OCFS2_XATTR_TREE_EXTENT;
1240 +
1241 + mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
1242 + "previous xattr blkno = %llu\n",
1243 + (unsigned long long)OCFS2_I(inode)->ip_blkno,
1244 + prev_cpos, prev_blkno);
1245 +
1246 + ret = ocfs2_lock_allocators(inode, root_bh, root_el,
1247 + clusters_to_add, 0, &data_ac,
1248 + &meta_ac, type, NULL);
1249 + if (ret) {
1250 + mlog_errno(ret);
1251 + goto leave;
1252 + }
1253 +
1254 + credits = ocfs2_calc_extend_credits(osb->sb, root_el, clusters_to_add);
1255 + handle = ocfs2_start_trans(osb, credits);
1256 + if (IS_ERR(handle)) {
1257 + ret = PTR_ERR(handle);
1258 + handle = NULL;
1259 + mlog_errno(ret);
1260 + goto leave;
1261 + }
1262 +
1263 + ret = ocfs2_journal_access(handle, inode, root_bh,
1264 + OCFS2_JOURNAL_ACCESS_WRITE);
1265 + if (ret < 0) {
1266 + mlog_errno(ret);
1267 + goto leave;
1268 + }
1269 +
1270 + ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
1271 + clusters_to_add, &bit_off, &num_bits);
1272 + if (ret < 0) {
1273 + if (ret != -ENOSPC)
1274 + mlog_errno(ret);
1275 + goto leave;
1276 + }
1277 +
1278 + BUG_ON(num_bits > clusters_to_add);
1279 +
1280 + block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
1281 + mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
1282 + num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
1283 +
1284 + if (prev_blkno + prev_clusters * bpc == block &&
1285 + (prev_clusters + num_bits) << osb->s_clustersize_bits <=
1286 + OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
1287 + /*
1288 + * If this cluster is contiguous with the old one and
1289 + * adding this new cluster, we don't surpass the limit of
1290 + * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
1291 + * initialized and used like other buckets in the previous
1292 + * cluster.
1293 + * So add it as a contiguous one. The caller will handle
1294 + * its init process.
1295 + */
1296 + v_start = prev_cpos + prev_clusters;
1297 + *num_clusters = prev_clusters + num_bits;
1298 + mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
1299 + num_bits);
1300 + } else {
1301 + ret = ocfs2_adjust_xattr_cross_cluster(inode,
1302 + handle,
1303 + first_bh,
1304 + header_bh,
1305 + block,
1306 + prev_blkno,
1307 + prev_clusters,
1308 + &v_start,
1309 + extend);
1310 + if (ret) {
1311 + mlog_errno(ret);
1312 + goto leave;
1313 + }
1314 + }
1315 +
1316 + mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
1317 + num_bits, block, v_start);
1318 + ret = ocfs2_xattr_tree_insert_extent(osb, handle, inode, root_bh,
1319 + v_start, block, num_bits,
1320 + 0, meta_ac);
1321 + if (ret < 0) {
1322 + mlog_errno(ret);
1323 + goto leave;
1324 + }
1325 +
1326 + ret = ocfs2_journal_dirty(handle, root_bh);
1327 + if (ret < 0) {
1328 + mlog_errno(ret);
1329 + goto leave;
1330 + }
1331 +
1332 +leave:
1333 + if (handle)
1334 + ocfs2_commit_trans(osb, handle);
1335 + if (data_ac)
1336 + ocfs2_free_alloc_context(data_ac);
1337 + if (meta_ac)
1338 + ocfs2_free_alloc_context(meta_ac);
1339 +
1340 + return ret;
1341 +}
1342 +
1343 +/*
1344 + * Extend a new xattr bucket and move xattrs to the end one by one until
1345 + * We meet with start_bh. Only move half of the xattrs to the bucket after it.
1346 + */
1347 +static int ocfs2_extend_xattr_bucket(struct inode *inode,
1348 + struct buffer_head *first_bh,
1349 + struct buffer_head *start_bh,
1350 + u32 num_clusters)
1351 +{
1352 + int ret, credits;
1353 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1354 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1355 + u64 start_blk = start_bh->b_blocknr, end_blk;
1356 + u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
1357 + handle_t *handle;
1358 + struct ocfs2_xattr_header *first_xh =
1359 + (struct ocfs2_xattr_header *)first_bh->b_data;
1360 + u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
1361 +
1362 + mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
1363 + "from %llu, len = %u\n", start_blk,
1364 + (unsigned long long)first_bh->b_blocknr, num_clusters);
1365 +
1366 + BUG_ON(bucket >= num_buckets);
1367 +
1368 + end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
1369 +
1370 + /*
1371 + * We will touch all the buckets after the start_bh(include it).
1372 + * Add one more bucket and modify the first_bh.
1373 + */
1374 + credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
1375 + handle = ocfs2_start_trans(osb, credits);
1376 + if (IS_ERR(handle)) {
1377 + ret = PTR_ERR(handle);
1378 + handle = NULL;
1379 + mlog_errno(ret);
1380 + goto out;
1381 + }
1382 +
1383 + ret = ocfs2_journal_access(handle, inode, first_bh,
1384 + OCFS2_JOURNAL_ACCESS_WRITE);
1385 + if (ret) {
1386 + mlog_errno(ret);
1387 + goto commit;
1388 + }
1389 +
1390 + while (end_blk != start_blk) {
1391 + ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
1392 + end_blk + blk_per_bucket, 0);
1393 + if (ret)
1394 + goto commit;
1395 + end_blk -= blk_per_bucket;
1396 + }
1397 +
1398 + /* Move half of the xattr in start_blk to the next bucket. */
1399 + ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
1400 + start_blk + blk_per_bucket, NULL, 0);
1401 +
1402 + le16_add_cpu(&first_xh->xh_num_buckets, 1);
1403 + ocfs2_journal_dirty(handle, first_bh);
1404 +
1405 +commit:
1406 + ocfs2_commit_trans(osb, handle);
1407 +out:
1408 + return ret;
1409 +}
1410 +
1411 +/*
1412 + * Add new xattr bucket in an extent record and adjust the buckets accordingly.
1413 + * xb_bh is the ocfs2_xattr_block.
1414 + * We will move all the buckets starting from header_bh to the next place. As
1415 + * for this one, half num of its xattrs will be moved to the next one.
1416 + *
1417 + * We will allocate a new cluster if current cluster is full and adjust
1418 + * header_bh and first_bh if the insert place is moved to the new cluster.
1419 + */
1420 +static int ocfs2_add_new_xattr_bucket(struct inode *inode,
1421 + struct buffer_head *xb_bh,
1422 + struct buffer_head *header_bh)
1423 +{
1424 + struct ocfs2_xattr_header *first_xh = NULL;
1425 + struct buffer_head *first_bh = NULL;
1426 + struct ocfs2_xattr_block *xb =
1427 + (struct ocfs2_xattr_block *)xb_bh->b_data;
1428 + struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
1429 + struct ocfs2_extent_list *el = &xb_root->xt_list;
1430 + struct ocfs2_xattr_header *xh =
1431 + (struct ocfs2_xattr_header *)header_bh->b_data;
1432 + u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
1433 + struct super_block *sb = inode->i_sb;
1434 + struct ocfs2_super *osb = OCFS2_SB(sb);
1435 + int ret, num_buckets, extend = 1;
1436 + u64 p_blkno;
1437 + u32 e_cpos, num_clusters;
1438 +
1439 + mlog(0, "Add new xattr bucket starting form %llu\n",
1440 + (unsigned long long)header_bh->b_blocknr);
1441 +
1442 + /*
1443 + * Add refrence for header_bh here because it may be
1444 + * changed in ocfs2_add_new_xattr_cluster and we need
1445 + * to free it in the end.
1446 + */
1447 + get_bh(header_bh);
1448 +
1449 + ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
1450 + &num_clusters, el);
1451 + if (ret) {
1452 + mlog_errno(ret);
1453 + goto out;
1454 + }
1455 +
1456 + ret = ocfs2_read_block(osb, p_blkno,
1457 + &first_bh, OCFS2_BH_CACHED, inode);
1458 + if (ret) {
1459 + mlog_errno(ret);
1460 + goto out;
1461 + }
1462 +
1463 + num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
1464 + first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
1465 +
1466 + if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
1467 + ret = ocfs2_add_new_xattr_cluster(inode,
1468 + xb_bh,
1469 + &first_bh,
1470 + &header_bh,
1471 + &num_clusters,
1472 + e_cpos,
1473 + p_blkno,
1474 + &extend);
1475 + if (ret) {
1476 + mlog_errno(ret);
1477 + goto out;
1478 + }
1479 + }
1480 +
1481 + if (extend)
1482 + ret = ocfs2_extend_xattr_bucket(inode,
1483 + first_bh,
1484 + header_bh,
1485 + num_clusters);
1486 + if (ret)
1487 + mlog_errno(ret);
1488 +out:
1489 + brelse(first_bh);
1490 + brelse(header_bh);
1491 + return ret;
1492 +}
1493 +
1494 +static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
1495 + struct ocfs2_xattr_bucket *bucket,
1496 + int offs)
1497 +{
1498 + int block_off = offs >> inode->i_sb->s_blocksize_bits;
1499 +
1500 + offs = offs % inode->i_sb->s_blocksize;
1501 + return bucket->bhs[block_off]->b_data + offs;
1502 +}
1503 +
1504 +/*
1505 + * Handle the normal xattr set, including replace, delete and new.
1506 + * When the bucket is empty, "is_empty" is set and the caller can
1507 + * free this bucket.
1508 + *
1509 + * Note: "local" indicates the real data's locality. So we can't
1510 + * just its bucket locality by its length.
1511 + */
1512 +static void ocfs2_xattr_set_entry_normal(struct inode *inode,
1513 + struct ocfs2_xattr_info *xi,
1514 + struct ocfs2_xattr_search *xs,
1515 + u32 name_hash,
1516 + int local,
1517 + int *is_empty)
1518 +{
1519 + struct ocfs2_xattr_entry *last, *xe;
1520 + int name_len = strlen(xi->name);
1521 + struct ocfs2_xattr_header *xh = xs->header;
1522 + u16 count = le16_to_cpu(xh->xh_count), start;
1523 + size_t blocksize = inode->i_sb->s_blocksize;
1524 + char *val;
1525 + size_t offs, size, new_size;
1526 +
1527 + last = &xh->xh_entries[count];
1528 + if (!xs->not_found) {
1529 + xe = xs->here;
1530 + offs = le16_to_cpu(xe->xe_name_offset);
1531 + if (ocfs2_xattr_is_local(xe))
1532 + size = OCFS2_XATTR_SIZE(name_len) +
1533 + OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
1534 + else
1535 + size = OCFS2_XATTR_SIZE(name_len) +
1536 + OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
1537 +
1538 + /*
1539 + * If the new value will be stored outside, xi->value has been
1540 + * initalized as an empty ocfs2_xattr_value_root, and the same
1541 + * goes with xi->value_len, so we can set new_size safely here.
1542 + * See ocfs2_xattr_set_in_bucket.
1543 + */
1544 + new_size = OCFS2_XATTR_SIZE(name_len) +
1545 + OCFS2_XATTR_SIZE(xi->value_len);
1546 +
1547 + le16_add_cpu(&xh->xh_name_value_len, -size);
1548 + if (xi->value) {
1549 + if (new_size > size)
1550 + goto set_new_name_value;
1551 +
1552 + /* Now replace the old value with new one. */
1553 + if (local)
1554 + xe->xe_value_size = cpu_to_le64(xi->value_len);
1555 + else
1556 + xe->xe_value_size = 0;
1557 +
1558 + val = ocfs2_xattr_bucket_get_val(inode,
1559 + &xs->bucket, offs);
1560 + memset(val + OCFS2_XATTR_SIZE(name_len), 0,
1561 + size - OCFS2_XATTR_SIZE(name_len));
1562 + if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
1563 + memcpy(val + OCFS2_XATTR_SIZE(name_len),
1564 + xi->value, xi->value_len);
1565 +
1566 + le16_add_cpu(&xh->xh_name_value_len, new_size);
1567 + ocfs2_xattr_set_local(xe, local);
1568 + return;
1569 + } else {
1570 + /* Remove the old entry. */
1571 + last -= 1;
1572 + memmove(xe, xe + 1,
1573 + (void *)last - (void *)xe);
1574 + memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1575 + le16_add_cpu(&xh->xh_count, -1);
1576 + if (xh->xh_count == 0 && is_empty)
1577 + *is_empty = 1;
1578 + return;
1579 + }
1580 + } else {
1581 + /* find a new entry for insert. */
1582 + int low = 0, high = count - 1, tmp;
1583 + struct ocfs2_xattr_entry *tmp_xe;
1584 +
1585 + while (low <= high) {
1586 + tmp = (low + high) / 2;
1587 + tmp_xe = &xh->xh_entries[tmp];
1588 +
1589 + if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1590 + low = tmp + 1;
1591 + else if (name_hash <
1592 + le32_to_cpu(tmp_xe->xe_name_hash))
1593 + high = tmp - 1;
1594 + else
1595 + break;
1596 + }
1597 +
1598 + xe = &xh->xh_entries[low];
1599 + if (low != count)
1600 + memmove(xe + 1, xe, (void *)last - (void *)xe);
1601 +
1602 + le16_add_cpu(&xh->xh_count, 1);
1603 + memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
1604 + xe->xe_name_hash = cpu_to_le32(name_hash);
1605 + xe->xe_name_len = name_len;
1606 + ocfs2_xattr_set_type(xe, xi->name_index);
1607 + }
1608 +
1609 +set_new_name_value:
1610 + /* Insert the new name+value. */
1611 + size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
1612 +
1613 + /*
1614 + * We must make sure that the name/value pair
1615 + * exists in the same block.
1616 + */
1617 + offs = le16_to_cpu(xh->xh_free_start);
1618 + start = offs - size;
1619 +
1620 + if (start >> inode->i_sb->s_blocksize_bits !=
1621 + (offs - 1) >> inode->i_sb->s_blocksize_bits) {
1622 + offs = offs - offs % blocksize;
1623 + xh->xh_free_start = cpu_to_le16(offs);
1624 + }
1625 +
1626 + val = ocfs2_xattr_bucket_get_val(inode,
1627 + &xs->bucket, offs - size);
1628 + xe->xe_name_offset = cpu_to_le16(offs - size);
1629 +
1630 + memset(val, 0, size);
1631 + memcpy(val, xi->name, name_len);
1632 + memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
1633 +
1634 + xe->xe_value_size = cpu_to_le64(xi->value_len);
1635 + ocfs2_xattr_set_local(xe, local);
1636 + xs->here = xe;
1637 + le16_add_cpu(&xh->xh_free_start, -size);
1638 + le16_add_cpu(&xh->xh_name_value_len, size);
1639 +
1640 + return;
1641 +}
1642 +
1643 +static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
1644 + handle_t *handle,
1645 + struct ocfs2_xattr_search *xs,
1646 + struct buffer_head **bhs,
1647 + u16 bh_num)
1648 +{
1649 + int ret = 0, off, block_off;
1650 + struct ocfs2_xattr_entry *xe = xs->here;
1651 +
1652 + /*
1653 + * First calculate all the blocks we should journal_access
1654 + * and journal_dirty. The first block should always be touched.
1655 + */
1656 + ret = ocfs2_journal_dirty(handle, bhs[0]);
1657 + if (ret)
1658 + mlog_errno(ret);
1659 +
1660 + /* calc the data. */
1661 + off = le16_to_cpu(xe->xe_name_offset);
1662 + block_off = off >> inode->i_sb->s_blocksize_bits;
1663 + ret = ocfs2_journal_dirty(handle, bhs[block_off]);
1664 + if (ret)
1665 + mlog_errno(ret);
1666 +
1667 + return ret;
1668 +}
1669 +
1670 +/*
1671 + * Set the xattr entry in the specified bucket.
1672 + * The bucket is indicated by xs->bucket and it should have the enough
1673 + * space for the xattr insertion.
1674 + */
1675 +static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
1676 + struct ocfs2_xattr_info *xi,
1677 + struct ocfs2_xattr_search *xs,
1678 + u32 name_hash,
1679 + int local,
1680 + int *bucket_empty)
1681 +{
1682 + int i, ret;
1683 + handle_t *handle = NULL;
1684 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1685 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1686 +
1687 + mlog(0, "Set xattr entry len = %d index = %d in bucket %llu\n",
1688 + xi->value_len, xi->name_index,
1689 + (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
1690 +
1691 + if (!xs->bucket.bhs[1]) {
1692 + ret = ocfs2_read_blocks(osb,
1693 + xs->bucket.bhs[0]->b_blocknr + 1,
1694 + blk_per_bucket - 1, &xs->bucket.bhs[1],
1695 + OCFS2_BH_CACHED, inode);
1696 + if (ret) {
1697 + mlog_errno(ret);
1698 + goto out;
1699 + }
1700 + }
1701 +
1702 + handle = ocfs2_start_trans(osb, blk_per_bucket);
1703 + if (IS_ERR(handle)) {
1704 + ret = PTR_ERR(handle);
1705 + handle = NULL;
1706 + mlog_errno(ret);
1707 + goto out;
1708 + }
1709 +
1710 + for (i = 0; i < blk_per_bucket; i++) {
1711 + ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
1712 + OCFS2_JOURNAL_ACCESS_WRITE);
1713 + if (ret < 0) {
1714 + mlog_errno(ret);
1715 + goto out;
1716 + }
1717 + }
1718 +
1719 + ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash,
1720 + local, bucket_empty);
1721 +
1722 + /*Only dirty the blocks we have touched in set xattr. */
1723 + ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
1724 + xs->bucket.bhs, blk_per_bucket);
1725 + if (ret)
1726 + mlog_errno(ret);
1727 +out:
1728 + ocfs2_commit_trans(osb, handle);
1729 +
1730 + return ret;
1731 +}
1732 +
1733 +static int ocfs2_xattr_value_update_size(struct inode *inode,
1734 + struct buffer_head *xe_bh,
1735 + struct ocfs2_xattr_entry *xe,
1736 + u64 new_size)
1737 +{
1738 + int ret;
1739 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1740 + handle_t *handle = NULL;
1741 +
1742 + handle = ocfs2_start_trans(osb, 1);
1743 + if (handle == NULL) {
1744 + ret = -ENOMEM;
1745 + mlog_errno(ret);
1746 + goto out;
1747 + }
1748 +
1749 + ret = ocfs2_journal_access(handle, inode, xe_bh,
1750 + OCFS2_JOURNAL_ACCESS_WRITE);
1751 + if (ret < 0) {
1752 + mlog_errno(ret);
1753 + goto out_commit;
1754 + }
1755 +
1756 + xe->xe_value_size = cpu_to_le64(new_size);
1757 +
1758 + ret = ocfs2_journal_dirty(handle, xe_bh);
1759 + if (ret < 0)
1760 + mlog_errno(ret);
1761 +
1762 +out_commit:
1763 + ocfs2_commit_trans(osb, handle);
1764 +out:
1765 + return ret;
1766 +}
1767 +
1768 +/*
1769 + * Truncate the specified xe_off entry in xattr bucket.
1770 + * bucket is indicated by header_bh and len is the new length.
1771 + * Both the ocfs2_xattr_value_root and the entry will be updated here.
1772 + *
1773 + * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
1774 + */
1775 +static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
1776 + struct buffer_head *header_bh,
1777 + int xe_off,
1778 + int len)
1779 +{
1780 + int ret, offset;
1781 + u64 value_blk;
1782 + struct buffer_head *value_bh = NULL;
1783 + struct ocfs2_xattr_value_root *xv;
1784 + struct ocfs2_xattr_entry *xe;
1785 + struct ocfs2_xattr_header *xh =
1786 + (struct ocfs2_xattr_header *)header_bh->b_data;
1787 + size_t blocksize = inode->i_sb->s_blocksize;
1788 +
1789 + xe = &xh->xh_entries[xe_off];
1790 +
1791 + BUG_ON(!xe || ocfs2_xattr_is_local(xe));
1792 +
1793 + offset = le16_to_cpu(xe->xe_name_offset) +
1794 + OCFS2_XATTR_SIZE(xe->xe_name_len);
1795 +
1796 + value_blk = offset / blocksize;
1797 +
1798 + /* We don't allow ocfs2_xattr_value to be stored in different block. */
1799 + BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
1800 + value_blk += header_bh->b_blocknr;
1801 +
1802 + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
1803 + &value_bh, OCFS2_BH_CACHED, inode);
1804 + if (ret) {
1805 + mlog_errno(ret);
1806 + goto out;
1807 + }
1808 +
1809 + xv = (struct ocfs2_xattr_value_root *)
1810 + (value_bh->b_data + offset % blocksize);
1811 +
1812 + mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
1813 + xe_off, (unsigned long long)header_bh->b_blocknr, len);
1814 + ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
1815 + if (ret) {
1816 + mlog_errno(ret);
1817 + goto out;
1818 + }
1819 +
1820 + ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
1821 + if (ret) {
1822 + mlog_errno(ret);
1823 + goto out;
1824 + }
1825 +
1826 +out:
1827 + brelse(value_bh);
1828 + return ret;
1829 +}
1830 +
1831 +static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
1832 + struct ocfs2_xattr_search *xs,
1833 + int len)
1834 +{
1835 + int ret, offset;
1836 + struct ocfs2_xattr_entry *xe = xs->here;
1837 + struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
1838 +
1839 + BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
1840 +
1841 + offset = xe - xh->xh_entries;
1842 + ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
1843 + offset, len);
1844 + if (ret)
1845 + mlog_errno(ret);
1846 +
1847 + return ret;
1848 +}
1849 +
1850 +static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
1851 + struct ocfs2_xattr_search *xs,
1852 + char *val,
1853 + int value_len)
1854 +{
1855 + int offset;
1856 + struct ocfs2_xattr_value_root *xv;
1857 + struct ocfs2_xattr_entry *xe = xs->here;
1858 +
1859 + BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
1860 +
1861 + offset = le16_to_cpu(xe->xe_name_offset) +
1862 + OCFS2_XATTR_SIZE(xe->xe_name_len);
1863 +
1864 + xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
1865 +
1866 + return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
1867 +}
1868 +
1869 +/*
1870 + * Remove the xattr bucket pointed by bucket_bh.
1871 + * All the buckets after it in the same xattr extent rec will be
1872 + * move forward one by one.
1873 + */
1874 +static int ocfs2_rm_xattr_bucket(struct inode *inode,
1875 + struct buffer_head *first_bh,
1876 + struct ocfs2_xattr_bucket *bucket)
1877 +{
1878 + int ret = 0, credits;
1879 + struct ocfs2_xattr_header *xh =
1880 + (struct ocfs2_xattr_header *)first_bh->b_data;
1881 + u16 bucket_num = le16_to_cpu(xh->xh_num_buckets);
1882 + u64 end, start = bucket->bhs[0]->b_blocknr;
1883 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1884 + handle_t *handle;
1885 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
1886 +
1887 + end = first_bh->b_blocknr + (bucket_num - 1) * blk_per_bucket;
1888 +
1889 + mlog(0, "rm xattr bucket %llu\n", start);
1890 + /*
1891 + * We need to update the first xattr_header and all the buckets starting
1892 + * from start in this xattr rec.
1893 + *
1894 + * XXX: Should we empty the old last bucket here?
1895 + */
1896 + credits = 1 + end - start;
1897 + handle = ocfs2_start_trans(osb, credits);
1898 + if (IS_ERR(handle)) {
1899 + ret = PTR_ERR(handle);
1900 + mlog_errno(ret);
1901 + return ret;
1902 + }
1903 +
1904 + ret = ocfs2_journal_access(handle, inode, first_bh,
1905 + OCFS2_JOURNAL_ACCESS_WRITE);
1906 + if (ret) {
1907 + mlog_errno(ret);
1908 + goto out_commit;
1909 + }
1910 +
1911 +
1912 + while (start < end) {
1913 + ret = ocfs2_cp_xattr_bucket(inode, handle,
1914 + start + blk_per_bucket,
1915 + start, 0);
1916 + if (ret) {
1917 + mlog_errno(ret);
1918 + goto out_commit;
1919 + }
1920 + start += blk_per_bucket;
1921 + }
1922 +
1923 + /* update the first_bh. */
1924 + xh->xh_num_buckets = cpu_to_le16(bucket_num - 1);
1925 + ocfs2_journal_dirty(handle, first_bh);
1926 +
1927 +out_commit:
1928 + ocfs2_commit_trans(osb, handle);
1929 + return ret;
1930 +}
1931 +
1932 +static int ocfs2_rm_xattr_cluster(struct inode *inode,
1933 + struct buffer_head *root_bh,
1934 + u64 blkno,
1935 + u32 cpos,
1936 + u32 len)
1937 +{
1938 + int ret;
1939 + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1940 + struct inode *tl_inode = osb->osb_tl_inode;
1941 + handle_t *handle;
1942 + struct ocfs2_xattr_block *xb =
1943 + (struct ocfs2_xattr_block *)root_bh->b_data;
1944 + struct ocfs2_extent_list *root_el = &xb->xb_attrs.xb_root.xt_list;
1945 + struct ocfs2_alloc_context *meta_ac = NULL;
1946 + struct ocfs2_cached_dealloc_ctxt dealloc;
1947 +
1948 + ocfs2_init_dealloc_ctxt(&dealloc);
1949 +
1950 + mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
1951 + cpos, len, (unsigned long long)blkno);
1952 +
1953 + ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
1954 +
1955 + ret = ocfs2_lock_allocators(inode, root_bh, root_el,
1956 + 0, 1, NULL, &meta_ac,
1957 + OCFS2_XATTR_TREE_EXTENT, NULL);
1958 + if (ret) {
1959 + mlog_errno(ret);
1960 + return ret;
1961 + }
1962 +
1963 + mutex_lock(&tl_inode->i_mutex);
1964 +
1965 + if (ocfs2_truncate_log_needs_flush(osb)) {
1966 + ret = __ocfs2_flush_truncate_log(osb);
1967 + if (ret < 0) {
1968 + mlog_errno(ret);
1969 + goto out;
1970 + }
1971 + }
1972 +
1973 + handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
1974 + if (handle == NULL) {
1975 + ret = -ENOMEM;
1976 + mlog_errno(ret);
1977 + goto out;
1978 + }
1979 +
1980 + ret = ocfs2_journal_access(handle, inode, root_bh,
1981 + OCFS2_JOURNAL_ACCESS_WRITE);
1982 + if (ret) {
1983 + mlog_errno(ret);
1984 + goto out_commit;
1985 + }
1986 +
1987 + ret = ocfs2_remove_extent(inode, root_bh, cpos, len, handle, meta_ac,
1988 + &dealloc, OCFS2_XATTR_TREE_EXTENT, NULL);
1989 + if (ret) {
1990 + mlog_errno(ret);
1991 + goto out_commit;
1992 + }
1993 +
1994 + le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
1995 +
1996 + ret = ocfs2_journal_dirty(handle, root_bh);
1997 + if (ret) {
1998 + mlog_errno(ret);
1999 + goto out_commit;
2000 + }
2001 +
2002 + ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
2003 + if (ret)
2004 + mlog_errno(ret);
2005 +
2006 +out_commit:
2007 + ocfs2_commit_trans(osb, handle);
2008 +out:
2009 + ocfs2_schedule_truncate_log_flush(osb, 1);
2010 +
2011 + mutex_unlock(&tl_inode->i_mutex);
2012 +
2013 + if (meta_ac)
2014 + ocfs2_free_alloc_context(meta_ac);
2015 +
2016 + ocfs2_run_deallocs(osb, &dealloc);
2017 +
2018 + return ret;
2019 +}
2020 +
2021 +/*
2022 + * Free the xattr bucket indicated by xs->bucket and if all the buckets
2023 + * in the clusters is free, free the clusters also.
2024 + */
2025 +static int ocfs2_xattr_bucket_shrink(struct inode *inode,
2026 + struct ocfs2_xattr_info *xi,
2027 + struct ocfs2_xattr_search *xs,
2028 + u32 name_hash)
2029 +{
2030 + int ret;
2031 + u32 e_cpos, num_clusters;
2032 + u64 p_blkno;
2033 + struct buffer_head *first_bh = NULL;
2034 + struct ocfs2_xattr_header *first_xh;
2035 + struct ocfs2_xattr_block *xb =
2036 + (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2037 +
2038 + BUG_ON(xs->header->xh_count != 0);
2039 +
2040 + ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2041 + &e_cpos, &num_clusters,
2042 + &xb->xb_attrs.xb_root.xt_list);
2043 + if (ret) {
2044 + mlog_errno(ret);
2045 + return ret;
2046 + }
2047 +
2048 + ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2049 + &first_bh, OCFS2_BH_CACHED, inode);
2050 + if (ret) {
2051 + mlog_errno(ret);
2052 + return ret;
2053 + }
2054 +
2055 + ret = ocfs2_rm_xattr_bucket(inode, first_bh, &xs->bucket);
2056 + if (ret) {
2057 + mlog_errno(ret);
2058 + goto out;
2059 + }
2060 +
2061 + first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
2062 + if (first_xh->xh_num_buckets == 0)
2063 + ret = ocfs2_rm_xattr_cluster(inode, xs->xattr_bh,
2064 + p_blkno, e_cpos,
2065 + num_clusters);
2066 +
2067 +out:
2068 + brelse(first_bh);
2069 + return ret;
2070 +}
2071 +
2072 +static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
2073 + struct ocfs2_xattr_search *xs)
2074 +{
2075 + handle_t *handle = NULL;
2076 + struct ocfs2_xattr_header *xh = xs->bucket.xh;
2077 + struct ocfs2_xattr_entry *last = &xh->xh_entries[
2078 + le16_to_cpu(xh->xh_count) - 1];
2079 + int ret = 0;
2080 +
2081 + handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
2082 + if (IS_ERR(handle)) {
2083 + ret = PTR_ERR(handle);
2084 + mlog_errno(ret);
2085 + return;
2086 + }
2087 +
2088 + ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
2089 + OCFS2_JOURNAL_ACCESS_WRITE);
2090 + if (ret) {
2091 + mlog_errno(ret);
2092 + goto out_commit;
2093 + }
2094 +
2095 + /* Remove the old entry. */
2096 + memmove(xs->here, xs->here + 1,
2097 + (void *)last - (void *)xs->here);
2098 + memset(last, 0, sizeof(struct ocfs2_xattr_entry));
2099 + le16_add_cpu(&xh->xh_count, -1);
2100 +
2101 + ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
2102 + if (ret < 0)
2103 + mlog_errno(ret);
2104 +out_commit:
2105 + ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2106 +}
2107 +
2108 +/*
2109 + * Set the xattr name/value in the bucket specified in xs.
2110 + *
2111 + * As the new value in xi may be stored in the bucket or in an outside cluster,
2112 + * we divide the whole process into 3 steps:
2113 + * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
2114 + * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
2115 + * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
2116 + * 4. If the clusters for the new outside value can't be allocated, we need
2117 + * to free the xattr we allocated in set.
2118 + */
2119 +static int ocfs2_xattr_set_in_bucket(struct inode *inode,
2120 + struct ocfs2_xattr_info *xi,
2121 + struct ocfs2_xattr_search *xs)
2122 +{
2123 + int ret, local = 1, bucket_empty = 0;
2124 + size_t value_len;
2125 + char *val = (char *)xi->value;
2126 + struct ocfs2_xattr_entry *xe = xs->here;
2127 + u32 name_hash = ocfs2_xattr_hash_by_name(inode,
2128 + xi->name_index, xi->name);
2129 +
2130 + if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
2131 + /*
2132 + * We need to truncate the xattr storage first.
2133 + *
2134 + * If both the old and new value are stored to
2135 + * outside block, we only need to truncate
2136 + * the storage and then set the value outside.
2137 + *
2138 + * If the new value should be stored within block,
2139 + * we should free all the outside block first and
2140 + * the modification to the xattr block will be done
2141 + * by following steps.
2142 + */
2143 + if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2144 + value_len = xi->value_len;
2145 + else
2146 + value_len = 0;
2147 +
2148 + ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
2149 + value_len);
2150 + if (ret)
2151 + goto out;
2152 +
2153 + if (value_len)
2154 + goto set_value_outside;
2155 + }
2156 +
2157 + value_len = xi->value_len;
2158 + /* So we have to handle the inside block change now. */
2159 + if (value_len > OCFS2_XATTR_INLINE_SIZE) {
2160 + /*
2161 + * If the new value will be stored outside of block,
2162 + * initalize a new empty value root and insert it first.
2163 + */
2164 + local = 0;
2165 + xi->value = &def_xv;
2166 + xi->value_len = OCFS2_XATTR_ROOT_SIZE;
2167 + }
2168 +
2169 + ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash,
2170 + local, &bucket_empty);
2171 + if (ret) {
2172 + mlog_errno(ret);
2173 + goto out;
2174 + }
2175 +
2176 + if (value_len > OCFS2_XATTR_INLINE_SIZE) {
2177 + /* allocate the space now for the outside block storage. */
2178 + ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
2179 + value_len);
2180 + if (ret) {
2181 + mlog_errno(ret);
2182 +
2183 + if (xs->not_found) {
2184 + /*
2185 + * We can't allocate enough clusters for outside
2186 + * storage and we have allocated xattr already,
2187 + * so need to remove it.
2188 + */
2189 + ocfs2_xattr_bucket_remove_xs(inode, xs);
2190 + }
2191 + goto out;
2192 + }
2193 + } else {
2194 + if (bucket_empty)
2195 + ret = ocfs2_xattr_bucket_shrink(inode, xi,
2196 + xs, name_hash);
2197 + goto out;
2198 + }
2199 +
2200 +set_value_outside:
2201 + ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
2202 +out:
2203 + return ret;
2204 +}
2205 +
2206 +/* check whether the xattr bucket is filled up with the same hash value. */
2207 +static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
2208 + struct ocfs2_xattr_bucket *bucket)
2209 +{
2210 + struct ocfs2_xattr_header *xh = bucket->xh;
2211 +
2212 + if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
2213 + xh->xh_entries[0].xe_name_hash) {
2214 + mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
2215 + "hash = %u\n",
2216 + (unsigned long long)bucket->bhs[0]->b_blocknr,
2217 + le32_to_cpu(xh->xh_entries[0].xe_name_hash));
2218 + return -ENOSPC;
2219 + }
2220 +
2221 + return 0;
2222 +}
2223 +
2224 +static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
2225 + struct ocfs2_xattr_info *xi,
2226 + struct ocfs2_xattr_search *xs)
2227 +{
2228 + struct ocfs2_xattr_header *xh;
2229 + struct ocfs2_xattr_entry *xe;
2230 + u16 count, header_size, xh_free_start;
2231 + int i, free, max_free, need, old;
2232 + size_t value_size = 0, name_len = strlen(xi->name);
2233 + size_t blocksize = inode->i_sb->s_blocksize;
2234 + int ret, allocation = 0;
2235 + u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2236 +
2237 + mlog_entry("Set xattr %s in xattr index block\n", xi->name);
2238 +
2239 +try_again:
2240 + xh = xs->header;
2241 + count = le16_to_cpu(xh->xh_count);
2242 + xh_free_start = le16_to_cpu(xh->xh_free_start);
2243 + header_size = sizeof(struct ocfs2_xattr_header) +
2244 + count * sizeof(struct ocfs2_xattr_entry);
2245 + max_free = OCFS2_XATTR_BUCKET_SIZE -
2246 + le16_to_cpu(xh->xh_name_value_len) - header_size;
2247 +
2248 + mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
2249 + "of %u which exceed block size\n",
2250 + (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
2251 + header_size);
2252 +
2253 + if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
2254 + value_size = OCFS2_XATTR_ROOT_SIZE;
2255 + else if (xi->value)
2256 + value_size = OCFS2_XATTR_SIZE(xi->value_len);
2257 +
2258 + if (xs->not_found)
2259 + need = sizeof(struct ocfs2_xattr_entry) +
2260 + OCFS2_XATTR_SIZE(name_len) + value_size;
2261 + else {
2262 + need = value_size + OCFS2_XATTR_SIZE(name_len);
2263 +
2264 + /*
2265 + * We only replace the old value if the new length is smaller
2266 + * than the old one. Otherwise we will allocate new space in the
2267 + * bucket to store it.
2268 + */
2269 + xe = xs->here;
2270 + if (ocfs2_xattr_is_local(xe))
2271 + old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
2272 + else
2273 + old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
2274 +
2275 + if (old >= value_size)
2276 + need = 0;
2277 + }
2278 +
2279 + free = xh_free_start - header_size;
2280 + /*
2281 + * We need to make sure the new name/value pair
2282 + * can exist in the same block.
2283 + */
2284 + if (xh_free_start % blocksize < need)
2285 + free -= xh_free_start % blocksize;
2286 +
2287 + mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
2288 + "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
2289 + " %u\n", xs->not_found,
2290 + (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
2291 + free, need, max_free, le16_to_cpu(xh->xh_free_start),
2292 + le16_to_cpu(xh->xh_name_value_len));
2293 +
2294 + if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
2295 + if (need <= max_free &&
2296 + count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
2297 + /*
2298 + * We can create the space by defragment. Since only the
2299 + * name/value will be moved, the xe shouldn't be changed
2300 + * in xs.
2301 + */
2302 + ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
2303 + if (ret) {
2304 + mlog_errno(ret);
2305 + goto out;
2306 + }
2307 +
2308 + xh_free_start = le16_to_cpu(xh->xh_free_start);
2309 + free = xh_free_start - header_size;
2310 + if (xh_free_start % blocksize < need)
2311 + free -= xh_free_start % blocksize;
2312 +
2313 + if (free >= need)
2314 + goto xattr_set;
2315 +
2316 + mlog(0, "Can't get enough space for xattr insert by "
2317 + "defragment. Need %u bytes, but we have %d, so "
2318 + "allocate new bucket for it.\n", need, free);
2319 + }
2320 +
2321 + /*
2322 + * We have to add new buckets or clusters and one
2323 + * allocation should leave us enough space for insert.
2324 + */
2325 + BUG_ON(allocation);
2326 +
2327 + /*
2328 + * We do not allow for overlapping ranges between buckets. And
2329 + * the maximum number of collisions we will allow for then is
2330 + * one bucket's worth, so check it here whether we need to
2331 + * add a new bucket for the insert.
2332 + */
2333 + ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
2334 + if (ret) {
2335 + mlog_errno(ret);
2336 + goto out;
2337 + }
2338 +
2339 + ret = ocfs2_add_new_xattr_bucket(inode,
2340 + xs->xattr_bh,
2341 + xs->bucket.bhs[0]);
2342 + if (ret) {
2343 + mlog_errno(ret);
2344 + goto out;
2345 + }
2346 +
2347 + for (i = 0; i < blk_per_bucket; i++)
2348 + brelse(xs->bucket.bhs[i]);
2349 +
2350 + memset(&xs->bucket, 0, sizeof(xs->bucket));
2351 +
2352 + ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
2353 + xi->name_index,
2354 + xi->name, xs);
2355 + if (ret && ret != -ENODATA)
2356 + goto out;
2357 + xs->not_found = ret;
2358 + allocation = 1;
2359 + goto try_again;
2360 + }
2361 +
2362 +xattr_set:
2363 + ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
2364 +out:
2365 + mlog_exit(ret);
2366 + return ret;
2367 +}
2368 diff --git a/fs/ocfs2/xattr.h b/fs/ocfs2/xattr.h
2369 index a69c8aa..d33dbe5 100644
2370 --- a/fs/ocfs2/xattr.h
2371 +++ b/fs/ocfs2/xattr.h
2372 @@ -64,4 +64,12 @@ static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
2373 {
2374 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
2375 }
2376 +
2377 +static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
2378 +{
2379 + u16 len = sb->s_blocksize -
2380 + offsetof(struct ocfs2_xattr_header, xh_entries);
2381 +
2382 + return len / sizeof(struct ocfs2_xattr_entry);
2383 +}
2384 #endif /* OCFS2_XATTR_H */
2385 --
2386 1.5.4.5
2387