blob: 029a9f4559f1ac30f77f59bb72c0edb6951ba133 [file] [log] [blame]
Tao Maf56654c2008-08-18 17:38:48 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
Tiger Yangc3cb6822008-10-23 16:33:03 +08006 * Copyright (C) 2004, 2008 Oracle. All rights reserved.
Tao Maf56654c2008-08-18 17:38:48 +08007 *
Tiger Yangcf1d6c72008-08-18 17:11:00 +08008 * CREDITS:
Tiger Yangc3cb6822008-10-23 16:33:03 +08009 * Lots of code in this file is copy from linux/fs/ext3/xattr.c.
10 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080011 *
Tao Maf56654c2008-08-18 17:38:48 +080012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
Tiger Yangc3cb6822008-10-23 16:33:03 +080014 * License version 2 as published by the Free Software Foundation.
Tao Maf56654c2008-08-18 17:38:48 +080015 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
Tao Maf56654c2008-08-18 17:38:48 +080020 */
21
Tiger Yangcf1d6c72008-08-18 17:11:00 +080022#include <linux/capability.h>
23#include <linux/fs.h>
24#include <linux/types.h>
25#include <linux/slab.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h>
28#include <linux/uio.h>
29#include <linux/sched.h>
30#include <linux/splice.h>
31#include <linux/mount.h>
32#include <linux/writeback.h>
33#include <linux/falloc.h>
Tao Ma01225592008-08-18 17:38:53 +080034#include <linux/sort.h>
Mark Fasheh99219ae2008-10-07 14:52:59 -070035#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
Tiger Yangcf1d6c72008-08-18 17:11:00 +080038
Tao Maf56654c2008-08-18 17:38:48 +080039#define MLOG_MASK_PREFIX ML_XATTR
40#include <cluster/masklog.h>
41
42#include "ocfs2.h"
43#include "alloc.h"
44#include "dlmglue.h"
45#include "file.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080046#include "symlink.h"
47#include "sysfile.h"
Tao Maf56654c2008-08-18 17:38:48 +080048#include "inode.h"
49#include "journal.h"
50#include "ocfs2_fs.h"
51#include "suballoc.h"
52#include "uptodate.h"
53#include "buffer_head_io.h"
Tao Ma0c044f02008-08-18 17:38:50 +080054#include "super.h"
Tiger Yangcf1d6c72008-08-18 17:11:00 +080055#include "xattr.h"
56
57
58struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
61};
62
Tao Ma0c044f02008-08-18 17:38:50 +080063struct ocfs2_xattr_bucket {
Joel Beckerba937122008-10-24 19:13:20 -070064 /* The inode these xattrs are associated with */
65 struct inode *bu_inode;
66
67 /* The actual buffers that make up the bucket */
Joel Becker4ac60322008-10-18 19:11:42 -070068 struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
Joel Beckerba937122008-10-24 19:13:20 -070069
70 /* How many blocks make up one bucket for this filesystem */
71 int bu_blocks;
Tao Ma0c044f02008-08-18 17:38:50 +080072};
73
Tiger Yangcf1d6c72008-08-18 17:11:00 +080074#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
75#define OCFS2_XATTR_INLINE_SIZE 80
76
77static struct ocfs2_xattr_def_value_root def_xv = {
78 .xv.xr_list.l_count = cpu_to_le16(1),
79};
80
81struct xattr_handler *ocfs2_xattr_handlers[] = {
82 &ocfs2_xattr_user_handler,
83 &ocfs2_xattr_trusted_handler,
84 NULL
85};
86
Tiger Yangc988fd02008-10-23 16:34:44 +080087static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
Tiger Yangcf1d6c72008-08-18 17:11:00 +080088 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
89 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
90};
91
92struct ocfs2_xattr_info {
93 int name_index;
94 const char *name;
95 const void *value;
96 size_t value_len;
97};
98
99struct ocfs2_xattr_search {
100 struct buffer_head *inode_bh;
101 /*
102 * xattr_bh point to the block buffer head which has extended attribute
103 * when extended attribute in inode, xattr_bh is equal to inode_bh.
104 */
105 struct buffer_head *xattr_bh;
106 struct ocfs2_xattr_header *header;
Joel Beckerba937122008-10-24 19:13:20 -0700107 struct ocfs2_xattr_bucket *bucket;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800108 void *base;
109 void *end;
110 struct ocfs2_xattr_entry *here;
111 int not_found;
112};
113
Tao Ma589dc262008-08-18 17:38:51 +0800114static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
115 struct ocfs2_xattr_header *xh,
116 int index,
117 int *block_off,
118 int *new_offset);
119
Joel Becker54f443f2008-10-20 18:43:07 -0700120static int ocfs2_xattr_block_find(struct inode *inode,
121 int name_index,
122 const char *name,
123 struct ocfs2_xattr_search *xs);
Tao Ma589dc262008-08-18 17:38:51 +0800124static int ocfs2_xattr_index_block_find(struct inode *inode,
125 struct buffer_head *root_bh,
126 int name_index,
127 const char *name,
128 struct ocfs2_xattr_search *xs);
129
Tao Ma0c044f02008-08-18 17:38:50 +0800130static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
131 struct ocfs2_xattr_tree_root *xt,
132 char *buffer,
133 size_t buffer_size);
134
Tao Ma01225592008-08-18 17:38:53 +0800135static int ocfs2_xattr_create_index_block(struct inode *inode,
136 struct ocfs2_xattr_search *xs);
137
138static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
139 struct ocfs2_xattr_info *xi,
140 struct ocfs2_xattr_search *xs);
141
Tao Maa3944252008-08-18 17:38:54 +0800142static int ocfs2_delete_xattr_index_block(struct inode *inode,
143 struct buffer_head *xb_bh);
144
Tiger Yang0030e002008-10-23 16:33:33 +0800145static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
146{
147 return (1 << osb->s_clustersize_bits) / OCFS2_XATTR_BUCKET_SIZE;
148}
149
150static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
151{
152 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
153}
154
155static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
156{
157 u16 len = sb->s_blocksize -
158 offsetof(struct ocfs2_xattr_header, xh_entries);
159
160 return len / sizeof(struct ocfs2_xattr_entry);
161}
162
Joel Becker9c7759a2008-10-24 16:21:03 -0700163#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
Joel Becker51def392008-10-24 16:57:21 -0700164#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
Joel Becker3e632942008-10-24 17:04:49 -0700165#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
Joel Becker9c7759a2008-10-24 16:21:03 -0700166
Joel Beckerba937122008-10-24 19:13:20 -0700167static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode)
Joel Becker6dde41d2008-10-24 17:16:48 -0700168{
Joel Beckerba937122008-10-24 19:13:20 -0700169 struct ocfs2_xattr_bucket *bucket;
170 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Joel Becker6dde41d2008-10-24 17:16:48 -0700171
Joel Beckerba937122008-10-24 19:13:20 -0700172 BUG_ON(blks > OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET);
173
174 bucket = kzalloc(sizeof(struct ocfs2_xattr_bucket), GFP_NOFS);
175 if (bucket) {
176 bucket->bu_inode = inode;
177 bucket->bu_blocks = blks;
178 }
179
180 return bucket;
181}
182
183static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket)
184{
185 int i;
186
187 for (i = 0; i < bucket->bu_blocks; i++) {
Joel Becker6dde41d2008-10-24 17:16:48 -0700188 brelse(bucket->bu_bhs[i]);
189 bucket->bu_bhs[i] = NULL;
190 }
191}
192
Joel Beckerba937122008-10-24 19:13:20 -0700193static void ocfs2_xattr_bucket_free(struct ocfs2_xattr_bucket *bucket)
194{
195 if (bucket) {
196 ocfs2_xattr_bucket_relse(bucket);
197 bucket->bu_inode = NULL;
198 kfree(bucket);
199 }
200}
201
Joel Becker784b8162008-10-24 17:33:40 -0700202/*
203 * A bucket that has never been written to disk doesn't need to be
204 * read. We just need the buffer_heads. Don't call this for
205 * buckets that are already on disk. ocfs2_read_xattr_bucket() initializes
206 * them fully.
207 */
Joel Beckerba937122008-10-24 19:13:20 -0700208static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700209 u64 xb_blkno)
210{
211 int i, rc = 0;
Joel Becker784b8162008-10-24 17:33:40 -0700212
Joel Beckerba937122008-10-24 19:13:20 -0700213 for (i = 0; i < bucket->bu_blocks; i++) {
214 bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb,
215 xb_blkno + i);
Joel Becker784b8162008-10-24 17:33:40 -0700216 if (!bucket->bu_bhs[i]) {
217 rc = -EIO;
218 mlog_errno(rc);
219 break;
220 }
221
Joel Beckerba937122008-10-24 19:13:20 -0700222 ocfs2_set_new_buffer_uptodate(bucket->bu_inode,
223 bucket->bu_bhs[i]);
Joel Becker784b8162008-10-24 17:33:40 -0700224 }
225
226 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700227 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700228 return rc;
229}
230
231/* Read the xattr bucket at xb_blkno */
Joel Beckerba937122008-10-24 19:13:20 -0700232static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket,
Joel Becker784b8162008-10-24 17:33:40 -0700233 u64 xb_blkno)
234{
Joel Beckerba937122008-10-24 19:13:20 -0700235 int rc;
Joel Becker784b8162008-10-24 17:33:40 -0700236
Joel Beckerba937122008-10-24 19:13:20 -0700237 rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno,
238 bucket->bu_blocks, bucket->bu_bhs, 0);
Joel Becker784b8162008-10-24 17:33:40 -0700239 if (rc)
Joel Beckerba937122008-10-24 19:13:20 -0700240 ocfs2_xattr_bucket_relse(bucket);
Joel Becker784b8162008-10-24 17:33:40 -0700241 return rc;
242}
243
Joel Becker1224be02008-10-24 18:47:33 -0700244static int ocfs2_xattr_bucket_journal_access(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700245 struct ocfs2_xattr_bucket *bucket,
246 int type)
247{
248 int i, rc = 0;
Joel Becker1224be02008-10-24 18:47:33 -0700249
Joel Beckerba937122008-10-24 19:13:20 -0700250 for (i = 0; i < bucket->bu_blocks; i++) {
251 rc = ocfs2_journal_access(handle, bucket->bu_inode,
Joel Becker1224be02008-10-24 18:47:33 -0700252 bucket->bu_bhs[i], type);
253 if (rc) {
254 mlog_errno(rc);
255 break;
256 }
257 }
258
259 return rc;
260}
261
262static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle,
Joel Becker1224be02008-10-24 18:47:33 -0700263 struct ocfs2_xattr_bucket *bucket)
264{
Joel Beckerba937122008-10-24 19:13:20 -0700265 int i;
Joel Becker1224be02008-10-24 18:47:33 -0700266
Joel Beckerba937122008-10-24 19:13:20 -0700267 for (i = 0; i < bucket->bu_blocks; i++)
Joel Becker1224be02008-10-24 18:47:33 -0700268 ocfs2_journal_dirty(handle, bucket->bu_bhs[i]);
269}
270
Joel Beckerba937122008-10-24 19:13:20 -0700271static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest,
Joel Becker4980c6d2008-10-24 18:54:43 -0700272 struct ocfs2_xattr_bucket *src)
273{
274 int i;
Joel Beckerba937122008-10-24 19:13:20 -0700275 int blocksize = src->bu_inode->i_sb->s_blocksize;
Joel Becker4980c6d2008-10-24 18:54:43 -0700276
Joel Beckerba937122008-10-24 19:13:20 -0700277 BUG_ON(dest->bu_blocks != src->bu_blocks);
278 BUG_ON(dest->bu_inode != src->bu_inode);
279
280 for (i = 0; i < src->bu_blocks; i++) {
Joel Becker4980c6d2008-10-24 18:54:43 -0700281 memcpy(bucket_block(dest, i), bucket_block(src, i),
282 blocksize);
283 }
284}
Joel Becker1224be02008-10-24 18:47:33 -0700285
Tao Ma936b8832008-10-09 23:06:14 +0800286static inline const char *ocfs2_xattr_prefix(int name_index)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800287{
288 struct xattr_handler *handler = NULL;
289
290 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
291 handler = ocfs2_xattr_handler_map[name_index];
292
Tao Ma936b8832008-10-09 23:06:14 +0800293 return handler ? handler->prefix : NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800294}
295
Mark Fasheh40daa162008-10-07 14:31:42 -0700296static u32 ocfs2_xattr_name_hash(struct inode *inode,
Tao Ma2057e5c2008-10-09 23:06:13 +0800297 const char *name,
Mark Fasheh40daa162008-10-07 14:31:42 -0700298 int name_len)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800299{
300 /* Get hash value of uuid from super block */
301 u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash;
302 int i;
303
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800304 /* hash extended attribute name */
305 for (i = 0; i < name_len; i++) {
306 hash = (hash << OCFS2_HASH_SHIFT) ^
307 (hash >> (8*sizeof(hash) - OCFS2_HASH_SHIFT)) ^
308 *name++;
309 }
310
311 return hash;
312}
313
314/*
315 * ocfs2_xattr_hash_entry()
316 *
317 * Compute the hash of an extended attribute.
318 */
319static void ocfs2_xattr_hash_entry(struct inode *inode,
320 struct ocfs2_xattr_header *header,
321 struct ocfs2_xattr_entry *entry)
322{
323 u32 hash = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800324 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800325
Tao Ma2057e5c2008-10-09 23:06:13 +0800326 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800327 entry->xe_name_hash = cpu_to_le32(hash);
328
329 return;
330}
Tao Maf56654c2008-08-18 17:38:48 +0800331
332static int ocfs2_xattr_extend_allocation(struct inode *inode,
333 u32 clusters_to_add,
334 struct buffer_head *xattr_bh,
335 struct ocfs2_xattr_value_root *xv)
336{
337 int status = 0;
338 int restart_func = 0;
339 int credits = 0;
340 handle_t *handle = NULL;
341 struct ocfs2_alloc_context *data_ac = NULL;
342 struct ocfs2_alloc_context *meta_ac = NULL;
343 enum ocfs2_alloc_restarted why;
344 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Tao Maf56654c2008-08-18 17:38:48 +0800345 u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700346 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800347
348 mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add);
349
Joel Becker8d6220d2008-08-22 12:46:09 -0700350 ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700351
Tao Maf56654c2008-08-18 17:38:48 +0800352restart_all:
353
Joel Beckerf99b9b72008-08-20 19:36:33 -0700354 status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
355 &data_ac, &meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +0800356 if (status) {
357 mlog_errno(status);
358 goto leave;
359 }
360
Joel Beckerf99b9b72008-08-20 19:36:33 -0700361 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
362 clusters_to_add);
Tao Maf56654c2008-08-18 17:38:48 +0800363 handle = ocfs2_start_trans(osb, credits);
364 if (IS_ERR(handle)) {
365 status = PTR_ERR(handle);
366 handle = NULL;
367 mlog_errno(status);
368 goto leave;
369 }
370
371restarted_transaction:
372 status = ocfs2_journal_access(handle, inode, xattr_bh,
373 OCFS2_JOURNAL_ACCESS_WRITE);
374 if (status < 0) {
375 mlog_errno(status);
376 goto leave;
377 }
378
379 prev_clusters = le32_to_cpu(xv->xr_clusters);
380 status = ocfs2_add_clusters_in_btree(osb,
381 inode,
382 &logical_start,
383 clusters_to_add,
384 0,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700385 &et,
Tao Maf56654c2008-08-18 17:38:48 +0800386 handle,
387 data_ac,
388 meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700389 &why);
Tao Maf56654c2008-08-18 17:38:48 +0800390 if ((status < 0) && (status != -EAGAIN)) {
391 if (status != -ENOSPC)
392 mlog_errno(status);
393 goto leave;
394 }
395
396 status = ocfs2_journal_dirty(handle, xattr_bh);
397 if (status < 0) {
398 mlog_errno(status);
399 goto leave;
400 }
401
402 clusters_to_add -= le32_to_cpu(xv->xr_clusters) - prev_clusters;
403
404 if (why != RESTART_NONE && clusters_to_add) {
405 if (why == RESTART_META) {
406 mlog(0, "restarting function.\n");
407 restart_func = 1;
408 } else {
409 BUG_ON(why != RESTART_TRANS);
410
411 mlog(0, "restarting transaction.\n");
412 /* TODO: This can be more intelligent. */
413 credits = ocfs2_calc_extend_credits(osb->sb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700414 et.et_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800415 clusters_to_add);
416 status = ocfs2_extend_trans(handle, credits);
417 if (status < 0) {
418 /* handle still has to be committed at
419 * this point. */
420 status = -ENOMEM;
421 mlog_errno(status);
422 goto leave;
423 }
424 goto restarted_transaction;
425 }
426 }
427
428leave:
429 if (handle) {
430 ocfs2_commit_trans(osb, handle);
431 handle = NULL;
432 }
433 if (data_ac) {
434 ocfs2_free_alloc_context(data_ac);
435 data_ac = NULL;
436 }
437 if (meta_ac) {
438 ocfs2_free_alloc_context(meta_ac);
439 meta_ac = NULL;
440 }
441 if ((!status) && restart_func) {
442 restart_func = 0;
443 goto restart_all;
444 }
445
446 return status;
447}
448
449static int __ocfs2_remove_xattr_range(struct inode *inode,
450 struct buffer_head *root_bh,
451 struct ocfs2_xattr_value_root *xv,
452 u32 cpos, u32 phys_cpos, u32 len,
453 struct ocfs2_cached_dealloc_ctxt *dealloc)
454{
455 int ret;
456 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
457 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
458 struct inode *tl_inode = osb->osb_tl_inode;
459 handle_t *handle;
460 struct ocfs2_alloc_context *meta_ac = NULL;
Joel Beckerf99b9b72008-08-20 19:36:33 -0700461 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +0800462
Joel Becker8d6220d2008-08-22 12:46:09 -0700463 ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700464
465 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +0800466 if (ret) {
467 mlog_errno(ret);
468 return ret;
469 }
470
471 mutex_lock(&tl_inode->i_mutex);
472
473 if (ocfs2_truncate_log_needs_flush(osb)) {
474 ret = __ocfs2_flush_truncate_log(osb);
475 if (ret < 0) {
476 mlog_errno(ret);
477 goto out;
478 }
479 }
480
481 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
482 if (IS_ERR(handle)) {
483 ret = PTR_ERR(handle);
484 mlog_errno(ret);
485 goto out;
486 }
487
488 ret = ocfs2_journal_access(handle, inode, root_bh,
489 OCFS2_JOURNAL_ACCESS_WRITE);
490 if (ret) {
491 mlog_errno(ret);
492 goto out_commit;
493 }
494
Joel Beckerf99b9b72008-08-20 19:36:33 -0700495 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
496 dealloc);
Tao Maf56654c2008-08-18 17:38:48 +0800497 if (ret) {
498 mlog_errno(ret);
499 goto out_commit;
500 }
501
502 le32_add_cpu(&xv->xr_clusters, -len);
503
504 ret = ocfs2_journal_dirty(handle, root_bh);
505 if (ret) {
506 mlog_errno(ret);
507 goto out_commit;
508 }
509
510 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
511 if (ret)
512 mlog_errno(ret);
513
514out_commit:
515 ocfs2_commit_trans(osb, handle);
516out:
517 mutex_unlock(&tl_inode->i_mutex);
518
519 if (meta_ac)
520 ocfs2_free_alloc_context(meta_ac);
521
522 return ret;
523}
524
525static int ocfs2_xattr_shrink_size(struct inode *inode,
526 u32 old_clusters,
527 u32 new_clusters,
528 struct buffer_head *root_bh,
529 struct ocfs2_xattr_value_root *xv)
530{
531 int ret = 0;
532 u32 trunc_len, cpos, phys_cpos, alloc_size;
533 u64 block;
534 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
535 struct ocfs2_cached_dealloc_ctxt dealloc;
536
537 ocfs2_init_dealloc_ctxt(&dealloc);
538
539 if (old_clusters <= new_clusters)
540 return 0;
541
542 cpos = new_clusters;
543 trunc_len = old_clusters - new_clusters;
544 while (trunc_len) {
545 ret = ocfs2_xattr_get_clusters(inode, cpos, &phys_cpos,
546 &alloc_size, &xv->xr_list);
547 if (ret) {
548 mlog_errno(ret);
549 goto out;
550 }
551
552 if (alloc_size > trunc_len)
553 alloc_size = trunc_len;
554
555 ret = __ocfs2_remove_xattr_range(inode, root_bh, xv, cpos,
556 phys_cpos, alloc_size,
557 &dealloc);
558 if (ret) {
559 mlog_errno(ret);
560 goto out;
561 }
562
563 block = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
564 ocfs2_remove_xattr_clusters_from_cache(inode, block,
565 alloc_size);
566 cpos += alloc_size;
567 trunc_len -= alloc_size;
568 }
569
570out:
571 ocfs2_schedule_truncate_log_flush(osb, 1);
572 ocfs2_run_deallocs(osb, &dealloc);
573
574 return ret;
575}
576
577static int ocfs2_xattr_value_truncate(struct inode *inode,
578 struct buffer_head *root_bh,
579 struct ocfs2_xattr_value_root *xv,
580 int len)
581{
582 int ret;
583 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, len);
584 u32 old_clusters = le32_to_cpu(xv->xr_clusters);
585
586 if (new_clusters == old_clusters)
587 return 0;
588
589 if (new_clusters > old_clusters)
590 ret = ocfs2_xattr_extend_allocation(inode,
591 new_clusters - old_clusters,
592 root_bh, xv);
593 else
594 ret = ocfs2_xattr_shrink_size(inode,
595 old_clusters, new_clusters,
596 root_bh, xv);
597
598 return ret;
599}
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800600
Tao Ma936b8832008-10-09 23:06:14 +0800601static int ocfs2_xattr_list_entry(char *buffer, size_t size,
602 size_t *result, const char *prefix,
603 const char *name, int name_len)
604{
605 char *p = buffer + *result;
606 int prefix_len = strlen(prefix);
607 int total_len = prefix_len + name_len + 1;
608
609 *result += total_len;
610
611 /* we are just looking for how big our buffer needs to be */
612 if (!size)
613 return 0;
614
615 if (*result > size)
616 return -ERANGE;
617
618 memcpy(p, prefix, prefix_len);
619 memcpy(p + prefix_len, name, name_len);
620 p[prefix_len + name_len] = '\0';
621
622 return 0;
623}
624
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800625static int ocfs2_xattr_list_entries(struct inode *inode,
626 struct ocfs2_xattr_header *header,
627 char *buffer, size_t buffer_size)
628{
Tao Ma936b8832008-10-09 23:06:14 +0800629 size_t result = 0;
630 int i, type, ret;
631 const char *prefix, *name;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800632
633 for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) {
634 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +0800635 type = ocfs2_xattr_get_type(entry);
636 prefix = ocfs2_xattr_prefix(type);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800637
Tao Ma936b8832008-10-09 23:06:14 +0800638 if (prefix) {
639 name = (const char *)header +
640 le16_to_cpu(entry->xe_name_offset);
641
642 ret = ocfs2_xattr_list_entry(buffer, buffer_size,
643 &result, prefix, name,
644 entry->xe_name_len);
645 if (ret)
646 return ret;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800647 }
648 }
649
Tao Ma936b8832008-10-09 23:06:14 +0800650 return result;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800651}
652
653static int ocfs2_xattr_ibody_list(struct inode *inode,
654 struct ocfs2_dinode *di,
655 char *buffer,
656 size_t buffer_size)
657{
658 struct ocfs2_xattr_header *header = NULL;
659 struct ocfs2_inode_info *oi = OCFS2_I(inode);
660 int ret = 0;
661
662 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
663 return ret;
664
665 header = (struct ocfs2_xattr_header *)
666 ((void *)di + inode->i_sb->s_blocksize -
667 le16_to_cpu(di->i_xattr_inline_size));
668
669 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
670
671 return ret;
672}
673
674static int ocfs2_xattr_block_list(struct inode *inode,
675 struct ocfs2_dinode *di,
676 char *buffer,
677 size_t buffer_size)
678{
679 struct buffer_head *blk_bh = NULL;
Tao Ma0c044f02008-08-18 17:38:50 +0800680 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800681 int ret = 0;
682
683 if (!di->i_xattr_loc)
684 return ret;
685
Joel Becker0fcaa56a2008-10-09 17:20:31 -0700686 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800687 if (ret < 0) {
688 mlog_errno(ret);
689 return ret;
690 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800691
Tao Ma0c044f02008-08-18 17:38:50 +0800692 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Joel Beckerf6087fb2008-10-20 18:20:43 -0700693 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
694 ret = -EIO;
695 goto cleanup;
696 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800697
Tao Ma0c044f02008-08-18 17:38:50 +0800698 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
699 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
700 ret = ocfs2_xattr_list_entries(inode, header,
701 buffer, buffer_size);
702 } else {
703 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
704 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
705 buffer, buffer_size);
706 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800707cleanup:
708 brelse(blk_bh);
709
710 return ret;
711}
712
713ssize_t ocfs2_listxattr(struct dentry *dentry,
714 char *buffer,
715 size_t size)
716{
717 int ret = 0, i_ret = 0, b_ret = 0;
718 struct buffer_head *di_bh = NULL;
719 struct ocfs2_dinode *di = NULL;
720 struct ocfs2_inode_info *oi = OCFS2_I(dentry->d_inode);
721
Tiger Yang8154da32008-08-18 17:11:46 +0800722 if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb)))
723 return -EOPNOTSUPP;
724
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800725 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
726 return ret;
727
728 ret = ocfs2_inode_lock(dentry->d_inode, &di_bh, 0);
729 if (ret < 0) {
730 mlog_errno(ret);
731 return ret;
732 }
733
734 di = (struct ocfs2_dinode *)di_bh->b_data;
735
736 down_read(&oi->ip_xattr_sem);
737 i_ret = ocfs2_xattr_ibody_list(dentry->d_inode, di, buffer, size);
738 if (i_ret < 0)
739 b_ret = 0;
740 else {
741 if (buffer) {
742 buffer += i_ret;
743 size -= i_ret;
744 }
745 b_ret = ocfs2_xattr_block_list(dentry->d_inode, di,
746 buffer, size);
747 if (b_ret < 0)
748 i_ret = 0;
749 }
750 up_read(&oi->ip_xattr_sem);
751 ocfs2_inode_unlock(dentry->d_inode, 0);
752
753 brelse(di_bh);
754
755 return i_ret + b_ret;
756}
757
758static int ocfs2_xattr_find_entry(int name_index,
759 const char *name,
760 struct ocfs2_xattr_search *xs)
761{
762 struct ocfs2_xattr_entry *entry;
763 size_t name_len;
764 int i, cmp = 1;
765
766 if (name == NULL)
767 return -EINVAL;
768
769 name_len = strlen(name);
770 entry = xs->here;
771 for (i = 0; i < le16_to_cpu(xs->header->xh_count); i++) {
772 cmp = name_index - ocfs2_xattr_get_type(entry);
773 if (!cmp)
774 cmp = name_len - entry->xe_name_len;
775 if (!cmp)
776 cmp = memcmp(name, (xs->base +
777 le16_to_cpu(entry->xe_name_offset)),
778 name_len);
779 if (cmp == 0)
780 break;
781 entry += 1;
782 }
783 xs->here = entry;
784
785 return cmp ? -ENODATA : 0;
786}
787
788static int ocfs2_xattr_get_value_outside(struct inode *inode,
Tao Ma589dc262008-08-18 17:38:51 +0800789 struct ocfs2_xattr_value_root *xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800790 void *buffer,
791 size_t len)
792{
793 u32 cpos, p_cluster, num_clusters, bpc, clusters;
794 u64 blkno;
795 int i, ret = 0;
796 size_t cplen, blocksize;
797 struct buffer_head *bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800798 struct ocfs2_extent_list *el;
799
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800800 el = &xv->xr_list;
801 clusters = le32_to_cpu(xv->xr_clusters);
802 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
803 blocksize = inode->i_sb->s_blocksize;
804
805 cpos = 0;
806 while (cpos < clusters) {
807 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
808 &num_clusters, el);
809 if (ret) {
810 mlog_errno(ret);
811 goto out;
812 }
813
814 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
815 /* Copy ocfs2_xattr_value */
816 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa56a2008-10-09 17:20:31 -0700817 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800818 if (ret) {
819 mlog_errno(ret);
820 goto out;
821 }
822
823 cplen = len >= blocksize ? blocksize : len;
824 memcpy(buffer, bh->b_data, cplen);
825 len -= cplen;
826 buffer += cplen;
827
828 brelse(bh);
829 bh = NULL;
830 if (len == 0)
831 break;
832 }
833 cpos += num_clusters;
834 }
835out:
836 return ret;
837}
838
839static int ocfs2_xattr_ibody_get(struct inode *inode,
840 int name_index,
841 const char *name,
842 void *buffer,
843 size_t buffer_size,
844 struct ocfs2_xattr_search *xs)
845{
846 struct ocfs2_inode_info *oi = OCFS2_I(inode);
847 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
Tao Ma589dc262008-08-18 17:38:51 +0800848 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800849 size_t size;
850 int ret = 0;
851
852 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL))
853 return -ENODATA;
854
855 xs->end = (void *)di + inode->i_sb->s_blocksize;
856 xs->header = (struct ocfs2_xattr_header *)
857 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
858 xs->base = (void *)xs->header;
859 xs->here = xs->header->xh_entries;
860
861 ret = ocfs2_xattr_find_entry(name_index, name, xs);
862 if (ret)
863 return ret;
864 size = le64_to_cpu(xs->here->xe_value_size);
865 if (buffer) {
866 if (size > buffer_size)
867 return -ERANGE;
868 if (ocfs2_xattr_is_local(xs->here)) {
869 memcpy(buffer, (void *)xs->base +
870 le16_to_cpu(xs->here->xe_name_offset) +
871 OCFS2_XATTR_SIZE(xs->here->xe_name_len), size);
872 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800873 xv = (struct ocfs2_xattr_value_root *)
874 (xs->base + le16_to_cpu(
875 xs->here->xe_name_offset) +
876 OCFS2_XATTR_SIZE(xs->here->xe_name_len));
877 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800878 buffer, size);
879 if (ret < 0) {
880 mlog_errno(ret);
881 return ret;
882 }
883 }
884 }
885
886 return size;
887}
888
889static int ocfs2_xattr_block_get(struct inode *inode,
890 int name_index,
891 const char *name,
892 void *buffer,
893 size_t buffer_size,
894 struct ocfs2_xattr_search *xs)
895{
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800896 struct ocfs2_xattr_block *xb;
Tao Ma589dc262008-08-18 17:38:51 +0800897 struct ocfs2_xattr_value_root *xv;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800898 size_t size;
Tao Ma589dc262008-08-18 17:38:51 +0800899 int ret = -ENODATA, name_offset, name_len, block_off, i;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800900
Joel Beckerba937122008-10-24 19:13:20 -0700901 xs->bucket = ocfs2_xattr_bucket_new(inode);
902 if (!xs->bucket) {
903 ret = -ENOMEM;
904 mlog_errno(ret);
905 goto cleanup;
906 }
Tao Ma589dc262008-08-18 17:38:51 +0800907
Joel Becker54f443f2008-10-20 18:43:07 -0700908 ret = ocfs2_xattr_block_find(inode, name_index, name, xs);
909 if (ret) {
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800910 mlog_errno(ret);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800911 goto cleanup;
912 }
913
Tiger Yang6c1e1832008-11-02 19:04:21 +0800914 if (xs->not_found) {
915 ret = -ENODATA;
916 goto cleanup;
917 }
918
Joel Becker54f443f2008-10-20 18:43:07 -0700919 xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800920 size = le64_to_cpu(xs->here->xe_value_size);
921 if (buffer) {
922 ret = -ERANGE;
923 if (size > buffer_size)
924 goto cleanup;
Tao Ma589dc262008-08-18 17:38:51 +0800925
926 name_offset = le16_to_cpu(xs->here->xe_name_offset);
927 name_len = OCFS2_XATTR_SIZE(xs->here->xe_name_len);
928 i = xs->here - xs->header->xh_entries;
929
930 if (le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED) {
931 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Beckerba937122008-10-24 19:13:20 -0700932 bucket_xh(xs->bucket),
Tao Ma589dc262008-08-18 17:38:51 +0800933 i,
934 &block_off,
935 &name_offset);
Joel Beckerba937122008-10-24 19:13:20 -0700936 xs->base = bucket_block(xs->bucket, block_off);
Tao Ma589dc262008-08-18 17:38:51 +0800937 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800938 if (ocfs2_xattr_is_local(xs->here)) {
939 memcpy(buffer, (void *)xs->base +
Tao Ma589dc262008-08-18 17:38:51 +0800940 name_offset + name_len, size);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800941 } else {
Tao Ma589dc262008-08-18 17:38:51 +0800942 xv = (struct ocfs2_xattr_value_root *)
943 (xs->base + name_offset + name_len);
944 ret = ocfs2_xattr_get_value_outside(inode, xv,
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800945 buffer, size);
946 if (ret < 0) {
947 mlog_errno(ret);
948 goto cleanup;
949 }
950 }
951 }
952 ret = size;
953cleanup:
Joel Beckerba937122008-10-24 19:13:20 -0700954 ocfs2_xattr_bucket_free(xs->bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800955
Joel Becker54f443f2008-10-20 18:43:07 -0700956 brelse(xs->xattr_bh);
957 xs->xattr_bh = NULL;
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800958 return ret;
959}
960
961/* ocfs2_xattr_get()
962 *
963 * Copy an extended attribute into the buffer provided.
964 * Buffer is NULL to compute the size of buffer required.
965 */
Tiger Yang0030e002008-10-23 16:33:33 +0800966static int ocfs2_xattr_get(struct inode *inode,
967 int name_index,
968 const char *name,
969 void *buffer,
970 size_t buffer_size)
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800971{
972 int ret;
973 struct ocfs2_dinode *di = NULL;
974 struct buffer_head *di_bh = NULL;
975 struct ocfs2_inode_info *oi = OCFS2_I(inode);
976 struct ocfs2_xattr_search xis = {
977 .not_found = -ENODATA,
978 };
979 struct ocfs2_xattr_search xbs = {
980 .not_found = -ENODATA,
981 };
982
Tiger Yang8154da32008-08-18 17:11:46 +0800983 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
984 return -EOPNOTSUPP;
985
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800986 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
987 ret = -ENODATA;
988
989 ret = ocfs2_inode_lock(inode, &di_bh, 0);
990 if (ret < 0) {
991 mlog_errno(ret);
992 return ret;
993 }
994 xis.inode_bh = xbs.inode_bh = di_bh;
995 di = (struct ocfs2_dinode *)di_bh->b_data;
996
997 down_read(&oi->ip_xattr_sem);
998 ret = ocfs2_xattr_ibody_get(inode, name_index, name, buffer,
999 buffer_size, &xis);
Tiger Yang6c1e1832008-11-02 19:04:21 +08001000 if (ret == -ENODATA && di->i_xattr_loc)
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001001 ret = ocfs2_xattr_block_get(inode, name_index, name, buffer,
1002 buffer_size, &xbs);
1003 up_read(&oi->ip_xattr_sem);
1004 ocfs2_inode_unlock(inode, 0);
1005
1006 brelse(di_bh);
1007
1008 return ret;
1009}
1010
1011static int __ocfs2_xattr_set_value_outside(struct inode *inode,
1012 struct ocfs2_xattr_value_root *xv,
1013 const void *value,
1014 int value_len)
1015{
1016 int ret = 0, i, cp_len, credits;
1017 u16 blocksize = inode->i_sb->s_blocksize;
1018 u32 p_cluster, num_clusters;
1019 u32 cpos = 0, bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1020 u32 clusters = ocfs2_clusters_for_bytes(inode->i_sb, value_len);
1021 u64 blkno;
1022 struct buffer_head *bh = NULL;
1023 handle_t *handle;
1024
1025 BUG_ON(clusters > le32_to_cpu(xv->xr_clusters));
1026
1027 credits = clusters * bpc;
1028 handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb), credits);
1029 if (IS_ERR(handle)) {
1030 ret = PTR_ERR(handle);
1031 mlog_errno(ret);
1032 goto out;
1033 }
1034
1035 while (cpos < clusters) {
1036 ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
1037 &num_clusters, &xv->xr_list);
1038 if (ret) {
1039 mlog_errno(ret);
1040 goto out_commit;
1041 }
1042
1043 blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cluster);
1044
1045 for (i = 0; i < num_clusters * bpc; i++, blkno++) {
Joel Becker0fcaa56a2008-10-09 17:20:31 -07001046 ret = ocfs2_read_block(inode, blkno, &bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001047 if (ret) {
1048 mlog_errno(ret);
1049 goto out_commit;
1050 }
1051
1052 ret = ocfs2_journal_access(handle,
1053 inode,
1054 bh,
1055 OCFS2_JOURNAL_ACCESS_WRITE);
1056 if (ret < 0) {
1057 mlog_errno(ret);
1058 goto out_commit;
1059 }
1060
1061 cp_len = value_len > blocksize ? blocksize : value_len;
1062 memcpy(bh->b_data, value, cp_len);
1063 value_len -= cp_len;
1064 value += cp_len;
1065 if (cp_len < blocksize)
1066 memset(bh->b_data + cp_len, 0,
1067 blocksize - cp_len);
1068
1069 ret = ocfs2_journal_dirty(handle, bh);
1070 if (ret < 0) {
1071 mlog_errno(ret);
1072 goto out_commit;
1073 }
1074 brelse(bh);
1075 bh = NULL;
1076
1077 /*
1078 * XXX: do we need to empty all the following
1079 * blocks in this cluster?
1080 */
1081 if (!value_len)
1082 break;
1083 }
1084 cpos += num_clusters;
1085 }
1086out_commit:
1087 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1088out:
1089 brelse(bh);
1090
1091 return ret;
1092}
1093
1094static int ocfs2_xattr_cleanup(struct inode *inode,
1095 struct ocfs2_xattr_info *xi,
1096 struct ocfs2_xattr_search *xs,
1097 size_t offs)
1098{
1099 handle_t *handle = NULL;
1100 int ret = 0;
1101 size_t name_len = strlen(xi->name);
1102 void *val = xs->base + offs;
1103 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1104
1105 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1106 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1107 if (IS_ERR(handle)) {
1108 ret = PTR_ERR(handle);
1109 mlog_errno(ret);
1110 goto out;
1111 }
1112 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1113 OCFS2_JOURNAL_ACCESS_WRITE);
1114 if (ret) {
1115 mlog_errno(ret);
1116 goto out_commit;
1117 }
1118 /* Decrease xattr count */
1119 le16_add_cpu(&xs->header->xh_count, -1);
1120 /* Remove the xattr entry and tree root which has already be set*/
1121 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1122 memset(val, 0, size);
1123
1124 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1125 if (ret < 0)
1126 mlog_errno(ret);
1127out_commit:
1128 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1129out:
1130 return ret;
1131}
1132
1133static int ocfs2_xattr_update_entry(struct inode *inode,
1134 struct ocfs2_xattr_info *xi,
1135 struct ocfs2_xattr_search *xs,
1136 size_t offs)
1137{
1138 handle_t *handle = NULL;
1139 int ret = 0;
1140
1141 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1142 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1143 if (IS_ERR(handle)) {
1144 ret = PTR_ERR(handle);
1145 mlog_errno(ret);
1146 goto out;
1147 }
1148 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1149 OCFS2_JOURNAL_ACCESS_WRITE);
1150 if (ret) {
1151 mlog_errno(ret);
1152 goto out_commit;
1153 }
1154
1155 xs->here->xe_name_offset = cpu_to_le16(offs);
1156 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1157 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE)
1158 ocfs2_xattr_set_local(xs->here, 1);
1159 else
1160 ocfs2_xattr_set_local(xs->here, 0);
1161 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1162
1163 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1164 if (ret < 0)
1165 mlog_errno(ret);
1166out_commit:
1167 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1168out:
1169 return ret;
1170}
1171
1172/*
1173 * ocfs2_xattr_set_value_outside()
1174 *
1175 * Set large size value in B tree.
1176 */
1177static int ocfs2_xattr_set_value_outside(struct inode *inode,
1178 struct ocfs2_xattr_info *xi,
1179 struct ocfs2_xattr_search *xs,
1180 size_t offs)
1181{
1182 size_t name_len = strlen(xi->name);
1183 void *val = xs->base + offs;
1184 struct ocfs2_xattr_value_root *xv = NULL;
1185 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1186 int ret = 0;
1187
1188 memset(val, 0, size);
1189 memcpy(val, xi->name, name_len);
1190 xv = (struct ocfs2_xattr_value_root *)
1191 (val + OCFS2_XATTR_SIZE(name_len));
1192 xv->xr_clusters = 0;
1193 xv->xr_last_eb_blk = 0;
1194 xv->xr_list.l_tree_depth = 0;
1195 xv->xr_list.l_count = cpu_to_le16(1);
1196 xv->xr_list.l_next_free_rec = 0;
1197
1198 ret = ocfs2_xattr_value_truncate(inode, xs->xattr_bh, xv,
1199 xi->value_len);
1200 if (ret < 0) {
1201 mlog_errno(ret);
1202 return ret;
1203 }
1204 ret = __ocfs2_xattr_set_value_outside(inode, xv, xi->value,
1205 xi->value_len);
1206 if (ret < 0) {
1207 mlog_errno(ret);
1208 return ret;
1209 }
1210 ret = ocfs2_xattr_update_entry(inode, xi, xs, offs);
1211 if (ret < 0)
1212 mlog_errno(ret);
1213
1214 return ret;
1215}
1216
1217/*
1218 * ocfs2_xattr_set_entry_local()
1219 *
1220 * Set, replace or remove extended attribute in local.
1221 */
1222static void ocfs2_xattr_set_entry_local(struct inode *inode,
1223 struct ocfs2_xattr_info *xi,
1224 struct ocfs2_xattr_search *xs,
1225 struct ocfs2_xattr_entry *last,
1226 size_t min_offs)
1227{
1228 size_t name_len = strlen(xi->name);
1229 int i;
1230
1231 if (xi->value && xs->not_found) {
1232 /* Insert the new xattr entry. */
1233 le16_add_cpu(&xs->header->xh_count, 1);
1234 ocfs2_xattr_set_type(last, xi->name_index);
1235 ocfs2_xattr_set_local(last, 1);
1236 last->xe_name_len = name_len;
1237 } else {
1238 void *first_val;
1239 void *val;
1240 size_t offs, size;
1241
1242 first_val = xs->base + min_offs;
1243 offs = le16_to_cpu(xs->here->xe_name_offset);
1244 val = xs->base + offs;
1245
1246 if (le64_to_cpu(xs->here->xe_value_size) >
1247 OCFS2_XATTR_INLINE_SIZE)
1248 size = OCFS2_XATTR_SIZE(name_len) +
1249 OCFS2_XATTR_ROOT_SIZE;
1250 else
1251 size = OCFS2_XATTR_SIZE(name_len) +
1252 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1253
1254 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1255 OCFS2_XATTR_SIZE(xi->value_len)) {
1256 /* The old and the new value have the
1257 same size. Just replace the value. */
1258 ocfs2_xattr_set_local(xs->here, 1);
1259 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1260 /* Clear value bytes. */
1261 memset(val + OCFS2_XATTR_SIZE(name_len),
1262 0,
1263 OCFS2_XATTR_SIZE(xi->value_len));
1264 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1265 xi->value,
1266 xi->value_len);
1267 return;
1268 }
1269 /* Remove the old name+value. */
1270 memmove(first_val + size, first_val, val - first_val);
1271 memset(first_val, 0, size);
1272 xs->here->xe_name_hash = 0;
1273 xs->here->xe_name_offset = 0;
1274 ocfs2_xattr_set_local(xs->here, 1);
1275 xs->here->xe_value_size = 0;
1276
1277 min_offs += size;
1278
1279 /* Adjust all value offsets. */
1280 last = xs->header->xh_entries;
1281 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1282 size_t o = le16_to_cpu(last->xe_name_offset);
1283
1284 if (o < offs)
1285 last->xe_name_offset = cpu_to_le16(o + size);
1286 last += 1;
1287 }
1288
1289 if (!xi->value) {
1290 /* Remove the old entry. */
1291 last -= 1;
1292 memmove(xs->here, xs->here + 1,
1293 (void *)last - (void *)xs->here);
1294 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
1295 le16_add_cpu(&xs->header->xh_count, -1);
1296 }
1297 }
1298 if (xi->value) {
1299 /* Insert the new name+value. */
1300 size_t size = OCFS2_XATTR_SIZE(name_len) +
1301 OCFS2_XATTR_SIZE(xi->value_len);
1302 void *val = xs->base + min_offs - size;
1303
1304 xs->here->xe_name_offset = cpu_to_le16(min_offs - size);
1305 memset(val, 0, size);
1306 memcpy(val, xi->name, name_len);
1307 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1308 xi->value,
1309 xi->value_len);
1310 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1311 ocfs2_xattr_set_local(xs->here, 1);
1312 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1313 }
1314
1315 return;
1316}
1317
1318/*
1319 * ocfs2_xattr_set_entry()
1320 *
1321 * Set extended attribute entry into inode or block.
1322 *
1323 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE,
1324 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(),
1325 * then set value in B tree with set_value_outside().
1326 */
1327static int ocfs2_xattr_set_entry(struct inode *inode,
1328 struct ocfs2_xattr_info *xi,
1329 struct ocfs2_xattr_search *xs,
1330 int flag)
1331{
1332 struct ocfs2_xattr_entry *last;
1333 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1334 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1335 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name);
1336 size_t size_l = 0;
1337 handle_t *handle = NULL;
1338 int free, i, ret;
1339 struct ocfs2_xattr_info xi_l = {
1340 .name_index = xi->name_index,
1341 .name = xi->name,
1342 .value = xi->value,
1343 .value_len = xi->value_len,
1344 };
1345
1346 /* Compute min_offs, last and free space. */
1347 last = xs->header->xh_entries;
1348
1349 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1350 size_t offs = le16_to_cpu(last->xe_name_offset);
1351 if (offs < min_offs)
1352 min_offs = offs;
1353 last += 1;
1354 }
1355
1356 free = min_offs - ((void *)last - xs->base) - sizeof(__u32);
1357 if (free < 0)
Joel Beckerb37c4d82008-10-20 18:24:03 -07001358 return -EIO;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001359
1360 if (!xs->not_found) {
1361 size_t size = 0;
1362 if (ocfs2_xattr_is_local(xs->here))
1363 size = OCFS2_XATTR_SIZE(name_len) +
1364 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1365 else
1366 size = OCFS2_XATTR_SIZE(name_len) +
1367 OCFS2_XATTR_ROOT_SIZE;
1368 free += (size + sizeof(struct ocfs2_xattr_entry));
1369 }
1370 /* Check free space in inode or block */
1371 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1372 if (free < sizeof(struct ocfs2_xattr_entry) +
1373 OCFS2_XATTR_SIZE(name_len) +
1374 OCFS2_XATTR_ROOT_SIZE) {
1375 ret = -ENOSPC;
1376 goto out;
1377 }
1378 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1379 xi_l.value = (void *)&def_xv;
1380 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE;
1381 } else if (xi->value) {
1382 if (free < sizeof(struct ocfs2_xattr_entry) +
1383 OCFS2_XATTR_SIZE(name_len) +
1384 OCFS2_XATTR_SIZE(xi->value_len)) {
1385 ret = -ENOSPC;
1386 goto out;
1387 }
1388 }
1389
1390 if (!xs->not_found) {
1391 /* For existing extended attribute */
1392 size_t size = OCFS2_XATTR_SIZE(name_len) +
1393 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1394 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1395 void *val = xs->base + offs;
1396
1397 if (ocfs2_xattr_is_local(xs->here) && size == size_l) {
1398 /* Replace existing local xattr with tree root */
1399 ret = ocfs2_xattr_set_value_outside(inode, xi, xs,
1400 offs);
1401 if (ret < 0)
1402 mlog_errno(ret);
1403 goto out;
1404 } else if (!ocfs2_xattr_is_local(xs->here)) {
1405 /* For existing xattr which has value outside */
1406 struct ocfs2_xattr_value_root *xv = NULL;
1407 xv = (struct ocfs2_xattr_value_root *)(val +
1408 OCFS2_XATTR_SIZE(name_len));
1409
1410 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1411 /*
1412 * If new value need set outside also,
1413 * first truncate old value to new value,
1414 * then set new value with set_value_outside().
1415 */
1416 ret = ocfs2_xattr_value_truncate(inode,
1417 xs->xattr_bh,
1418 xv,
1419 xi->value_len);
1420 if (ret < 0) {
1421 mlog_errno(ret);
1422 goto out;
1423 }
1424
1425 ret = __ocfs2_xattr_set_value_outside(inode,
1426 xv,
1427 xi->value,
1428 xi->value_len);
1429 if (ret < 0) {
1430 mlog_errno(ret);
1431 goto out;
1432 }
1433
1434 ret = ocfs2_xattr_update_entry(inode,
1435 xi,
1436 xs,
1437 offs);
1438 if (ret < 0)
1439 mlog_errno(ret);
1440 goto out;
1441 } else {
1442 /*
1443 * If new value need set in local,
1444 * just trucate old value to zero.
1445 */
1446 ret = ocfs2_xattr_value_truncate(inode,
1447 xs->xattr_bh,
1448 xv,
1449 0);
1450 if (ret < 0)
1451 mlog_errno(ret);
1452 }
1453 }
1454 }
1455
1456 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1457 OCFS2_INODE_UPDATE_CREDITS);
1458 if (IS_ERR(handle)) {
1459 ret = PTR_ERR(handle);
1460 mlog_errno(ret);
1461 goto out;
1462 }
1463
1464 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1465 OCFS2_JOURNAL_ACCESS_WRITE);
1466 if (ret) {
1467 mlog_errno(ret);
1468 goto out_commit;
1469 }
1470
1471 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
Tao Ma28b8ca02008-09-01 08:45:18 +08001472 /* set extended attribute in external block. */
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001473 ret = ocfs2_extend_trans(handle,
Tao Ma28b8ca02008-09-01 08:45:18 +08001474 OCFS2_INODE_UPDATE_CREDITS +
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001475 OCFS2_XATTR_BLOCK_UPDATE_CREDITS);
1476 if (ret) {
1477 mlog_errno(ret);
1478 goto out_commit;
1479 }
1480 ret = ocfs2_journal_access(handle, inode, xs->xattr_bh,
1481 OCFS2_JOURNAL_ACCESS_WRITE);
1482 if (ret) {
1483 mlog_errno(ret);
1484 goto out_commit;
1485 }
1486 }
1487
1488 /*
1489 * Set value in local, include set tree root in local.
1490 * This is the first step for value size >INLINE_SIZE.
1491 */
1492 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1493
1494 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1495 ret = ocfs2_journal_dirty(handle, xs->xattr_bh);
1496 if (ret < 0) {
1497 mlog_errno(ret);
1498 goto out_commit;
1499 }
1500 }
1501
1502 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) &&
1503 (flag & OCFS2_INLINE_XATTR_FL)) {
1504 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1505 unsigned int xattrsize = osb->s_xattr_inline_size;
1506
1507 /*
1508 * Adjust extent record count or inline data size
1509 * to reserve space for extended attribute.
1510 */
1511 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1512 struct ocfs2_inline_data *idata = &di->id2.i_data;
1513 le16_add_cpu(&idata->id_count, -xattrsize);
1514 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1515 struct ocfs2_extent_list *el = &di->id2.i_list;
1516 le16_add_cpu(&el->l_count, -(xattrsize /
1517 sizeof(struct ocfs2_extent_rec)));
1518 }
1519 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1520 }
1521 /* Update xattr flag */
1522 spin_lock(&oi->ip_lock);
1523 oi->ip_dyn_features |= flag;
1524 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1525 spin_unlock(&oi->ip_lock);
1526 /* Update inode ctime */
1527 inode->i_ctime = CURRENT_TIME;
1528 di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
1529 di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
1530
1531 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
1532 if (ret < 0)
1533 mlog_errno(ret);
1534
1535out_commit:
1536 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1537
1538 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) {
1539 /*
1540 * Set value outside in B tree.
1541 * This is the second step for value size > INLINE_SIZE.
1542 */
1543 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1544 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, offs);
1545 if (ret < 0) {
1546 int ret2;
1547
1548 mlog_errno(ret);
1549 /*
1550 * If set value outside failed, we have to clean
1551 * the junk tree root we have already set in local.
1552 */
1553 ret2 = ocfs2_xattr_cleanup(inode, xi, xs, offs);
1554 if (ret2 < 0)
1555 mlog_errno(ret2);
1556 }
1557 }
1558out:
1559 return ret;
1560
1561}
1562
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001563static int ocfs2_remove_value_outside(struct inode*inode,
1564 struct buffer_head *bh,
1565 struct ocfs2_xattr_header *header)
1566{
1567 int ret = 0, i;
1568
1569 for (i = 0; i < le16_to_cpu(header->xh_count); i++) {
1570 struct ocfs2_xattr_entry *entry = &header->xh_entries[i];
1571
1572 if (!ocfs2_xattr_is_local(entry)) {
1573 struct ocfs2_xattr_value_root *xv;
1574 void *val;
1575
1576 val = (void *)header +
1577 le16_to_cpu(entry->xe_name_offset);
1578 xv = (struct ocfs2_xattr_value_root *)
1579 (val + OCFS2_XATTR_SIZE(entry->xe_name_len));
1580 ret = ocfs2_xattr_value_truncate(inode, bh, xv, 0);
1581 if (ret < 0) {
1582 mlog_errno(ret);
1583 return ret;
1584 }
1585 }
1586 }
1587
1588 return ret;
1589}
1590
1591static int ocfs2_xattr_ibody_remove(struct inode *inode,
1592 struct buffer_head *di_bh)
1593{
1594
1595 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1596 struct ocfs2_xattr_header *header;
1597 int ret;
1598
1599 header = (struct ocfs2_xattr_header *)
1600 ((void *)di + inode->i_sb->s_blocksize -
1601 le16_to_cpu(di->i_xattr_inline_size));
1602
1603 ret = ocfs2_remove_value_outside(inode, di_bh, header);
1604
1605 return ret;
1606}
1607
1608static int ocfs2_xattr_block_remove(struct inode *inode,
1609 struct buffer_head *blk_bh)
1610{
1611 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001612 int ret = 0;
1613
1614 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
Tao Maa3944252008-08-18 17:38:54 +08001615 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1616 struct ocfs2_xattr_header *header = &(xb->xb_attrs.xb_header);
1617 ret = ocfs2_remove_value_outside(inode, blk_bh, header);
1618 } else
1619 ret = ocfs2_delete_xattr_index_block(inode, blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001620
1621 return ret;
1622}
1623
Tao Ma08413892008-08-29 09:00:19 +08001624static int ocfs2_xattr_free_block(struct inode *inode,
1625 u64 block)
1626{
1627 struct inode *xb_alloc_inode;
1628 struct buffer_head *xb_alloc_bh = NULL;
1629 struct buffer_head *blk_bh = NULL;
1630 struct ocfs2_xattr_block *xb;
1631 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1632 handle_t *handle;
1633 int ret = 0;
1634 u64 blk, bg_blkno;
1635 u16 bit;
1636
Joel Becker0fcaa56a2008-10-09 17:20:31 -07001637 ret = ocfs2_read_block(inode, block, &blk_bh);
Tao Ma08413892008-08-29 09:00:19 +08001638 if (ret < 0) {
1639 mlog_errno(ret);
1640 goto out;
1641 }
1642
Joel Beckerf6087fb2008-10-20 18:20:43 -07001643 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1644 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1645 ret = -EIO;
Tao Ma08413892008-08-29 09:00:19 +08001646 goto out;
1647 }
1648
1649 ret = ocfs2_xattr_block_remove(inode, blk_bh);
1650 if (ret < 0) {
1651 mlog_errno(ret);
1652 goto out;
1653 }
1654
Tao Ma08413892008-08-29 09:00:19 +08001655 blk = le64_to_cpu(xb->xb_blkno);
1656 bit = le16_to_cpu(xb->xb_suballoc_bit);
1657 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1658
1659 xb_alloc_inode = ocfs2_get_system_file_inode(osb,
1660 EXTENT_ALLOC_SYSTEM_INODE,
1661 le16_to_cpu(xb->xb_suballoc_slot));
1662 if (!xb_alloc_inode) {
1663 ret = -ENOMEM;
1664 mlog_errno(ret);
1665 goto out;
1666 }
1667 mutex_lock(&xb_alloc_inode->i_mutex);
1668
1669 ret = ocfs2_inode_lock(xb_alloc_inode, &xb_alloc_bh, 1);
1670 if (ret < 0) {
1671 mlog_errno(ret);
1672 goto out_mutex;
1673 }
1674
1675 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
1676 if (IS_ERR(handle)) {
1677 ret = PTR_ERR(handle);
1678 mlog_errno(ret);
1679 goto out_unlock;
1680 }
1681
1682 ret = ocfs2_free_suballoc_bits(handle, xb_alloc_inode, xb_alloc_bh,
1683 bit, bg_blkno, 1);
1684 if (ret < 0)
1685 mlog_errno(ret);
1686
1687 ocfs2_commit_trans(osb, handle);
1688out_unlock:
1689 ocfs2_inode_unlock(xb_alloc_inode, 1);
1690 brelse(xb_alloc_bh);
1691out_mutex:
1692 mutex_unlock(&xb_alloc_inode->i_mutex);
1693 iput(xb_alloc_inode);
1694out:
1695 brelse(blk_bh);
1696 return ret;
1697}
1698
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001699/*
1700 * ocfs2_xattr_remove()
1701 *
1702 * Free extended attribute resources associated with this inode.
1703 */
1704int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh)
1705{
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001706 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1707 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1708 handle_t *handle;
1709 int ret;
1710
Tiger Yang8154da32008-08-18 17:11:46 +08001711 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
1712 return 0;
1713
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001714 if (!(oi->ip_dyn_features & OCFS2_HAS_XATTR_FL))
1715 return 0;
1716
1717 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1718 ret = ocfs2_xattr_ibody_remove(inode, di_bh);
1719 if (ret < 0) {
1720 mlog_errno(ret);
1721 goto out;
1722 }
1723 }
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001724
Tao Ma08413892008-08-29 09:00:19 +08001725 if (di->i_xattr_loc) {
1726 ret = ocfs2_xattr_free_block(inode,
1727 le64_to_cpu(di->i_xattr_loc));
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001728 if (ret < 0) {
1729 mlog_errno(ret);
1730 goto out;
1731 }
1732 }
1733
1734 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
1735 OCFS2_INODE_UPDATE_CREDITS);
1736 if (IS_ERR(handle)) {
1737 ret = PTR_ERR(handle);
1738 mlog_errno(ret);
1739 goto out;
1740 }
1741 ret = ocfs2_journal_access(handle, inode, di_bh,
1742 OCFS2_JOURNAL_ACCESS_WRITE);
1743 if (ret) {
1744 mlog_errno(ret);
1745 goto out_commit;
1746 }
1747
Tao Ma08413892008-08-29 09:00:19 +08001748 di->i_xattr_loc = 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001749
1750 spin_lock(&oi->ip_lock);
1751 oi->ip_dyn_features &= ~(OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL);
1752 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1753 spin_unlock(&oi->ip_lock);
1754
1755 ret = ocfs2_journal_dirty(handle, di_bh);
1756 if (ret < 0)
1757 mlog_errno(ret);
1758out_commit:
1759 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
1760out:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001761 return ret;
1762}
1763
1764static int ocfs2_xattr_has_space_inline(struct inode *inode,
1765 struct ocfs2_dinode *di)
1766{
1767 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1768 unsigned int xattrsize = OCFS2_SB(inode->i_sb)->s_xattr_inline_size;
1769 int free;
1770
1771 if (xattrsize < OCFS2_MIN_XATTR_INLINE_SIZE)
1772 return 0;
1773
1774 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1775 struct ocfs2_inline_data *idata = &di->id2.i_data;
1776 free = le16_to_cpu(idata->id_count) - le64_to_cpu(di->i_size);
1777 } else if (ocfs2_inode_is_fast_symlink(inode)) {
1778 free = ocfs2_fast_symlink_chars(inode->i_sb) -
1779 le64_to_cpu(di->i_size);
1780 } else {
1781 struct ocfs2_extent_list *el = &di->id2.i_list;
1782 free = (le16_to_cpu(el->l_count) -
1783 le16_to_cpu(el->l_next_free_rec)) *
1784 sizeof(struct ocfs2_extent_rec);
1785 }
1786 if (free >= xattrsize)
1787 return 1;
1788
1789 return 0;
1790}
1791
1792/*
1793 * ocfs2_xattr_ibody_find()
1794 *
1795 * Find extended attribute in inode block and
1796 * fill search info into struct ocfs2_xattr_search.
1797 */
1798static int ocfs2_xattr_ibody_find(struct inode *inode,
1799 int name_index,
1800 const char *name,
1801 struct ocfs2_xattr_search *xs)
1802{
1803 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1804 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1805 int ret;
1806 int has_space = 0;
1807
1808 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1809 return 0;
1810
1811 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1812 down_read(&oi->ip_alloc_sem);
1813 has_space = ocfs2_xattr_has_space_inline(inode, di);
1814 up_read(&oi->ip_alloc_sem);
1815 if (!has_space)
1816 return 0;
1817 }
1818
1819 xs->xattr_bh = xs->inode_bh;
1820 xs->end = (void *)di + inode->i_sb->s_blocksize;
1821 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)
1822 xs->header = (struct ocfs2_xattr_header *)
1823 (xs->end - le16_to_cpu(di->i_xattr_inline_size));
1824 else
1825 xs->header = (struct ocfs2_xattr_header *)
1826 (xs->end - OCFS2_SB(inode->i_sb)->s_xattr_inline_size);
1827 xs->base = (void *)xs->header;
1828 xs->here = xs->header->xh_entries;
1829
1830 /* Find the named attribute. */
1831 if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) {
1832 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1833 if (ret && ret != -ENODATA)
1834 return ret;
1835 xs->not_found = ret;
1836 }
1837
1838 return 0;
1839}
1840
1841/*
1842 * ocfs2_xattr_ibody_set()
1843 *
1844 * Set, replace or remove an extended attribute into inode block.
1845 *
1846 */
1847static int ocfs2_xattr_ibody_set(struct inode *inode,
1848 struct ocfs2_xattr_info *xi,
1849 struct ocfs2_xattr_search *xs)
1850{
1851 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1852 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1853 int ret;
1854
1855 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
1856 return -ENOSPC;
1857
1858 down_write(&oi->ip_alloc_sem);
1859 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
1860 if (!ocfs2_xattr_has_space_inline(inode, di)) {
1861 ret = -ENOSPC;
1862 goto out;
1863 }
1864 }
1865
1866 ret = ocfs2_xattr_set_entry(inode, xi, xs,
1867 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL));
1868out:
1869 up_write(&oi->ip_alloc_sem);
1870
1871 return ret;
1872}
1873
1874/*
1875 * ocfs2_xattr_block_find()
1876 *
1877 * Find extended attribute in external block and
1878 * fill search info into struct ocfs2_xattr_search.
1879 */
1880static int ocfs2_xattr_block_find(struct inode *inode,
1881 int name_index,
1882 const char *name,
1883 struct ocfs2_xattr_search *xs)
1884{
1885 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1886 struct buffer_head *blk_bh = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08001887 struct ocfs2_xattr_block *xb;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001888 int ret = 0;
1889
1890 if (!di->i_xattr_loc)
1891 return ret;
1892
Joel Becker0fcaa56a2008-10-09 17:20:31 -07001893 ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001894 if (ret < 0) {
1895 mlog_errno(ret);
1896 return ret;
1897 }
Joel Beckerf6087fb2008-10-20 18:20:43 -07001898
1899 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
1900 if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) {
1901 ret = -EIO;
1902 goto cleanup;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001903 }
1904
1905 xs->xattr_bh = blk_bh;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001906
Tao Ma589dc262008-08-18 17:38:51 +08001907 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
1908 xs->header = &xb->xb_attrs.xb_header;
1909 xs->base = (void *)xs->header;
1910 xs->end = (void *)(blk_bh->b_data) + blk_bh->b_size;
1911 xs->here = xs->header->xh_entries;
1912
1913 ret = ocfs2_xattr_find_entry(name_index, name, xs);
1914 } else
1915 ret = ocfs2_xattr_index_block_find(inode, blk_bh,
1916 name_index,
1917 name, xs);
1918
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001919 if (ret && ret != -ENODATA) {
1920 xs->xattr_bh = NULL;
1921 goto cleanup;
1922 }
1923 xs->not_found = ret;
1924 return 0;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001925cleanup:
1926 brelse(blk_bh);
1927
1928 return ret;
1929}
1930
1931/*
1932 * ocfs2_xattr_block_set()
1933 *
1934 * Set, replace or remove an extended attribute into external block.
1935 *
1936 */
1937static int ocfs2_xattr_block_set(struct inode *inode,
1938 struct ocfs2_xattr_info *xi,
1939 struct ocfs2_xattr_search *xs)
1940{
1941 struct buffer_head *new_bh = NULL;
1942 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1943 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
1944 struct ocfs2_alloc_context *meta_ac = NULL;
1945 handle_t *handle = NULL;
1946 struct ocfs2_xattr_block *xblk = NULL;
1947 u16 suballoc_bit_start;
1948 u32 num_got;
1949 u64 first_blkno;
1950 int ret;
1951
1952 if (!xs->xattr_bh) {
1953 /*
1954 * Alloc one external block for extended attribute
1955 * outside of inode.
1956 */
1957 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
1958 if (ret < 0) {
1959 mlog_errno(ret);
1960 goto out;
1961 }
1962 handle = ocfs2_start_trans(osb,
1963 OCFS2_XATTR_BLOCK_CREATE_CREDITS);
1964 if (IS_ERR(handle)) {
1965 ret = PTR_ERR(handle);
1966 mlog_errno(ret);
1967 goto out;
1968 }
1969 ret = ocfs2_journal_access(handle, inode, xs->inode_bh,
1970 OCFS2_JOURNAL_ACCESS_CREATE);
1971 if (ret < 0) {
1972 mlog_errno(ret);
1973 goto out_commit;
1974 }
1975
1976 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
1977 &suballoc_bit_start, &num_got,
1978 &first_blkno);
1979 if (ret < 0) {
1980 mlog_errno(ret);
1981 goto out_commit;
1982 }
1983
1984 new_bh = sb_getblk(inode->i_sb, first_blkno);
1985 ocfs2_set_new_buffer_uptodate(inode, new_bh);
1986
1987 ret = ocfs2_journal_access(handle, inode, new_bh,
1988 OCFS2_JOURNAL_ACCESS_CREATE);
1989 if (ret < 0) {
1990 mlog_errno(ret);
1991 goto out_commit;
1992 }
1993
1994 /* Initialize ocfs2_xattr_block */
1995 xs->xattr_bh = new_bh;
1996 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
1997 memset(xblk, 0, inode->i_sb->s_blocksize);
1998 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
1999 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num);
2000 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2001 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2002 xblk->xb_blkno = cpu_to_le64(first_blkno);
2003
2004 xs->header = &xblk->xb_attrs.xb_header;
2005 xs->base = (void *)xs->header;
2006 xs->end = (void *)xblk + inode->i_sb->s_blocksize;
2007 xs->here = xs->header->xh_entries;
2008
2009
2010 ret = ocfs2_journal_dirty(handle, new_bh);
2011 if (ret < 0) {
2012 mlog_errno(ret);
2013 goto out_commit;
2014 }
2015 di->i_xattr_loc = cpu_to_le64(first_blkno);
2016 ret = ocfs2_journal_dirty(handle, xs->inode_bh);
2017 if (ret < 0)
2018 mlog_errno(ret);
2019out_commit:
2020 ocfs2_commit_trans(osb, handle);
2021out:
2022 if (meta_ac)
2023 ocfs2_free_alloc_context(meta_ac);
2024 if (ret < 0)
2025 return ret;
Tao Ma01225592008-08-18 17:38:53 +08002026 } else
2027 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2028
2029 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2030 /* Set extended attribute into external block */
2031 ret = ocfs2_xattr_set_entry(inode, xi, xs, OCFS2_HAS_XATTR_FL);
2032 if (!ret || ret != -ENOSPC)
2033 goto end;
2034
2035 ret = ocfs2_xattr_create_index_block(inode, xs);
2036 if (ret)
2037 goto end;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002038 }
2039
Tao Ma01225592008-08-18 17:38:53 +08002040 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs);
Tao Ma01225592008-08-18 17:38:53 +08002041
2042end:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002043
2044 return ret;
2045}
2046
2047/*
2048 * ocfs2_xattr_set()
2049 *
2050 * Set, replace or remove an extended attribute for this inode.
2051 * value is NULL to remove an existing extended attribute, else either
2052 * create or replace an extended attribute.
2053 */
2054int ocfs2_xattr_set(struct inode *inode,
2055 int name_index,
2056 const char *name,
2057 const void *value,
2058 size_t value_len,
2059 int flags)
2060{
2061 struct buffer_head *di_bh = NULL;
2062 struct ocfs2_dinode *di;
2063 int ret;
2064
2065 struct ocfs2_xattr_info xi = {
2066 .name_index = name_index,
2067 .name = name,
2068 .value = value,
2069 .value_len = value_len,
2070 };
2071
2072 struct ocfs2_xattr_search xis = {
2073 .not_found = -ENODATA,
2074 };
2075
2076 struct ocfs2_xattr_search xbs = {
2077 .not_found = -ENODATA,
2078 };
2079
Tiger Yang8154da32008-08-18 17:11:46 +08002080 if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb)))
2081 return -EOPNOTSUPP;
2082
Joel Beckerba937122008-10-24 19:13:20 -07002083 /*
2084 * Only xbs will be used on indexed trees. xis doesn't need a
2085 * bucket.
2086 */
2087 xbs.bucket = ocfs2_xattr_bucket_new(inode);
2088 if (!xbs.bucket) {
2089 mlog_errno(-ENOMEM);
2090 return -ENOMEM;
2091 }
2092
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002093 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2094 if (ret < 0) {
2095 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002096 goto cleanup_nolock;
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002097 }
2098 xis.inode_bh = xbs.inode_bh = di_bh;
2099 di = (struct ocfs2_dinode *)di_bh->b_data;
2100
2101 down_write(&OCFS2_I(inode)->ip_xattr_sem);
2102 /*
2103 * Scan inode and external block to find the same name
2104 * extended attribute and collect search infomation.
2105 */
2106 ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis);
2107 if (ret)
2108 goto cleanup;
2109 if (xis.not_found) {
2110 ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs);
2111 if (ret)
2112 goto cleanup;
2113 }
2114
2115 if (xis.not_found && xbs.not_found) {
2116 ret = -ENODATA;
2117 if (flags & XATTR_REPLACE)
2118 goto cleanup;
2119 ret = 0;
2120 if (!value)
2121 goto cleanup;
2122 } else {
2123 ret = -EEXIST;
2124 if (flags & XATTR_CREATE)
2125 goto cleanup;
2126 }
2127
2128 if (!value) {
2129 /* Remove existing extended attribute */
2130 if (!xis.not_found)
2131 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2132 else if (!xbs.not_found)
2133 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2134 } else {
2135 /* We always try to set extended attribute into inode first*/
2136 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2137 if (!ret && !xbs.not_found) {
2138 /*
2139 * If succeed and that extended attribute existing in
2140 * external block, then we will remove it.
2141 */
2142 xi.value = NULL;
2143 xi.value_len = 0;
2144 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2145 } else if (ret == -ENOSPC) {
2146 if (di->i_xattr_loc && !xbs.xattr_bh) {
2147 ret = ocfs2_xattr_block_find(inode, name_index,
2148 name, &xbs);
2149 if (ret)
2150 goto cleanup;
2151 }
2152 /*
2153 * If no space in inode, we will set extended attribute
2154 * into external block.
2155 */
2156 ret = ocfs2_xattr_block_set(inode, &xi, &xbs);
2157 if (ret)
2158 goto cleanup;
2159 if (!xis.not_found) {
2160 /*
2161 * If succeed and that extended attribute
2162 * existing in inode, we will remove it.
2163 */
2164 xi.value = NULL;
2165 xi.value_len = 0;
2166 ret = ocfs2_xattr_ibody_set(inode, &xi, &xis);
2167 }
2168 }
2169 }
2170cleanup:
2171 up_write(&OCFS2_I(inode)->ip_xattr_sem);
2172 ocfs2_inode_unlock(inode, 1);
Joel Beckerba937122008-10-24 19:13:20 -07002173cleanup_nolock:
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002174 brelse(di_bh);
2175 brelse(xbs.xattr_bh);
Joel Beckerba937122008-10-24 19:13:20 -07002176 ocfs2_xattr_bucket_free(xbs.bucket);
Tiger Yangcf1d6c72008-08-18 17:11:00 +08002177
2178 return ret;
2179}
2180
Tao Ma0c044f02008-08-18 17:38:50 +08002181/*
2182 * Find the xattr extent rec which may contains name_hash.
2183 * e_cpos will be the first name hash of the xattr rec.
2184 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
2185 */
2186static int ocfs2_xattr_get_rec(struct inode *inode,
2187 u32 name_hash,
2188 u64 *p_blkno,
2189 u32 *e_cpos,
2190 u32 *num_clusters,
2191 struct ocfs2_extent_list *el)
2192{
2193 int ret = 0, i;
2194 struct buffer_head *eb_bh = NULL;
2195 struct ocfs2_extent_block *eb;
2196 struct ocfs2_extent_rec *rec = NULL;
2197 u64 e_blkno = 0;
2198
2199 if (el->l_tree_depth) {
2200 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
2201 if (ret) {
2202 mlog_errno(ret);
2203 goto out;
2204 }
2205
2206 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2207 el = &eb->h_list;
2208
2209 if (el->l_tree_depth) {
2210 ocfs2_error(inode->i_sb,
2211 "Inode %lu has non zero tree depth in "
2212 "xattr tree block %llu\n", inode->i_ino,
2213 (unsigned long long)eb_bh->b_blocknr);
2214 ret = -EROFS;
2215 goto out;
2216 }
2217 }
2218
2219 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
2220 rec = &el->l_recs[i];
2221
2222 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
2223 e_blkno = le64_to_cpu(rec->e_blkno);
2224 break;
2225 }
2226 }
2227
2228 if (!e_blkno) {
2229 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
2230 "record (%u, %u, 0) in xattr", inode->i_ino,
2231 le32_to_cpu(rec->e_cpos),
2232 ocfs2_rec_clusters(el, rec));
2233 ret = -EROFS;
2234 goto out;
2235 }
2236
2237 *p_blkno = le64_to_cpu(rec->e_blkno);
2238 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2239 if (e_cpos)
2240 *e_cpos = le32_to_cpu(rec->e_cpos);
2241out:
2242 brelse(eb_bh);
2243 return ret;
2244}
2245
2246typedef int (xattr_bucket_func)(struct inode *inode,
2247 struct ocfs2_xattr_bucket *bucket,
2248 void *para);
2249
Tao Ma589dc262008-08-18 17:38:51 +08002250static int ocfs2_find_xe_in_bucket(struct inode *inode,
Joel Beckere2356a32008-10-27 15:01:54 -07002251 struct ocfs2_xattr_bucket *bucket,
Tao Ma589dc262008-08-18 17:38:51 +08002252 int name_index,
2253 const char *name,
2254 u32 name_hash,
2255 u16 *xe_index,
2256 int *found)
2257{
2258 int i, ret = 0, cmp = 1, block_off, new_offset;
Joel Beckere2356a32008-10-27 15:01:54 -07002259 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma589dc262008-08-18 17:38:51 +08002260 size_t name_len = strlen(name);
2261 struct ocfs2_xattr_entry *xe = NULL;
Tao Ma589dc262008-08-18 17:38:51 +08002262 char *xe_name;
2263
2264 /*
2265 * We don't use binary search in the bucket because there
2266 * may be multiple entries with the same name hash.
2267 */
2268 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
2269 xe = &xh->xh_entries[i];
2270
2271 if (name_hash > le32_to_cpu(xe->xe_name_hash))
2272 continue;
2273 else if (name_hash < le32_to_cpu(xe->xe_name_hash))
2274 break;
2275
2276 cmp = name_index - ocfs2_xattr_get_type(xe);
2277 if (!cmp)
2278 cmp = name_len - xe->xe_name_len;
2279 if (cmp)
2280 continue;
2281
2282 ret = ocfs2_xattr_bucket_get_name_value(inode,
2283 xh,
2284 i,
2285 &block_off,
2286 &new_offset);
2287 if (ret) {
2288 mlog_errno(ret);
2289 break;
2290 }
2291
Joel Beckere2356a32008-10-27 15:01:54 -07002292 xe_name = bucket_block(bucket, block_off) + new_offset;
2293 if (!memcmp(name, xe_name, name_len)) {
Tao Ma589dc262008-08-18 17:38:51 +08002294 *xe_index = i;
2295 *found = 1;
2296 ret = 0;
2297 break;
2298 }
2299 }
2300
2301 return ret;
2302}
2303
2304/*
2305 * Find the specified xattr entry in a series of buckets.
2306 * This series start from p_blkno and last for num_clusters.
2307 * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains
2308 * the num of the valid buckets.
2309 *
2310 * Return the buffer_head this xattr should reside in. And if the xattr's
2311 * hash is in the gap of 2 buckets, return the lower bucket.
2312 */
2313static int ocfs2_xattr_bucket_find(struct inode *inode,
2314 int name_index,
2315 const char *name,
2316 u32 name_hash,
2317 u64 p_blkno,
2318 u32 first_hash,
2319 u32 num_clusters,
2320 struct ocfs2_xattr_search *xs)
2321{
2322 int ret, found = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002323 struct ocfs2_xattr_header *xh = NULL;
2324 struct ocfs2_xattr_entry *xe = NULL;
2325 u16 index = 0;
2326 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2327 int low_bucket = 0, bucket, high_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002328 struct ocfs2_xattr_bucket *search;
Tao Ma589dc262008-08-18 17:38:51 +08002329 u32 last_hash;
Joel Beckere2356a32008-10-27 15:01:54 -07002330 u64 blkno, lower_blkno = 0;
Tao Ma589dc262008-08-18 17:38:51 +08002331
Joel Beckere2356a32008-10-27 15:01:54 -07002332 search = ocfs2_xattr_bucket_new(inode);
2333 if (!search) {
2334 ret = -ENOMEM;
2335 mlog_errno(ret);
2336 goto out;
2337 }
2338
2339 ret = ocfs2_read_xattr_bucket(search, p_blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002340 if (ret) {
2341 mlog_errno(ret);
2342 goto out;
2343 }
2344
Joel Beckere2356a32008-10-27 15:01:54 -07002345 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002346 high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1;
Tao Ma589dc262008-08-18 17:38:51 +08002347 while (low_bucket <= high_bucket) {
Joel Beckere2356a32008-10-27 15:01:54 -07002348 ocfs2_xattr_bucket_relse(search);
2349
Tao Ma589dc262008-08-18 17:38:51 +08002350 bucket = (low_bucket + high_bucket) / 2;
Tao Ma589dc262008-08-18 17:38:51 +08002351 blkno = p_blkno + bucket * blk_per_bucket;
Joel Beckere2356a32008-10-27 15:01:54 -07002352 ret = ocfs2_read_xattr_bucket(search, blkno);
Tao Ma589dc262008-08-18 17:38:51 +08002353 if (ret) {
2354 mlog_errno(ret);
2355 goto out;
2356 }
2357
Joel Beckere2356a32008-10-27 15:01:54 -07002358 xh = bucket_xh(search);
Tao Ma589dc262008-08-18 17:38:51 +08002359 xe = &xh->xh_entries[0];
2360 if (name_hash < le32_to_cpu(xe->xe_name_hash)) {
2361 high_bucket = bucket - 1;
2362 continue;
2363 }
2364
2365 /*
2366 * Check whether the hash of the last entry in our
Tao Ma5a0956112008-09-19 22:17:41 +08002367 * bucket is larger than the search one. for an empty
2368 * bucket, the last one is also the first one.
Tao Ma589dc262008-08-18 17:38:51 +08002369 */
Tao Ma5a0956112008-09-19 22:17:41 +08002370 if (xh->xh_count)
2371 xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1];
2372
Tao Ma589dc262008-08-18 17:38:51 +08002373 last_hash = le32_to_cpu(xe->xe_name_hash);
2374
Joel Beckere2356a32008-10-27 15:01:54 -07002375 /* record lower_blkno which may be the insert place. */
2376 lower_blkno = blkno;
Tao Ma589dc262008-08-18 17:38:51 +08002377
2378 if (name_hash > le32_to_cpu(xe->xe_name_hash)) {
2379 low_bucket = bucket + 1;
2380 continue;
2381 }
2382
2383 /* the searched xattr should reside in this bucket if exists. */
Joel Beckere2356a32008-10-27 15:01:54 -07002384 ret = ocfs2_find_xe_in_bucket(inode, search,
Tao Ma589dc262008-08-18 17:38:51 +08002385 name_index, name, name_hash,
2386 &index, &found);
2387 if (ret) {
2388 mlog_errno(ret);
2389 goto out;
2390 }
2391 break;
2392 }
2393
2394 /*
2395 * Record the bucket we have found.
2396 * When the xattr's hash value is in the gap of 2 buckets, we will
2397 * always set it to the previous bucket.
2398 */
Joel Beckere2356a32008-10-27 15:01:54 -07002399 if (!lower_blkno)
2400 lower_blkno = p_blkno;
2401
2402 /* This should be in cache - we just read it during the search */
2403 ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno);
2404 if (ret) {
2405 mlog_errno(ret);
2406 goto out;
Tao Ma589dc262008-08-18 17:38:51 +08002407 }
Tao Ma589dc262008-08-18 17:38:51 +08002408
Joel Beckerba937122008-10-24 19:13:20 -07002409 xs->header = bucket_xh(xs->bucket);
2410 xs->base = bucket_block(xs->bucket, 0);
Tao Ma589dc262008-08-18 17:38:51 +08002411 xs->end = xs->base + inode->i_sb->s_blocksize;
2412
2413 if (found) {
Tao Ma589dc262008-08-18 17:38:51 +08002414 xs->here = &xs->header->xh_entries[index];
2415 mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name,
Joel Beckerba937122008-10-24 19:13:20 -07002416 (unsigned long long)bucket_blkno(xs->bucket), index);
Tao Ma589dc262008-08-18 17:38:51 +08002417 } else
2418 ret = -ENODATA;
2419
2420out:
Joel Beckere2356a32008-10-27 15:01:54 -07002421 ocfs2_xattr_bucket_free(search);
Tao Ma589dc262008-08-18 17:38:51 +08002422 return ret;
2423}
2424
2425static int ocfs2_xattr_index_block_find(struct inode *inode,
2426 struct buffer_head *root_bh,
2427 int name_index,
2428 const char *name,
2429 struct ocfs2_xattr_search *xs)
2430{
2431 int ret;
2432 struct ocfs2_xattr_block *xb =
2433 (struct ocfs2_xattr_block *)root_bh->b_data;
2434 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
2435 struct ocfs2_extent_list *el = &xb_root->xt_list;
2436 u64 p_blkno = 0;
2437 u32 first_hash, num_clusters = 0;
Tao Ma2057e5c2008-10-09 23:06:13 +08002438 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
Tao Ma589dc262008-08-18 17:38:51 +08002439
2440 if (le16_to_cpu(el->l_next_free_rec) == 0)
2441 return -ENODATA;
2442
2443 mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n",
2444 name, name_hash, name_index);
2445
2446 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash,
2447 &num_clusters, el);
2448 if (ret) {
2449 mlog_errno(ret);
2450 goto out;
2451 }
2452
2453 BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash);
2454
2455 mlog(0, "find xattr extent rec %u clusters from %llu, the first hash "
Mark Fashehde29c082008-10-29 14:45:30 -07002456 "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno,
2457 first_hash);
Tao Ma589dc262008-08-18 17:38:51 +08002458
2459 ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash,
2460 p_blkno, first_hash, num_clusters, xs);
2461
2462out:
2463 return ret;
2464}
2465
Tao Ma0c044f02008-08-18 17:38:50 +08002466static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2467 u64 blkno,
2468 u32 clusters,
2469 xattr_bucket_func *func,
2470 void *para)
2471{
Joel Becker6dde41d2008-10-24 17:16:48 -07002472 int i, ret = 0;
Tao Ma0c044f02008-08-18 17:38:50 +08002473 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2474 u32 num_buckets = clusters * bpc;
Joel Beckerba937122008-10-24 19:13:20 -07002475 struct ocfs2_xattr_bucket *bucket;
Tao Ma0c044f02008-08-18 17:38:50 +08002476
Joel Beckerba937122008-10-24 19:13:20 -07002477 bucket = ocfs2_xattr_bucket_new(inode);
2478 if (!bucket) {
2479 mlog_errno(-ENOMEM);
2480 return -ENOMEM;
2481 }
Tao Ma0c044f02008-08-18 17:38:50 +08002482
2483 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07002484 clusters, (unsigned long long)blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002485
Joel Beckerba937122008-10-24 19:13:20 -07002486 for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) {
2487 ret = ocfs2_read_xattr_bucket(bucket, blkno);
Tao Ma0c044f02008-08-18 17:38:50 +08002488 if (ret) {
2489 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002490 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002491 }
2492
Tao Ma0c044f02008-08-18 17:38:50 +08002493 /*
2494 * The real bucket num in this series of blocks is stored
2495 * in the 1st bucket.
2496 */
2497 if (i == 0)
Joel Beckerba937122008-10-24 19:13:20 -07002498 num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets);
Tao Ma0c044f02008-08-18 17:38:50 +08002499
Mark Fashehde29c082008-10-29 14:45:30 -07002500 mlog(0, "iterating xattr bucket %llu, first hash %u\n",
2501 (unsigned long long)blkno,
Joel Beckerba937122008-10-24 19:13:20 -07002502 le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash));
Tao Ma0c044f02008-08-18 17:38:50 +08002503 if (func) {
Joel Beckerba937122008-10-24 19:13:20 -07002504 ret = func(inode, bucket, para);
2505 if (ret)
Tao Ma0c044f02008-08-18 17:38:50 +08002506 mlog_errno(ret);
Joel Beckerba937122008-10-24 19:13:20 -07002507 /* Fall through to bucket_relse() */
Tao Ma0c044f02008-08-18 17:38:50 +08002508 }
2509
Joel Beckerba937122008-10-24 19:13:20 -07002510 ocfs2_xattr_bucket_relse(bucket);
2511 if (ret)
2512 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002513 }
2514
Joel Beckerba937122008-10-24 19:13:20 -07002515 ocfs2_xattr_bucket_free(bucket);
Tao Ma0c044f02008-08-18 17:38:50 +08002516 return ret;
2517}
2518
2519struct ocfs2_xattr_tree_list {
2520 char *buffer;
2521 size_t buffer_size;
Tao Ma936b8832008-10-09 23:06:14 +08002522 size_t result;
Tao Ma0c044f02008-08-18 17:38:50 +08002523};
2524
2525static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2526 struct ocfs2_xattr_header *xh,
2527 int index,
2528 int *block_off,
2529 int *new_offset)
2530{
2531 u16 name_offset;
2532
2533 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2534 return -EINVAL;
2535
2536 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2537
2538 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2539 *new_offset = name_offset % inode->i_sb->s_blocksize;
2540
2541 return 0;
2542}
2543
2544static int ocfs2_list_xattr_bucket(struct inode *inode,
2545 struct ocfs2_xattr_bucket *bucket,
2546 void *para)
2547{
Tao Ma936b8832008-10-09 23:06:14 +08002548 int ret = 0, type;
Tao Ma0c044f02008-08-18 17:38:50 +08002549 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
Tao Ma0c044f02008-08-18 17:38:50 +08002550 int i, block_off, new_offset;
Tao Ma936b8832008-10-09 23:06:14 +08002551 const char *prefix, *name;
Tao Ma0c044f02008-08-18 17:38:50 +08002552
Joel Becker3e632942008-10-24 17:04:49 -07002553 for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) {
2554 struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i];
Tao Ma936b8832008-10-09 23:06:14 +08002555 type = ocfs2_xattr_get_type(entry);
2556 prefix = ocfs2_xattr_prefix(type);
Tao Ma0c044f02008-08-18 17:38:50 +08002557
Tao Ma936b8832008-10-09 23:06:14 +08002558 if (prefix) {
Tao Ma0c044f02008-08-18 17:38:50 +08002559 ret = ocfs2_xattr_bucket_get_name_value(inode,
Joel Becker3e632942008-10-24 17:04:49 -07002560 bucket_xh(bucket),
Tao Ma0c044f02008-08-18 17:38:50 +08002561 i,
2562 &block_off,
2563 &new_offset);
2564 if (ret)
2565 break;
Tao Ma936b8832008-10-09 23:06:14 +08002566
Joel Becker51def392008-10-24 16:57:21 -07002567 name = (const char *)bucket_block(bucket, block_off) +
Tao Ma936b8832008-10-09 23:06:14 +08002568 new_offset;
2569 ret = ocfs2_xattr_list_entry(xl->buffer,
2570 xl->buffer_size,
2571 &xl->result,
2572 prefix, name,
2573 entry->xe_name_len);
2574 if (ret)
2575 break;
Tao Ma0c044f02008-08-18 17:38:50 +08002576 }
2577 }
2578
2579 return ret;
2580}
2581
2582static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2583 struct ocfs2_xattr_tree_root *xt,
2584 char *buffer,
2585 size_t buffer_size)
2586{
2587 struct ocfs2_extent_list *el = &xt->xt_list;
2588 int ret = 0;
2589 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2590 u64 p_blkno = 0;
2591 struct ocfs2_xattr_tree_list xl = {
2592 .buffer = buffer,
2593 .buffer_size = buffer_size,
Tao Ma936b8832008-10-09 23:06:14 +08002594 .result = 0,
Tao Ma0c044f02008-08-18 17:38:50 +08002595 };
2596
2597 if (le16_to_cpu(el->l_next_free_rec) == 0)
2598 return 0;
2599
2600 while (name_hash > 0) {
2601 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2602 &e_cpos, &num_clusters, el);
2603 if (ret) {
2604 mlog_errno(ret);
2605 goto out;
2606 }
2607
2608 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2609 ocfs2_list_xattr_bucket,
2610 &xl);
2611 if (ret) {
2612 mlog_errno(ret);
2613 goto out;
2614 }
2615
2616 if (e_cpos == 0)
2617 break;
2618
2619 name_hash = e_cpos - 1;
2620 }
2621
Tao Ma936b8832008-10-09 23:06:14 +08002622 ret = xl.result;
Tao Ma0c044f02008-08-18 17:38:50 +08002623out:
2624 return ret;
2625}
Tao Ma01225592008-08-18 17:38:53 +08002626
2627static int cmp_xe(const void *a, const void *b)
2628{
2629 const struct ocfs2_xattr_entry *l = a, *r = b;
2630 u32 l_hash = le32_to_cpu(l->xe_name_hash);
2631 u32 r_hash = le32_to_cpu(r->xe_name_hash);
2632
2633 if (l_hash > r_hash)
2634 return 1;
2635 if (l_hash < r_hash)
2636 return -1;
2637 return 0;
2638}
2639
2640static void swap_xe(void *a, void *b, int size)
2641{
2642 struct ocfs2_xattr_entry *l = a, *r = b, tmp;
2643
2644 tmp = *l;
2645 memcpy(l, r, sizeof(struct ocfs2_xattr_entry));
2646 memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry));
2647}
2648
2649/*
2650 * When the ocfs2_xattr_block is filled up, new bucket will be created
2651 * and all the xattr entries will be moved to the new bucket.
Joel Becker178eeac2008-10-27 15:18:29 -07002652 * The header goes at the start of the bucket, and the names+values are
2653 * filled from the end. This is why *target starts as the last buffer.
Tao Ma01225592008-08-18 17:38:53 +08002654 * Note: we need to sort the entries since they are not saved in order
2655 * in the ocfs2_xattr_block.
2656 */
2657static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
2658 struct buffer_head *xb_bh,
Joel Becker178eeac2008-10-27 15:18:29 -07002659 struct ocfs2_xattr_bucket *bucket)
Tao Ma01225592008-08-18 17:38:53 +08002660{
2661 int i, blocksize = inode->i_sb->s_blocksize;
Joel Becker178eeac2008-10-27 15:18:29 -07002662 int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08002663 u16 offset, size, off_change;
2664 struct ocfs2_xattr_entry *xe;
2665 struct ocfs2_xattr_block *xb =
2666 (struct ocfs2_xattr_block *)xb_bh->b_data;
2667 struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07002668 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma01225592008-08-18 17:38:53 +08002669 u16 count = le16_to_cpu(xb_xh->xh_count);
Joel Becker178eeac2008-10-27 15:18:29 -07002670 char *src = xb_bh->b_data;
2671 char *target = bucket_block(bucket, blks - 1);
Tao Ma01225592008-08-18 17:38:53 +08002672
2673 mlog(0, "cp xattr from block %llu to bucket %llu\n",
2674 (unsigned long long)xb_bh->b_blocknr,
Joel Becker178eeac2008-10-27 15:18:29 -07002675 (unsigned long long)bucket_blkno(bucket));
Tao Ma01225592008-08-18 17:38:53 +08002676
Joel Becker178eeac2008-10-27 15:18:29 -07002677 for (i = 0; i < blks; i++)
2678 memset(bucket_block(bucket, i), 0, blocksize);
2679
Tao Ma01225592008-08-18 17:38:53 +08002680 /*
2681 * Since the xe_name_offset is based on ocfs2_xattr_header,
2682 * there is a offset change corresponding to the change of
2683 * ocfs2_xattr_header's position.
2684 */
2685 off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2686 xe = &xb_xh->xh_entries[count - 1];
2687 offset = le16_to_cpu(xe->xe_name_offset) + off_change;
2688 size = blocksize - offset;
2689
2690 /* copy all the names and values. */
Tao Ma01225592008-08-18 17:38:53 +08002691 memcpy(target + offset, src + offset, size);
2692
2693 /* Init new header now. */
2694 xh->xh_count = xb_xh->xh_count;
2695 xh->xh_num_buckets = cpu_to_le16(1);
2696 xh->xh_name_value_len = cpu_to_le16(size);
2697 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size);
2698
2699 /* copy all the entries. */
Joel Becker178eeac2008-10-27 15:18:29 -07002700 target = bucket_block(bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08002701 offset = offsetof(struct ocfs2_xattr_header, xh_entries);
2702 size = count * sizeof(struct ocfs2_xattr_entry);
2703 memcpy(target + offset, (char *)xb_xh + offset, size);
2704
2705 /* Change the xe offset for all the xe because of the move. */
2706 off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize +
2707 offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
2708 for (i = 0; i < count; i++)
2709 le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change);
2710
2711 mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n",
2712 offset, size, off_change);
2713
2714 sort(target + offset, count, sizeof(struct ocfs2_xattr_entry),
2715 cmp_xe, swap_xe);
2716}
2717
2718/*
2719 * After we move xattr from block to index btree, we have to
2720 * update ocfs2_xattr_search to the new xe and base.
2721 *
2722 * When the entry is in xattr block, xattr_bh indicates the storage place.
2723 * While if the entry is in index b-tree, "bucket" indicates the
2724 * real place of the xattr.
2725 */
Joel Becker178eeac2008-10-27 15:18:29 -07002726static void ocfs2_xattr_update_xattr_search(struct inode *inode,
2727 struct ocfs2_xattr_search *xs,
2728 struct buffer_head *old_bh)
Tao Ma01225592008-08-18 17:38:53 +08002729{
Tao Ma01225592008-08-18 17:38:53 +08002730 char *buf = old_bh->b_data;
2731 struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf;
2732 struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header;
Joel Becker178eeac2008-10-27 15:18:29 -07002733 int i;
Tao Ma01225592008-08-18 17:38:53 +08002734
Joel Beckerba937122008-10-24 19:13:20 -07002735 xs->header = bucket_xh(xs->bucket);
Joel Becker178eeac2008-10-27 15:18:29 -07002736 xs->base = bucket_block(xs->bucket, 0);
Tao Ma01225592008-08-18 17:38:53 +08002737 xs->end = xs->base + inode->i_sb->s_blocksize;
2738
Joel Becker178eeac2008-10-27 15:18:29 -07002739 if (xs->not_found)
2740 return;
Tao Ma01225592008-08-18 17:38:53 +08002741
Joel Becker178eeac2008-10-27 15:18:29 -07002742 i = xs->here - old_xh->xh_entries;
2743 xs->here = &xs->header->xh_entries[i];
Tao Ma01225592008-08-18 17:38:53 +08002744}
2745
2746static int ocfs2_xattr_create_index_block(struct inode *inode,
2747 struct ocfs2_xattr_search *xs)
2748{
2749 int ret, credits = OCFS2_SUBALLOC_ALLOC;
2750 u32 bit_off, len;
2751 u64 blkno;
2752 handle_t *handle;
2753 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2754 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2755 struct ocfs2_alloc_context *data_ac;
Tao Ma01225592008-08-18 17:38:53 +08002756 struct buffer_head *xb_bh = xs->xattr_bh;
2757 struct ocfs2_xattr_block *xb =
2758 (struct ocfs2_xattr_block *)xb_bh->b_data;
2759 struct ocfs2_xattr_tree_root *xr;
2760 u16 xb_flags = le16_to_cpu(xb->xb_flags);
Tao Ma01225592008-08-18 17:38:53 +08002761
2762 mlog(0, "create xattr index block for %llu\n",
2763 (unsigned long long)xb_bh->b_blocknr);
2764
2765 BUG_ON(xb_flags & OCFS2_XATTR_INDEXED);
Joel Becker178eeac2008-10-27 15:18:29 -07002766 BUG_ON(!xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08002767
2768 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
2769 if (ret) {
2770 mlog_errno(ret);
2771 goto out;
2772 }
2773
2774 /*
2775 * XXX:
2776 * We can use this lock for now, and maybe move to a dedicated mutex
2777 * if performance becomes a problem later.
2778 */
2779 down_write(&oi->ip_alloc_sem);
2780
2781 /*
Joel Becker178eeac2008-10-27 15:18:29 -07002782 * We need more credits. One for the xattr block update and one
2783 * for each block of the new xattr bucket.
Tao Ma01225592008-08-18 17:38:53 +08002784 */
Joel Becker178eeac2008-10-27 15:18:29 -07002785 credits += 1 + ocfs2_blocks_per_xattr_bucket(inode->i_sb);
Tao Ma01225592008-08-18 17:38:53 +08002786 handle = ocfs2_start_trans(osb, credits);
2787 if (IS_ERR(handle)) {
2788 ret = PTR_ERR(handle);
2789 mlog_errno(ret);
2790 goto out_sem;
2791 }
2792
2793 ret = ocfs2_journal_access(handle, inode, xb_bh,
2794 OCFS2_JOURNAL_ACCESS_WRITE);
2795 if (ret) {
2796 mlog_errno(ret);
2797 goto out_commit;
2798 }
2799
2800 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2801 if (ret) {
2802 mlog_errno(ret);
2803 goto out_commit;
2804 }
2805
2806 /*
2807 * The bucket may spread in many blocks, and
2808 * we will only touch the 1st block and the last block
2809 * in the whole bucket(one for entry and one for data).
2810 */
2811 blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
2812
Mark Fashehde29c082008-10-29 14:45:30 -07002813 mlog(0, "allocate 1 cluster from %llu to xattr block\n",
2814 (unsigned long long)blkno);
Tao Ma01225592008-08-18 17:38:53 +08002815
Joel Becker178eeac2008-10-27 15:18:29 -07002816 ret = ocfs2_init_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08002817 if (ret) {
2818 mlog_errno(ret);
2819 goto out_commit;
2820 }
2821
Joel Becker178eeac2008-10-27 15:18:29 -07002822 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
2823 OCFS2_JOURNAL_ACCESS_CREATE);
Joel Beckerbd60bd32008-10-20 18:25:56 -07002824 if (ret) {
2825 mlog_errno(ret);
2826 goto out_commit;
2827 }
Tao Ma01225592008-08-18 17:38:53 +08002828
Joel Becker178eeac2008-10-27 15:18:29 -07002829 ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket);
2830 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
2831
2832 ocfs2_xattr_update_xattr_search(inode, xs, xb_bh);
2833
Tao Ma01225592008-08-18 17:38:53 +08002834 /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */
2835 memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize -
2836 offsetof(struct ocfs2_xattr_block, xb_attrs));
2837
2838 xr = &xb->xb_attrs.xb_root;
2839 xr->xt_clusters = cpu_to_le32(1);
2840 xr->xt_last_eb_blk = 0;
2841 xr->xt_list.l_tree_depth = 0;
2842 xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb));
2843 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2844
2845 xr->xt_list.l_recs[0].e_cpos = 0;
2846 xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
2847 xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
2848
2849 xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED);
2850
2851 ret = ocfs2_journal_dirty(handle, xb_bh);
2852 if (ret) {
2853 mlog_errno(ret);
2854 goto out_commit;
2855 }
2856
2857out_commit:
2858 ocfs2_commit_trans(osb, handle);
2859
2860out_sem:
2861 up_write(&oi->ip_alloc_sem);
2862
2863out:
2864 if (data_ac)
2865 ocfs2_free_alloc_context(data_ac);
2866
Tao Ma01225592008-08-18 17:38:53 +08002867 return ret;
2868}
2869
2870static int cmp_xe_offset(const void *a, const void *b)
2871{
2872 const struct ocfs2_xattr_entry *l = a, *r = b;
2873 u32 l_name_offset = le16_to_cpu(l->xe_name_offset);
2874 u32 r_name_offset = le16_to_cpu(r->xe_name_offset);
2875
2876 if (l_name_offset < r_name_offset)
2877 return 1;
2878 if (l_name_offset > r_name_offset)
2879 return -1;
2880 return 0;
2881}
2882
2883/*
2884 * defrag a xattr bucket if we find that the bucket has some
2885 * holes beteen name/value pairs.
2886 * We will move all the name/value pairs to the end of the bucket
2887 * so that we can spare some space for insertion.
2888 */
2889static int ocfs2_defrag_xattr_bucket(struct inode *inode,
2890 struct ocfs2_xattr_bucket *bucket)
2891{
2892 int ret, i;
2893 size_t end, offset, len, value_len;
2894 struct ocfs2_xattr_header *xh;
2895 char *entries, *buf, *bucket_buf = NULL;
Joel Becker9c7759a2008-10-24 16:21:03 -07002896 u64 blkno = bucket_blkno(bucket);
Tao Ma01225592008-08-18 17:38:53 +08002897 u16 xh_free_start;
Tao Ma01225592008-08-18 17:38:53 +08002898 size_t blocksize = inode->i_sb->s_blocksize;
2899 handle_t *handle;
Tao Ma01225592008-08-18 17:38:53 +08002900 struct ocfs2_xattr_entry *xe;
Joel Becker161d6f32008-10-27 15:25:18 -07002901 struct ocfs2_xattr_bucket *wb = NULL;
Tao Ma01225592008-08-18 17:38:53 +08002902
2903 /*
2904 * In order to make the operation more efficient and generic,
2905 * we copy all the blocks into a contiguous memory and do the
2906 * defragment there, so if anything is error, we will not touch
2907 * the real block.
2908 */
2909 bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS);
2910 if (!bucket_buf) {
2911 ret = -EIO;
2912 goto out;
2913 }
2914
Joel Becker161d6f32008-10-27 15:25:18 -07002915 wb = ocfs2_xattr_bucket_new(inode);
2916 if (!wb) {
2917 ret = -ENOMEM;
2918 goto out;
2919 }
Tao Ma01225592008-08-18 17:38:53 +08002920
Joel Becker161d6f32008-10-27 15:25:18 -07002921 ret = ocfs2_read_xattr_bucket(wb, blkno);
2922 if (ret)
2923 goto out;
2924
2925 buf = bucket_buf;
2926 for (i = 0; i < wb->bu_blocks; i++, buf += blocksize)
2927 memcpy(buf, bucket_block(wb, i), blocksize);
2928
2929 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), wb->bu_blocks);
Tao Ma01225592008-08-18 17:38:53 +08002930 if (IS_ERR(handle)) {
2931 ret = PTR_ERR(handle);
2932 handle = NULL;
2933 mlog_errno(ret);
2934 goto out;
2935 }
2936
Joel Becker161d6f32008-10-27 15:25:18 -07002937 ret = ocfs2_xattr_bucket_journal_access(handle, wb,
2938 OCFS2_JOURNAL_ACCESS_WRITE);
2939 if (ret < 0) {
2940 mlog_errno(ret);
2941 goto commit;
Tao Ma01225592008-08-18 17:38:53 +08002942 }
2943
2944 xh = (struct ocfs2_xattr_header *)bucket_buf;
2945 entries = (char *)xh->xh_entries;
2946 xh_free_start = le16_to_cpu(xh->xh_free_start);
2947
2948 mlog(0, "adjust xattr bucket in %llu, count = %u, "
2949 "xh_free_start = %u, xh_name_value_len = %u.\n",
Mark Fashehde29c082008-10-29 14:45:30 -07002950 (unsigned long long)blkno, le16_to_cpu(xh->xh_count),
2951 xh_free_start, le16_to_cpu(xh->xh_name_value_len));
Tao Ma01225592008-08-18 17:38:53 +08002952
2953 /*
2954 * sort all the entries by their offset.
2955 * the largest will be the first, so that we can
2956 * move them to the end one by one.
2957 */
2958 sort(entries, le16_to_cpu(xh->xh_count),
2959 sizeof(struct ocfs2_xattr_entry),
2960 cmp_xe_offset, swap_xe);
2961
2962 /* Move all name/values to the end of the bucket. */
2963 xe = xh->xh_entries;
2964 end = OCFS2_XATTR_BUCKET_SIZE;
2965 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
2966 offset = le16_to_cpu(xe->xe_name_offset);
2967 if (ocfs2_xattr_is_local(xe))
2968 value_len = OCFS2_XATTR_SIZE(
2969 le64_to_cpu(xe->xe_value_size));
2970 else
2971 value_len = OCFS2_XATTR_ROOT_SIZE;
2972 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
2973
2974 /*
2975 * We must make sure that the name/value pair
2976 * exist in the same block. So adjust end to
2977 * the previous block end if needed.
2978 */
2979 if (((end - len) / blocksize !=
2980 (end - 1) / blocksize))
2981 end = end - end % blocksize;
2982
2983 if (end > offset + len) {
2984 memmove(bucket_buf + end - len,
2985 bucket_buf + offset, len);
2986 xe->xe_name_offset = cpu_to_le16(end - len);
2987 }
2988
2989 mlog_bug_on_msg(end < offset + len, "Defrag check failed for "
2990 "bucket %llu\n", (unsigned long long)blkno);
2991
2992 end -= len;
2993 }
2994
2995 mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for "
2996 "bucket %llu\n", (unsigned long long)blkno);
2997
2998 if (xh_free_start == end)
2999 goto commit;
3000
3001 memset(bucket_buf + xh_free_start, 0, end - xh_free_start);
3002 xh->xh_free_start = cpu_to_le16(end);
3003
3004 /* sort the entries by their name_hash. */
3005 sort(entries, le16_to_cpu(xh->xh_count),
3006 sizeof(struct ocfs2_xattr_entry),
3007 cmp_xe, swap_xe);
3008
3009 buf = bucket_buf;
Joel Becker161d6f32008-10-27 15:25:18 -07003010 for (i = 0; i < wb->bu_blocks; i++, buf += blocksize)
3011 memcpy(bucket_block(wb, i), buf, blocksize);
3012 ocfs2_xattr_bucket_journal_dirty(handle, wb);
Tao Ma01225592008-08-18 17:38:53 +08003013
3014commit:
3015 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3016out:
Joel Becker161d6f32008-10-27 15:25:18 -07003017 ocfs2_xattr_bucket_free(wb);
Tao Ma01225592008-08-18 17:38:53 +08003018 kfree(bucket_buf);
3019 return ret;
3020}
3021
3022/*
3023 * Move half nums of the xattr bucket in the previous cluster to this new
3024 * cluster. We only touch the last cluster of the previous extend record.
3025 *
3026 * first_bh is the first buffer_head of a series of bucket in the same
3027 * extent rec and header_bh is the header of one bucket in this cluster.
3028 * They will be updated if we move the data header_bh contains to the new
3029 * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster.
3030 */
3031static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode,
3032 handle_t *handle,
3033 struct buffer_head **first_bh,
3034 struct buffer_head **header_bh,
3035 u64 new_blkno,
3036 u64 prev_blkno,
3037 u32 num_clusters,
3038 u32 *first_hash)
3039{
3040 int i, ret, credits;
3041 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3042 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3043 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3044 int blocksize = inode->i_sb->s_blocksize;
3045 struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL;
3046 struct ocfs2_xattr_header *new_xh;
3047 struct ocfs2_xattr_header *xh =
3048 (struct ocfs2_xattr_header *)((*first_bh)->b_data);
3049
3050 BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets);
3051 BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize);
3052
3053 prev_bh = *first_bh;
3054 get_bh(prev_bh);
3055 xh = (struct ocfs2_xattr_header *)prev_bh->b_data;
3056
3057 prev_blkno += (num_clusters - 1) * bpc + bpc / 2;
3058
3059 mlog(0, "move half of xattrs in cluster %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003060 (unsigned long long)prev_blkno, (unsigned long long)new_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003061
3062 /*
3063 * We need to update the 1st half of the new cluster and
3064 * 1 more for the update of the 1st bucket of the previous
3065 * extent record.
3066 */
3067 credits = bpc / 2 + 1;
3068 ret = ocfs2_extend_trans(handle, credits);
3069 if (ret) {
3070 mlog_errno(ret);
3071 goto out;
3072 }
3073
3074 ret = ocfs2_journal_access(handle, inode, prev_bh,
3075 OCFS2_JOURNAL_ACCESS_WRITE);
3076 if (ret) {
3077 mlog_errno(ret);
3078 goto out;
3079 }
3080
3081 for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) {
3082 old_bh = new_bh = NULL;
3083 new_bh = sb_getblk(inode->i_sb, new_blkno);
3084 if (!new_bh) {
3085 ret = -EIO;
3086 mlog_errno(ret);
3087 goto out;
3088 }
3089
3090 ocfs2_set_new_buffer_uptodate(inode, new_bh);
3091
3092 ret = ocfs2_journal_access(handle, inode, new_bh,
3093 OCFS2_JOURNAL_ACCESS_CREATE);
3094 if (ret < 0) {
3095 mlog_errno(ret);
3096 brelse(new_bh);
3097 goto out;
3098 }
3099
Joel Becker0fcaa56a2008-10-09 17:20:31 -07003100 ret = ocfs2_read_block(inode, prev_blkno, &old_bh);
Tao Ma01225592008-08-18 17:38:53 +08003101 if (ret < 0) {
3102 mlog_errno(ret);
3103 brelse(new_bh);
3104 goto out;
3105 }
3106
3107 memcpy(new_bh->b_data, old_bh->b_data, blocksize);
3108
3109 if (i == 0) {
3110 new_xh = (struct ocfs2_xattr_header *)new_bh->b_data;
3111 new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2);
3112
3113 if (first_hash)
3114 *first_hash = le32_to_cpu(
3115 new_xh->xh_entries[0].xe_name_hash);
3116 new_first_bh = new_bh;
3117 get_bh(new_first_bh);
3118 }
3119
3120 ocfs2_journal_dirty(handle, new_bh);
3121
3122 if (*header_bh == old_bh) {
3123 brelse(*header_bh);
3124 *header_bh = new_bh;
3125 get_bh(*header_bh);
3126
3127 brelse(*first_bh);
3128 *first_bh = new_first_bh;
3129 get_bh(*first_bh);
3130 }
3131 brelse(new_bh);
3132 brelse(old_bh);
3133 }
3134
3135 le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2));
3136
3137 ocfs2_journal_dirty(handle, prev_bh);
3138out:
3139 brelse(prev_bh);
3140 brelse(new_first_bh);
3141 return ret;
3142}
3143
Tao Ma01225592008-08-18 17:38:53 +08003144/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003145 * Find the suitable pos when we divide a bucket into 2.
3146 * We have to make sure the xattrs with the same hash value exist
3147 * in the same bucket.
3148 *
3149 * If this ocfs2_xattr_header covers more than one hash value, find a
3150 * place where the hash value changes. Try to find the most even split.
3151 * The most common case is that all entries have different hash values,
3152 * and the first check we make will find a place to split.
Tao Ma01225592008-08-18 17:38:53 +08003153 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003154static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh)
3155{
3156 struct ocfs2_xattr_entry *entries = xh->xh_entries;
3157 int count = le16_to_cpu(xh->xh_count);
3158 int delta, middle = count / 2;
3159
3160 /*
3161 * We start at the middle. Each step gets farther away in both
3162 * directions. We therefore hit the change in hash value
3163 * nearest to the middle. Note that this loop does not execute for
3164 * count < 2.
3165 */
3166 for (delta = 0; delta < middle; delta++) {
3167 /* Let's check delta earlier than middle */
3168 if (cmp_xe(&entries[middle - delta - 1],
3169 &entries[middle - delta]))
3170 return middle - delta;
3171
3172 /* For even counts, don't walk off the end */
3173 if ((middle + delta + 1) == count)
3174 continue;
3175
3176 /* Now try delta past middle */
3177 if (cmp_xe(&entries[middle + delta],
3178 &entries[middle + delta + 1]))
3179 return middle + delta + 1;
3180 }
3181
3182 /* Every entry had the same hash */
3183 return count;
3184}
3185
3186/*
3187 * Move some xattrs in old bucket(blk) to new bucket(new_blk).
3188 * first_hash will record the 1st hash of the new bucket.
3189 *
3190 * Normally half of the xattrs will be moved. But we have to make
3191 * sure that the xattrs with the same hash value are stored in the
3192 * same bucket. If all the xattrs in this bucket have the same hash
3193 * value, the new bucket will be initialized as an empty one and the
3194 * first_hash will be initialized as (hash_value+1).
3195 */
3196static int ocfs2_divide_xattr_bucket(struct inode *inode,
3197 handle_t *handle,
3198 u64 blk,
3199 u64 new_blk,
3200 u32 *first_hash,
3201 int new_bucket_head)
Tao Ma01225592008-08-18 17:38:53 +08003202{
3203 int ret, i;
Tao Ma80bcaf32008-10-27 06:06:24 +08003204 int count, start, len, name_value_len = 0, xe_len, name_offset = 0;
Joel Beckerba937122008-10-24 19:13:20 -07003205 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003206 struct ocfs2_xattr_header *xh;
3207 struct ocfs2_xattr_entry *xe;
3208 int blocksize = inode->i_sb->s_blocksize;
3209
Tao Ma80bcaf32008-10-27 06:06:24 +08003210 mlog(0, "move some of xattrs from bucket %llu to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003211 (unsigned long long)blk, (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003212
Joel Beckerba937122008-10-24 19:13:20 -07003213 s_bucket = ocfs2_xattr_bucket_new(inode);
3214 t_bucket = ocfs2_xattr_bucket_new(inode);
3215 if (!s_bucket || !t_bucket) {
3216 ret = -ENOMEM;
3217 mlog_errno(ret);
3218 goto out;
3219 }
Tao Ma01225592008-08-18 17:38:53 +08003220
Joel Beckerba937122008-10-24 19:13:20 -07003221 ret = ocfs2_read_xattr_bucket(s_bucket, blk);
Tao Ma01225592008-08-18 17:38:53 +08003222 if (ret) {
3223 mlog_errno(ret);
3224 goto out;
3225 }
3226
Joel Beckerba937122008-10-24 19:13:20 -07003227 ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003228 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08003229 if (ret) {
3230 mlog_errno(ret);
3231 goto out;
3232 }
3233
Joel Becker784b8162008-10-24 17:33:40 -07003234 /*
3235 * Even if !new_bucket_head, we're overwriting t_bucket. Thus,
3236 * there's no need to read it.
3237 */
Joel Beckerba937122008-10-24 19:13:20 -07003238 ret = ocfs2_init_xattr_bucket(t_bucket, new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003239 if (ret) {
3240 mlog_errno(ret);
3241 goto out;
3242 }
3243
Joel Beckerba937122008-10-24 19:13:20 -07003244 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003245 new_bucket_head ?
3246 OCFS2_JOURNAL_ACCESS_CREATE :
3247 OCFS2_JOURNAL_ACCESS_WRITE);
3248 if (ret) {
3249 mlog_errno(ret);
3250 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003251 }
3252
Joel Beckerba937122008-10-24 19:13:20 -07003253 xh = bucket_xh(s_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003254 count = le16_to_cpu(xh->xh_count);
3255 start = ocfs2_xattr_find_divide_pos(xh);
3256
3257 if (start == count) {
3258 xe = &xh->xh_entries[start-1];
3259
3260 /*
3261 * initialized a new empty bucket here.
3262 * The hash value is set as one larger than
3263 * that of the last entry in the previous bucket.
3264 */
Joel Beckerba937122008-10-24 19:13:20 -07003265 for (i = 0; i < t_bucket->bu_blocks; i++)
3266 memset(bucket_block(t_bucket, i), 0, blocksize);
Tao Ma80bcaf32008-10-27 06:06:24 +08003267
Joel Beckerba937122008-10-24 19:13:20 -07003268 xh = bucket_xh(t_bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08003269 xh->xh_free_start = cpu_to_le16(blocksize);
3270 xh->xh_entries[0].xe_name_hash = xe->xe_name_hash;
3271 le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1);
3272
3273 goto set_num_buckets;
3274 }
3275
Tao Ma01225592008-08-18 17:38:53 +08003276 /* copy the whole bucket to the new first. */
Joel Beckerba937122008-10-24 19:13:20 -07003277 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003278
3279 /* update the new bucket. */
Joel Beckerba937122008-10-24 19:13:20 -07003280 xh = bucket_xh(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003281
3282 /*
3283 * Calculate the total name/value len and xh_free_start for
3284 * the old bucket first.
3285 */
3286 name_offset = OCFS2_XATTR_BUCKET_SIZE;
3287 name_value_len = 0;
3288 for (i = 0; i < start; i++) {
3289 xe = &xh->xh_entries[i];
3290 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3291 if (ocfs2_xattr_is_local(xe))
3292 xe_len +=
3293 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3294 else
3295 xe_len += OCFS2_XATTR_ROOT_SIZE;
3296 name_value_len += xe_len;
3297 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
3298 name_offset = le16_to_cpu(xe->xe_name_offset);
3299 }
3300
3301 /*
3302 * Now begin the modification to the new bucket.
3303 *
3304 * In the new bucket, We just move the xattr entry to the beginning
3305 * and don't touch the name/value. So there will be some holes in the
3306 * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is
3307 * called.
3308 */
3309 xe = &xh->xh_entries[start];
3310 len = sizeof(struct ocfs2_xattr_entry) * (count - start);
3311 mlog(0, "mv xattr entry len %d from %d to %d\n", len,
Mark Fashehff1ec202008-08-19 10:54:29 -07003312 (int)((char *)xe - (char *)xh),
3313 (int)((char *)xh->xh_entries - (char *)xh));
Tao Ma01225592008-08-18 17:38:53 +08003314 memmove((char *)xh->xh_entries, (char *)xe, len);
3315 xe = &xh->xh_entries[count - start];
3316 len = sizeof(struct ocfs2_xattr_entry) * start;
3317 memset((char *)xe, 0, len);
3318
3319 le16_add_cpu(&xh->xh_count, -start);
3320 le16_add_cpu(&xh->xh_name_value_len, -name_value_len);
3321
3322 /* Calculate xh_free_start for the new bucket. */
3323 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
3324 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
3325 xe = &xh->xh_entries[i];
3326 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
3327 if (ocfs2_xattr_is_local(xe))
3328 xe_len +=
3329 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3330 else
3331 xe_len += OCFS2_XATTR_ROOT_SIZE;
3332 if (le16_to_cpu(xe->xe_name_offset) <
3333 le16_to_cpu(xh->xh_free_start))
3334 xh->xh_free_start = xe->xe_name_offset;
3335 }
3336
Tao Ma80bcaf32008-10-27 06:06:24 +08003337set_num_buckets:
Tao Ma01225592008-08-18 17:38:53 +08003338 /* set xh->xh_num_buckets for the new xh. */
3339 if (new_bucket_head)
3340 xh->xh_num_buckets = cpu_to_le16(1);
3341 else
3342 xh->xh_num_buckets = 0;
3343
Joel Beckerba937122008-10-24 19:13:20 -07003344 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003345
3346 /* store the first_hash of the new bucket. */
3347 if (first_hash)
3348 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3349
3350 /*
Tao Ma80bcaf32008-10-27 06:06:24 +08003351 * Now only update the 1st block of the old bucket. If we
3352 * just added a new empty bucket, there is no need to modify
3353 * it.
Tao Ma01225592008-08-18 17:38:53 +08003354 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003355 if (start == count)
3356 goto out;
3357
Joel Beckerba937122008-10-24 19:13:20 -07003358 xh = bucket_xh(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003359 memset(&xh->xh_entries[start], 0,
3360 sizeof(struct ocfs2_xattr_entry) * (count - start));
3361 xh->xh_count = cpu_to_le16(start);
3362 xh->xh_free_start = cpu_to_le16(name_offset);
3363 xh->xh_name_value_len = cpu_to_le16(name_value_len);
3364
Joel Beckerba937122008-10-24 19:13:20 -07003365 ocfs2_xattr_bucket_journal_dirty(handle, s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003366
3367out:
Joel Beckerba937122008-10-24 19:13:20 -07003368 ocfs2_xattr_bucket_free(s_bucket);
3369 ocfs2_xattr_bucket_free(t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003370
3371 return ret;
3372}
3373
3374/*
3375 * Copy xattr from one bucket to another bucket.
3376 *
3377 * The caller must make sure that the journal transaction
3378 * has enough space for journaling.
3379 */
3380static int ocfs2_cp_xattr_bucket(struct inode *inode,
3381 handle_t *handle,
3382 u64 s_blkno,
3383 u64 t_blkno,
3384 int t_is_new)
3385{
Joel Becker4980c6d2008-10-24 18:54:43 -07003386 int ret;
Joel Beckerba937122008-10-24 19:13:20 -07003387 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
Tao Ma01225592008-08-18 17:38:53 +08003388
3389 BUG_ON(s_blkno == t_blkno);
3390
3391 mlog(0, "cp bucket %llu to %llu, target is %d\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003392 (unsigned long long)s_blkno, (unsigned long long)t_blkno,
3393 t_is_new);
Tao Ma01225592008-08-18 17:38:53 +08003394
Joel Beckerba937122008-10-24 19:13:20 -07003395 s_bucket = ocfs2_xattr_bucket_new(inode);
3396 t_bucket = ocfs2_xattr_bucket_new(inode);
3397 if (!s_bucket || !t_bucket) {
3398 ret = -ENOMEM;
3399 mlog_errno(ret);
3400 goto out;
3401 }
3402
3403 ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003404 if (ret)
3405 goto out;
3406
Joel Becker784b8162008-10-24 17:33:40 -07003407 /*
3408 * Even if !t_is_new, we're overwriting t_bucket. Thus,
3409 * there's no need to read it.
3410 */
Joel Beckerba937122008-10-24 19:13:20 -07003411 ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003412 if (ret)
3413 goto out;
3414
Joel Beckerba937122008-10-24 19:13:20 -07003415 ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket,
Joel Becker1224be02008-10-24 18:47:33 -07003416 t_is_new ?
3417 OCFS2_JOURNAL_ACCESS_CREATE :
3418 OCFS2_JOURNAL_ACCESS_WRITE);
3419 if (ret)
3420 goto out;
Tao Ma01225592008-08-18 17:38:53 +08003421
Joel Beckerba937122008-10-24 19:13:20 -07003422 ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket);
3423 ocfs2_xattr_bucket_journal_dirty(handle, t_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003424
3425out:
Joel Beckerba937122008-10-24 19:13:20 -07003426 ocfs2_xattr_bucket_free(t_bucket);
3427 ocfs2_xattr_bucket_free(s_bucket);
Tao Ma01225592008-08-18 17:38:53 +08003428
3429 return ret;
3430}
3431
3432/*
3433 * Copy one xattr cluster from src_blk to to_blk.
3434 * The to_blk will become the first bucket header of the cluster, so its
3435 * xh_num_buckets will be initialized as the bucket num in the cluster.
3436 */
3437static int ocfs2_cp_xattr_cluster(struct inode *inode,
3438 handle_t *handle,
3439 struct buffer_head *first_bh,
3440 u64 src_blk,
3441 u64 to_blk,
3442 u32 *first_hash)
3443{
3444 int i, ret, credits;
3445 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3446 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3447 int num_buckets = ocfs2_xattr_buckets_per_cluster(osb);
3448 struct buffer_head *bh = NULL;
3449 struct ocfs2_xattr_header *xh;
3450 u64 to_blk_start = to_blk;
3451
Mark Fashehde29c082008-10-29 14:45:30 -07003452 mlog(0, "cp xattrs from cluster %llu to %llu\n",
3453 (unsigned long long)src_blk, (unsigned long long)to_blk);
Tao Ma01225592008-08-18 17:38:53 +08003454
3455 /*
3456 * We need to update the new cluster and 1 more for the update of
3457 * the 1st bucket of the previous extent rec.
3458 */
3459 credits = bpc + 1;
3460 ret = ocfs2_extend_trans(handle, credits);
3461 if (ret) {
3462 mlog_errno(ret);
3463 goto out;
3464 }
3465
3466 ret = ocfs2_journal_access(handle, inode, first_bh,
3467 OCFS2_JOURNAL_ACCESS_WRITE);
3468 if (ret) {
3469 mlog_errno(ret);
3470 goto out;
3471 }
3472
3473 for (i = 0; i < num_buckets; i++) {
3474 ret = ocfs2_cp_xattr_bucket(inode, handle,
3475 src_blk, to_blk, 1);
3476 if (ret) {
3477 mlog_errno(ret);
3478 goto out;
3479 }
3480
3481 src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3482 to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3483 }
3484
3485 /* update the old bucket header. */
3486 xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3487 le16_add_cpu(&xh->xh_num_buckets, -num_buckets);
3488
3489 ocfs2_journal_dirty(handle, first_bh);
3490
3491 /* update the new bucket header. */
Joel Becker0fcaa56a2008-10-09 17:20:31 -07003492 ret = ocfs2_read_block(inode, to_blk_start, &bh);
Tao Ma01225592008-08-18 17:38:53 +08003493 if (ret < 0) {
3494 mlog_errno(ret);
3495 goto out;
3496 }
3497
3498 ret = ocfs2_journal_access(handle, inode, bh,
3499 OCFS2_JOURNAL_ACCESS_WRITE);
3500 if (ret) {
3501 mlog_errno(ret);
3502 goto out;
3503 }
3504
3505 xh = (struct ocfs2_xattr_header *)bh->b_data;
3506 xh->xh_num_buckets = cpu_to_le16(num_buckets);
3507
3508 ocfs2_journal_dirty(handle, bh);
3509
3510 if (first_hash)
3511 *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3512out:
3513 brelse(bh);
3514 return ret;
3515}
3516
3517/*
Tao Ma80bcaf32008-10-27 06:06:24 +08003518 * Move some xattrs in this cluster to the new cluster.
Tao Ma01225592008-08-18 17:38:53 +08003519 * This function should only be called when bucket size == cluster size.
3520 * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead.
3521 */
Tao Ma80bcaf32008-10-27 06:06:24 +08003522static int ocfs2_divide_xattr_cluster(struct inode *inode,
3523 handle_t *handle,
3524 u64 prev_blk,
3525 u64 new_blk,
3526 u32 *first_hash)
Tao Ma01225592008-08-18 17:38:53 +08003527{
3528 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3529 int ret, credits = 2 * blk_per_bucket;
3530
3531 BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize);
3532
3533 ret = ocfs2_extend_trans(handle, credits);
3534 if (ret) {
3535 mlog_errno(ret);
3536 return ret;
3537 }
3538
3539 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08003540 return ocfs2_divide_xattr_bucket(inode, handle, prev_blk,
3541 new_blk, first_hash, 1);
Tao Ma01225592008-08-18 17:38:53 +08003542}
3543
3544/*
3545 * Move some xattrs from the old cluster to the new one since they are not
3546 * contiguous in ocfs2 xattr tree.
3547 *
3548 * new_blk starts a new separate cluster, and we will move some xattrs from
3549 * prev_blk to it. v_start will be set as the first name hash value in this
3550 * new cluster so that it can be used as e_cpos during tree insertion and
3551 * don't collide with our original b-tree operations. first_bh and header_bh
3552 * will also be updated since they will be used in ocfs2_extend_xattr_bucket
3553 * to extend the insert bucket.
3554 *
3555 * The problem is how much xattr should we move to the new one and when should
3556 * we update first_bh and header_bh?
3557 * 1. If cluster size > bucket size, that means the previous cluster has more
3558 * than 1 bucket, so just move half nums of bucket into the new cluster and
3559 * update the first_bh and header_bh if the insert bucket has been moved
3560 * to the new cluster.
3561 * 2. If cluster_size == bucket_size:
3562 * a) If the previous extent rec has more than one cluster and the insert
3563 * place isn't in the last cluster, copy the entire last cluster to the
3564 * new one. This time, we don't need to upate the first_bh and header_bh
3565 * since they will not be moved into the new cluster.
3566 * b) Otherwise, move the bottom half of the xattrs in the last cluster into
3567 * the new one. And we set the extend flag to zero if the insert place is
3568 * moved into the new allocated cluster since no extend is needed.
3569 */
3570static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode,
3571 handle_t *handle,
3572 struct buffer_head **first_bh,
3573 struct buffer_head **header_bh,
3574 u64 new_blk,
3575 u64 prev_blk,
3576 u32 prev_clusters,
3577 u32 *v_start,
3578 int *extend)
3579{
3580 int ret = 0;
3581 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3582
3583 mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003584 (unsigned long long)prev_blk, prev_clusters,
3585 (unsigned long long)new_blk);
Tao Ma01225592008-08-18 17:38:53 +08003586
3587 if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1)
3588 ret = ocfs2_mv_xattr_bucket_cross_cluster(inode,
3589 handle,
3590 first_bh,
3591 header_bh,
3592 new_blk,
3593 prev_blk,
3594 prev_clusters,
3595 v_start);
3596 else {
3597 u64 last_blk = prev_blk + bpc * (prev_clusters - 1);
3598
3599 if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk)
3600 ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh,
3601 last_blk, new_blk,
3602 v_start);
3603 else {
Tao Ma80bcaf32008-10-27 06:06:24 +08003604 ret = ocfs2_divide_xattr_cluster(inode, handle,
3605 last_blk, new_blk,
3606 v_start);
Tao Ma01225592008-08-18 17:38:53 +08003607
3608 if ((*header_bh)->b_blocknr == last_blk && extend)
3609 *extend = 0;
3610 }
3611 }
3612
3613 return ret;
3614}
3615
3616/*
3617 * Add a new cluster for xattr storage.
3618 *
3619 * If the new cluster is contiguous with the previous one, it will be
3620 * appended to the same extent record, and num_clusters will be updated.
3621 * If not, we will insert a new extent for it and move some xattrs in
3622 * the last cluster into the new allocated one.
3623 * We also need to limit the maximum size of a btree leaf, otherwise we'll
3624 * lose the benefits of hashing because we'll have to search large leaves.
3625 * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize,
3626 * if it's bigger).
3627 *
3628 * first_bh is the first block of the previous extent rec and header_bh
3629 * indicates the bucket we will insert the new xattrs. They will be updated
3630 * when the header_bh is moved into the new cluster.
3631 */
3632static int ocfs2_add_new_xattr_cluster(struct inode *inode,
3633 struct buffer_head *root_bh,
3634 struct buffer_head **first_bh,
3635 struct buffer_head **header_bh,
3636 u32 *num_clusters,
3637 u32 prev_cpos,
3638 u64 prev_blkno,
3639 int *extend)
3640{
3641 int ret, credits;
3642 u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
3643 u32 prev_clusters = *num_clusters;
3644 u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0;
3645 u64 block;
3646 handle_t *handle = NULL;
3647 struct ocfs2_alloc_context *data_ac = NULL;
3648 struct ocfs2_alloc_context *meta_ac = NULL;
3649 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003650 struct ocfs2_extent_tree et;
Tao Ma01225592008-08-18 17:38:53 +08003651
3652 mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, "
3653 "previous xattr blkno = %llu\n",
3654 (unsigned long long)OCFS2_I(inode)->ip_blkno,
Mark Fashehde29c082008-10-29 14:45:30 -07003655 prev_cpos, (unsigned long long)prev_blkno);
Tao Ma01225592008-08-18 17:38:53 +08003656
Joel Becker8d6220d2008-08-22 12:46:09 -07003657 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003658
3659 ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
3660 &data_ac, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08003661 if (ret) {
3662 mlog_errno(ret);
3663 goto leave;
3664 }
3665
Joel Beckerf99b9b72008-08-20 19:36:33 -07003666 credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el,
3667 clusters_to_add);
Tao Ma01225592008-08-18 17:38:53 +08003668 handle = ocfs2_start_trans(osb, credits);
3669 if (IS_ERR(handle)) {
3670 ret = PTR_ERR(handle);
3671 handle = NULL;
3672 mlog_errno(ret);
3673 goto leave;
3674 }
3675
3676 ret = ocfs2_journal_access(handle, inode, root_bh,
3677 OCFS2_JOURNAL_ACCESS_WRITE);
3678 if (ret < 0) {
3679 mlog_errno(ret);
3680 goto leave;
3681 }
3682
3683 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
3684 clusters_to_add, &bit_off, &num_bits);
3685 if (ret < 0) {
3686 if (ret != -ENOSPC)
3687 mlog_errno(ret);
3688 goto leave;
3689 }
3690
3691 BUG_ON(num_bits > clusters_to_add);
3692
3693 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
3694 mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n",
3695 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
3696
3697 if (prev_blkno + prev_clusters * bpc == block &&
3698 (prev_clusters + num_bits) << osb->s_clustersize_bits <=
3699 OCFS2_MAX_XATTR_TREE_LEAF_SIZE) {
3700 /*
3701 * If this cluster is contiguous with the old one and
3702 * adding this new cluster, we don't surpass the limit of
3703 * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be
3704 * initialized and used like other buckets in the previous
3705 * cluster.
3706 * So add it as a contiguous one. The caller will handle
3707 * its init process.
3708 */
3709 v_start = prev_cpos + prev_clusters;
3710 *num_clusters = prev_clusters + num_bits;
3711 mlog(0, "Add contiguous %u clusters to previous extent rec.\n",
3712 num_bits);
3713 } else {
3714 ret = ocfs2_adjust_xattr_cross_cluster(inode,
3715 handle,
3716 first_bh,
3717 header_bh,
3718 block,
3719 prev_blkno,
3720 prev_clusters,
3721 &v_start,
3722 extend);
3723 if (ret) {
3724 mlog_errno(ret);
3725 goto leave;
3726 }
3727 }
3728
Tao Ma28b8ca02008-09-01 08:45:18 +08003729 if (handle->h_buffer_credits < credits) {
3730 /*
3731 * The journal has been restarted before, and don't
3732 * have enough space for the insertion, so extend it
3733 * here.
3734 */
3735 ret = ocfs2_extend_trans(handle, credits);
3736 if (ret) {
3737 mlog_errno(ret);
3738 goto leave;
3739 }
3740 }
Tao Ma01225592008-08-18 17:38:53 +08003741 mlog(0, "Insert %u clusters at block %llu for xattr at %u\n",
Mark Fashehde29c082008-10-29 14:45:30 -07003742 num_bits, (unsigned long long)block, v_start);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003743 ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block,
3744 num_bits, 0, meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08003745 if (ret < 0) {
3746 mlog_errno(ret);
3747 goto leave;
3748 }
3749
3750 ret = ocfs2_journal_dirty(handle, root_bh);
3751 if (ret < 0) {
3752 mlog_errno(ret);
3753 goto leave;
3754 }
3755
3756leave:
3757 if (handle)
3758 ocfs2_commit_trans(osb, handle);
3759 if (data_ac)
3760 ocfs2_free_alloc_context(data_ac);
3761 if (meta_ac)
3762 ocfs2_free_alloc_context(meta_ac);
3763
3764 return ret;
3765}
3766
3767/*
3768 * Extend a new xattr bucket and move xattrs to the end one by one until
3769 * We meet with start_bh. Only move half of the xattrs to the bucket after it.
3770 */
3771static int ocfs2_extend_xattr_bucket(struct inode *inode,
3772 struct buffer_head *first_bh,
3773 struct buffer_head *start_bh,
3774 u32 num_clusters)
3775{
3776 int ret, credits;
3777 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3778 u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
3779 u64 start_blk = start_bh->b_blocknr, end_blk;
3780 u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb);
3781 handle_t *handle;
3782 struct ocfs2_xattr_header *first_xh =
3783 (struct ocfs2_xattr_header *)first_bh->b_data;
3784 u16 bucket = le16_to_cpu(first_xh->xh_num_buckets);
3785
3786 mlog(0, "extend xattr bucket in %llu, xattr extend rec starting "
Mark Fashehde29c082008-10-29 14:45:30 -07003787 "from %llu, len = %u\n", (unsigned long long)start_blk,
Tao Ma01225592008-08-18 17:38:53 +08003788 (unsigned long long)first_bh->b_blocknr, num_clusters);
3789
3790 BUG_ON(bucket >= num_buckets);
3791
3792 end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket;
3793
3794 /*
3795 * We will touch all the buckets after the start_bh(include it).
Joel Becker1224be02008-10-24 18:47:33 -07003796 * Then we add one more bucket.
Tao Ma01225592008-08-18 17:38:53 +08003797 */
Joel Becker1224be02008-10-24 18:47:33 -07003798 credits = end_blk - start_blk + 3 * blk_per_bucket + 1;
Tao Ma01225592008-08-18 17:38:53 +08003799 handle = ocfs2_start_trans(osb, credits);
3800 if (IS_ERR(handle)) {
3801 ret = PTR_ERR(handle);
3802 handle = NULL;
3803 mlog_errno(ret);
3804 goto out;
3805 }
3806
3807 ret = ocfs2_journal_access(handle, inode, first_bh,
3808 OCFS2_JOURNAL_ACCESS_WRITE);
3809 if (ret) {
3810 mlog_errno(ret);
3811 goto commit;
3812 }
3813
3814 while (end_blk != start_blk) {
3815 ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk,
3816 end_blk + blk_per_bucket, 0);
3817 if (ret)
3818 goto commit;
3819 end_blk -= blk_per_bucket;
3820 }
3821
3822 /* Move half of the xattr in start_blk to the next bucket. */
Tao Ma80bcaf32008-10-27 06:06:24 +08003823 ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk,
3824 start_blk + blk_per_bucket, NULL, 0);
Tao Ma01225592008-08-18 17:38:53 +08003825
3826 le16_add_cpu(&first_xh->xh_num_buckets, 1);
3827 ocfs2_journal_dirty(handle, first_bh);
3828
3829commit:
3830 ocfs2_commit_trans(osb, handle);
3831out:
3832 return ret;
3833}
3834
3835/*
3836 * Add new xattr bucket in an extent record and adjust the buckets accordingly.
3837 * xb_bh is the ocfs2_xattr_block.
3838 * We will move all the buckets starting from header_bh to the next place. As
3839 * for this one, half num of its xattrs will be moved to the next one.
3840 *
3841 * We will allocate a new cluster if current cluster is full and adjust
3842 * header_bh and first_bh if the insert place is moved to the new cluster.
3843 */
3844static int ocfs2_add_new_xattr_bucket(struct inode *inode,
3845 struct buffer_head *xb_bh,
3846 struct buffer_head *header_bh)
3847{
3848 struct ocfs2_xattr_header *first_xh = NULL;
3849 struct buffer_head *first_bh = NULL;
3850 struct ocfs2_xattr_block *xb =
3851 (struct ocfs2_xattr_block *)xb_bh->b_data;
3852 struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root;
3853 struct ocfs2_extent_list *el = &xb_root->xt_list;
3854 struct ocfs2_xattr_header *xh =
3855 (struct ocfs2_xattr_header *)header_bh->b_data;
3856 u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash);
3857 struct super_block *sb = inode->i_sb;
3858 struct ocfs2_super *osb = OCFS2_SB(sb);
3859 int ret, num_buckets, extend = 1;
3860 u64 p_blkno;
3861 u32 e_cpos, num_clusters;
3862
3863 mlog(0, "Add new xattr bucket starting form %llu\n",
3864 (unsigned long long)header_bh->b_blocknr);
3865
3866 /*
3867 * Add refrence for header_bh here because it may be
3868 * changed in ocfs2_add_new_xattr_cluster and we need
3869 * to free it in the end.
3870 */
3871 get_bh(header_bh);
3872
3873 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos,
3874 &num_clusters, el);
3875 if (ret) {
3876 mlog_errno(ret);
3877 goto out;
3878 }
3879
Joel Becker0fcaa56a2008-10-09 17:20:31 -07003880 ret = ocfs2_read_block(inode, p_blkno, &first_bh);
Tao Ma01225592008-08-18 17:38:53 +08003881 if (ret) {
3882 mlog_errno(ret);
3883 goto out;
3884 }
3885
3886 num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters;
3887 first_xh = (struct ocfs2_xattr_header *)first_bh->b_data;
3888
3889 if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) {
3890 ret = ocfs2_add_new_xattr_cluster(inode,
3891 xb_bh,
3892 &first_bh,
3893 &header_bh,
3894 &num_clusters,
3895 e_cpos,
3896 p_blkno,
3897 &extend);
3898 if (ret) {
3899 mlog_errno(ret);
3900 goto out;
3901 }
3902 }
3903
3904 if (extend)
3905 ret = ocfs2_extend_xattr_bucket(inode,
3906 first_bh,
3907 header_bh,
3908 num_clusters);
3909 if (ret)
3910 mlog_errno(ret);
3911out:
3912 brelse(first_bh);
3913 brelse(header_bh);
3914 return ret;
3915}
3916
3917static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
3918 struct ocfs2_xattr_bucket *bucket,
3919 int offs)
3920{
3921 int block_off = offs >> inode->i_sb->s_blocksize_bits;
3922
3923 offs = offs % inode->i_sb->s_blocksize;
Joel Becker51def392008-10-24 16:57:21 -07003924 return bucket_block(bucket, block_off) + offs;
Tao Ma01225592008-08-18 17:38:53 +08003925}
3926
3927/*
3928 * Handle the normal xattr set, including replace, delete and new.
Tao Ma01225592008-08-18 17:38:53 +08003929 *
3930 * Note: "local" indicates the real data's locality. So we can't
3931 * just its bucket locality by its length.
3932 */
3933static void ocfs2_xattr_set_entry_normal(struct inode *inode,
3934 struct ocfs2_xattr_info *xi,
3935 struct ocfs2_xattr_search *xs,
3936 u32 name_hash,
Tao Ma5a0956112008-09-19 22:17:41 +08003937 int local)
Tao Ma01225592008-08-18 17:38:53 +08003938{
3939 struct ocfs2_xattr_entry *last, *xe;
3940 int name_len = strlen(xi->name);
3941 struct ocfs2_xattr_header *xh = xs->header;
3942 u16 count = le16_to_cpu(xh->xh_count), start;
3943 size_t blocksize = inode->i_sb->s_blocksize;
3944 char *val;
3945 size_t offs, size, new_size;
3946
3947 last = &xh->xh_entries[count];
3948 if (!xs->not_found) {
3949 xe = xs->here;
3950 offs = le16_to_cpu(xe->xe_name_offset);
3951 if (ocfs2_xattr_is_local(xe))
3952 size = OCFS2_XATTR_SIZE(name_len) +
3953 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
3954 else
3955 size = OCFS2_XATTR_SIZE(name_len) +
3956 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
3957
3958 /*
3959 * If the new value will be stored outside, xi->value has been
3960 * initalized as an empty ocfs2_xattr_value_root, and the same
3961 * goes with xi->value_len, so we can set new_size safely here.
3962 * See ocfs2_xattr_set_in_bucket.
3963 */
3964 new_size = OCFS2_XATTR_SIZE(name_len) +
3965 OCFS2_XATTR_SIZE(xi->value_len);
3966
3967 le16_add_cpu(&xh->xh_name_value_len, -size);
3968 if (xi->value) {
3969 if (new_size > size)
3970 goto set_new_name_value;
3971
3972 /* Now replace the old value with new one. */
3973 if (local)
3974 xe->xe_value_size = cpu_to_le64(xi->value_len);
3975 else
3976 xe->xe_value_size = 0;
3977
3978 val = ocfs2_xattr_bucket_get_val(inode,
Joel Beckerba937122008-10-24 19:13:20 -07003979 xs->bucket, offs);
Tao Ma01225592008-08-18 17:38:53 +08003980 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
3981 size - OCFS2_XATTR_SIZE(name_len));
3982 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
3983 memcpy(val + OCFS2_XATTR_SIZE(name_len),
3984 xi->value, xi->value_len);
3985
3986 le16_add_cpu(&xh->xh_name_value_len, new_size);
3987 ocfs2_xattr_set_local(xe, local);
3988 return;
3989 } else {
Tao Ma5a0956112008-09-19 22:17:41 +08003990 /*
3991 * Remove the old entry if there is more than one.
3992 * We don't remove the last entry so that we can
3993 * use it to indicate the hash value of the empty
3994 * bucket.
3995 */
Tao Ma01225592008-08-18 17:38:53 +08003996 last -= 1;
Tao Ma01225592008-08-18 17:38:53 +08003997 le16_add_cpu(&xh->xh_count, -1);
Tao Ma5a0956112008-09-19 22:17:41 +08003998 if (xh->xh_count) {
3999 memmove(xe, xe + 1,
4000 (void *)last - (void *)xe);
4001 memset(last, 0,
4002 sizeof(struct ocfs2_xattr_entry));
4003 } else
4004 xh->xh_free_start =
4005 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4006
Tao Ma01225592008-08-18 17:38:53 +08004007 return;
4008 }
4009 } else {
4010 /* find a new entry for insert. */
4011 int low = 0, high = count - 1, tmp;
4012 struct ocfs2_xattr_entry *tmp_xe;
4013
Tao Ma5a0956112008-09-19 22:17:41 +08004014 while (low <= high && count) {
Tao Ma01225592008-08-18 17:38:53 +08004015 tmp = (low + high) / 2;
4016 tmp_xe = &xh->xh_entries[tmp];
4017
4018 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4019 low = tmp + 1;
4020 else if (name_hash <
4021 le32_to_cpu(tmp_xe->xe_name_hash))
4022 high = tmp - 1;
Tao Ma06b240d2008-09-19 22:16:34 +08004023 else {
4024 low = tmp;
Tao Ma01225592008-08-18 17:38:53 +08004025 break;
Tao Ma06b240d2008-09-19 22:16:34 +08004026 }
Tao Ma01225592008-08-18 17:38:53 +08004027 }
4028
4029 xe = &xh->xh_entries[low];
4030 if (low != count)
4031 memmove(xe + 1, xe, (void *)last - (void *)xe);
4032
4033 le16_add_cpu(&xh->xh_count, 1);
4034 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4035 xe->xe_name_hash = cpu_to_le32(name_hash);
4036 xe->xe_name_len = name_len;
4037 ocfs2_xattr_set_type(xe, xi->name_index);
4038 }
4039
4040set_new_name_value:
4041 /* Insert the new name+value. */
4042 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4043
4044 /*
4045 * We must make sure that the name/value pair
4046 * exists in the same block.
4047 */
4048 offs = le16_to_cpu(xh->xh_free_start);
4049 start = offs - size;
4050
4051 if (start >> inode->i_sb->s_blocksize_bits !=
4052 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4053 offs = offs - offs % blocksize;
4054 xh->xh_free_start = cpu_to_le16(offs);
4055 }
4056
Joel Beckerba937122008-10-24 19:13:20 -07004057 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
Tao Ma01225592008-08-18 17:38:53 +08004058 xe->xe_name_offset = cpu_to_le16(offs - size);
4059
4060 memset(val, 0, size);
4061 memcpy(val, xi->name, name_len);
4062 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4063
4064 xe->xe_value_size = cpu_to_le64(xi->value_len);
4065 ocfs2_xattr_set_local(xe, local);
4066 xs->here = xe;
4067 le16_add_cpu(&xh->xh_free_start, -size);
4068 le16_add_cpu(&xh->xh_name_value_len, size);
4069
4070 return;
4071}
4072
Tao Ma01225592008-08-18 17:38:53 +08004073/*
4074 * Set the xattr entry in the specified bucket.
4075 * The bucket is indicated by xs->bucket and it should have the enough
4076 * space for the xattr insertion.
4077 */
4078static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4079 struct ocfs2_xattr_info *xi,
4080 struct ocfs2_xattr_search *xs,
4081 u32 name_hash,
Tao Ma5a0956112008-09-19 22:17:41 +08004082 int local)
Tao Ma01225592008-08-18 17:38:53 +08004083{
Joel Becker1224be02008-10-24 18:47:33 -07004084 int ret;
Tao Ma01225592008-08-18 17:38:53 +08004085 handle_t *handle = NULL;
Tao Ma01225592008-08-18 17:38:53 +08004086 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Joel Becker02dbf382008-10-27 18:07:45 -07004087 u64 blkno;
Tao Ma01225592008-08-18 17:38:53 +08004088
Mark Fashehff1ec202008-08-19 10:54:29 -07004089 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4090 (unsigned long)xi->value_len, xi->name_index,
Joel Beckerba937122008-10-24 19:13:20 -07004091 (unsigned long long)bucket_blkno(xs->bucket));
Tao Ma01225592008-08-18 17:38:53 +08004092
Joel Beckerba937122008-10-24 19:13:20 -07004093 if (!xs->bucket->bu_bhs[1]) {
Joel Becker02dbf382008-10-27 18:07:45 -07004094 blkno = bucket_blkno(xs->bucket);
4095 ocfs2_xattr_bucket_relse(xs->bucket);
4096 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
Tao Ma01225592008-08-18 17:38:53 +08004097 if (ret) {
4098 mlog_errno(ret);
4099 goto out;
4100 }
4101 }
4102
Joel Becker02dbf382008-10-27 18:07:45 -07004103 handle = ocfs2_start_trans(osb, xs->bucket->bu_blocks);
Tao Ma01225592008-08-18 17:38:53 +08004104 if (IS_ERR(handle)) {
4105 ret = PTR_ERR(handle);
4106 handle = NULL;
4107 mlog_errno(ret);
4108 goto out;
4109 }
4110
Joel Beckerba937122008-10-24 19:13:20 -07004111 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004112 OCFS2_JOURNAL_ACCESS_WRITE);
4113 if (ret < 0) {
4114 mlog_errno(ret);
4115 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004116 }
4117
Tao Ma5a0956112008-09-19 22:17:41 +08004118 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
Joel Beckerba937122008-10-24 19:13:20 -07004119 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004120
Tao Ma01225592008-08-18 17:38:53 +08004121out:
4122 ocfs2_commit_trans(osb, handle);
4123
4124 return ret;
4125}
4126
4127static int ocfs2_xattr_value_update_size(struct inode *inode,
4128 struct buffer_head *xe_bh,
4129 struct ocfs2_xattr_entry *xe,
4130 u64 new_size)
4131{
4132 int ret;
4133 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4134 handle_t *handle = NULL;
4135
4136 handle = ocfs2_start_trans(osb, 1);
Tao Mad3264792008-10-24 07:57:28 +08004137 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004138 ret = -ENOMEM;
4139 mlog_errno(ret);
4140 goto out;
4141 }
4142
4143 ret = ocfs2_journal_access(handle, inode, xe_bh,
4144 OCFS2_JOURNAL_ACCESS_WRITE);
4145 if (ret < 0) {
4146 mlog_errno(ret);
4147 goto out_commit;
4148 }
4149
4150 xe->xe_value_size = cpu_to_le64(new_size);
4151
4152 ret = ocfs2_journal_dirty(handle, xe_bh);
4153 if (ret < 0)
4154 mlog_errno(ret);
4155
4156out_commit:
4157 ocfs2_commit_trans(osb, handle);
4158out:
4159 return ret;
4160}
4161
4162/*
4163 * Truncate the specified xe_off entry in xattr bucket.
4164 * bucket is indicated by header_bh and len is the new length.
4165 * Both the ocfs2_xattr_value_root and the entry will be updated here.
4166 *
4167 * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed.
4168 */
4169static int ocfs2_xattr_bucket_value_truncate(struct inode *inode,
4170 struct buffer_head *header_bh,
4171 int xe_off,
4172 int len)
4173{
4174 int ret, offset;
4175 u64 value_blk;
4176 struct buffer_head *value_bh = NULL;
4177 struct ocfs2_xattr_value_root *xv;
4178 struct ocfs2_xattr_entry *xe;
4179 struct ocfs2_xattr_header *xh =
4180 (struct ocfs2_xattr_header *)header_bh->b_data;
4181 size_t blocksize = inode->i_sb->s_blocksize;
4182
4183 xe = &xh->xh_entries[xe_off];
4184
4185 BUG_ON(!xe || ocfs2_xattr_is_local(xe));
4186
4187 offset = le16_to_cpu(xe->xe_name_offset) +
4188 OCFS2_XATTR_SIZE(xe->xe_name_len);
4189
4190 value_blk = offset / blocksize;
4191
4192 /* We don't allow ocfs2_xattr_value to be stored in different block. */
4193 BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize);
4194 value_blk += header_bh->b_blocknr;
4195
Joel Becker0fcaa56a2008-10-09 17:20:31 -07004196 ret = ocfs2_read_block(inode, value_blk, &value_bh);
Tao Ma01225592008-08-18 17:38:53 +08004197 if (ret) {
4198 mlog_errno(ret);
4199 goto out;
4200 }
4201
4202 xv = (struct ocfs2_xattr_value_root *)
4203 (value_bh->b_data + offset % blocksize);
4204
4205 mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n",
4206 xe_off, (unsigned long long)header_bh->b_blocknr, len);
4207 ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len);
4208 if (ret) {
4209 mlog_errno(ret);
4210 goto out;
4211 }
4212
4213 ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len);
4214 if (ret) {
4215 mlog_errno(ret);
4216 goto out;
4217 }
4218
4219out:
4220 brelse(value_bh);
4221 return ret;
4222}
4223
4224static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
4225 struct ocfs2_xattr_search *xs,
4226 int len)
4227{
4228 int ret, offset;
4229 struct ocfs2_xattr_entry *xe = xs->here;
4230 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
4231
Joel Beckerba937122008-10-24 19:13:20 -07004232 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
Tao Ma01225592008-08-18 17:38:53 +08004233
4234 offset = xe - xh->xh_entries;
Joel Beckerba937122008-10-24 19:13:20 -07004235 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0],
Tao Ma01225592008-08-18 17:38:53 +08004236 offset, len);
4237 if (ret)
4238 mlog_errno(ret);
4239
4240 return ret;
4241}
4242
4243static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
4244 struct ocfs2_xattr_search *xs,
4245 char *val,
4246 int value_len)
4247{
4248 int offset;
4249 struct ocfs2_xattr_value_root *xv;
4250 struct ocfs2_xattr_entry *xe = xs->here;
4251
4252 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
4253
4254 offset = le16_to_cpu(xe->xe_name_offset) +
4255 OCFS2_XATTR_SIZE(xe->xe_name_len);
4256
4257 xv = (struct ocfs2_xattr_value_root *)(xs->base + offset);
4258
4259 return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len);
4260}
4261
Tao Ma01225592008-08-18 17:38:53 +08004262static int ocfs2_rm_xattr_cluster(struct inode *inode,
4263 struct buffer_head *root_bh,
4264 u64 blkno,
4265 u32 cpos,
4266 u32 len)
4267{
4268 int ret;
4269 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4270 struct inode *tl_inode = osb->osb_tl_inode;
4271 handle_t *handle;
4272 struct ocfs2_xattr_block *xb =
4273 (struct ocfs2_xattr_block *)root_bh->b_data;
Tao Ma01225592008-08-18 17:38:53 +08004274 struct ocfs2_alloc_context *meta_ac = NULL;
4275 struct ocfs2_cached_dealloc_ctxt dealloc;
Joel Beckerf99b9b72008-08-20 19:36:33 -07004276 struct ocfs2_extent_tree et;
4277
Joel Becker8d6220d2008-08-22 12:46:09 -07004278 ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Ma01225592008-08-18 17:38:53 +08004279
4280 ocfs2_init_dealloc_ctxt(&dealloc);
4281
4282 mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n",
4283 cpos, len, (unsigned long long)blkno);
4284
4285 ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len);
4286
Joel Beckerf99b9b72008-08-20 19:36:33 -07004287 ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
Tao Ma01225592008-08-18 17:38:53 +08004288 if (ret) {
4289 mlog_errno(ret);
4290 return ret;
4291 }
4292
4293 mutex_lock(&tl_inode->i_mutex);
4294
4295 if (ocfs2_truncate_log_needs_flush(osb)) {
4296 ret = __ocfs2_flush_truncate_log(osb);
4297 if (ret < 0) {
4298 mlog_errno(ret);
4299 goto out;
4300 }
4301 }
4302
4303 handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
Tao Mad3264792008-10-24 07:57:28 +08004304 if (IS_ERR(handle)) {
Tao Ma01225592008-08-18 17:38:53 +08004305 ret = -ENOMEM;
4306 mlog_errno(ret);
4307 goto out;
4308 }
4309
4310 ret = ocfs2_journal_access(handle, inode, root_bh,
4311 OCFS2_JOURNAL_ACCESS_WRITE);
4312 if (ret) {
4313 mlog_errno(ret);
4314 goto out_commit;
4315 }
4316
Joel Beckerf99b9b72008-08-20 19:36:33 -07004317 ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
4318 &dealloc);
Tao Ma01225592008-08-18 17:38:53 +08004319 if (ret) {
4320 mlog_errno(ret);
4321 goto out_commit;
4322 }
4323
4324 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len);
4325
4326 ret = ocfs2_journal_dirty(handle, root_bh);
4327 if (ret) {
4328 mlog_errno(ret);
4329 goto out_commit;
4330 }
4331
4332 ret = ocfs2_truncate_log_append(osb, handle, blkno, len);
4333 if (ret)
4334 mlog_errno(ret);
4335
4336out_commit:
4337 ocfs2_commit_trans(osb, handle);
4338out:
4339 ocfs2_schedule_truncate_log_flush(osb, 1);
4340
4341 mutex_unlock(&tl_inode->i_mutex);
4342
4343 if (meta_ac)
4344 ocfs2_free_alloc_context(meta_ac);
4345
4346 ocfs2_run_deallocs(osb, &dealloc);
4347
4348 return ret;
4349}
4350
Tao Ma01225592008-08-18 17:38:53 +08004351static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
4352 struct ocfs2_xattr_search *xs)
4353{
4354 handle_t *handle = NULL;
Joel Beckerba937122008-10-24 19:13:20 -07004355 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004356 struct ocfs2_xattr_entry *last = &xh->xh_entries[
4357 le16_to_cpu(xh->xh_count) - 1];
4358 int ret = 0;
4359
Joel Becker1224be02008-10-24 18:47:33 -07004360 handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)),
4361 ocfs2_blocks_per_xattr_bucket(inode->i_sb));
Tao Ma01225592008-08-18 17:38:53 +08004362 if (IS_ERR(handle)) {
4363 ret = PTR_ERR(handle);
4364 mlog_errno(ret);
4365 return;
4366 }
4367
Joel Beckerba937122008-10-24 19:13:20 -07004368 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
Joel Becker1224be02008-10-24 18:47:33 -07004369 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma01225592008-08-18 17:38:53 +08004370 if (ret) {
4371 mlog_errno(ret);
4372 goto out_commit;
4373 }
4374
4375 /* Remove the old entry. */
4376 memmove(xs->here, xs->here + 1,
4377 (void *)last - (void *)xs->here);
4378 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
4379 le16_add_cpu(&xh->xh_count, -1);
4380
Joel Beckerba937122008-10-24 19:13:20 -07004381 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
Joel Becker1224be02008-10-24 18:47:33 -07004382
Tao Ma01225592008-08-18 17:38:53 +08004383out_commit:
4384 ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
4385}
4386
4387/*
4388 * Set the xattr name/value in the bucket specified in xs.
4389 *
4390 * As the new value in xi may be stored in the bucket or in an outside cluster,
4391 * we divide the whole process into 3 steps:
4392 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
4393 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
4394 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
4395 * 4. If the clusters for the new outside value can't be allocated, we need
4396 * to free the xattr we allocated in set.
4397 */
4398static int ocfs2_xattr_set_in_bucket(struct inode *inode,
4399 struct ocfs2_xattr_info *xi,
4400 struct ocfs2_xattr_search *xs)
4401{
Tao Ma5a0956112008-09-19 22:17:41 +08004402 int ret, local = 1;
Tao Ma01225592008-08-18 17:38:53 +08004403 size_t value_len;
4404 char *val = (char *)xi->value;
4405 struct ocfs2_xattr_entry *xe = xs->here;
Tao Ma2057e5c2008-10-09 23:06:13 +08004406 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
4407 strlen(xi->name));
Tao Ma01225592008-08-18 17:38:53 +08004408
4409 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
4410 /*
4411 * We need to truncate the xattr storage first.
4412 *
4413 * If both the old and new value are stored to
4414 * outside block, we only need to truncate
4415 * the storage and then set the value outside.
4416 *
4417 * If the new value should be stored within block,
4418 * we should free all the outside block first and
4419 * the modification to the xattr block will be done
4420 * by following steps.
4421 */
4422 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4423 value_len = xi->value_len;
4424 else
4425 value_len = 0;
4426
4427 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4428 value_len);
4429 if (ret)
4430 goto out;
4431
4432 if (value_len)
4433 goto set_value_outside;
4434 }
4435
4436 value_len = xi->value_len;
4437 /* So we have to handle the inside block change now. */
4438 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
4439 /*
4440 * If the new value will be stored outside of block,
4441 * initalize a new empty value root and insert it first.
4442 */
4443 local = 0;
4444 xi->value = &def_xv;
4445 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
4446 }
4447
Tao Ma5a0956112008-09-19 22:17:41 +08004448 ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local);
Tao Ma01225592008-08-18 17:38:53 +08004449 if (ret) {
4450 mlog_errno(ret);
4451 goto out;
4452 }
4453
Tao Ma5a0956112008-09-19 22:17:41 +08004454 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
4455 goto out;
Tao Ma01225592008-08-18 17:38:53 +08004456
Tao Ma5a0956112008-09-19 22:17:41 +08004457 /* allocate the space now for the outside block storage. */
4458 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
4459 value_len);
4460 if (ret) {
4461 mlog_errno(ret);
4462
4463 if (xs->not_found) {
4464 /*
4465 * We can't allocate enough clusters for outside
4466 * storage and we have allocated xattr already,
4467 * so need to remove it.
4468 */
4469 ocfs2_xattr_bucket_remove_xs(inode, xs);
Tao Ma01225592008-08-18 17:38:53 +08004470 }
Tao Ma01225592008-08-18 17:38:53 +08004471 goto out;
4472 }
4473
4474set_value_outside:
4475 ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len);
4476out:
4477 return ret;
4478}
4479
Tao Ma80bcaf32008-10-27 06:06:24 +08004480/*
4481 * check whether the xattr bucket is filled up with the same hash value.
4482 * If we want to insert the xattr with the same hash, return -ENOSPC.
4483 * If we want to insert a xattr with different hash value, go ahead
4484 * and ocfs2_divide_xattr_bucket will handle this.
4485 */
Tao Ma01225592008-08-18 17:38:53 +08004486static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
Tao Ma80bcaf32008-10-27 06:06:24 +08004487 struct ocfs2_xattr_bucket *bucket,
4488 const char *name)
Tao Ma01225592008-08-18 17:38:53 +08004489{
Joel Becker3e632942008-10-24 17:04:49 -07004490 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Ma80bcaf32008-10-27 06:06:24 +08004491 u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name));
4492
4493 if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash))
4494 return 0;
Tao Ma01225592008-08-18 17:38:53 +08004495
4496 if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash ==
4497 xh->xh_entries[0].xe_name_hash) {
4498 mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, "
4499 "hash = %u\n",
Joel Becker9c7759a2008-10-24 16:21:03 -07004500 (unsigned long long)bucket_blkno(bucket),
Tao Ma01225592008-08-18 17:38:53 +08004501 le32_to_cpu(xh->xh_entries[0].xe_name_hash));
4502 return -ENOSPC;
4503 }
4504
4505 return 0;
4506}
4507
4508static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
4509 struct ocfs2_xattr_info *xi,
4510 struct ocfs2_xattr_search *xs)
4511{
4512 struct ocfs2_xattr_header *xh;
4513 struct ocfs2_xattr_entry *xe;
4514 u16 count, header_size, xh_free_start;
Joel Becker6dde41d2008-10-24 17:16:48 -07004515 int free, max_free, need, old;
Tao Ma01225592008-08-18 17:38:53 +08004516 size_t value_size = 0, name_len = strlen(xi->name);
4517 size_t blocksize = inode->i_sb->s_blocksize;
4518 int ret, allocation = 0;
Tao Ma01225592008-08-18 17:38:53 +08004519
4520 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
4521
4522try_again:
4523 xh = xs->header;
4524 count = le16_to_cpu(xh->xh_count);
4525 xh_free_start = le16_to_cpu(xh->xh_free_start);
4526 header_size = sizeof(struct ocfs2_xattr_header) +
4527 count * sizeof(struct ocfs2_xattr_entry);
4528 max_free = OCFS2_XATTR_BUCKET_SIZE -
4529 le16_to_cpu(xh->xh_name_value_len) - header_size;
4530
4531 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
4532 "of %u which exceed block size\n",
Joel Beckerba937122008-10-24 19:13:20 -07004533 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004534 header_size);
4535
4536 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE)
4537 value_size = OCFS2_XATTR_ROOT_SIZE;
4538 else if (xi->value)
4539 value_size = OCFS2_XATTR_SIZE(xi->value_len);
4540
4541 if (xs->not_found)
4542 need = sizeof(struct ocfs2_xattr_entry) +
4543 OCFS2_XATTR_SIZE(name_len) + value_size;
4544 else {
4545 need = value_size + OCFS2_XATTR_SIZE(name_len);
4546
4547 /*
4548 * We only replace the old value if the new length is smaller
4549 * than the old one. Otherwise we will allocate new space in the
4550 * bucket to store it.
4551 */
4552 xe = xs->here;
4553 if (ocfs2_xattr_is_local(xe))
4554 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4555 else
4556 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4557
4558 if (old >= value_size)
4559 need = 0;
4560 }
4561
4562 free = xh_free_start - header_size;
4563 /*
4564 * We need to make sure the new name/value pair
4565 * can exist in the same block.
4566 */
4567 if (xh_free_start % blocksize < need)
4568 free -= xh_free_start % blocksize;
4569
4570 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
4571 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
4572 " %u\n", xs->not_found,
Joel Beckerba937122008-10-24 19:13:20 -07004573 (unsigned long long)bucket_blkno(xs->bucket),
Tao Ma01225592008-08-18 17:38:53 +08004574 free, need, max_free, le16_to_cpu(xh->xh_free_start),
4575 le16_to_cpu(xh->xh_name_value_len));
4576
4577 if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4578 if (need <= max_free &&
4579 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
4580 /*
4581 * We can create the space by defragment. Since only the
4582 * name/value will be moved, the xe shouldn't be changed
4583 * in xs.
4584 */
Joel Beckerba937122008-10-24 19:13:20 -07004585 ret = ocfs2_defrag_xattr_bucket(inode, xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004586 if (ret) {
4587 mlog_errno(ret);
4588 goto out;
4589 }
4590
4591 xh_free_start = le16_to_cpu(xh->xh_free_start);
4592 free = xh_free_start - header_size;
4593 if (xh_free_start % blocksize < need)
4594 free -= xh_free_start % blocksize;
4595
4596 if (free >= need)
4597 goto xattr_set;
4598
4599 mlog(0, "Can't get enough space for xattr insert by "
4600 "defragment. Need %u bytes, but we have %d, so "
4601 "allocate new bucket for it.\n", need, free);
4602 }
4603
4604 /*
4605 * We have to add new buckets or clusters and one
4606 * allocation should leave us enough space for insert.
4607 */
4608 BUG_ON(allocation);
4609
4610 /*
4611 * We do not allow for overlapping ranges between buckets. And
4612 * the maximum number of collisions we will allow for then is
4613 * one bucket's worth, so check it here whether we need to
4614 * add a new bucket for the insert.
4615 */
Tao Ma80bcaf32008-10-27 06:06:24 +08004616 ret = ocfs2_check_xattr_bucket_collision(inode,
Joel Beckerba937122008-10-24 19:13:20 -07004617 xs->bucket,
Tao Ma80bcaf32008-10-27 06:06:24 +08004618 xi->name);
Tao Ma01225592008-08-18 17:38:53 +08004619 if (ret) {
4620 mlog_errno(ret);
4621 goto out;
4622 }
4623
4624 ret = ocfs2_add_new_xattr_bucket(inode,
4625 xs->xattr_bh,
Joel Beckerba937122008-10-24 19:13:20 -07004626 xs->bucket->bu_bhs[0]);
Tao Ma01225592008-08-18 17:38:53 +08004627 if (ret) {
4628 mlog_errno(ret);
4629 goto out;
4630 }
4631
Joel Beckerba937122008-10-24 19:13:20 -07004632 ocfs2_xattr_bucket_relse(xs->bucket);
Tao Ma01225592008-08-18 17:38:53 +08004633
4634 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
4635 xi->name_index,
4636 xi->name, xs);
4637 if (ret && ret != -ENODATA)
4638 goto out;
4639 xs->not_found = ret;
4640 allocation = 1;
4641 goto try_again;
4642 }
4643
4644xattr_set:
4645 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs);
4646out:
4647 mlog_exit(ret);
4648 return ret;
4649}
Tao Maa3944252008-08-18 17:38:54 +08004650
4651static int ocfs2_delete_xattr_in_bucket(struct inode *inode,
4652 struct ocfs2_xattr_bucket *bucket,
4653 void *para)
4654{
4655 int ret = 0;
Joel Becker3e632942008-10-24 17:04:49 -07004656 struct ocfs2_xattr_header *xh = bucket_xh(bucket);
Tao Maa3944252008-08-18 17:38:54 +08004657 u16 i;
4658 struct ocfs2_xattr_entry *xe;
4659
4660 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4661 xe = &xh->xh_entries[i];
4662 if (ocfs2_xattr_is_local(xe))
4663 continue;
4664
4665 ret = ocfs2_xattr_bucket_value_truncate(inode,
Joel Becker4ac60322008-10-18 19:11:42 -07004666 bucket->bu_bhs[0],
Tao Maa3944252008-08-18 17:38:54 +08004667 i, 0);
4668 if (ret) {
4669 mlog_errno(ret);
4670 break;
4671 }
4672 }
4673
4674 return ret;
4675}
4676
4677static int ocfs2_delete_xattr_index_block(struct inode *inode,
4678 struct buffer_head *xb_bh)
4679{
4680 struct ocfs2_xattr_block *xb =
4681 (struct ocfs2_xattr_block *)xb_bh->b_data;
4682 struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list;
4683 int ret = 0;
4684 u32 name_hash = UINT_MAX, e_cpos, num_clusters;
4685 u64 p_blkno;
4686
4687 if (le16_to_cpu(el->l_next_free_rec) == 0)
4688 return 0;
4689
4690 while (name_hash > 0) {
4691 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
4692 &e_cpos, &num_clusters, el);
4693 if (ret) {
4694 mlog_errno(ret);
4695 goto out;
4696 }
4697
4698 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
4699 ocfs2_delete_xattr_in_bucket,
4700 NULL);
4701 if (ret) {
4702 mlog_errno(ret);
4703 goto out;
4704 }
4705
4706 ret = ocfs2_rm_xattr_cluster(inode, xb_bh,
4707 p_blkno, e_cpos, num_clusters);
4708 if (ret) {
4709 mlog_errno(ret);
4710 break;
4711 }
4712
4713 if (e_cpos == 0)
4714 break;
4715
4716 name_hash = e_cpos - 1;
4717 }
4718
4719out:
4720 return ret;
4721}
Mark Fasheh99219ae2008-10-07 14:52:59 -07004722
4723/*
4724 * 'trusted' attributes support
4725 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07004726static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
4727 size_t list_size, const char *name,
4728 size_t name_len)
4729{
Tiger Yangceb1eba2008-10-23 16:34:13 +08004730 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07004731 const size_t total_len = prefix_len + name_len + 1;
4732
4733 if (list && total_len <= list_size) {
4734 memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len);
4735 memcpy(list + prefix_len, name, name_len);
4736 list[prefix_len + name_len] = '\0';
4737 }
4738 return total_len;
4739}
4740
4741static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name,
4742 void *buffer, size_t size)
4743{
4744 if (strcmp(name, "") == 0)
4745 return -EINVAL;
4746 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name,
4747 buffer, size);
4748}
4749
4750static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name,
4751 const void *value, size_t size, int flags)
4752{
4753 if (strcmp(name, "") == 0)
4754 return -EINVAL;
4755
4756 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value,
4757 size, flags);
4758}
4759
4760struct xattr_handler ocfs2_xattr_trusted_handler = {
4761 .prefix = XATTR_TRUSTED_PREFIX,
4762 .list = ocfs2_xattr_trusted_list,
4763 .get = ocfs2_xattr_trusted_get,
4764 .set = ocfs2_xattr_trusted_set,
4765};
4766
Mark Fasheh99219ae2008-10-07 14:52:59 -07004767/*
4768 * 'user' attributes support
4769 */
Mark Fasheh99219ae2008-10-07 14:52:59 -07004770static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
4771 size_t list_size, const char *name,
4772 size_t name_len)
4773{
Tiger Yangceb1eba2008-10-23 16:34:13 +08004774 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
Mark Fasheh99219ae2008-10-07 14:52:59 -07004775 const size_t total_len = prefix_len + name_len + 1;
4776 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4777
4778 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4779 return 0;
4780
4781 if (list && total_len <= list_size) {
4782 memcpy(list, XATTR_USER_PREFIX, prefix_len);
4783 memcpy(list + prefix_len, name, name_len);
4784 list[prefix_len + name_len] = '\0';
4785 }
4786 return total_len;
4787}
4788
4789static int ocfs2_xattr_user_get(struct inode *inode, const char *name,
4790 void *buffer, size_t size)
4791{
4792 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4793
4794 if (strcmp(name, "") == 0)
4795 return -EINVAL;
4796 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4797 return -EOPNOTSUPP;
4798 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name,
4799 buffer, size);
4800}
4801
4802static int ocfs2_xattr_user_set(struct inode *inode, const char *name,
4803 const void *value, size_t size, int flags)
4804{
4805 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
4806
4807 if (strcmp(name, "") == 0)
4808 return -EINVAL;
4809 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
4810 return -EOPNOTSUPP;
4811
4812 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value,
4813 size, flags);
4814}
4815
4816struct xattr_handler ocfs2_xattr_user_handler = {
4817 .prefix = XATTR_USER_PREFIX,
4818 .list = ocfs2_xattr_user_list,
4819 .get = ocfs2_xattr_user_get,
4820 .set = ocfs2_xattr_user_set,
4821};