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