Commit | Line | Data |
---|---|---|
f56654c4 TM |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * xattr.c | |
5 | * | |
c3cb6827 | 6 | * Copyright (C) 2004, 2008 Oracle. All rights reserved. |
f56654c4 | 7 | * |
cf1d6c76 | 8 | * CREDITS: |
c3cb6827 TY |
9 | * Lots of code in this file is copy from linux/fs/ext3/xattr.c. |
10 | * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> | |
cf1d6c76 | 11 | * |
f56654c4 TM |
12 | * This program is free software; you can redistribute it and/or |
13 | * modify it under the terms of the GNU General Public | |
c3cb6827 | 14 | * License version 2 as published by the Free Software Foundation. |
f56654c4 TM |
15 | * |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * General Public License for more details. | |
f56654c4 TM |
20 | */ |
21 | ||
cf1d6c76 TY |
22 | #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> | |
01225596 | 34 | #include <linux/sort.h> |
99219aea MF |
35 | #include <linux/init.h> |
36 | #include <linux/module.h> | |
37 | #include <linux/string.h> | |
cf1d6c76 | 38 | |
f56654c4 TM |
39 | #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" | |
cf1d6c76 TY |
46 | #include "symlink.h" |
47 | #include "sysfile.h" | |
f56654c4 TM |
48 | #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" | |
0c044f0b | 54 | #include "super.h" |
cf1d6c76 TY |
55 | #include "xattr.h" |
56 | ||
57 | ||
58 | struct ocfs2_xattr_def_value_root { | |
59 | struct ocfs2_xattr_value_root xv; | |
60 | struct ocfs2_extent_rec er; | |
61 | }; | |
62 | ||
0c044f0b | 63 | struct ocfs2_xattr_bucket { |
ba937127 JB |
64 | /* The inode these xattrs are associated with */ |
65 | struct inode *bu_inode; | |
66 | ||
67 | /* The actual buffers that make up the bucket */ | |
4ac6032d | 68 | struct buffer_head *bu_bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET]; |
ba937127 JB |
69 | |
70 | /* How many blocks make up one bucket for this filesystem */ | |
71 | int bu_blocks; | |
0c044f0b TM |
72 | }; |
73 | ||
cf1d6c76 TY |
74 | #define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root)) |
75 | #define OCFS2_XATTR_INLINE_SIZE 80 | |
76 | ||
77 | static struct ocfs2_xattr_def_value_root def_xv = { | |
78 | .xv.xr_list.l_count = cpu_to_le16(1), | |
79 | }; | |
80 | ||
81 | struct xattr_handler *ocfs2_xattr_handlers[] = { | |
82 | &ocfs2_xattr_user_handler, | |
83 | &ocfs2_xattr_trusted_handler, | |
84 | NULL | |
85 | }; | |
86 | ||
c988fd04 | 87 | static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = { |
cf1d6c76 TY |
88 | [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler, |
89 | [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler, | |
90 | }; | |
91 | ||
92 | struct ocfs2_xattr_info { | |
93 | int name_index; | |
94 | const char *name; | |
95 | const void *value; | |
96 | size_t value_len; | |
97 | }; | |
98 | ||
99 | struct 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; | |
ba937127 | 107 | struct ocfs2_xattr_bucket *bucket; |
cf1d6c76 TY |
108 | void *base; |
109 | void *end; | |
110 | struct ocfs2_xattr_entry *here; | |
111 | int not_found; | |
112 | }; | |
113 | ||
589dc260 TM |
114 | static 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 | ||
54f443f4 JB |
120 | static int ocfs2_xattr_block_find(struct inode *inode, |
121 | int name_index, | |
122 | const char *name, | |
123 | struct ocfs2_xattr_search *xs); | |
589dc260 TM |
124 | static 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 | ||
0c044f0b TM |
130 | static 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 | ||
01225596 TM |
135 | static int ocfs2_xattr_create_index_block(struct inode *inode, |
136 | struct ocfs2_xattr_search *xs); | |
137 | ||
138 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, | |
139 | struct ocfs2_xattr_info *xi, | |
140 | struct ocfs2_xattr_search *xs); | |
141 | ||
a3944256 TM |
142 | static int ocfs2_delete_xattr_index_block(struct inode *inode, |
143 | struct buffer_head *xb_bh); | |
144 | ||
0030e001 TY |
145 | static 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 | ||
150 | static 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 | ||
155 | static 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 | ||
9c7759aa | 163 | #define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr) |
51def39f | 164 | #define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data) |
3e632946 | 165 | #define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0)) |
9c7759aa | 166 | |
ba937127 | 167 | static struct ocfs2_xattr_bucket *ocfs2_xattr_bucket_new(struct inode *inode) |
6dde41d9 | 168 | { |
ba937127 JB |
169 | struct ocfs2_xattr_bucket *bucket; |
170 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
6dde41d9 | 171 | |
ba937127 JB |
172 | 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 | ||
183 | static void ocfs2_xattr_bucket_relse(struct ocfs2_xattr_bucket *bucket) | |
184 | { | |
185 | int i; | |
186 | ||
187 | for (i = 0; i < bucket->bu_blocks; i++) { | |
6dde41d9 JB |
188 | brelse(bucket->bu_bhs[i]); |
189 | bucket->bu_bhs[i] = NULL; | |
190 | } | |
191 | } | |
192 | ||
ba937127 JB |
193 | static 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 | ||
784b816a JB |
202 | /* |
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 | */ | |
ba937127 | 208 | static int ocfs2_init_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
784b816a JB |
209 | u64 xb_blkno) |
210 | { | |
211 | int i, rc = 0; | |
784b816a | 212 | |
ba937127 JB |
213 | for (i = 0; i < bucket->bu_blocks; i++) { |
214 | bucket->bu_bhs[i] = sb_getblk(bucket->bu_inode->i_sb, | |
215 | xb_blkno + i); | |
784b816a JB |
216 | if (!bucket->bu_bhs[i]) { |
217 | rc = -EIO; | |
218 | mlog_errno(rc); | |
219 | break; | |
220 | } | |
221 | ||
ba937127 JB |
222 | ocfs2_set_new_buffer_uptodate(bucket->bu_inode, |
223 | bucket->bu_bhs[i]); | |
784b816a JB |
224 | } |
225 | ||
226 | if (rc) | |
ba937127 | 227 | ocfs2_xattr_bucket_relse(bucket); |
784b816a JB |
228 | return rc; |
229 | } | |
230 | ||
231 | /* Read the xattr bucket at xb_blkno */ | |
ba937127 | 232 | static int ocfs2_read_xattr_bucket(struct ocfs2_xattr_bucket *bucket, |
784b816a JB |
233 | u64 xb_blkno) |
234 | { | |
ba937127 | 235 | int rc; |
784b816a | 236 | |
ba937127 JB |
237 | rc = ocfs2_read_blocks(bucket->bu_inode, xb_blkno, |
238 | bucket->bu_blocks, bucket->bu_bhs, 0); | |
784b816a | 239 | if (rc) |
ba937127 | 240 | ocfs2_xattr_bucket_relse(bucket); |
784b816a JB |
241 | return rc; |
242 | } | |
243 | ||
1224be02 | 244 | static int ocfs2_xattr_bucket_journal_access(handle_t *handle, |
1224be02 JB |
245 | struct ocfs2_xattr_bucket *bucket, |
246 | int type) | |
247 | { | |
248 | int i, rc = 0; | |
1224be02 | 249 | |
ba937127 JB |
250 | for (i = 0; i < bucket->bu_blocks; i++) { |
251 | rc = ocfs2_journal_access(handle, bucket->bu_inode, | |
1224be02 JB |
252 | bucket->bu_bhs[i], type); |
253 | if (rc) { | |
254 | mlog_errno(rc); | |
255 | break; | |
256 | } | |
257 | } | |
258 | ||
259 | return rc; | |
260 | } | |
261 | ||
262 | static void ocfs2_xattr_bucket_journal_dirty(handle_t *handle, | |
1224be02 JB |
263 | struct ocfs2_xattr_bucket *bucket) |
264 | { | |
ba937127 | 265 | int i; |
1224be02 | 266 | |
ba937127 | 267 | for (i = 0; i < bucket->bu_blocks; i++) |
1224be02 JB |
268 | ocfs2_journal_dirty(handle, bucket->bu_bhs[i]); |
269 | } | |
270 | ||
ba937127 | 271 | static void ocfs2_xattr_bucket_copy_data(struct ocfs2_xattr_bucket *dest, |
4980c6da JB |
272 | struct ocfs2_xattr_bucket *src) |
273 | { | |
274 | int i; | |
ba937127 JB |
275 | int blocksize = src->bu_inode->i_sb->s_blocksize; |
276 | ||
277 | BUG_ON(dest->bu_blocks != src->bu_blocks); | |
278 | BUG_ON(dest->bu_inode != src->bu_inode); | |
4980c6da | 279 | |
ba937127 | 280 | for (i = 0; i < src->bu_blocks; i++) { |
4980c6da JB |
281 | memcpy(bucket_block(dest, i), bucket_block(src, i), |
282 | blocksize); | |
283 | } | |
284 | } | |
1224be02 | 285 | |
936b8834 | 286 | static inline const char *ocfs2_xattr_prefix(int name_index) |
cf1d6c76 TY |
287 | { |
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 | ||
936b8834 | 293 | return handler ? handler->prefix : NULL; |
cf1d6c76 TY |
294 | } |
295 | ||
40daa16a | 296 | static u32 ocfs2_xattr_name_hash(struct inode *inode, |
2057e5c6 | 297 | const char *name, |
40daa16a | 298 | int name_len) |
cf1d6c76 TY |
299 | { |
300 | /* Get hash value of uuid from super block */ | |
301 | u32 hash = OCFS2_SB(inode->i_sb)->uuid_hash; | |
302 | int i; | |
303 | ||
cf1d6c76 TY |
304 | /* 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 | */ | |
319 | static 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; | |
cf1d6c76 | 324 | char *name = (char *)header + le16_to_cpu(entry->xe_name_offset); |
cf1d6c76 | 325 | |
2057e5c6 | 326 | hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len); |
cf1d6c76 TY |
327 | entry->xe_name_hash = cpu_to_le32(hash); |
328 | ||
329 | return; | |
330 | } | |
f56654c4 TM |
331 | |
332 | static 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); | |
f56654c4 | 345 | u32 prev_clusters, logical_start = le32_to_cpu(xv->xr_clusters); |
f99b9b7c | 346 | struct ocfs2_extent_tree et; |
f56654c4 TM |
347 | |
348 | mlog(0, "(clusters_to_add for xattr= %u)\n", clusters_to_add); | |
349 | ||
8d6220d6 | 350 | ocfs2_init_xattr_value_extent_tree(&et, inode, xattr_bh, xv); |
f99b9b7c | 351 | |
f56654c4 TM |
352 | restart_all: |
353 | ||
f99b9b7c JB |
354 | status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0, |
355 | &data_ac, &meta_ac); | |
f56654c4 TM |
356 | if (status) { |
357 | mlog_errno(status); | |
358 | goto leave; | |
359 | } | |
360 | ||
f99b9b7c JB |
361 | credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el, |
362 | clusters_to_add); | |
f56654c4 TM |
363 | 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 | ||
371 | restarted_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, | |
f99b9b7c | 385 | &et, |
f56654c4 TM |
386 | handle, |
387 | data_ac, | |
388 | meta_ac, | |
f99b9b7c | 389 | &why); |
f56654c4 TM |
390 | 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, | |
f99b9b7c | 414 | et.et_root_el, |
f56654c4 TM |
415 | 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 | ||
428 | leave: | |
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 | ||
449 | static 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; | |
f99b9b7c JB |
461 | struct ocfs2_extent_tree et; |
462 | ||
8d6220d6 | 463 | ocfs2_init_xattr_value_extent_tree(&et, inode, root_bh, xv); |
f56654c4 | 464 | |
f99b9b7c | 465 | ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac); |
f56654c4 TM |
466 | 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 | ||
f99b9b7c JB |
495 | ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac, |
496 | dealloc); | |
f56654c4 TM |
497 | 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 | ||
514 | out_commit: | |
515 | ocfs2_commit_trans(osb, handle); | |
516 | out: | |
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 | ||
525 | static 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 | ||
570 | out: | |
571 | ocfs2_schedule_truncate_log_flush(osb, 1); | |
572 | ocfs2_run_deallocs(osb, &dealloc); | |
573 | ||
574 | return ret; | |
575 | } | |
576 | ||
577 | static 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 | } | |
cf1d6c76 | 600 | |
936b8834 TM |
601 | static 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 | ||
cf1d6c76 TY |
625 | static int ocfs2_xattr_list_entries(struct inode *inode, |
626 | struct ocfs2_xattr_header *header, | |
627 | char *buffer, size_t buffer_size) | |
628 | { | |
936b8834 TM |
629 | size_t result = 0; |
630 | int i, type, ret; | |
631 | const char *prefix, *name; | |
cf1d6c76 TY |
632 | |
633 | for (i = 0 ; i < le16_to_cpu(header->xh_count); i++) { | |
634 | struct ocfs2_xattr_entry *entry = &header->xh_entries[i]; | |
936b8834 TM |
635 | type = ocfs2_xattr_get_type(entry); |
636 | prefix = ocfs2_xattr_prefix(type); | |
637 | ||
638 | 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; | |
cf1d6c76 TY |
647 | } |
648 | } | |
649 | ||
936b8834 | 650 | return result; |
cf1d6c76 TY |
651 | } |
652 | ||
653 | static 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 | ||
674 | static 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; | |
0c044f0b | 680 | struct ocfs2_xattr_block *xb; |
cf1d6c76 TY |
681 | int ret = 0; |
682 | ||
683 | if (!di->i_xattr_loc) | |
684 | return ret; | |
685 | ||
0fcaa56a | 686 | ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh); |
cf1d6c76 TY |
687 | if (ret < 0) { |
688 | mlog_errno(ret); | |
689 | return ret; | |
690 | } | |
cf1d6c76 | 691 | |
0c044f0b | 692 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
f6087fb7 JB |
693 | if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { |
694 | ret = -EIO; | |
695 | goto cleanup; | |
696 | } | |
cf1d6c76 | 697 | |
0c044f0b TM |
698 | 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 | } | |
cf1d6c76 TY |
707 | cleanup: |
708 | brelse(blk_bh); | |
709 | ||
710 | return ret; | |
711 | } | |
712 | ||
713 | ssize_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 | ||
8154da3d TY |
722 | if (!ocfs2_supports_xattr(OCFS2_SB(dentry->d_sb))) |
723 | return -EOPNOTSUPP; | |
724 | ||
cf1d6c76 TY |
725 | 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 | ||
758 | static 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 | ||
788 | static int ocfs2_xattr_get_value_outside(struct inode *inode, | |
589dc260 | 789 | struct ocfs2_xattr_value_root *xv, |
cf1d6c76 TY |
790 | 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; | |
cf1d6c76 TY |
798 | struct ocfs2_extent_list *el; |
799 | ||
cf1d6c76 TY |
800 | 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++) { | |
0fcaa56a | 817 | ret = ocfs2_read_block(inode, blkno, &bh); |
cf1d6c76 TY |
818 | 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 | } | |
835 | out: | |
836 | return ret; | |
837 | } | |
838 | ||
839 | static 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; | |
589dc260 | 848 | struct ocfs2_xattr_value_root *xv; |
cf1d6c76 TY |
849 | 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 { | |
589dc260 TM |
873 | 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, | |
cf1d6c76 TY |
878 | buffer, size); |
879 | if (ret < 0) { | |
880 | mlog_errno(ret); | |
881 | return ret; | |
882 | } | |
883 | } | |
884 | } | |
885 | ||
886 | return size; | |
887 | } | |
888 | ||
889 | static 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 | { | |
cf1d6c76 | 896 | struct ocfs2_xattr_block *xb; |
589dc260 | 897 | struct ocfs2_xattr_value_root *xv; |
cf1d6c76 | 898 | size_t size; |
589dc260 | 899 | int ret = -ENODATA, name_offset, name_len, block_off, i; |
cf1d6c76 | 900 | |
ba937127 JB |
901 | xs->bucket = ocfs2_xattr_bucket_new(inode); |
902 | if (!xs->bucket) { | |
903 | ret = -ENOMEM; | |
904 | mlog_errno(ret); | |
905 | goto cleanup; | |
906 | } | |
589dc260 | 907 | |
54f443f4 JB |
908 | ret = ocfs2_xattr_block_find(inode, name_index, name, xs); |
909 | if (ret) { | |
cf1d6c76 | 910 | mlog_errno(ret); |
cf1d6c76 TY |
911 | goto cleanup; |
912 | } | |
913 | ||
6c1e183e TY |
914 | if (xs->not_found) { |
915 | ret = -ENODATA; | |
916 | goto cleanup; | |
917 | } | |
918 | ||
54f443f4 | 919 | xb = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; |
cf1d6c76 TY |
920 | size = le64_to_cpu(xs->here->xe_value_size); |
921 | if (buffer) { | |
922 | ret = -ERANGE; | |
923 | if (size > buffer_size) | |
924 | goto cleanup; | |
589dc260 TM |
925 | |
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, | |
ba937127 | 932 | bucket_xh(xs->bucket), |
589dc260 TM |
933 | i, |
934 | &block_off, | |
935 | &name_offset); | |
ba937127 | 936 | xs->base = bucket_block(xs->bucket, block_off); |
589dc260 | 937 | } |
cf1d6c76 TY |
938 | if (ocfs2_xattr_is_local(xs->here)) { |
939 | memcpy(buffer, (void *)xs->base + | |
589dc260 | 940 | name_offset + name_len, size); |
cf1d6c76 | 941 | } else { |
589dc260 TM |
942 | xv = (struct ocfs2_xattr_value_root *) |
943 | (xs->base + name_offset + name_len); | |
944 | ret = ocfs2_xattr_get_value_outside(inode, xv, | |
cf1d6c76 TY |
945 | buffer, size); |
946 | if (ret < 0) { | |
947 | mlog_errno(ret); | |
948 | goto cleanup; | |
949 | } | |
950 | } | |
951 | } | |
952 | ret = size; | |
953 | cleanup: | |
ba937127 | 954 | ocfs2_xattr_bucket_free(xs->bucket); |
cf1d6c76 | 955 | |
54f443f4 JB |
956 | brelse(xs->xattr_bh); |
957 | xs->xattr_bh = NULL; | |
cf1d6c76 TY |
958 | 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 | */ | |
0030e001 TY |
966 | static int ocfs2_xattr_get(struct inode *inode, |
967 | int name_index, | |
968 | const char *name, | |
969 | void *buffer, | |
970 | size_t buffer_size) | |
cf1d6c76 TY |
971 | { |
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 | ||
8154da3d TY |
983 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
984 | return -EOPNOTSUPP; | |
985 | ||
cf1d6c76 TY |
986 | 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); | |
6c1e183e | 1000 | if (ret == -ENODATA && di->i_xattr_loc) |
cf1d6c76 TY |
1001 | 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 | ||
1011 | static 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++) { | |
0fcaa56a | 1046 | ret = ocfs2_read_block(inode, blkno, &bh); |
cf1d6c76 TY |
1047 | 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 | } | |
1086 | out_commit: | |
1087 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
1088 | out: | |
1089 | brelse(bh); | |
1090 | ||
1091 | return ret; | |
1092 | } | |
1093 | ||
1094 | static 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); | |
1127 | out_commit: | |
1128 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
1129 | out: | |
1130 | return ret; | |
1131 | } | |
1132 | ||
1133 | static 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); | |
1166 | out_commit: | |
1167 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
1168 | out: | |
1169 | return ret; | |
1170 | } | |
1171 | ||
1172 | /* | |
1173 | * ocfs2_xattr_set_value_outside() | |
1174 | * | |
1175 | * Set large size value in B tree. | |
1176 | */ | |
1177 | static 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 | */ | |
1222 | static 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 | */ | |
1327 | static 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) | |
b37c4d84 | 1358 | return -EIO; |
cf1d6c76 TY |
1359 | |
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)) { | |
28b8ca0b | 1472 | /* set extended attribute in external block. */ |
cf1d6c76 | 1473 | ret = ocfs2_extend_trans(handle, |
28b8ca0b | 1474 | OCFS2_INODE_UPDATE_CREDITS + |
cf1d6c76 TY |
1475 | 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 | ||
1535 | out_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 | } | |
1558 | out: | |
1559 | return ret; | |
1560 | ||
1561 | } | |
1562 | ||
cf1d6c76 TY |
1563 | static 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 | ||
1591 | static 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 | ||
1608 | static int ocfs2_xattr_block_remove(struct inode *inode, | |
1609 | struct buffer_head *blk_bh) | |
1610 | { | |
1611 | struct ocfs2_xattr_block *xb; | |
cf1d6c76 TY |
1612 | int ret = 0; |
1613 | ||
1614 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; | |
a3944256 TM |
1615 | 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); | |
cf1d6c76 TY |
1620 | |
1621 | return ret; | |
1622 | } | |
1623 | ||
08413899 TM |
1624 | static 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 | ||
0fcaa56a | 1637 | ret = ocfs2_read_block(inode, block, &blk_bh); |
08413899 TM |
1638 | if (ret < 0) { |
1639 | mlog_errno(ret); | |
1640 | goto out; | |
1641 | } | |
1642 | ||
f6087fb7 JB |
1643 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; |
1644 | if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { | |
1645 | ret = -EIO; | |
08413899 TM |
1646 | 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 | ||
08413899 TM |
1655 | 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); | |
1688 | out_unlock: | |
1689 | ocfs2_inode_unlock(xb_alloc_inode, 1); | |
1690 | brelse(xb_alloc_bh); | |
1691 | out_mutex: | |
1692 | mutex_unlock(&xb_alloc_inode->i_mutex); | |
1693 | iput(xb_alloc_inode); | |
1694 | out: | |
1695 | brelse(blk_bh); | |
1696 | return ret; | |
1697 | } | |
1698 | ||
cf1d6c76 TY |
1699 | /* |
1700 | * ocfs2_xattr_remove() | |
1701 | * | |
1702 | * Free extended attribute resources associated with this inode. | |
1703 | */ | |
1704 | int ocfs2_xattr_remove(struct inode *inode, struct buffer_head *di_bh) | |
1705 | { | |
cf1d6c76 TY |
1706 | 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 | ||
8154da3d TY |
1711 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
1712 | return 0; | |
1713 | ||
cf1d6c76 TY |
1714 | 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 | } | |
cf1d6c76 | 1724 | |
08413899 TM |
1725 | if (di->i_xattr_loc) { |
1726 | ret = ocfs2_xattr_free_block(inode, | |
1727 | le64_to_cpu(di->i_xattr_loc)); | |
cf1d6c76 TY |
1728 | 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 | ||
08413899 | 1748 | di->i_xattr_loc = 0; |
cf1d6c76 TY |
1749 | |
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); | |
1758 | out_commit: | |
1759 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
1760 | out: | |
cf1d6c76 TY |
1761 | return ret; |
1762 | } | |
1763 | ||
1764 | static 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 | */ | |
1798 | static 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 | */ | |
1847 | static 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)); | |
1868 | out: | |
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 | */ | |
1880 | static 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; | |
589dc260 | 1887 | struct ocfs2_xattr_block *xb; |
cf1d6c76 TY |
1888 | int ret = 0; |
1889 | ||
1890 | if (!di->i_xattr_loc) | |
1891 | return ret; | |
1892 | ||
0fcaa56a | 1893 | ret = ocfs2_read_block(inode, le64_to_cpu(di->i_xattr_loc), &blk_bh); |
cf1d6c76 TY |
1894 | if (ret < 0) { |
1895 | mlog_errno(ret); | |
1896 | return ret; | |
1897 | } | |
f6087fb7 JB |
1898 | |
1899 | xb = (struct ocfs2_xattr_block *)blk_bh->b_data; | |
1900 | if (!OCFS2_IS_VALID_XATTR_BLOCK(xb)) { | |
1901 | ret = -EIO; | |
1902 | goto cleanup; | |
cf1d6c76 TY |
1903 | } |
1904 | ||
1905 | xs->xattr_bh = blk_bh; | |
589dc260 TM |
1906 | |
1907 | 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); | |
cf1d6c76 | 1918 | |
cf1d6c76 TY |
1919 | if (ret && ret != -ENODATA) { |
1920 | xs->xattr_bh = NULL; | |
1921 | goto cleanup; | |
1922 | } | |
1923 | xs->not_found = ret; | |
1924 | return 0; | |
cf1d6c76 TY |
1925 | cleanup: |
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 | */ | |
1937 | static 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); | |
2019 | out_commit: | |
2020 | ocfs2_commit_trans(osb, handle); | |
2021 | out: | |
2022 | if (meta_ac) | |
2023 | ocfs2_free_alloc_context(meta_ac); | |
2024 | if (ret < 0) | |
2025 | return ret; | |
01225596 TM |
2026 | } 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; | |
cf1d6c76 TY |
2038 | } |
2039 | ||
01225596 | 2040 | ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs); |
01225596 TM |
2041 | |
2042 | end: | |
cf1d6c76 TY |
2043 | |
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 | */ | |
2054 | int 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 | ||
8154da3d TY |
2080 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
2081 | return -EOPNOTSUPP; | |
2082 | ||
ba937127 JB |
2083 | /* |
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 | ||
cf1d6c76 TY |
2093 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
2094 | if (ret < 0) { | |
2095 | mlog_errno(ret); | |
ba937127 | 2096 | goto cleanup_nolock; |
cf1d6c76 TY |
2097 | } |
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 | } | |
2170 | cleanup: | |
2171 | up_write(&OCFS2_I(inode)->ip_xattr_sem); | |
2172 | ocfs2_inode_unlock(inode, 1); | |
ba937127 | 2173 | cleanup_nolock: |
cf1d6c76 TY |
2174 | brelse(di_bh); |
2175 | brelse(xbs.xattr_bh); | |
ba937127 | 2176 | ocfs2_xattr_bucket_free(xbs.bucket); |
cf1d6c76 TY |
2177 | |
2178 | return ret; | |
2179 | } | |
2180 | ||
0c044f0b TM |
2181 | /* |
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 | */ | |
2186 | static 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); | |
2241 | out: | |
2242 | brelse(eb_bh); | |
2243 | return ret; | |
2244 | } | |
2245 | ||
2246 | typedef int (xattr_bucket_func)(struct inode *inode, | |
2247 | struct ocfs2_xattr_bucket *bucket, | |
2248 | void *para); | |
2249 | ||
589dc260 TM |
2250 | static int ocfs2_find_xe_in_bucket(struct inode *inode, |
2251 | struct buffer_head *header_bh, | |
2252 | 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; | |
2259 | struct ocfs2_xattr_header *xh = | |
2260 | (struct ocfs2_xattr_header *)header_bh->b_data; | |
2261 | size_t name_len = strlen(name); | |
2262 | struct ocfs2_xattr_entry *xe = NULL; | |
2263 | struct buffer_head *name_bh = NULL; | |
2264 | char *xe_name; | |
2265 | ||
2266 | /* | |
2267 | * We don't use binary search in the bucket because there | |
2268 | * may be multiple entries with the same name hash. | |
2269 | */ | |
2270 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { | |
2271 | xe = &xh->xh_entries[i]; | |
2272 | ||
2273 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) | |
2274 | continue; | |
2275 | else if (name_hash < le32_to_cpu(xe->xe_name_hash)) | |
2276 | break; | |
2277 | ||
2278 | cmp = name_index - ocfs2_xattr_get_type(xe); | |
2279 | if (!cmp) | |
2280 | cmp = name_len - xe->xe_name_len; | |
2281 | if (cmp) | |
2282 | continue; | |
2283 | ||
2284 | ret = ocfs2_xattr_bucket_get_name_value(inode, | |
2285 | xh, | |
2286 | i, | |
2287 | &block_off, | |
2288 | &new_offset); | |
2289 | if (ret) { | |
2290 | mlog_errno(ret); | |
2291 | break; | |
2292 | } | |
2293 | ||
0fcaa56a JB |
2294 | ret = ocfs2_read_block(inode, header_bh->b_blocknr + block_off, |
2295 | &name_bh); | |
589dc260 TM |
2296 | if (ret) { |
2297 | mlog_errno(ret); | |
2298 | break; | |
2299 | } | |
2300 | xe_name = name_bh->b_data + new_offset; | |
2301 | ||
2302 | cmp = memcmp(name, xe_name, name_len); | |
2303 | brelse(name_bh); | |
2304 | name_bh = NULL; | |
2305 | ||
2306 | if (cmp == 0) { | |
2307 | *xe_index = i; | |
2308 | *found = 1; | |
2309 | ret = 0; | |
2310 | break; | |
2311 | } | |
2312 | } | |
2313 | ||
2314 | return ret; | |
2315 | } | |
2316 | ||
2317 | /* | |
2318 | * Find the specified xattr entry in a series of buckets. | |
2319 | * This series start from p_blkno and last for num_clusters. | |
2320 | * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains | |
2321 | * the num of the valid buckets. | |
2322 | * | |
2323 | * Return the buffer_head this xattr should reside in. And if the xattr's | |
2324 | * hash is in the gap of 2 buckets, return the lower bucket. | |
2325 | */ | |
2326 | static int ocfs2_xattr_bucket_find(struct inode *inode, | |
2327 | int name_index, | |
2328 | const char *name, | |
2329 | u32 name_hash, | |
2330 | u64 p_blkno, | |
2331 | u32 first_hash, | |
2332 | u32 num_clusters, | |
2333 | struct ocfs2_xattr_search *xs) | |
2334 | { | |
2335 | int ret, found = 0; | |
2336 | struct buffer_head *bh = NULL; | |
2337 | struct buffer_head *lower_bh = NULL; | |
2338 | struct ocfs2_xattr_header *xh = NULL; | |
2339 | struct ocfs2_xattr_entry *xe = NULL; | |
2340 | u16 index = 0; | |
2341 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
2342 | int low_bucket = 0, bucket, high_bucket; | |
2343 | u32 last_hash; | |
2344 | u64 blkno; | |
2345 | ||
0fcaa56a | 2346 | ret = ocfs2_read_block(inode, p_blkno, &bh); |
589dc260 TM |
2347 | if (ret) { |
2348 | mlog_errno(ret); | |
2349 | goto out; | |
2350 | } | |
2351 | ||
2352 | xh = (struct ocfs2_xattr_header *)bh->b_data; | |
2353 | high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1; | |
2354 | ||
2355 | while (low_bucket <= high_bucket) { | |
2356 | brelse(bh); | |
2357 | bh = NULL; | |
2358 | bucket = (low_bucket + high_bucket) / 2; | |
2359 | ||
2360 | blkno = p_blkno + bucket * blk_per_bucket; | |
2361 | ||
0fcaa56a | 2362 | ret = ocfs2_read_block(inode, blkno, &bh); |
589dc260 TM |
2363 | if (ret) { |
2364 | mlog_errno(ret); | |
2365 | goto out; | |
2366 | } | |
2367 | ||
2368 | xh = (struct ocfs2_xattr_header *)bh->b_data; | |
2369 | xe = &xh->xh_entries[0]; | |
2370 | if (name_hash < le32_to_cpu(xe->xe_name_hash)) { | |
2371 | high_bucket = bucket - 1; | |
2372 | continue; | |
2373 | } | |
2374 | ||
2375 | /* | |
2376 | * Check whether the hash of the last entry in our | |
5a095611 TM |
2377 | * bucket is larger than the search one. for an empty |
2378 | * bucket, the last one is also the first one. | |
589dc260 | 2379 | */ |
5a095611 TM |
2380 | if (xh->xh_count) |
2381 | xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1]; | |
2382 | ||
589dc260 TM |
2383 | last_hash = le32_to_cpu(xe->xe_name_hash); |
2384 | ||
2385 | /* record lower_bh which may be the insert place. */ | |
2386 | brelse(lower_bh); | |
2387 | lower_bh = bh; | |
2388 | bh = NULL; | |
2389 | ||
2390 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) { | |
2391 | low_bucket = bucket + 1; | |
2392 | continue; | |
2393 | } | |
2394 | ||
2395 | /* the searched xattr should reside in this bucket if exists. */ | |
2396 | ret = ocfs2_find_xe_in_bucket(inode, lower_bh, | |
2397 | name_index, name, name_hash, | |
2398 | &index, &found); | |
2399 | if (ret) { | |
2400 | mlog_errno(ret); | |
2401 | goto out; | |
2402 | } | |
2403 | break; | |
2404 | } | |
2405 | ||
2406 | /* | |
2407 | * Record the bucket we have found. | |
2408 | * When the xattr's hash value is in the gap of 2 buckets, we will | |
2409 | * always set it to the previous bucket. | |
2410 | */ | |
2411 | if (!lower_bh) { | |
2412 | /* | |
2413 | * We can't find any bucket whose first name_hash is less | |
2414 | * than the find name_hash. | |
2415 | */ | |
2416 | BUG_ON(bh->b_blocknr != p_blkno); | |
2417 | lower_bh = bh; | |
2418 | bh = NULL; | |
2419 | } | |
ba937127 | 2420 | xs->bucket->bu_bhs[0] = lower_bh; |
589dc260 TM |
2421 | lower_bh = NULL; |
2422 | ||
ba937127 JB |
2423 | xs->header = bucket_xh(xs->bucket); |
2424 | xs->base = bucket_block(xs->bucket, 0); | |
589dc260 TM |
2425 | xs->end = xs->base + inode->i_sb->s_blocksize; |
2426 | ||
2427 | if (found) { | |
2428 | /* | |
2429 | * If we have found the xattr enty, read all the blocks in | |
2430 | * this bucket. | |
2431 | */ | |
ba937127 JB |
2432 | ret = ocfs2_read_blocks(inode, bucket_blkno(xs->bucket) + 1, |
2433 | blk_per_bucket - 1, &xs->bucket->bu_bhs[1], | |
1efd47f8 | 2434 | 0); |
589dc260 TM |
2435 | if (ret) { |
2436 | mlog_errno(ret); | |
2437 | goto out; | |
2438 | } | |
2439 | ||
2440 | xs->here = &xs->header->xh_entries[index]; | |
2441 | mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name, | |
ba937127 | 2442 | (unsigned long long)bucket_blkno(xs->bucket), index); |
589dc260 TM |
2443 | } else |
2444 | ret = -ENODATA; | |
2445 | ||
2446 | out: | |
2447 | brelse(bh); | |
2448 | brelse(lower_bh); | |
2449 | return ret; | |
2450 | } | |
2451 | ||
2452 | static int ocfs2_xattr_index_block_find(struct inode *inode, | |
2453 | struct buffer_head *root_bh, | |
2454 | int name_index, | |
2455 | const char *name, | |
2456 | struct ocfs2_xattr_search *xs) | |
2457 | { | |
2458 | int ret; | |
2459 | struct ocfs2_xattr_block *xb = | |
2460 | (struct ocfs2_xattr_block *)root_bh->b_data; | |
2461 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; | |
2462 | struct ocfs2_extent_list *el = &xb_root->xt_list; | |
2463 | u64 p_blkno = 0; | |
2464 | u32 first_hash, num_clusters = 0; | |
2057e5c6 | 2465 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
589dc260 TM |
2466 | |
2467 | if (le16_to_cpu(el->l_next_free_rec) == 0) | |
2468 | return -ENODATA; | |
2469 | ||
2470 | mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n", | |
2471 | name, name_hash, name_index); | |
2472 | ||
2473 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash, | |
2474 | &num_clusters, el); | |
2475 | if (ret) { | |
2476 | mlog_errno(ret); | |
2477 | goto out; | |
2478 | } | |
2479 | ||
2480 | BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); | |
2481 | ||
2482 | mlog(0, "find xattr extent rec %u clusters from %llu, the first hash " | |
de29c085 MF |
2483 | "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno, |
2484 | first_hash); | |
589dc260 TM |
2485 | |
2486 | ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, | |
2487 | p_blkno, first_hash, num_clusters, xs); | |
2488 | ||
2489 | out: | |
2490 | return ret; | |
2491 | } | |
2492 | ||
0c044f0b TM |
2493 | static int ocfs2_iterate_xattr_buckets(struct inode *inode, |
2494 | u64 blkno, | |
2495 | u32 clusters, | |
2496 | xattr_bucket_func *func, | |
2497 | void *para) | |
2498 | { | |
6dde41d9 | 2499 | int i, ret = 0; |
0c044f0b TM |
2500 | u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); |
2501 | u32 num_buckets = clusters * bpc; | |
ba937127 | 2502 | struct ocfs2_xattr_bucket *bucket; |
0c044f0b | 2503 | |
ba937127 JB |
2504 | bucket = ocfs2_xattr_bucket_new(inode); |
2505 | if (!bucket) { | |
2506 | mlog_errno(-ENOMEM); | |
2507 | return -ENOMEM; | |
2508 | } | |
0c044f0b TM |
2509 | |
2510 | mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n", | |
de29c085 | 2511 | clusters, (unsigned long long)blkno); |
0c044f0b | 2512 | |
ba937127 JB |
2513 | for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) { |
2514 | ret = ocfs2_read_xattr_bucket(bucket, blkno); | |
0c044f0b TM |
2515 | if (ret) { |
2516 | mlog_errno(ret); | |
ba937127 | 2517 | break; |
0c044f0b TM |
2518 | } |
2519 | ||
0c044f0b TM |
2520 | /* |
2521 | * The real bucket num in this series of blocks is stored | |
2522 | * in the 1st bucket. | |
2523 | */ | |
2524 | if (i == 0) | |
ba937127 | 2525 | num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets); |
0c044f0b | 2526 | |
de29c085 MF |
2527 | mlog(0, "iterating xattr bucket %llu, first hash %u\n", |
2528 | (unsigned long long)blkno, | |
ba937127 | 2529 | le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash)); |
0c044f0b | 2530 | if (func) { |
ba937127 JB |
2531 | ret = func(inode, bucket, para); |
2532 | if (ret) | |
0c044f0b | 2533 | mlog_errno(ret); |
ba937127 | 2534 | /* Fall through to bucket_relse() */ |
0c044f0b TM |
2535 | } |
2536 | ||
ba937127 JB |
2537 | ocfs2_xattr_bucket_relse(bucket); |
2538 | if (ret) | |
2539 | break; | |
0c044f0b TM |
2540 | } |
2541 | ||
ba937127 | 2542 | ocfs2_xattr_bucket_free(bucket); |
0c044f0b TM |
2543 | return ret; |
2544 | } | |
2545 | ||
2546 | struct ocfs2_xattr_tree_list { | |
2547 | char *buffer; | |
2548 | size_t buffer_size; | |
936b8834 | 2549 | size_t result; |
0c044f0b TM |
2550 | }; |
2551 | ||
2552 | static int ocfs2_xattr_bucket_get_name_value(struct inode *inode, | |
2553 | struct ocfs2_xattr_header *xh, | |
2554 | int index, | |
2555 | int *block_off, | |
2556 | int *new_offset) | |
2557 | { | |
2558 | u16 name_offset; | |
2559 | ||
2560 | if (index < 0 || index >= le16_to_cpu(xh->xh_count)) | |
2561 | return -EINVAL; | |
2562 | ||
2563 | name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset); | |
2564 | ||
2565 | *block_off = name_offset >> inode->i_sb->s_blocksize_bits; | |
2566 | *new_offset = name_offset % inode->i_sb->s_blocksize; | |
2567 | ||
2568 | return 0; | |
2569 | } | |
2570 | ||
2571 | static int ocfs2_list_xattr_bucket(struct inode *inode, | |
2572 | struct ocfs2_xattr_bucket *bucket, | |
2573 | void *para) | |
2574 | { | |
936b8834 | 2575 | int ret = 0, type; |
0c044f0b | 2576 | struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para; |
0c044f0b | 2577 | int i, block_off, new_offset; |
936b8834 | 2578 | const char *prefix, *name; |
0c044f0b | 2579 | |
3e632946 JB |
2580 | for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) { |
2581 | struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i]; | |
936b8834 TM |
2582 | type = ocfs2_xattr_get_type(entry); |
2583 | prefix = ocfs2_xattr_prefix(type); | |
0c044f0b | 2584 | |
936b8834 | 2585 | if (prefix) { |
0c044f0b | 2586 | ret = ocfs2_xattr_bucket_get_name_value(inode, |
3e632946 | 2587 | bucket_xh(bucket), |
0c044f0b TM |
2588 | i, |
2589 | &block_off, | |
2590 | &new_offset); | |
2591 | if (ret) | |
2592 | break; | |
936b8834 | 2593 | |
51def39f | 2594 | name = (const char *)bucket_block(bucket, block_off) + |
936b8834 TM |
2595 | new_offset; |
2596 | ret = ocfs2_xattr_list_entry(xl->buffer, | |
2597 | xl->buffer_size, | |
2598 | &xl->result, | |
2599 | prefix, name, | |
2600 | entry->xe_name_len); | |
2601 | if (ret) | |
2602 | break; | |
0c044f0b TM |
2603 | } |
2604 | } | |
2605 | ||
2606 | return ret; | |
2607 | } | |
2608 | ||
2609 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, | |
2610 | struct ocfs2_xattr_tree_root *xt, | |
2611 | char *buffer, | |
2612 | size_t buffer_size) | |
2613 | { | |
2614 | struct ocfs2_extent_list *el = &xt->xt_list; | |
2615 | int ret = 0; | |
2616 | u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0; | |
2617 | u64 p_blkno = 0; | |
2618 | struct ocfs2_xattr_tree_list xl = { | |
2619 | .buffer = buffer, | |
2620 | .buffer_size = buffer_size, | |
936b8834 | 2621 | .result = 0, |
0c044f0b TM |
2622 | }; |
2623 | ||
2624 | if (le16_to_cpu(el->l_next_free_rec) == 0) | |
2625 | return 0; | |
2626 | ||
2627 | while (name_hash > 0) { | |
2628 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, | |
2629 | &e_cpos, &num_clusters, el); | |
2630 | if (ret) { | |
2631 | mlog_errno(ret); | |
2632 | goto out; | |
2633 | } | |
2634 | ||
2635 | ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters, | |
2636 | ocfs2_list_xattr_bucket, | |
2637 | &xl); | |
2638 | if (ret) { | |
2639 | mlog_errno(ret); | |
2640 | goto out; | |
2641 | } | |
2642 | ||
2643 | if (e_cpos == 0) | |
2644 | break; | |
2645 | ||
2646 | name_hash = e_cpos - 1; | |
2647 | } | |
2648 | ||
936b8834 | 2649 | ret = xl.result; |
0c044f0b TM |
2650 | out: |
2651 | return ret; | |
2652 | } | |
01225596 TM |
2653 | |
2654 | static int cmp_xe(const void *a, const void *b) | |
2655 | { | |
2656 | const struct ocfs2_xattr_entry *l = a, *r = b; | |
2657 | u32 l_hash = le32_to_cpu(l->xe_name_hash); | |
2658 | u32 r_hash = le32_to_cpu(r->xe_name_hash); | |
2659 | ||
2660 | if (l_hash > r_hash) | |
2661 | return 1; | |
2662 | if (l_hash < r_hash) | |
2663 | return -1; | |
2664 | return 0; | |
2665 | } | |
2666 | ||
2667 | static void swap_xe(void *a, void *b, int size) | |
2668 | { | |
2669 | struct ocfs2_xattr_entry *l = a, *r = b, tmp; | |
2670 | ||
2671 | tmp = *l; | |
2672 | memcpy(l, r, sizeof(struct ocfs2_xattr_entry)); | |
2673 | memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry)); | |
2674 | } | |
2675 | ||
2676 | /* | |
2677 | * When the ocfs2_xattr_block is filled up, new bucket will be created | |
2678 | * and all the xattr entries will be moved to the new bucket. | |
2679 | * Note: we need to sort the entries since they are not saved in order | |
2680 | * in the ocfs2_xattr_block. | |
2681 | */ | |
2682 | static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode, | |
2683 | struct buffer_head *xb_bh, | |
2684 | struct buffer_head *xh_bh, | |
2685 | struct buffer_head *data_bh) | |
2686 | { | |
2687 | int i, blocksize = inode->i_sb->s_blocksize; | |
2688 | u16 offset, size, off_change; | |
2689 | struct ocfs2_xattr_entry *xe; | |
2690 | struct ocfs2_xattr_block *xb = | |
2691 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
2692 | struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header; | |
2693 | struct ocfs2_xattr_header *xh = | |
2694 | (struct ocfs2_xattr_header *)xh_bh->b_data; | |
2695 | u16 count = le16_to_cpu(xb_xh->xh_count); | |
2696 | char *target = xh_bh->b_data, *src = xb_bh->b_data; | |
2697 | ||
2698 | mlog(0, "cp xattr from block %llu to bucket %llu\n", | |
2699 | (unsigned long long)xb_bh->b_blocknr, | |
2700 | (unsigned long long)xh_bh->b_blocknr); | |
2701 | ||
2702 | memset(xh_bh->b_data, 0, blocksize); | |
2703 | if (data_bh) | |
2704 | memset(data_bh->b_data, 0, blocksize); | |
2705 | /* | |
2706 | * Since the xe_name_offset is based on ocfs2_xattr_header, | |
2707 | * there is a offset change corresponding to the change of | |
2708 | * ocfs2_xattr_header's position. | |
2709 | */ | |
2710 | off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); | |
2711 | xe = &xb_xh->xh_entries[count - 1]; | |
2712 | offset = le16_to_cpu(xe->xe_name_offset) + off_change; | |
2713 | size = blocksize - offset; | |
2714 | ||
2715 | /* copy all the names and values. */ | |
2716 | if (data_bh) | |
2717 | target = data_bh->b_data; | |
2718 | memcpy(target + offset, src + offset, size); | |
2719 | ||
2720 | /* Init new header now. */ | |
2721 | xh->xh_count = xb_xh->xh_count; | |
2722 | xh->xh_num_buckets = cpu_to_le16(1); | |
2723 | xh->xh_name_value_len = cpu_to_le16(size); | |
2724 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size); | |
2725 | ||
2726 | /* copy all the entries. */ | |
2727 | target = xh_bh->b_data; | |
2728 | offset = offsetof(struct ocfs2_xattr_header, xh_entries); | |
2729 | size = count * sizeof(struct ocfs2_xattr_entry); | |
2730 | memcpy(target + offset, (char *)xb_xh + offset, size); | |
2731 | ||
2732 | /* Change the xe offset for all the xe because of the move. */ | |
2733 | off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize + | |
2734 | offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); | |
2735 | for (i = 0; i < count; i++) | |
2736 | le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change); | |
2737 | ||
2738 | mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n", | |
2739 | offset, size, off_change); | |
2740 | ||
2741 | sort(target + offset, count, sizeof(struct ocfs2_xattr_entry), | |
2742 | cmp_xe, swap_xe); | |
2743 | } | |
2744 | ||
2745 | /* | |
2746 | * After we move xattr from block to index btree, we have to | |
2747 | * update ocfs2_xattr_search to the new xe and base. | |
2748 | * | |
2749 | * When the entry is in xattr block, xattr_bh indicates the storage place. | |
2750 | * While if the entry is in index b-tree, "bucket" indicates the | |
2751 | * real place of the xattr. | |
2752 | */ | |
2753 | static int ocfs2_xattr_update_xattr_search(struct inode *inode, | |
2754 | struct ocfs2_xattr_search *xs, | |
2755 | struct buffer_head *old_bh, | |
2756 | struct buffer_head *new_bh) | |
2757 | { | |
2758 | int ret = 0; | |
2759 | char *buf = old_bh->b_data; | |
2760 | struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf; | |
2761 | struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header; | |
2762 | int i, blocksize = inode->i_sb->s_blocksize; | |
2763 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
2764 | ||
ba937127 | 2765 | xs->bucket->bu_bhs[0] = new_bh; |
01225596 | 2766 | get_bh(new_bh); |
ba937127 | 2767 | xs->header = bucket_xh(xs->bucket); |
01225596 TM |
2768 | |
2769 | xs->base = new_bh->b_data; | |
2770 | xs->end = xs->base + inode->i_sb->s_blocksize; | |
2771 | ||
2772 | if (!xs->not_found) { | |
2773 | if (OCFS2_XATTR_BUCKET_SIZE != blocksize) { | |
31d33073 | 2774 | ret = ocfs2_read_blocks(inode, |
ba937127 JB |
2775 | bucket_blkno(xs->bucket) + 1, |
2776 | blk_per_bucket - 1, &xs->bucket->bu_bhs[1], | |
1efd47f8 | 2777 | 0); |
01225596 TM |
2778 | if (ret) { |
2779 | mlog_errno(ret); | |
2780 | return ret; | |
2781 | } | |
2782 | ||
01225596 | 2783 | } |
83099bc6 TM |
2784 | i = xs->here - old_xh->xh_entries; |
2785 | xs->here = &xs->header->xh_entries[i]; | |
01225596 TM |
2786 | } |
2787 | ||
2788 | return ret; | |
2789 | } | |
2790 | ||
2791 | static int ocfs2_xattr_create_index_block(struct inode *inode, | |
2792 | struct ocfs2_xattr_search *xs) | |
2793 | { | |
2794 | int ret, credits = OCFS2_SUBALLOC_ALLOC; | |
2795 | u32 bit_off, len; | |
2796 | u64 blkno; | |
2797 | handle_t *handle; | |
2798 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
2799 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
2800 | struct ocfs2_alloc_context *data_ac; | |
2801 | struct buffer_head *xh_bh = NULL, *data_bh = NULL; | |
2802 | struct buffer_head *xb_bh = xs->xattr_bh; | |
2803 | struct ocfs2_xattr_block *xb = | |
2804 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
2805 | struct ocfs2_xattr_tree_root *xr; | |
2806 | u16 xb_flags = le16_to_cpu(xb->xb_flags); | |
2807 | u16 bpb = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
2808 | ||
2809 | mlog(0, "create xattr index block for %llu\n", | |
2810 | (unsigned long long)xb_bh->b_blocknr); | |
2811 | ||
2812 | BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); | |
2813 | ||
2814 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); | |
2815 | if (ret) { | |
2816 | mlog_errno(ret); | |
2817 | goto out; | |
2818 | } | |
2819 | ||
2820 | /* | |
2821 | * XXX: | |
2822 | * We can use this lock for now, and maybe move to a dedicated mutex | |
2823 | * if performance becomes a problem later. | |
2824 | */ | |
2825 | down_write(&oi->ip_alloc_sem); | |
2826 | ||
2827 | /* | |
2828 | * 3 more credits, one for xattr block update, one for the 1st block | |
2829 | * of the new xattr bucket and one for the value/data. | |
2830 | */ | |
2831 | credits += 3; | |
2832 | handle = ocfs2_start_trans(osb, credits); | |
2833 | if (IS_ERR(handle)) { | |
2834 | ret = PTR_ERR(handle); | |
2835 | mlog_errno(ret); | |
2836 | goto out_sem; | |
2837 | } | |
2838 | ||
2839 | ret = ocfs2_journal_access(handle, inode, xb_bh, | |
2840 | OCFS2_JOURNAL_ACCESS_WRITE); | |
2841 | if (ret) { | |
2842 | mlog_errno(ret); | |
2843 | goto out_commit; | |
2844 | } | |
2845 | ||
2846 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len); | |
2847 | if (ret) { | |
2848 | mlog_errno(ret); | |
2849 | goto out_commit; | |
2850 | } | |
2851 | ||
2852 | /* | |
2853 | * The bucket may spread in many blocks, and | |
2854 | * we will only touch the 1st block and the last block | |
2855 | * in the whole bucket(one for entry and one for data). | |
2856 | */ | |
2857 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); | |
2858 | ||
de29c085 MF |
2859 | mlog(0, "allocate 1 cluster from %llu to xattr block\n", |
2860 | (unsigned long long)blkno); | |
01225596 TM |
2861 | |
2862 | xh_bh = sb_getblk(inode->i_sb, blkno); | |
2863 | if (!xh_bh) { | |
2864 | ret = -EIO; | |
2865 | mlog_errno(ret); | |
2866 | goto out_commit; | |
2867 | } | |
2868 | ||
2869 | ocfs2_set_new_buffer_uptodate(inode, xh_bh); | |
2870 | ||
2871 | ret = ocfs2_journal_access(handle, inode, xh_bh, | |
2872 | OCFS2_JOURNAL_ACCESS_CREATE); | |
2873 | if (ret) { | |
2874 | mlog_errno(ret); | |
2875 | goto out_commit; | |
2876 | } | |
2877 | ||
2878 | if (bpb > 1) { | |
2879 | data_bh = sb_getblk(inode->i_sb, blkno + bpb - 1); | |
2880 | if (!data_bh) { | |
2881 | ret = -EIO; | |
2882 | mlog_errno(ret); | |
2883 | goto out_commit; | |
2884 | } | |
2885 | ||
2886 | ocfs2_set_new_buffer_uptodate(inode, data_bh); | |
2887 | ||
2888 | ret = ocfs2_journal_access(handle, inode, data_bh, | |
2889 | OCFS2_JOURNAL_ACCESS_CREATE); | |
2890 | if (ret) { | |
2891 | mlog_errno(ret); | |
2892 | goto out_commit; | |
2893 | } | |
2894 | } | |
2895 | ||
2896 | ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xh_bh, data_bh); | |
2897 | ||
2898 | ocfs2_journal_dirty(handle, xh_bh); | |
2899 | if (data_bh) | |
2900 | ocfs2_journal_dirty(handle, data_bh); | |
2901 | ||
bd60bd37 JB |
2902 | ret = ocfs2_xattr_update_xattr_search(inode, xs, xb_bh, xh_bh); |
2903 | if (ret) { | |
2904 | mlog_errno(ret); | |
2905 | goto out_commit; | |
2906 | } | |
01225596 TM |
2907 | |
2908 | /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */ | |
2909 | memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - | |
2910 | offsetof(struct ocfs2_xattr_block, xb_attrs)); | |
2911 | ||
2912 | xr = &xb->xb_attrs.xb_root; | |
2913 | xr->xt_clusters = cpu_to_le32(1); | |
2914 | xr->xt_last_eb_blk = 0; | |
2915 | xr->xt_list.l_tree_depth = 0; | |
2916 | xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); | |
2917 | xr->xt_list.l_next_free_rec = cpu_to_le16(1); | |
2918 | ||
2919 | xr->xt_list.l_recs[0].e_cpos = 0; | |
2920 | xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno); | |
2921 | xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); | |
2922 | ||
2923 | xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED); | |
2924 | ||
2925 | ret = ocfs2_journal_dirty(handle, xb_bh); | |
2926 | if (ret) { | |
2927 | mlog_errno(ret); | |
2928 | goto out_commit; | |
2929 | } | |
2930 | ||
2931 | out_commit: | |
2932 | ocfs2_commit_trans(osb, handle); | |
2933 | ||
2934 | out_sem: | |
2935 | up_write(&oi->ip_alloc_sem); | |
2936 | ||
2937 | out: | |
2938 | if (data_ac) | |
2939 | ocfs2_free_alloc_context(data_ac); | |
2940 | ||
2941 | brelse(xh_bh); | |
2942 | brelse(data_bh); | |
2943 | ||
2944 | return ret; | |
2945 | } | |
2946 | ||
2947 | static int cmp_xe_offset(const void *a, const void *b) | |
2948 | { | |
2949 | const struct ocfs2_xattr_entry *l = a, *r = b; | |
2950 | u32 l_name_offset = le16_to_cpu(l->xe_name_offset); | |
2951 | u32 r_name_offset = le16_to_cpu(r->xe_name_offset); | |
2952 | ||
2953 | if (l_name_offset < r_name_offset) | |
2954 | return 1; | |
2955 | if (l_name_offset > r_name_offset) | |
2956 | return -1; | |
2957 | return 0; | |
2958 | } | |
2959 | ||
2960 | /* | |
2961 | * defrag a xattr bucket if we find that the bucket has some | |
2962 | * holes beteen name/value pairs. | |
2963 | * We will move all the name/value pairs to the end of the bucket | |
2964 | * so that we can spare some space for insertion. | |
2965 | */ | |
2966 | static int ocfs2_defrag_xattr_bucket(struct inode *inode, | |
2967 | struct ocfs2_xattr_bucket *bucket) | |
2968 | { | |
2969 | int ret, i; | |
2970 | size_t end, offset, len, value_len; | |
2971 | struct ocfs2_xattr_header *xh; | |
2972 | char *entries, *buf, *bucket_buf = NULL; | |
9c7759aa | 2973 | u64 blkno = bucket_blkno(bucket); |
01225596 TM |
2974 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
2975 | u16 xh_free_start; | |
01225596 TM |
2976 | size_t blocksize = inode->i_sb->s_blocksize; |
2977 | handle_t *handle; | |
2978 | struct buffer_head **bhs; | |
2979 | struct ocfs2_xattr_entry *xe; | |
2980 | ||
2981 | bhs = kzalloc(sizeof(struct buffer_head *) * blk_per_bucket, | |
2982 | GFP_NOFS); | |
2983 | if (!bhs) | |
2984 | return -ENOMEM; | |
2985 | ||
1efd47f8 | 2986 | ret = ocfs2_read_blocks(inode, blkno, blk_per_bucket, bhs, 0); |
01225596 TM |
2987 | if (ret) |
2988 | goto out; | |
2989 | ||
2990 | /* | |
2991 | * In order to make the operation more efficient and generic, | |
2992 | * we copy all the blocks into a contiguous memory and do the | |
2993 | * defragment there, so if anything is error, we will not touch | |
2994 | * the real block. | |
2995 | */ | |
2996 | bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); | |
2997 | if (!bucket_buf) { | |
2998 | ret = -EIO; | |
2999 | goto out; | |
3000 | } | |
3001 | ||
3002 | buf = bucket_buf; | |
3003 | for (i = 0; i < blk_per_bucket; i++, buf += blocksize) | |
3004 | memcpy(buf, bhs[i]->b_data, blocksize); | |
3005 | ||
3006 | handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), blk_per_bucket); | |
3007 | if (IS_ERR(handle)) { | |
3008 | ret = PTR_ERR(handle); | |
3009 | handle = NULL; | |
3010 | mlog_errno(ret); | |
3011 | goto out; | |
3012 | } | |
3013 | ||
3014 | for (i = 0; i < blk_per_bucket; i++) { | |
3015 | ret = ocfs2_journal_access(handle, inode, bhs[i], | |
3016 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3017 | if (ret < 0) { | |
3018 | mlog_errno(ret); | |
3019 | goto commit; | |
3020 | } | |
3021 | } | |
3022 | ||
3023 | xh = (struct ocfs2_xattr_header *)bucket_buf; | |
3024 | entries = (char *)xh->xh_entries; | |
3025 | xh_free_start = le16_to_cpu(xh->xh_free_start); | |
3026 | ||
3027 | mlog(0, "adjust xattr bucket in %llu, count = %u, " | |
3028 | "xh_free_start = %u, xh_name_value_len = %u.\n", | |
de29c085 MF |
3029 | (unsigned long long)blkno, le16_to_cpu(xh->xh_count), |
3030 | xh_free_start, le16_to_cpu(xh->xh_name_value_len)); | |
01225596 TM |
3031 | |
3032 | /* | |
3033 | * sort all the entries by their offset. | |
3034 | * the largest will be the first, so that we can | |
3035 | * move them to the end one by one. | |
3036 | */ | |
3037 | sort(entries, le16_to_cpu(xh->xh_count), | |
3038 | sizeof(struct ocfs2_xattr_entry), | |
3039 | cmp_xe_offset, swap_xe); | |
3040 | ||
3041 | /* Move all name/values to the end of the bucket. */ | |
3042 | xe = xh->xh_entries; | |
3043 | end = OCFS2_XATTR_BUCKET_SIZE; | |
3044 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { | |
3045 | offset = le16_to_cpu(xe->xe_name_offset); | |
3046 | if (ocfs2_xattr_is_local(xe)) | |
3047 | value_len = OCFS2_XATTR_SIZE( | |
3048 | le64_to_cpu(xe->xe_value_size)); | |
3049 | else | |
3050 | value_len = OCFS2_XATTR_ROOT_SIZE; | |
3051 | len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len; | |
3052 | ||
3053 | /* | |
3054 | * We must make sure that the name/value pair | |
3055 | * exist in the same block. So adjust end to | |
3056 | * the previous block end if needed. | |
3057 | */ | |
3058 | if (((end - len) / blocksize != | |
3059 | (end - 1) / blocksize)) | |
3060 | end = end - end % blocksize; | |
3061 | ||
3062 | if (end > offset + len) { | |
3063 | memmove(bucket_buf + end - len, | |
3064 | bucket_buf + offset, len); | |
3065 | xe->xe_name_offset = cpu_to_le16(end - len); | |
3066 | } | |
3067 | ||
3068 | mlog_bug_on_msg(end < offset + len, "Defrag check failed for " | |
3069 | "bucket %llu\n", (unsigned long long)blkno); | |
3070 | ||
3071 | end -= len; | |
3072 | } | |
3073 | ||
3074 | mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for " | |
3075 | "bucket %llu\n", (unsigned long long)blkno); | |
3076 | ||
3077 | if (xh_free_start == end) | |
3078 | goto commit; | |
3079 | ||
3080 | memset(bucket_buf + xh_free_start, 0, end - xh_free_start); | |
3081 | xh->xh_free_start = cpu_to_le16(end); | |
3082 | ||
3083 | /* sort the entries by their name_hash. */ | |
3084 | sort(entries, le16_to_cpu(xh->xh_count), | |
3085 | sizeof(struct ocfs2_xattr_entry), | |
3086 | cmp_xe, swap_xe); | |
3087 | ||
3088 | buf = bucket_buf; | |
3089 | for (i = 0; i < blk_per_bucket; i++, buf += blocksize) { | |
3090 | memcpy(bhs[i]->b_data, buf, blocksize); | |
3091 | ocfs2_journal_dirty(handle, bhs[i]); | |
3092 | } | |
3093 | ||
3094 | commit: | |
3095 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
3096 | out: | |
3097 | ||
3098 | if (bhs) { | |
3099 | for (i = 0; i < blk_per_bucket; i++) | |
3100 | brelse(bhs[i]); | |
3101 | } | |
3102 | kfree(bhs); | |
3103 | ||
3104 | kfree(bucket_buf); | |
3105 | return ret; | |
3106 | } | |
3107 | ||
3108 | /* | |
3109 | * Move half nums of the xattr bucket in the previous cluster to this new | |
3110 | * cluster. We only touch the last cluster of the previous extend record. | |
3111 | * | |
3112 | * first_bh is the first buffer_head of a series of bucket in the same | |
3113 | * extent rec and header_bh is the header of one bucket in this cluster. | |
3114 | * They will be updated if we move the data header_bh contains to the new | |
3115 | * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster. | |
3116 | */ | |
3117 | static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode, | |
3118 | handle_t *handle, | |
3119 | struct buffer_head **first_bh, | |
3120 | struct buffer_head **header_bh, | |
3121 | u64 new_blkno, | |
3122 | u64 prev_blkno, | |
3123 | u32 num_clusters, | |
3124 | u32 *first_hash) | |
3125 | { | |
3126 | int i, ret, credits; | |
3127 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
3128 | int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); | |
3129 | int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); | |
3130 | int blocksize = inode->i_sb->s_blocksize; | |
3131 | struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL; | |
3132 | struct ocfs2_xattr_header *new_xh; | |
3133 | struct ocfs2_xattr_header *xh = | |
3134 | (struct ocfs2_xattr_header *)((*first_bh)->b_data); | |
3135 | ||
3136 | BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets); | |
3137 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize); | |
3138 | ||
3139 | prev_bh = *first_bh; | |
3140 | get_bh(prev_bh); | |
3141 | xh = (struct ocfs2_xattr_header *)prev_bh->b_data; | |
3142 | ||
3143 | prev_blkno += (num_clusters - 1) * bpc + bpc / 2; | |
3144 | ||
3145 | mlog(0, "move half of xattrs in cluster %llu to %llu\n", | |
de29c085 | 3146 | (unsigned long long)prev_blkno, (unsigned long long)new_blkno); |
01225596 TM |
3147 | |
3148 | /* | |
3149 | * We need to update the 1st half of the new cluster and | |
3150 | * 1 more for the update of the 1st bucket of the previous | |
3151 | * extent record. | |
3152 | */ | |
3153 | credits = bpc / 2 + 1; | |
3154 | ret = ocfs2_extend_trans(handle, credits); | |
3155 | if (ret) { | |
3156 | mlog_errno(ret); | |
3157 | goto out; | |
3158 | } | |
3159 | ||
3160 | ret = ocfs2_journal_access(handle, inode, prev_bh, | |
3161 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3162 | if (ret) { | |
3163 | mlog_errno(ret); | |
3164 | goto out; | |
3165 | } | |
3166 | ||
3167 | for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) { | |
3168 | old_bh = new_bh = NULL; | |
3169 | new_bh = sb_getblk(inode->i_sb, new_blkno); | |
3170 | if (!new_bh) { | |
3171 | ret = -EIO; | |
3172 | mlog_errno(ret); | |
3173 | goto out; | |
3174 | } | |
3175 | ||
3176 | ocfs2_set_new_buffer_uptodate(inode, new_bh); | |
3177 | ||
3178 | ret = ocfs2_journal_access(handle, inode, new_bh, | |
3179 | OCFS2_JOURNAL_ACCESS_CREATE); | |
3180 | if (ret < 0) { | |
3181 | mlog_errno(ret); | |
3182 | brelse(new_bh); | |
3183 | goto out; | |
3184 | } | |
3185 | ||
0fcaa56a | 3186 | ret = ocfs2_read_block(inode, prev_blkno, &old_bh); |
01225596 TM |
3187 | if (ret < 0) { |
3188 | mlog_errno(ret); | |
3189 | brelse(new_bh); | |
3190 | goto out; | |
3191 | } | |
3192 | ||
3193 | memcpy(new_bh->b_data, old_bh->b_data, blocksize); | |
3194 | ||
3195 | if (i == 0) { | |
3196 | new_xh = (struct ocfs2_xattr_header *)new_bh->b_data; | |
3197 | new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2); | |
3198 | ||
3199 | if (first_hash) | |
3200 | *first_hash = le32_to_cpu( | |
3201 | new_xh->xh_entries[0].xe_name_hash); | |
3202 | new_first_bh = new_bh; | |
3203 | get_bh(new_first_bh); | |
3204 | } | |
3205 | ||
3206 | ocfs2_journal_dirty(handle, new_bh); | |
3207 | ||
3208 | if (*header_bh == old_bh) { | |
3209 | brelse(*header_bh); | |
3210 | *header_bh = new_bh; | |
3211 | get_bh(*header_bh); | |
3212 | ||
3213 | brelse(*first_bh); | |
3214 | *first_bh = new_first_bh; | |
3215 | get_bh(*first_bh); | |
3216 | } | |
3217 | brelse(new_bh); | |
3218 | brelse(old_bh); | |
3219 | } | |
3220 | ||
3221 | le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2)); | |
3222 | ||
3223 | ocfs2_journal_dirty(handle, prev_bh); | |
3224 | out: | |
3225 | brelse(prev_bh); | |
3226 | brelse(new_first_bh); | |
3227 | return ret; | |
3228 | } | |
3229 | ||
01225596 | 3230 | /* |
80bcaf34 TM |
3231 | * Find the suitable pos when we divide a bucket into 2. |
3232 | * We have to make sure the xattrs with the same hash value exist | |
3233 | * in the same bucket. | |
3234 | * | |
3235 | * If this ocfs2_xattr_header covers more than one hash value, find a | |
3236 | * place where the hash value changes. Try to find the most even split. | |
3237 | * The most common case is that all entries have different hash values, | |
3238 | * and the first check we make will find a place to split. | |
3239 | */ | |
3240 | static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh) | |
3241 | { | |
3242 | struct ocfs2_xattr_entry *entries = xh->xh_entries; | |
3243 | int count = le16_to_cpu(xh->xh_count); | |
3244 | int delta, middle = count / 2; | |
3245 | ||
3246 | /* | |
3247 | * We start at the middle. Each step gets farther away in both | |
3248 | * directions. We therefore hit the change in hash value | |
3249 | * nearest to the middle. Note that this loop does not execute for | |
3250 | * count < 2. | |
3251 | */ | |
3252 | for (delta = 0; delta < middle; delta++) { | |
3253 | /* Let's check delta earlier than middle */ | |
3254 | if (cmp_xe(&entries[middle - delta - 1], | |
3255 | &entries[middle - delta])) | |
3256 | return middle - delta; | |
3257 | ||
3258 | /* For even counts, don't walk off the end */ | |
3259 | if ((middle + delta + 1) == count) | |
3260 | continue; | |
3261 | ||
3262 | /* Now try delta past middle */ | |
3263 | if (cmp_xe(&entries[middle + delta], | |
3264 | &entries[middle + delta + 1])) | |
3265 | return middle + delta + 1; | |
3266 | } | |
3267 | ||
3268 | /* Every entry had the same hash */ | |
3269 | return count; | |
3270 | } | |
3271 | ||
3272 | /* | |
3273 | * Move some xattrs in old bucket(blk) to new bucket(new_blk). | |
01225596 | 3274 | * first_hash will record the 1st hash of the new bucket. |
80bcaf34 TM |
3275 | * |
3276 | * Normally half of the xattrs will be moved. But we have to make | |
3277 | * sure that the xattrs with the same hash value are stored in the | |
3278 | * same bucket. If all the xattrs in this bucket have the same hash | |
3279 | * value, the new bucket will be initialized as an empty one and the | |
3280 | * first_hash will be initialized as (hash_value+1). | |
01225596 | 3281 | */ |
80bcaf34 TM |
3282 | static int ocfs2_divide_xattr_bucket(struct inode *inode, |
3283 | handle_t *handle, | |
3284 | u64 blk, | |
3285 | u64 new_blk, | |
3286 | u32 *first_hash, | |
3287 | int new_bucket_head) | |
01225596 TM |
3288 | { |
3289 | int ret, i; | |
80bcaf34 | 3290 | int count, start, len, name_value_len = 0, xe_len, name_offset = 0; |
ba937127 | 3291 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
01225596 TM |
3292 | struct ocfs2_xattr_header *xh; |
3293 | struct ocfs2_xattr_entry *xe; | |
3294 | int blocksize = inode->i_sb->s_blocksize; | |
3295 | ||
80bcaf34 | 3296 | mlog(0, "move some of xattrs from bucket %llu to %llu\n", |
de29c085 | 3297 | (unsigned long long)blk, (unsigned long long)new_blk); |
01225596 | 3298 | |
ba937127 JB |
3299 | s_bucket = ocfs2_xattr_bucket_new(inode); |
3300 | t_bucket = ocfs2_xattr_bucket_new(inode); | |
3301 | if (!s_bucket || !t_bucket) { | |
3302 | ret = -ENOMEM; | |
3303 | mlog_errno(ret); | |
3304 | goto out; | |
3305 | } | |
01225596 | 3306 | |
ba937127 | 3307 | ret = ocfs2_read_xattr_bucket(s_bucket, blk); |
01225596 TM |
3308 | if (ret) { |
3309 | mlog_errno(ret); | |
3310 | goto out; | |
3311 | } | |
3312 | ||
ba937127 | 3313 | ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket, |
1224be02 | 3314 | OCFS2_JOURNAL_ACCESS_WRITE); |
01225596 TM |
3315 | if (ret) { |
3316 | mlog_errno(ret); | |
3317 | goto out; | |
3318 | } | |
3319 | ||
784b816a JB |
3320 | /* |
3321 | * Even if !new_bucket_head, we're overwriting t_bucket. Thus, | |
3322 | * there's no need to read it. | |
3323 | */ | |
ba937127 | 3324 | ret = ocfs2_init_xattr_bucket(t_bucket, new_blk); |
01225596 TM |
3325 | if (ret) { |
3326 | mlog_errno(ret); | |
3327 | goto out; | |
3328 | } | |
3329 | ||
ba937127 | 3330 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
1224be02 JB |
3331 | new_bucket_head ? |
3332 | OCFS2_JOURNAL_ACCESS_CREATE : | |
3333 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3334 | if (ret) { | |
3335 | mlog_errno(ret); | |
3336 | goto out; | |
01225596 TM |
3337 | } |
3338 | ||
ba937127 | 3339 | xh = bucket_xh(s_bucket); |
80bcaf34 TM |
3340 | count = le16_to_cpu(xh->xh_count); |
3341 | start = ocfs2_xattr_find_divide_pos(xh); | |
3342 | ||
3343 | if (start == count) { | |
3344 | xe = &xh->xh_entries[start-1]; | |
3345 | ||
3346 | /* | |
3347 | * initialized a new empty bucket here. | |
3348 | * The hash value is set as one larger than | |
3349 | * that of the last entry in the previous bucket. | |
3350 | */ | |
ba937127 JB |
3351 | for (i = 0; i < t_bucket->bu_blocks; i++) |
3352 | memset(bucket_block(t_bucket, i), 0, blocksize); | |
80bcaf34 | 3353 | |
ba937127 | 3354 | xh = bucket_xh(t_bucket); |
80bcaf34 TM |
3355 | xh->xh_free_start = cpu_to_le16(blocksize); |
3356 | xh->xh_entries[0].xe_name_hash = xe->xe_name_hash; | |
3357 | le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1); | |
3358 | ||
3359 | goto set_num_buckets; | |
3360 | } | |
3361 | ||
01225596 | 3362 | /* copy the whole bucket to the new first. */ |
ba937127 | 3363 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
01225596 TM |
3364 | |
3365 | /* update the new bucket. */ | |
ba937127 | 3366 | xh = bucket_xh(t_bucket); |
01225596 TM |
3367 | |
3368 | /* | |
3369 | * Calculate the total name/value len and xh_free_start for | |
3370 | * the old bucket first. | |
3371 | */ | |
3372 | name_offset = OCFS2_XATTR_BUCKET_SIZE; | |
3373 | name_value_len = 0; | |
3374 | for (i = 0; i < start; i++) { | |
3375 | xe = &xh->xh_entries[i]; | |
3376 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); | |
3377 | if (ocfs2_xattr_is_local(xe)) | |
3378 | xe_len += | |
3379 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
3380 | else | |
3381 | xe_len += OCFS2_XATTR_ROOT_SIZE; | |
3382 | name_value_len += xe_len; | |
3383 | if (le16_to_cpu(xe->xe_name_offset) < name_offset) | |
3384 | name_offset = le16_to_cpu(xe->xe_name_offset); | |
3385 | } | |
3386 | ||
3387 | /* | |
3388 | * Now begin the modification to the new bucket. | |
3389 | * | |
3390 | * In the new bucket, We just move the xattr entry to the beginning | |
3391 | * and don't touch the name/value. So there will be some holes in the | |
3392 | * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is | |
3393 | * called. | |
3394 | */ | |
3395 | xe = &xh->xh_entries[start]; | |
3396 | len = sizeof(struct ocfs2_xattr_entry) * (count - start); | |
3397 | mlog(0, "mv xattr entry len %d from %d to %d\n", len, | |
ff1ec20e MF |
3398 | (int)((char *)xe - (char *)xh), |
3399 | (int)((char *)xh->xh_entries - (char *)xh)); | |
01225596 TM |
3400 | memmove((char *)xh->xh_entries, (char *)xe, len); |
3401 | xe = &xh->xh_entries[count - start]; | |
3402 | len = sizeof(struct ocfs2_xattr_entry) * start; | |
3403 | memset((char *)xe, 0, len); | |
3404 | ||
3405 | le16_add_cpu(&xh->xh_count, -start); | |
3406 | le16_add_cpu(&xh->xh_name_value_len, -name_value_len); | |
3407 | ||
3408 | /* Calculate xh_free_start for the new bucket. */ | |
3409 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); | |
3410 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { | |
3411 | xe = &xh->xh_entries[i]; | |
3412 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); | |
3413 | if (ocfs2_xattr_is_local(xe)) | |
3414 | xe_len += | |
3415 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
3416 | else | |
3417 | xe_len += OCFS2_XATTR_ROOT_SIZE; | |
3418 | if (le16_to_cpu(xe->xe_name_offset) < | |
3419 | le16_to_cpu(xh->xh_free_start)) | |
3420 | xh->xh_free_start = xe->xe_name_offset; | |
3421 | } | |
3422 | ||
80bcaf34 | 3423 | set_num_buckets: |
01225596 TM |
3424 | /* set xh->xh_num_buckets for the new xh. */ |
3425 | if (new_bucket_head) | |
3426 | xh->xh_num_buckets = cpu_to_le16(1); | |
3427 | else | |
3428 | xh->xh_num_buckets = 0; | |
3429 | ||
ba937127 | 3430 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
01225596 TM |
3431 | |
3432 | /* store the first_hash of the new bucket. */ | |
3433 | if (first_hash) | |
3434 | *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); | |
3435 | ||
3436 | /* | |
80bcaf34 TM |
3437 | * Now only update the 1st block of the old bucket. If we |
3438 | * just added a new empty bucket, there is no need to modify | |
3439 | * it. | |
01225596 | 3440 | */ |
80bcaf34 TM |
3441 | if (start == count) |
3442 | goto out; | |
3443 | ||
ba937127 | 3444 | xh = bucket_xh(s_bucket); |
01225596 TM |
3445 | memset(&xh->xh_entries[start], 0, |
3446 | sizeof(struct ocfs2_xattr_entry) * (count - start)); | |
3447 | xh->xh_count = cpu_to_le16(start); | |
3448 | xh->xh_free_start = cpu_to_le16(name_offset); | |
3449 | xh->xh_name_value_len = cpu_to_le16(name_value_len); | |
3450 | ||
ba937127 | 3451 | ocfs2_xattr_bucket_journal_dirty(handle, s_bucket); |
01225596 TM |
3452 | |
3453 | out: | |
ba937127 JB |
3454 | ocfs2_xattr_bucket_free(s_bucket); |
3455 | ocfs2_xattr_bucket_free(t_bucket); | |
01225596 TM |
3456 | |
3457 | return ret; | |
3458 | } | |
3459 | ||
3460 | /* | |
3461 | * Copy xattr from one bucket to another bucket. | |
3462 | * | |
3463 | * The caller must make sure that the journal transaction | |
3464 | * has enough space for journaling. | |
3465 | */ | |
3466 | static int ocfs2_cp_xattr_bucket(struct inode *inode, | |
3467 | handle_t *handle, | |
3468 | u64 s_blkno, | |
3469 | u64 t_blkno, | |
3470 | int t_is_new) | |
3471 | { | |
4980c6da | 3472 | int ret; |
ba937127 | 3473 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
01225596 TM |
3474 | |
3475 | BUG_ON(s_blkno == t_blkno); | |
3476 | ||
3477 | mlog(0, "cp bucket %llu to %llu, target is %d\n", | |
de29c085 MF |
3478 | (unsigned long long)s_blkno, (unsigned long long)t_blkno, |
3479 | t_is_new); | |
01225596 | 3480 | |
ba937127 JB |
3481 | s_bucket = ocfs2_xattr_bucket_new(inode); |
3482 | t_bucket = ocfs2_xattr_bucket_new(inode); | |
3483 | if (!s_bucket || !t_bucket) { | |
3484 | ret = -ENOMEM; | |
3485 | mlog_errno(ret); | |
3486 | goto out; | |
3487 | } | |
3488 | ||
3489 | ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno); | |
01225596 TM |
3490 | if (ret) |
3491 | goto out; | |
3492 | ||
784b816a JB |
3493 | /* |
3494 | * Even if !t_is_new, we're overwriting t_bucket. Thus, | |
3495 | * there's no need to read it. | |
3496 | */ | |
ba937127 | 3497 | ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno); |
01225596 TM |
3498 | if (ret) |
3499 | goto out; | |
3500 | ||
ba937127 | 3501 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
1224be02 JB |
3502 | t_is_new ? |
3503 | OCFS2_JOURNAL_ACCESS_CREATE : | |
3504 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3505 | if (ret) | |
3506 | goto out; | |
01225596 | 3507 | |
ba937127 JB |
3508 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
3509 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); | |
01225596 TM |
3510 | |
3511 | out: | |
ba937127 JB |
3512 | ocfs2_xattr_bucket_free(t_bucket); |
3513 | ocfs2_xattr_bucket_free(s_bucket); | |
01225596 TM |
3514 | |
3515 | return ret; | |
3516 | } | |
3517 | ||
3518 | /* | |
3519 | * Copy one xattr cluster from src_blk to to_blk. | |
3520 | * The to_blk will become the first bucket header of the cluster, so its | |
3521 | * xh_num_buckets will be initialized as the bucket num in the cluster. | |
3522 | */ | |
3523 | static int ocfs2_cp_xattr_cluster(struct inode *inode, | |
3524 | handle_t *handle, | |
3525 | struct buffer_head *first_bh, | |
3526 | u64 src_blk, | |
3527 | u64 to_blk, | |
3528 | u32 *first_hash) | |
3529 | { | |
3530 | int i, ret, credits; | |
3531 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
3532 | int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); | |
3533 | int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); | |
3534 | struct buffer_head *bh = NULL; | |
3535 | struct ocfs2_xattr_header *xh; | |
3536 | u64 to_blk_start = to_blk; | |
3537 | ||
de29c085 MF |
3538 | mlog(0, "cp xattrs from cluster %llu to %llu\n", |
3539 | (unsigned long long)src_blk, (unsigned long long)to_blk); | |
01225596 TM |
3540 | |
3541 | /* | |
3542 | * We need to update the new cluster and 1 more for the update of | |
3543 | * the 1st bucket of the previous extent rec. | |
3544 | */ | |
3545 | credits = bpc + 1; | |
3546 | ret = ocfs2_extend_trans(handle, credits); | |
3547 | if (ret) { | |
3548 | mlog_errno(ret); | |
3549 | goto out; | |
3550 | } | |
3551 | ||
3552 | ret = ocfs2_journal_access(handle, inode, first_bh, | |
3553 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3554 | if (ret) { | |
3555 | mlog_errno(ret); | |
3556 | goto out; | |
3557 | } | |
3558 | ||
3559 | for (i = 0; i < num_buckets; i++) { | |
3560 | ret = ocfs2_cp_xattr_bucket(inode, handle, | |
3561 | src_blk, to_blk, 1); | |
3562 | if (ret) { | |
3563 | mlog_errno(ret); | |
3564 | goto out; | |
3565 | } | |
3566 | ||
3567 | src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
3568 | to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
3569 | } | |
3570 | ||
3571 | /* update the old bucket header. */ | |
3572 | xh = (struct ocfs2_xattr_header *)first_bh->b_data; | |
3573 | le16_add_cpu(&xh->xh_num_buckets, -num_buckets); | |
3574 | ||
3575 | ocfs2_journal_dirty(handle, first_bh); | |
3576 | ||
3577 | /* update the new bucket header. */ | |
0fcaa56a | 3578 | ret = ocfs2_read_block(inode, to_blk_start, &bh); |
01225596 TM |
3579 | if (ret < 0) { |
3580 | mlog_errno(ret); | |
3581 | goto out; | |
3582 | } | |
3583 | ||
3584 | ret = ocfs2_journal_access(handle, inode, bh, | |
3585 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3586 | if (ret) { | |
3587 | mlog_errno(ret); | |
3588 | goto out; | |
3589 | } | |
3590 | ||
3591 | xh = (struct ocfs2_xattr_header *)bh->b_data; | |
3592 | xh->xh_num_buckets = cpu_to_le16(num_buckets); | |
3593 | ||
3594 | ocfs2_journal_dirty(handle, bh); | |
3595 | ||
3596 | if (first_hash) | |
3597 | *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); | |
3598 | out: | |
3599 | brelse(bh); | |
3600 | return ret; | |
3601 | } | |
3602 | ||
3603 | /* | |
80bcaf34 | 3604 | * Move some xattrs in this cluster to the new cluster. |
01225596 TM |
3605 | * This function should only be called when bucket size == cluster size. |
3606 | * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. | |
3607 | */ | |
80bcaf34 TM |
3608 | static int ocfs2_divide_xattr_cluster(struct inode *inode, |
3609 | handle_t *handle, | |
3610 | u64 prev_blk, | |
3611 | u64 new_blk, | |
3612 | u32 *first_hash) | |
01225596 TM |
3613 | { |
3614 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
3615 | int ret, credits = 2 * blk_per_bucket; | |
3616 | ||
3617 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize); | |
3618 | ||
3619 | ret = ocfs2_extend_trans(handle, credits); | |
3620 | if (ret) { | |
3621 | mlog_errno(ret); | |
3622 | return ret; | |
3623 | } | |
3624 | ||
3625 | /* Move half of the xattr in start_blk to the next bucket. */ | |
80bcaf34 TM |
3626 | return ocfs2_divide_xattr_bucket(inode, handle, prev_blk, |
3627 | new_blk, first_hash, 1); | |
01225596 TM |
3628 | } |
3629 | ||
3630 | /* | |
3631 | * Move some xattrs from the old cluster to the new one since they are not | |
3632 | * contiguous in ocfs2 xattr tree. | |
3633 | * | |
3634 | * new_blk starts a new separate cluster, and we will move some xattrs from | |
3635 | * prev_blk to it. v_start will be set as the first name hash value in this | |
3636 | * new cluster so that it can be used as e_cpos during tree insertion and | |
3637 | * don't collide with our original b-tree operations. first_bh and header_bh | |
3638 | * will also be updated since they will be used in ocfs2_extend_xattr_bucket | |
3639 | * to extend the insert bucket. | |
3640 | * | |
3641 | * The problem is how much xattr should we move to the new one and when should | |
3642 | * we update first_bh and header_bh? | |
3643 | * 1. If cluster size > bucket size, that means the previous cluster has more | |
3644 | * than 1 bucket, so just move half nums of bucket into the new cluster and | |
3645 | * update the first_bh and header_bh if the insert bucket has been moved | |
3646 | * to the new cluster. | |
3647 | * 2. If cluster_size == bucket_size: | |
3648 | * a) If the previous extent rec has more than one cluster and the insert | |
3649 | * place isn't in the last cluster, copy the entire last cluster to the | |
3650 | * new one. This time, we don't need to upate the first_bh and header_bh | |
3651 | * since they will not be moved into the new cluster. | |
3652 | * b) Otherwise, move the bottom half of the xattrs in the last cluster into | |
3653 | * the new one. And we set the extend flag to zero if the insert place is | |
3654 | * moved into the new allocated cluster since no extend is needed. | |
3655 | */ | |
3656 | static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode, | |
3657 | handle_t *handle, | |
3658 | struct buffer_head **first_bh, | |
3659 | struct buffer_head **header_bh, | |
3660 | u64 new_blk, | |
3661 | u64 prev_blk, | |
3662 | u32 prev_clusters, | |
3663 | u32 *v_start, | |
3664 | int *extend) | |
3665 | { | |
3666 | int ret = 0; | |
3667 | int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); | |
3668 | ||
3669 | mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n", | |
de29c085 MF |
3670 | (unsigned long long)prev_blk, prev_clusters, |
3671 | (unsigned long long)new_blk); | |
01225596 TM |
3672 | |
3673 | if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) | |
3674 | ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, | |
3675 | handle, | |
3676 | first_bh, | |
3677 | header_bh, | |
3678 | new_blk, | |
3679 | prev_blk, | |
3680 | prev_clusters, | |
3681 | v_start); | |
3682 | else { | |
3683 | u64 last_blk = prev_blk + bpc * (prev_clusters - 1); | |
3684 | ||
3685 | if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk) | |
3686 | ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh, | |
3687 | last_blk, new_blk, | |
3688 | v_start); | |
3689 | else { | |
80bcaf34 TM |
3690 | ret = ocfs2_divide_xattr_cluster(inode, handle, |
3691 | last_blk, new_blk, | |
3692 | v_start); | |
01225596 TM |
3693 | |
3694 | if ((*header_bh)->b_blocknr == last_blk && extend) | |
3695 | *extend = 0; | |
3696 | } | |
3697 | } | |
3698 | ||
3699 | return ret; | |
3700 | } | |
3701 | ||
3702 | /* | |
3703 | * Add a new cluster for xattr storage. | |
3704 | * | |
3705 | * If the new cluster is contiguous with the previous one, it will be | |
3706 | * appended to the same extent record, and num_clusters will be updated. | |
3707 | * If not, we will insert a new extent for it and move some xattrs in | |
3708 | * the last cluster into the new allocated one. | |
3709 | * We also need to limit the maximum size of a btree leaf, otherwise we'll | |
3710 | * lose the benefits of hashing because we'll have to search large leaves. | |
3711 | * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize, | |
3712 | * if it's bigger). | |
3713 | * | |
3714 | * first_bh is the first block of the previous extent rec and header_bh | |
3715 | * indicates the bucket we will insert the new xattrs. They will be updated | |
3716 | * when the header_bh is moved into the new cluster. | |
3717 | */ | |
3718 | static int ocfs2_add_new_xattr_cluster(struct inode *inode, | |
3719 | struct buffer_head *root_bh, | |
3720 | struct buffer_head **first_bh, | |
3721 | struct buffer_head **header_bh, | |
3722 | u32 *num_clusters, | |
3723 | u32 prev_cpos, | |
3724 | u64 prev_blkno, | |
3725 | int *extend) | |
3726 | { | |
3727 | int ret, credits; | |
3728 | u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); | |
3729 | u32 prev_clusters = *num_clusters; | |
3730 | u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; | |
3731 | u64 block; | |
3732 | handle_t *handle = NULL; | |
3733 | struct ocfs2_alloc_context *data_ac = NULL; | |
3734 | struct ocfs2_alloc_context *meta_ac = NULL; | |
3735 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
f99b9b7c | 3736 | struct ocfs2_extent_tree et; |
01225596 TM |
3737 | |
3738 | mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " | |
3739 | "previous xattr blkno = %llu\n", | |
3740 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
de29c085 | 3741 | prev_cpos, (unsigned long long)prev_blkno); |
01225596 | 3742 | |
8d6220d6 | 3743 | ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh); |
f99b9b7c JB |
3744 | |
3745 | ret = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0, | |
3746 | &data_ac, &meta_ac); | |
01225596 TM |
3747 | if (ret) { |
3748 | mlog_errno(ret); | |
3749 | goto leave; | |
3750 | } | |
3751 | ||
f99b9b7c JB |
3752 | credits = ocfs2_calc_extend_credits(osb->sb, et.et_root_el, |
3753 | clusters_to_add); | |
01225596 TM |
3754 | handle = ocfs2_start_trans(osb, credits); |
3755 | if (IS_ERR(handle)) { | |
3756 | ret = PTR_ERR(handle); | |
3757 | handle = NULL; | |
3758 | mlog_errno(ret); | |
3759 | goto leave; | |
3760 | } | |
3761 | ||
3762 | ret = ocfs2_journal_access(handle, inode, root_bh, | |
3763 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3764 | if (ret < 0) { | |
3765 | mlog_errno(ret); | |
3766 | goto leave; | |
3767 | } | |
3768 | ||
3769 | ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, | |
3770 | clusters_to_add, &bit_off, &num_bits); | |
3771 | if (ret < 0) { | |
3772 | if (ret != -ENOSPC) | |
3773 | mlog_errno(ret); | |
3774 | goto leave; | |
3775 | } | |
3776 | ||
3777 | BUG_ON(num_bits > clusters_to_add); | |
3778 | ||
3779 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); | |
3780 | mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n", | |
3781 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
3782 | ||
3783 | if (prev_blkno + prev_clusters * bpc == block && | |
3784 | (prev_clusters + num_bits) << osb->s_clustersize_bits <= | |
3785 | OCFS2_MAX_XATTR_TREE_LEAF_SIZE) { | |
3786 | /* | |
3787 | * If this cluster is contiguous with the old one and | |
3788 | * adding this new cluster, we don't surpass the limit of | |
3789 | * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be | |
3790 | * initialized and used like other buckets in the previous | |
3791 | * cluster. | |
3792 | * So add it as a contiguous one. The caller will handle | |
3793 | * its init process. | |
3794 | */ | |
3795 | v_start = prev_cpos + prev_clusters; | |
3796 | *num_clusters = prev_clusters + num_bits; | |
3797 | mlog(0, "Add contiguous %u clusters to previous extent rec.\n", | |
3798 | num_bits); | |
3799 | } else { | |
3800 | ret = ocfs2_adjust_xattr_cross_cluster(inode, | |
3801 | handle, | |
3802 | first_bh, | |
3803 | header_bh, | |
3804 | block, | |
3805 | prev_blkno, | |
3806 | prev_clusters, | |
3807 | &v_start, | |
3808 | extend); | |
3809 | if (ret) { | |
3810 | mlog_errno(ret); | |
3811 | goto leave; | |
3812 | } | |
3813 | } | |
3814 | ||
28b8ca0b TM |
3815 | if (handle->h_buffer_credits < credits) { |
3816 | /* | |
3817 | * The journal has been restarted before, and don't | |
3818 | * have enough space for the insertion, so extend it | |
3819 | * here. | |
3820 | */ | |
3821 | ret = ocfs2_extend_trans(handle, credits); | |
3822 | if (ret) { | |
3823 | mlog_errno(ret); | |
3824 | goto leave; | |
3825 | } | |
3826 | } | |
01225596 | 3827 | mlog(0, "Insert %u clusters at block %llu for xattr at %u\n", |
de29c085 | 3828 | num_bits, (unsigned long long)block, v_start); |
f99b9b7c JB |
3829 | ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block, |
3830 | num_bits, 0, meta_ac); | |
01225596 TM |
3831 | if (ret < 0) { |
3832 | mlog_errno(ret); | |
3833 | goto leave; | |
3834 | } | |
3835 | ||
3836 | ret = ocfs2_journal_dirty(handle, root_bh); | |
3837 | if (ret < 0) { | |
3838 | mlog_errno(ret); | |
3839 | goto leave; | |
3840 | } | |
3841 | ||
3842 | leave: | |
3843 | if (handle) | |
3844 | ocfs2_commit_trans(osb, handle); | |
3845 | if (data_ac) | |
3846 | ocfs2_free_alloc_context(data_ac); | |
3847 | if (meta_ac) | |
3848 | ocfs2_free_alloc_context(meta_ac); | |
3849 | ||
3850 | return ret; | |
3851 | } | |
3852 | ||
3853 | /* | |
3854 | * Extend a new xattr bucket and move xattrs to the end one by one until | |
3855 | * We meet with start_bh. Only move half of the xattrs to the bucket after it. | |
3856 | */ | |
3857 | static int ocfs2_extend_xattr_bucket(struct inode *inode, | |
3858 | struct buffer_head *first_bh, | |
3859 | struct buffer_head *start_bh, | |
3860 | u32 num_clusters) | |
3861 | { | |
3862 | int ret, credits; | |
3863 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
3864 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
3865 | u64 start_blk = start_bh->b_blocknr, end_blk; | |
3866 | u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb); | |
3867 | handle_t *handle; | |
3868 | struct ocfs2_xattr_header *first_xh = | |
3869 | (struct ocfs2_xattr_header *)first_bh->b_data; | |
3870 | u16 bucket = le16_to_cpu(first_xh->xh_num_buckets); | |
3871 | ||
3872 | mlog(0, "extend xattr bucket in %llu, xattr extend rec starting " | |
de29c085 | 3873 | "from %llu, len = %u\n", (unsigned long long)start_blk, |
01225596 TM |
3874 | (unsigned long long)first_bh->b_blocknr, num_clusters); |
3875 | ||
3876 | BUG_ON(bucket >= num_buckets); | |
3877 | ||
3878 | end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket; | |
3879 | ||
3880 | /* | |
3881 | * We will touch all the buckets after the start_bh(include it). | |
1224be02 | 3882 | * Then we add one more bucket. |
01225596 | 3883 | */ |
1224be02 | 3884 | credits = end_blk - start_blk + 3 * blk_per_bucket + 1; |
01225596 TM |
3885 | handle = ocfs2_start_trans(osb, credits); |
3886 | if (IS_ERR(handle)) { | |
3887 | ret = PTR_ERR(handle); | |
3888 | handle = NULL; | |
3889 | mlog_errno(ret); | |
3890 | goto out; | |
3891 | } | |
3892 | ||
3893 | ret = ocfs2_journal_access(handle, inode, first_bh, | |
3894 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3895 | if (ret) { | |
3896 | mlog_errno(ret); | |
3897 | goto commit; | |
3898 | } | |
3899 | ||
3900 | while (end_blk != start_blk) { | |
3901 | ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk, | |
3902 | end_blk + blk_per_bucket, 0); | |
3903 | if (ret) | |
3904 | goto commit; | |
3905 | end_blk -= blk_per_bucket; | |
3906 | } | |
3907 | ||
3908 | /* Move half of the xattr in start_blk to the next bucket. */ | |
80bcaf34 TM |
3909 | ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk, |
3910 | start_blk + blk_per_bucket, NULL, 0); | |
01225596 TM |
3911 | |
3912 | le16_add_cpu(&first_xh->xh_num_buckets, 1); | |
3913 | ocfs2_journal_dirty(handle, first_bh); | |
3914 | ||
3915 | commit: | |
3916 | ocfs2_commit_trans(osb, handle); | |
3917 | out: | |
3918 | return ret; | |
3919 | } | |
3920 | ||
3921 | /* | |
3922 | * Add new xattr bucket in an extent record and adjust the buckets accordingly. | |
3923 | * xb_bh is the ocfs2_xattr_block. | |
3924 | * We will move all the buckets starting from header_bh to the next place. As | |
3925 | * for this one, half num of its xattrs will be moved to the next one. | |
3926 | * | |
3927 | * We will allocate a new cluster if current cluster is full and adjust | |
3928 | * header_bh and first_bh if the insert place is moved to the new cluster. | |
3929 | */ | |
3930 | static int ocfs2_add_new_xattr_bucket(struct inode *inode, | |
3931 | struct buffer_head *xb_bh, | |
3932 | struct buffer_head *header_bh) | |
3933 | { | |
3934 | struct ocfs2_xattr_header *first_xh = NULL; | |
3935 | struct buffer_head *first_bh = NULL; | |
3936 | struct ocfs2_xattr_block *xb = | |
3937 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
3938 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; | |
3939 | struct ocfs2_extent_list *el = &xb_root->xt_list; | |
3940 | struct ocfs2_xattr_header *xh = | |
3941 | (struct ocfs2_xattr_header *)header_bh->b_data; | |
3942 | u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); | |
3943 | struct super_block *sb = inode->i_sb; | |
3944 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
3945 | int ret, num_buckets, extend = 1; | |
3946 | u64 p_blkno; | |
3947 | u32 e_cpos, num_clusters; | |
3948 | ||
3949 | mlog(0, "Add new xattr bucket starting form %llu\n", | |
3950 | (unsigned long long)header_bh->b_blocknr); | |
3951 | ||
3952 | /* | |
3953 | * Add refrence for header_bh here because it may be | |
3954 | * changed in ocfs2_add_new_xattr_cluster and we need | |
3955 | * to free it in the end. | |
3956 | */ | |
3957 | get_bh(header_bh); | |
3958 | ||
3959 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos, | |
3960 | &num_clusters, el); | |
3961 | if (ret) { | |
3962 | mlog_errno(ret); | |
3963 | goto out; | |
3964 | } | |
3965 | ||
0fcaa56a | 3966 | ret = ocfs2_read_block(inode, p_blkno, &first_bh); |
01225596 TM |
3967 | if (ret) { |
3968 | mlog_errno(ret); | |
3969 | goto out; | |
3970 | } | |
3971 | ||
3972 | num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters; | |
3973 | first_xh = (struct ocfs2_xattr_header *)first_bh->b_data; | |
3974 | ||
3975 | if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) { | |
3976 | ret = ocfs2_add_new_xattr_cluster(inode, | |
3977 | xb_bh, | |
3978 | &first_bh, | |
3979 | &header_bh, | |
3980 | &num_clusters, | |
3981 | e_cpos, | |
3982 | p_blkno, | |
3983 | &extend); | |
3984 | if (ret) { | |
3985 | mlog_errno(ret); | |
3986 | goto out; | |
3987 | } | |
3988 | } | |
3989 | ||
3990 | if (extend) | |
3991 | ret = ocfs2_extend_xattr_bucket(inode, | |
3992 | first_bh, | |
3993 | header_bh, | |
3994 | num_clusters); | |
3995 | if (ret) | |
3996 | mlog_errno(ret); | |
3997 | out: | |
3998 | brelse(first_bh); | |
3999 | brelse(header_bh); | |
4000 | return ret; | |
4001 | } | |
4002 | ||
4003 | static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode, | |
4004 | struct ocfs2_xattr_bucket *bucket, | |
4005 | int offs) | |
4006 | { | |
4007 | int block_off = offs >> inode->i_sb->s_blocksize_bits; | |
4008 | ||
4009 | offs = offs % inode->i_sb->s_blocksize; | |
51def39f | 4010 | return bucket_block(bucket, block_off) + offs; |
01225596 TM |
4011 | } |
4012 | ||
4013 | /* | |
4014 | * Handle the normal xattr set, including replace, delete and new. | |
01225596 TM |
4015 | * |
4016 | * Note: "local" indicates the real data's locality. So we can't | |
4017 | * just its bucket locality by its length. | |
4018 | */ | |
4019 | static void ocfs2_xattr_set_entry_normal(struct inode *inode, | |
4020 | struct ocfs2_xattr_info *xi, | |
4021 | struct ocfs2_xattr_search *xs, | |
4022 | u32 name_hash, | |
5a095611 | 4023 | int local) |
01225596 TM |
4024 | { |
4025 | struct ocfs2_xattr_entry *last, *xe; | |
4026 | int name_len = strlen(xi->name); | |
4027 | struct ocfs2_xattr_header *xh = xs->header; | |
4028 | u16 count = le16_to_cpu(xh->xh_count), start; | |
4029 | size_t blocksize = inode->i_sb->s_blocksize; | |
4030 | char *val; | |
4031 | size_t offs, size, new_size; | |
4032 | ||
4033 | last = &xh->xh_entries[count]; | |
4034 | if (!xs->not_found) { | |
4035 | xe = xs->here; | |
4036 | offs = le16_to_cpu(xe->xe_name_offset); | |
4037 | if (ocfs2_xattr_is_local(xe)) | |
4038 | size = OCFS2_XATTR_SIZE(name_len) + | |
4039 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
4040 | else | |
4041 | size = OCFS2_XATTR_SIZE(name_len) + | |
4042 | OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); | |
4043 | ||
4044 | /* | |
4045 | * If the new value will be stored outside, xi->value has been | |
4046 | * initalized as an empty ocfs2_xattr_value_root, and the same | |
4047 | * goes with xi->value_len, so we can set new_size safely here. | |
4048 | * See ocfs2_xattr_set_in_bucket. | |
4049 | */ | |
4050 | new_size = OCFS2_XATTR_SIZE(name_len) + | |
4051 | OCFS2_XATTR_SIZE(xi->value_len); | |
4052 | ||
4053 | le16_add_cpu(&xh->xh_name_value_len, -size); | |
4054 | if (xi->value) { | |
4055 | if (new_size > size) | |
4056 | goto set_new_name_value; | |
4057 | ||
4058 | /* Now replace the old value with new one. */ | |
4059 | if (local) | |
4060 | xe->xe_value_size = cpu_to_le64(xi->value_len); | |
4061 | else | |
4062 | xe->xe_value_size = 0; | |
4063 | ||
4064 | val = ocfs2_xattr_bucket_get_val(inode, | |
ba937127 | 4065 | xs->bucket, offs); |
01225596 TM |
4066 | memset(val + OCFS2_XATTR_SIZE(name_len), 0, |
4067 | size - OCFS2_XATTR_SIZE(name_len)); | |
4068 | if (OCFS2_XATTR_SIZE(xi->value_len) > 0) | |
4069 | memcpy(val + OCFS2_XATTR_SIZE(name_len), | |
4070 | xi->value, xi->value_len); | |
4071 | ||
4072 | le16_add_cpu(&xh->xh_name_value_len, new_size); | |
4073 | ocfs2_xattr_set_local(xe, local); | |
4074 | return; | |
4075 | } else { | |
5a095611 TM |
4076 | /* |
4077 | * Remove the old entry if there is more than one. | |
4078 | * We don't remove the last entry so that we can | |
4079 | * use it to indicate the hash value of the empty | |
4080 | * bucket. | |
4081 | */ | |
01225596 | 4082 | last -= 1; |
01225596 | 4083 | le16_add_cpu(&xh->xh_count, -1); |
5a095611 TM |
4084 | if (xh->xh_count) { |
4085 | memmove(xe, xe + 1, | |
4086 | (void *)last - (void *)xe); | |
4087 | memset(last, 0, | |
4088 | sizeof(struct ocfs2_xattr_entry)); | |
4089 | } else | |
4090 | xh->xh_free_start = | |
4091 | cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); | |
4092 | ||
01225596 TM |
4093 | return; |
4094 | } | |
4095 | } else { | |
4096 | /* find a new entry for insert. */ | |
4097 | int low = 0, high = count - 1, tmp; | |
4098 | struct ocfs2_xattr_entry *tmp_xe; | |
4099 | ||
5a095611 | 4100 | while (low <= high && count) { |
01225596 TM |
4101 | tmp = (low + high) / 2; |
4102 | tmp_xe = &xh->xh_entries[tmp]; | |
4103 | ||
4104 | if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash)) | |
4105 | low = tmp + 1; | |
4106 | else if (name_hash < | |
4107 | le32_to_cpu(tmp_xe->xe_name_hash)) | |
4108 | high = tmp - 1; | |
06b240d8 TM |
4109 | else { |
4110 | low = tmp; | |
01225596 | 4111 | break; |
06b240d8 | 4112 | } |
01225596 TM |
4113 | } |
4114 | ||
4115 | xe = &xh->xh_entries[low]; | |
4116 | if (low != count) | |
4117 | memmove(xe + 1, xe, (void *)last - (void *)xe); | |
4118 | ||
4119 | le16_add_cpu(&xh->xh_count, 1); | |
4120 | memset(xe, 0, sizeof(struct ocfs2_xattr_entry)); | |
4121 | xe->xe_name_hash = cpu_to_le32(name_hash); | |
4122 | xe->xe_name_len = name_len; | |
4123 | ocfs2_xattr_set_type(xe, xi->name_index); | |
4124 | } | |
4125 | ||
4126 | set_new_name_value: | |
4127 | /* Insert the new name+value. */ | |
4128 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len); | |
4129 | ||
4130 | /* | |
4131 | * We must make sure that the name/value pair | |
4132 | * exists in the same block. | |
4133 | */ | |
4134 | offs = le16_to_cpu(xh->xh_free_start); | |
4135 | start = offs - size; | |
4136 | ||
4137 | if (start >> inode->i_sb->s_blocksize_bits != | |
4138 | (offs - 1) >> inode->i_sb->s_blocksize_bits) { | |
4139 | offs = offs - offs % blocksize; | |
4140 | xh->xh_free_start = cpu_to_le16(offs); | |
4141 | } | |
4142 | ||
ba937127 | 4143 | val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size); |
01225596 TM |
4144 | xe->xe_name_offset = cpu_to_le16(offs - size); |
4145 | ||
4146 | memset(val, 0, size); | |
4147 | memcpy(val, xi->name, name_len); | |
4148 | memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len); | |
4149 | ||
4150 | xe->xe_value_size = cpu_to_le64(xi->value_len); | |
4151 | ocfs2_xattr_set_local(xe, local); | |
4152 | xs->here = xe; | |
4153 | le16_add_cpu(&xh->xh_free_start, -size); | |
4154 | le16_add_cpu(&xh->xh_name_value_len, size); | |
4155 | ||
4156 | return; | |
4157 | } | |
4158 | ||
01225596 TM |
4159 | /* |
4160 | * Set the xattr entry in the specified bucket. | |
4161 | * The bucket is indicated by xs->bucket and it should have the enough | |
4162 | * space for the xattr insertion. | |
4163 | */ | |
4164 | static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode, | |
4165 | struct ocfs2_xattr_info *xi, | |
4166 | struct ocfs2_xattr_search *xs, | |
4167 | u32 name_hash, | |
5a095611 | 4168 | int local) |
01225596 | 4169 | { |
1224be02 | 4170 | int ret; |
01225596 TM |
4171 | handle_t *handle = NULL; |
4172 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
4173 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4174 | ||
ff1ec20e MF |
4175 | mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n", |
4176 | (unsigned long)xi->value_len, xi->name_index, | |
ba937127 | 4177 | (unsigned long long)bucket_blkno(xs->bucket)); |
01225596 | 4178 | |
ba937127 | 4179 | if (!xs->bucket->bu_bhs[1]) { |
31d33073 | 4180 | ret = ocfs2_read_blocks(inode, |
ba937127 JB |
4181 | bucket_blkno(xs->bucket) + 1, |
4182 | blk_per_bucket - 1, &xs->bucket->bu_bhs[1], | |
1efd47f8 | 4183 | 0); |
01225596 TM |
4184 | if (ret) { |
4185 | mlog_errno(ret); | |
4186 | goto out; | |
4187 | } | |
4188 | } | |
4189 | ||
4190 | handle = ocfs2_start_trans(osb, blk_per_bucket); | |
4191 | if (IS_ERR(handle)) { | |
4192 | ret = PTR_ERR(handle); | |
4193 | handle = NULL; | |
4194 | mlog_errno(ret); | |
4195 | goto out; | |
4196 | } | |
4197 | ||
ba937127 | 4198 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
1224be02 JB |
4199 | OCFS2_JOURNAL_ACCESS_WRITE); |
4200 | if (ret < 0) { | |
4201 | mlog_errno(ret); | |
4202 | goto out; | |
01225596 TM |
4203 | } |
4204 | ||
5a095611 | 4205 | ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local); |
ba937127 | 4206 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
01225596 | 4207 | |
01225596 TM |
4208 | out: |
4209 | ocfs2_commit_trans(osb, handle); | |
4210 | ||
4211 | return ret; | |
4212 | } | |
4213 | ||
4214 | static int ocfs2_xattr_value_update_size(struct inode *inode, | |
4215 | struct buffer_head *xe_bh, | |
4216 | struct ocfs2_xattr_entry *xe, | |
4217 | u64 new_size) | |
4218 | { | |
4219 | int ret; | |
4220 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4221 | handle_t *handle = NULL; | |
4222 | ||
4223 | handle = ocfs2_start_trans(osb, 1); | |
d3264799 | 4224 | if (IS_ERR(handle)) { |
01225596 TM |
4225 | ret = -ENOMEM; |
4226 | mlog_errno(ret); | |
4227 | goto out; | |
4228 | } | |
4229 | ||
4230 | ret = ocfs2_journal_access(handle, inode, xe_bh, | |
4231 | OCFS2_JOURNAL_ACCESS_WRITE); | |
4232 | if (ret < 0) { | |
4233 | mlog_errno(ret); | |
4234 | goto out_commit; | |
4235 | } | |
4236 | ||
4237 | xe->xe_value_size = cpu_to_le64(new_size); | |
4238 | ||
4239 | ret = ocfs2_journal_dirty(handle, xe_bh); | |
4240 | if (ret < 0) | |
4241 | mlog_errno(ret); | |
4242 | ||
4243 | out_commit: | |
4244 | ocfs2_commit_trans(osb, handle); | |
4245 | out: | |
4246 | return ret; | |
4247 | } | |
4248 | ||
4249 | /* | |
4250 | * Truncate the specified xe_off entry in xattr bucket. | |
4251 | * bucket is indicated by header_bh and len is the new length. | |
4252 | * Both the ocfs2_xattr_value_root and the entry will be updated here. | |
4253 | * | |
4254 | * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed. | |
4255 | */ | |
4256 | static int ocfs2_xattr_bucket_value_truncate(struct inode *inode, | |
4257 | struct buffer_head *header_bh, | |
4258 | int xe_off, | |
4259 | int len) | |
4260 | { | |
4261 | int ret, offset; | |
4262 | u64 value_blk; | |
4263 | struct buffer_head *value_bh = NULL; | |
4264 | struct ocfs2_xattr_value_root *xv; | |
4265 | struct ocfs2_xattr_entry *xe; | |
4266 | struct ocfs2_xattr_header *xh = | |
4267 | (struct ocfs2_xattr_header *)header_bh->b_data; | |
4268 | size_t blocksize = inode->i_sb->s_blocksize; | |
4269 | ||
4270 | xe = &xh->xh_entries[xe_off]; | |
4271 | ||
4272 | BUG_ON(!xe || ocfs2_xattr_is_local(xe)); | |
4273 | ||
4274 | offset = le16_to_cpu(xe->xe_name_offset) + | |
4275 | OCFS2_XATTR_SIZE(xe->xe_name_len); | |
4276 | ||
4277 | value_blk = offset / blocksize; | |
4278 | ||
4279 | /* We don't allow ocfs2_xattr_value to be stored in different block. */ | |
4280 | BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize); | |
4281 | value_blk += header_bh->b_blocknr; | |
4282 | ||
0fcaa56a | 4283 | ret = ocfs2_read_block(inode, value_blk, &value_bh); |
01225596 TM |
4284 | if (ret) { |
4285 | mlog_errno(ret); | |
4286 | goto out; | |
4287 | } | |
4288 | ||
4289 | xv = (struct ocfs2_xattr_value_root *) | |
4290 | (value_bh->b_data + offset % blocksize); | |
4291 | ||
4292 | mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n", | |
4293 | xe_off, (unsigned long long)header_bh->b_blocknr, len); | |
4294 | ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len); | |
4295 | if (ret) { | |
4296 | mlog_errno(ret); | |
4297 | goto out; | |
4298 | } | |
4299 | ||
4300 | ret = ocfs2_xattr_value_update_size(inode, header_bh, xe, len); | |
4301 | if (ret) { | |
4302 | mlog_errno(ret); | |
4303 | goto out; | |
4304 | } | |
4305 | ||
4306 | out: | |
4307 | brelse(value_bh); | |
4308 | return ret; | |
4309 | } | |
4310 | ||
4311 | static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode, | |
4312 | struct ocfs2_xattr_search *xs, | |
4313 | int len) | |
4314 | { | |
4315 | int ret, offset; | |
4316 | struct ocfs2_xattr_entry *xe = xs->here; | |
4317 | struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base; | |
4318 | ||
ba937127 | 4319 | BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe)); |
01225596 TM |
4320 | |
4321 | offset = xe - xh->xh_entries; | |
ba937127 | 4322 | ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0], |
01225596 TM |
4323 | offset, len); |
4324 | if (ret) | |
4325 | mlog_errno(ret); | |
4326 | ||
4327 | return ret; | |
4328 | } | |
4329 | ||
4330 | static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode, | |
4331 | struct ocfs2_xattr_search *xs, | |
4332 | char *val, | |
4333 | int value_len) | |
4334 | { | |
4335 | int offset; | |
4336 | struct ocfs2_xattr_value_root *xv; | |
4337 | struct ocfs2_xattr_entry *xe = xs->here; | |
4338 | ||
4339 | BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe)); | |
4340 | ||
4341 | offset = le16_to_cpu(xe->xe_name_offset) + | |
4342 | OCFS2_XATTR_SIZE(xe->xe_name_len); | |
4343 | ||
4344 | xv = (struct ocfs2_xattr_value_root *)(xs->base + offset); | |
4345 | ||
4346 | return __ocfs2_xattr_set_value_outside(inode, xv, val, value_len); | |
4347 | } | |
4348 | ||
01225596 TM |
4349 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
4350 | struct buffer_head *root_bh, | |
4351 | u64 blkno, | |
4352 | u32 cpos, | |
4353 | u32 len) | |
4354 | { | |
4355 | int ret; | |
4356 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4357 | struct inode *tl_inode = osb->osb_tl_inode; | |
4358 | handle_t *handle; | |
4359 | struct ocfs2_xattr_block *xb = | |
4360 | (struct ocfs2_xattr_block *)root_bh->b_data; | |
01225596 TM |
4361 | struct ocfs2_alloc_context *meta_ac = NULL; |
4362 | struct ocfs2_cached_dealloc_ctxt dealloc; | |
f99b9b7c JB |
4363 | struct ocfs2_extent_tree et; |
4364 | ||
8d6220d6 | 4365 | ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh); |
01225596 TM |
4366 | |
4367 | ocfs2_init_dealloc_ctxt(&dealloc); | |
4368 | ||
4369 | mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n", | |
4370 | cpos, len, (unsigned long long)blkno); | |
4371 | ||
4372 | ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len); | |
4373 | ||
f99b9b7c | 4374 | ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac); |
01225596 TM |
4375 | if (ret) { |
4376 | mlog_errno(ret); | |
4377 | return ret; | |
4378 | } | |
4379 | ||
4380 | mutex_lock(&tl_inode->i_mutex); | |
4381 | ||
4382 | if (ocfs2_truncate_log_needs_flush(osb)) { | |
4383 | ret = __ocfs2_flush_truncate_log(osb); | |
4384 | if (ret < 0) { | |
4385 | mlog_errno(ret); | |
4386 | goto out; | |
4387 | } | |
4388 | } | |
4389 | ||
4390 | handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); | |
d3264799 | 4391 | if (IS_ERR(handle)) { |
01225596 TM |
4392 | ret = -ENOMEM; |
4393 | mlog_errno(ret); | |
4394 | goto out; | |
4395 | } | |
4396 | ||
4397 | ret = ocfs2_journal_access(handle, inode, root_bh, | |
4398 | OCFS2_JOURNAL_ACCESS_WRITE); | |
4399 | if (ret) { | |
4400 | mlog_errno(ret); | |
4401 | goto out_commit; | |
4402 | } | |
4403 | ||
f99b9b7c JB |
4404 | ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac, |
4405 | &dealloc); | |
01225596 TM |
4406 | if (ret) { |
4407 | mlog_errno(ret); | |
4408 | goto out_commit; | |
4409 | } | |
4410 | ||
4411 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len); | |
4412 | ||
4413 | ret = ocfs2_journal_dirty(handle, root_bh); | |
4414 | if (ret) { | |
4415 | mlog_errno(ret); | |
4416 | goto out_commit; | |
4417 | } | |
4418 | ||
4419 | ret = ocfs2_truncate_log_append(osb, handle, blkno, len); | |
4420 | if (ret) | |
4421 | mlog_errno(ret); | |
4422 | ||
4423 | out_commit: | |
4424 | ocfs2_commit_trans(osb, handle); | |
4425 | out: | |
4426 | ocfs2_schedule_truncate_log_flush(osb, 1); | |
4427 | ||
4428 | mutex_unlock(&tl_inode->i_mutex); | |
4429 | ||
4430 | if (meta_ac) | |
4431 | ocfs2_free_alloc_context(meta_ac); | |
4432 | ||
4433 | ocfs2_run_deallocs(osb, &dealloc); | |
4434 | ||
4435 | return ret; | |
4436 | } | |
4437 | ||
01225596 TM |
4438 | static void ocfs2_xattr_bucket_remove_xs(struct inode *inode, |
4439 | struct ocfs2_xattr_search *xs) | |
4440 | { | |
4441 | handle_t *handle = NULL; | |
ba937127 | 4442 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
01225596 TM |
4443 | struct ocfs2_xattr_entry *last = &xh->xh_entries[ |
4444 | le16_to_cpu(xh->xh_count) - 1]; | |
4445 | int ret = 0; | |
4446 | ||
1224be02 JB |
4447 | handle = ocfs2_start_trans((OCFS2_SB(inode->i_sb)), |
4448 | ocfs2_blocks_per_xattr_bucket(inode->i_sb)); | |
01225596 TM |
4449 | if (IS_ERR(handle)) { |
4450 | ret = PTR_ERR(handle); | |
4451 | mlog_errno(ret); | |
4452 | return; | |
4453 | } | |
4454 | ||
ba937127 | 4455 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
1224be02 | 4456 | OCFS2_JOURNAL_ACCESS_WRITE); |
01225596 TM |
4457 | if (ret) { |
4458 | mlog_errno(ret); | |
4459 | goto out_commit; | |
4460 | } | |
4461 | ||
4462 | /* Remove the old entry. */ | |
4463 | memmove(xs->here, xs->here + 1, | |
4464 | (void *)last - (void *)xs->here); | |
4465 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); | |
4466 | le16_add_cpu(&xh->xh_count, -1); | |
4467 | ||
ba937127 | 4468 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
1224be02 | 4469 | |
01225596 TM |
4470 | out_commit: |
4471 | ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle); | |
4472 | } | |
4473 | ||
4474 | /* | |
4475 | * Set the xattr name/value in the bucket specified in xs. | |
4476 | * | |
4477 | * As the new value in xi may be stored in the bucket or in an outside cluster, | |
4478 | * we divide the whole process into 3 steps: | |
4479 | * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket) | |
4480 | * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs) | |
4481 | * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside) | |
4482 | * 4. If the clusters for the new outside value can't be allocated, we need | |
4483 | * to free the xattr we allocated in set. | |
4484 | */ | |
4485 | static int ocfs2_xattr_set_in_bucket(struct inode *inode, | |
4486 | struct ocfs2_xattr_info *xi, | |
4487 | struct ocfs2_xattr_search *xs) | |
4488 | { | |
5a095611 | 4489 | int ret, local = 1; |
01225596 TM |
4490 | size_t value_len; |
4491 | char *val = (char *)xi->value; | |
4492 | struct ocfs2_xattr_entry *xe = xs->here; | |
2057e5c6 TM |
4493 | u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name, |
4494 | strlen(xi->name)); | |
01225596 TM |
4495 | |
4496 | if (!xs->not_found && !ocfs2_xattr_is_local(xe)) { | |
4497 | /* | |
4498 | * We need to truncate the xattr storage first. | |
4499 | * | |
4500 | * If both the old and new value are stored to | |
4501 | * outside block, we only need to truncate | |
4502 | * the storage and then set the value outside. | |
4503 | * | |
4504 | * If the new value should be stored within block, | |
4505 | * we should free all the outside block first and | |
4506 | * the modification to the xattr block will be done | |
4507 | * by following steps. | |
4508 | */ | |
4509 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) | |
4510 | value_len = xi->value_len; | |
4511 | else | |
4512 | value_len = 0; | |
4513 | ||
4514 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, | |
4515 | value_len); | |
4516 | if (ret) | |
4517 | goto out; | |
4518 | ||
4519 | if (value_len) | |
4520 | goto set_value_outside; | |
4521 | } | |
4522 | ||
4523 | value_len = xi->value_len; | |
4524 | /* So we have to handle the inside block change now. */ | |
4525 | if (value_len > OCFS2_XATTR_INLINE_SIZE) { | |
4526 | /* | |
4527 | * If the new value will be stored outside of block, | |
4528 | * initalize a new empty value root and insert it first. | |
4529 | */ | |
4530 | local = 0; | |
4531 | xi->value = &def_xv; | |
4532 | xi->value_len = OCFS2_XATTR_ROOT_SIZE; | |
4533 | } | |
4534 | ||
5a095611 | 4535 | ret = ocfs2_xattr_set_entry_in_bucket(inode, xi, xs, name_hash, local); |
01225596 TM |
4536 | if (ret) { |
4537 | mlog_errno(ret); | |
4538 | goto out; | |
4539 | } | |
4540 | ||
5a095611 TM |
4541 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
4542 | goto out; | |
01225596 | 4543 | |
5a095611 TM |
4544 | /* allocate the space now for the outside block storage. */ |
4545 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, | |
4546 | value_len); | |
4547 | if (ret) { | |
4548 | mlog_errno(ret); | |
4549 | ||
4550 | if (xs->not_found) { | |
4551 | /* | |
4552 | * We can't allocate enough clusters for outside | |
4553 | * storage and we have allocated xattr already, | |
4554 | * so need to remove it. | |
4555 | */ | |
4556 | ocfs2_xattr_bucket_remove_xs(inode, xs); | |
01225596 | 4557 | } |
01225596 TM |
4558 | goto out; |
4559 | } | |
4560 | ||
4561 | set_value_outside: | |
4562 | ret = ocfs2_xattr_bucket_set_value_outside(inode, xs, val, value_len); | |
4563 | out: | |
4564 | return ret; | |
4565 | } | |
4566 | ||
80bcaf34 TM |
4567 | /* |
4568 | * check whether the xattr bucket is filled up with the same hash value. | |
4569 | * If we want to insert the xattr with the same hash, return -ENOSPC. | |
4570 | * If we want to insert a xattr with different hash value, go ahead | |
4571 | * and ocfs2_divide_xattr_bucket will handle this. | |
4572 | */ | |
01225596 | 4573 | static int ocfs2_check_xattr_bucket_collision(struct inode *inode, |
80bcaf34 TM |
4574 | struct ocfs2_xattr_bucket *bucket, |
4575 | const char *name) | |
01225596 | 4576 | { |
3e632946 | 4577 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
80bcaf34 TM |
4578 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
4579 | ||
4580 | if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash)) | |
4581 | return 0; | |
01225596 TM |
4582 | |
4583 | if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash == | |
4584 | xh->xh_entries[0].xe_name_hash) { | |
4585 | mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, " | |
4586 | "hash = %u\n", | |
9c7759aa | 4587 | (unsigned long long)bucket_blkno(bucket), |
01225596 TM |
4588 | le32_to_cpu(xh->xh_entries[0].xe_name_hash)); |
4589 | return -ENOSPC; | |
4590 | } | |
4591 | ||
4592 | return 0; | |
4593 | } | |
4594 | ||
4595 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, | |
4596 | struct ocfs2_xattr_info *xi, | |
4597 | struct ocfs2_xattr_search *xs) | |
4598 | { | |
4599 | struct ocfs2_xattr_header *xh; | |
4600 | struct ocfs2_xattr_entry *xe; | |
4601 | u16 count, header_size, xh_free_start; | |
6dde41d9 | 4602 | int free, max_free, need, old; |
01225596 TM |
4603 | size_t value_size = 0, name_len = strlen(xi->name); |
4604 | size_t blocksize = inode->i_sb->s_blocksize; | |
4605 | int ret, allocation = 0; | |
01225596 TM |
4606 | |
4607 | mlog_entry("Set xattr %s in xattr index block\n", xi->name); | |
4608 | ||
4609 | try_again: | |
4610 | xh = xs->header; | |
4611 | count = le16_to_cpu(xh->xh_count); | |
4612 | xh_free_start = le16_to_cpu(xh->xh_free_start); | |
4613 | header_size = sizeof(struct ocfs2_xattr_header) + | |
4614 | count * sizeof(struct ocfs2_xattr_entry); | |
4615 | max_free = OCFS2_XATTR_BUCKET_SIZE - | |
4616 | le16_to_cpu(xh->xh_name_value_len) - header_size; | |
4617 | ||
4618 | mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size " | |
4619 | "of %u which exceed block size\n", | |
ba937127 | 4620 | (unsigned long long)bucket_blkno(xs->bucket), |
01225596 TM |
4621 | header_size); |
4622 | ||
4623 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) | |
4624 | value_size = OCFS2_XATTR_ROOT_SIZE; | |
4625 | else if (xi->value) | |
4626 | value_size = OCFS2_XATTR_SIZE(xi->value_len); | |
4627 | ||
4628 | if (xs->not_found) | |
4629 | need = sizeof(struct ocfs2_xattr_entry) + | |
4630 | OCFS2_XATTR_SIZE(name_len) + value_size; | |
4631 | else { | |
4632 | need = value_size + OCFS2_XATTR_SIZE(name_len); | |
4633 | ||
4634 | /* | |
4635 | * We only replace the old value if the new length is smaller | |
4636 | * than the old one. Otherwise we will allocate new space in the | |
4637 | * bucket to store it. | |
4638 | */ | |
4639 | xe = xs->here; | |
4640 | if (ocfs2_xattr_is_local(xe)) | |
4641 | old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
4642 | else | |
4643 | old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); | |
4644 | ||
4645 | if (old >= value_size) | |
4646 | need = 0; | |
4647 | } | |
4648 | ||
4649 | free = xh_free_start - header_size; | |
4650 | /* | |
4651 | * We need to make sure the new name/value pair | |
4652 | * can exist in the same block. | |
4653 | */ | |
4654 | if (xh_free_start % blocksize < need) | |
4655 | free -= xh_free_start % blocksize; | |
4656 | ||
4657 | mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, " | |
4658 | "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len =" | |
4659 | " %u\n", xs->not_found, | |
ba937127 | 4660 | (unsigned long long)bucket_blkno(xs->bucket), |
01225596 TM |
4661 | free, need, max_free, le16_to_cpu(xh->xh_free_start), |
4662 | le16_to_cpu(xh->xh_name_value_len)); | |
4663 | ||
4664 | if (free < need || count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) { | |
4665 | if (need <= max_free && | |
4666 | count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) { | |
4667 | /* | |
4668 | * We can create the space by defragment. Since only the | |
4669 | * name/value will be moved, the xe shouldn't be changed | |
4670 | * in xs. | |
4671 | */ | |
ba937127 | 4672 | ret = ocfs2_defrag_xattr_bucket(inode, xs->bucket); |
01225596 TM |
4673 | if (ret) { |
4674 | mlog_errno(ret); | |
4675 | goto out; | |
4676 | } | |
4677 | ||
4678 | xh_free_start = le16_to_cpu(xh->xh_free_start); | |
4679 | free = xh_free_start - header_size; | |
4680 | if (xh_free_start % blocksize < need) | |
4681 | free -= xh_free_start % blocksize; | |
4682 | ||
4683 | if (free >= need) | |
4684 | goto xattr_set; | |
4685 | ||
4686 | mlog(0, "Can't get enough space for xattr insert by " | |
4687 | "defragment. Need %u bytes, but we have %d, so " | |
4688 | "allocate new bucket for it.\n", need, free); | |
4689 | } | |
4690 | ||
4691 | /* | |
4692 | * We have to add new buckets or clusters and one | |
4693 | * allocation should leave us enough space for insert. | |
4694 | */ | |
4695 | BUG_ON(allocation); | |
4696 | ||
4697 | /* | |
4698 | * We do not allow for overlapping ranges between buckets. And | |
4699 | * the maximum number of collisions we will allow for then is | |
4700 | * one bucket's worth, so check it here whether we need to | |
4701 | * add a new bucket for the insert. | |
4702 | */ | |
80bcaf34 | 4703 | ret = ocfs2_check_xattr_bucket_collision(inode, |
ba937127 | 4704 | xs->bucket, |
80bcaf34 | 4705 | xi->name); |
01225596 TM |
4706 | if (ret) { |
4707 | mlog_errno(ret); | |
4708 | goto out; | |
4709 | } | |
4710 | ||
4711 | ret = ocfs2_add_new_xattr_bucket(inode, | |
4712 | xs->xattr_bh, | |
ba937127 | 4713 | xs->bucket->bu_bhs[0]); |
01225596 TM |
4714 | if (ret) { |
4715 | mlog_errno(ret); | |
4716 | goto out; | |
4717 | } | |
4718 | ||
ba937127 | 4719 | ocfs2_xattr_bucket_relse(xs->bucket); |
01225596 TM |
4720 | |
4721 | ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh, | |
4722 | xi->name_index, | |
4723 | xi->name, xs); | |
4724 | if (ret && ret != -ENODATA) | |
4725 | goto out; | |
4726 | xs->not_found = ret; | |
4727 | allocation = 1; | |
4728 | goto try_again; | |
4729 | } | |
4730 | ||
4731 | xattr_set: | |
4732 | ret = ocfs2_xattr_set_in_bucket(inode, xi, xs); | |
4733 | out: | |
4734 | mlog_exit(ret); | |
4735 | return ret; | |
4736 | } | |
a3944256 TM |
4737 | |
4738 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, | |
4739 | struct ocfs2_xattr_bucket *bucket, | |
4740 | void *para) | |
4741 | { | |
4742 | int ret = 0; | |
3e632946 | 4743 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
a3944256 TM |
4744 | u16 i; |
4745 | struct ocfs2_xattr_entry *xe; | |
4746 | ||
4747 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { | |
4748 | xe = &xh->xh_entries[i]; | |
4749 | if (ocfs2_xattr_is_local(xe)) | |
4750 | continue; | |
4751 | ||
4752 | ret = ocfs2_xattr_bucket_value_truncate(inode, | |
4ac6032d | 4753 | bucket->bu_bhs[0], |
a3944256 TM |
4754 | i, 0); |
4755 | if (ret) { | |
4756 | mlog_errno(ret); | |
4757 | break; | |
4758 | } | |
4759 | } | |
4760 | ||
4761 | return ret; | |
4762 | } | |
4763 | ||
4764 | static int ocfs2_delete_xattr_index_block(struct inode *inode, | |
4765 | struct buffer_head *xb_bh) | |
4766 | { | |
4767 | struct ocfs2_xattr_block *xb = | |
4768 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
4769 | struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; | |
4770 | int ret = 0; | |
4771 | u32 name_hash = UINT_MAX, e_cpos, num_clusters; | |
4772 | u64 p_blkno; | |
4773 | ||
4774 | if (le16_to_cpu(el->l_next_free_rec) == 0) | |
4775 | return 0; | |
4776 | ||
4777 | while (name_hash > 0) { | |
4778 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, | |
4779 | &e_cpos, &num_clusters, el); | |
4780 | if (ret) { | |
4781 | mlog_errno(ret); | |
4782 | goto out; | |
4783 | } | |
4784 | ||
4785 | ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters, | |
4786 | ocfs2_delete_xattr_in_bucket, | |
4787 | NULL); | |
4788 | if (ret) { | |
4789 | mlog_errno(ret); | |
4790 | goto out; | |
4791 | } | |
4792 | ||
4793 | ret = ocfs2_rm_xattr_cluster(inode, xb_bh, | |
4794 | p_blkno, e_cpos, num_clusters); | |
4795 | if (ret) { | |
4796 | mlog_errno(ret); | |
4797 | break; | |
4798 | } | |
4799 | ||
4800 | if (e_cpos == 0) | |
4801 | break; | |
4802 | ||
4803 | name_hash = e_cpos - 1; | |
4804 | } | |
4805 | ||
4806 | out: | |
4807 | return ret; | |
4808 | } | |
99219aea MF |
4809 | |
4810 | /* | |
4811 | * 'trusted' attributes support | |
4812 | */ | |
99219aea MF |
4813 | static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list, |
4814 | size_t list_size, const char *name, | |
4815 | size_t name_len) | |
4816 | { | |
ceb1eba3 | 4817 | const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
99219aea MF |
4818 | const size_t total_len = prefix_len + name_len + 1; |
4819 | ||
4820 | if (list && total_len <= list_size) { | |
4821 | memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len); | |
4822 | memcpy(list + prefix_len, name, name_len); | |
4823 | list[prefix_len + name_len] = '\0'; | |
4824 | } | |
4825 | return total_len; | |
4826 | } | |
4827 | ||
4828 | static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name, | |
4829 | void *buffer, size_t size) | |
4830 | { | |
4831 | if (strcmp(name, "") == 0) | |
4832 | return -EINVAL; | |
4833 | return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name, | |
4834 | buffer, size); | |
4835 | } | |
4836 | ||
4837 | static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name, | |
4838 | const void *value, size_t size, int flags) | |
4839 | { | |
4840 | if (strcmp(name, "") == 0) | |
4841 | return -EINVAL; | |
4842 | ||
4843 | return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value, | |
4844 | size, flags); | |
4845 | } | |
4846 | ||
4847 | struct xattr_handler ocfs2_xattr_trusted_handler = { | |
4848 | .prefix = XATTR_TRUSTED_PREFIX, | |
4849 | .list = ocfs2_xattr_trusted_list, | |
4850 | .get = ocfs2_xattr_trusted_get, | |
4851 | .set = ocfs2_xattr_trusted_set, | |
4852 | }; | |
4853 | ||
99219aea MF |
4854 | /* |
4855 | * 'user' attributes support | |
4856 | */ | |
99219aea MF |
4857 | static size_t ocfs2_xattr_user_list(struct inode *inode, char *list, |
4858 | size_t list_size, const char *name, | |
4859 | size_t name_len) | |
4860 | { | |
ceb1eba3 | 4861 | const size_t prefix_len = XATTR_USER_PREFIX_LEN; |
99219aea MF |
4862 | const size_t total_len = prefix_len + name_len + 1; |
4863 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4864 | ||
4865 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) | |
4866 | return 0; | |
4867 | ||
4868 | if (list && total_len <= list_size) { | |
4869 | memcpy(list, XATTR_USER_PREFIX, prefix_len); | |
4870 | memcpy(list + prefix_len, name, name_len); | |
4871 | list[prefix_len + name_len] = '\0'; | |
4872 | } | |
4873 | return total_len; | |
4874 | } | |
4875 | ||
4876 | static int ocfs2_xattr_user_get(struct inode *inode, const char *name, | |
4877 | void *buffer, size_t size) | |
4878 | { | |
4879 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4880 | ||
4881 | if (strcmp(name, "") == 0) | |
4882 | return -EINVAL; | |
4883 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) | |
4884 | return -EOPNOTSUPP; | |
4885 | return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name, | |
4886 | buffer, size); | |
4887 | } | |
4888 | ||
4889 | static int ocfs2_xattr_user_set(struct inode *inode, const char *name, | |
4890 | const void *value, size_t size, int flags) | |
4891 | { | |
4892 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4893 | ||
4894 | if (strcmp(name, "") == 0) | |
4895 | return -EINVAL; | |
4896 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) | |
4897 | return -EOPNOTSUPP; | |
4898 | ||
4899 | return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value, | |
4900 | size, flags); | |
4901 | } | |
4902 | ||
4903 | struct xattr_handler ocfs2_xattr_user_handler = { | |
4904 | .prefix = XATTR_USER_PREFIX, | |
4905 | .list = ocfs2_xattr_user_list, | |
4906 | .get = ocfs2_xattr_user_get, | |
4907 | .set = ocfs2_xattr_user_set, | |
4908 | }; |