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