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