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