blob: 8594df36640eccfebb385dd8ae55160db652e7fc [file] [log] [blame]
Tao Maf56654c2008-08-18 17:38:48 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
Tiger Yangc3cb6822008-10-23 16:33:03 +08006 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
Tao Maf56654c2008-08-18 17:38:48 +08007 *
Tiger Yangcf1d6c72008-08-18 17:11:00 +08008 * CREDITS:
Tiger Yangc3cb6822008-10-23 16:33:03 +08009 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080011 *
Tao Maf56654c2008-08-18 17:38:48 +080012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
Tiger Yangc3cb6822008-10-23 16:33:03 +080014 * License version 2 as published by the Free Software Foundation.
Tao Maf56654c2008-08-18 17:38:48 +080015 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
Tao Maf56654c2008-08-18 17:38:48 +080020 */
21
Tiger Yangcf1d6c72008-08-18 17:11:00 +080022#include <linux/capability.h>
23#include <linux/fs.h>
24#include <linux/types.h>
25#include <linux/slab.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h>
28#include <linux/uio.h>
29#include <linux/sched.h>
30#include <linux/splice.h>
31#include <linux/mount.h>
32#include <linux/writeback.h>
33#include <linux/falloc.h>
Tao Ma01225592008-08-18 17:38:53 +080034#include <linux/sort.h>
Mark Fasheh99219ae2008-10-07 14:52:59 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080038
Tao Maf56654c2008-08-18 17:38:48 +080039#define MLOG_MASK_PREFIX ML_XATTR
40#include <cluster/masklog.h>
41
42#include "ocfs2.h"
43#include "alloc.h"
44#include "dlmglue.h"
45#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080046#include "symlink.h"
47#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080048#include "inode.h"
49#include "journal.h"
50#include "ocfs2_fs.h"
51#include "suballoc.h"
52#include "uptodate.h"
53#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080054#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080055#include "xattr.h"
56
57
58struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
61};
62
Tao Ma0c044f02008-08-18 17:38:50 +080063struct ocfs2_xattr_bucket {
Joel Becker4ac60322008-10-18 19:11:42 -070064 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
65 struct ocfs2_xattr_header *bu_xh;
Tao Ma0c044f02008-08-18 17:38:50 +080066};
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
Joel Becker9c7759a2008-10-24 16:21:03 -0700157#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700158#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker9c7759a2008-10-24 16:21:03 -0700159
Tao Ma936b8832008-10-09 23:06:14 +0800160static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800161{
162 struct xattr_handler *handler = NULL;
163
164 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
165 handler = ocfs2_xattr_handler_map[name_index];
166
Tao Ma936b8832008-10-09 23:06:14 +0800167 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800168}
169
Mark Fasheh40daa162008-10-07 14:31:42 -0700170static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800171 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700172 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800173{
174 /* Get hash value of uuid from super block */
175 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
176 int i;
177
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800178 /* hash extended attribute name */
179 for (i = 0; i < name_len; i++) {
180 hash = (hash << OCFS2_HASH_SHIFT) ^
181 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
182 *name++;
183 }
184
185 return hash;
186}
187
188/*
189 * ocfs2_xattr_hash_entry()
190 *
191 * Compute the hash of an extended attribute.
192 */
193static void ocfs2_xattr_hash_entry(struct inode *inode,
194 struct ocfs2_xattr_header *header,
195 struct ocfs2_xattr_entry *entry)
196{
197 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800198 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800199
Tao Ma2057e5c2008-10-09 23:06:13 +0800200 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800201 entry->xe_name_hash = cpu_to_le32(hash);
202
203 return;
204}
Tao Maf56654c2008-08-18 17:38:48 +0800205
206static int ocfs2_xattr_extend_allocation(struct inode *inode,
207 u32 clusters_to_add,
208 struct buffer_head *xattr_bh,
209 struct ocfs2_xattr_value_root *xv)
210{
211 int status = 0;
212 int restart_func = 0;
213 int credits = 0;
214 handle_t *handle = NULL;
215 struct ocfs2_alloc_context *data_ac = NULL;
216 struct ocfs2_alloc_context *meta_ac = NULL;
217 enum ocfs2_alloc_restarted why;
218 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Maf56654c2008-08-18 17:38:48 +0800219 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700220 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800221
222 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
223
Joel Becker8d6220d2008-08-22 12:46:09 -0700224 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700225
Tao Maf56654c2008-08-18 17:38:48 +0800226restart_all:
227
Joel Beckerf99b9b72008-08-20 19:36:33 -0700228 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
229 &data_ac, &meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +0800230 if (status) {
231 mlog_errno(status);
232 goto leave;
233 }
234
Joel Beckerf99b9b72008-08-20 19:36:33 -0700235 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
236 clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800237 handle = ocfs2_start_trans(osb, credits);
238 if (IS_ERR(handle)) {
239 status = PTR_ERR(handle);
240 handle = NULL;
241 mlog_errno(status);
242 goto leave;
243 }
244
245restarted_transaction:
246 status = ocfs2_journal_access(handle, inode, xattr_bh,
247 OCFS2_JOURNAL_ACCESS_WRITE);
248 if (status < 0) {
249 mlog_errno(status);
250 goto leave;
251 }
252
253 prev_clusters = le32_to_cpu(xv->xr_clusters);
254 status = ocfs2_add_clusters_in_btree(osb,
255 inode,
256 &logical_start,
257 clusters_to_add,
258 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700259 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800260 handle,
261 data_ac,
262 meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700263 &why);
Tao Maf56654c2008-08-18 17:38:48 +0800264 if ((status < 0) && (status != -EAGAIN)) {
265 if (status != -ENOSPC)
266 mlog_errno(status);
267 goto leave;
268 }
269
270 status = ocfs2_journal_dirty(handle, xattr_bh);
271 if (status < 0) {
272 mlog_errno(status);
273 goto leave;
274 }
275
276 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
277
278 if (why != RESTART_NONE && clusters_to_add) {
279 if (why == RESTART_META) {
280 mlog(0, "restarting function.\n");
281 restart_func = 1;
282 } else {
283 BUG_ON(why != RESTART_TRANS);
284
285 mlog(0, "restarting transaction.\n");
286 /* TODO: This can be more intelligent. */
287 credits = ocfs2_calc_extend_credits(osb->sb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700288 et.et_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800289 clusters_to_add);
290 status = ocfs2_extend_trans(handle, credits);
291 if (status < 0) {
292 /* handle still has to be committed at
293 * this point. */
294 status = -ENOMEM;
295 mlog_errno(status);
296 goto leave;
297 }
298 goto restarted_transaction;
299 }
300 }
301
302leave:
303 if (handle) {
304 ocfs2_commit_trans(osb, handle);
305 handle = NULL;
306 }
307 if (data_ac) {
308 ocfs2_free_alloc_context(data_ac);
309 data_ac = NULL;
310 }
311 if (meta_ac) {
312 ocfs2_free_alloc_context(meta_ac);
313 meta_ac = NULL;
314 }
315 if ((!status) && restart_func) {
316 restart_func = 0;
317 goto restart_all;
318 }
319
320 return status;
321}
322
323static int __ocfs2_remove_xattr_range(struct inode *inode,
324 struct buffer_head *root_bh,
325 struct ocfs2_xattr_value_root *xv,
326 u32 cpos, u32 phys_cpos, u32 len,
327 struct ocfs2_cached_dealloc_ctxt *dealloc)
328{
329 int ret;
330 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
331 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
332 struct inode *tl_inode = osb->osb_tl_inode;
333 handle_t *handle;
334 struct ocfs2_alloc_context *meta_ac = NULL;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700335 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800336
Joel Becker8d6220d2008-08-22 12:46:09 -0700337 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700338
339 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +0800340 if (ret) {
341 mlog_errno(ret);
342 return ret;
343 }
344
345 mutex_lock(&tl_inode->i_mutex);
346
347 if (ocfs2_truncate_log_needs_flush(osb)) {
348 ret = __ocfs2_flush_truncate_log(osb);
349 if (ret < 0) {
350 mlog_errno(ret);
351 goto out;
352 }
353 }
354
355 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
356 if (IS_ERR(handle)) {
357 ret = PTR_ERR(handle);
358 mlog_errno(ret);
359 goto out;
360 }
361
362 ret = ocfs2_journal_access(handle, inode, root_bh,
363 OCFS2_JOURNAL_ACCESS_WRITE);
364 if (ret) {
365 mlog_errno(ret);
366 goto out_commit;
367 }
368
Joel Beckerf99b9b72008-08-20 19:36:33 -0700369 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
370 dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800371 if (ret) {
372 mlog_errno(ret);
373 goto out_commit;
374 }
375
376 le32_add_cpu(&xv->xr_clusters, -len);
377
378 ret = ocfs2_journal_dirty(handle, root_bh);
379 if (ret) {
380 mlog_errno(ret);
381 goto out_commit;
382 }
383
384 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
385 if (ret)
386 mlog_errno(ret);
387
388out_commit:
389 ocfs2_commit_trans(osb, handle);
390out:
391 mutex_unlock(&tl_inode->i_mutex);
392
393 if (meta_ac)
394 ocfs2_free_alloc_context(meta_ac);
395
396 return ret;
397}
398
399static int ocfs2_xattr_shrink_size(struct inode *inode,
400 u32 old_clusters,
401 u32 new_clusters,
402 struct buffer_head *root_bh,
403 struct ocfs2_xattr_value_root *xv)
404{
405 int ret = 0;
406 u32 trunc_len, cpos, phys_cpos, alloc_size;
407 u64 block;
408 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
409 struct ocfs2_cached_dealloc_ctxt dealloc;
410
411 ocfs2_init_dealloc_ctxt(&dealloc);
412
413 if (old_clusters <= new_clusters)
414 return 0;
415
416 cpos = new_clusters;
417 trunc_len = old_clusters - new_clusters;
418 while (trunc_len) {
419 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
420 &alloc_size, &xv->xr_list);
421 if (ret) {
422 mlog_errno(ret);
423 goto out;
424 }
425
426 if (alloc_size > trunc_len)
427 alloc_size = trunc_len;
428
429 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
430 phys_cpos, alloc_size,
431 &dealloc);
432 if (ret) {
433 mlog_errno(ret);
434 goto out;
435 }
436
437 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
438 ocfs2_remove_xattr_clusters_from_cache(inode, block,
439 alloc_size);
440 cpos += alloc_size;
441 trunc_len -= alloc_size;
442 }
443
444out:
445 ocfs2_schedule_truncate_log_flush(osb, 1);
446 ocfs2_run_deallocs(osb, &dealloc);
447
448 return ret;
449}
450
451static int ocfs2_xattr_value_truncate(struct inode *inode,
452 struct buffer_head *root_bh,
453 struct ocfs2_xattr_value_root *xv,
454 int len)
455{
456 int ret;
457 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
458 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
459
460 if (new_clusters == old_clusters)
461 return 0;
462
463 if (new_clusters > old_clusters)
464 ret = ocfs2_xattr_extend_allocation(inode,
465 new_clusters - old_clusters,
466 root_bh, xv);
467 else
468 ret = ocfs2_xattr_shrink_size(inode,
469 old_clusters, new_clusters,
470 root_bh, xv);
471
472 return ret;
473}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800474
Tao Ma936b8832008-10-09 23:06:14 +0800475static int ocfs2_xattr_list_entry(char *buffer, size_t size,
476 size_t *result, const char *prefix,
477 const char *name, int name_len)
478{
479 char *p = buffer + *result;
480 int prefix_len = strlen(prefix);
481 int total_len = prefix_len + name_len + 1;
482
483 *result += total_len;
484
485 /* we are just looking for how big our buffer needs to be */
486 if (!size)
487 return 0;
488
489 if (*result > size)
490 return -ERANGE;
491
492 memcpy(p, prefix, prefix_len);
493 memcpy(p + prefix_len, name, name_len);
494 p[prefix_len + name_len] = '\0';
495
496 return 0;
497}
498
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800499static int ocfs2_xattr_list_entries(struct inode *inode,
500 struct ocfs2_xattr_header *header,
501 char *buffer, size_t buffer_size)
502{
Tao Ma936b8832008-10-09 23:06:14 +0800503 size_t result = 0;
504 int i, type, ret;
505 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800506
507 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
508 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800509 type = ocfs2_xattr_get_type(entry);
510 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800511
Tao Ma936b8832008-10-09 23:06:14 +0800512 if (prefix) {
513 name = (const char *)header +
514 le16_to_cpu(entry->xe_name_offset);
515
516 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
517 &result, prefix, name,
518 entry->xe_name_len);
519 if (ret)
520 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800521 }
522 }
523
Tao Ma936b8832008-10-09 23:06:14 +0800524 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800525}
526
527static int ocfs2_xattr_ibody_list(struct inode *inode,
528 struct ocfs2_dinode *di,
529 char *buffer,
530 size_t buffer_size)
531{
532 struct ocfs2_xattr_header *header = NULL;
533 struct ocfs2_inode_info *oi = OCFS2_I(inode);
534 int ret = 0;
535
536 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
537 return ret;
538
539 header = (struct ocfs2_xattr_header *)
540 ((void *)di + inode->i_sb->s_blocksize -
541 le16_to_cpu(di->i_xattr_inline_size));
542
543 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
544
545 return ret;
546}
547
548static int ocfs2_xattr_block_list(struct inode *inode,
549 struct ocfs2_dinode *di,
550 char *buffer,
551 size_t buffer_size)
552{
553 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800554 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800555 int ret = 0;
556
557 if (!di->i_xattr_loc)
558 return ret;
559
Joel Becker0fcaa562008-10-09 17:20:31 -0700560 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800561 if (ret < 0) {
562 mlog_errno(ret);
563 return ret;
564 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800565
Tao Ma0c044f02008-08-18 17:38:50 +0800566 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Joel Beckerf6087fb2008-10-20 18:20:43 -0700567 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
568 ret = -EIO;
569 goto cleanup;
570 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800571
Tao Ma0c044f02008-08-18 17:38:50 +0800572 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
573 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
574 ret = ocfs2_xattr_list_entries(inode, header,
575 buffer, buffer_size);
576 } else {
577 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
578 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
579 buffer, buffer_size);
580 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800581cleanup:
582 brelse(blk_bh);
583
584 return ret;
585}
586
587ssize_t ocfs2_listxattr(struct dentry *dentry,
588 char *buffer,
589 size_t size)
590{
591 int ret = 0, i_ret = 0, b_ret = 0;
592 struct buffer_head *di_bh = NULL;
593 struct ocfs2_dinode *di = NULL;
594 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
595
Tiger Yang8154da32008-08-18 17:11:46 +0800596 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
597 return -EOPNOTSUPP;
598
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800599 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
600 return ret;
601
602 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
603 if (ret < 0) {
604 mlog_errno(ret);
605 return ret;
606 }
607
608 di = (struct ocfs2_dinode *)di_bh->b_data;
609
610 down_read(&oi->ip_xattr_sem);
611 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
612 if (i_ret < 0)
613 b_ret = 0;
614 else {
615 if (buffer) {
616 buffer += i_ret;
617 size -= i_ret;
618 }
619 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
620 buffer, size);
621 if (b_ret < 0)
622 i_ret = 0;
623 }
624 up_read(&oi->ip_xattr_sem);
625 ocfs2_inode_unlock(dentry->d_inode, 0);
626
627 brelse(di_bh);
628
629 return i_ret + b_ret;
630}
631
632static int ocfs2_xattr_find_entry(int name_index,
633 const char *name,
634 struct ocfs2_xattr_search *xs)
635{
636 struct ocfs2_xattr_entry *entry;
637 size_t name_len;
638 int i, cmp = 1;
639
640 if (name == NULL)
641 return -EINVAL;
642
643 name_len = strlen(name);
644 entry = xs->here;
645 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
646 cmp = name_index - ocfs2_xattr_get_type(entry);
647 if (!cmp)
648 cmp = name_len - entry->xe_name_len;
649 if (!cmp)
650 cmp = memcmp(name, (xs->base +
651 le16_to_cpu(entry->xe_name_offset)),
652 name_len);
653 if (cmp == 0)
654 break;
655 entry += 1;
656 }
657 xs->here = entry;
658
659 return cmp ? -ENODATA : 0;
660}
661
662static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800663 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800664 void *buffer,
665 size_t len)
666{
667 u32 cpos, p_cluster, num_clusters, bpc, clusters;
668 u64 blkno;
669 int i, ret = 0;
670 size_t cplen, blocksize;
671 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800672 struct ocfs2_extent_list *el;
673
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800674 el = &xv->xr_list;
675 clusters = le32_to_cpu(xv->xr_clusters);
676 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
677 blocksize = inode->i_sb->s_blocksize;
678
679 cpos = 0;
680 while (cpos < clusters) {
681 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
682 &num_clusters, el);
683 if (ret) {
684 mlog_errno(ret);
685 goto out;
686 }
687
688 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
689 /* Copy ocfs2_xattr_value */
690 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa562008-10-09 17:20:31 -0700691 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800692 if (ret) {
693 mlog_errno(ret);
694 goto out;
695 }
696
697 cplen = len >= blocksize ? blocksize : len;
698 memcpy(buffer, bh->b_data, cplen);
699 len -= cplen;
700 buffer += cplen;
701
702 brelse(bh);
703 bh = NULL;
704 if (len == 0)
705 break;
706 }
707 cpos += num_clusters;
708 }
709out:
710 return ret;
711}
712
713static int ocfs2_xattr_ibody_get(struct inode *inode,
714 int name_index,
715 const char *name,
716 void *buffer,
717 size_t buffer_size,
718 struct ocfs2_xattr_search *xs)
719{
720 struct ocfs2_inode_info *oi = OCFS2_I(inode);
721 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800722 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800723 size_t size;
724 int ret = 0;
725
726 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
727 return -ENODATA;
728
729 xs->end = (void *)di + inode->i_sb->s_blocksize;
730 xs->header = (struct ocfs2_xattr_header *)
731 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
732 xs->base = (void *)xs->header;
733 xs->here = xs->header->xh_entries;
734
735 ret = ocfs2_xattr_find_entry(name_index, name, xs);
736 if (ret)
737 return ret;
738 size = le64_to_cpu(xs->here->xe_value_size);
739 if (buffer) {
740 if (size > buffer_size)
741 return -ERANGE;
742 if (ocfs2_xattr_is_local(xs->here)) {
743 memcpy(buffer, (void *)xs->base +
744 le16_to_cpu(xs->here->xe_name_offset) +
745 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
746 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800747 xv = (struct ocfs2_xattr_value_root *)
748 (xs->base + le16_to_cpu(
749 xs->here->xe_name_offset) +
750 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
751 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800752 buffer, size);
753 if (ret < 0) {
754 mlog_errno(ret);
755 return ret;
756 }
757 }
758 }
759
760 return size;
761}
762
763static int ocfs2_xattr_block_get(struct inode *inode,
764 int name_index,
765 const char *name,
766 void *buffer,
767 size_t buffer_size,
768 struct ocfs2_xattr_search *xs)
769{
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800770 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +0800771 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800772 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +0800773 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800774
Tao Ma589dc262008-08-18 17:38:51 +0800775 memset(&xs->bucket, 0, sizeof(xs->bucket));
776
Joel Becker54f443f2008-10-20 18:43:07 -0700777 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
778 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800779 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800780 goto cleanup;
781 }
782
Tiger Yang6c1e1832008-11-02 19:04:21 +0800783 if (xs->not_found) {
784 ret = -ENODATA;
785 goto cleanup;
786 }
787
Joel Becker54f443f2008-10-20 18:43:07 -0700788 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800789 size = le64_to_cpu(xs->here->xe_value_size);
790 if (buffer) {
791 ret = -ERANGE;
792 if (size > buffer_size)
793 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +0800794
795 name_offset = le16_to_cpu(xs->here->xe_name_offset);
796 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
797 i = xs->here - xs->header->xh_entries;
798
799 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
800 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker4ac60322008-10-18 19:11:42 -0700801 xs->bucket.bu_xh,
Tao Ma589dc262008-08-18 17:38:51 +0800802 i,
803 &block_off,
804 &name_offset);
Joel Becker51def392008-10-24 16:57:21 -0700805 xs->base = bucket_block(&xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +0800806 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800807 if (ocfs2_xattr_is_local(xs->here)) {
808 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +0800809 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800810 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800811 xv = (struct ocfs2_xattr_value_root *)
812 (xs->base + name_offset + name_len);
813 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800814 buffer, size);
815 if (ret < 0) {
816 mlog_errno(ret);
817 goto cleanup;
818 }
819 }
820 }
821 ret = size;
822cleanup:
Tao Ma589dc262008-08-18 17:38:51 +0800823 for (i = 0; i < OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET; i++)
Joel Becker4ac60322008-10-18 19:11:42 -0700824 brelse(xs->bucket.bu_bhs[i]);
Tao Ma589dc262008-08-18 17:38:51 +0800825 memset(&xs->bucket, 0, sizeof(xs->bucket));
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800826
Joel Becker54f443f2008-10-20 18:43:07 -0700827 brelse(xs->xattr_bh);
828 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800829 return ret;
830}
831
832/* ocfs2_xattr_get()
833 *
834 * Copy an extended attribute into the buffer provided.
835 * Buffer is NULL to compute the size of buffer required.
836 */
Tiger Yang0030e002008-10-23 16:33:33 +0800837static int ocfs2_xattr_get(struct inode *inode,
838 int name_index,
839 const char *name,
840 void *buffer,
841 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800842{
843 int ret;
844 struct ocfs2_dinode *di = NULL;
845 struct buffer_head *di_bh = NULL;
846 struct ocfs2_inode_info *oi = OCFS2_I(inode);
847 struct ocfs2_xattr_search xis = {
848 .not_found = -ENODATA,
849 };
850 struct ocfs2_xattr_search xbs = {
851 .not_found = -ENODATA,
852 };
853
Tiger Yang8154da32008-08-18 17:11:46 +0800854 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
855 return -EOPNOTSUPP;
856
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800857 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
858 ret = -ENODATA;
859
860 ret = ocfs2_inode_lock(inode, &di_bh, 0);
861 if (ret < 0) {
862 mlog_errno(ret);
863 return ret;
864 }
865 xis.inode_bh = xbs.inode_bh = di_bh;
866 di = (struct ocfs2_dinode *)di_bh->b_data;
867
868 down_read(&oi->ip_xattr_sem);
869 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
870 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +0800871 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800872 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
873 buffer_size, &xbs);
874 up_read(&oi->ip_xattr_sem);
875 ocfs2_inode_unlock(inode, 0);
876
877 brelse(di_bh);
878
879 return ret;
880}
881
882static int __ocfs2_xattr_set_value_outside(struct inode *inode,
883 struct ocfs2_xattr_value_root *xv,
884 const void *value,
885 int value_len)
886{
887 int ret = 0, i, cp_len, credits;
888 u16 blocksize = inode->i_sb->s_blocksize;
889 u32 p_cluster, num_clusters;
890 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
891 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
892 u64 blkno;
893 struct buffer_head *bh = NULL;
894 handle_t *handle;
895
896 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
897
898 credits = clusters * bpc;
899 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
900 if (IS_ERR(handle)) {
901 ret = PTR_ERR(handle);
902 mlog_errno(ret);
903 goto out;
904 }
905
906 while (cpos < clusters) {
907 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
908 &num_clusters, &xv->xr_list);
909 if (ret) {
910 mlog_errno(ret);
911 goto out_commit;
912 }
913
914 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
915
916 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa562008-10-09 17:20:31 -0700917 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800918 if (ret) {
919 mlog_errno(ret);
920 goto out_commit;
921 }
922
923 ret = ocfs2_journal_access(handle,
924 inode,
925 bh,
926 OCFS2_JOURNAL_ACCESS_WRITE);
927 if (ret < 0) {
928 mlog_errno(ret);
929 goto out_commit;
930 }
931
932 cp_len = value_len > blocksize ? blocksize : value_len;
933 memcpy(bh->b_data, value, cp_len);
934 value_len -= cp_len;
935 value += cp_len;
936 if (cp_len < blocksize)
937 memset(bh->b_data + cp_len, 0,
938 blocksize - cp_len);
939
940 ret = ocfs2_journal_dirty(handle, bh);
941 if (ret < 0) {
942 mlog_errno(ret);
943 goto out_commit;
944 }
945 brelse(bh);
946 bh = NULL;
947
948 /*
949 * XXX: do we need to empty all the following
950 * blocks in this cluster?
951 */
952 if (!value_len)
953 break;
954 }
955 cpos += num_clusters;
956 }
957out_commit:
958 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
959out:
960 brelse(bh);
961
962 return ret;
963}
964
965static int ocfs2_xattr_cleanup(struct inode *inode,
966 struct ocfs2_xattr_info *xi,
967 struct ocfs2_xattr_search *xs,
968 size_t offs)
969{
970 handle_t *handle = NULL;
971 int ret = 0;
972 size_t name_len = strlen(xi->name);
973 void *val = xs->base + offs;
974 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
975
976 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
977 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
978 if (IS_ERR(handle)) {
979 ret = PTR_ERR(handle);
980 mlog_errno(ret);
981 goto out;
982 }
983 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
984 OCFS2_JOURNAL_ACCESS_WRITE);
985 if (ret) {
986 mlog_errno(ret);
987 goto out_commit;
988 }
989 /* Decrease xattr count */
990 le16_add_cpu(&xs->header->xh_count, -1);
991 /* Remove the xattr entry and tree root which has already be set*/
992 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
993 memset(val, 0, size);
994
995 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
996 if (ret < 0)
997 mlog_errno(ret);
998out_commit:
999 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1000out:
1001 return ret;
1002}
1003
1004static int ocfs2_xattr_update_entry(struct inode *inode,
1005 struct ocfs2_xattr_info *xi,
1006 struct ocfs2_xattr_search *xs,
1007 size_t offs)
1008{
1009 handle_t *handle = NULL;
1010 int ret = 0;
1011
1012 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1013 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1014 if (IS_ERR(handle)) {
1015 ret = PTR_ERR(handle);
1016 mlog_errno(ret);
1017 goto out;
1018 }
1019 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1020 OCFS2_JOURNAL_ACCESS_WRITE);
1021 if (ret) {
1022 mlog_errno(ret);
1023 goto out_commit;
1024 }
1025
1026 xs->here->xe_name_offset = cpu_to_le16(offs);
1027 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1028 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1029 ocfs2_xattr_set_local(xs->here, 1);
1030 else
1031 ocfs2_xattr_set_local(xs->here, 0);
1032 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1033
1034 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1035 if (ret < 0)
1036 mlog_errno(ret);
1037out_commit:
1038 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1039out:
1040 return ret;
1041}
1042
1043/*
1044 * ocfs2_xattr_set_value_outside()
1045 *
1046 * Set large size value in B tree.
1047 */
1048static int ocfs2_xattr_set_value_outside(struct inode *inode,
1049 struct ocfs2_xattr_info *xi,
1050 struct ocfs2_xattr_search *xs,
1051 size_t offs)
1052{
1053 size_t name_len = strlen(xi->name);
1054 void *val = xs->base + offs;
1055 struct ocfs2_xattr_value_root *xv = NULL;
1056 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1057 int ret = 0;
1058
1059 memset(val, 0, size);
1060 memcpy(val, xi->name, name_len);
1061 xv = (struct ocfs2_xattr_value_root *)
1062 (val + OCFS2_XATTR_SIZE(name_len));
1063 xv->xr_clusters = 0;
1064 xv->xr_last_eb_blk = 0;
1065 xv->xr_list.l_tree_depth = 0;
1066 xv->xr_list.l_count = cpu_to_le16(1);
1067 xv->xr_list.l_next_free_rec = 0;
1068
1069 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1070 xi->value_len);
1071 if (ret < 0) {
1072 mlog_errno(ret);
1073 return ret;
1074 }
1075 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1076 xi->value_len);
1077 if (ret < 0) {
1078 mlog_errno(ret);
1079 return ret;
1080 }
1081 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1082 if (ret < 0)
1083 mlog_errno(ret);
1084
1085 return ret;
1086}
1087
1088/*
1089 * ocfs2_xattr_set_entry_local()
1090 *
1091 * Set, replace or remove extended attribute in local.
1092 */
1093static void ocfs2_xattr_set_entry_local(struct inode *inode,
1094 struct ocfs2_xattr_info *xi,
1095 struct ocfs2_xattr_search *xs,
1096 struct ocfs2_xattr_entry *last,
1097 size_t min_offs)
1098{
1099 size_t name_len = strlen(xi->name);
1100 int i;
1101
1102 if (xi->value && xs->not_found) {
1103 /* Insert the new xattr entry. */
1104 le16_add_cpu(&xs->header->xh_count, 1);
1105 ocfs2_xattr_set_type(last, xi->name_index);
1106 ocfs2_xattr_set_local(last, 1);
1107 last->xe_name_len = name_len;
1108 } else {
1109 void *first_val;
1110 void *val;
1111 size_t offs, size;
1112
1113 first_val = xs->base + min_offs;
1114 offs = le16_to_cpu(xs->here->xe_name_offset);
1115 val = xs->base + offs;
1116
1117 if (le64_to_cpu(xs->here->xe_value_size) >
1118 OCFS2_XATTR_INLINE_SIZE)
1119 size = OCFS2_XATTR_SIZE(name_len) +
1120 OCFS2_XATTR_ROOT_SIZE;
1121 else
1122 size = OCFS2_XATTR_SIZE(name_len) +
1123 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1124
1125 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1126 OCFS2_XATTR_SIZE(xi->value_len)) {
1127 /* The old and the new value have the
1128 same size. Just replace the value. */
1129 ocfs2_xattr_set_local(xs->here, 1);
1130 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1131 /* Clear value bytes. */
1132 memset(val + OCFS2_XATTR_SIZE(name_len),
1133 0,
1134 OCFS2_XATTR_SIZE(xi->value_len));
1135 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1136 xi->value,
1137 xi->value_len);
1138 return;
1139 }
1140 /* Remove the old name+value. */
1141 memmove(first_val + size, first_val, val - first_val);
1142 memset(first_val, 0, size);
1143 xs->here->xe_name_hash = 0;
1144 xs->here->xe_name_offset = 0;
1145 ocfs2_xattr_set_local(xs->here, 1);
1146 xs->here->xe_value_size = 0;
1147
1148 min_offs += size;
1149
1150 /* Adjust all value offsets. */
1151 last = xs->header->xh_entries;
1152 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1153 size_t o = le16_to_cpu(last->xe_name_offset);
1154
1155 if (o < offs)
1156 last->xe_name_offset = cpu_to_le16(o + size);
1157 last += 1;
1158 }
1159
1160 if (!xi->value) {
1161 /* Remove the old entry. */
1162 last -= 1;
1163 memmove(xs->here, xs->here + 1,
1164 (void *)last - (void *)xs->here);
1165 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1166 le16_add_cpu(&xs->header->xh_count, -1);
1167 }
1168 }
1169 if (xi->value) {
1170 /* Insert the new name+value. */
1171 size_t size = OCFS2_XATTR_SIZE(name_len) +
1172 OCFS2_XATTR_SIZE(xi->value_len);
1173 void *val = xs->base + min_offs - size;
1174
1175 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1176 memset(val, 0, size);
1177 memcpy(val, xi->name, name_len);
1178 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1179 xi->value,
1180 xi->value_len);
1181 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1182 ocfs2_xattr_set_local(xs->here, 1);
1183 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1184 }
1185
1186 return;
1187}
1188
1189/*
1190 * ocfs2_xattr_set_entry()
1191 *
1192 * Set extended attribute entry into inode or block.
1193 *
1194 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1195 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1196 * then set value in B tree with set_value_outside().
1197 */
1198static int ocfs2_xattr_set_entry(struct inode *inode,
1199 struct ocfs2_xattr_info *xi,
1200 struct ocfs2_xattr_search *xs,
1201 int flag)
1202{
1203 struct ocfs2_xattr_entry *last;
1204 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1205 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1206 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1207 size_t size_l = 0;
1208 handle_t *handle = NULL;
1209 int free, i, ret;
1210 struct ocfs2_xattr_info xi_l = {
1211 .name_index = xi->name_index,
1212 .name = xi->name,
1213 .value = xi->value,
1214 .value_len = xi->value_len,
1215 };
1216
1217 /* Compute min_offs, last and free space. */
1218 last = xs->header->xh_entries;
1219
1220 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1221 size_t offs = le16_to_cpu(last->xe_name_offset);
1222 if (offs < min_offs)
1223 min_offs = offs;
1224 last += 1;
1225 }
1226
1227 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1228 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001229 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001230
1231 if (!xs->not_found) {
1232 size_t size = 0;
1233 if (ocfs2_xattr_is_local(xs->here))
1234 size = OCFS2_XATTR_SIZE(name_len) +
1235 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1236 else
1237 size = OCFS2_XATTR_SIZE(name_len) +
1238 OCFS2_XATTR_ROOT_SIZE;
1239 free += (size + sizeof(struct ocfs2_xattr_entry));
1240 }
1241 /* Check free space in inode or block */
1242 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1243 if (free < sizeof(struct ocfs2_xattr_entry) +
1244 OCFS2_XATTR_SIZE(name_len) +
1245 OCFS2_XATTR_ROOT_SIZE) {
1246 ret = -ENOSPC;
1247 goto out;
1248 }
1249 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1250 xi_l.value = (void *)&def_xv;
1251 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1252 } else if (xi->value) {
1253 if (free < sizeof(struct ocfs2_xattr_entry) +
1254 OCFS2_XATTR_SIZE(name_len) +
1255 OCFS2_XATTR_SIZE(xi->value_len)) {
1256 ret = -ENOSPC;
1257 goto out;
1258 }
1259 }
1260
1261 if (!xs->not_found) {
1262 /* For existing extended attribute */
1263 size_t size = OCFS2_XATTR_SIZE(name_len) +
1264 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1265 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1266 void *val = xs->base + offs;
1267
1268 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1269 /* Replace existing local xattr with tree root */
1270 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1271 offs);
1272 if (ret < 0)
1273 mlog_errno(ret);
1274 goto out;
1275 } else if (!ocfs2_xattr_is_local(xs->here)) {
1276 /* For existing xattr which has value outside */
1277 struct ocfs2_xattr_value_root *xv = NULL;
1278 xv = (struct ocfs2_xattr_value_root *)(val +
1279 OCFS2_XATTR_SIZE(name_len));
1280
1281 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1282 /*
1283 * If new value need set outside also,
1284 * first truncate old value to new value,
1285 * then set new value with set_value_outside().
1286 */
1287 ret = ocfs2_xattr_value_truncate(inode,
1288 xs->xattr_bh,
1289 xv,
1290 xi->value_len);
1291 if (ret < 0) {
1292 mlog_errno(ret);
1293 goto out;
1294 }
1295
1296 ret = __ocfs2_xattr_set_value_outside(inode,
1297 xv,
1298 xi->value,
1299 xi->value_len);
1300 if (ret < 0) {
1301 mlog_errno(ret);
1302 goto out;
1303 }
1304
1305 ret = ocfs2_xattr_update_entry(inode,
1306 xi,
1307 xs,
1308 offs);
1309 if (ret < 0)
1310 mlog_errno(ret);
1311 goto out;
1312 } else {
1313 /*
1314 * If new value need set in local,
1315 * just trucate old value to zero.
1316 */
1317 ret = ocfs2_xattr_value_truncate(inode,
1318 xs->xattr_bh,
1319 xv,
1320 0);
1321 if (ret < 0)
1322 mlog_errno(ret);
1323 }
1324 }
1325 }
1326
1327 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1328 OCFS2_INODE_UPDATE_CREDITS);
1329 if (IS_ERR(handle)) {
1330 ret = PTR_ERR(handle);
1331 mlog_errno(ret);
1332 goto out;
1333 }
1334
1335 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1336 OCFS2_JOURNAL_ACCESS_WRITE);
1337 if (ret) {
1338 mlog_errno(ret);
1339 goto out_commit;
1340 }
1341
1342 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tao Ma28b8ca02008-09-01 08:45:18 +08001343 /* set extended attribute in external block. */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001344 ret = ocfs2_extend_trans(handle,
Tao Ma28b8ca02008-09-01 08:45:18 +08001345 OCFS2_INODE_UPDATE_CREDITS +
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001346 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1347 if (ret) {
1348 mlog_errno(ret);
1349 goto out_commit;
1350 }
1351 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1352 OCFS2_JOURNAL_ACCESS_WRITE);
1353 if (ret) {
1354 mlog_errno(ret);
1355 goto out_commit;
1356 }
1357 }
1358
1359 /*
1360 * Set value in local, include set tree root in local.
1361 * This is the first step for value size >INLINE_SIZE.
1362 */
1363 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1364
1365 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1366 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1367 if (ret < 0) {
1368 mlog_errno(ret);
1369 goto out_commit;
1370 }
1371 }
1372
1373 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1374 (flag & OCFS2_INLINE_XATTR_FL)) {
1375 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1376 unsigned int xattrsize = osb->s_xattr_inline_size;
1377
1378 /*
1379 * Adjust extent record count or inline data size
1380 * to reserve space for extended attribute.
1381 */
1382 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1383 struct ocfs2_inline_data *idata = &di->id2.i_data;
1384 le16_add_cpu(&idata->id_count, -xattrsize);
1385 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1386 struct ocfs2_extent_list *el = &di->id2.i_list;
1387 le16_add_cpu(&el->l_count, -(xattrsize /
1388 sizeof(struct ocfs2_extent_rec)));
1389 }
1390 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1391 }
1392 /* Update xattr flag */
1393 spin_lock(&oi->ip_lock);
1394 oi->ip_dyn_features |= flag;
1395 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1396 spin_unlock(&oi->ip_lock);
1397 /* Update inode ctime */
1398 inode->i_ctime = CURRENT_TIME;
1399 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1400 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1401
1402 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1403 if (ret < 0)
1404 mlog_errno(ret);
1405
1406out_commit:
1407 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1408
1409 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1410 /*
1411 * Set value outside in B tree.
1412 * This is the second step for value size > INLINE_SIZE.
1413 */
1414 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1415 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1416 if (ret < 0) {
1417 int ret2;
1418
1419 mlog_errno(ret);
1420 /*
1421 * If set value outside failed, we have to clean
1422 * the junk tree root we have already set in local.
1423 */
1424 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1425 if (ret2 < 0)
1426 mlog_errno(ret2);
1427 }
1428 }
1429out:
1430 return ret;
1431
1432}
1433
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001434static int ocfs2_remove_value_outside(struct inode*inode,
1435 struct buffer_head *bh,
1436 struct ocfs2_xattr_header *header)
1437{
1438 int ret = 0, i;
1439
1440 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1441 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1442
1443 if (!ocfs2_xattr_is_local(entry)) {
1444 struct ocfs2_xattr_value_root *xv;
1445 void *val;
1446
1447 val = (void *)header +
1448 le16_to_cpu(entry->xe_name_offset);
1449 xv = (struct ocfs2_xattr_value_root *)
1450 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1451 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1452 if (ret < 0) {
1453 mlog_errno(ret);
1454 return ret;
1455 }
1456 }
1457 }
1458
1459 return ret;
1460}
1461
1462static int ocfs2_xattr_ibody_remove(struct inode *inode,
1463 struct buffer_head *di_bh)
1464{
1465
1466 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1467 struct ocfs2_xattr_header *header;
1468 int ret;
1469
1470 header = (struct ocfs2_xattr_header *)
1471 ((void *)di + inode->i_sb->s_blocksize -
1472 le16_to_cpu(di->i_xattr_inline_size));
1473
1474 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1475
1476 return ret;
1477}
1478
1479static int ocfs2_xattr_block_remove(struct inode *inode,
1480 struct buffer_head *blk_bh)
1481{
1482 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001483 int ret = 0;
1484
1485 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001486 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1487 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1488 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1489 } else
1490 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001491
1492 return ret;
1493}
1494
Tao Ma08413892008-08-29 09:00:19 +08001495static int ocfs2_xattr_free_block(struct inode *inode,
1496 u64 block)
1497{
1498 struct inode *xb_alloc_inode;
1499 struct buffer_head *xb_alloc_bh = NULL;
1500 struct buffer_head *blk_bh = NULL;
1501 struct ocfs2_xattr_block *xb;
1502 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1503 handle_t *handle;
1504 int ret = 0;
1505 u64 blk, bg_blkno;
1506 u16 bit;
1507
Joel Becker0fcaa562008-10-09 17:20:31 -07001508 ret = ocfs2_read_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001509 if (ret < 0) {
1510 mlog_errno(ret);
1511 goto out;
1512 }
1513
Joel Beckerf6087fb2008-10-20 18:20:43 -07001514 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1515 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1516 ret = -EIO;
Tao Ma08413892008-08-29 09:00:19 +08001517 goto out;
1518 }
1519
1520 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1521 if (ret < 0) {
1522 mlog_errno(ret);
1523 goto out;
1524 }
1525
Tao Ma08413892008-08-29 09:00:19 +08001526 blk = le64_to_cpu(xb->xb_blkno);
1527 bit = le16_to_cpu(xb->xb_suballoc_bit);
1528 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1529
1530 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1531 EXTENT_ALLOC_SYSTEM_INODE,
1532 le16_to_cpu(xb->xb_suballoc_slot));
1533 if (!xb_alloc_inode) {
1534 ret = -ENOMEM;
1535 mlog_errno(ret);
1536 goto out;
1537 }
1538 mutex_lock(&xb_alloc_inode->i_mutex);
1539
1540 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1541 if (ret < 0) {
1542 mlog_errno(ret);
1543 goto out_mutex;
1544 }
1545
1546 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1547 if (IS_ERR(handle)) {
1548 ret = PTR_ERR(handle);
1549 mlog_errno(ret);
1550 goto out_unlock;
1551 }
1552
1553 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1554 bit, bg_blkno, 1);
1555 if (ret < 0)
1556 mlog_errno(ret);
1557
1558 ocfs2_commit_trans(osb, handle);
1559out_unlock:
1560 ocfs2_inode_unlock(xb_alloc_inode, 1);
1561 brelse(xb_alloc_bh);
1562out_mutex:
1563 mutex_unlock(&xb_alloc_inode->i_mutex);
1564 iput(xb_alloc_inode);
1565out:
1566 brelse(blk_bh);
1567 return ret;
1568}
1569
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001570/*
1571 * ocfs2_xattr_remove()
1572 *
1573 * Free extended attribute resources associated with this inode.
1574 */
1575int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1576{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001577 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1578 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1579 handle_t *handle;
1580 int ret;
1581
Tiger Yang8154da32008-08-18 17:11:46 +08001582 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1583 return 0;
1584
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001585 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1586 return 0;
1587
1588 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1589 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1590 if (ret < 0) {
1591 mlog_errno(ret);
1592 goto out;
1593 }
1594 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001595
Tao Ma08413892008-08-29 09:00:19 +08001596 if (di->i_xattr_loc) {
1597 ret = ocfs2_xattr_free_block(inode,
1598 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001599 if (ret < 0) {
1600 mlog_errno(ret);
1601 goto out;
1602 }
1603 }
1604
1605 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1606 OCFS2_INODE_UPDATE_CREDITS);
1607 if (IS_ERR(handle)) {
1608 ret = PTR_ERR(handle);
1609 mlog_errno(ret);
1610 goto out;
1611 }
1612 ret = ocfs2_journal_access(handle, inode, di_bh,
1613 OCFS2_JOURNAL_ACCESS_WRITE);
1614 if (ret) {
1615 mlog_errno(ret);
1616 goto out_commit;
1617 }
1618
Tao Ma08413892008-08-29 09:00:19 +08001619 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001620
1621 spin_lock(&oi->ip_lock);
1622 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1623 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1624 spin_unlock(&oi->ip_lock);
1625
1626 ret = ocfs2_journal_dirty(handle, di_bh);
1627 if (ret < 0)
1628 mlog_errno(ret);
1629out_commit:
1630 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1631out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001632 return ret;
1633}
1634
1635static int ocfs2_xattr_has_space_inline(struct inode *inode,
1636 struct ocfs2_dinode *di)
1637{
1638 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1639 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1640 int free;
1641
1642 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1643 return 0;
1644
1645 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1646 struct ocfs2_inline_data *idata = &di->id2.i_data;
1647 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1648 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1649 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1650 le64_to_cpu(di->i_size);
1651 } else {
1652 struct ocfs2_extent_list *el = &di->id2.i_list;
1653 free = (le16_to_cpu(el->l_count) -
1654 le16_to_cpu(el->l_next_free_rec)) *
1655 sizeof(struct ocfs2_extent_rec);
1656 }
1657 if (free >= xattrsize)
1658 return 1;
1659
1660 return 0;
1661}
1662
1663/*
1664 * ocfs2_xattr_ibody_find()
1665 *
1666 * Find extended attribute in inode block and
1667 * fill search info into struct ocfs2_xattr_search.
1668 */
1669static int ocfs2_xattr_ibody_find(struct inode *inode,
1670 int name_index,
1671 const char *name,
1672 struct ocfs2_xattr_search *xs)
1673{
1674 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1675 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1676 int ret;
1677 int has_space = 0;
1678
1679 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1680 return 0;
1681
1682 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1683 down_read(&oi->ip_alloc_sem);
1684 has_space = ocfs2_xattr_has_space_inline(inode, di);
1685 up_read(&oi->ip_alloc_sem);
1686 if (!has_space)
1687 return 0;
1688 }
1689
1690 xs->xattr_bh = xs->inode_bh;
1691 xs->end = (void *)di + inode->i_sb->s_blocksize;
1692 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1693 xs->header = (struct ocfs2_xattr_header *)
1694 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1695 else
1696 xs->header = (struct ocfs2_xattr_header *)
1697 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1698 xs->base = (void *)xs->header;
1699 xs->here = xs->header->xh_entries;
1700
1701 /* Find the named attribute. */
1702 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1703 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1704 if (ret && ret != -ENODATA)
1705 return ret;
1706 xs->not_found = ret;
1707 }
1708
1709 return 0;
1710}
1711
1712/*
1713 * ocfs2_xattr_ibody_set()
1714 *
1715 * Set, replace or remove an extended attribute into inode block.
1716 *
1717 */
1718static int ocfs2_xattr_ibody_set(struct inode *inode,
1719 struct ocfs2_xattr_info *xi,
1720 struct ocfs2_xattr_search *xs)
1721{
1722 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1723 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1724 int ret;
1725
1726 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1727 return -ENOSPC;
1728
1729 down_write(&oi->ip_alloc_sem);
1730 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1731 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1732 ret = -ENOSPC;
1733 goto out;
1734 }
1735 }
1736
1737 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1738 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1739out:
1740 up_write(&oi->ip_alloc_sem);
1741
1742 return ret;
1743}
1744
1745/*
1746 * ocfs2_xattr_block_find()
1747 *
1748 * Find extended attribute in external block and
1749 * fill search info into struct ocfs2_xattr_search.
1750 */
1751static int ocfs2_xattr_block_find(struct inode *inode,
1752 int name_index,
1753 const char *name,
1754 struct ocfs2_xattr_search *xs)
1755{
1756 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1757 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08001758 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001759 int ret = 0;
1760
1761 if (!di->i_xattr_loc)
1762 return ret;
1763
Joel Becker0fcaa562008-10-09 17:20:31 -07001764 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001765 if (ret < 0) {
1766 mlog_errno(ret);
1767 return ret;
1768 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07001769
1770 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1771 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1772 ret = -EIO;
1773 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001774 }
1775
1776 xs->xattr_bh = blk_bh;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001777
Tao Ma589dc262008-08-18 17:38:51 +08001778 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1779 xs->header = &xb->xb_attrs.xb_header;
1780 xs->base = (void *)xs->header;
1781 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1782 xs->here = xs->header->xh_entries;
1783
1784 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1785 } else
1786 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1787 name_index,
1788 name, xs);
1789
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001790 if (ret && ret != -ENODATA) {
1791 xs->xattr_bh = NULL;
1792 goto cleanup;
1793 }
1794 xs->not_found = ret;
1795 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001796cleanup:
1797 brelse(blk_bh);
1798
1799 return ret;
1800}
1801
1802/*
1803 * ocfs2_xattr_block_set()
1804 *
1805 * Set, replace or remove an extended attribute into external block.
1806 *
1807 */
1808static int ocfs2_xattr_block_set(struct inode *inode,
1809 struct ocfs2_xattr_info *xi,
1810 struct ocfs2_xattr_search *xs)
1811{
1812 struct buffer_head *new_bh = NULL;
1813 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1814 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1815 struct ocfs2_alloc_context *meta_ac = NULL;
1816 handle_t *handle = NULL;
1817 struct ocfs2_xattr_block *xblk = NULL;
1818 u16 suballoc_bit_start;
1819 u32 num_got;
1820 u64 first_blkno;
1821 int ret;
1822
1823 if (!xs->xattr_bh) {
1824 /*
1825 * Alloc one external block for extended attribute
1826 * outside of inode.
1827 */
1828 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1829 if (ret < 0) {
1830 mlog_errno(ret);
1831 goto out;
1832 }
1833 handle = ocfs2_start_trans(osb,
1834 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1835 if (IS_ERR(handle)) {
1836 ret = PTR_ERR(handle);
1837 mlog_errno(ret);
1838 goto out;
1839 }
1840 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1841 OCFS2_JOURNAL_ACCESS_CREATE);
1842 if (ret < 0) {
1843 mlog_errno(ret);
1844 goto out_commit;
1845 }
1846
1847 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1848 &suballoc_bit_start, &num_got,
1849 &first_blkno);
1850 if (ret < 0) {
1851 mlog_errno(ret);
1852 goto out_commit;
1853 }
1854
1855 new_bh = sb_getblk(inode->i_sb, first_blkno);
1856 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1857
1858 ret = ocfs2_journal_access(handle, inode, new_bh,
1859 OCFS2_JOURNAL_ACCESS_CREATE);
1860 if (ret < 0) {
1861 mlog_errno(ret);
1862 goto out_commit;
1863 }
1864
1865 /* Initialize ocfs2_xattr_block */
1866 xs->xattr_bh = new_bh;
1867 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1868 memset(xblk, 0, inode->i_sb->s_blocksize);
1869 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1870 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
1871 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1872 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
1873 xblk->xb_blkno = cpu_to_le64(first_blkno);
1874
1875 xs->header = &xblk->xb_attrs.xb_header;
1876 xs->base = (void *)xs->header;
1877 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
1878 xs->here = xs->header->xh_entries;
1879
1880
1881 ret = ocfs2_journal_dirty(handle, new_bh);
1882 if (ret < 0) {
1883 mlog_errno(ret);
1884 goto out_commit;
1885 }
1886 di->i_xattr_loc = cpu_to_le64(first_blkno);
1887 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1888 if (ret < 0)
1889 mlog_errno(ret);
1890out_commit:
1891 ocfs2_commit_trans(osb, handle);
1892out:
1893 if (meta_ac)
1894 ocfs2_free_alloc_context(meta_ac);
1895 if (ret < 0)
1896 return ret;
Tao Ma01225592008-08-18 17:38:53 +08001897 } else
1898 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
1899
1900 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
1901 /* Set extended attribute into external block */
1902 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
1903 if (!ret || ret != -ENOSPC)
1904 goto end;
1905
1906 ret = ocfs2_xattr_create_index_block(inode, xs);
1907 if (ret)
1908 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001909 }
1910
Tao Ma01225592008-08-18 17:38:53 +08001911 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
Tao Ma01225592008-08-18 17:38:53 +08001912
1913end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001914
1915 return ret;
1916}
1917
1918/*
1919 * ocfs2_xattr_set()
1920 *
1921 * Set, replace or remove an extended attribute for this inode.
1922 * value is NULL to remove an existing extended attribute, else either
1923 * create or replace an extended attribute.
1924 */
1925int ocfs2_xattr_set(struct inode *inode,
1926 int name_index,
1927 const char *name,
1928 const void *value,
1929 size_t value_len,
1930 int flags)
1931{
1932 struct buffer_head *di_bh = NULL;
1933 struct ocfs2_dinode *di;
1934 int ret;
Tao Ma01225592008-08-18 17:38:53 +08001935 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001936
1937 struct ocfs2_xattr_info xi = {
1938 .name_index = name_index,
1939 .name = name,
1940 .value = value,
1941 .value_len = value_len,
1942 };
1943
1944 struct ocfs2_xattr_search xis = {
1945 .not_found = -ENODATA,
1946 };
1947
1948 struct ocfs2_xattr_search xbs = {
1949 .not_found = -ENODATA,
1950 };
1951
Tiger Yang8154da32008-08-18 17:11:46 +08001952 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1953 return -EOPNOTSUPP;
1954
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001955 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1956 if (ret < 0) {
1957 mlog_errno(ret);
1958 return ret;
1959 }
1960 xis.inode_bh = xbs.inode_bh = di_bh;
1961 di = (struct ocfs2_dinode *)di_bh->b_data;
1962
1963 down_write(&OCFS2_I(inode)->ip_xattr_sem);
1964 /*
1965 * Scan inode and external block to find the same name
1966 * extended attribute and collect search infomation.
1967 */
1968 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
1969 if (ret)
1970 goto cleanup;
1971 if (xis.not_found) {
1972 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
1973 if (ret)
1974 goto cleanup;
1975 }
1976
1977 if (xis.not_found && xbs.not_found) {
1978 ret = -ENODATA;
1979 if (flags & XATTR_REPLACE)
1980 goto cleanup;
1981 ret = 0;
1982 if (!value)
1983 goto cleanup;
1984 } else {
1985 ret = -EEXIST;
1986 if (flags & XATTR_CREATE)
1987 goto cleanup;
1988 }
1989
1990 if (!value) {
1991 /* Remove existing extended attribute */
1992 if (!xis.not_found)
1993 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1994 else if (!xbs.not_found)
1995 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
1996 } else {
1997 /* We always try to set extended attribute into inode first*/
1998 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
1999 if (!ret && !xbs.not_found) {
2000 /*
2001 * If succeed and that extended attribute existing in
2002 * external block, then we will remove it.
2003 */
2004 xi.value = NULL;
2005 xi.value_len = 0;
2006 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2007 } else if (ret == -ENOSPC) {
2008 if (di->i_xattr_loc && !xbs.xattr_bh) {
2009 ret = ocfs2_xattr_block_find(inode, name_index,
2010 name, &xbs);
2011 if (ret)
2012 goto cleanup;
2013 }
2014 /*
2015 * If no space in inode, we will set extended attribute
2016 * into external block.
2017 */
2018 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2019 if (ret)
2020 goto cleanup;
2021 if (!xis.not_found) {
2022 /*
2023 * If succeed and that extended attribute
2024 * existing in inode, we will remove it.
2025 */
2026 xi.value = NULL;
2027 xi.value_len = 0;
2028 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2029 }
2030 }
2031 }
2032cleanup:
2033 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2034 ocfs2_inode_unlock(inode, 1);
2035 brelse(di_bh);
2036 brelse(xbs.xattr_bh);
Tao Ma01225592008-08-18 17:38:53 +08002037 for (i = 0; i < blk_per_bucket; i++)
Joel Becker4ac60322008-10-18 19:11:42 -07002038 brelse(xbs.bucket.bu_bhs[i]);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002039
2040 return ret;
2041}
2042
Tao Ma0c044f02008-08-18 17:38:50 +08002043/*
2044 * Find the xattr extent rec which may contains name_hash.
2045 * e_cpos will be the first name hash of the xattr rec.
2046 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2047 */
2048static int ocfs2_xattr_get_rec(struct inode *inode,
2049 u32 name_hash,
2050 u64 *p_blkno,
2051 u32 *e_cpos,
2052 u32 *num_clusters,
2053 struct ocfs2_extent_list *el)
2054{
2055 int ret = 0, i;
2056 struct buffer_head *eb_bh = NULL;
2057 struct ocfs2_extent_block *eb;
2058 struct ocfs2_extent_rec *rec = NULL;
2059 u64 e_blkno = 0;
2060
2061 if (el->l_tree_depth) {
2062 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2063 if (ret) {
2064 mlog_errno(ret);
2065 goto out;
2066 }
2067
2068 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2069 el = &eb->h_list;
2070
2071 if (el->l_tree_depth) {
2072 ocfs2_error(inode->i_sb,
2073 "Inode %lu has non zero tree depth in "
2074 "xattr tree block %llu\n", inode->i_ino,
2075 (unsigned long long)eb_bh->b_blocknr);
2076 ret = -EROFS;
2077 goto out;
2078 }
2079 }
2080
2081 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2082 rec = &el->l_recs[i];
2083
2084 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2085 e_blkno = le64_to_cpu(rec->e_blkno);
2086 break;
2087 }
2088 }
2089
2090 if (!e_blkno) {
2091 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2092 "record (%u, %u, 0) in xattr", inode->i_ino,
2093 le32_to_cpu(rec->e_cpos),
2094 ocfs2_rec_clusters(el, rec));
2095 ret = -EROFS;
2096 goto out;
2097 }
2098
2099 *p_blkno = le64_to_cpu(rec->e_blkno);
2100 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2101 if (e_cpos)
2102 *e_cpos = le32_to_cpu(rec->e_cpos);
2103out:
2104 brelse(eb_bh);
2105 return ret;
2106}
2107
2108typedef int (xattr_bucket_func)(struct inode *inode,
2109 struct ocfs2_xattr_bucket *bucket,
2110 void *para);
2111
Tao Ma589dc262008-08-18 17:38:51 +08002112static int ocfs2_find_xe_in_bucket(struct inode *inode,
2113 struct buffer_head *header_bh,
2114 int name_index,
2115 const char *name,
2116 u32 name_hash,
2117 u16 *xe_index,
2118 int *found)
2119{
2120 int i, ret = 0, cmp = 1, block_off, new_offset;
2121 struct ocfs2_xattr_header *xh =
2122 (struct ocfs2_xattr_header *)header_bh->b_data;
2123 size_t name_len = strlen(name);
2124 struct ocfs2_xattr_entry *xe = NULL;
2125 struct buffer_head *name_bh = NULL;
2126 char *xe_name;
2127
2128 /*
2129 * We don't use binary search in the bucket because there
2130 * may be multiple entries with the same name hash.
2131 */
2132 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2133 xe = &xh->xh_entries[i];
2134
2135 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2136 continue;
2137 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2138 break;
2139
2140 cmp = name_index - ocfs2_xattr_get_type(xe);
2141 if (!cmp)
2142 cmp = name_len - xe->xe_name_len;
2143 if (cmp)
2144 continue;
2145
2146 ret = ocfs2_xattr_bucket_get_name_value(inode,
2147 xh,
2148 i,
2149 &block_off,
2150 &new_offset);
2151 if (ret) {
2152 mlog_errno(ret);
2153 break;
2154 }
2155
Joel Becker0fcaa562008-10-09 17:20:31 -07002156 ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off,
2157 &name_bh);
Tao Ma589dc262008-08-18 17:38:51 +08002158 if (ret) {
2159 mlog_errno(ret);
2160 break;
2161 }
2162 xe_name = name_bh->b_data + new_offset;
2163
2164 cmp = memcmp(name, xe_name, name_len);
2165 brelse(name_bh);
2166 name_bh = NULL;
2167
2168 if (cmp == 0) {
2169 *xe_index = i;
2170 *found = 1;
2171 ret = 0;
2172 break;
2173 }
2174 }
2175
2176 return ret;
2177}
2178
2179/*
2180 * Find the specified xattr entry in a series of buckets.
2181 * This series start from p_blkno and last for num_clusters.
2182 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2183 * the num of the valid buckets.
2184 *
2185 * Return the buffer_head this xattr should reside in. And if the xattr's
2186 * hash is in the gap of 2 buckets, return the lower bucket.
2187 */
2188static int ocfs2_xattr_bucket_find(struct inode *inode,
2189 int name_index,
2190 const char *name,
2191 u32 name_hash,
2192 u64 p_blkno,
2193 u32 first_hash,
2194 u32 num_clusters,
2195 struct ocfs2_xattr_search *xs)
2196{
2197 int ret, found = 0;
2198 struct buffer_head *bh = NULL;
2199 struct buffer_head *lower_bh = NULL;
2200 struct ocfs2_xattr_header *xh = NULL;
2201 struct ocfs2_xattr_entry *xe = NULL;
2202 u16 index = 0;
2203 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2204 int low_bucket = 0, bucket, high_bucket;
2205 u32 last_hash;
2206 u64 blkno;
2207
Joel Becker0fcaa562008-10-09 17:20:31 -07002208 ret = ocfs2_read_block(inode, p_blkno, &bh);
Tao Ma589dc262008-08-18 17:38:51 +08002209 if (ret) {
2210 mlog_errno(ret);
2211 goto out;
2212 }
2213
2214 xh = (struct ocfs2_xattr_header *)bh->b_data;
2215 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
2216
2217 while (low_bucket <= high_bucket) {
2218 brelse(bh);
2219 bh = NULL;
2220 bucket = (low_bucket + high_bucket) / 2;
2221
2222 blkno = p_blkno + bucket * blk_per_bucket;
2223
Joel Becker0fcaa562008-10-09 17:20:31 -07002224 ret = ocfs2_read_block(inode, blkno, &bh);
Tao Ma589dc262008-08-18 17:38:51 +08002225 if (ret) {
2226 mlog_errno(ret);
2227 goto out;
2228 }
2229
2230 xh = (struct ocfs2_xattr_header *)bh->b_data;
2231 xe = &xh->xh_entries[0];
2232 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2233 high_bucket = bucket - 1;
2234 continue;
2235 }
2236
2237 /*
2238 * Check whether the hash of the last entry in our
Tao Ma5a095612008-09-19 22:17:41 +08002239 * bucket is larger than the search one. for an empty
2240 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002241 */
Tao Ma5a095612008-09-19 22:17:41 +08002242 if (xh->xh_count)
2243 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2244
Tao Ma589dc262008-08-18 17:38:51 +08002245 last_hash = le32_to_cpu(xe->xe_name_hash);
2246
2247 /* record lower_bh which may be the insert place. */
2248 brelse(lower_bh);
2249 lower_bh = bh;
2250 bh = NULL;
2251
2252 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2253 low_bucket = bucket + 1;
2254 continue;
2255 }
2256
2257 /* the searched xattr should reside in this bucket if exists. */
2258 ret = ocfs2_find_xe_in_bucket(inode, lower_bh,
2259 name_index, name, name_hash,
2260 &index, &found);
2261 if (ret) {
2262 mlog_errno(ret);
2263 goto out;
2264 }
2265 break;
2266 }
2267
2268 /*
2269 * Record the bucket we have found.
2270 * When the xattr's hash value is in the gap of 2 buckets, we will
2271 * always set it to the previous bucket.
2272 */
2273 if (!lower_bh) {
2274 /*
2275 * We can't find any bucket whose first name_hash is less
2276 * than the find name_hash.
2277 */
2278 BUG_ON(bh->b_blocknr != p_blkno);
2279 lower_bh = bh;
2280 bh = NULL;
2281 }
Joel Becker4ac60322008-10-18 19:11:42 -07002282 xs->bucket.bu_bhs[0] = lower_bh;
2283 xs->bucket.bu_xh = (struct ocfs2_xattr_header *)
Joel Becker51def392008-10-24 16:57:21 -07002284 bucket_block(&xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002285 lower_bh = NULL;
2286
Joel Becker4ac60322008-10-18 19:11:42 -07002287 xs->header = xs->bucket.bu_xh;
Joel Becker51def392008-10-24 16:57:21 -07002288 xs->base = bucket_block(&xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002289 xs->end = xs->base + inode->i_sb->s_blocksize;
2290
2291 if (found) {
2292 /*
2293 * If we have found the xattr enty, read all the blocks in
2294 * this bucket.
2295 */
Joel Becker9c7759a2008-10-24 16:21:03 -07002296 ret = ocfs2_read_blocks(inode, bucket_blkno(&xs->bucket) + 1,
Joel Becker4ac60322008-10-18 19:11:42 -07002297 blk_per_bucket - 1, &xs->bucket.bu_bhs[1],
Mark Fasheh1efd47f2008-10-14 18:31:46 -07002298 0);
Tao Ma589dc262008-08-18 17:38:51 +08002299 if (ret) {
2300 mlog_errno(ret);
2301 goto out;
2302 }
2303
2304 xs->here = &xs->header->xh_entries[index];
2305 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Becker9c7759a2008-10-24 16:21:03 -07002306 (unsigned long long)bucket_blkno(&xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08002307 } else
2308 ret = -ENODATA;
2309
2310out:
2311 brelse(bh);
2312 brelse(lower_bh);
2313 return ret;
2314}
2315
2316static int ocfs2_xattr_index_block_find(struct inode *inode,
2317 struct buffer_head *root_bh,
2318 int name_index,
2319 const char *name,
2320 struct ocfs2_xattr_search *xs)
2321{
2322 int ret;
2323 struct ocfs2_xattr_block *xb =
2324 (struct ocfs2_xattr_block *)root_bh->b_data;
2325 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2326 struct ocfs2_extent_list *el = &xb_root->xt_list;
2327 u64 p_blkno = 0;
2328 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08002329 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08002330
2331 if (le16_to_cpu(el->l_next_free_rec) == 0)
2332 return -ENODATA;
2333
2334 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2335 name, name_hash, name_index);
2336
2337 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2338 &num_clusters, el);
2339 if (ret) {
2340 mlog_errno(ret);
2341 goto out;
2342 }
2343
2344 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2345
2346 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07002347 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2348 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08002349
2350 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2351 p_blkno, first_hash, num_clusters, xs);
2352
2353out:
2354 return ret;
2355}
2356
Tao Ma0c044f02008-08-18 17:38:50 +08002357static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2358 u64 blkno,
2359 u32 clusters,
2360 xattr_bucket_func *func,
2361 void *para)
2362{
2363 int i, j, ret = 0;
2364 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2365 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2366 u32 num_buckets = clusters * bpc;
2367 struct ocfs2_xattr_bucket bucket;
2368
2369 memset(&bucket, 0, sizeof(bucket));
2370
2371 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07002372 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002373
2374 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
Joel Becker31d33072008-10-09 17:20:30 -07002375 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket,
Joel Becker4ac60322008-10-18 19:11:42 -07002376 bucket.bu_bhs, 0);
Tao Ma0c044f02008-08-18 17:38:50 +08002377 if (ret) {
2378 mlog_errno(ret);
2379 goto out;
2380 }
2381
Joel Becker51def392008-10-24 16:57:21 -07002382 bucket.bu_xh = (struct ocfs2_xattr_header *)bucket_block(&bucket, 0);
Tao Ma0c044f02008-08-18 17:38:50 +08002383 /*
2384 * The real bucket num in this series of blocks is stored
2385 * in the 1st bucket.
2386 */
2387 if (i == 0)
Joel Becker4ac60322008-10-18 19:11:42 -07002388 num_buckets = le16_to_cpu(bucket.bu_xh->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08002389
Mark Fashehde29c082008-10-29 14:45:30 -07002390 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2391 (unsigned long long)blkno,
Joel Becker4ac60322008-10-18 19:11:42 -07002392 le32_to_cpu(bucket.bu_xh->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08002393 if (func) {
2394 ret = func(inode, &bucket, para);
2395 if (ret) {
2396 mlog_errno(ret);
2397 break;
2398 }
2399 }
2400
2401 for (j = 0; j < blk_per_bucket; j++)
Joel Becker4ac60322008-10-18 19:11:42 -07002402 brelse(bucket.bu_bhs[j]);
Tao Ma0c044f02008-08-18 17:38:50 +08002403 memset(&bucket, 0, sizeof(bucket));
2404 }
2405
2406out:
2407 for (j = 0; j < blk_per_bucket; j++)
Joel Becker4ac60322008-10-18 19:11:42 -07002408 brelse(bucket.bu_bhs[j]);
Tao Ma0c044f02008-08-18 17:38:50 +08002409
2410 return ret;
2411}
2412
2413struct ocfs2_xattr_tree_list {
2414 char *buffer;
2415 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08002416 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08002417};
2418
2419static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2420 struct ocfs2_xattr_header *xh,
2421 int index,
2422 int *block_off,
2423 int *new_offset)
2424{
2425 u16 name_offset;
2426
2427 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2428 return -EINVAL;
2429
2430 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2431
2432 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2433 *new_offset = name_offset % inode->i_sb->s_blocksize;
2434
2435 return 0;
2436}
2437
2438static int ocfs2_list_xattr_bucket(struct inode *inode,
2439 struct ocfs2_xattr_bucket *bucket,
2440 void *para)
2441{
Tao Ma936b8832008-10-09 23:06:14 +08002442 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08002443 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08002444 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08002445 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08002446
Joel Becker4ac60322008-10-18 19:11:42 -07002447 for (i = 0 ; i < le16_to_cpu(bucket->bu_xh->xh_count); i++) {
2448 struct ocfs2_xattr_entry *entry = &bucket->bu_xh->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08002449 type = ocfs2_xattr_get_type(entry);
2450 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08002451
Tao Ma936b8832008-10-09 23:06:14 +08002452 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08002453 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker4ac60322008-10-18 19:11:42 -07002454 bucket->bu_xh,
Tao Ma0c044f02008-08-18 17:38:50 +08002455 i,
2456 &block_off,
2457 &new_offset);
2458 if (ret)
2459 break;
Tao Ma936b8832008-10-09 23:06:14 +08002460
Joel Becker51def392008-10-24 16:57:21 -07002461 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08002462 new_offset;
2463 ret = ocfs2_xattr_list_entry(xl->buffer,
2464 xl->buffer_size,
2465 &xl->result,
2466 prefix, name,
2467 entry->xe_name_len);
2468 if (ret)
2469 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002470 }
2471 }
2472
2473 return ret;
2474}
2475
2476static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2477 struct ocfs2_xattr_tree_root *xt,
2478 char *buffer,
2479 size_t buffer_size)
2480{
2481 struct ocfs2_extent_list *el = &xt->xt_list;
2482 int ret = 0;
2483 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2484 u64 p_blkno = 0;
2485 struct ocfs2_xattr_tree_list xl = {
2486 .buffer = buffer,
2487 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08002488 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08002489 };
2490
2491 if (le16_to_cpu(el->l_next_free_rec) == 0)
2492 return 0;
2493
2494 while (name_hash > 0) {
2495 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2496 &e_cpos, &num_clusters, el);
2497 if (ret) {
2498 mlog_errno(ret);
2499 goto out;
2500 }
2501
2502 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2503 ocfs2_list_xattr_bucket,
2504 &xl);
2505 if (ret) {
2506 mlog_errno(ret);
2507 goto out;
2508 }
2509
2510 if (e_cpos == 0)
2511 break;
2512
2513 name_hash = e_cpos - 1;
2514 }
2515
Tao Ma936b8832008-10-09 23:06:14 +08002516 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08002517out:
2518 return ret;
2519}
Tao Ma01225592008-08-18 17:38:53 +08002520
2521static int cmp_xe(const void *a, const void *b)
2522{
2523 const struct ocfs2_xattr_entry *l = a, *r = b;
2524 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2525 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2526
2527 if (l_hash > r_hash)
2528 return 1;
2529 if (l_hash < r_hash)
2530 return -1;
2531 return 0;
2532}
2533
2534static void swap_xe(void *a, void *b, int size)
2535{
2536 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2537
2538 tmp = *l;
2539 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2540 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2541}
2542
2543/*
2544 * When the ocfs2_xattr_block is filled up, new bucket will be created
2545 * and all the xattr entries will be moved to the new bucket.
2546 * Note: we need to sort the entries since they are not saved in order
2547 * in the ocfs2_xattr_block.
2548 */
2549static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2550 struct buffer_head *xb_bh,
2551 struct buffer_head *xh_bh,
2552 struct buffer_head *data_bh)
2553{
2554 int i, blocksize = inode->i_sb->s_blocksize;
2555 u16 offset, size, off_change;
2556 struct ocfs2_xattr_entry *xe;
2557 struct ocfs2_xattr_block *xb =
2558 (struct ocfs2_xattr_block *)xb_bh->b_data;
2559 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
2560 struct ocfs2_xattr_header *xh =
2561 (struct ocfs2_xattr_header *)xh_bh->b_data;
2562 u16 count = le16_to_cpu(xb_xh->xh_count);
2563 char *target = xh_bh->b_data, *src = xb_bh->b_data;
2564
2565 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2566 (unsigned long long)xb_bh->b_blocknr,
2567 (unsigned long long)xh_bh->b_blocknr);
2568
2569 memset(xh_bh->b_data, 0, blocksize);
2570 if (data_bh)
2571 memset(data_bh->b_data, 0, blocksize);
2572 /*
2573 * Since the xe_name_offset is based on ocfs2_xattr_header,
2574 * there is a offset change corresponding to the change of
2575 * ocfs2_xattr_header's position.
2576 */
2577 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2578 xe = &xb_xh->xh_entries[count - 1];
2579 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2580 size = blocksize - offset;
2581
2582 /* copy all the names and values. */
2583 if (data_bh)
2584 target = data_bh->b_data;
2585 memcpy(target + offset, src + offset, size);
2586
2587 /* Init new header now. */
2588 xh->xh_count = xb_xh->xh_count;
2589 xh->xh_num_buckets = cpu_to_le16(1);
2590 xh->xh_name_value_len = cpu_to_le16(size);
2591 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2592
2593 /* copy all the entries. */
2594 target = xh_bh->b_data;
2595 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2596 size = count * sizeof(struct ocfs2_xattr_entry);
2597 memcpy(target + offset, (char *)xb_xh + offset, size);
2598
2599 /* Change the xe offset for all the xe because of the move. */
2600 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2601 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2602 for (i = 0; i < count; i++)
2603 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2604
2605 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2606 offset, size, off_change);
2607
2608 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2609 cmp_xe, swap_xe);
2610}
2611
2612/*
2613 * After we move xattr from block to index btree, we have to
2614 * update ocfs2_xattr_search to the new xe and base.
2615 *
2616 * When the entry is in xattr block, xattr_bh indicates the storage place.
2617 * While if the entry is in index b-tree, "bucket" indicates the
2618 * real place of the xattr.
2619 */
2620static int ocfs2_xattr_update_xattr_search(struct inode *inode,
2621 struct ocfs2_xattr_search *xs,
2622 struct buffer_head *old_bh,
2623 struct buffer_head *new_bh)
2624{
2625 int ret = 0;
2626 char *buf = old_bh->b_data;
2627 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2628 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
2629 int i, blocksize = inode->i_sb->s_blocksize;
2630 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2631
Joel Becker4ac60322008-10-18 19:11:42 -07002632 xs->bucket.bu_bhs[0] = new_bh;
Tao Ma01225592008-08-18 17:38:53 +08002633 get_bh(new_bh);
Joel Becker51def392008-10-24 16:57:21 -07002634 xs->bucket.bu_xh = (struct ocfs2_xattr_header *)bucket_block(&xs->bucket, 0);
Joel Becker4ac60322008-10-18 19:11:42 -07002635 xs->header = xs->bucket.bu_xh;
Tao Ma01225592008-08-18 17:38:53 +08002636
2637 xs->base = new_bh->b_data;
2638 xs->end = xs->base + inode->i_sb->s_blocksize;
2639
2640 if (!xs->not_found) {
2641 if (OCFS2_XATTR_BUCKET_SIZE != blocksize) {
Joel Becker31d33072008-10-09 17:20:30 -07002642 ret = ocfs2_read_blocks(inode,
Joel Becker9c7759a2008-10-24 16:21:03 -07002643 bucket_blkno(&xs->bucket) + 1,
Joel Becker4ac60322008-10-18 19:11:42 -07002644 blk_per_bucket - 1, &xs->bucket.bu_bhs[1],
Mark Fasheh1efd47f2008-10-14 18:31:46 -07002645 0);
Tao Ma01225592008-08-18 17:38:53 +08002646 if (ret) {
2647 mlog_errno(ret);
2648 return ret;
2649 }
2650
Tao Ma01225592008-08-18 17:38:53 +08002651 }
Tao Ma83099bc2008-12-05 09:14:10 +08002652 i = xs->here - old_xh->xh_entries;
2653 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08002654 }
2655
2656 return ret;
2657}
2658
2659static int ocfs2_xattr_create_index_block(struct inode *inode,
2660 struct ocfs2_xattr_search *xs)
2661{
2662 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2663 u32 bit_off, len;
2664 u64 blkno;
2665 handle_t *handle;
2666 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2667 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2668 struct ocfs2_alloc_context *data_ac;
2669 struct buffer_head *xh_bh = NULL, *data_bh = NULL;
2670 struct buffer_head *xb_bh = xs->xattr_bh;
2671 struct ocfs2_xattr_block *xb =
2672 (struct ocfs2_xattr_block *)xb_bh->b_data;
2673 struct ocfs2_xattr_tree_root *xr;
2674 u16 xb_flags = le16_to_cpu(xb->xb_flags);
2675 u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2676
2677 mlog(0, "create xattr index block for %llu\n",
2678 (unsigned long long)xb_bh->b_blocknr);
2679
2680 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
2681
2682 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2683 if (ret) {
2684 mlog_errno(ret);
2685 goto out;
2686 }
2687
2688 /*
2689 * XXX:
2690 * We can use this lock for now, and maybe move to a dedicated mutex
2691 * if performance becomes a problem later.
2692 */
2693 down_write(&oi->ip_alloc_sem);
2694
2695 /*
2696 * 3 more credits, one for xattr block update, one for the 1st block
2697 * of the new xattr bucket and one for the value/data.
2698 */
2699 credits += 3;
2700 handle = ocfs2_start_trans(osb, credits);
2701 if (IS_ERR(handle)) {
2702 ret = PTR_ERR(handle);
2703 mlog_errno(ret);
2704 goto out_sem;
2705 }
2706
2707 ret = ocfs2_journal_access(handle, inode, xb_bh,
2708 OCFS2_JOURNAL_ACCESS_WRITE);
2709 if (ret) {
2710 mlog_errno(ret);
2711 goto out_commit;
2712 }
2713
2714 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2715 if (ret) {
2716 mlog_errno(ret);
2717 goto out_commit;
2718 }
2719
2720 /*
2721 * The bucket may spread in many blocks, and
2722 * we will only touch the 1st block and the last block
2723 * in the whole bucket(one for entry and one for data).
2724 */
2725 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2726
Mark Fashehde29c082008-10-29 14:45:30 -07002727 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
2728 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08002729
2730 xh_bh = sb_getblk(inode->i_sb, blkno);
2731 if (!xh_bh) {
2732 ret = -EIO;
2733 mlog_errno(ret);
2734 goto out_commit;
2735 }
2736
2737 ocfs2_set_new_buffer_uptodate(inode, xh_bh);
2738
2739 ret = ocfs2_journal_access(handle, inode, xh_bh,
2740 OCFS2_JOURNAL_ACCESS_CREATE);
2741 if (ret) {
2742 mlog_errno(ret);
2743 goto out_commit;
2744 }
2745
2746 if (bpb > 1) {
2747 data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1);
2748 if (!data_bh) {
2749 ret = -EIO;
2750 mlog_errno(ret);
2751 goto out_commit;
2752 }
2753
2754 ocfs2_set_new_buffer_uptodate(inode, data_bh);
2755
2756 ret = ocfs2_journal_access(handle, inode, data_bh,
2757 OCFS2_JOURNAL_ACCESS_CREATE);
2758 if (ret) {
2759 mlog_errno(ret);
2760 goto out_commit;
2761 }
2762 }
2763
2764 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh);
2765
2766 ocfs2_journal_dirty(handle, xh_bh);
2767 if (data_bh)
2768 ocfs2_journal_dirty(handle, data_bh);
2769
Joel Beckerbd60bd32008-10-20 18:25:56 -07002770 ret = ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh);
2771 if (ret) {
2772 mlog_errno(ret);
2773 goto out_commit;
2774 }
Tao Ma01225592008-08-18 17:38:53 +08002775
2776 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2777 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2778 offsetof(struct ocfs2_xattr_block, xb_attrs));
2779
2780 xr = &xb->xb_attrs.xb_root;
2781 xr->xt_clusters = cpu_to_le32(1);
2782 xr->xt_last_eb_blk = 0;
2783 xr->xt_list.l_tree_depth = 0;
2784 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2785 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2786
2787 xr->xt_list.l_recs[0].e_cpos = 0;
2788 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2789 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2790
2791 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2792
2793 ret = ocfs2_journal_dirty(handle, xb_bh);
2794 if (ret) {
2795 mlog_errno(ret);
2796 goto out_commit;
2797 }
2798
2799out_commit:
2800 ocfs2_commit_trans(osb, handle);
2801
2802out_sem:
2803 up_write(&oi->ip_alloc_sem);
2804
2805out:
2806 if (data_ac)
2807 ocfs2_free_alloc_context(data_ac);
2808
2809 brelse(xh_bh);
2810 brelse(data_bh);
2811
2812 return ret;
2813}
2814
2815static int cmp_xe_offset(const void *a, const void *b)
2816{
2817 const struct ocfs2_xattr_entry *l = a, *r = b;
2818 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2819 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2820
2821 if (l_name_offset < r_name_offset)
2822 return 1;
2823 if (l_name_offset > r_name_offset)
2824 return -1;
2825 return 0;
2826}
2827
2828/*
2829 * defrag a xattr bucket if we find that the bucket has some
2830 * holes beteen name/value pairs.
2831 * We will move all the name/value pairs to the end of the bucket
2832 * so that we can spare some space for insertion.
2833 */
2834static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2835 struct ocfs2_xattr_bucket *bucket)
2836{
2837 int ret, i;
2838 size_t end, offset, len, value_len;
2839 struct ocfs2_xattr_header *xh;
2840 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07002841 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08002842 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2843 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08002844 size_t blocksize = inode->i_sb->s_blocksize;
2845 handle_t *handle;
2846 struct buffer_head **bhs;
2847 struct ocfs2_xattr_entry *xe;
2848
2849 bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
2850 GFP_NOFS);
2851 if (!bhs)
2852 return -ENOMEM;
2853
Mark Fasheh1efd47f2008-10-14 18:31:46 -07002854 ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0);
Tao Ma01225592008-08-18 17:38:53 +08002855 if (ret)
2856 goto out;
2857
2858 /*
2859 * In order to make the operation more efficient and generic,
2860 * we copy all the blocks into a contiguous memory and do the
2861 * defragment there, so if anything is error, we will not touch
2862 * the real block.
2863 */
2864 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2865 if (!bucket_buf) {
2866 ret = -EIO;
2867 goto out;
2868 }
2869
2870 buf = bucket_buf;
2871 for (i = 0; i < blk_per_bucket; i++, buf += blocksize)
2872 memcpy(buf, bhs[i]->b_data, blocksize);
2873
2874 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket);
2875 if (IS_ERR(handle)) {
2876 ret = PTR_ERR(handle);
2877 handle = NULL;
2878 mlog_errno(ret);
2879 goto out;
2880 }
2881
2882 for (i = 0; i < blk_per_bucket; i++) {
2883 ret = ocfs2_journal_access(handle, inode, bhs[i],
2884 OCFS2_JOURNAL_ACCESS_WRITE);
2885 if (ret < 0) {
2886 mlog_errno(ret);
2887 goto commit;
2888 }
2889 }
2890
2891 xh = (struct ocfs2_xattr_header *)bucket_buf;
2892 entries = (char *)xh->xh_entries;
2893 xh_free_start = le16_to_cpu(xh->xh_free_start);
2894
2895 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2896 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07002897 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
2898 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08002899
2900 /*
2901 * sort all the entries by their offset.
2902 * the largest will be the first, so that we can
2903 * move them to the end one by one.
2904 */
2905 sort(entries, le16_to_cpu(xh->xh_count),
2906 sizeof(struct ocfs2_xattr_entry),
2907 cmp_xe_offset, swap_xe);
2908
2909 /* Move all name/values to the end of the bucket. */
2910 xe = xh->xh_entries;
2911 end = OCFS2_XATTR_BUCKET_SIZE;
2912 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2913 offset = le16_to_cpu(xe->xe_name_offset);
2914 if (ocfs2_xattr_is_local(xe))
2915 value_len = OCFS2_XATTR_SIZE(
2916 le64_to_cpu(xe->xe_value_size));
2917 else
2918 value_len = OCFS2_XATTR_ROOT_SIZE;
2919 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2920
2921 /*
2922 * We must make sure that the name/value pair
2923 * exist in the same block. So adjust end to
2924 * the previous block end if needed.
2925 */
2926 if (((end - len) / blocksize !=
2927 (end - 1) / blocksize))
2928 end = end - end % blocksize;
2929
2930 if (end > offset + len) {
2931 memmove(bucket_buf + end - len,
2932 bucket_buf + offset, len);
2933 xe->xe_name_offset = cpu_to_le16(end - len);
2934 }
2935
2936 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2937 "bucket %llu\n", (unsigned long long)blkno);
2938
2939 end -= len;
2940 }
2941
2942 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2943 "bucket %llu\n", (unsigned long long)blkno);
2944
2945 if (xh_free_start == end)
2946 goto commit;
2947
2948 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
2949 xh->xh_free_start = cpu_to_le16(end);
2950
2951 /* sort the entries by their name_hash. */
2952 sort(entries, le16_to_cpu(xh->xh_count),
2953 sizeof(struct ocfs2_xattr_entry),
2954 cmp_xe, swap_xe);
2955
2956 buf = bucket_buf;
2957 for (i = 0; i < blk_per_bucket; i++, buf += blocksize) {
2958 memcpy(bhs[i]->b_data, buf, blocksize);
2959 ocfs2_journal_dirty(handle, bhs[i]);
2960 }
2961
2962commit:
2963 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
2964out:
2965
2966 if (bhs) {
2967 for (i = 0; i < blk_per_bucket; i++)
2968 brelse(bhs[i]);
2969 }
2970 kfree(bhs);
2971
2972 kfree(bucket_buf);
2973 return ret;
2974}
2975
2976/*
2977 * Move half nums of the xattr bucket in the previous cluster to this new
2978 * cluster. We only touch the last cluster of the previous extend record.
2979 *
2980 * first_bh is the first buffer_head of a series of bucket in the same
2981 * extent rec and header_bh is the header of one bucket in this cluster.
2982 * They will be updated if we move the data header_bh contains to the new
2983 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
2984 */
2985static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
2986 handle_t *handle,
2987 struct buffer_head **first_bh,
2988 struct buffer_head **header_bh,
2989 u64 new_blkno,
2990 u64 prev_blkno,
2991 u32 num_clusters,
2992 u32 *first_hash)
2993{
2994 int i, ret, credits;
2995 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2996 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
2997 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
2998 int blocksize = inode->i_sb->s_blocksize;
2999 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3000 struct ocfs2_xattr_header *new_xh;
3001 struct ocfs2_xattr_header *xh =
3002 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3003
3004 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3005 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3006
3007 prev_bh = *first_bh;
3008 get_bh(prev_bh);
3009 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3010
3011 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3012
3013 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003014 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003015
3016 /*
3017 * We need to update the 1st half of the new cluster and
3018 * 1 more for the update of the 1st bucket of the previous
3019 * extent record.
3020 */
3021 credits = bpc / 2 + 1;
3022 ret = ocfs2_extend_trans(handle, credits);
3023 if (ret) {
3024 mlog_errno(ret);
3025 goto out;
3026 }
3027
3028 ret = ocfs2_journal_access(handle, inode, prev_bh,
3029 OCFS2_JOURNAL_ACCESS_WRITE);
3030 if (ret) {
3031 mlog_errno(ret);
3032 goto out;
3033 }
3034
3035 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3036 old_bh = new_bh = NULL;
3037 new_bh = sb_getblk(inode->i_sb, new_blkno);
3038 if (!new_bh) {
3039 ret = -EIO;
3040 mlog_errno(ret);
3041 goto out;
3042 }
3043
3044 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3045
3046 ret = ocfs2_journal_access(handle, inode, new_bh,
3047 OCFS2_JOURNAL_ACCESS_CREATE);
3048 if (ret < 0) {
3049 mlog_errno(ret);
3050 brelse(new_bh);
3051 goto out;
3052 }
3053
Joel Becker0fcaa562008-10-09 17:20:31 -07003054 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
Tao Ma01225592008-08-18 17:38:53 +08003055 if (ret < 0) {
3056 mlog_errno(ret);
3057 brelse(new_bh);
3058 goto out;
3059 }
3060
3061 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3062
3063 if (i == 0) {
3064 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3065 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3066
3067 if (first_hash)
3068 *first_hash = le32_to_cpu(
3069 new_xh->xh_entries[0].xe_name_hash);
3070 new_first_bh = new_bh;
3071 get_bh(new_first_bh);
3072 }
3073
3074 ocfs2_journal_dirty(handle, new_bh);
3075
3076 if (*header_bh == old_bh) {
3077 brelse(*header_bh);
3078 *header_bh = new_bh;
3079 get_bh(*header_bh);
3080
3081 brelse(*first_bh);
3082 *first_bh = new_first_bh;
3083 get_bh(*first_bh);
3084 }
3085 brelse(new_bh);
3086 brelse(old_bh);
3087 }
3088
3089 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3090
3091 ocfs2_journal_dirty(handle, prev_bh);
3092out:
3093 brelse(prev_bh);
3094 brelse(new_first_bh);
3095 return ret;
3096}
3097
3098static int ocfs2_read_xattr_bucket(struct inode *inode,
3099 u64 blkno,
3100 struct buffer_head **bhs,
3101 int new)
3102{
3103 int ret = 0;
3104 u16 i, blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3105
3106 if (!new)
Joel Becker31d33072008-10-09 17:20:30 -07003107 return ocfs2_read_blocks(inode, blkno,
Mark Fasheh1efd47f2008-10-14 18:31:46 -07003108 blk_per_bucket, bhs, 0);
Tao Ma01225592008-08-18 17:38:53 +08003109
3110 for (i = 0; i < blk_per_bucket; i++) {
3111 bhs[i] = sb_getblk(inode->i_sb, blkno + i);
3112 if (bhs[i] == NULL) {
3113 ret = -EIO;
3114 mlog_errno(ret);
3115 break;
3116 }
3117 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
3118 }
3119
3120 return ret;
3121}
3122
3123/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003124 * Find the suitable pos when we divide a bucket into 2.
3125 * We have to make sure the xattrs with the same hash value exist
3126 * in the same bucket.
3127 *
3128 * If this ocfs2_xattr_header covers more than one hash value, find a
3129 * place where the hash value changes. Try to find the most even split.
3130 * The most common case is that all entries have different hash values,
3131 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003132 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003133static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3134{
3135 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3136 int count = le16_to_cpu(xh->xh_count);
3137 int delta, middle = count / 2;
3138
3139 /*
3140 * We start at the middle. Each step gets farther away in both
3141 * directions. We therefore hit the change in hash value
3142 * nearest to the middle. Note that this loop does not execute for
3143 * count < 2.
3144 */
3145 for (delta = 0; delta < middle; delta++) {
3146 /* Let's check delta earlier than middle */
3147 if (cmp_xe(&entries[middle - delta - 1],
3148 &entries[middle - delta]))
3149 return middle - delta;
3150
3151 /* For even counts, don't walk off the end */
3152 if ((middle + delta + 1) == count)
3153 continue;
3154
3155 /* Now try delta past middle */
3156 if (cmp_xe(&entries[middle + delta],
3157 &entries[middle + delta + 1]))
3158 return middle + delta + 1;
3159 }
3160
3161 /* Every entry had the same hash */
3162 return count;
3163}
3164
3165/*
3166 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3167 * first_hash will record the 1st hash of the new bucket.
3168 *
3169 * Normally half of the xattrs will be moved. But we have to make
3170 * sure that the xattrs with the same hash value are stored in the
3171 * same bucket. If all the xattrs in this bucket have the same hash
3172 * value, the new bucket will be initialized as an empty one and the
3173 * first_hash will be initialized as (hash_value+1).
3174 */
3175static int ocfs2_divide_xattr_bucket(struct inode *inode,
3176 handle_t *handle,
3177 u64 blk,
3178 u64 new_blk,
3179 u32 *first_hash,
3180 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003181{
3182 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003183 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Tao Ma01225592008-08-18 17:38:53 +08003184 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3185 struct buffer_head **s_bhs, **t_bhs = NULL;
3186 struct ocfs2_xattr_header *xh;
3187 struct ocfs2_xattr_entry *xe;
3188 int blocksize = inode->i_sb->s_blocksize;
3189
Tao Ma80bcaf32008-10-27 06:06:24 +08003190 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003191 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003192
3193 s_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3194 if (!s_bhs)
3195 return -ENOMEM;
3196
3197 ret = ocfs2_read_xattr_bucket(inode, blk, s_bhs, 0);
3198 if (ret) {
3199 mlog_errno(ret);
3200 goto out;
3201 }
3202
3203 ret = ocfs2_journal_access(handle, inode, s_bhs[0],
3204 OCFS2_JOURNAL_ACCESS_WRITE);
3205 if (ret) {
3206 mlog_errno(ret);
3207 goto out;
3208 }
3209
3210 t_bhs = kcalloc(blk_per_bucket, sizeof(struct buffer_head *), GFP_NOFS);
3211 if (!t_bhs) {
3212 ret = -ENOMEM;
3213 goto out;
3214 }
3215
3216 ret = ocfs2_read_xattr_bucket(inode, new_blk, t_bhs, new_bucket_head);
3217 if (ret) {
3218 mlog_errno(ret);
3219 goto out;
3220 }
3221
3222 for (i = 0; i < blk_per_bucket; i++) {
3223 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
Joel Beckereb6ff232008-10-20 18:32:48 -07003224 new_bucket_head ?
3225 OCFS2_JOURNAL_ACCESS_CREATE :
3226 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003227 if (ret) {
3228 mlog_errno(ret);
3229 goto out;
3230 }
3231 }
3232
Tao Ma80bcaf32008-10-27 06:06:24 +08003233 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3234 count = le16_to_cpu(xh->xh_count);
3235 start = ocfs2_xattr_find_divide_pos(xh);
3236
3237 if (start == count) {
3238 xe = &xh->xh_entries[start-1];
3239
3240 /*
3241 * initialized a new empty bucket here.
3242 * The hash value is set as one larger than
3243 * that of the last entry in the previous bucket.
3244 */
3245 for (i = 0; i < blk_per_bucket; i++)
3246 memset(t_bhs[i]->b_data, 0, blocksize);
3247
3248 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
3249 xh->xh_free_start = cpu_to_le16(blocksize);
3250 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3251 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3252
3253 goto set_num_buckets;
3254 }
3255
Tao Ma01225592008-08-18 17:38:53 +08003256 /* copy the whole bucket to the new first. */
3257 for (i = 0; i < blk_per_bucket; i++)
3258 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3259
3260 /* update the new bucket. */
3261 xh = (struct ocfs2_xattr_header *)t_bhs[0]->b_data;
Tao Ma01225592008-08-18 17:38:53 +08003262
3263 /*
3264 * Calculate the total name/value len and xh_free_start for
3265 * the old bucket first.
3266 */
3267 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3268 name_value_len = 0;
3269 for (i = 0; i < start; i++) {
3270 xe = &xh->xh_entries[i];
3271 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3272 if (ocfs2_xattr_is_local(xe))
3273 xe_len +=
3274 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3275 else
3276 xe_len += OCFS2_XATTR_ROOT_SIZE;
3277 name_value_len += xe_len;
3278 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3279 name_offset = le16_to_cpu(xe->xe_name_offset);
3280 }
3281
3282 /*
3283 * Now begin the modification to the new bucket.
3284 *
3285 * In the new bucket, We just move the xattr entry to the beginning
3286 * and don't touch the name/value. So there will be some holes in the
3287 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3288 * called.
3289 */
3290 xe = &xh->xh_entries[start];
3291 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3292 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003293 (int)((char *)xe - (char *)xh),
3294 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003295 memmove((char *)xh->xh_entries, (char *)xe, len);
3296 xe = &xh->xh_entries[count - start];
3297 len = sizeof(struct ocfs2_xattr_entry) * start;
3298 memset((char *)xe, 0, len);
3299
3300 le16_add_cpu(&xh->xh_count, -start);
3301 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3302
3303 /* Calculate xh_free_start for the new bucket. */
3304 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3305 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3306 xe = &xh->xh_entries[i];
3307 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3308 if (ocfs2_xattr_is_local(xe))
3309 xe_len +=
3310 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3311 else
3312 xe_len += OCFS2_XATTR_ROOT_SIZE;
3313 if (le16_to_cpu(xe->xe_name_offset) <
3314 le16_to_cpu(xh->xh_free_start))
3315 xh->xh_free_start = xe->xe_name_offset;
3316 }
3317
Tao Ma80bcaf32008-10-27 06:06:24 +08003318set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003319 /* set xh->xh_num_buckets for the new xh. */
3320 if (new_bucket_head)
3321 xh->xh_num_buckets = cpu_to_le16(1);
3322 else
3323 xh->xh_num_buckets = 0;
3324
3325 for (i = 0; i < blk_per_bucket; i++) {
3326 ocfs2_journal_dirty(handle, t_bhs[i]);
3327 if (ret)
3328 mlog_errno(ret);
3329 }
3330
3331 /* store the first_hash of the new bucket. */
3332 if (first_hash)
3333 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3334
3335 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003336 * Now only update the 1st block of the old bucket. If we
3337 * just added a new empty bucket, there is no need to modify
3338 * it.
Tao Ma01225592008-08-18 17:38:53 +08003339 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003340 if (start == count)
3341 goto out;
3342
Tao Ma01225592008-08-18 17:38:53 +08003343 xh = (struct ocfs2_xattr_header *)s_bhs[0]->b_data;
3344 memset(&xh->xh_entries[start], 0,
3345 sizeof(struct ocfs2_xattr_entry) * (count - start));
3346 xh->xh_count = cpu_to_le16(start);
3347 xh->xh_free_start = cpu_to_le16(name_offset);
3348 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3349
3350 ocfs2_journal_dirty(handle, s_bhs[0]);
3351 if (ret)
3352 mlog_errno(ret);
3353
3354out:
3355 if (s_bhs) {
3356 for (i = 0; i < blk_per_bucket; i++)
3357 brelse(s_bhs[i]);
3358 }
3359 kfree(s_bhs);
3360
3361 if (t_bhs) {
3362 for (i = 0; i < blk_per_bucket; i++)
3363 brelse(t_bhs[i]);
3364 }
3365 kfree(t_bhs);
3366
3367 return ret;
3368}
3369
3370/*
3371 * Copy xattr from one bucket to another bucket.
3372 *
3373 * The caller must make sure that the journal transaction
3374 * has enough space for journaling.
3375 */
3376static int ocfs2_cp_xattr_bucket(struct inode *inode,
3377 handle_t *handle,
3378 u64 s_blkno,
3379 u64 t_blkno,
3380 int t_is_new)
3381{
3382 int ret, i;
3383 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3384 int blocksize = inode->i_sb->s_blocksize;
3385 struct buffer_head **s_bhs, **t_bhs = NULL;
3386
3387 BUG_ON(s_blkno == t_blkno);
3388
3389 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003390 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3391 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003392
3393 s_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3394 GFP_NOFS);
3395 if (!s_bhs)
3396 return -ENOMEM;
3397
3398 ret = ocfs2_read_xattr_bucket(inode, s_blkno, s_bhs, 0);
3399 if (ret)
3400 goto out;
3401
3402 t_bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket,
3403 GFP_NOFS);
3404 if (!t_bhs) {
3405 ret = -ENOMEM;
3406 goto out;
3407 }
3408
3409 ret = ocfs2_read_xattr_bucket(inode, t_blkno, t_bhs, t_is_new);
3410 if (ret)
3411 goto out;
3412
3413 for (i = 0; i < blk_per_bucket; i++) {
3414 ret = ocfs2_journal_access(handle, inode, t_bhs[i],
Joel Beckereb6ff232008-10-20 18:32:48 -07003415 t_is_new ?
3416 OCFS2_JOURNAL_ACCESS_CREATE :
Tao Ma01225592008-08-18 17:38:53 +08003417 OCFS2_JOURNAL_ACCESS_WRITE);
3418 if (ret)
3419 goto out;
3420 }
3421
3422 for (i = 0; i < blk_per_bucket; i++) {
3423 memcpy(t_bhs[i]->b_data, s_bhs[i]->b_data, blocksize);
3424 ocfs2_journal_dirty(handle, t_bhs[i]);
3425 }
3426
3427out:
3428 if (s_bhs) {
3429 for (i = 0; i < blk_per_bucket; i++)
3430 brelse(s_bhs[i]);
3431 }
3432 kfree(s_bhs);
3433
3434 if (t_bhs) {
3435 for (i = 0; i < blk_per_bucket; i++)
3436 brelse(t_bhs[i]);
3437 }
3438 kfree(t_bhs);
3439
3440 return ret;
3441}
3442
3443/*
3444 * Copy one xattr cluster from src_blk to to_blk.
3445 * The to_blk will become the first bucket header of the cluster, so its
3446 * xh_num_buckets will be initialized as the bucket num in the cluster.
3447 */
3448static int ocfs2_cp_xattr_cluster(struct inode *inode,
3449 handle_t *handle,
3450 struct buffer_head *first_bh,
3451 u64 src_blk,
3452 u64 to_blk,
3453 u32 *first_hash)
3454{
3455 int i, ret, credits;
3456 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3457 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3458 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3459 struct buffer_head *bh = NULL;
3460 struct ocfs2_xattr_header *xh;
3461 u64 to_blk_start = to_blk;
3462
Mark Fashehde29c082008-10-29 14:45:30 -07003463 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3464 (unsigned long long)src_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003465
3466 /*
3467 * We need to update the new cluster and 1 more for the update of
3468 * the 1st bucket of the previous extent rec.
3469 */
3470 credits = bpc + 1;
3471 ret = ocfs2_extend_trans(handle, credits);
3472 if (ret) {
3473 mlog_errno(ret);
3474 goto out;
3475 }
3476
3477 ret = ocfs2_journal_access(handle, inode, first_bh,
3478 OCFS2_JOURNAL_ACCESS_WRITE);
3479 if (ret) {
3480 mlog_errno(ret);
3481 goto out;
3482 }
3483
3484 for (i = 0; i < num_buckets; i++) {
3485 ret = ocfs2_cp_xattr_bucket(inode, handle,
3486 src_blk, to_blk, 1);
3487 if (ret) {
3488 mlog_errno(ret);
3489 goto out;
3490 }
3491
3492 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3493 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3494 }
3495
3496 /* update the old bucket header. */
3497 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3498 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3499
3500 ocfs2_journal_dirty(handle, first_bh);
3501
3502 /* update the new bucket header. */
Joel Becker0fcaa562008-10-09 17:20:31 -07003503 ret = ocfs2_read_block(inode, to_blk_start, &bh);
Tao Ma01225592008-08-18 17:38:53 +08003504 if (ret < 0) {
3505 mlog_errno(ret);
3506 goto out;
3507 }
3508
3509 ret = ocfs2_journal_access(handle, inode, bh,
3510 OCFS2_JOURNAL_ACCESS_WRITE);
3511 if (ret) {
3512 mlog_errno(ret);
3513 goto out;
3514 }
3515
3516 xh = (struct ocfs2_xattr_header *)bh->b_data;
3517 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3518
3519 ocfs2_journal_dirty(handle, bh);
3520
3521 if (first_hash)
3522 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3523out:
3524 brelse(bh);
3525 return ret;
3526}
3527
3528/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003529 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08003530 * This function should only be called when bucket size == cluster size.
3531 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3532 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003533static int ocfs2_divide_xattr_cluster(struct inode *inode,
3534 handle_t *handle,
3535 u64 prev_blk,
3536 u64 new_blk,
3537 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003538{
3539 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3540 int ret, credits = 2 * blk_per_bucket;
3541
3542 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3543
3544 ret = ocfs2_extend_trans(handle, credits);
3545 if (ret) {
3546 mlog_errno(ret);
3547 return ret;
3548 }
3549
3550 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08003551 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3552 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08003553}
3554
3555/*
3556 * Move some xattrs from the old cluster to the new one since they are not
3557 * contiguous in ocfs2 xattr tree.
3558 *
3559 * new_blk starts a new separate cluster, and we will move some xattrs from
3560 * prev_blk to it. v_start will be set as the first name hash value in this
3561 * new cluster so that it can be used as e_cpos during tree insertion and
3562 * don't collide with our original b-tree operations. first_bh and header_bh
3563 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3564 * to extend the insert bucket.
3565 *
3566 * The problem is how much xattr should we move to the new one and when should
3567 * we update first_bh and header_bh?
3568 * 1. If cluster size > bucket size, that means the previous cluster has more
3569 * than 1 bucket, so just move half nums of bucket into the new cluster and
3570 * update the first_bh and header_bh if the insert bucket has been moved
3571 * to the new cluster.
3572 * 2. If cluster_size == bucket_size:
3573 * a) If the previous extent rec has more than one cluster and the insert
3574 * place isn't in the last cluster, copy the entire last cluster to the
3575 * new one. This time, we don't need to upate the first_bh and header_bh
3576 * since they will not be moved into the new cluster.
3577 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3578 * the new one. And we set the extend flag to zero if the insert place is
3579 * moved into the new allocated cluster since no extend is needed.
3580 */
3581static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3582 handle_t *handle,
3583 struct buffer_head **first_bh,
3584 struct buffer_head **header_bh,
3585 u64 new_blk,
3586 u64 prev_blk,
3587 u32 prev_clusters,
3588 u32 *v_start,
3589 int *extend)
3590{
3591 int ret = 0;
3592 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3593
3594 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003595 (unsigned long long)prev_blk, prev_clusters,
3596 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003597
3598 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3599 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3600 handle,
3601 first_bh,
3602 header_bh,
3603 new_blk,
3604 prev_blk,
3605 prev_clusters,
3606 v_start);
3607 else {
3608 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3609
3610 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3611 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3612 last_blk, new_blk,
3613 v_start);
3614 else {
Tao Ma80bcaf32008-10-27 06:06:24 +08003615 ret = ocfs2_divide_xattr_cluster(inode, handle,
3616 last_blk, new_blk,
3617 v_start);
Tao Ma01225592008-08-18 17:38:53 +08003618
3619 if ((*header_bh)->b_blocknr == last_blk && extend)
3620 *extend = 0;
3621 }
3622 }
3623
3624 return ret;
3625}
3626
3627/*
3628 * Add a new cluster for xattr storage.
3629 *
3630 * If the new cluster is contiguous with the previous one, it will be
3631 * appended to the same extent record, and num_clusters will be updated.
3632 * If not, we will insert a new extent for it and move some xattrs in
3633 * the last cluster into the new allocated one.
3634 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3635 * lose the benefits of hashing because we'll have to search large leaves.
3636 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3637 * if it's bigger).
3638 *
3639 * first_bh is the first block of the previous extent rec and header_bh
3640 * indicates the bucket we will insert the new xattrs. They will be updated
3641 * when the header_bh is moved into the new cluster.
3642 */
3643static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3644 struct buffer_head *root_bh,
3645 struct buffer_head **first_bh,
3646 struct buffer_head **header_bh,
3647 u32 *num_clusters,
3648 u32 prev_cpos,
3649 u64 prev_blkno,
3650 int *extend)
3651{
3652 int ret, credits;
3653 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3654 u32 prev_clusters = *num_clusters;
3655 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3656 u64 block;
3657 handle_t *handle = NULL;
3658 struct ocfs2_alloc_context *data_ac = NULL;
3659 struct ocfs2_alloc_context *meta_ac = NULL;
3660 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003661 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08003662
3663 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3664 "previous xattr blkno = %llu\n",
3665 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehde29c082008-10-29 14:45:30 -07003666 prev_cpos, (unsigned long long)prev_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003667
Joel Becker8d6220d2008-08-22 12:46:09 -07003668 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003669
3670 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3671 &data_ac, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08003672 if (ret) {
3673 mlog_errno(ret);
3674 goto leave;
3675 }
3676
Joel Beckerf99b9b72008-08-20 19:36:33 -07003677 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3678 clusters_to_add);
Tao Ma01225592008-08-18 17:38:53 +08003679 handle = ocfs2_start_trans(osb, credits);
3680 if (IS_ERR(handle)) {
3681 ret = PTR_ERR(handle);
3682 handle = NULL;
3683 mlog_errno(ret);
3684 goto leave;
3685 }
3686
3687 ret = ocfs2_journal_access(handle, inode, root_bh,
3688 OCFS2_JOURNAL_ACCESS_WRITE);
3689 if (ret < 0) {
3690 mlog_errno(ret);
3691 goto leave;
3692 }
3693
3694 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3695 clusters_to_add, &bit_off, &num_bits);
3696 if (ret < 0) {
3697 if (ret != -ENOSPC)
3698 mlog_errno(ret);
3699 goto leave;
3700 }
3701
3702 BUG_ON(num_bits > clusters_to_add);
3703
3704 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3705 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3706 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3707
3708 if (prev_blkno + prev_clusters * bpc == block &&
3709 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3710 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3711 /*
3712 * If this cluster is contiguous with the old one and
3713 * adding this new cluster, we don't surpass the limit of
3714 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3715 * initialized and used like other buckets in the previous
3716 * cluster.
3717 * So add it as a contiguous one. The caller will handle
3718 * its init process.
3719 */
3720 v_start = prev_cpos + prev_clusters;
3721 *num_clusters = prev_clusters + num_bits;
3722 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3723 num_bits);
3724 } else {
3725 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3726 handle,
3727 first_bh,
3728 header_bh,
3729 block,
3730 prev_blkno,
3731 prev_clusters,
3732 &v_start,
3733 extend);
3734 if (ret) {
3735 mlog_errno(ret);
3736 goto leave;
3737 }
3738 }
3739
Tao Ma28b8ca02008-09-01 08:45:18 +08003740 if (handle->h_buffer_credits < credits) {
3741 /*
3742 * The journal has been restarted before, and don't
3743 * have enough space for the insertion, so extend it
3744 * here.
3745 */
3746 ret = ocfs2_extend_trans(handle, credits);
3747 if (ret) {
3748 mlog_errno(ret);
3749 goto leave;
3750 }
3751 }
Tao Ma01225592008-08-18 17:38:53 +08003752 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003753 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003754 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3755 num_bits, 0, meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08003756 if (ret < 0) {
3757 mlog_errno(ret);
3758 goto leave;
3759 }
3760
3761 ret = ocfs2_journal_dirty(handle, root_bh);
3762 if (ret < 0) {
3763 mlog_errno(ret);
3764 goto leave;
3765 }
3766
3767leave:
3768 if (handle)
3769 ocfs2_commit_trans(osb, handle);
3770 if (data_ac)
3771 ocfs2_free_alloc_context(data_ac);
3772 if (meta_ac)
3773 ocfs2_free_alloc_context(meta_ac);
3774
3775 return ret;
3776}
3777
3778/*
3779 * Extend a new xattr bucket and move xattrs to the end one by one until
3780 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3781 */
3782static int ocfs2_extend_xattr_bucket(struct inode *inode,
3783 struct buffer_head *first_bh,
3784 struct buffer_head *start_bh,
3785 u32 num_clusters)
3786{
3787 int ret, credits;
3788 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3789 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3790 u64 start_blk = start_bh->b_blocknr, end_blk;
3791 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3792 handle_t *handle;
3793 struct ocfs2_xattr_header *first_xh =
3794 (struct ocfs2_xattr_header *)first_bh->b_data;
3795 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3796
3797 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Mark Fashehde29c082008-10-29 14:45:30 -07003798 "from %llu, len = %u\n", (unsigned long long)start_blk,
Tao Ma01225592008-08-18 17:38:53 +08003799 (unsigned long long)first_bh->b_blocknr, num_clusters);
3800
3801 BUG_ON(bucket >= num_buckets);
3802
3803 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3804
3805 /*
3806 * We will touch all the buckets after the start_bh(include it).
3807 * Add one more bucket and modify the first_bh.
3808 */
3809 credits = end_blk - start_blk + 2 * blk_per_bucket + 1;
3810 handle = ocfs2_start_trans(osb, credits);
3811 if (IS_ERR(handle)) {
3812 ret = PTR_ERR(handle);
3813 handle = NULL;
3814 mlog_errno(ret);
3815 goto out;
3816 }
3817
3818 ret = ocfs2_journal_access(handle, inode, first_bh,
3819 OCFS2_JOURNAL_ACCESS_WRITE);
3820 if (ret) {
3821 mlog_errno(ret);
3822 goto commit;
3823 }
3824
3825 while (end_blk != start_blk) {
3826 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3827 end_blk + blk_per_bucket, 0);
3828 if (ret)
3829 goto commit;
3830 end_blk -= blk_per_bucket;
3831 }
3832
3833 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08003834 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
3835 start_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08003836
3837 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3838 ocfs2_journal_dirty(handle, first_bh);
3839
3840commit:
3841 ocfs2_commit_trans(osb, handle);
3842out:
3843 return ret;
3844}
3845
3846/*
3847 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3848 * xb_bh is the ocfs2_xattr_block.
3849 * We will move all the buckets starting from header_bh to the next place. As
3850 * for this one, half num of its xattrs will be moved to the next one.
3851 *
3852 * We will allocate a new cluster if current cluster is full and adjust
3853 * header_bh and first_bh if the insert place is moved to the new cluster.
3854 */
3855static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3856 struct buffer_head *xb_bh,
3857 struct buffer_head *header_bh)
3858{
3859 struct ocfs2_xattr_header *first_xh = NULL;
3860 struct buffer_head *first_bh = NULL;
3861 struct ocfs2_xattr_block *xb =
3862 (struct ocfs2_xattr_block *)xb_bh->b_data;
3863 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3864 struct ocfs2_extent_list *el = &xb_root->xt_list;
3865 struct ocfs2_xattr_header *xh =
3866 (struct ocfs2_xattr_header *)header_bh->b_data;
3867 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3868 struct super_block *sb = inode->i_sb;
3869 struct ocfs2_super *osb = OCFS2_SB(sb);
3870 int ret, num_buckets, extend = 1;
3871 u64 p_blkno;
3872 u32 e_cpos, num_clusters;
3873
3874 mlog(0, "Add new xattr bucket starting form %llu\n",
3875 (unsigned long long)header_bh->b_blocknr);
3876
3877 /*
3878 * Add refrence for header_bh here because it may be
3879 * changed in ocfs2_add_new_xattr_cluster and we need
3880 * to free it in the end.
3881 */
3882 get_bh(header_bh);
3883
3884 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3885 &num_clusters, el);
3886 if (ret) {
3887 mlog_errno(ret);
3888 goto out;
3889 }
3890
Joel Becker0fcaa562008-10-09 17:20:31 -07003891 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
Tao Ma01225592008-08-18 17:38:53 +08003892 if (ret) {
3893 mlog_errno(ret);
3894 goto out;
3895 }
3896
3897 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3898 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3899
3900 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3901 ret = ocfs2_add_new_xattr_cluster(inode,
3902 xb_bh,
3903 &first_bh,
3904 &header_bh,
3905 &num_clusters,
3906 e_cpos,
3907 p_blkno,
3908 &extend);
3909 if (ret) {
3910 mlog_errno(ret);
3911 goto out;
3912 }
3913 }
3914
3915 if (extend)
3916 ret = ocfs2_extend_xattr_bucket(inode,
3917 first_bh,
3918 header_bh,
3919 num_clusters);
3920 if (ret)
3921 mlog_errno(ret);
3922out:
3923 brelse(first_bh);
3924 brelse(header_bh);
3925 return ret;
3926}
3927
3928static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3929 struct ocfs2_xattr_bucket *bucket,
3930 int offs)
3931{
3932 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3933
3934 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07003935 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08003936}
3937
3938/*
3939 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08003940 *
3941 * Note: "local" indicates the real data's locality. So we can't
3942 * just its bucket locality by its length.
3943 */
3944static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3945 struct ocfs2_xattr_info *xi,
3946 struct ocfs2_xattr_search *xs,
3947 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08003948 int local)
Tao Ma01225592008-08-18 17:38:53 +08003949{
3950 struct ocfs2_xattr_entry *last, *xe;
3951 int name_len = strlen(xi->name);
3952 struct ocfs2_xattr_header *xh = xs->header;
3953 u16 count = le16_to_cpu(xh->xh_count), start;
3954 size_t blocksize = inode->i_sb->s_blocksize;
3955 char *val;
3956 size_t offs, size, new_size;
3957
3958 last = &xh->xh_entries[count];
3959 if (!xs->not_found) {
3960 xe = xs->here;
3961 offs = le16_to_cpu(xe->xe_name_offset);
3962 if (ocfs2_xattr_is_local(xe))
3963 size = OCFS2_XATTR_SIZE(name_len) +
3964 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3965 else
3966 size = OCFS2_XATTR_SIZE(name_len) +
3967 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3968
3969 /*
3970 * If the new value will be stored outside, xi->value has been
3971 * initalized as an empty ocfs2_xattr_value_root, and the same
3972 * goes with xi->value_len, so we can set new_size safely here.
3973 * See ocfs2_xattr_set_in_bucket.
3974 */
3975 new_size = OCFS2_XATTR_SIZE(name_len) +
3976 OCFS2_XATTR_SIZE(xi->value_len);
3977
3978 le16_add_cpu(&xh->xh_name_value_len, -size);
3979 if (xi->value) {
3980 if (new_size > size)
3981 goto set_new_name_value;
3982
3983 /* Now replace the old value with new one. */
3984 if (local)
3985 xe->xe_value_size = cpu_to_le64(xi->value_len);
3986 else
3987 xe->xe_value_size = 0;
3988
3989 val = ocfs2_xattr_bucket_get_val(inode,
3990 &xs->bucket, offs);
3991 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3992 size - OCFS2_XATTR_SIZE(name_len));
3993 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3994 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3995 xi->value, xi->value_len);
3996
3997 le16_add_cpu(&xh->xh_name_value_len, new_size);
3998 ocfs2_xattr_set_local(xe, local);
3999 return;
4000 } else {
Tao Ma5a095612008-09-19 22:17:41 +08004001 /*
4002 * Remove the old entry if there is more than one.
4003 * We don't remove the last entry so that we can
4004 * use it to indicate the hash value of the empty
4005 * bucket.
4006 */
Tao Ma01225592008-08-18 17:38:53 +08004007 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08004008 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a095612008-09-19 22:17:41 +08004009 if (xh->xh_count) {
4010 memmove(xe, xe + 1,
4011 (void *)last - (void *)xe);
4012 memset(last, 0,
4013 sizeof(struct ocfs2_xattr_entry));
4014 } else
4015 xh->xh_free_start =
4016 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4017
Tao Ma01225592008-08-18 17:38:53 +08004018 return;
4019 }
4020 } else {
4021 /* find a new entry for insert. */
4022 int low = 0, high = count - 1, tmp;
4023 struct ocfs2_xattr_entry *tmp_xe;
4024
Tao Ma5a095612008-09-19 22:17:41 +08004025 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004026 tmp = (low + high) / 2;
4027 tmp_xe = &xh->xh_entries[tmp];
4028
4029 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4030 low = tmp + 1;
4031 else if (name_hash <
4032 le32_to_cpu(tmp_xe->xe_name_hash))
4033 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004034 else {
4035 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004036 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004037 }
Tao Ma01225592008-08-18 17:38:53 +08004038 }
4039
4040 xe = &xh->xh_entries[low];
4041 if (low != count)
4042 memmove(xe + 1, xe, (void *)last - (void *)xe);
4043
4044 le16_add_cpu(&xh->xh_count, 1);
4045 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4046 xe->xe_name_hash = cpu_to_le32(name_hash);
4047 xe->xe_name_len = name_len;
4048 ocfs2_xattr_set_type(xe, xi->name_index);
4049 }
4050
4051set_new_name_value:
4052 /* Insert the new name+value. */
4053 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4054
4055 /*
4056 * We must make sure that the name/value pair
4057 * exists in the same block.
4058 */
4059 offs = le16_to_cpu(xh->xh_free_start);
4060 start = offs - size;
4061
4062 if (start >> inode->i_sb->s_blocksize_bits !=
4063 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4064 offs = offs - offs % blocksize;
4065 xh->xh_free_start = cpu_to_le16(offs);
4066 }
4067
4068 val = ocfs2_xattr_bucket_get_val(inode,
4069 &xs->bucket, offs - size);
4070 xe->xe_name_offset = cpu_to_le16(offs - size);
4071
4072 memset(val, 0, size);
4073 memcpy(val, xi->name, name_len);
4074 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4075
4076 xe->xe_value_size = cpu_to_le64(xi->value_len);
4077 ocfs2_xattr_set_local(xe, local);
4078 xs->here = xe;
4079 le16_add_cpu(&xh->xh_free_start, -size);
4080 le16_add_cpu(&xh->xh_name_value_len, size);
4081
4082 return;
4083}
4084
4085static int ocfs2_xattr_bucket_handle_journal(struct inode *inode,
4086 handle_t *handle,
4087 struct ocfs2_xattr_search *xs,
4088 struct buffer_head **bhs,
4089 u16 bh_num)
4090{
4091 int ret = 0, off, block_off;
4092 struct ocfs2_xattr_entry *xe = xs->here;
4093
4094 /*
4095 * First calculate all the blocks we should journal_access
4096 * and journal_dirty. The first block should always be touched.
4097 */
4098 ret = ocfs2_journal_dirty(handle, bhs[0]);
4099 if (ret)
4100 mlog_errno(ret);
4101
4102 /* calc the data. */
4103 off = le16_to_cpu(xe->xe_name_offset);
4104 block_off = off >> inode->i_sb->s_blocksize_bits;
4105 ret = ocfs2_journal_dirty(handle, bhs[block_off]);
4106 if (ret)
4107 mlog_errno(ret);
4108
4109 return ret;
4110}
4111
4112/*
4113 * Set the xattr entry in the specified bucket.
4114 * The bucket is indicated by xs->bucket and it should have the enough
4115 * space for the xattr insertion.
4116 */
4117static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4118 struct ocfs2_xattr_info *xi,
4119 struct ocfs2_xattr_search *xs,
4120 u32 name_hash,
Tao Ma5a095612008-09-19 22:17:41 +08004121 int local)
Tao Ma01225592008-08-18 17:38:53 +08004122{
4123 int i, ret;
4124 handle_t *handle = NULL;
4125 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4126 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4127
Mark Fashehff1ec202008-08-19 10:54:29 -07004128 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4129 (unsigned long)xi->value_len, xi->name_index,
Joel Becker9c7759a2008-10-24 16:21:03 -07004130 (unsigned long long)bucket_blkno(&xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004131
Joel Becker4ac60322008-10-18 19:11:42 -07004132 if (!xs->bucket.bu_bhs[1]) {
Joel Becker31d33072008-10-09 17:20:30 -07004133 ret = ocfs2_read_blocks(inode,
Joel Becker9c7759a2008-10-24 16:21:03 -07004134 bucket_blkno(&xs->bucket) + 1,
Joel Becker4ac60322008-10-18 19:11:42 -07004135 blk_per_bucket - 1, &xs->bucket.bu_bhs[1],
Mark Fasheh1efd47f2008-10-14 18:31:46 -07004136 0);
Tao Ma01225592008-08-18 17:38:53 +08004137 if (ret) {
4138 mlog_errno(ret);
4139 goto out;
4140 }
4141 }
4142
4143 handle = ocfs2_start_trans(osb, blk_per_bucket);
4144 if (IS_ERR(handle)) {
4145 ret = PTR_ERR(handle);
4146 handle = NULL;
4147 mlog_errno(ret);
4148 goto out;
4149 }
4150
4151 for (i = 0; i < blk_per_bucket; i++) {
Joel Becker4ac60322008-10-18 19:11:42 -07004152 ret = ocfs2_journal_access(handle, inode, xs->bucket.bu_bhs[i],
Tao Ma01225592008-08-18 17:38:53 +08004153 OCFS2_JOURNAL_ACCESS_WRITE);
4154 if (ret < 0) {
4155 mlog_errno(ret);
4156 goto out;
4157 }
4158 }
4159
Tao Ma5a095612008-09-19 22:17:41 +08004160 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004161
4162 /*Only dirty the blocks we have touched in set xattr. */
4163 ret = ocfs2_xattr_bucket_handle_journal(inode, handle, xs,
Joel Becker4ac60322008-10-18 19:11:42 -07004164 xs->bucket.bu_bhs, blk_per_bucket);
Tao Ma01225592008-08-18 17:38:53 +08004165 if (ret)
4166 mlog_errno(ret);
4167out:
4168 ocfs2_commit_trans(osb, handle);
4169
4170 return ret;
4171}
4172
4173static int ocfs2_xattr_value_update_size(struct inode *inode,
4174 struct buffer_head *xe_bh,
4175 struct ocfs2_xattr_entry *xe,
4176 u64 new_size)
4177{
4178 int ret;
4179 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4180 handle_t *handle = NULL;
4181
4182 handle = ocfs2_start_trans(osb, 1);
Tao Mad3264792008-10-24 07:57:28 +08004183 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004184 ret = -ENOMEM;
4185 mlog_errno(ret);
4186 goto out;
4187 }
4188
4189 ret = ocfs2_journal_access(handle, inode, xe_bh,
4190 OCFS2_JOURNAL_ACCESS_WRITE);
4191 if (ret < 0) {
4192 mlog_errno(ret);
4193 goto out_commit;
4194 }
4195
4196 xe->xe_value_size = cpu_to_le64(new_size);
4197
4198 ret = ocfs2_journal_dirty(handle, xe_bh);
4199 if (ret < 0)
4200 mlog_errno(ret);
4201
4202out_commit:
4203 ocfs2_commit_trans(osb, handle);
4204out:
4205 return ret;
4206}
4207
4208/*
4209 * Truncate the specified xe_off entry in xattr bucket.
4210 * bucket is indicated by header_bh and len is the new length.
4211 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4212 *
4213 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4214 */
4215static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4216 struct buffer_head *header_bh,
4217 int xe_off,
4218 int len)
4219{
4220 int ret, offset;
4221 u64 value_blk;
4222 struct buffer_head *value_bh = NULL;
4223 struct ocfs2_xattr_value_root *xv;
4224 struct ocfs2_xattr_entry *xe;
4225 struct ocfs2_xattr_header *xh =
4226 (struct ocfs2_xattr_header *)header_bh->b_data;
4227 size_t blocksize = inode->i_sb->s_blocksize;
4228
4229 xe = &xh->xh_entries[xe_off];
4230
4231 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4232
4233 offset = le16_to_cpu(xe->xe_name_offset) +
4234 OCFS2_XATTR_SIZE(xe->xe_name_len);
4235
4236 value_blk = offset / blocksize;
4237
4238 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4239 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4240 value_blk += header_bh->b_blocknr;
4241
Joel Becker0fcaa562008-10-09 17:20:31 -07004242 ret = ocfs2_read_block(inode, value_blk, &value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004243 if (ret) {
4244 mlog_errno(ret);
4245 goto out;
4246 }
4247
4248 xv = (struct ocfs2_xattr_value_root *)
4249 (value_bh->b_data + offset % blocksize);
4250
4251 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4252 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4253 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4254 if (ret) {
4255 mlog_errno(ret);
4256 goto out;
4257 }
4258
4259 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4260 if (ret) {
4261 mlog_errno(ret);
4262 goto out;
4263 }
4264
4265out:
4266 brelse(value_bh);
4267 return ret;
4268}
4269
4270static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4271 struct ocfs2_xattr_search *xs,
4272 int len)
4273{
4274 int ret, offset;
4275 struct ocfs2_xattr_entry *xe = xs->here;
4276 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4277
Joel Becker4ac60322008-10-18 19:11:42 -07004278 BUG_ON(!xs->bucket.bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004279
4280 offset = xe - xh->xh_entries;
Joel Becker4ac60322008-10-18 19:11:42 -07004281 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket.bu_bhs[0],
Tao Ma01225592008-08-18 17:38:53 +08004282 offset, len);
4283 if (ret)
4284 mlog_errno(ret);
4285
4286 return ret;
4287}
4288
4289static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4290 struct ocfs2_xattr_search *xs,
4291 char *val,
4292 int value_len)
4293{
4294 int offset;
4295 struct ocfs2_xattr_value_root *xv;
4296 struct ocfs2_xattr_entry *xe = xs->here;
4297
4298 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4299
4300 offset = le16_to_cpu(xe->xe_name_offset) +
4301 OCFS2_XATTR_SIZE(xe->xe_name_len);
4302
4303 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4304
4305 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4306}
4307
Tao Ma01225592008-08-18 17:38:53 +08004308static int ocfs2_rm_xattr_cluster(struct inode *inode,
4309 struct buffer_head *root_bh,
4310 u64 blkno,
4311 u32 cpos,
4312 u32 len)
4313{
4314 int ret;
4315 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4316 struct inode *tl_inode = osb->osb_tl_inode;
4317 handle_t *handle;
4318 struct ocfs2_xattr_block *xb =
4319 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004320 struct ocfs2_alloc_context *meta_ac = NULL;
4321 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004322 struct ocfs2_extent_tree et;
4323
Joel Becker8d6220d2008-08-22 12:46:09 -07004324 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004325
4326 ocfs2_init_dealloc_ctxt(&dealloc);
4327
4328 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4329 cpos, len, (unsigned long long)blkno);
4330
4331 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4332
Joel Beckerf99b9b72008-08-20 19:36:33 -07004333 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004334 if (ret) {
4335 mlog_errno(ret);
4336 return ret;
4337 }
4338
4339 mutex_lock(&tl_inode->i_mutex);
4340
4341 if (ocfs2_truncate_log_needs_flush(osb)) {
4342 ret = __ocfs2_flush_truncate_log(osb);
4343 if (ret < 0) {
4344 mlog_errno(ret);
4345 goto out;
4346 }
4347 }
4348
4349 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
Tao Mad3264792008-10-24 07:57:28 +08004350 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004351 ret = -ENOMEM;
4352 mlog_errno(ret);
4353 goto out;
4354 }
4355
4356 ret = ocfs2_journal_access(handle, inode, root_bh,
4357 OCFS2_JOURNAL_ACCESS_WRITE);
4358 if (ret) {
4359 mlog_errno(ret);
4360 goto out_commit;
4361 }
4362
Joel Beckerf99b9b72008-08-20 19:36:33 -07004363 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4364 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004365 if (ret) {
4366 mlog_errno(ret);
4367 goto out_commit;
4368 }
4369
4370 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4371
4372 ret = ocfs2_journal_dirty(handle, root_bh);
4373 if (ret) {
4374 mlog_errno(ret);
4375 goto out_commit;
4376 }
4377
4378 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4379 if (ret)
4380 mlog_errno(ret);
4381
4382out_commit:
4383 ocfs2_commit_trans(osb, handle);
4384out:
4385 ocfs2_schedule_truncate_log_flush(osb, 1);
4386
4387 mutex_unlock(&tl_inode->i_mutex);
4388
4389 if (meta_ac)
4390 ocfs2_free_alloc_context(meta_ac);
4391
4392 ocfs2_run_deallocs(osb, &dealloc);
4393
4394 return ret;
4395}
4396
Tao Ma01225592008-08-18 17:38:53 +08004397static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4398 struct ocfs2_xattr_search *xs)
4399{
4400 handle_t *handle = NULL;
Joel Becker4ac60322008-10-18 19:11:42 -07004401 struct ocfs2_xattr_header *xh = xs->bucket.bu_xh;
Tao Ma01225592008-08-18 17:38:53 +08004402 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4403 le16_to_cpu(xh->xh_count) - 1];
4404 int ret = 0;
4405
4406 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), 1);
4407 if (IS_ERR(handle)) {
4408 ret = PTR_ERR(handle);
4409 mlog_errno(ret);
4410 return;
4411 }
4412
Joel Becker4ac60322008-10-18 19:11:42 -07004413 ret = ocfs2_journal_access(handle, inode, xs->bucket.bu_bhs[0],
Tao Ma01225592008-08-18 17:38:53 +08004414 OCFS2_JOURNAL_ACCESS_WRITE);
4415 if (ret) {
4416 mlog_errno(ret);
4417 goto out_commit;
4418 }
4419
4420 /* Remove the old entry. */
4421 memmove(xs->here, xs->here + 1,
4422 (void *)last - (void *)xs->here);
4423 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4424 le16_add_cpu(&xh->xh_count, -1);
4425
Joel Becker4ac60322008-10-18 19:11:42 -07004426 ret = ocfs2_journal_dirty(handle, xs->bucket.bu_bhs[0]);
Tao Ma01225592008-08-18 17:38:53 +08004427 if (ret < 0)
4428 mlog_errno(ret);
4429out_commit:
4430 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4431}
4432
4433/*
4434 * Set the xattr name/value in the bucket specified in xs.
4435 *
4436 * As the new value in xi may be stored in the bucket or in an outside cluster,
4437 * we divide the whole process into 3 steps:
4438 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4439 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4440 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4441 * 4. If the clusters for the new outside value can't be allocated, we need
4442 * to free the xattr we allocated in set.
4443 */
4444static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4445 struct ocfs2_xattr_info *xi,
4446 struct ocfs2_xattr_search *xs)
4447{
Tao Ma5a095612008-09-19 22:17:41 +08004448 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004449 size_t value_len;
4450 char *val = (char *)xi->value;
4451 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004452 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4453 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004454
4455 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4456 /*
4457 * We need to truncate the xattr storage first.
4458 *
4459 * If both the old and new value are stored to
4460 * outside block, we only need to truncate
4461 * the storage and then set the value outside.
4462 *
4463 * If the new value should be stored within block,
4464 * we should free all the outside block first and
4465 * the modification to the xattr block will be done
4466 * by following steps.
4467 */
4468 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4469 value_len = xi->value_len;
4470 else
4471 value_len = 0;
4472
4473 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4474 value_len);
4475 if (ret)
4476 goto out;
4477
4478 if (value_len)
4479 goto set_value_outside;
4480 }
4481
4482 value_len = xi->value_len;
4483 /* So we have to handle the inside block change now. */
4484 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4485 /*
4486 * If the new value will be stored outside of block,
4487 * initalize a new empty value root and insert it first.
4488 */
4489 local = 0;
4490 xi->value = &def_xv;
4491 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4492 }
4493
Tao Ma5a095612008-09-19 22:17:41 +08004494 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004495 if (ret) {
4496 mlog_errno(ret);
4497 goto out;
4498 }
4499
Tao Ma5a095612008-09-19 22:17:41 +08004500 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4501 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004502
Tao Ma5a095612008-09-19 22:17:41 +08004503 /* allocate the space now for the outside block storage. */
4504 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4505 value_len);
4506 if (ret) {
4507 mlog_errno(ret);
4508
4509 if (xs->not_found) {
4510 /*
4511 * We can't allocate enough clusters for outside
4512 * storage and we have allocated xattr already,
4513 * so need to remove it.
4514 */
4515 ocfs2_xattr_bucket_remove_xs(inode, xs);
Tao Ma01225592008-08-18 17:38:53 +08004516 }
Tao Ma01225592008-08-18 17:38:53 +08004517 goto out;
4518 }
4519
4520set_value_outside:
4521 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4522out:
4523 return ret;
4524}
4525
Tao Ma80bcaf32008-10-27 06:06:24 +08004526/*
4527 * check whether the xattr bucket is filled up with the same hash value.
4528 * If we want to insert the xattr with the same hash, return -ENOSPC.
4529 * If we want to insert a xattr with different hash value, go ahead
4530 * and ocfs2_divide_xattr_bucket will handle this.
4531 */
Tao Ma01225592008-08-18 17:38:53 +08004532static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004533 struct ocfs2_xattr_bucket *bucket,
4534 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004535{
Joel Becker4ac60322008-10-18 19:11:42 -07004536 struct ocfs2_xattr_header *xh = bucket->bu_xh;
Tao Ma80bcaf32008-10-27 06:06:24 +08004537 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4538
4539 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4540 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004541
4542 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4543 xh->xh_entries[0].xe_name_hash) {
4544 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4545 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004546 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004547 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4548 return -ENOSPC;
4549 }
4550
4551 return 0;
4552}
4553
4554static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4555 struct ocfs2_xattr_info *xi,
4556 struct ocfs2_xattr_search *xs)
4557{
4558 struct ocfs2_xattr_header *xh;
4559 struct ocfs2_xattr_entry *xe;
4560 u16 count, header_size, xh_free_start;
4561 int i, free, max_free, need, old;
4562 size_t value_size = 0, name_len = strlen(xi->name);
4563 size_t blocksize = inode->i_sb->s_blocksize;
4564 int ret, allocation = 0;
4565 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
4566
4567 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4568
4569try_again:
4570 xh = xs->header;
4571 count = le16_to_cpu(xh->xh_count);
4572 xh_free_start = le16_to_cpu(xh->xh_free_start);
4573 header_size = sizeof(struct ocfs2_xattr_header) +
4574 count * sizeof(struct ocfs2_xattr_entry);
4575 max_free = OCFS2_XATTR_BUCKET_SIZE -
4576 le16_to_cpu(xh->xh_name_value_len) - header_size;
4577
4578 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4579 "of %u which exceed block size\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004580 (unsigned long long)bucket_blkno(&xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004581 header_size);
4582
4583 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4584 value_size = OCFS2_XATTR_ROOT_SIZE;
4585 else if (xi->value)
4586 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4587
4588 if (xs->not_found)
4589 need = sizeof(struct ocfs2_xattr_entry) +
4590 OCFS2_XATTR_SIZE(name_len) + value_size;
4591 else {
4592 need = value_size + OCFS2_XATTR_SIZE(name_len);
4593
4594 /*
4595 * We only replace the old value if the new length is smaller
4596 * than the old one. Otherwise we will allocate new space in the
4597 * bucket to store it.
4598 */
4599 xe = xs->here;
4600 if (ocfs2_xattr_is_local(xe))
4601 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4602 else
4603 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4604
4605 if (old >= value_size)
4606 need = 0;
4607 }
4608
4609 free = xh_free_start - header_size;
4610 /*
4611 * We need to make sure the new name/value pair
4612 * can exist in the same block.
4613 */
4614 if (xh_free_start % blocksize < need)
4615 free -= xh_free_start % blocksize;
4616
4617 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4618 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4619 " %u\n", xs->not_found,
Joel Becker9c7759a2008-10-24 16:21:03 -07004620 (unsigned long long)bucket_blkno(&xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004621 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4622 le16_to_cpu(xh->xh_name_value_len));
4623
4624 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4625 if (need <= max_free &&
4626 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4627 /*
4628 * We can create the space by defragment. Since only the
4629 * name/value will be moved, the xe shouldn't be changed
4630 * in xs.
4631 */
4632 ret = ocfs2_defrag_xattr_bucket(inode, &xs->bucket);
4633 if (ret) {
4634 mlog_errno(ret);
4635 goto out;
4636 }
4637
4638 xh_free_start = le16_to_cpu(xh->xh_free_start);
4639 free = xh_free_start - header_size;
4640 if (xh_free_start % blocksize < need)
4641 free -= xh_free_start % blocksize;
4642
4643 if (free >= need)
4644 goto xattr_set;
4645
4646 mlog(0, "Can't get enough space for xattr insert by "
4647 "defragment. Need %u bytes, but we have %d, so "
4648 "allocate new bucket for it.\n", need, free);
4649 }
4650
4651 /*
4652 * We have to add new buckets or clusters and one
4653 * allocation should leave us enough space for insert.
4654 */
4655 BUG_ON(allocation);
4656
4657 /*
4658 * We do not allow for overlapping ranges between buckets. And
4659 * the maximum number of collisions we will allow for then is
4660 * one bucket's worth, so check it here whether we need to
4661 * add a new bucket for the insert.
4662 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004663 ret = ocfs2_check_xattr_bucket_collision(inode,
4664 &xs->bucket,
4665 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08004666 if (ret) {
4667 mlog_errno(ret);
4668 goto out;
4669 }
4670
4671 ret = ocfs2_add_new_xattr_bucket(inode,
4672 xs->xattr_bh,
Joel Becker4ac60322008-10-18 19:11:42 -07004673 xs->bucket.bu_bhs[0]);
Tao Ma01225592008-08-18 17:38:53 +08004674 if (ret) {
4675 mlog_errno(ret);
4676 goto out;
4677 }
4678
4679 for (i = 0; i < blk_per_bucket; i++)
Joel Becker4ac60322008-10-18 19:11:42 -07004680 brelse(xs->bucket.bu_bhs[i]);
Tao Ma01225592008-08-18 17:38:53 +08004681
4682 memset(&xs->bucket, 0, sizeof(xs->bucket));
4683
4684 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4685 xi->name_index,
4686 xi->name, xs);
4687 if (ret && ret != -ENODATA)
4688 goto out;
4689 xs->not_found = ret;
4690 allocation = 1;
4691 goto try_again;
4692 }
4693
4694xattr_set:
4695 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4696out:
4697 mlog_exit(ret);
4698 return ret;
4699}
Tao Maa3944252008-08-18 17:38:54 +08004700
4701static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4702 struct ocfs2_xattr_bucket *bucket,
4703 void *para)
4704{
4705 int ret = 0;
Joel Becker4ac60322008-10-18 19:11:42 -07004706 struct ocfs2_xattr_header *xh = bucket->bu_xh;
Tao Maa3944252008-08-18 17:38:54 +08004707 u16 i;
4708 struct ocfs2_xattr_entry *xe;
4709
4710 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4711 xe = &xh->xh_entries[i];
4712 if (ocfs2_xattr_is_local(xe))
4713 continue;
4714
4715 ret = ocfs2_xattr_bucket_value_truncate(inode,
Joel Becker4ac60322008-10-18 19:11:42 -07004716 bucket->bu_bhs[0],
Tao Maa3944252008-08-18 17:38:54 +08004717 i, 0);
4718 if (ret) {
4719 mlog_errno(ret);
4720 break;
4721 }
4722 }
4723
4724 return ret;
4725}
4726
4727static int ocfs2_delete_xattr_index_block(struct inode *inode,
4728 struct buffer_head *xb_bh)
4729{
4730 struct ocfs2_xattr_block *xb =
4731 (struct ocfs2_xattr_block *)xb_bh->b_data;
4732 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4733 int ret = 0;
4734 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4735 u64 p_blkno;
4736
4737 if (le16_to_cpu(el->l_next_free_rec) == 0)
4738 return 0;
4739
4740 while (name_hash > 0) {
4741 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4742 &e_cpos, &num_clusters, el);
4743 if (ret) {
4744 mlog_errno(ret);
4745 goto out;
4746 }
4747
4748 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4749 ocfs2_delete_xattr_in_bucket,
4750 NULL);
4751 if (ret) {
4752 mlog_errno(ret);
4753 goto out;
4754 }
4755
4756 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4757 p_blkno, e_cpos, num_clusters);
4758 if (ret) {
4759 mlog_errno(ret);
4760 break;
4761 }
4762
4763 if (e_cpos == 0)
4764 break;
4765
4766 name_hash = e_cpos - 1;
4767 }
4768
4769out:
4770 return ret;
4771}
Mark Fasheh99219ae2008-10-07 14:52:59 -07004772
4773/*
4774 * 'trusted' attributes support
4775 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07004776static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4777 size_t list_size, const char *name,
4778 size_t name_len)
4779{
Tiger Yangceb1eba2008-10-23 16:34:13 +08004780 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07004781 const size_t total_len = prefix_len + name_len + 1;
4782
4783 if (list && total_len <= list_size) {
4784 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4785 memcpy(list + prefix_len, name, name_len);
4786 list[prefix_len + name_len] = '\0';
4787 }
4788 return total_len;
4789}
4790
4791static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4792 void *buffer, size_t size)
4793{
4794 if (strcmp(name, "") == 0)
4795 return -EINVAL;
4796 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4797 buffer, size);
4798}
4799
4800static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4801 const void *value, size_t size, int flags)
4802{
4803 if (strcmp(name, "") == 0)
4804 return -EINVAL;
4805
4806 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4807 size, flags);
4808}
4809
4810struct xattr_handler ocfs2_xattr_trusted_handler = {
4811 .prefix = XATTR_TRUSTED_PREFIX,
4812 .list = ocfs2_xattr_trusted_list,
4813 .get = ocfs2_xattr_trusted_get,
4814 .set = ocfs2_xattr_trusted_set,
4815};
4816
Mark Fasheh99219ae2008-10-07 14:52:59 -07004817/*
4818 * 'user' attributes support
4819 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07004820static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4821 size_t list_size, const char *name,
4822 size_t name_len)
4823{
Tiger Yangceb1eba2008-10-23 16:34:13 +08004824 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07004825 const size_t total_len = prefix_len + name_len + 1;
4826 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4827
4828 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4829 return 0;
4830
4831 if (list && total_len <= list_size) {
4832 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4833 memcpy(list + prefix_len, name, name_len);
4834 list[prefix_len + name_len] = '\0';
4835 }
4836 return total_len;
4837}
4838
4839static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4840 void *buffer, size_t size)
4841{
4842 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4843
4844 if (strcmp(name, "") == 0)
4845 return -EINVAL;
4846 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4847 return -EOPNOTSUPP;
4848 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4849 buffer, size);
4850}
4851
4852static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4853 const void *value, size_t size, int flags)
4854{
4855 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4856
4857 if (strcmp(name, "") == 0)
4858 return -EINVAL;
4859 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4860 return -EOPNOTSUPP;
4861
4862 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4863 size, flags);
4864}
4865
4866struct xattr_handler ocfs2_xattr_user_handler = {
4867 .prefix = XATTR_USER_PREFIX,
4868 .list = ocfs2_xattr_user_list,
4869 .get = ocfs2_xattr_user_get,
4870 .set = ocfs2_xattr_user_set,
4871};