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