blob: 38e3e5e216bdb460620453bec4996371c218cc9b [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 *
6 * Copyright (C) 2008 Oracle. All rights reserved.
7 *
Tiger Yangcf1d6c72008-08-18 17:11:00 +08008 * CREDITS:
9 * Lots of code in this file is taken from ext3.
10 *
Tao Maf56654c2008-08-18 17:38:48 +080011 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
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.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
Tiger Yangcf1d6c72008-08-18 17:11:00 +080027#include <linux/capability.h>
28#include <linux/fs.h>
29#include <linux/types.h>
30#include <linux/slab.h>
31#include <linux/highmem.h>
32#include <linux/pagemap.h>
33#include <linux/uio.h>
34#include <linux/sched.h>
35#include <linux/splice.h>
36#include <linux/mount.h>
37#include <linux/writeback.h>
38#include <linux/falloc.h>
Tao Ma01225592008-08-18 17:38:53 +080039#include <linux/sort.h>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080040
Tao Maf56654c2008-08-18 17:38:48 +080041#define MLOG_MASK_PREFIX ML_XATTR
42#include <cluster/masklog.h>
43
44#include "ocfs2.h"
45#include "alloc.h"
46#include "dlmglue.h"
47#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080048#include "symlink.h"
49#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080050#include "inode.h"
51#include "journal.h"
52#include "ocfs2_fs.h"
53#include "suballoc.h"
54#include "uptodate.h"
55#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080056#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080057#include "xattr.h"
58
59
60struct ocfs2_xattr_def_value_root {
61 struct ocfs2_xattr_value_root xv;
62 struct ocfs2_extent_rec er;
63};
64
Tao Ma0c044f02008-08-18 17:38:50 +080065struct ocfs2_xattr_bucket {
66 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
67 struct ocfs2_xattr_header *xh;
68};
69
Tiger Yangcf1d6c72008-08-18 17:11:00 +080070#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
71#define OCFS2_XATTR_INLINE_SIZE 80
72
73static struct ocfs2_xattr_def_value_root def_xv = {
74 .xv.xr_list.l_count = cpu_to_le16(1),
75};
76
77struct xattr_handler *ocfs2_xattr_handlers[] = {
78 &ocfs2_xattr_user_handler,
79 &ocfs2_xattr_trusted_handler,
80 NULL
81};
82
83static struct xattr_handler *ocfs2_xattr_handler_map[] = {
84 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
85 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
86};
87
88struct ocfs2_xattr_info {
89 int name_index;
90 const char *name;
91 const void *value;
92 size_t value_len;
93};
94
95struct ocfs2_xattr_search {
96 struct buffer_head *inode_bh;
97 /*
98 * xattr_bh point to the block buffer head which has extended attribute
99 * when extended attribute in inode, xattr_bh is equal to inode_bh.
100 */
101 struct buffer_head *xattr_bh;
102 struct ocfs2_xattr_header *header;
Tao Ma589dc262008-08-18 17:38:51 +0800103 struct ocfs2_xattr_bucket bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800104 void *base;
105 void *end;
106 struct ocfs2_xattr_entry *here;
107 int not_found;
108};
109
Tao Ma589dc262008-08-18 17:38:51 +0800110static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
111 struct ocfs2_xattr_header *xh,
112 int index,
113 int *block_off,
114 int *new_offset);
115
116static int ocfs2_xattr_index_block_find(struct inode *inode,
117 struct buffer_head *root_bh,
118 int name_index,
119 const char *name,
120 struct ocfs2_xattr_search *xs);
121
Tao Ma0c044f02008-08-18 17:38:50 +0800122static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
123 struct ocfs2_xattr_tree_root *xt,
124 char *buffer,
125 size_t buffer_size);
126
Tao Ma01225592008-08-18 17:38:53 +0800127static int ocfs2_xattr_create_index_block(struct inode *inode,
128 struct ocfs2_xattr_search *xs);
129
130static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
131 struct ocfs2_xattr_info *xi,
132 struct ocfs2_xattr_search *xs);
133
Tao Maa3944252008-08-18 17:38:54 +0800134static int ocfs2_delete_xattr_index_block(struct inode *inode,
135 struct buffer_head *xb_bh);
136
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800137static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
138{
139 struct xattr_handler *handler = NULL;
140
141 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
142 handler = ocfs2_xattr_handler_map[name_index];
143
144 return handler;
145}
146
147static inline u32 ocfs2_xattr_name_hash(struct inode *inode,
148 char *prefix,
149 int prefix_len,
150 char *name,
151 int name_len)
152{
153 /* Get hash value of uuid from super block */
154 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
155 int i;
156
157 /* hash extended attribute prefix */
158 for (i = 0; i < prefix_len; i++) {
159 hash = (hash << OCFS2_HASH_SHIFT) ^
160 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
161 *prefix++;
162 }
163 /* hash extended attribute name */
164 for (i = 0; i < name_len; i++) {
165 hash = (hash << OCFS2_HASH_SHIFT) ^
166 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
167 *name++;
168 }
169
170 return hash;
171}
172
173/*
174 * ocfs2_xattr_hash_entry()
175 *
176 * Compute the hash of an extended attribute.
177 */
178static void ocfs2_xattr_hash_entry(struct inode *inode,
179 struct ocfs2_xattr_header *header,
180 struct ocfs2_xattr_entry *entry)
181{
182 u32 hash = 0;
183 struct xattr_handler *handler =
184 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
185 char *prefix = handler->prefix;
186 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
187 int prefix_len = strlen(handler->prefix);
188
189 hash = ocfs2_xattr_name_hash(inode, prefix, prefix_len, name,
190 entry->xe_name_len);
191 entry->xe_name_hash = cpu_to_le32(hash);
192
193 return;
194}
Tao Maf56654c2008-08-18 17:38:48 +0800195
196static int ocfs2_xattr_extend_allocation(struct inode *inode,
197 u32 clusters_to_add,
198 struct buffer_head *xattr_bh,
199 struct ocfs2_xattr_value_root *xv)
200{
201 int status = 0;
202 int restart_func = 0;
203 int credits = 0;
204 handle_t *handle = NULL;
205 struct ocfs2_alloc_context *data_ac = NULL;
206 struct ocfs2_alloc_context *meta_ac = NULL;
207 enum ocfs2_alloc_restarted why;
208 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Maf56654c2008-08-18 17:38:48 +0800209 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700210 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800211
212 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
213
Joel Becker8d6220d2008-08-22 12:46:09 -0700214 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700215
Tao Maf56654c2008-08-18 17:38:48 +0800216restart_all:
217
Joel Beckerf99b9b72008-08-20 19:36:33 -0700218 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
219 &data_ac, &meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +0800220 if (status) {
221 mlog_errno(status);
222 goto leave;
223 }
224
Joel Beckerf99b9b72008-08-20 19:36:33 -0700225 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
226 clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800227 handle = ocfs2_start_trans(osb, credits);
228 if (IS_ERR(handle)) {
229 status = PTR_ERR(handle);
230 handle = NULL;
231 mlog_errno(status);
232 goto leave;
233 }
234
235restarted_transaction:
236 status = ocfs2_journal_access(handle, inode, xattr_bh,
237 OCFS2_JOURNAL_ACCESS_WRITE);
238 if (status < 0) {
239 mlog_errno(status);
240 goto leave;
241 }
242
243 prev_clusters = le32_to_cpu(xv->xr_clusters);
244 status = ocfs2_add_clusters_in_btree(osb,
245 inode,
246 &logical_start,
247 clusters_to_add,
248 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700249 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800250 handle,
251 data_ac,
252 meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700253 &why);
Tao Maf56654c2008-08-18 17:38:48 +0800254 if ((status < 0) && (status != -EAGAIN)) {
255 if (status != -ENOSPC)
256 mlog_errno(status);
257 goto leave;
258 }
259
260 status = ocfs2_journal_dirty(handle, xattr_bh);
261 if (status < 0) {
262 mlog_errno(status);
263 goto leave;
264 }
265
266 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
267
268 if (why != RESTART_NONE && clusters_to_add) {
269 if (why == RESTART_META) {
270 mlog(0, "restarting function.\n");
271 restart_func = 1;
272 } else {
273 BUG_ON(why != RESTART_TRANS);
274
275 mlog(0, "restarting transaction.\n");
276 /* TODO: This can be more intelligent. */
277 credits = ocfs2_calc_extend_credits(osb->sb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700278 et.et_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800279 clusters_to_add);
280 status = ocfs2_extend_trans(handle, credits);
281 if (status < 0) {
282 /* handle still has to be committed at
283 * this point. */
284 status = -ENOMEM;
285 mlog_errno(status);
286 goto leave;
287 }
288 goto restarted_transaction;
289 }
290 }
291
292leave:
293 if (handle) {
294 ocfs2_commit_trans(osb, handle);
295 handle = NULL;
296 }
297 if (data_ac) {
298 ocfs2_free_alloc_context(data_ac);
299 data_ac = NULL;
300 }
301 if (meta_ac) {
302 ocfs2_free_alloc_context(meta_ac);
303 meta_ac = NULL;
304 }
305 if ((!status) && restart_func) {
306 restart_func = 0;
307 goto restart_all;
308 }
309
310 return status;
311}
312
313static int __ocfs2_remove_xattr_range(struct inode *inode,
314 struct buffer_head *root_bh,
315 struct ocfs2_xattr_value_root *xv,
316 u32 cpos, u32 phys_cpos, u32 len,
317 struct ocfs2_cached_dealloc_ctxt *dealloc)
318{
319 int ret;
320 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
321 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
322 struct inode *tl_inode = osb->osb_tl_inode;
323 handle_t *handle;
324 struct ocfs2_alloc_context *meta_ac = NULL;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700325 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800326
Joel Becker8d6220d2008-08-22 12:46:09 -0700327 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700328
329 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +0800330 if (ret) {
331 mlog_errno(ret);
332 return ret;
333 }
334
335 mutex_lock(&tl_inode->i_mutex);
336
337 if (ocfs2_truncate_log_needs_flush(osb)) {
338 ret = __ocfs2_flush_truncate_log(osb);
339 if (ret < 0) {
340 mlog_errno(ret);
341 goto out;
342 }
343 }
344
345 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
346 if (IS_ERR(handle)) {
347 ret = PTR_ERR(handle);
348 mlog_errno(ret);
349 goto out;
350 }
351
352 ret = ocfs2_journal_access(handle, inode, root_bh,
353 OCFS2_JOURNAL_ACCESS_WRITE);
354 if (ret) {
355 mlog_errno(ret);
356 goto out_commit;
357 }
358
Joel Beckerf99b9b72008-08-20 19:36:33 -0700359 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
360 dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800361 if (ret) {
362 mlog_errno(ret);
363 goto out_commit;
364 }
365
366 le32_add_cpu(&xv->xr_clusters, -len);
367
368 ret = ocfs2_journal_dirty(handle, root_bh);
369 if (ret) {
370 mlog_errno(ret);
371 goto out_commit;
372 }
373
374 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
375 if (ret)
376 mlog_errno(ret);
377
378out_commit:
379 ocfs2_commit_trans(osb, handle);
380out:
381 mutex_unlock(&tl_inode->i_mutex);
382
383 if (meta_ac)
384 ocfs2_free_alloc_context(meta_ac);
385
386 return ret;
387}
388
389static int ocfs2_xattr_shrink_size(struct inode *inode,
390 u32 old_clusters,
391 u32 new_clusters,
392 struct buffer_head *root_bh,
393 struct ocfs2_xattr_value_root *xv)
394{
395 int ret = 0;
396 u32 trunc_len, cpos, phys_cpos, alloc_size;
397 u64 block;
398 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
399 struct ocfs2_cached_dealloc_ctxt dealloc;
400
401 ocfs2_init_dealloc_ctxt(&dealloc);
402
403 if (old_clusters <= new_clusters)
404 return 0;
405
406 cpos = new_clusters;
407 trunc_len = old_clusters - new_clusters;
408 while (trunc_len) {
409 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
410 &alloc_size, &xv->xr_list);
411 if (ret) {
412 mlog_errno(ret);
413 goto out;
414 }
415
416 if (alloc_size > trunc_len)
417 alloc_size = trunc_len;
418
419 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
420 phys_cpos, alloc_size,
421 &dealloc);
422 if (ret) {
423 mlog_errno(ret);
424 goto out;
425 }
426
427 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
428 ocfs2_remove_xattr_clusters_from_cache(inode, block,
429 alloc_size);
430 cpos += alloc_size;
431 trunc_len -= alloc_size;
432 }
433
434out:
435 ocfs2_schedule_truncate_log_flush(osb, 1);
436 ocfs2_run_deallocs(osb, &dealloc);
437
438 return ret;
439}
440
441static int ocfs2_xattr_value_truncate(struct inode *inode,
442 struct buffer_head *root_bh,
443 struct ocfs2_xattr_value_root *xv,
444 int len)
445{
446 int ret;
447 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
448 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
449
450 if (new_clusters == old_clusters)
451 return 0;
452
453 if (new_clusters > old_clusters)
454 ret = ocfs2_xattr_extend_allocation(inode,
455 new_clusters - old_clusters,
456 root_bh, xv);
457 else
458 ret = ocfs2_xattr_shrink_size(inode,
459 old_clusters, new_clusters,
460 root_bh, xv);
461
462 return ret;
463}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800464
465static int ocfs2_xattr_list_entries(struct inode *inode,
466 struct ocfs2_xattr_header *header,
467 char *buffer, size_t buffer_size)
468{
469 size_t rest = buffer_size;
470 int i;
471
472 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
473 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
474 struct xattr_handler *handler =
475 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
476
477 if (handler) {
478 size_t size = handler->list(inode, buffer, rest,
479 ((char *)header +
480 le16_to_cpu(entry->xe_name_offset)),
481 entry->xe_name_len);
482 if (buffer) {
483 if (size > rest)
484 return -ERANGE;
485 buffer += size;
486 }
487 rest -= size;
488 }
489 }
490
491 return buffer_size - rest;
492}
493
494static int ocfs2_xattr_ibody_list(struct inode *inode,
495 struct ocfs2_dinode *di,
496 char *buffer,
497 size_t buffer_size)
498{
499 struct ocfs2_xattr_header *header = NULL;
500 struct ocfs2_inode_info *oi = OCFS2_I(inode);
501 int ret = 0;
502
503 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
504 return ret;
505
506 header = (struct ocfs2_xattr_header *)
507 ((void *)di + inode->i_sb->s_blocksize -
508 le16_to_cpu(di->i_xattr_inline_size));
509
510 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
511
512 return ret;
513}
514
515static int ocfs2_xattr_block_list(struct inode *inode,
516 struct ocfs2_dinode *di,
517 char *buffer,
518 size_t buffer_size)
519{
520 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800521 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800522 int ret = 0;
523
524 if (!di->i_xattr_loc)
525 return ret;
526
527 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
528 le64_to_cpu(di->i_xattr_loc),
529 &blk_bh, OCFS2_BH_CACHED, inode);
530 if (ret < 0) {
531 mlog_errno(ret);
532 return ret;
533 }
534 /*Verify the signature of xattr block*/
535 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
536 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
537 ret = -EFAULT;
538 goto cleanup;
539 }
540
Tao Ma0c044f02008-08-18 17:38:50 +0800541 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800542
Tao Ma0c044f02008-08-18 17:38:50 +0800543 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
544 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
545 ret = ocfs2_xattr_list_entries(inode, header,
546 buffer, buffer_size);
547 } else {
548 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
549 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
550 buffer, buffer_size);
551 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800552cleanup:
553 brelse(blk_bh);
554
555 return ret;
556}
557
558ssize_t ocfs2_listxattr(struct dentry *dentry,
559 char *buffer,
560 size_t size)
561{
562 int ret = 0, i_ret = 0, b_ret = 0;
563 struct buffer_head *di_bh = NULL;
564 struct ocfs2_dinode *di = NULL;
565 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
566
Tiger Yang8154da32008-08-18 17:11:46 +0800567 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
568 return -EOPNOTSUPP;
569
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800570 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
571 return ret;
572
573 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
574 if (ret < 0) {
575 mlog_errno(ret);
576 return ret;
577 }
578
579 di = (struct ocfs2_dinode *)di_bh->b_data;
580
581 down_read(&oi->ip_xattr_sem);
582 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
583 if (i_ret < 0)
584 b_ret = 0;
585 else {
586 if (buffer) {
587 buffer += i_ret;
588 size -= i_ret;
589 }
590 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
591 buffer, size);
592 if (b_ret < 0)
593 i_ret = 0;
594 }
595 up_read(&oi->ip_xattr_sem);
596 ocfs2_inode_unlock(dentry->d_inode, 0);
597
598 brelse(di_bh);
599
600 return i_ret + b_ret;
601}
602
603static int ocfs2_xattr_find_entry(int name_index,
604 const char *name,
605 struct ocfs2_xattr_search *xs)
606{
607 struct ocfs2_xattr_entry *entry;
608 size_t name_len;
609 int i, cmp = 1;
610
611 if (name == NULL)
612 return -EINVAL;
613
614 name_len = strlen(name);
615 entry = xs->here;
616 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
617 cmp = name_index - ocfs2_xattr_get_type(entry);
618 if (!cmp)
619 cmp = name_len - entry->xe_name_len;
620 if (!cmp)
621 cmp = memcmp(name, (xs->base +
622 le16_to_cpu(entry->xe_name_offset)),
623 name_len);
624 if (cmp == 0)
625 break;
626 entry += 1;
627 }
628 xs->here = entry;
629
630 return cmp ? -ENODATA : 0;
631}
632
633static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800634 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800635 void *buffer,
636 size_t len)
637{
638 u32 cpos, p_cluster, num_clusters, bpc, clusters;
639 u64 blkno;
640 int i, ret = 0;
641 size_t cplen, blocksize;
642 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800643 struct ocfs2_extent_list *el;
644
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800645 el = &xv->xr_list;
646 clusters = le32_to_cpu(xv->xr_clusters);
647 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
648 blocksize = inode->i_sb->s_blocksize;
649
650 cpos = 0;
651 while (cpos < clusters) {
652 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
653 &num_clusters, el);
654 if (ret) {
655 mlog_errno(ret);
656 goto out;
657 }
658
659 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
660 /* Copy ocfs2_xattr_value */
661 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
662 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
663 &bh, OCFS2_BH_CACHED, inode);
664 if (ret) {
665 mlog_errno(ret);
666 goto out;
667 }
668
669 cplen = len >= blocksize ? blocksize : len;
670 memcpy(buffer, bh->b_data, cplen);
671 len -= cplen;
672 buffer += cplen;
673
674 brelse(bh);
675 bh = NULL;
676 if (len == 0)
677 break;
678 }
679 cpos += num_clusters;
680 }
681out:
682 return ret;
683}
684
685static int ocfs2_xattr_ibody_get(struct inode *inode,
686 int name_index,
687 const char *name,
688 void *buffer,
689 size_t buffer_size,
690 struct ocfs2_xattr_search *xs)
691{
692 struct ocfs2_inode_info *oi = OCFS2_I(inode);
693 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800694 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800695 size_t size;
696 int ret = 0;
697
698 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
699 return -ENODATA;
700
701 xs->end = (void *)di + inode->i_sb->s_blocksize;
702 xs->header = (struct ocfs2_xattr_header *)
703 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
704 xs->base = (void *)xs->header;
705 xs->here = xs->header->xh_entries;
706
707 ret = ocfs2_xattr_find_entry(name_index, name, xs);
708 if (ret)
709 return ret;
710 size = le64_to_cpu(xs->here->xe_value_size);
711 if (buffer) {
712 if (size > buffer_size)
713 return -ERANGE;
714 if (ocfs2_xattr_is_local(xs->here)) {
715 memcpy(buffer, (void *)xs->base +
716 le16_to_cpu(xs->here->xe_name_offset) +
717 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
718 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800719 xv = (struct ocfs2_xattr_value_root *)
720 (xs->base + le16_to_cpu(
721 xs->here->xe_name_offset) +
722 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
723 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800724 buffer, size);
725 if (ret < 0) {
726 mlog_errno(ret);
727 return ret;
728 }
729 }
730 }
731
732 return size;
733}
734
735static int ocfs2_xattr_block_get(struct inode *inode,
736 int name_index,
737 const char *name,
738 void *buffer,
739 size_t buffer_size,
740 struct ocfs2_xattr_search *xs)
741{
742 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
743 struct buffer_head *blk_bh = NULL;
744 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +0800745 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800746 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +0800747 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800748
749 if (!di->i_xattr_loc)
750 return ret;
751
Tao Ma589dc262008-08-18 17:38:51 +0800752 memset(&xs->bucket, 0, sizeof(xs->bucket));
753
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800754 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
755 le64_to_cpu(di->i_xattr_loc),
756 &blk_bh, OCFS2_BH_CACHED, inode);
757 if (ret < 0) {
758 mlog_errno(ret);
759 return ret;
760 }
761 /*Verify the signature of xattr block*/
762 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
763 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
764 ret = -EFAULT;
765 goto cleanup;
766 }
767
768 xs->xattr_bh = blk_bh;
769 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800770
Tao Ma589dc262008-08-18 17:38:51 +0800771 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
772 xs->header = &xb->xb_attrs.xb_header;
773 xs->base = (void *)xs->header;
774 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
775 xs->here = xs->header->xh_entries;
776
777 ret = ocfs2_xattr_find_entry(name_index, name, xs);
778 } else
779 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
780 name_index,
781 name, xs);
782
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800783 if (ret)
784 goto cleanup;
785 size = le64_to_cpu(xs->here->xe_value_size);
786 if (buffer) {
787 ret = -ERANGE;
788 if (size > buffer_size)
789 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +0800790
791 name_offset = le16_to_cpu(xs->here->xe_name_offset);
792 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
793 i = xs->here - xs->header->xh_entries;
794
795 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
796 ret = ocfs2_xattr_bucket_get_name_value(inode,
797 xs->bucket.xh,
798 i,
799 &block_off,
800 &name_offset);
801 xs->base = xs->bucket.bhs[block_off]->b_data;
802 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800803 if (ocfs2_xattr_is_local(xs->here)) {
804 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +0800805 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800806 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800807 xv = (struct ocfs2_xattr_value_root *)
808 (xs->base + name_offset + name_len);
809 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800810 buffer, size);
811 if (ret < 0) {
812 mlog_errno(ret);
813 goto cleanup;
814 }
815 }
816 }
817 ret = size;
818cleanup:
Tao Ma589dc262008-08-18 17:38:51 +0800819 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
820 brelse(xs->bucket.bhs[i]);
821 memset(&xs->bucket, 0, sizeof(xs->bucket));
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800822
Tao Ma589dc262008-08-18 17:38:51 +0800823 brelse(blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800824 return ret;
825}
826
827/* ocfs2_xattr_get()
828 *
829 * Copy an extended attribute into the buffer provided.
830 * Buffer is NULL to compute the size of buffer required.
831 */
832int ocfs2_xattr_get(struct inode *inode,
833 int name_index,
834 const char *name,
835 void *buffer,
836 size_t buffer_size)
837{
838 int ret;
839 struct ocfs2_dinode *di = NULL;
840 struct buffer_head *di_bh = NULL;
841 struct ocfs2_inode_info *oi = OCFS2_I(inode);
842 struct ocfs2_xattr_search xis = {
843 .not_found = -ENODATA,
844 };
845 struct ocfs2_xattr_search xbs = {
846 .not_found = -ENODATA,
847 };
848
Tiger Yang8154da32008-08-18 17:11:46 +0800849 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
850 return -EOPNOTSUPP;
851
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800852 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
853 ret = -ENODATA;
854
855 ret = ocfs2_inode_lock(inode, &di_bh, 0);
856 if (ret < 0) {
857 mlog_errno(ret);
858 return ret;
859 }
860 xis.inode_bh = xbs.inode_bh = di_bh;
861 di = (struct ocfs2_dinode *)di_bh->b_data;
862
863 down_read(&oi->ip_xattr_sem);
864 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
865 buffer_size, &xis);
866 if (ret == -ENODATA)
867 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
868 buffer_size, &xbs);
869 up_read(&oi->ip_xattr_sem);
870 ocfs2_inode_unlock(inode, 0);
871
872 brelse(di_bh);
873
874 return ret;
875}
876
877static int __ocfs2_xattr_set_value_outside(struct inode *inode,
878 struct ocfs2_xattr_value_root *xv,
879 const void *value,
880 int value_len)
881{
882 int ret = 0, i, cp_len, credits;
883 u16 blocksize = inode->i_sb->s_blocksize;
884 u32 p_cluster, num_clusters;
885 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
886 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
887 u64 blkno;
888 struct buffer_head *bh = NULL;
889 handle_t *handle;
890
891 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
892
893 credits = clusters * bpc;
894 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
895 if (IS_ERR(handle)) {
896 ret = PTR_ERR(handle);
897 mlog_errno(ret);
898 goto out;
899 }
900
901 while (cpos < clusters) {
902 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
903 &num_clusters, &xv->xr_list);
904 if (ret) {
905 mlog_errno(ret);
906 goto out_commit;
907 }
908
909 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
910
911 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
912 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
913 &bh, OCFS2_BH_CACHED, inode);
914 if (ret) {
915 mlog_errno(ret);
916 goto out_commit;
917 }
918
919 ret = ocfs2_journal_access(handle,
920 inode,
921 bh,
922 OCFS2_JOURNAL_ACCESS_WRITE);
923 if (ret < 0) {
924 mlog_errno(ret);
925 goto out_commit;
926 }
927
928 cp_len = value_len > blocksize ? blocksize : value_len;
929 memcpy(bh->b_data, value, cp_len);
930 value_len -= cp_len;
931 value += cp_len;
932 if (cp_len < blocksize)
933 memset(bh->b_data + cp_len, 0,
934 blocksize - cp_len);
935
936 ret = ocfs2_journal_dirty(handle, bh);
937 if (ret < 0) {
938 mlog_errno(ret);
939 goto out_commit;
940 }
941 brelse(bh);
942 bh = NULL;
943
944 /*
945 * XXX: do we need to empty all the following
946 * blocks in this cluster?
947 */
948 if (!value_len)
949 break;
950 }
951 cpos += num_clusters;
952 }
953out_commit:
954 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
955out:
956 brelse(bh);
957
958 return ret;
959}
960
961static int ocfs2_xattr_cleanup(struct inode *inode,
962 struct ocfs2_xattr_info *xi,
963 struct ocfs2_xattr_search *xs,
964 size_t offs)
965{
966 handle_t *handle = NULL;
967 int ret = 0;
968 size_t name_len = strlen(xi->name);
969 void *val = xs->base + offs;
970 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
971
972 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
973 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
974 if (IS_ERR(handle)) {
975 ret = PTR_ERR(handle);
976 mlog_errno(ret);
977 goto out;
978 }
979 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
980 OCFS2_JOURNAL_ACCESS_WRITE);
981 if (ret) {
982 mlog_errno(ret);
983 goto out_commit;
984 }
985 /* Decrease xattr count */
986 le16_add_cpu(&xs->header->xh_count, -1);
987 /* Remove the xattr entry and tree root which has already be set*/
988 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
989 memset(val, 0, size);
990
991 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
992 if (ret < 0)
993 mlog_errno(ret);
994out_commit:
995 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
996out:
997 return ret;
998}
999
1000static int ocfs2_xattr_update_entry(struct inode *inode,
1001 struct ocfs2_xattr_info *xi,
1002 struct ocfs2_xattr_search *xs,
1003 size_t offs)
1004{
1005 handle_t *handle = NULL;
1006 int ret = 0;
1007
1008 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1009 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1010 if (IS_ERR(handle)) {
1011 ret = PTR_ERR(handle);
1012 mlog_errno(ret);
1013 goto out;
1014 }
1015 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1016 OCFS2_JOURNAL_ACCESS_WRITE);
1017 if (ret) {
1018 mlog_errno(ret);
1019 goto out_commit;
1020 }
1021
1022 xs->here->xe_name_offset = cpu_to_le16(offs);
1023 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1024 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1025 ocfs2_xattr_set_local(xs->here, 1);
1026 else
1027 ocfs2_xattr_set_local(xs->here, 0);
1028 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1029
1030 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1031 if (ret < 0)
1032 mlog_errno(ret);
1033out_commit:
1034 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1035out:
1036 return ret;
1037}
1038
1039/*
1040 * ocfs2_xattr_set_value_outside()
1041 *
1042 * Set large size value in B tree.
1043 */
1044static int ocfs2_xattr_set_value_outside(struct inode *inode,
1045 struct ocfs2_xattr_info *xi,
1046 struct ocfs2_xattr_search *xs,
1047 size_t offs)
1048{
1049 size_t name_len = strlen(xi->name);
1050 void *val = xs->base + offs;
1051 struct ocfs2_xattr_value_root *xv = NULL;
1052 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1053 int ret = 0;
1054
1055 memset(val, 0, size);
1056 memcpy(val, xi->name, name_len);
1057 xv = (struct ocfs2_xattr_value_root *)
1058 (val + OCFS2_XATTR_SIZE(name_len));
1059 xv->xr_clusters = 0;
1060 xv->xr_last_eb_blk = 0;
1061 xv->xr_list.l_tree_depth = 0;
1062 xv->xr_list.l_count = cpu_to_le16(1);
1063 xv->xr_list.l_next_free_rec = 0;
1064
1065 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1066 xi->value_len);
1067 if (ret < 0) {
1068 mlog_errno(ret);
1069 return ret;
1070 }
1071 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1072 xi->value_len);
1073 if (ret < 0) {
1074 mlog_errno(ret);
1075 return ret;
1076 }
1077 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1078 if (ret < 0)
1079 mlog_errno(ret);
1080
1081 return ret;
1082}
1083
1084/*
1085 * ocfs2_xattr_set_entry_local()
1086 *
1087 * Set, replace or remove extended attribute in local.
1088 */
1089static void ocfs2_xattr_set_entry_local(struct inode *inode,
1090 struct ocfs2_xattr_info *xi,
1091 struct ocfs2_xattr_search *xs,
1092 struct ocfs2_xattr_entry *last,
1093 size_t min_offs)
1094{
1095 size_t name_len = strlen(xi->name);
1096 int i;
1097
1098 if (xi->value && xs->not_found) {
1099 /* Insert the new xattr entry. */
1100 le16_add_cpu(&xs->header->xh_count, 1);
1101 ocfs2_xattr_set_type(last, xi->name_index);
1102 ocfs2_xattr_set_local(last, 1);
1103 last->xe_name_len = name_len;
1104 } else {
1105 void *first_val;
1106 void *val;
1107 size_t offs, size;
1108
1109 first_val = xs->base + min_offs;
1110 offs = le16_to_cpu(xs->here->xe_name_offset);
1111 val = xs->base + offs;
1112
1113 if (le64_to_cpu(xs->here->xe_value_size) >
1114 OCFS2_XATTR_INLINE_SIZE)
1115 size = OCFS2_XATTR_SIZE(name_len) +
1116 OCFS2_XATTR_ROOT_SIZE;
1117 else
1118 size = OCFS2_XATTR_SIZE(name_len) +
1119 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1120
1121 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1122 OCFS2_XATTR_SIZE(xi->value_len)) {
1123 /* The old and the new value have the
1124 same size. Just replace the value. */
1125 ocfs2_xattr_set_local(xs->here, 1);
1126 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1127 /* Clear value bytes. */
1128 memset(val + OCFS2_XATTR_SIZE(name_len),
1129 0,
1130 OCFS2_XATTR_SIZE(xi->value_len));
1131 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1132 xi->value,
1133 xi->value_len);
1134 return;
1135 }
1136 /* Remove the old name+value. */
1137 memmove(first_val + size, first_val, val - first_val);
1138 memset(first_val, 0, size);
1139 xs->here->xe_name_hash = 0;
1140 xs->here->xe_name_offset = 0;
1141 ocfs2_xattr_set_local(xs->here, 1);
1142 xs->here->xe_value_size = 0;
1143
1144 min_offs += size;
1145
1146 /* Adjust all value offsets. */
1147 last = xs->header->xh_entries;
1148 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1149 size_t o = le16_to_cpu(last->xe_name_offset);
1150
1151 if (o < offs)
1152 last->xe_name_offset = cpu_to_le16(o + size);
1153 last += 1;
1154 }
1155
1156 if (!xi->value) {
1157 /* Remove the old entry. */
1158 last -= 1;
1159 memmove(xs->here, xs->here + 1,
1160 (void *)last - (void *)xs->here);
1161 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1162 le16_add_cpu(&xs->header->xh_count, -1);
1163 }
1164 }
1165 if (xi->value) {
1166 /* Insert the new name+value. */
1167 size_t size = OCFS2_XATTR_SIZE(name_len) +
1168 OCFS2_XATTR_SIZE(xi->value_len);
1169 void *val = xs->base + min_offs - size;
1170
1171 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1172 memset(val, 0, size);
1173 memcpy(val, xi->name, name_len);
1174 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1175 xi->value,
1176 xi->value_len);
1177 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1178 ocfs2_xattr_set_local(xs->here, 1);
1179 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1180 }
1181
1182 return;
1183}
1184
1185/*
1186 * ocfs2_xattr_set_entry()
1187 *
1188 * Set extended attribute entry into inode or block.
1189 *
1190 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1191 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1192 * then set value in B tree with set_value_outside().
1193 */
1194static int ocfs2_xattr_set_entry(struct inode *inode,
1195 struct ocfs2_xattr_info *xi,
1196 struct ocfs2_xattr_search *xs,
1197 int flag)
1198{
1199 struct ocfs2_xattr_entry *last;
1200 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1201 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1202 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1203 size_t size_l = 0;
1204 handle_t *handle = NULL;
1205 int free, i, ret;
1206 struct ocfs2_xattr_info xi_l = {
1207 .name_index = xi->name_index,
1208 .name = xi->name,
1209 .value = xi->value,
1210 .value_len = xi->value_len,
1211 };
1212
1213 /* Compute min_offs, last and free space. */
1214 last = xs->header->xh_entries;
1215
1216 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1217 size_t offs = le16_to_cpu(last->xe_name_offset);
1218 if (offs < min_offs)
1219 min_offs = offs;
1220 last += 1;
1221 }
1222
1223 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1224 if (free < 0)
1225 return -EFAULT;
1226
1227 if (!xs->not_found) {
1228 size_t size = 0;
1229 if (ocfs2_xattr_is_local(xs->here))
1230 size = OCFS2_XATTR_SIZE(name_len) +
1231 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1232 else
1233 size = OCFS2_XATTR_SIZE(name_len) +
1234 OCFS2_XATTR_ROOT_SIZE;
1235 free += (size + sizeof(struct ocfs2_xattr_entry));
1236 }
1237 /* Check free space in inode or block */
1238 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1239 if (free < sizeof(struct ocfs2_xattr_entry) +
1240 OCFS2_XATTR_SIZE(name_len) +
1241 OCFS2_XATTR_ROOT_SIZE) {
1242 ret = -ENOSPC;
1243 goto out;
1244 }
1245 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1246 xi_l.value = (void *)&def_xv;
1247 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1248 } else if (xi->value) {
1249 if (free < sizeof(struct ocfs2_xattr_entry) +
1250 OCFS2_XATTR_SIZE(name_len) +
1251 OCFS2_XATTR_SIZE(xi->value_len)) {
1252 ret = -ENOSPC;
1253 goto out;
1254 }
1255 }
1256
1257 if (!xs->not_found) {
1258 /* For existing extended attribute */
1259 size_t size = OCFS2_XATTR_SIZE(name_len) +
1260 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1261 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1262 void *val = xs->base + offs;
1263
1264 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1265 /* Replace existing local xattr with tree root */
1266 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1267 offs);
1268 if (ret < 0)
1269 mlog_errno(ret);
1270 goto out;
1271 } else if (!ocfs2_xattr_is_local(xs->here)) {
1272 /* For existing xattr which has value outside */
1273 struct ocfs2_xattr_value_root *xv = NULL;
1274 xv = (struct ocfs2_xattr_value_root *)(val +
1275 OCFS2_XATTR_SIZE(name_len));
1276
1277 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1278 /*
1279 * If new value need set outside also,
1280 * first truncate old value to new value,
1281 * then set new value with set_value_outside().
1282 */
1283 ret = ocfs2_xattr_value_truncate(inode,
1284 xs->xattr_bh,
1285 xv,
1286 xi->value_len);
1287 if (ret < 0) {
1288 mlog_errno(ret);
1289 goto out;
1290 }
1291
1292 ret = __ocfs2_xattr_set_value_outside(inode,
1293 xv,
1294 xi->value,
1295 xi->value_len);
1296 if (ret < 0) {
1297 mlog_errno(ret);
1298 goto out;
1299 }
1300
1301 ret = ocfs2_xattr_update_entry(inode,
1302 xi,
1303 xs,
1304 offs);
1305 if (ret < 0)
1306 mlog_errno(ret);
1307 goto out;
1308 } else {
1309 /*
1310 * If new value need set in local,
1311 * just trucate old value to zero.
1312 */
1313 ret = ocfs2_xattr_value_truncate(inode,
1314 xs->xattr_bh,
1315 xv,
1316 0);
1317 if (ret < 0)
1318 mlog_errno(ret);
1319 }
1320 }
1321 }
1322
1323 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1324 OCFS2_INODE_UPDATE_CREDITS);
1325 if (IS_ERR(handle)) {
1326 ret = PTR_ERR(handle);
1327 mlog_errno(ret);
1328 goto out;
1329 }
1330
1331 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1332 OCFS2_JOURNAL_ACCESS_WRITE);
1333 if (ret) {
1334 mlog_errno(ret);
1335 goto out_commit;
1336 }
1337
1338 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tao Ma28b8ca02008-09-01 08:45:18 +08001339 /* set extended attribute in external block. */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001340 ret = ocfs2_extend_trans(handle,
Tao Ma28b8ca02008-09-01 08:45:18 +08001341 OCFS2_INODE_UPDATE_CREDITS +
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001342 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1343 if (ret) {
1344 mlog_errno(ret);
1345 goto out_commit;
1346 }
1347 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1348 OCFS2_JOURNAL_ACCESS_WRITE);
1349 if (ret) {
1350 mlog_errno(ret);
1351 goto out_commit;
1352 }
1353 }
1354
1355 /*
1356 * Set value in local, include set tree root in local.
1357 * This is the first step for value size >INLINE_SIZE.
1358 */
1359 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1360
1361 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1362 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1363 if (ret < 0) {
1364 mlog_errno(ret);
1365 goto out_commit;
1366 }
1367 }
1368
1369 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1370 (flag & OCFS2_INLINE_XATTR_FL)) {
1371 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1372 unsigned int xattrsize = osb->s_xattr_inline_size;
1373
1374 /*
1375 * Adjust extent record count or inline data size
1376 * to reserve space for extended attribute.
1377 */
1378 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1379 struct ocfs2_inline_data *idata = &di->id2.i_data;
1380 le16_add_cpu(&idata->id_count, -xattrsize);
1381 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1382 struct ocfs2_extent_list *el = &di->id2.i_list;
1383 le16_add_cpu(&el->l_count, -(xattrsize /
1384 sizeof(struct ocfs2_extent_rec)));
1385 }
1386 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1387 }
1388 /* Update xattr flag */
1389 spin_lock(&oi->ip_lock);
1390 oi->ip_dyn_features |= flag;
1391 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1392 spin_unlock(&oi->ip_lock);
1393 /* Update inode ctime */
1394 inode->i_ctime = CURRENT_TIME;
1395 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1396 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1397
1398 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1399 if (ret < 0)
1400 mlog_errno(ret);
1401
1402out_commit:
1403 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1404
1405 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1406 /*
1407 * Set value outside in B tree.
1408 * This is the second step for value size > INLINE_SIZE.
1409 */
1410 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1411 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1412 if (ret < 0) {
1413 int ret2;
1414
1415 mlog_errno(ret);
1416 /*
1417 * If set value outside failed, we have to clean
1418 * the junk tree root we have already set in local.
1419 */
1420 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1421 if (ret2 < 0)
1422 mlog_errno(ret2);
1423 }
1424 }
1425out:
1426 return ret;
1427
1428}
1429
1430static int ocfs2_xattr_free_block(handle_t *handle,
1431 struct ocfs2_super *osb,
1432 struct ocfs2_xattr_block *xb)
1433{
1434 struct inode *xb_alloc_inode;
1435 struct buffer_head *xb_alloc_bh = NULL;
1436 u64 blk = le64_to_cpu(xb->xb_blkno);
1437 u16 bit = le16_to_cpu(xb->xb_suballoc_bit);
1438 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1439 int ret = 0;
1440
1441 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1442 EXTENT_ALLOC_SYSTEM_INODE,
1443 le16_to_cpu(xb->xb_suballoc_slot));
1444 if (!xb_alloc_inode) {
1445 ret = -ENOMEM;
1446 mlog_errno(ret);
1447 goto out;
1448 }
1449 mutex_lock(&xb_alloc_inode->i_mutex);
1450
1451 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1452 if (ret < 0) {
1453 mlog_errno(ret);
1454 goto out_mutex;
1455 }
1456 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
1457 if (ret < 0) {
1458 mlog_errno(ret);
1459 goto out_unlock;
1460 }
1461 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1462 bit, bg_blkno, 1);
1463 if (ret < 0)
1464 mlog_errno(ret);
1465out_unlock:
1466 ocfs2_inode_unlock(xb_alloc_inode, 1);
1467 brelse(xb_alloc_bh);
1468out_mutex:
1469 mutex_unlock(&xb_alloc_inode->i_mutex);
1470 iput(xb_alloc_inode);
1471out:
1472 return ret;
1473}
1474
1475static int ocfs2_remove_value_outside(struct inode*inode,
1476 struct buffer_head *bh,
1477 struct ocfs2_xattr_header *header)
1478{
1479 int ret = 0, i;
1480
1481 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1482 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1483
1484 if (!ocfs2_xattr_is_local(entry)) {
1485 struct ocfs2_xattr_value_root *xv;
1486 void *val;
1487
1488 val = (void *)header +
1489 le16_to_cpu(entry->xe_name_offset);
1490 xv = (struct ocfs2_xattr_value_root *)
1491 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1492 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1493 if (ret < 0) {
1494 mlog_errno(ret);
1495 return ret;
1496 }
1497 }
1498 }
1499
1500 return ret;
1501}
1502
1503static int ocfs2_xattr_ibody_remove(struct inode *inode,
1504 struct buffer_head *di_bh)
1505{
1506
1507 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1508 struct ocfs2_xattr_header *header;
1509 int ret;
1510
1511 header = (struct ocfs2_xattr_header *)
1512 ((void *)di + inode->i_sb->s_blocksize -
1513 le16_to_cpu(di->i_xattr_inline_size));
1514
1515 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1516
1517 return ret;
1518}
1519
1520static int ocfs2_xattr_block_remove(struct inode *inode,
1521 struct buffer_head *blk_bh)
1522{
1523 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001524 int ret = 0;
1525
1526 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001527 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1528 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1529 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1530 } else
1531 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001532
1533 return ret;
1534}
1535
1536/*
1537 * ocfs2_xattr_remove()
1538 *
1539 * Free extended attribute resources associated with this inode.
1540 */
1541int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1542{
1543 struct ocfs2_xattr_block *xb;
1544 struct buffer_head *blk_bh = NULL;
1545 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1546 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1547 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1548 handle_t *handle;
1549 int ret;
1550
Tiger Yang8154da32008-08-18 17:11:46 +08001551 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1552 return 0;
1553
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001554 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1555 return 0;
1556
1557 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1558 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1559 if (ret < 0) {
1560 mlog_errno(ret);
1561 goto out;
1562 }
1563 }
1564 if (di->i_xattr_loc) {
1565 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1566 le64_to_cpu(di->i_xattr_loc),
1567 &blk_bh, OCFS2_BH_CACHED, inode);
1568 if (ret < 0) {
1569 mlog_errno(ret);
1570 return ret;
1571 }
1572 /*Verify the signature of xattr block*/
1573 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1574 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1575 ret = -EFAULT;
1576 goto out;
1577 }
1578
1579 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1580 if (ret < 0) {
1581 mlog_errno(ret);
1582 goto out;
1583 }
1584 }
1585
1586 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1587 OCFS2_INODE_UPDATE_CREDITS);
1588 if (IS_ERR(handle)) {
1589 ret = PTR_ERR(handle);
1590 mlog_errno(ret);
1591 goto out;
1592 }
1593 ret = ocfs2_journal_access(handle, inode, di_bh,
1594 OCFS2_JOURNAL_ACCESS_WRITE);
1595 if (ret) {
1596 mlog_errno(ret);
1597 goto out_commit;
1598 }
1599
1600 if (di->i_xattr_loc) {
1601 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1602 ocfs2_xattr_free_block(handle, osb, xb);
1603 di->i_xattr_loc = cpu_to_le64(0);
1604 }
1605
1606 spin_lock(&oi->ip_lock);
1607 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1608 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1609 spin_unlock(&oi->ip_lock);
1610
1611 ret = ocfs2_journal_dirty(handle, di_bh);
1612 if (ret < 0)
1613 mlog_errno(ret);
1614out_commit:
1615 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1616out:
1617 brelse(blk_bh);
1618
1619 return ret;
1620}
1621
1622static int ocfs2_xattr_has_space_inline(struct inode *inode,
1623 struct ocfs2_dinode *di)
1624{
1625 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1626 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1627 int free;
1628
1629 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1630 return 0;
1631
1632 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1633 struct ocfs2_inline_data *idata = &di->id2.i_data;
1634 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1635 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1636 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1637 le64_to_cpu(di->i_size);
1638 } else {
1639 struct ocfs2_extent_list *el = &di->id2.i_list;
1640 free = (le16_to_cpu(el->l_count) -
1641 le16_to_cpu(el->l_next_free_rec)) *
1642 sizeof(struct ocfs2_extent_rec);
1643 }
1644 if (free >= xattrsize)
1645 return 1;
1646
1647 return 0;
1648}
1649
1650/*
1651 * ocfs2_xattr_ibody_find()
1652 *
1653 * Find extended attribute in inode block and
1654 * fill search info into struct ocfs2_xattr_search.
1655 */
1656static int ocfs2_xattr_ibody_find(struct inode *inode,
1657 int name_index,
1658 const char *name,
1659 struct ocfs2_xattr_search *xs)
1660{
1661 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1662 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1663 int ret;
1664 int has_space = 0;
1665
1666 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1667 return 0;
1668
1669 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1670 down_read(&oi->ip_alloc_sem);
1671 has_space = ocfs2_xattr_has_space_inline(inode, di);
1672 up_read(&oi->ip_alloc_sem);
1673 if (!has_space)
1674 return 0;
1675 }
1676
1677 xs->xattr_bh = xs->inode_bh;
1678 xs->end = (void *)di + inode->i_sb->s_blocksize;
1679 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1680 xs->header = (struct ocfs2_xattr_header *)
1681 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1682 else
1683 xs->header = (struct ocfs2_xattr_header *)
1684 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1685 xs->base = (void *)xs->header;
1686 xs->here = xs->header->xh_entries;
1687
1688 /* Find the named attribute. */
1689 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1690 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1691 if (ret && ret != -ENODATA)
1692 return ret;
1693 xs->not_found = ret;
1694 }
1695
1696 return 0;
1697}
1698
1699/*
1700 * ocfs2_xattr_ibody_set()
1701 *
1702 * Set, replace or remove an extended attribute into inode block.
1703 *
1704 */
1705static int ocfs2_xattr_ibody_set(struct inode *inode,
1706 struct ocfs2_xattr_info *xi,
1707 struct ocfs2_xattr_search *xs)
1708{
1709 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1710 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1711 int ret;
1712
1713 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1714 return -ENOSPC;
1715
1716 down_write(&oi->ip_alloc_sem);
1717 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1718 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1719 ret = -ENOSPC;
1720 goto out;
1721 }
1722 }
1723
1724 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1725 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1726out:
1727 up_write(&oi->ip_alloc_sem);
1728
1729 return ret;
1730}
1731
1732/*
1733 * ocfs2_xattr_block_find()
1734 *
1735 * Find extended attribute in external block and
1736 * fill search info into struct ocfs2_xattr_search.
1737 */
1738static int ocfs2_xattr_block_find(struct inode *inode,
1739 int name_index,
1740 const char *name,
1741 struct ocfs2_xattr_search *xs)
1742{
1743 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1744 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08001745 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001746 int ret = 0;
1747
1748 if (!di->i_xattr_loc)
1749 return ret;
1750
1751 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
1752 le64_to_cpu(di->i_xattr_loc),
1753 &blk_bh, OCFS2_BH_CACHED, inode);
1754 if (ret < 0) {
1755 mlog_errno(ret);
1756 return ret;
1757 }
1758 /*Verify the signature of xattr block*/
1759 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
1760 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
1761 ret = -EFAULT;
1762 goto cleanup;
1763 }
1764
1765 xs->xattr_bh = blk_bh;
Tao Ma589dc262008-08-18 17:38:51 +08001766 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001767
Tao Ma589dc262008-08-18 17:38:51 +08001768 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1769 xs->header = &xb->xb_attrs.xb_header;
1770 xs->base = (void *)xs->header;
1771 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1772 xs->here = xs->header->xh_entries;
1773
1774 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1775 } else
1776 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1777 name_index,
1778 name, xs);
1779
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001780 if (ret && ret != -ENODATA) {
1781 xs->xattr_bh = NULL;
1782 goto cleanup;
1783 }
1784 xs->not_found = ret;
1785 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001786cleanup:
1787 brelse(blk_bh);
1788
1789 return ret;
1790}
1791
1792/*
Tao Ma01225592008-08-18 17:38:53 +08001793 * When all the xattrs are deleted from index btree, the ocfs2_xattr_tree
1794 * will be erased and ocfs2_xattr_block will have its ocfs2_xattr_header
1795 * re-initialized.
1796 */
1797static int ocfs2_restore_xattr_block(struct inode *inode,
1798 struct ocfs2_xattr_search *xs)
1799{
1800 int ret;
1801 handle_t *handle;
1802 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1803 struct ocfs2_xattr_block *xb =
1804 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1805 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
1806 u16 xb_flags = le16_to_cpu(xb->xb_flags);
1807
1808 BUG_ON(!(xb_flags & OCFS2_XATTR_INDEXED) ||
1809 le16_to_cpu(el->l_next_free_rec) != 0);
1810
1811 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1812 if (IS_ERR(handle)) {
1813 ret = PTR_ERR(handle);
1814 handle = NULL;
1815 goto out;
1816 }
1817
1818 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1819 OCFS2_JOURNAL_ACCESS_WRITE);
1820 if (ret < 0) {
1821 mlog_errno(ret);
1822 goto out_commit;
1823 }
1824
1825 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
1826 offsetof(struct ocfs2_xattr_block, xb_attrs));
1827
1828 xb->xb_flags = cpu_to_le16(xb_flags & ~OCFS2_XATTR_INDEXED);
1829
1830 ocfs2_journal_dirty(handle, xs->xattr_bh);
1831
1832out_commit:
1833 ocfs2_commit_trans(osb, handle);
1834out:
1835 return ret;
1836}
1837
1838/*
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001839 * ocfs2_xattr_block_set()
1840 *
1841 * Set, replace or remove an extended attribute into external block.
1842 *
1843 */
1844static int ocfs2_xattr_block_set(struct inode *inode,
1845 struct ocfs2_xattr_info *xi,
1846 struct ocfs2_xattr_search *xs)
1847{
1848 struct buffer_head *new_bh = NULL;
1849 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1850 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1851 struct ocfs2_alloc_context *meta_ac = NULL;
1852 handle_t *handle = NULL;
1853 struct ocfs2_xattr_block *xblk = NULL;
1854 u16 suballoc_bit_start;
1855 u32 num_got;
1856 u64 first_blkno;
1857 int ret;
1858
1859 if (!xs->xattr_bh) {
1860 /*
1861 * Alloc one external block for extended attribute
1862 * outside of inode.
1863 */
1864 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1865 if (ret < 0) {
1866 mlog_errno(ret);
1867 goto out;
1868 }
1869 handle = ocfs2_start_trans(osb,
1870 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1871 if (IS_ERR(handle)) {
1872 ret = PTR_ERR(handle);
1873 mlog_errno(ret);
1874 goto out;
1875 }
1876 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1877 OCFS2_JOURNAL_ACCESS_CREATE);
1878 if (ret < 0) {
1879 mlog_errno(ret);
1880 goto out_commit;
1881 }
1882
1883 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1884 &suballoc_bit_start, &num_got,
1885 &first_blkno);
1886 if (ret < 0) {
1887 mlog_errno(ret);
1888 goto out_commit;
1889 }
1890
1891 new_bh = sb_getblk(inode->i_sb, first_blkno);
1892 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1893
1894 ret = ocfs2_journal_access(handle, inode, new_bh,
1895 OCFS2_JOURNAL_ACCESS_CREATE);
1896 if (ret < 0) {
1897 mlog_errno(ret);
1898 goto out_commit;
1899 }
1900
1901 /* Initialize ocfs2_xattr_block */
1902 xs->xattr_bh = new_bh;
1903 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1904 memset(xblk, 0, inode->i_sb->s_blocksize);
1905 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1906 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1907 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1908 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1909 xblk->xb_blkno = cpu_to_le64(first_blkno);
1910
1911 xs->header = &xblk->xb_attrs.xb_header;
1912 xs->base = (void *)xs->header;
1913 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1914 xs->here = xs->header->xh_entries;
1915
1916
1917 ret = ocfs2_journal_dirty(handle, new_bh);
1918 if (ret < 0) {
1919 mlog_errno(ret);
1920 goto out_commit;
1921 }
1922 di->i_xattr_loc = cpu_to_le64(first_blkno);
1923 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1924 if (ret < 0)
1925 mlog_errno(ret);
1926out_commit:
1927 ocfs2_commit_trans(osb, handle);
1928out:
1929 if (meta_ac)
1930 ocfs2_free_alloc_context(meta_ac);
1931 if (ret < 0)
1932 return ret;
Tao Ma01225592008-08-18 17:38:53 +08001933 } else
1934 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1935
1936 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1937 /* Set extended attribute into external block */
1938 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1939 if (!ret || ret != -ENOSPC)
1940 goto end;
1941
1942 ret = ocfs2_xattr_create_index_block(inode, xs);
1943 if (ret)
1944 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001945 }
1946
Tao Ma01225592008-08-18 17:38:53 +08001947 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
1948 if (!ret && xblk->xb_attrs.xb_root.xt_list.l_next_free_rec == 0)
1949 ret = ocfs2_restore_xattr_block(inode, xs);
1950
1951end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001952
1953 return ret;
1954}
1955
1956/*
1957 * ocfs2_xattr_set()
1958 *
1959 * Set, replace or remove an extended attribute for this inode.
1960 * value is NULL to remove an existing extended attribute, else either
1961 * create or replace an extended attribute.
1962 */
1963int ocfs2_xattr_set(struct inode *inode,
1964 int name_index,
1965 const char *name,
1966 const void *value,
1967 size_t value_len,
1968 int flags)
1969{
1970 struct buffer_head *di_bh = NULL;
1971 struct ocfs2_dinode *di;
1972 int ret;
Tao Ma01225592008-08-18 17:38:53 +08001973 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001974
1975 struct ocfs2_xattr_info xi = {
1976 .name_index = name_index,
1977 .name = name,
1978 .value = value,
1979 .value_len = value_len,
1980 };
1981
1982 struct ocfs2_xattr_search xis = {
1983 .not_found = -ENODATA,
1984 };
1985
1986 struct ocfs2_xattr_search xbs = {
1987 .not_found = -ENODATA,
1988 };
1989
Tiger Yang8154da32008-08-18 17:11:46 +08001990 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1991 return -EOPNOTSUPP;
1992
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001993 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1994 if (ret < 0) {
1995 mlog_errno(ret);
1996 return ret;
1997 }
1998 xis.inode_bh = xbs.inode_bh = di_bh;
1999 di = (struct ocfs2_dinode *)di_bh->b_data;
2000
2001 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2002 /*
2003 * Scan inode and external block to find the same name
2004 * extended attribute and collect search infomation.
2005 */
2006 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2007 if (ret)
2008 goto cleanup;
2009 if (xis.not_found) {
2010 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2011 if (ret)
2012 goto cleanup;
2013 }
2014
2015 if (xis.not_found && xbs.not_found) {
2016 ret = -ENODATA;
2017 if (flags & XATTR_REPLACE)
2018 goto cleanup;
2019 ret = 0;
2020 if (!value)
2021 goto cleanup;
2022 } else {
2023 ret = -EEXIST;
2024 if (flags & XATTR_CREATE)
2025 goto cleanup;
2026 }
2027
2028 if (!value) {
2029 /* Remove existing extended attribute */
2030 if (!xis.not_found)
2031 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2032 else if (!xbs.not_found)
2033 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2034 } else {
2035 /* We always try to set extended attribute into inode first*/
2036 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2037 if (!ret && !xbs.not_found) {
2038 /*
2039 * If succeed and that extended attribute existing in
2040 * external block, then we will remove it.
2041 */
2042 xi.value = NULL;
2043 xi.value_len = 0;
2044 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2045 } else if (ret == -ENOSPC) {
2046 if (di->i_xattr_loc && !xbs.xattr_bh) {
2047 ret = ocfs2_xattr_block_find(inode, name_index,
2048 name, &xbs);
2049 if (ret)
2050 goto cleanup;
2051 }
2052 /*
2053 * If no space in inode, we will set extended attribute
2054 * into external block.
2055 */
2056 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2057 if (ret)
2058 goto cleanup;
2059 if (!xis.not_found) {
2060 /*
2061 * If succeed and that extended attribute
2062 * existing in inode, we will remove it.
2063 */
2064 xi.value = NULL;
2065 xi.value_len = 0;
2066 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2067 }
2068 }
2069 }
2070cleanup:
2071 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2072 ocfs2_inode_unlock(inode, 1);
2073 brelse(di_bh);
2074 brelse(xbs.xattr_bh);
Tao Ma01225592008-08-18 17:38:53 +08002075 for (i = 0; i < blk_per_bucket; i++)
2076 brelse(xbs.bucket.bhs[i]);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002077
2078 return ret;
2079}
2080
Tao Ma589dc262008-08-18 17:38:51 +08002081static inline u32 ocfs2_xattr_hash_by_name(struct inode *inode,
2082 int name_index,
2083 const char *suffix_name)
2084{
2085 struct xattr_handler *handler = ocfs2_xattr_handler(name_index);
2086 char *prefix = handler->prefix;
2087 int prefix_len = strlen(handler->prefix);
2088
2089 return ocfs2_xattr_name_hash(inode, prefix, prefix_len,
2090 (char *)suffix_name, strlen(suffix_name));
2091}
2092
Tao Ma0c044f02008-08-18 17:38:50 +08002093/*
2094 * Find the xattr extent rec which may contains name_hash.
2095 * e_cpos will be the first name hash of the xattr rec.
2096 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2097 */
2098static int ocfs2_xattr_get_rec(struct inode *inode,
2099 u32 name_hash,
2100 u64 *p_blkno,
2101 u32 *e_cpos,
2102 u32 *num_clusters,
2103 struct ocfs2_extent_list *el)
2104{
2105 int ret = 0, i;
2106 struct buffer_head *eb_bh = NULL;
2107 struct ocfs2_extent_block *eb;
2108 struct ocfs2_extent_rec *rec = NULL;
2109 u64 e_blkno = 0;
2110
2111 if (el->l_tree_depth) {
2112 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2113 if (ret) {
2114 mlog_errno(ret);
2115 goto out;
2116 }
2117
2118 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2119 el = &eb->h_list;
2120
2121 if (el->l_tree_depth) {
2122 ocfs2_error(inode->i_sb,
2123 "Inode %lu has non zero tree depth in "
2124 "xattr tree block %llu\n", inode->i_ino,
2125 (unsigned long long)eb_bh->b_blocknr);
2126 ret = -EROFS;
2127 goto out;
2128 }
2129 }
2130
2131 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2132 rec = &el->l_recs[i];
2133
2134 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2135 e_blkno = le64_to_cpu(rec->e_blkno);
2136 break;
2137 }
2138 }
2139
2140 if (!e_blkno) {
2141 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2142 "record (%u, %u, 0) in xattr", inode->i_ino,
2143 le32_to_cpu(rec->e_cpos),
2144 ocfs2_rec_clusters(el, rec));
2145 ret = -EROFS;
2146 goto out;
2147 }
2148
2149 *p_blkno = le64_to_cpu(rec->e_blkno);
2150 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2151 if (e_cpos)
2152 *e_cpos = le32_to_cpu(rec->e_cpos);
2153out:
2154 brelse(eb_bh);
2155 return ret;
2156}
2157
2158typedef int (xattr_bucket_func)(struct inode *inode,
2159 struct ocfs2_xattr_bucket *bucket,
2160 void *para);
2161
Tao Ma589dc262008-08-18 17:38:51 +08002162static int ocfs2_find_xe_in_bucket(struct inode *inode,
2163 struct buffer_head *header_bh,
2164 int name_index,
2165 const char *name,
2166 u32 name_hash,
2167 u16 *xe_index,
2168 int *found)
2169{
2170 int i, ret = 0, cmp = 1, block_off, new_offset;
2171 struct ocfs2_xattr_header *xh =
2172 (struct ocfs2_xattr_header *)header_bh->b_data;
2173 size_t name_len = strlen(name);
2174 struct ocfs2_xattr_entry *xe = NULL;
2175 struct buffer_head *name_bh = NULL;
2176 char *xe_name;
2177
2178 /*
2179 * We don't use binary search in the bucket because there
2180 * may be multiple entries with the same name hash.
2181 */
2182 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2183 xe = &xh->xh_entries[i];
2184
2185 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2186 continue;
2187 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2188 break;
2189
2190 cmp = name_index - ocfs2_xattr_get_type(xe);
2191 if (!cmp)
2192 cmp = name_len - xe->xe_name_len;
2193 if (cmp)
2194 continue;
2195
2196 ret = ocfs2_xattr_bucket_get_name_value(inode,
2197 xh,
2198 i,
2199 &block_off,
2200 &new_offset);
2201 if (ret) {
2202 mlog_errno(ret);
2203 break;
2204 }
2205
2206 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
2207 header_bh->b_blocknr + block_off,
2208 &name_bh, OCFS2_BH_CACHED, inode);
2209 if (ret) {
2210 mlog_errno(ret);
2211 break;
2212 }
2213 xe_name = name_bh->b_data + new_offset;
2214
2215 cmp = memcmp(name, xe_name, name_len);
2216 brelse(name_bh);
2217 name_bh = NULL;
2218
2219 if (cmp == 0) {
2220 *xe_index = i;
2221 *found = 1;
2222 ret = 0;
2223 break;
2224 }
2225 }
2226
2227 return ret;
2228}
2229
2230/*
2231 * Find the specified xattr entry in a series of buckets.
2232 * This series start from p_blkno and last for num_clusters.
2233 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2234 * the num of the valid buckets.
2235 *
2236 * Return the buffer_head this xattr should reside in. And if the xattr's
2237 * hash is in the gap of 2 buckets, return the lower bucket.
2238 */
2239static int ocfs2_xattr_bucket_find(struct inode *inode,
2240 int name_index,
2241 const char *name,
2242 u32 name_hash,
2243 u64 p_blkno,
2244 u32 first_hash,
2245 u32 num_clusters,
2246 struct ocfs2_xattr_search *xs)
2247{
2248 int ret, found = 0;
2249 struct buffer_head *bh = NULL;
2250 struct buffer_head *lower_bh = NULL;
2251 struct ocfs2_xattr_header *xh = NULL;
2252 struct ocfs2_xattr_entry *xe = NULL;
2253 u16 index = 0;
2254 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2255 int low_bucket = 0, bucket, high_bucket;
2256 u32 last_hash;
2257 u64 blkno;
2258
2259 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
2260 &bh, OCFS2_BH_CACHED, inode);
2261 if (ret) {
2262 mlog_errno(ret);
2263 goto out;
2264 }
2265
2266 xh = (struct ocfs2_xattr_header *)bh->b_data;
2267 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2268
2269 while (low_bucket <= high_bucket) {
2270 brelse(bh);
2271 bh = NULL;
2272 bucket = (low_bucket + high_bucket) / 2;
2273
2274 blkno = p_blkno + bucket * blk_per_bucket;
2275
2276 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
2277 &bh, OCFS2_BH_CACHED, inode);
2278 if (ret) {
2279 mlog_errno(ret);
2280 goto out;
2281 }
2282
2283 xh = (struct ocfs2_xattr_header *)bh->b_data;
2284 xe = &xh->xh_entries[0];
2285 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2286 high_bucket = bucket - 1;
2287 continue;
2288 }
2289
2290 /*
2291 * Check whether the hash of the last entry in our
2292 * bucket is larger than the search one.
2293 */
2294 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2295 last_hash = le32_to_cpu(xe->xe_name_hash);
2296
2297 /* record lower_bh which may be the insert place. */
2298 brelse(lower_bh);
2299 lower_bh = bh;
2300 bh = NULL;
2301
2302 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2303 low_bucket = bucket + 1;
2304 continue;
2305 }
2306
2307 /* the searched xattr should reside in this bucket if exists. */
2308 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2309 name_index, name, name_hash,
2310 &index, &found);
2311 if (ret) {
2312 mlog_errno(ret);
2313 goto out;
2314 }
2315 break;
2316 }
2317
2318 /*
2319 * Record the bucket we have found.
2320 * When the xattr's hash value is in the gap of 2 buckets, we will
2321 * always set it to the previous bucket.
2322 */
2323 if (!lower_bh) {
2324 /*
2325 * We can't find any bucket whose first name_hash is less
2326 * than the find name_hash.
2327 */
2328 BUG_ON(bh->b_blocknr != p_blkno);
2329 lower_bh = bh;
2330 bh = NULL;
2331 }
2332 xs->bucket.bhs[0] = lower_bh;
2333 xs->bucket.xh = (struct ocfs2_xattr_header *)
2334 xs->bucket.bhs[0]->b_data;
2335 lower_bh = NULL;
2336
2337 xs->header = xs->bucket.xh;
2338 xs->base = xs->bucket.bhs[0]->b_data;
2339 xs->end = xs->base + inode->i_sb->s_blocksize;
2340
2341 if (found) {
2342 /*
2343 * If we have found the xattr enty, read all the blocks in
2344 * this bucket.
2345 */
2346 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2347 xs->bucket.bhs[0]->b_blocknr + 1,
2348 blk_per_bucket - 1, &xs->bucket.bhs[1],
2349 OCFS2_BH_CACHED, inode);
2350 if (ret) {
2351 mlog_errno(ret);
2352 goto out;
2353 }
2354
2355 xs->here = &xs->header->xh_entries[index];
2356 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
2357 (unsigned long long)xs->bucket.bhs[0]->b_blocknr, index);
2358 } else
2359 ret = -ENODATA;
2360
2361out:
2362 brelse(bh);
2363 brelse(lower_bh);
2364 return ret;
2365}
2366
2367static int ocfs2_xattr_index_block_find(struct inode *inode,
2368 struct buffer_head *root_bh,
2369 int name_index,
2370 const char *name,
2371 struct ocfs2_xattr_search *xs)
2372{
2373 int ret;
2374 struct ocfs2_xattr_block *xb =
2375 (struct ocfs2_xattr_block *)root_bh->b_data;
2376 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2377 struct ocfs2_extent_list *el = &xb_root->xt_list;
2378 u64 p_blkno = 0;
2379 u32 first_hash, num_clusters = 0;
2380 u32 name_hash = ocfs2_xattr_hash_by_name(inode, name_index, name);
2381
2382 if (le16_to_cpu(el->l_next_free_rec) == 0)
2383 return -ENODATA;
2384
2385 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2386 name, name_hash, name_index);
2387
2388 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2389 &num_clusters, el);
2390 if (ret) {
2391 mlog_errno(ret);
2392 goto out;
2393 }
2394
2395 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2396
2397 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
2398 "in the rec is %u\n", num_clusters, p_blkno, first_hash);
2399
2400 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2401 p_blkno, first_hash, num_clusters, xs);
2402
2403out:
2404 return ret;
2405}
2406
Tao Ma0c044f02008-08-18 17:38:50 +08002407static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2408 u64 blkno,
2409 u32 clusters,
2410 xattr_bucket_func *func,
2411 void *para)
2412{
2413 int i, j, ret = 0;
2414 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2415 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2416 u32 num_buckets = clusters * bpc;
2417 struct ocfs2_xattr_bucket bucket;
2418
2419 memset(&bucket, 0, sizeof(bucket));
2420
2421 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2422 clusters, blkno);
2423
2424 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2425 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2426 blkno, blk_per_bucket,
2427 bucket.bhs, OCFS2_BH_CACHED, inode);
2428 if (ret) {
2429 mlog_errno(ret);
2430 goto out;
2431 }
2432
2433 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2434 /*
2435 * The real bucket num in this series of blocks is stored
2436 * in the 1st bucket.
2437 */
2438 if (i == 0)
2439 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2440
2441 mlog(0, "iterating xattr bucket %llu\n", blkno);
2442 if (func) {
2443 ret = func(inode, &bucket, para);
2444 if (ret) {
2445 mlog_errno(ret);
2446 break;
2447 }
2448 }
2449
2450 for (j = 0; j < blk_per_bucket; j++)
2451 brelse(bucket.bhs[j]);
2452 memset(&bucket, 0, sizeof(bucket));
2453 }
2454
2455out:
2456 for (j = 0; j < blk_per_bucket; j++)
2457 brelse(bucket.bhs[j]);
2458
2459 return ret;
2460}
2461
2462struct ocfs2_xattr_tree_list {
2463 char *buffer;
2464 size_t buffer_size;
2465};
2466
2467static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2468 struct ocfs2_xattr_header *xh,
2469 int index,
2470 int *block_off,
2471 int *new_offset)
2472{
2473 u16 name_offset;
2474
2475 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2476 return -EINVAL;
2477
2478 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2479
2480 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2481 *new_offset = name_offset % inode->i_sb->s_blocksize;
2482
2483 return 0;
2484}
2485
2486static int ocfs2_list_xattr_bucket(struct inode *inode,
2487 struct ocfs2_xattr_bucket *bucket,
2488 void *para)
2489{
2490 int ret = 0;
2491 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2492 size_t size;
2493 int i, block_off, new_offset;
2494
2495 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2496 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2497 struct xattr_handler *handler =
2498 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2499
2500 if (handler) {
2501 ret = ocfs2_xattr_bucket_get_name_value(inode,
2502 bucket->xh,
2503 i,
2504 &block_off,
2505 &new_offset);
2506 if (ret)
2507 break;
2508 size = handler->list(inode, xl->buffer, xl->buffer_size,
2509 bucket->bhs[block_off]->b_data +
2510 new_offset,
2511 entry->xe_name_len);
2512 if (xl->buffer) {
2513 if (size > xl->buffer_size)
2514 return -ERANGE;
2515 xl->buffer += size;
2516 }
2517 xl->buffer_size -= size;
2518 }
2519 }
2520
2521 return ret;
2522}
2523
2524static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2525 struct ocfs2_xattr_tree_root *xt,
2526 char *buffer,
2527 size_t buffer_size)
2528{
2529 struct ocfs2_extent_list *el = &xt->xt_list;
2530 int ret = 0;
2531 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2532 u64 p_blkno = 0;
2533 struct ocfs2_xattr_tree_list xl = {
2534 .buffer = buffer,
2535 .buffer_size = buffer_size,
2536 };
2537
2538 if (le16_to_cpu(el->l_next_free_rec) == 0)
2539 return 0;
2540
2541 while (name_hash > 0) {
2542 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2543 &e_cpos, &num_clusters, el);
2544 if (ret) {
2545 mlog_errno(ret);
2546 goto out;
2547 }
2548
2549 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2550 ocfs2_list_xattr_bucket,
2551 &xl);
2552 if (ret) {
2553 mlog_errno(ret);
2554 goto out;
2555 }
2556
2557 if (e_cpos == 0)
2558 break;
2559
2560 name_hash = e_cpos - 1;
2561 }
2562
2563 ret = buffer_size - xl.buffer_size;
2564out:
2565 return ret;
2566}
Tao Ma01225592008-08-18 17:38:53 +08002567
2568static int cmp_xe(const void *a, const void *b)
2569{
2570 const struct ocfs2_xattr_entry *l = a, *r = b;
2571 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2572 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2573
2574 if (l_hash > r_hash)
2575 return 1;
2576 if (l_hash < r_hash)
2577 return -1;
2578 return 0;
2579}
2580
2581static void swap_xe(void *a, void *b, int size)
2582{
2583 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2584
2585 tmp = *l;
2586 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2587 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2588}
2589
2590/*
2591 * When the ocfs2_xattr_block is filled up, new bucket will be created
2592 * and all the xattr entries will be moved to the new bucket.
2593 * Note: we need to sort the entries since they are not saved in order
2594 * in the ocfs2_xattr_block.
2595 */
2596static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2597 struct buffer_head *xb_bh,
2598 struct buffer_head *xh_bh,
2599 struct buffer_head *data_bh)
2600{
2601 int i, blocksize = inode->i_sb->s_blocksize;
2602 u16 offset, size, off_change;
2603 struct ocfs2_xattr_entry *xe;
2604 struct ocfs2_xattr_block *xb =
2605 (struct ocfs2_xattr_block *)xb_bh->b_data;
2606 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2607 struct ocfs2_xattr_header *xh =
2608 (struct ocfs2_xattr_header *)xh_bh->b_data;
2609 u16 count = le16_to_cpu(xb_xh->xh_count);
2610 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2611
2612 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2613 (unsigned long long)xb_bh->b_blocknr,
2614 (unsigned long long)xh_bh->b_blocknr);
2615
2616 memset(xh_bh->b_data, 0, blocksize);
2617 if (data_bh)
2618 memset(data_bh->b_data, 0, blocksize);
2619 /*
2620 * Since the xe_name_offset is based on ocfs2_xattr_header,
2621 * there is a offset change corresponding to the change of
2622 * ocfs2_xattr_header's position.
2623 */
2624 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2625 xe = &xb_xh->xh_entries[count - 1];
2626 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2627 size = blocksize - offset;
2628
2629 /* copy all the names and values. */
2630 if (data_bh)
2631 target = data_bh->b_data;
2632 memcpy(target + offset, src + offset, size);
2633
2634 /* Init new header now. */
2635 xh->xh_count = xb_xh->xh_count;
2636 xh->xh_num_buckets = cpu_to_le16(1);
2637 xh->xh_name_value_len = cpu_to_le16(size);
2638 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2639
2640 /* copy all the entries. */
2641 target = xh_bh->b_data;
2642 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2643 size = count * sizeof(struct ocfs2_xattr_entry);
2644 memcpy(target + offset, (char *)xb_xh + offset, size);
2645
2646 /* Change the xe offset for all the xe because of the move. */
2647 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2648 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2649 for (i = 0; i < count; i++)
2650 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2651
2652 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2653 offset, size, off_change);
2654
2655 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2656 cmp_xe, swap_xe);
2657}
2658
2659/*
2660 * After we move xattr from block to index btree, we have to
2661 * update ocfs2_xattr_search to the new xe and base.
2662 *
2663 * When the entry is in xattr block, xattr_bh indicates the storage place.
2664 * While if the entry is in index b-tree, "bucket" indicates the
2665 * real place of the xattr.
2666 */
2667static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2668 struct ocfs2_xattr_search *xs,
2669 struct buffer_head *old_bh,
2670 struct buffer_head *new_bh)
2671{
2672 int ret = 0;
2673 char *buf = old_bh->b_data;
2674 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2675 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2676 int i, blocksize = inode->i_sb->s_blocksize;
2677 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2678
2679 xs->bucket.bhs[0] = new_bh;
2680 get_bh(new_bh);
2681 xs->bucket.xh = (struct ocfs2_xattr_header *)xs->bucket.bhs[0]->b_data;
2682 xs->header = xs->bucket.xh;
2683
2684 xs->base = new_bh->b_data;
2685 xs->end = xs->base + inode->i_sb->s_blocksize;
2686
2687 if (!xs->not_found) {
2688 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
2689 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2690 xs->bucket.bhs[0]->b_blocknr + 1,
2691 blk_per_bucket - 1, &xs->bucket.bhs[1],
2692 OCFS2_BH_CACHED, inode);
2693 if (ret) {
2694 mlog_errno(ret);
2695 return ret;
2696 }
2697
2698 i = xs->here - old_xh->xh_entries;
2699 xs->here = &xs->header->xh_entries[i];
2700 }
2701 }
2702
2703 return ret;
2704}
2705
2706static int ocfs2_xattr_create_index_block(struct inode *inode,
2707 struct ocfs2_xattr_search *xs)
2708{
2709 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2710 u32 bit_off, len;
2711 u64 blkno;
2712 handle_t *handle;
2713 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2714 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2715 struct ocfs2_alloc_context *data_ac;
2716 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2717 struct buffer_head *xb_bh = xs->xattr_bh;
2718 struct ocfs2_xattr_block *xb =
2719 (struct ocfs2_xattr_block *)xb_bh->b_data;
2720 struct ocfs2_xattr_tree_root *xr;
2721 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2722 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2723
2724 mlog(0, "create xattr index block for %llu\n",
2725 (unsigned long long)xb_bh->b_blocknr);
2726
2727 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2728
2729 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2730 if (ret) {
2731 mlog_errno(ret);
2732 goto out;
2733 }
2734
2735 /*
2736 * XXX:
2737 * We can use this lock for now, and maybe move to a dedicated mutex
2738 * if performance becomes a problem later.
2739 */
2740 down_write(&oi->ip_alloc_sem);
2741
2742 /*
2743 * 3 more credits, one for xattr block update, one for the 1st block
2744 * of the new xattr bucket and one for the value/data.
2745 */
2746 credits += 3;
2747 handle = ocfs2_start_trans(osb, credits);
2748 if (IS_ERR(handle)) {
2749 ret = PTR_ERR(handle);
2750 mlog_errno(ret);
2751 goto out_sem;
2752 }
2753
2754 ret = ocfs2_journal_access(handle, inode, xb_bh,
2755 OCFS2_JOURNAL_ACCESS_WRITE);
2756 if (ret) {
2757 mlog_errno(ret);
2758 goto out_commit;
2759 }
2760
2761 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2762 if (ret) {
2763 mlog_errno(ret);
2764 goto out_commit;
2765 }
2766
2767 /*
2768 * The bucket may spread in many blocks, and
2769 * we will only touch the 1st block and the last block
2770 * in the whole bucket(one for entry and one for data).
2771 */
2772 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2773
2774 mlog(0, "allocate 1 cluster from %llu to xattr block\n", blkno);
2775
2776 xh_bh = sb_getblk(inode->i_sb, blkno);
2777 if (!xh_bh) {
2778 ret = -EIO;
2779 mlog_errno(ret);
2780 goto out_commit;
2781 }
2782
2783 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2784
2785 ret = ocfs2_journal_access(handle, inode, xh_bh,
2786 OCFS2_JOURNAL_ACCESS_CREATE);
2787 if (ret) {
2788 mlog_errno(ret);
2789 goto out_commit;
2790 }
2791
2792 if (bpb > 1) {
2793 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2794 if (!data_bh) {
2795 ret = -EIO;
2796 mlog_errno(ret);
2797 goto out_commit;
2798 }
2799
2800 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2801
2802 ret = ocfs2_journal_access(handle, inode, data_bh,
2803 OCFS2_JOURNAL_ACCESS_CREATE);
2804 if (ret) {
2805 mlog_errno(ret);
2806 goto out_commit;
2807 }
2808 }
2809
2810 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2811
2812 ocfs2_journal_dirty(handle, xh_bh);
2813 if (data_bh)
2814 ocfs2_journal_dirty(handle, data_bh);
2815
2816 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2817
2818 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2819 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2820 offsetof(struct ocfs2_xattr_block, xb_attrs));
2821
2822 xr = &xb->xb_attrs.xb_root;
2823 xr->xt_clusters = cpu_to_le32(1);
2824 xr->xt_last_eb_blk = 0;
2825 xr->xt_list.l_tree_depth = 0;
2826 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2827 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2828
2829 xr->xt_list.l_recs[0].e_cpos = 0;
2830 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2831 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2832
2833 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2834
2835 ret = ocfs2_journal_dirty(handle, xb_bh);
2836 if (ret) {
2837 mlog_errno(ret);
2838 goto out_commit;
2839 }
2840
2841out_commit:
2842 ocfs2_commit_trans(osb, handle);
2843
2844out_sem:
2845 up_write(&oi->ip_alloc_sem);
2846
2847out:
2848 if (data_ac)
2849 ocfs2_free_alloc_context(data_ac);
2850
2851 brelse(xh_bh);
2852 brelse(data_bh);
2853
2854 return ret;
2855}
2856
2857static int cmp_xe_offset(const void *a, const void *b)
2858{
2859 const struct ocfs2_xattr_entry *l = a, *r = b;
2860 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2861 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2862
2863 if (l_name_offset < r_name_offset)
2864 return 1;
2865 if (l_name_offset > r_name_offset)
2866 return -1;
2867 return 0;
2868}
2869
2870/*
2871 * defrag a xattr bucket if we find that the bucket has some
2872 * holes beteen name/value pairs.
2873 * We will move all the name/value pairs to the end of the bucket
2874 * so that we can spare some space for insertion.
2875 */
2876static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2877 struct ocfs2_xattr_bucket *bucket)
2878{
2879 int ret, i;
2880 size_t end, offset, len, value_len;
2881 struct ocfs2_xattr_header *xh;
2882 char *entries, *buf, *bucket_buf = NULL;
2883 u64 blkno = bucket->bhs[0]->b_blocknr;
2884 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2885 u16 xh_free_start;
2886 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2887 size_t blocksize = inode->i_sb->s_blocksize;
2888 handle_t *handle;
2889 struct buffer_head **bhs;
2890 struct ocfs2_xattr_entry *xe;
2891
2892 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2893 GFP_NOFS);
2894 if (!bhs)
2895 return -ENOMEM;
2896
2897 ret = ocfs2_read_blocks(osb, blkno, blk_per_bucket, bhs,
2898 OCFS2_BH_CACHED, inode);
2899 if (ret)
2900 goto out;
2901
2902 /*
2903 * In order to make the operation more efficient and generic,
2904 * we copy all the blocks into a contiguous memory and do the
2905 * defragment there, so if anything is error, we will not touch
2906 * the real block.
2907 */
2908 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2909 if (!bucket_buf) {
2910 ret = -EIO;
2911 goto out;
2912 }
2913
2914 buf = bucket_buf;
2915 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2916 memcpy(buf, bhs[i]->b_data, blocksize);
2917
2918 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2919 if (IS_ERR(handle)) {
2920 ret = PTR_ERR(handle);
2921 handle = NULL;
2922 mlog_errno(ret);
2923 goto out;
2924 }
2925
2926 for (i = 0; i < blk_per_bucket; i++) {
2927 ret = ocfs2_journal_access(handle, inode, bhs[i],
2928 OCFS2_JOURNAL_ACCESS_WRITE);
2929 if (ret < 0) {
2930 mlog_errno(ret);
2931 goto commit;
2932 }
2933 }
2934
2935 xh = (struct ocfs2_xattr_header *)bucket_buf;
2936 entries = (char *)xh->xh_entries;
2937 xh_free_start = le16_to_cpu(xh->xh_free_start);
2938
2939 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2940 "xh_free_start = %u, xh_name_value_len = %u.\n",
2941 blkno, le16_to_cpu(xh->xh_count), xh_free_start,
2942 le16_to_cpu(xh->xh_name_value_len));
2943
2944 /*
2945 * sort all the entries by their offset.
2946 * the largest will be the first, so that we can
2947 * move them to the end one by one.
2948 */
2949 sort(entries, le16_to_cpu(xh->xh_count),
2950 sizeof(struct ocfs2_xattr_entry),
2951 cmp_xe_offset, swap_xe);
2952
2953 /* Move all name/values to the end of the bucket. */
2954 xe = xh->xh_entries;
2955 end = OCFS2_XATTR_BUCKET_SIZE;
2956 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2957 offset = le16_to_cpu(xe->xe_name_offset);
2958 if (ocfs2_xattr_is_local(xe))
2959 value_len = OCFS2_XATTR_SIZE(
2960 le64_to_cpu(xe->xe_value_size));
2961 else
2962 value_len = OCFS2_XATTR_ROOT_SIZE;
2963 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2964
2965 /*
2966 * We must make sure that the name/value pair
2967 * exist in the same block. So adjust end to
2968 * the previous block end if needed.
2969 */
2970 if (((end - len) / blocksize !=
2971 (end - 1) / blocksize))
2972 end = end - end % blocksize;
2973
2974 if (end > offset + len) {
2975 memmove(bucket_buf + end - len,
2976 bucket_buf + offset, len);
2977 xe->xe_name_offset = cpu_to_le16(end - len);
2978 }
2979
2980 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2981 "bucket %llu\n", (unsigned long long)blkno);
2982
2983 end -= len;
2984 }
2985
2986 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2987 "bucket %llu\n", (unsigned long long)blkno);
2988
2989 if (xh_free_start == end)
2990 goto commit;
2991
2992 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2993 xh->xh_free_start = cpu_to_le16(end);
2994
2995 /* sort the entries by their name_hash. */
2996 sort(entries, le16_to_cpu(xh->xh_count),
2997 sizeof(struct ocfs2_xattr_entry),
2998 cmp_xe, swap_xe);
2999
3000 buf = bucket_buf;
3001 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
3002 memcpy(bhs[i]->b_data, buf, blocksize);
3003 ocfs2_journal_dirty(handle, bhs[i]);
3004 }
3005
3006commit:
3007 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3008out:
3009
3010 if (bhs) {
3011 for (i = 0; i < blk_per_bucket; i++)
3012 brelse(bhs[i]);
3013 }
3014 kfree(bhs);
3015
3016 kfree(bucket_buf);
3017 return ret;
3018}
3019
3020/*
3021 * Move half nums of the xattr bucket in the previous cluster to this new
3022 * cluster. We only touch the last cluster of the previous extend record.
3023 *
3024 * first_bh is the first buffer_head of a series of bucket in the same
3025 * extent rec and header_bh is the header of one bucket in this cluster.
3026 * They will be updated if we move the data header_bh contains to the new
3027 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3028 */
3029static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3030 handle_t *handle,
3031 struct buffer_head **first_bh,
3032 struct buffer_head **header_bh,
3033 u64 new_blkno,
3034 u64 prev_blkno,
3035 u32 num_clusters,
3036 u32 *first_hash)
3037{
3038 int i, ret, credits;
3039 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3040 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3041 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3042 int blocksize = inode->i_sb->s_blocksize;
3043 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3044 struct ocfs2_xattr_header *new_xh;
3045 struct ocfs2_xattr_header *xh =
3046 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3047
3048 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3049 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3050
3051 prev_bh = *first_bh;
3052 get_bh(prev_bh);
3053 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3054
3055 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3056
3057 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
3058 prev_blkno, new_blkno);
3059
3060 /*
3061 * We need to update the 1st half of the new cluster and
3062 * 1 more for the update of the 1st bucket of the previous
3063 * extent record.
3064 */
3065 credits = bpc / 2 + 1;
3066 ret = ocfs2_extend_trans(handle, credits);
3067 if (ret) {
3068 mlog_errno(ret);
3069 goto out;
3070 }
3071
3072 ret = ocfs2_journal_access(handle, inode, prev_bh,
3073 OCFS2_JOURNAL_ACCESS_WRITE);
3074 if (ret) {
3075 mlog_errno(ret);
3076 goto out;
3077 }
3078
3079 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3080 old_bh = new_bh = NULL;
3081 new_bh = sb_getblk(inode->i_sb, new_blkno);
3082 if (!new_bh) {
3083 ret = -EIO;
3084 mlog_errno(ret);
3085 goto out;
3086 }
3087
3088 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3089
3090 ret = ocfs2_journal_access(handle, inode, new_bh,
3091 OCFS2_JOURNAL_ACCESS_CREATE);
3092 if (ret < 0) {
3093 mlog_errno(ret);
3094 brelse(new_bh);
3095 goto out;
3096 }
3097
3098 ret = ocfs2_read_block(osb, prev_blkno,
3099 &old_bh, OCFS2_BH_CACHED, inode);
3100 if (ret < 0) {
3101 mlog_errno(ret);
3102 brelse(new_bh);
3103 goto out;
3104 }
3105
3106 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3107
3108 if (i == 0) {
3109 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3110 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3111
3112 if (first_hash)
3113 *first_hash = le32_to_cpu(
3114 new_xh->xh_entries[0].xe_name_hash);
3115 new_first_bh = new_bh;
3116 get_bh(new_first_bh);
3117 }
3118
3119 ocfs2_journal_dirty(handle, new_bh);
3120
3121 if (*header_bh == old_bh) {
3122 brelse(*header_bh);
3123 *header_bh = new_bh;
3124 get_bh(*header_bh);
3125
3126 brelse(*first_bh);
3127 *first_bh = new_first_bh;
3128 get_bh(*first_bh);
3129 }
3130 brelse(new_bh);
3131 brelse(old_bh);
3132 }
3133
3134 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3135
3136 ocfs2_journal_dirty(handle, prev_bh);
3137out:
3138 brelse(prev_bh);
3139 brelse(new_first_bh);
3140 return ret;
3141}
3142
3143static int ocfs2_read_xattr_bucket(struct inode *inode,
3144 u64 blkno,
3145 struct buffer_head **bhs,
3146 int new)
3147{
3148 int ret = 0;
3149 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3150
3151 if (!new)
3152 return ocfs2_read_blocks(OCFS2_SB(inode->i_sb), blkno,
3153 blk_per_bucket, bhs,
3154 OCFS2_BH_CACHED, inode);
3155
3156 for (i = 0; i < blk_per_bucket; i++) {
3157 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3158 if (bhs[i] == NULL) {
3159 ret = -EIO;
3160 mlog_errno(ret);
3161 break;
3162 }
3163 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3164 }
3165
3166 return ret;
3167}
3168
3169/*
3170 * Move half num of the xattrs in old bucket(blk) to new bucket(new_blk).
3171 * first_hash will record the 1st hash of the new bucket.
3172 */
3173static int ocfs2_half_xattr_bucket(struct inode *inode,
3174 handle_t *handle,
3175 u64 blk,
3176 u64 new_blk,
3177 u32 *first_hash,
3178 int new_bucket_head)
3179{
3180 int ret, i;
3181 u16 count, start, len, name_value_len, xe_len, name_offset;
3182 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3183 struct buffer_head **s_bhs, **t_bhs = NULL;
3184 struct ocfs2_xattr_header *xh;
3185 struct ocfs2_xattr_entry *xe;
3186 int blocksize = inode->i_sb->s_blocksize;
3187
3188 mlog(0, "move half of xattrs from bucket %llu to %llu\n",
3189 blk, new_blk);
3190
3191 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3192 if (!s_bhs)
3193 return -ENOMEM;
3194
3195 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3196 if (ret) {
3197 mlog_errno(ret);
3198 goto out;
3199 }
3200
3201 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3202 OCFS2_JOURNAL_ACCESS_WRITE);
3203 if (ret) {
3204 mlog_errno(ret);
3205 goto out;
3206 }
3207
3208 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3209 if (!t_bhs) {
3210 ret = -ENOMEM;
3211 goto out;
3212 }
3213
3214 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3215 if (ret) {
3216 mlog_errno(ret);
3217 goto out;
3218 }
3219
3220 for (i = 0; i < blk_per_bucket; i++) {
3221 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3222 OCFS2_JOURNAL_ACCESS_CREATE);
3223 if (ret) {
3224 mlog_errno(ret);
3225 goto out;
3226 }
3227 }
3228
3229 /* copy the whole bucket to the new first. */
3230 for (i = 0; i < blk_per_bucket; i++)
3231 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3232
3233 /* update the new bucket. */
3234 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3235 count = le16_to_cpu(xh->xh_count);
3236 start = count / 2;
3237
3238 /*
3239 * Calculate the total name/value len and xh_free_start for
3240 * the old bucket first.
3241 */
3242 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3243 name_value_len = 0;
3244 for (i = 0; i < start; i++) {
3245 xe = &xh->xh_entries[i];
3246 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3247 if (ocfs2_xattr_is_local(xe))
3248 xe_len +=
3249 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3250 else
3251 xe_len += OCFS2_XATTR_ROOT_SIZE;
3252 name_value_len += xe_len;
3253 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3254 name_offset = le16_to_cpu(xe->xe_name_offset);
3255 }
3256
3257 /*
3258 * Now begin the modification to the new bucket.
3259 *
3260 * In the new bucket, We just move the xattr entry to the beginning
3261 * and don't touch the name/value. So there will be some holes in the
3262 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3263 * called.
3264 */
3265 xe = &xh->xh_entries[start];
3266 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3267 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003268 (int)((char *)xe - (char *)xh),
3269 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003270 memmove((char *)xh->xh_entries, (char *)xe, len);
3271 xe = &xh->xh_entries[count - start];
3272 len = sizeof(struct ocfs2_xattr_entry) * start;
3273 memset((char *)xe, 0, len);
3274
3275 le16_add_cpu(&xh->xh_count, -start);
3276 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3277
3278 /* Calculate xh_free_start for the new bucket. */
3279 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3280 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3281 xe = &xh->xh_entries[i];
3282 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3283 if (ocfs2_xattr_is_local(xe))
3284 xe_len +=
3285 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3286 else
3287 xe_len += OCFS2_XATTR_ROOT_SIZE;
3288 if (le16_to_cpu(xe->xe_name_offset) <
3289 le16_to_cpu(xh->xh_free_start))
3290 xh->xh_free_start = xe->xe_name_offset;
3291 }
3292
3293 /* set xh->xh_num_buckets for the new xh. */
3294 if (new_bucket_head)
3295 xh->xh_num_buckets = cpu_to_le16(1);
3296 else
3297 xh->xh_num_buckets = 0;
3298
3299 for (i = 0; i < blk_per_bucket; i++) {
3300 ocfs2_journal_dirty(handle, t_bhs[i]);
3301 if (ret)
3302 mlog_errno(ret);
3303 }
3304
3305 /* store the first_hash of the new bucket. */
3306 if (first_hash)
3307 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3308
3309 /*
3310 * Now only update the 1st block of the old bucket.
3311 * Please note that the entry has been sorted already above.
3312 */
3313 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3314 memset(&xh->xh_entries[start], 0,
3315 sizeof(struct ocfs2_xattr_entry) * (count - start));
3316 xh->xh_count = cpu_to_le16(start);
3317 xh->xh_free_start = cpu_to_le16(name_offset);
3318 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3319
3320 ocfs2_journal_dirty(handle, s_bhs[0]);
3321 if (ret)
3322 mlog_errno(ret);
3323
3324out:
3325 if (s_bhs) {
3326 for (i = 0; i < blk_per_bucket; i++)
3327 brelse(s_bhs[i]);
3328 }
3329 kfree(s_bhs);
3330
3331 if (t_bhs) {
3332 for (i = 0; i < blk_per_bucket; i++)
3333 brelse(t_bhs[i]);
3334 }
3335 kfree(t_bhs);
3336
3337 return ret;
3338}
3339
3340/*
3341 * Copy xattr from one bucket to another bucket.
3342 *
3343 * The caller must make sure that the journal transaction
3344 * has enough space for journaling.
3345 */
3346static int ocfs2_cp_xattr_bucket(struct inode *inode,
3347 handle_t *handle,
3348 u64 s_blkno,
3349 u64 t_blkno,
3350 int t_is_new)
3351{
3352 int ret, i;
3353 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3354 int blocksize = inode->i_sb->s_blocksize;
3355 struct buffer_head **s_bhs, **t_bhs = NULL;
3356
3357 BUG_ON(s_blkno == t_blkno);
3358
3359 mlog(0, "cp bucket %llu to %llu, target is %d\n",
3360 s_blkno, t_blkno, t_is_new);
3361
3362 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3363 GFP_NOFS);
3364 if (!s_bhs)
3365 return -ENOMEM;
3366
3367 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3368 if (ret)
3369 goto out;
3370
3371 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3372 GFP_NOFS);
3373 if (!t_bhs) {
3374 ret = -ENOMEM;
3375 goto out;
3376 }
3377
3378 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3379 if (ret)
3380 goto out;
3381
3382 for (i = 0; i < blk_per_bucket; i++) {
3383 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
3384 OCFS2_JOURNAL_ACCESS_WRITE);
3385 if (ret)
3386 goto out;
3387 }
3388
3389 for (i = 0; i < blk_per_bucket; i++) {
3390 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3391 ocfs2_journal_dirty(handle, t_bhs[i]);
3392 }
3393
3394out:
3395 if (s_bhs) {
3396 for (i = 0; i < blk_per_bucket; i++)
3397 brelse(s_bhs[i]);
3398 }
3399 kfree(s_bhs);
3400
3401 if (t_bhs) {
3402 for (i = 0; i < blk_per_bucket; i++)
3403 brelse(t_bhs[i]);
3404 }
3405 kfree(t_bhs);
3406
3407 return ret;
3408}
3409
3410/*
3411 * Copy one xattr cluster from src_blk to to_blk.
3412 * The to_blk will become the first bucket header of the cluster, so its
3413 * xh_num_buckets will be initialized as the bucket num in the cluster.
3414 */
3415static int ocfs2_cp_xattr_cluster(struct inode *inode,
3416 handle_t *handle,
3417 struct buffer_head *first_bh,
3418 u64 src_blk,
3419 u64 to_blk,
3420 u32 *first_hash)
3421{
3422 int i, ret, credits;
3423 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3424 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3425 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3426 struct buffer_head *bh = NULL;
3427 struct ocfs2_xattr_header *xh;
3428 u64 to_blk_start = to_blk;
3429
3430 mlog(0, "cp xattrs from cluster %llu to %llu\n", src_blk, to_blk);
3431
3432 /*
3433 * We need to update the new cluster and 1 more for the update of
3434 * the 1st bucket of the previous extent rec.
3435 */
3436 credits = bpc + 1;
3437 ret = ocfs2_extend_trans(handle, credits);
3438 if (ret) {
3439 mlog_errno(ret);
3440 goto out;
3441 }
3442
3443 ret = ocfs2_journal_access(handle, inode, first_bh,
3444 OCFS2_JOURNAL_ACCESS_WRITE);
3445 if (ret) {
3446 mlog_errno(ret);
3447 goto out;
3448 }
3449
3450 for (i = 0; i < num_buckets; i++) {
3451 ret = ocfs2_cp_xattr_bucket(inode, handle,
3452 src_blk, to_blk, 1);
3453 if (ret) {
3454 mlog_errno(ret);
3455 goto out;
3456 }
3457
3458 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3459 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3460 }
3461
3462 /* update the old bucket header. */
3463 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3464 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3465
3466 ocfs2_journal_dirty(handle, first_bh);
3467
3468 /* update the new bucket header. */
3469 ret = ocfs2_read_block(osb, to_blk_start, &bh, OCFS2_BH_CACHED, inode);
3470 if (ret < 0) {
3471 mlog_errno(ret);
3472 goto out;
3473 }
3474
3475 ret = ocfs2_journal_access(handle, inode, bh,
3476 OCFS2_JOURNAL_ACCESS_WRITE);
3477 if (ret) {
3478 mlog_errno(ret);
3479 goto out;
3480 }
3481
3482 xh = (struct ocfs2_xattr_header *)bh->b_data;
3483 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3484
3485 ocfs2_journal_dirty(handle, bh);
3486
3487 if (first_hash)
3488 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3489out:
3490 brelse(bh);
3491 return ret;
3492}
3493
3494/*
3495 * Move half of the xattrs in this cluster to the new cluster.
3496 * This function should only be called when bucket size == cluster size.
3497 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3498 */
3499static int ocfs2_half_xattr_cluster(struct inode *inode,
3500 handle_t *handle,
3501 u64 prev_blk,
3502 u64 new_blk,
3503 u32 *first_hash)
3504{
3505 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3506 int ret, credits = 2 * blk_per_bucket;
3507
3508 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3509
3510 ret = ocfs2_extend_trans(handle, credits);
3511 if (ret) {
3512 mlog_errno(ret);
3513 return ret;
3514 }
3515
3516 /* Move half of the xattr in start_blk to the next bucket. */
3517 return ocfs2_half_xattr_bucket(inode, handle, prev_blk,
3518 new_blk, first_hash, 1);
3519}
3520
3521/*
3522 * Move some xattrs from the old cluster to the new one since they are not
3523 * contiguous in ocfs2 xattr tree.
3524 *
3525 * new_blk starts a new separate cluster, and we will move some xattrs from
3526 * prev_blk to it. v_start will be set as the first name hash value in this
3527 * new cluster so that it can be used as e_cpos during tree insertion and
3528 * don't collide with our original b-tree operations. first_bh and header_bh
3529 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3530 * to extend the insert bucket.
3531 *
3532 * The problem is how much xattr should we move to the new one and when should
3533 * we update first_bh and header_bh?
3534 * 1. If cluster size > bucket size, that means the previous cluster has more
3535 * than 1 bucket, so just move half nums of bucket into the new cluster and
3536 * update the first_bh and header_bh if the insert bucket has been moved
3537 * to the new cluster.
3538 * 2. If cluster_size == bucket_size:
3539 * a) If the previous extent rec has more than one cluster and the insert
3540 * place isn't in the last cluster, copy the entire last cluster to the
3541 * new one. This time, we don't need to upate the first_bh and header_bh
3542 * since they will not be moved into the new cluster.
3543 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3544 * the new one. And we set the extend flag to zero if the insert place is
3545 * moved into the new allocated cluster since no extend is needed.
3546 */
3547static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3548 handle_t *handle,
3549 struct buffer_head **first_bh,
3550 struct buffer_head **header_bh,
3551 u64 new_blk,
3552 u64 prev_blk,
3553 u32 prev_clusters,
3554 u32 *v_start,
3555 int *extend)
3556{
3557 int ret = 0;
3558 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3559
3560 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
3561 prev_blk, prev_clusters, new_blk);
3562
3563 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3564 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3565 handle,
3566 first_bh,
3567 header_bh,
3568 new_blk,
3569 prev_blk,
3570 prev_clusters,
3571 v_start);
3572 else {
3573 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3574
3575 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3576 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3577 last_blk, new_blk,
3578 v_start);
3579 else {
3580 ret = ocfs2_half_xattr_cluster(inode, handle,
3581 last_blk, new_blk,
3582 v_start);
3583
3584 if ((*header_bh)->b_blocknr == last_blk && extend)
3585 *extend = 0;
3586 }
3587 }
3588
3589 return ret;
3590}
3591
3592/*
3593 * Add a new cluster for xattr storage.
3594 *
3595 * If the new cluster is contiguous with the previous one, it will be
3596 * appended to the same extent record, and num_clusters will be updated.
3597 * If not, we will insert a new extent for it and move some xattrs in
3598 * the last cluster into the new allocated one.
3599 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3600 * lose the benefits of hashing because we'll have to search large leaves.
3601 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3602 * if it's bigger).
3603 *
3604 * first_bh is the first block of the previous extent rec and header_bh
3605 * indicates the bucket we will insert the new xattrs. They will be updated
3606 * when the header_bh is moved into the new cluster.
3607 */
3608static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3609 struct buffer_head *root_bh,
3610 struct buffer_head **first_bh,
3611 struct buffer_head **header_bh,
3612 u32 *num_clusters,
3613 u32 prev_cpos,
3614 u64 prev_blkno,
3615 int *extend)
3616{
3617 int ret, credits;
3618 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3619 u32 prev_clusters = *num_clusters;
3620 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3621 u64 block;
3622 handle_t *handle = NULL;
3623 struct ocfs2_alloc_context *data_ac = NULL;
3624 struct ocfs2_alloc_context *meta_ac = NULL;
3625 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003626 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08003627
3628 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3629 "previous xattr blkno = %llu\n",
3630 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3631 prev_cpos, prev_blkno);
3632
Joel Becker8d6220d2008-08-22 12:46:09 -07003633 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003634
3635 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3636 &data_ac, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08003637 if (ret) {
3638 mlog_errno(ret);
3639 goto leave;
3640 }
3641
Joel Beckerf99b9b72008-08-20 19:36:33 -07003642 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3643 clusters_to_add);
Tao Ma01225592008-08-18 17:38:53 +08003644 handle = ocfs2_start_trans(osb, credits);
3645 if (IS_ERR(handle)) {
3646 ret = PTR_ERR(handle);
3647 handle = NULL;
3648 mlog_errno(ret);
3649 goto leave;
3650 }
3651
3652 ret = ocfs2_journal_access(handle, inode, root_bh,
3653 OCFS2_JOURNAL_ACCESS_WRITE);
3654 if (ret < 0) {
3655 mlog_errno(ret);
3656 goto leave;
3657 }
3658
3659 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3660 clusters_to_add, &bit_off, &num_bits);
3661 if (ret < 0) {
3662 if (ret != -ENOSPC)
3663 mlog_errno(ret);
3664 goto leave;
3665 }
3666
3667 BUG_ON(num_bits > clusters_to_add);
3668
3669 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3670 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3671 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3672
3673 if (prev_blkno + prev_clusters * bpc == block &&
3674 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3675 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3676 /*
3677 * If this cluster is contiguous with the old one and
3678 * adding this new cluster, we don't surpass the limit of
3679 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3680 * initialized and used like other buckets in the previous
3681 * cluster.
3682 * So add it as a contiguous one. The caller will handle
3683 * its init process.
3684 */
3685 v_start = prev_cpos + prev_clusters;
3686 *num_clusters = prev_clusters + num_bits;
3687 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3688 num_bits);
3689 } else {
3690 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3691 handle,
3692 first_bh,
3693 header_bh,
3694 block,
3695 prev_blkno,
3696 prev_clusters,
3697 &v_start,
3698 extend);
3699 if (ret) {
3700 mlog_errno(ret);
3701 goto leave;
3702 }
3703 }
3704
Tao Ma28b8ca02008-09-01 08:45:18 +08003705 if (handle->h_buffer_credits < credits) {
3706 /*
3707 * The journal has been restarted before, and don't
3708 * have enough space for the insertion, so extend it
3709 * here.
3710 */
3711 ret = ocfs2_extend_trans(handle, credits);
3712 if (ret) {
3713 mlog_errno(ret);
3714 goto leave;
3715 }
3716 }
Tao Ma01225592008-08-18 17:38:53 +08003717 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
3718 num_bits, block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003719 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3720 num_bits, 0, meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08003721 if (ret < 0) {
3722 mlog_errno(ret);
3723 goto leave;
3724 }
3725
3726 ret = ocfs2_journal_dirty(handle, root_bh);
3727 if (ret < 0) {
3728 mlog_errno(ret);
3729 goto leave;
3730 }
3731
3732leave:
3733 if (handle)
3734 ocfs2_commit_trans(osb, handle);
3735 if (data_ac)
3736 ocfs2_free_alloc_context(data_ac);
3737 if (meta_ac)
3738 ocfs2_free_alloc_context(meta_ac);
3739
3740 return ret;
3741}
3742
3743/*
3744 * Extend a new xattr bucket and move xattrs to the end one by one until
3745 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3746 */
3747static int ocfs2_extend_xattr_bucket(struct inode *inode,
3748 struct buffer_head *first_bh,
3749 struct buffer_head *start_bh,
3750 u32 num_clusters)
3751{
3752 int ret, credits;
3753 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3754 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3755 u64 start_blk = start_bh->b_blocknr, end_blk;
3756 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3757 handle_t *handle;
3758 struct ocfs2_xattr_header *first_xh =
3759 (struct ocfs2_xattr_header *)first_bh->b_data;
3760 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3761
3762 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
3763 "from %llu, len = %u\n", start_blk,
3764 (unsigned long long)first_bh->b_blocknr, num_clusters);
3765
3766 BUG_ON(bucket >= num_buckets);
3767
3768 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3769
3770 /*
3771 * We will touch all the buckets after the start_bh(include it).
3772 * Add one more bucket and modify the first_bh.
3773 */
3774 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3775 handle = ocfs2_start_trans(osb, credits);
3776 if (IS_ERR(handle)) {
3777 ret = PTR_ERR(handle);
3778 handle = NULL;
3779 mlog_errno(ret);
3780 goto out;
3781 }
3782
3783 ret = ocfs2_journal_access(handle, inode, first_bh,
3784 OCFS2_JOURNAL_ACCESS_WRITE);
3785 if (ret) {
3786 mlog_errno(ret);
3787 goto commit;
3788 }
3789
3790 while (end_blk != start_blk) {
3791 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3792 end_blk + blk_per_bucket, 0);
3793 if (ret)
3794 goto commit;
3795 end_blk -= blk_per_bucket;
3796 }
3797
3798 /* Move half of the xattr in start_blk to the next bucket. */
3799 ret = ocfs2_half_xattr_bucket(inode, handle, start_blk,
3800 start_blk + blk_per_bucket, NULL, 0);
3801
3802 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3803 ocfs2_journal_dirty(handle, first_bh);
3804
3805commit:
3806 ocfs2_commit_trans(osb, handle);
3807out:
3808 return ret;
3809}
3810
3811/*
3812 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3813 * xb_bh is the ocfs2_xattr_block.
3814 * We will move all the buckets starting from header_bh to the next place. As
3815 * for this one, half num of its xattrs will be moved to the next one.
3816 *
3817 * We will allocate a new cluster if current cluster is full and adjust
3818 * header_bh and first_bh if the insert place is moved to the new cluster.
3819 */
3820static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3821 struct buffer_head *xb_bh,
3822 struct buffer_head *header_bh)
3823{
3824 struct ocfs2_xattr_header *first_xh = NULL;
3825 struct buffer_head *first_bh = NULL;
3826 struct ocfs2_xattr_block *xb =
3827 (struct ocfs2_xattr_block *)xb_bh->b_data;
3828 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3829 struct ocfs2_extent_list *el = &xb_root->xt_list;
3830 struct ocfs2_xattr_header *xh =
3831 (struct ocfs2_xattr_header *)header_bh->b_data;
3832 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3833 struct super_block *sb = inode->i_sb;
3834 struct ocfs2_super *osb = OCFS2_SB(sb);
3835 int ret, num_buckets, extend = 1;
3836 u64 p_blkno;
3837 u32 e_cpos, num_clusters;
3838
3839 mlog(0, "Add new xattr bucket starting form %llu\n",
3840 (unsigned long long)header_bh->b_blocknr);
3841
3842 /*
3843 * Add refrence for header_bh here because it may be
3844 * changed in ocfs2_add_new_xattr_cluster and we need
3845 * to free it in the end.
3846 */
3847 get_bh(header_bh);
3848
3849 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3850 &num_clusters, el);
3851 if (ret) {
3852 mlog_errno(ret);
3853 goto out;
3854 }
3855
3856 ret = ocfs2_read_block(osb, p_blkno,
3857 &first_bh, OCFS2_BH_CACHED, inode);
3858 if (ret) {
3859 mlog_errno(ret);
3860 goto out;
3861 }
3862
3863 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3864 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3865
3866 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3867 ret = ocfs2_add_new_xattr_cluster(inode,
3868 xb_bh,
3869 &first_bh,
3870 &header_bh,
3871 &num_clusters,
3872 e_cpos,
3873 p_blkno,
3874 &extend);
3875 if (ret) {
3876 mlog_errno(ret);
3877 goto out;
3878 }
3879 }
3880
3881 if (extend)
3882 ret = ocfs2_extend_xattr_bucket(inode,
3883 first_bh,
3884 header_bh,
3885 num_clusters);
3886 if (ret)
3887 mlog_errno(ret);
3888out:
3889 brelse(first_bh);
3890 brelse(header_bh);
3891 return ret;
3892}
3893
3894static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3895 struct ocfs2_xattr_bucket *bucket,
3896 int offs)
3897{
3898 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3899
3900 offs = offs % inode->i_sb->s_blocksize;
3901 return bucket->bhs[block_off]->b_data + offs;
3902}
3903
3904/*
3905 * Handle the normal xattr set, including replace, delete and new.
3906 * When the bucket is empty, "is_empty" is set and the caller can
3907 * free this bucket.
3908 *
3909 * Note: "local" indicates the real data's locality. So we can't
3910 * just its bucket locality by its length.
3911 */
3912static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3913 struct ocfs2_xattr_info *xi,
3914 struct ocfs2_xattr_search *xs,
3915 u32 name_hash,
3916 int local,
3917 int *is_empty)
3918{
3919 struct ocfs2_xattr_entry *last, *xe;
3920 int name_len = strlen(xi->name);
3921 struct ocfs2_xattr_header *xh = xs->header;
3922 u16 count = le16_to_cpu(xh->xh_count), start;
3923 size_t blocksize = inode->i_sb->s_blocksize;
3924 char *val;
3925 size_t offs, size, new_size;
3926
3927 last = &xh->xh_entries[count];
3928 if (!xs->not_found) {
3929 xe = xs->here;
3930 offs = le16_to_cpu(xe->xe_name_offset);
3931 if (ocfs2_xattr_is_local(xe))
3932 size = OCFS2_XATTR_SIZE(name_len) +
3933 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3934 else
3935 size = OCFS2_XATTR_SIZE(name_len) +
3936 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3937
3938 /*
3939 * If the new value will be stored outside, xi->value has been
3940 * initalized as an empty ocfs2_xattr_value_root, and the same
3941 * goes with xi->value_len, so we can set new_size safely here.
3942 * See ocfs2_xattr_set_in_bucket.
3943 */
3944 new_size = OCFS2_XATTR_SIZE(name_len) +
3945 OCFS2_XATTR_SIZE(xi->value_len);
3946
3947 le16_add_cpu(&xh->xh_name_value_len, -size);
3948 if (xi->value) {
3949 if (new_size > size)
3950 goto set_new_name_value;
3951
3952 /* Now replace the old value with new one. */
3953 if (local)
3954 xe->xe_value_size = cpu_to_le64(xi->value_len);
3955 else
3956 xe->xe_value_size = 0;
3957
3958 val = ocfs2_xattr_bucket_get_val(inode,
3959 &xs->bucket, offs);
3960 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3961 size - OCFS2_XATTR_SIZE(name_len));
3962 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3963 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3964 xi->value, xi->value_len);
3965
3966 le16_add_cpu(&xh->xh_name_value_len, new_size);
3967 ocfs2_xattr_set_local(xe, local);
3968 return;
3969 } else {
3970 /* Remove the old entry. */
3971 last -= 1;
3972 memmove(xe, xe + 1,
3973 (void *)last - (void *)xe);
3974 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
3975 le16_add_cpu(&xh->xh_count, -1);
3976 if (xh->xh_count == 0 && is_empty)
3977 *is_empty = 1;
3978 return;
3979 }
3980 } else {
3981 /* find a new entry for insert. */
3982 int low = 0, high = count - 1, tmp;
3983 struct ocfs2_xattr_entry *tmp_xe;
3984
3985 while (low <= high) {
3986 tmp = (low + high) / 2;
3987 tmp_xe = &xh->xh_entries[tmp];
3988
3989 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
3990 low = tmp + 1;
3991 else if (name_hash <
3992 le32_to_cpu(tmp_xe->xe_name_hash))
3993 high = tmp - 1;
3994 else
3995 break;
3996 }
3997
3998 xe = &xh->xh_entries[low];
3999 if (low != count)
4000 memmove(xe + 1, xe, (void *)last - (void *)xe);
4001
4002 le16_add_cpu(&xh->xh_count, 1);
4003 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4004 xe->xe_name_hash = cpu_to_le32(name_hash);
4005 xe->xe_name_len = name_len;
4006 ocfs2_xattr_set_type(xe, xi->name_index);
4007 }
4008
4009set_new_name_value:
4010 /* Insert the new name+value. */
4011 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4012
4013 /*
4014 * We must make sure that the name/value pair
4015 * exists in the same block.
4016 */
4017 offs = le16_to_cpu(xh->xh_free_start);
4018 start = offs - size;
4019
4020 if (start >> inode->i_sb->s_blocksize_bits !=
4021 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4022 offs = offs - offs % blocksize;
4023 xh->xh_free_start = cpu_to_le16(offs);
4024 }
4025
4026 val = ocfs2_xattr_bucket_get_val(inode,
4027 &xs->bucket, offs - size);
4028 xe->xe_name_offset = cpu_to_le16(offs - size);
4029
4030 memset(val, 0, size);
4031 memcpy(val, xi->name, name_len);
4032 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4033
4034 xe->xe_value_size = cpu_to_le64(xi->value_len);
4035 ocfs2_xattr_set_local(xe, local);
4036 xs->here = xe;
4037 le16_add_cpu(&xh->xh_free_start, -size);
4038 le16_add_cpu(&xh->xh_name_value_len, size);
4039
4040 return;
4041}
4042
4043static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4044 handle_t *handle,
4045 struct ocfs2_xattr_search *xs,
4046 struct buffer_head **bhs,
4047 u16 bh_num)
4048{
4049 int ret = 0, off, block_off;
4050 struct ocfs2_xattr_entry *xe = xs->here;
4051
4052 /*
4053 * First calculate all the blocks we should journal_access
4054 * and journal_dirty. The first block should always be touched.
4055 */
4056 ret = ocfs2_journal_dirty(handle, bhs[0]);
4057 if (ret)
4058 mlog_errno(ret);
4059
4060 /* calc the data. */
4061 off = le16_to_cpu(xe->xe_name_offset);
4062 block_off = off >> inode->i_sb->s_blocksize_bits;
4063 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4064 if (ret)
4065 mlog_errno(ret);
4066
4067 return ret;
4068}
4069
4070/*
4071 * Set the xattr entry in the specified bucket.
4072 * The bucket is indicated by xs->bucket and it should have the enough
4073 * space for the xattr insertion.
4074 */
4075static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4076 struct ocfs2_xattr_info *xi,
4077 struct ocfs2_xattr_search *xs,
4078 u32 name_hash,
4079 int local,
4080 int *bucket_empty)
4081{
4082 int i, ret;
4083 handle_t *handle = NULL;
4084 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4085 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4086
Mark Fashehff1ec202008-08-19 10:54:29 -07004087 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4088 (unsigned long)xi->value_len, xi->name_index,
Tao Ma01225592008-08-18 17:38:53 +08004089 (unsigned long long)xs->bucket.bhs[0]->b_blocknr);
4090
4091 if (!xs->bucket.bhs[1]) {
4092 ret = ocfs2_read_blocks(osb,
4093 xs->bucket.bhs[0]->b_blocknr + 1,
4094 blk_per_bucket - 1, &xs->bucket.bhs[1],
4095 OCFS2_BH_CACHED, inode);
4096 if (ret) {
4097 mlog_errno(ret);
4098 goto out;
4099 }
4100 }
4101
4102 handle = ocfs2_start_trans(osb, blk_per_bucket);
4103 if (IS_ERR(handle)) {
4104 ret = PTR_ERR(handle);
4105 handle = NULL;
4106 mlog_errno(ret);
4107 goto out;
4108 }
4109
4110 for (i = 0; i < blk_per_bucket; i++) {
4111 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[i],
4112 OCFS2_JOURNAL_ACCESS_WRITE);
4113 if (ret < 0) {
4114 mlog_errno(ret);
4115 goto out;
4116 }
4117 }
4118
4119 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash,
4120 local, bucket_empty);
4121
4122 /*Only dirty the blocks we have touched in set xattr. */
4123 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
4124 xs->bucket.bhs, blk_per_bucket);
4125 if (ret)
4126 mlog_errno(ret);
4127out:
4128 ocfs2_commit_trans(osb, handle);
4129
4130 return ret;
4131}
4132
4133static int ocfs2_xattr_value_update_size(struct inode *inode,
4134 struct buffer_head *xe_bh,
4135 struct ocfs2_xattr_entry *xe,
4136 u64 new_size)
4137{
4138 int ret;
4139 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4140 handle_t *handle = NULL;
4141
4142 handle = ocfs2_start_trans(osb, 1);
4143 if (handle == NULL) {
4144 ret = -ENOMEM;
4145 mlog_errno(ret);
4146 goto out;
4147 }
4148
4149 ret = ocfs2_journal_access(handle, inode, xe_bh,
4150 OCFS2_JOURNAL_ACCESS_WRITE);
4151 if (ret < 0) {
4152 mlog_errno(ret);
4153 goto out_commit;
4154 }
4155
4156 xe->xe_value_size = cpu_to_le64(new_size);
4157
4158 ret = ocfs2_journal_dirty(handle, xe_bh);
4159 if (ret < 0)
4160 mlog_errno(ret);
4161
4162out_commit:
4163 ocfs2_commit_trans(osb, handle);
4164out:
4165 return ret;
4166}
4167
4168/*
4169 * Truncate the specified xe_off entry in xattr bucket.
4170 * bucket is indicated by header_bh and len is the new length.
4171 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4172 *
4173 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4174 */
4175static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4176 struct buffer_head *header_bh,
4177 int xe_off,
4178 int len)
4179{
4180 int ret, offset;
4181 u64 value_blk;
4182 struct buffer_head *value_bh = NULL;
4183 struct ocfs2_xattr_value_root *xv;
4184 struct ocfs2_xattr_entry *xe;
4185 struct ocfs2_xattr_header *xh =
4186 (struct ocfs2_xattr_header *)header_bh->b_data;
4187 size_t blocksize = inode->i_sb->s_blocksize;
4188
4189 xe = &xh->xh_entries[xe_off];
4190
4191 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4192
4193 offset = le16_to_cpu(xe->xe_name_offset) +
4194 OCFS2_XATTR_SIZE(xe->xe_name_len);
4195
4196 value_blk = offset / blocksize;
4197
4198 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4199 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4200 value_blk += header_bh->b_blocknr;
4201
4202 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), value_blk,
4203 &value_bh, OCFS2_BH_CACHED, inode);
4204 if (ret) {
4205 mlog_errno(ret);
4206 goto out;
4207 }
4208
4209 xv = (struct ocfs2_xattr_value_root *)
4210 (value_bh->b_data + offset % blocksize);
4211
4212 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4213 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4214 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4215 if (ret) {
4216 mlog_errno(ret);
4217 goto out;
4218 }
4219
4220 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4221 if (ret) {
4222 mlog_errno(ret);
4223 goto out;
4224 }
4225
4226out:
4227 brelse(value_bh);
4228 return ret;
4229}
4230
4231static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4232 struct ocfs2_xattr_search *xs,
4233 int len)
4234{
4235 int ret, offset;
4236 struct ocfs2_xattr_entry *xe = xs->here;
4237 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4238
4239 BUG_ON(!xs->bucket.bhs[0] || !xe || ocfs2_xattr_is_local(xe));
4240
4241 offset = xe - xh->xh_entries;
4242 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bhs[0],
4243 offset, len);
4244 if (ret)
4245 mlog_errno(ret);
4246
4247 return ret;
4248}
4249
4250static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4251 struct ocfs2_xattr_search *xs,
4252 char *val,
4253 int value_len)
4254{
4255 int offset;
4256 struct ocfs2_xattr_value_root *xv;
4257 struct ocfs2_xattr_entry *xe = xs->here;
4258
4259 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4260
4261 offset = le16_to_cpu(xe->xe_name_offset) +
4262 OCFS2_XATTR_SIZE(xe->xe_name_len);
4263
4264 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4265
4266 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4267}
4268
4269/*
4270 * Remove the xattr bucket pointed by bucket_bh.
4271 * All the buckets after it in the same xattr extent rec will be
4272 * move forward one by one.
4273 */
4274static int ocfs2_rm_xattr_bucket(struct inode *inode,
4275 struct buffer_head *first_bh,
4276 struct ocfs2_xattr_bucket *bucket)
4277{
4278 int ret = 0, credits;
4279 struct ocfs2_xattr_header *xh =
4280 (struct ocfs2_xattr_header *)first_bh->b_data;
4281 u16 bucket_num = le16_to_cpu(xh->xh_num_buckets);
4282 u64 end, start = bucket->bhs[0]->b_blocknr;
4283 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4284 handle_t *handle;
4285 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4286
4287 end = first_bh->b_blocknr + (bucket_num - 1) * blk_per_bucket;
4288
4289 mlog(0, "rm xattr bucket %llu\n", start);
4290 /*
4291 * We need to update the first xattr_header and all the buckets starting
4292 * from start in this xattr rec.
4293 *
4294 * XXX: Should we empty the old last bucket here?
4295 */
4296 credits = 1 + end - start;
4297 handle = ocfs2_start_trans(osb, credits);
4298 if (IS_ERR(handle)) {
4299 ret = PTR_ERR(handle);
4300 mlog_errno(ret);
4301 return ret;
4302 }
4303
4304 ret = ocfs2_journal_access(handle, inode, first_bh,
4305 OCFS2_JOURNAL_ACCESS_WRITE);
4306 if (ret) {
4307 mlog_errno(ret);
4308 goto out_commit;
4309 }
4310
4311
4312 while (start < end) {
4313 ret = ocfs2_cp_xattr_bucket(inode, handle,
4314 start + blk_per_bucket,
4315 start, 0);
4316 if (ret) {
4317 mlog_errno(ret);
4318 goto out_commit;
4319 }
4320 start += blk_per_bucket;
4321 }
4322
4323 /* update the first_bh. */
4324 xh->xh_num_buckets = cpu_to_le16(bucket_num - 1);
4325 ocfs2_journal_dirty(handle, first_bh);
4326
4327out_commit:
4328 ocfs2_commit_trans(osb, handle);
4329 return ret;
4330}
4331
4332static int ocfs2_rm_xattr_cluster(struct inode *inode,
4333 struct buffer_head *root_bh,
4334 u64 blkno,
4335 u32 cpos,
4336 u32 len)
4337{
4338 int ret;
4339 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4340 struct inode *tl_inode = osb->osb_tl_inode;
4341 handle_t *handle;
4342 struct ocfs2_xattr_block *xb =
4343 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004344 struct ocfs2_alloc_context *meta_ac = NULL;
4345 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004346 struct ocfs2_extent_tree et;
4347
Joel Becker8d6220d2008-08-22 12:46:09 -07004348 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004349
4350 ocfs2_init_dealloc_ctxt(&dealloc);
4351
4352 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4353 cpos, len, (unsigned long long)blkno);
4354
4355 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4356
Joel Beckerf99b9b72008-08-20 19:36:33 -07004357 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004358 if (ret) {
4359 mlog_errno(ret);
4360 return ret;
4361 }
4362
4363 mutex_lock(&tl_inode->i_mutex);
4364
4365 if (ocfs2_truncate_log_needs_flush(osb)) {
4366 ret = __ocfs2_flush_truncate_log(osb);
4367 if (ret < 0) {
4368 mlog_errno(ret);
4369 goto out;
4370 }
4371 }
4372
4373 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
4374 if (handle == NULL) {
4375 ret = -ENOMEM;
4376 mlog_errno(ret);
4377 goto out;
4378 }
4379
4380 ret = ocfs2_journal_access(handle, inode, root_bh,
4381 OCFS2_JOURNAL_ACCESS_WRITE);
4382 if (ret) {
4383 mlog_errno(ret);
4384 goto out_commit;
4385 }
4386
Joel Beckerf99b9b72008-08-20 19:36:33 -07004387 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4388 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004389 if (ret) {
4390 mlog_errno(ret);
4391 goto out_commit;
4392 }
4393
4394 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4395
4396 ret = ocfs2_journal_dirty(handle, root_bh);
4397 if (ret) {
4398 mlog_errno(ret);
4399 goto out_commit;
4400 }
4401
4402 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4403 if (ret)
4404 mlog_errno(ret);
4405
4406out_commit:
4407 ocfs2_commit_trans(osb, handle);
4408out:
4409 ocfs2_schedule_truncate_log_flush(osb, 1);
4410
4411 mutex_unlock(&tl_inode->i_mutex);
4412
4413 if (meta_ac)
4414 ocfs2_free_alloc_context(meta_ac);
4415
4416 ocfs2_run_deallocs(osb, &dealloc);
4417
4418 return ret;
4419}
4420
4421/*
4422 * Free the xattr bucket indicated by xs->bucket and if all the buckets
4423 * in the clusters is free, free the clusters also.
4424 */
4425static int ocfs2_xattr_bucket_shrink(struct inode *inode,
4426 struct ocfs2_xattr_info *xi,
4427 struct ocfs2_xattr_search *xs,
4428 u32 name_hash)
4429{
4430 int ret;
4431 u32 e_cpos, num_clusters;
4432 u64 p_blkno;
4433 struct buffer_head *first_bh = NULL;
4434 struct ocfs2_xattr_header *first_xh;
4435 struct ocfs2_xattr_block *xb =
4436 (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
4437
4438 BUG_ON(xs->header->xh_count != 0);
4439
4440 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4441 &e_cpos, &num_clusters,
4442 &xb->xb_attrs.xb_root.xt_list);
4443 if (ret) {
4444 mlog_errno(ret);
4445 return ret;
4446 }
4447
4448 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), p_blkno,
4449 &first_bh, OCFS2_BH_CACHED, inode);
4450 if (ret) {
4451 mlog_errno(ret);
4452 return ret;
4453 }
4454
4455 ret = ocfs2_rm_xattr_bucket(inode, first_bh, &xs->bucket);
4456 if (ret) {
4457 mlog_errno(ret);
4458 goto out;
4459 }
4460
4461 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
4462 if (first_xh->xh_num_buckets == 0)
4463 ret = ocfs2_rm_xattr_cluster(inode, xs->xattr_bh,
4464 p_blkno, e_cpos,
4465 num_clusters);
4466
4467out:
4468 brelse(first_bh);
4469 return ret;
4470}
4471
4472static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4473 struct ocfs2_xattr_search *xs)
4474{
4475 handle_t *handle = NULL;
4476 struct ocfs2_xattr_header *xh = xs->bucket.xh;
4477 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4478 le16_to_cpu(xh->xh_count) - 1];
4479 int ret = 0;
4480
4481 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4482 if (IS_ERR(handle)) {
4483 ret = PTR_ERR(handle);
4484 mlog_errno(ret);
4485 return;
4486 }
4487
4488 ret = ocfs2_journal_access(handle, inode, xs->bucket.bhs[0],
4489 OCFS2_JOURNAL_ACCESS_WRITE);
4490 if (ret) {
4491 mlog_errno(ret);
4492 goto out_commit;
4493 }
4494
4495 /* Remove the old entry. */
4496 memmove(xs->here, xs->here + 1,
4497 (void *)last - (void *)xs->here);
4498 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4499 le16_add_cpu(&xh->xh_count, -1);
4500
4501 ret = ocfs2_journal_dirty(handle, xs->bucket.bhs[0]);
4502 if (ret < 0)
4503 mlog_errno(ret);
4504out_commit:
4505 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4506}
4507
4508/*
4509 * Set the xattr name/value in the bucket specified in xs.
4510 *
4511 * As the new value in xi may be stored in the bucket or in an outside cluster,
4512 * we divide the whole process into 3 steps:
4513 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4514 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4515 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4516 * 4. If the clusters for the new outside value can't be allocated, we need
4517 * to free the xattr we allocated in set.
4518 */
4519static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4520 struct ocfs2_xattr_info *xi,
4521 struct ocfs2_xattr_search *xs)
4522{
4523 int ret, local = 1, bucket_empty = 0;
4524 size_t value_len;
4525 char *val = (char *)xi->value;
4526 struct ocfs2_xattr_entry *xe = xs->here;
4527 u32 name_hash = ocfs2_xattr_hash_by_name(inode,
4528 xi->name_index, xi->name);
4529
4530 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4531 /*
4532 * We need to truncate the xattr storage first.
4533 *
4534 * If both the old and new value are stored to
4535 * outside block, we only need to truncate
4536 * the storage and then set the value outside.
4537 *
4538 * If the new value should be stored within block,
4539 * we should free all the outside block first and
4540 * the modification to the xattr block will be done
4541 * by following steps.
4542 */
4543 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4544 value_len = xi->value_len;
4545 else
4546 value_len = 0;
4547
4548 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4549 value_len);
4550 if (ret)
4551 goto out;
4552
4553 if (value_len)
4554 goto set_value_outside;
4555 }
4556
4557 value_len = xi->value_len;
4558 /* So we have to handle the inside block change now. */
4559 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4560 /*
4561 * If the new value will be stored outside of block,
4562 * initalize a new empty value root and insert it first.
4563 */
4564 local = 0;
4565 xi->value = &def_xv;
4566 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4567 }
4568
4569 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash,
4570 local, &bucket_empty);
4571 if (ret) {
4572 mlog_errno(ret);
4573 goto out;
4574 }
4575
4576 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4577 /* allocate the space now for the outside block storage. */
4578 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4579 value_len);
4580 if (ret) {
4581 mlog_errno(ret);
4582
4583 if (xs->not_found) {
4584 /*
4585 * We can't allocate enough clusters for outside
4586 * storage and we have allocated xattr already,
4587 * so need to remove it.
4588 */
4589 ocfs2_xattr_bucket_remove_xs(inode, xs);
4590 }
4591 goto out;
4592 }
4593 } else {
4594 if (bucket_empty)
4595 ret = ocfs2_xattr_bucket_shrink(inode, xi,
4596 xs, name_hash);
4597 goto out;
4598 }
4599
4600set_value_outside:
4601 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4602out:
4603 return ret;
4604}
4605
4606/* check whether the xattr bucket is filled up with the same hash value. */
4607static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
4608 struct ocfs2_xattr_bucket *bucket)
4609{
4610 struct ocfs2_xattr_header *xh = bucket->xh;
4611
4612 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4613 xh->xh_entries[0].xe_name_hash) {
4614 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4615 "hash = %u\n",
4616 (unsigned long long)bucket->bhs[0]->b_blocknr,
4617 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4618 return -ENOSPC;
4619 }
4620
4621 return 0;
4622}
4623
4624static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4625 struct ocfs2_xattr_info *xi,
4626 struct ocfs2_xattr_search *xs)
4627{
4628 struct ocfs2_xattr_header *xh;
4629 struct ocfs2_xattr_entry *xe;
4630 u16 count, header_size, xh_free_start;
4631 int i, free, max_free, need, old;
4632 size_t value_size = 0, name_len = strlen(xi->name);
4633 size_t blocksize = inode->i_sb->s_blocksize;
4634 int ret, allocation = 0;
4635 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4636
4637 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4638
4639try_again:
4640 xh = xs->header;
4641 count = le16_to_cpu(xh->xh_count);
4642 xh_free_start = le16_to_cpu(xh->xh_free_start);
4643 header_size = sizeof(struct ocfs2_xattr_header) +
4644 count * sizeof(struct ocfs2_xattr_entry);
4645 max_free = OCFS2_XATTR_BUCKET_SIZE -
4646 le16_to_cpu(xh->xh_name_value_len) - header_size;
4647
4648 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4649 "of %u which exceed block size\n",
4650 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4651 header_size);
4652
4653 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4654 value_size = OCFS2_XATTR_ROOT_SIZE;
4655 else if (xi->value)
4656 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4657
4658 if (xs->not_found)
4659 need = sizeof(struct ocfs2_xattr_entry) +
4660 OCFS2_XATTR_SIZE(name_len) + value_size;
4661 else {
4662 need = value_size + OCFS2_XATTR_SIZE(name_len);
4663
4664 /*
4665 * We only replace the old value if the new length is smaller
4666 * than the old one. Otherwise we will allocate new space in the
4667 * bucket to store it.
4668 */
4669 xe = xs->here;
4670 if (ocfs2_xattr_is_local(xe))
4671 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4672 else
4673 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4674
4675 if (old >= value_size)
4676 need = 0;
4677 }
4678
4679 free = xh_free_start - header_size;
4680 /*
4681 * We need to make sure the new name/value pair
4682 * can exist in the same block.
4683 */
4684 if (xh_free_start % blocksize < need)
4685 free -= xh_free_start % blocksize;
4686
4687 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4688 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4689 " %u\n", xs->not_found,
4690 (unsigned long long)xs->bucket.bhs[0]->b_blocknr,
4691 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4692 le16_to_cpu(xh->xh_name_value_len));
4693
4694 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4695 if (need <= max_free &&
4696 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4697 /*
4698 * We can create the space by defragment. Since only the
4699 * name/value will be moved, the xe shouldn't be changed
4700 * in xs.
4701 */
4702 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4703 if (ret) {
4704 mlog_errno(ret);
4705 goto out;
4706 }
4707
4708 xh_free_start = le16_to_cpu(xh->xh_free_start);
4709 free = xh_free_start - header_size;
4710 if (xh_free_start % blocksize < need)
4711 free -= xh_free_start % blocksize;
4712
4713 if (free >= need)
4714 goto xattr_set;
4715
4716 mlog(0, "Can't get enough space for xattr insert by "
4717 "defragment. Need %u bytes, but we have %d, so "
4718 "allocate new bucket for it.\n", need, free);
4719 }
4720
4721 /*
4722 * We have to add new buckets or clusters and one
4723 * allocation should leave us enough space for insert.
4724 */
4725 BUG_ON(allocation);
4726
4727 /*
4728 * We do not allow for overlapping ranges between buckets. And
4729 * the maximum number of collisions we will allow for then is
4730 * one bucket's worth, so check it here whether we need to
4731 * add a new bucket for the insert.
4732 */
4733 ret = ocfs2_check_xattr_bucket_collision(inode, &xs->bucket);
4734 if (ret) {
4735 mlog_errno(ret);
4736 goto out;
4737 }
4738
4739 ret = ocfs2_add_new_xattr_bucket(inode,
4740 xs->xattr_bh,
4741 xs->bucket.bhs[0]);
4742 if (ret) {
4743 mlog_errno(ret);
4744 goto out;
4745 }
4746
4747 for (i = 0; i < blk_per_bucket; i++)
4748 brelse(xs->bucket.bhs[i]);
4749
4750 memset(&xs->bucket, 0, sizeof(xs->bucket));
4751
4752 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4753 xi->name_index,
4754 xi->name, xs);
4755 if (ret && ret != -ENODATA)
4756 goto out;
4757 xs->not_found = ret;
4758 allocation = 1;
4759 goto try_again;
4760 }
4761
4762xattr_set:
4763 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4764out:
4765 mlog_exit(ret);
4766 return ret;
4767}
Tao Maa3944252008-08-18 17:38:54 +08004768
4769static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4770 struct ocfs2_xattr_bucket *bucket,
4771 void *para)
4772{
4773 int ret = 0;
4774 struct ocfs2_xattr_header *xh = bucket->xh;
4775 u16 i;
4776 struct ocfs2_xattr_entry *xe;
4777
4778 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4779 xe = &xh->xh_entries[i];
4780 if (ocfs2_xattr_is_local(xe))
4781 continue;
4782
4783 ret = ocfs2_xattr_bucket_value_truncate(inode,
4784 bucket->bhs[0],
4785 i, 0);
4786 if (ret) {
4787 mlog_errno(ret);
4788 break;
4789 }
4790 }
4791
4792 return ret;
4793}
4794
4795static int ocfs2_delete_xattr_index_block(struct inode *inode,
4796 struct buffer_head *xb_bh)
4797{
4798 struct ocfs2_xattr_block *xb =
4799 (struct ocfs2_xattr_block *)xb_bh->b_data;
4800 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4801 int ret = 0;
4802 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4803 u64 p_blkno;
4804
4805 if (le16_to_cpu(el->l_next_free_rec) == 0)
4806 return 0;
4807
4808 while (name_hash > 0) {
4809 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4810 &e_cpos, &num_clusters, el);
4811 if (ret) {
4812 mlog_errno(ret);
4813 goto out;
4814 }
4815
4816 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4817 ocfs2_delete_xattr_in_bucket,
4818 NULL);
4819 if (ret) {
4820 mlog_errno(ret);
4821 goto out;
4822 }
4823
4824 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4825 p_blkno, e_cpos, num_clusters);
4826 if (ret) {
4827 mlog_errno(ret);
4828 break;
4829 }
4830
4831 if (e_cpos == 0)
4832 break;
4833
4834 name_hash = e_cpos - 1;
4835 }
4836
4837out:
4838 return ret;
4839}