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