]>
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 | ||
6c3faba4 TY |
2328 | /* |
2329 | * This function only called duing creating inode | |
2330 | * for init security/acl xattrs of the new inode. | |
2331 | * The xattrs could be put into ibody or extent block, | |
2332 | * xattr bucket would not be use in this case. | |
2333 | * transanction credits also be reserved in here. | |
2334 | */ | |
2335 | int ocfs2_xattr_set_handle(handle_t *handle, | |
2336 | struct inode *inode, | |
2337 | struct buffer_head *di_bh, | |
2338 | int name_index, | |
2339 | const char *name, | |
2340 | const void *value, | |
2341 | size_t value_len, | |
2342 | int flags, | |
2343 | struct ocfs2_alloc_context *meta_ac, | |
2344 | struct ocfs2_alloc_context *data_ac) | |
2345 | { | |
2346 | struct ocfs2_dinode *di; | |
2347 | int ret; | |
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 | ||
2364 | struct ocfs2_xattr_set_ctxt ctxt = { | |
2365 | .handle = handle, | |
2366 | .meta_ac = meta_ac, | |
2367 | .data_ac = data_ac, | |
2368 | }; | |
2369 | ||
2370 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) | |
2371 | return -EOPNOTSUPP; | |
2372 | ||
2373 | xis.inode_bh = xbs.inode_bh = di_bh; | |
2374 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
2375 | ||
2376 | down_write(&OCFS2_I(inode)->ip_xattr_sem); | |
2377 | ||
2378 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); | |
2379 | if (ret) | |
2380 | goto cleanup; | |
2381 | if (xis.not_found) { | |
2382 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); | |
2383 | if (ret) | |
2384 | goto cleanup; | |
2385 | } | |
2386 | ||
2387 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); | |
2388 | ||
2389 | cleanup: | |
2390 | up_write(&OCFS2_I(inode)->ip_xattr_sem); | |
2391 | brelse(xbs.xattr_bh); | |
2392 | ||
2393 | return ret; | |
2394 | } | |
2395 | ||
cf1d6c76 TY |
2396 | /* |
2397 | * ocfs2_xattr_set() | |
2398 | * | |
2399 | * Set, replace or remove an extended attribute for this inode. | |
2400 | * value is NULL to remove an existing extended attribute, else either | |
2401 | * create or replace an extended attribute. | |
2402 | */ | |
2403 | int ocfs2_xattr_set(struct inode *inode, | |
2404 | int name_index, | |
2405 | const char *name, | |
2406 | const void *value, | |
2407 | size_t value_len, | |
2408 | int flags) | |
2409 | { | |
2410 | struct buffer_head *di_bh = NULL; | |
2411 | struct ocfs2_dinode *di; | |
85db90e7 | 2412 | int ret, credits; |
78f30c31 | 2413 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
85db90e7 | 2414 | struct inode *tl_inode = osb->osb_tl_inode; |
78f30c31 | 2415 | struct ocfs2_xattr_set_ctxt ctxt = { NULL, NULL, }; |
cf1d6c76 TY |
2416 | |
2417 | struct ocfs2_xattr_info xi = { | |
2418 | .name_index = name_index, | |
2419 | .name = name, | |
2420 | .value = value, | |
2421 | .value_len = value_len, | |
2422 | }; | |
2423 | ||
2424 | struct ocfs2_xattr_search xis = { | |
2425 | .not_found = -ENODATA, | |
2426 | }; | |
2427 | ||
2428 | struct ocfs2_xattr_search xbs = { | |
2429 | .not_found = -ENODATA, | |
2430 | }; | |
2431 | ||
8154da3d TY |
2432 | if (!ocfs2_supports_xattr(OCFS2_SB(inode->i_sb))) |
2433 | return -EOPNOTSUPP; | |
2434 | ||
ba937127 JB |
2435 | /* |
2436 | * Only xbs will be used on indexed trees. xis doesn't need a | |
2437 | * bucket. | |
2438 | */ | |
2439 | xbs.bucket = ocfs2_xattr_bucket_new(inode); | |
2440 | if (!xbs.bucket) { | |
2441 | mlog_errno(-ENOMEM); | |
2442 | return -ENOMEM; | |
2443 | } | |
2444 | ||
cf1d6c76 TY |
2445 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
2446 | if (ret < 0) { | |
2447 | mlog_errno(ret); | |
ba937127 | 2448 | goto cleanup_nolock; |
cf1d6c76 TY |
2449 | } |
2450 | xis.inode_bh = xbs.inode_bh = di_bh; | |
2451 | di = (struct ocfs2_dinode *)di_bh->b_data; | |
2452 | ||
2453 | down_write(&OCFS2_I(inode)->ip_xattr_sem); | |
2454 | /* | |
2455 | * Scan inode and external block to find the same name | |
2456 | * extended attribute and collect search infomation. | |
2457 | */ | |
2458 | ret = ocfs2_xattr_ibody_find(inode, name_index, name, &xis); | |
2459 | if (ret) | |
2460 | goto cleanup; | |
2461 | if (xis.not_found) { | |
2462 | ret = ocfs2_xattr_block_find(inode, name_index, name, &xbs); | |
2463 | if (ret) | |
2464 | goto cleanup; | |
2465 | } | |
2466 | ||
2467 | if (xis.not_found && xbs.not_found) { | |
2468 | ret = -ENODATA; | |
2469 | if (flags & XATTR_REPLACE) | |
2470 | goto cleanup; | |
2471 | ret = 0; | |
2472 | if (!value) | |
2473 | goto cleanup; | |
2474 | } else { | |
2475 | ret = -EEXIST; | |
2476 | if (flags & XATTR_CREATE) | |
2477 | goto cleanup; | |
2478 | } | |
2479 | ||
85db90e7 TM |
2480 | |
2481 | mutex_lock(&tl_inode->i_mutex); | |
2482 | ||
2483 | if (ocfs2_truncate_log_needs_flush(osb)) { | |
2484 | ret = __ocfs2_flush_truncate_log(osb); | |
2485 | if (ret < 0) { | |
2486 | mutex_unlock(&tl_inode->i_mutex); | |
2487 | mlog_errno(ret); | |
2488 | goto cleanup; | |
2489 | } | |
2490 | } | |
2491 | mutex_unlock(&tl_inode->i_mutex); | |
2492 | ||
2493 | ret = ocfs2_init_xattr_set_ctxt(inode, di, &xi, &xis, | |
2494 | &xbs, &ctxt, &credits); | |
78f30c31 TM |
2495 | if (ret) { |
2496 | mlog_errno(ret); | |
2497 | goto cleanup; | |
2498 | } | |
2499 | ||
85db90e7 TM |
2500 | ctxt.handle = ocfs2_start_trans(osb, credits); |
2501 | if (IS_ERR(ctxt.handle)) { | |
2502 | ret = PTR_ERR(ctxt.handle); | |
2503 | mlog_errno(ret); | |
2504 | goto cleanup; | |
cf1d6c76 | 2505 | } |
85db90e7 TM |
2506 | |
2507 | ret = __ocfs2_xattr_set_handle(inode, di, &xi, &xis, &xbs, &ctxt); | |
2508 | ||
2509 | ocfs2_commit_trans(osb, ctxt.handle); | |
2510 | ||
78f30c31 TM |
2511 | if (ctxt.data_ac) |
2512 | ocfs2_free_alloc_context(ctxt.data_ac); | |
2513 | if (ctxt.meta_ac) | |
2514 | ocfs2_free_alloc_context(ctxt.meta_ac); | |
2515 | if (ocfs2_dealloc_has_cluster(&ctxt.dealloc)) | |
2516 | ocfs2_schedule_truncate_log_flush(osb, 1); | |
2517 | ocfs2_run_deallocs(osb, &ctxt.dealloc); | |
cf1d6c76 TY |
2518 | cleanup: |
2519 | up_write(&OCFS2_I(inode)->ip_xattr_sem); | |
2520 | ocfs2_inode_unlock(inode, 1); | |
ba937127 | 2521 | cleanup_nolock: |
cf1d6c76 TY |
2522 | brelse(di_bh); |
2523 | brelse(xbs.xattr_bh); | |
ba937127 | 2524 | ocfs2_xattr_bucket_free(xbs.bucket); |
cf1d6c76 TY |
2525 | |
2526 | return ret; | |
2527 | } | |
2528 | ||
0c044f0b TM |
2529 | /* |
2530 | * Find the xattr extent rec which may contains name_hash. | |
2531 | * e_cpos will be the first name hash of the xattr rec. | |
2532 | * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list. | |
2533 | */ | |
2534 | static int ocfs2_xattr_get_rec(struct inode *inode, | |
2535 | u32 name_hash, | |
2536 | u64 *p_blkno, | |
2537 | u32 *e_cpos, | |
2538 | u32 *num_clusters, | |
2539 | struct ocfs2_extent_list *el) | |
2540 | { | |
2541 | int ret = 0, i; | |
2542 | struct buffer_head *eb_bh = NULL; | |
2543 | struct ocfs2_extent_block *eb; | |
2544 | struct ocfs2_extent_rec *rec = NULL; | |
2545 | u64 e_blkno = 0; | |
2546 | ||
2547 | if (el->l_tree_depth) { | |
2548 | ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh); | |
2549 | if (ret) { | |
2550 | mlog_errno(ret); | |
2551 | goto out; | |
2552 | } | |
2553 | ||
2554 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; | |
2555 | el = &eb->h_list; | |
2556 | ||
2557 | if (el->l_tree_depth) { | |
2558 | ocfs2_error(inode->i_sb, | |
2559 | "Inode %lu has non zero tree depth in " | |
2560 | "xattr tree block %llu\n", inode->i_ino, | |
2561 | (unsigned long long)eb_bh->b_blocknr); | |
2562 | ret = -EROFS; | |
2563 | goto out; | |
2564 | } | |
2565 | } | |
2566 | ||
2567 | for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { | |
2568 | rec = &el->l_recs[i]; | |
2569 | ||
2570 | if (le32_to_cpu(rec->e_cpos) <= name_hash) { | |
2571 | e_blkno = le64_to_cpu(rec->e_blkno); | |
2572 | break; | |
2573 | } | |
2574 | } | |
2575 | ||
2576 | if (!e_blkno) { | |
2577 | ocfs2_error(inode->i_sb, "Inode %lu has bad extent " | |
2578 | "record (%u, %u, 0) in xattr", inode->i_ino, | |
2579 | le32_to_cpu(rec->e_cpos), | |
2580 | ocfs2_rec_clusters(el, rec)); | |
2581 | ret = -EROFS; | |
2582 | goto out; | |
2583 | } | |
2584 | ||
2585 | *p_blkno = le64_to_cpu(rec->e_blkno); | |
2586 | *num_clusters = le16_to_cpu(rec->e_leaf_clusters); | |
2587 | if (e_cpos) | |
2588 | *e_cpos = le32_to_cpu(rec->e_cpos); | |
2589 | out: | |
2590 | brelse(eb_bh); | |
2591 | return ret; | |
2592 | } | |
2593 | ||
2594 | typedef int (xattr_bucket_func)(struct inode *inode, | |
2595 | struct ocfs2_xattr_bucket *bucket, | |
2596 | void *para); | |
2597 | ||
589dc260 | 2598 | static int ocfs2_find_xe_in_bucket(struct inode *inode, |
e2356a3f | 2599 | struct ocfs2_xattr_bucket *bucket, |
589dc260 TM |
2600 | int name_index, |
2601 | const char *name, | |
2602 | u32 name_hash, | |
2603 | u16 *xe_index, | |
2604 | int *found) | |
2605 | { | |
2606 | int i, ret = 0, cmp = 1, block_off, new_offset; | |
e2356a3f | 2607 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
589dc260 TM |
2608 | size_t name_len = strlen(name); |
2609 | struct ocfs2_xattr_entry *xe = NULL; | |
589dc260 TM |
2610 | char *xe_name; |
2611 | ||
2612 | /* | |
2613 | * We don't use binary search in the bucket because there | |
2614 | * may be multiple entries with the same name hash. | |
2615 | */ | |
2616 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { | |
2617 | xe = &xh->xh_entries[i]; | |
2618 | ||
2619 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) | |
2620 | continue; | |
2621 | else if (name_hash < le32_to_cpu(xe->xe_name_hash)) | |
2622 | break; | |
2623 | ||
2624 | cmp = name_index - ocfs2_xattr_get_type(xe); | |
2625 | if (!cmp) | |
2626 | cmp = name_len - xe->xe_name_len; | |
2627 | if (cmp) | |
2628 | continue; | |
2629 | ||
2630 | ret = ocfs2_xattr_bucket_get_name_value(inode, | |
2631 | xh, | |
2632 | i, | |
2633 | &block_off, | |
2634 | &new_offset); | |
2635 | if (ret) { | |
2636 | mlog_errno(ret); | |
2637 | break; | |
2638 | } | |
2639 | ||
e2356a3f JB |
2640 | xe_name = bucket_block(bucket, block_off) + new_offset; |
2641 | if (!memcmp(name, xe_name, name_len)) { | |
589dc260 TM |
2642 | *xe_index = i; |
2643 | *found = 1; | |
2644 | ret = 0; | |
2645 | break; | |
2646 | } | |
2647 | } | |
2648 | ||
2649 | return ret; | |
2650 | } | |
2651 | ||
2652 | /* | |
2653 | * Find the specified xattr entry in a series of buckets. | |
2654 | * This series start from p_blkno and last for num_clusters. | |
2655 | * The ocfs2_xattr_header.xh_num_buckets of the first bucket contains | |
2656 | * the num of the valid buckets. | |
2657 | * | |
2658 | * Return the buffer_head this xattr should reside in. And if the xattr's | |
2659 | * hash is in the gap of 2 buckets, return the lower bucket. | |
2660 | */ | |
2661 | static int ocfs2_xattr_bucket_find(struct inode *inode, | |
2662 | int name_index, | |
2663 | const char *name, | |
2664 | u32 name_hash, | |
2665 | u64 p_blkno, | |
2666 | u32 first_hash, | |
2667 | u32 num_clusters, | |
2668 | struct ocfs2_xattr_search *xs) | |
2669 | { | |
2670 | int ret, found = 0; | |
589dc260 TM |
2671 | struct ocfs2_xattr_header *xh = NULL; |
2672 | struct ocfs2_xattr_entry *xe = NULL; | |
2673 | u16 index = 0; | |
2674 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
2675 | int low_bucket = 0, bucket, high_bucket; | |
e2356a3f | 2676 | struct ocfs2_xattr_bucket *search; |
589dc260 | 2677 | u32 last_hash; |
e2356a3f | 2678 | u64 blkno, lower_blkno = 0; |
589dc260 | 2679 | |
e2356a3f JB |
2680 | search = ocfs2_xattr_bucket_new(inode); |
2681 | if (!search) { | |
2682 | ret = -ENOMEM; | |
2683 | mlog_errno(ret); | |
2684 | goto out; | |
2685 | } | |
2686 | ||
2687 | ret = ocfs2_read_xattr_bucket(search, p_blkno); | |
589dc260 TM |
2688 | if (ret) { |
2689 | mlog_errno(ret); | |
2690 | goto out; | |
2691 | } | |
2692 | ||
e2356a3f | 2693 | xh = bucket_xh(search); |
589dc260 | 2694 | high_bucket = le16_to_cpu(xh->xh_num_buckets) - 1; |
589dc260 | 2695 | while (low_bucket <= high_bucket) { |
e2356a3f | 2696 | ocfs2_xattr_bucket_relse(search); |
589dc260 | 2697 | |
e2356a3f | 2698 | bucket = (low_bucket + high_bucket) / 2; |
589dc260 | 2699 | blkno = p_blkno + bucket * blk_per_bucket; |
e2356a3f | 2700 | ret = ocfs2_read_xattr_bucket(search, blkno); |
589dc260 TM |
2701 | if (ret) { |
2702 | mlog_errno(ret); | |
2703 | goto out; | |
2704 | } | |
2705 | ||
e2356a3f | 2706 | xh = bucket_xh(search); |
589dc260 TM |
2707 | xe = &xh->xh_entries[0]; |
2708 | if (name_hash < le32_to_cpu(xe->xe_name_hash)) { | |
2709 | high_bucket = bucket - 1; | |
2710 | continue; | |
2711 | } | |
2712 | ||
2713 | /* | |
2714 | * Check whether the hash of the last entry in our | |
5a095611 TM |
2715 | * bucket is larger than the search one. for an empty |
2716 | * bucket, the last one is also the first one. | |
589dc260 | 2717 | */ |
5a095611 TM |
2718 | if (xh->xh_count) |
2719 | xe = &xh->xh_entries[le16_to_cpu(xh->xh_count) - 1]; | |
2720 | ||
589dc260 TM |
2721 | last_hash = le32_to_cpu(xe->xe_name_hash); |
2722 | ||
e2356a3f JB |
2723 | /* record lower_blkno which may be the insert place. */ |
2724 | lower_blkno = blkno; | |
589dc260 TM |
2725 | |
2726 | if (name_hash > le32_to_cpu(xe->xe_name_hash)) { | |
2727 | low_bucket = bucket + 1; | |
2728 | continue; | |
2729 | } | |
2730 | ||
2731 | /* the searched xattr should reside in this bucket if exists. */ | |
e2356a3f | 2732 | ret = ocfs2_find_xe_in_bucket(inode, search, |
589dc260 TM |
2733 | name_index, name, name_hash, |
2734 | &index, &found); | |
2735 | if (ret) { | |
2736 | mlog_errno(ret); | |
2737 | goto out; | |
2738 | } | |
2739 | break; | |
2740 | } | |
2741 | ||
2742 | /* | |
2743 | * Record the bucket we have found. | |
2744 | * When the xattr's hash value is in the gap of 2 buckets, we will | |
2745 | * always set it to the previous bucket. | |
2746 | */ | |
e2356a3f JB |
2747 | if (!lower_blkno) |
2748 | lower_blkno = p_blkno; | |
2749 | ||
2750 | /* This should be in cache - we just read it during the search */ | |
2751 | ret = ocfs2_read_xattr_bucket(xs->bucket, lower_blkno); | |
2752 | if (ret) { | |
2753 | mlog_errno(ret); | |
2754 | goto out; | |
589dc260 | 2755 | } |
589dc260 | 2756 | |
ba937127 JB |
2757 | xs->header = bucket_xh(xs->bucket); |
2758 | xs->base = bucket_block(xs->bucket, 0); | |
589dc260 TM |
2759 | xs->end = xs->base + inode->i_sb->s_blocksize; |
2760 | ||
2761 | if (found) { | |
589dc260 TM |
2762 | xs->here = &xs->header->xh_entries[index]; |
2763 | mlog(0, "find xattr %s in bucket %llu, entry = %u\n", name, | |
ba937127 | 2764 | (unsigned long long)bucket_blkno(xs->bucket), index); |
589dc260 TM |
2765 | } else |
2766 | ret = -ENODATA; | |
2767 | ||
2768 | out: | |
e2356a3f | 2769 | ocfs2_xattr_bucket_free(search); |
589dc260 TM |
2770 | return ret; |
2771 | } | |
2772 | ||
2773 | static int ocfs2_xattr_index_block_find(struct inode *inode, | |
2774 | struct buffer_head *root_bh, | |
2775 | int name_index, | |
2776 | const char *name, | |
2777 | struct ocfs2_xattr_search *xs) | |
2778 | { | |
2779 | int ret; | |
2780 | struct ocfs2_xattr_block *xb = | |
2781 | (struct ocfs2_xattr_block *)root_bh->b_data; | |
2782 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; | |
2783 | struct ocfs2_extent_list *el = &xb_root->xt_list; | |
2784 | u64 p_blkno = 0; | |
2785 | u32 first_hash, num_clusters = 0; | |
2057e5c6 | 2786 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
589dc260 TM |
2787 | |
2788 | if (le16_to_cpu(el->l_next_free_rec) == 0) | |
2789 | return -ENODATA; | |
2790 | ||
2791 | mlog(0, "find xattr %s, hash = %u, index = %d in xattr tree\n", | |
2792 | name, name_hash, name_index); | |
2793 | ||
2794 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &first_hash, | |
2795 | &num_clusters, el); | |
2796 | if (ret) { | |
2797 | mlog_errno(ret); | |
2798 | goto out; | |
2799 | } | |
2800 | ||
2801 | BUG_ON(p_blkno == 0 || num_clusters == 0 || first_hash > name_hash); | |
2802 | ||
2803 | mlog(0, "find xattr extent rec %u clusters from %llu, the first hash " | |
de29c085 MF |
2804 | "in the rec is %u\n", num_clusters, (unsigned long long)p_blkno, |
2805 | first_hash); | |
589dc260 TM |
2806 | |
2807 | ret = ocfs2_xattr_bucket_find(inode, name_index, name, name_hash, | |
2808 | p_blkno, first_hash, num_clusters, xs); | |
2809 | ||
2810 | out: | |
2811 | return ret; | |
2812 | } | |
2813 | ||
0c044f0b TM |
2814 | static int ocfs2_iterate_xattr_buckets(struct inode *inode, |
2815 | u64 blkno, | |
2816 | u32 clusters, | |
2817 | xattr_bucket_func *func, | |
2818 | void *para) | |
2819 | { | |
6dde41d9 | 2820 | int i, ret = 0; |
0c044f0b TM |
2821 | u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)); |
2822 | u32 num_buckets = clusters * bpc; | |
ba937127 | 2823 | struct ocfs2_xattr_bucket *bucket; |
0c044f0b | 2824 | |
ba937127 JB |
2825 | bucket = ocfs2_xattr_bucket_new(inode); |
2826 | if (!bucket) { | |
2827 | mlog_errno(-ENOMEM); | |
2828 | return -ENOMEM; | |
2829 | } | |
0c044f0b TM |
2830 | |
2831 | mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n", | |
de29c085 | 2832 | clusters, (unsigned long long)blkno); |
0c044f0b | 2833 | |
ba937127 JB |
2834 | for (i = 0; i < num_buckets; i++, blkno += bucket->bu_blocks) { |
2835 | ret = ocfs2_read_xattr_bucket(bucket, blkno); | |
0c044f0b TM |
2836 | if (ret) { |
2837 | mlog_errno(ret); | |
ba937127 | 2838 | break; |
0c044f0b TM |
2839 | } |
2840 | ||
0c044f0b TM |
2841 | /* |
2842 | * The real bucket num in this series of blocks is stored | |
2843 | * in the 1st bucket. | |
2844 | */ | |
2845 | if (i == 0) | |
ba937127 | 2846 | num_buckets = le16_to_cpu(bucket_xh(bucket)->xh_num_buckets); |
0c044f0b | 2847 | |
de29c085 MF |
2848 | mlog(0, "iterating xattr bucket %llu, first hash %u\n", |
2849 | (unsigned long long)blkno, | |
ba937127 | 2850 | le32_to_cpu(bucket_xh(bucket)->xh_entries[0].xe_name_hash)); |
0c044f0b | 2851 | if (func) { |
ba937127 JB |
2852 | ret = func(inode, bucket, para); |
2853 | if (ret) | |
0c044f0b | 2854 | mlog_errno(ret); |
ba937127 | 2855 | /* Fall through to bucket_relse() */ |
0c044f0b TM |
2856 | } |
2857 | ||
ba937127 JB |
2858 | ocfs2_xattr_bucket_relse(bucket); |
2859 | if (ret) | |
2860 | break; | |
0c044f0b TM |
2861 | } |
2862 | ||
ba937127 | 2863 | ocfs2_xattr_bucket_free(bucket); |
0c044f0b TM |
2864 | return ret; |
2865 | } | |
2866 | ||
2867 | struct ocfs2_xattr_tree_list { | |
2868 | char *buffer; | |
2869 | size_t buffer_size; | |
936b8834 | 2870 | size_t result; |
0c044f0b TM |
2871 | }; |
2872 | ||
2873 | static int ocfs2_xattr_bucket_get_name_value(struct inode *inode, | |
2874 | struct ocfs2_xattr_header *xh, | |
2875 | int index, | |
2876 | int *block_off, | |
2877 | int *new_offset) | |
2878 | { | |
2879 | u16 name_offset; | |
2880 | ||
2881 | if (index < 0 || index >= le16_to_cpu(xh->xh_count)) | |
2882 | return -EINVAL; | |
2883 | ||
2884 | name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset); | |
2885 | ||
2886 | *block_off = name_offset >> inode->i_sb->s_blocksize_bits; | |
2887 | *new_offset = name_offset % inode->i_sb->s_blocksize; | |
2888 | ||
2889 | return 0; | |
2890 | } | |
2891 | ||
2892 | static int ocfs2_list_xattr_bucket(struct inode *inode, | |
2893 | struct ocfs2_xattr_bucket *bucket, | |
2894 | void *para) | |
2895 | { | |
936b8834 | 2896 | int ret = 0, type; |
0c044f0b | 2897 | struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para; |
0c044f0b | 2898 | int i, block_off, new_offset; |
936b8834 | 2899 | const char *prefix, *name; |
0c044f0b | 2900 | |
3e632946 JB |
2901 | for (i = 0 ; i < le16_to_cpu(bucket_xh(bucket)->xh_count); i++) { |
2902 | struct ocfs2_xattr_entry *entry = &bucket_xh(bucket)->xh_entries[i]; | |
936b8834 TM |
2903 | type = ocfs2_xattr_get_type(entry); |
2904 | prefix = ocfs2_xattr_prefix(type); | |
0c044f0b | 2905 | |
936b8834 | 2906 | if (prefix) { |
0c044f0b | 2907 | ret = ocfs2_xattr_bucket_get_name_value(inode, |
3e632946 | 2908 | bucket_xh(bucket), |
0c044f0b TM |
2909 | i, |
2910 | &block_off, | |
2911 | &new_offset); | |
2912 | if (ret) | |
2913 | break; | |
936b8834 | 2914 | |
51def39f | 2915 | name = (const char *)bucket_block(bucket, block_off) + |
936b8834 TM |
2916 | new_offset; |
2917 | ret = ocfs2_xattr_list_entry(xl->buffer, | |
2918 | xl->buffer_size, | |
2919 | &xl->result, | |
2920 | prefix, name, | |
2921 | entry->xe_name_len); | |
2922 | if (ret) | |
2923 | break; | |
0c044f0b TM |
2924 | } |
2925 | } | |
2926 | ||
2927 | return ret; | |
2928 | } | |
2929 | ||
2930 | static int ocfs2_xattr_tree_list_index_block(struct inode *inode, | |
2931 | struct ocfs2_xattr_tree_root *xt, | |
2932 | char *buffer, | |
2933 | size_t buffer_size) | |
2934 | { | |
2935 | struct ocfs2_extent_list *el = &xt->xt_list; | |
2936 | int ret = 0; | |
2937 | u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0; | |
2938 | u64 p_blkno = 0; | |
2939 | struct ocfs2_xattr_tree_list xl = { | |
2940 | .buffer = buffer, | |
2941 | .buffer_size = buffer_size, | |
936b8834 | 2942 | .result = 0, |
0c044f0b TM |
2943 | }; |
2944 | ||
2945 | if (le16_to_cpu(el->l_next_free_rec) == 0) | |
2946 | return 0; | |
2947 | ||
2948 | while (name_hash > 0) { | |
2949 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, | |
2950 | &e_cpos, &num_clusters, el); | |
2951 | if (ret) { | |
2952 | mlog_errno(ret); | |
2953 | goto out; | |
2954 | } | |
2955 | ||
2956 | ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters, | |
2957 | ocfs2_list_xattr_bucket, | |
2958 | &xl); | |
2959 | if (ret) { | |
2960 | mlog_errno(ret); | |
2961 | goto out; | |
2962 | } | |
2963 | ||
2964 | if (e_cpos == 0) | |
2965 | break; | |
2966 | ||
2967 | name_hash = e_cpos - 1; | |
2968 | } | |
2969 | ||
936b8834 | 2970 | ret = xl.result; |
0c044f0b TM |
2971 | out: |
2972 | return ret; | |
2973 | } | |
01225596 TM |
2974 | |
2975 | static int cmp_xe(const void *a, const void *b) | |
2976 | { | |
2977 | const struct ocfs2_xattr_entry *l = a, *r = b; | |
2978 | u32 l_hash = le32_to_cpu(l->xe_name_hash); | |
2979 | u32 r_hash = le32_to_cpu(r->xe_name_hash); | |
2980 | ||
2981 | if (l_hash > r_hash) | |
2982 | return 1; | |
2983 | if (l_hash < r_hash) | |
2984 | return -1; | |
2985 | return 0; | |
2986 | } | |
2987 | ||
2988 | static void swap_xe(void *a, void *b, int size) | |
2989 | { | |
2990 | struct ocfs2_xattr_entry *l = a, *r = b, tmp; | |
2991 | ||
2992 | tmp = *l; | |
2993 | memcpy(l, r, sizeof(struct ocfs2_xattr_entry)); | |
2994 | memcpy(r, &tmp, sizeof(struct ocfs2_xattr_entry)); | |
2995 | } | |
2996 | ||
2997 | /* | |
2998 | * When the ocfs2_xattr_block is filled up, new bucket will be created | |
2999 | * and all the xattr entries will be moved to the new bucket. | |
178eeac3 JB |
3000 | * The header goes at the start of the bucket, and the names+values are |
3001 | * filled from the end. This is why *target starts as the last buffer. | |
01225596 TM |
3002 | * Note: we need to sort the entries since they are not saved in order |
3003 | * in the ocfs2_xattr_block. | |
3004 | */ | |
3005 | static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode, | |
3006 | struct buffer_head *xb_bh, | |
178eeac3 | 3007 | struct ocfs2_xattr_bucket *bucket) |
01225596 TM |
3008 | { |
3009 | int i, blocksize = inode->i_sb->s_blocksize; | |
178eeac3 | 3010 | int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb); |
01225596 TM |
3011 | u16 offset, size, off_change; |
3012 | struct ocfs2_xattr_entry *xe; | |
3013 | struct ocfs2_xattr_block *xb = | |
3014 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
3015 | struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header; | |
178eeac3 | 3016 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
01225596 | 3017 | u16 count = le16_to_cpu(xb_xh->xh_count); |
178eeac3 JB |
3018 | char *src = xb_bh->b_data; |
3019 | char *target = bucket_block(bucket, blks - 1); | |
01225596 TM |
3020 | |
3021 | mlog(0, "cp xattr from block %llu to bucket %llu\n", | |
3022 | (unsigned long long)xb_bh->b_blocknr, | |
178eeac3 JB |
3023 | (unsigned long long)bucket_blkno(bucket)); |
3024 | ||
3025 | for (i = 0; i < blks; i++) | |
3026 | memset(bucket_block(bucket, i), 0, blocksize); | |
01225596 | 3027 | |
01225596 TM |
3028 | /* |
3029 | * Since the xe_name_offset is based on ocfs2_xattr_header, | |
3030 | * there is a offset change corresponding to the change of | |
3031 | * ocfs2_xattr_header's position. | |
3032 | */ | |
3033 | off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); | |
3034 | xe = &xb_xh->xh_entries[count - 1]; | |
3035 | offset = le16_to_cpu(xe->xe_name_offset) + off_change; | |
3036 | size = blocksize - offset; | |
3037 | ||
3038 | /* copy all the names and values. */ | |
01225596 TM |
3039 | memcpy(target + offset, src + offset, size); |
3040 | ||
3041 | /* Init new header now. */ | |
3042 | xh->xh_count = xb_xh->xh_count; | |
3043 | xh->xh_num_buckets = cpu_to_le16(1); | |
3044 | xh->xh_name_value_len = cpu_to_le16(size); | |
3045 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE - size); | |
3046 | ||
3047 | /* copy all the entries. */ | |
178eeac3 | 3048 | target = bucket_block(bucket, 0); |
01225596 TM |
3049 | offset = offsetof(struct ocfs2_xattr_header, xh_entries); |
3050 | size = count * sizeof(struct ocfs2_xattr_entry); | |
3051 | memcpy(target + offset, (char *)xb_xh + offset, size); | |
3052 | ||
3053 | /* Change the xe offset for all the xe because of the move. */ | |
3054 | off_change = OCFS2_XATTR_BUCKET_SIZE - blocksize + | |
3055 | offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header); | |
3056 | for (i = 0; i < count; i++) | |
3057 | le16_add_cpu(&xh->xh_entries[i].xe_name_offset, off_change); | |
3058 | ||
3059 | mlog(0, "copy entry: start = %u, size = %u, offset_change = %u\n", | |
3060 | offset, size, off_change); | |
3061 | ||
3062 | sort(target + offset, count, sizeof(struct ocfs2_xattr_entry), | |
3063 | cmp_xe, swap_xe); | |
3064 | } | |
3065 | ||
3066 | /* | |
3067 | * After we move xattr from block to index btree, we have to | |
3068 | * update ocfs2_xattr_search to the new xe and base. | |
3069 | * | |
3070 | * When the entry is in xattr block, xattr_bh indicates the storage place. | |
3071 | * While if the entry is in index b-tree, "bucket" indicates the | |
3072 | * real place of the xattr. | |
3073 | */ | |
178eeac3 JB |
3074 | static void ocfs2_xattr_update_xattr_search(struct inode *inode, |
3075 | struct ocfs2_xattr_search *xs, | |
3076 | struct buffer_head *old_bh) | |
01225596 | 3077 | { |
01225596 TM |
3078 | char *buf = old_bh->b_data; |
3079 | struct ocfs2_xattr_block *old_xb = (struct ocfs2_xattr_block *)buf; | |
3080 | struct ocfs2_xattr_header *old_xh = &old_xb->xb_attrs.xb_header; | |
178eeac3 | 3081 | int i; |
01225596 | 3082 | |
ba937127 | 3083 | xs->header = bucket_xh(xs->bucket); |
178eeac3 | 3084 | xs->base = bucket_block(xs->bucket, 0); |
01225596 TM |
3085 | xs->end = xs->base + inode->i_sb->s_blocksize; |
3086 | ||
178eeac3 JB |
3087 | if (xs->not_found) |
3088 | return; | |
01225596 | 3089 | |
178eeac3 JB |
3090 | i = xs->here - old_xh->xh_entries; |
3091 | xs->here = &xs->header->xh_entries[i]; | |
01225596 TM |
3092 | } |
3093 | ||
3094 | static int ocfs2_xattr_create_index_block(struct inode *inode, | |
78f30c31 TM |
3095 | struct ocfs2_xattr_search *xs, |
3096 | struct ocfs2_xattr_set_ctxt *ctxt) | |
01225596 | 3097 | { |
85db90e7 | 3098 | int ret; |
01225596 TM |
3099 | u32 bit_off, len; |
3100 | u64 blkno; | |
85db90e7 | 3101 | handle_t *handle = ctxt->handle; |
01225596 TM |
3102 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
3103 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
01225596 TM |
3104 | struct buffer_head *xb_bh = xs->xattr_bh; |
3105 | struct ocfs2_xattr_block *xb = | |
3106 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
3107 | struct ocfs2_xattr_tree_root *xr; | |
3108 | u16 xb_flags = le16_to_cpu(xb->xb_flags); | |
01225596 TM |
3109 | |
3110 | mlog(0, "create xattr index block for %llu\n", | |
3111 | (unsigned long long)xb_bh->b_blocknr); | |
3112 | ||
3113 | BUG_ON(xb_flags & OCFS2_XATTR_INDEXED); | |
178eeac3 | 3114 | BUG_ON(!xs->bucket); |
01225596 | 3115 | |
01225596 TM |
3116 | /* |
3117 | * XXX: | |
3118 | * We can use this lock for now, and maybe move to a dedicated mutex | |
3119 | * if performance becomes a problem later. | |
3120 | */ | |
3121 | down_write(&oi->ip_alloc_sem); | |
3122 | ||
01225596 TM |
3123 | ret = ocfs2_journal_access(handle, inode, xb_bh, |
3124 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3125 | if (ret) { | |
3126 | mlog_errno(ret); | |
85db90e7 | 3127 | goto out; |
01225596 TM |
3128 | } |
3129 | ||
78f30c31 TM |
3130 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, |
3131 | 1, 1, &bit_off, &len); | |
01225596 TM |
3132 | if (ret) { |
3133 | mlog_errno(ret); | |
85db90e7 | 3134 | goto out; |
01225596 TM |
3135 | } |
3136 | ||
3137 | /* | |
3138 | * The bucket may spread in many blocks, and | |
3139 | * we will only touch the 1st block and the last block | |
3140 | * in the whole bucket(one for entry and one for data). | |
3141 | */ | |
3142 | blkno = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); | |
3143 | ||
de29c085 MF |
3144 | mlog(0, "allocate 1 cluster from %llu to xattr block\n", |
3145 | (unsigned long long)blkno); | |
01225596 | 3146 | |
178eeac3 JB |
3147 | ret = ocfs2_init_xattr_bucket(xs->bucket, blkno); |
3148 | if (ret) { | |
01225596 | 3149 | mlog_errno(ret); |
85db90e7 | 3150 | goto out; |
01225596 TM |
3151 | } |
3152 | ||
178eeac3 JB |
3153 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
3154 | OCFS2_JOURNAL_ACCESS_CREATE); | |
01225596 TM |
3155 | if (ret) { |
3156 | mlog_errno(ret); | |
85db90e7 | 3157 | goto out; |
01225596 TM |
3158 | } |
3159 | ||
178eeac3 JB |
3160 | ocfs2_cp_xattr_block_to_bucket(inode, xb_bh, xs->bucket); |
3161 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); | |
01225596 | 3162 | |
178eeac3 | 3163 | ocfs2_xattr_update_xattr_search(inode, xs, xb_bh); |
01225596 TM |
3164 | |
3165 | /* Change from ocfs2_xattr_header to ocfs2_xattr_tree_root */ | |
3166 | memset(&xb->xb_attrs, 0, inode->i_sb->s_blocksize - | |
3167 | offsetof(struct ocfs2_xattr_block, xb_attrs)); | |
3168 | ||
3169 | xr = &xb->xb_attrs.xb_root; | |
3170 | xr->xt_clusters = cpu_to_le32(1); | |
3171 | xr->xt_last_eb_blk = 0; | |
3172 | xr->xt_list.l_tree_depth = 0; | |
3173 | xr->xt_list.l_count = cpu_to_le16(ocfs2_xattr_recs_per_xb(inode->i_sb)); | |
3174 | xr->xt_list.l_next_free_rec = cpu_to_le16(1); | |
3175 | ||
3176 | xr->xt_list.l_recs[0].e_cpos = 0; | |
3177 | xr->xt_list.l_recs[0].e_blkno = cpu_to_le64(blkno); | |
3178 | xr->xt_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1); | |
3179 | ||
3180 | xb->xb_flags = cpu_to_le16(xb_flags | OCFS2_XATTR_INDEXED); | |
3181 | ||
85db90e7 | 3182 | ocfs2_journal_dirty(handle, xb_bh); |
01225596 | 3183 | |
85db90e7 | 3184 | out: |
01225596 TM |
3185 | up_write(&oi->ip_alloc_sem); |
3186 | ||
01225596 TM |
3187 | return ret; |
3188 | } | |
3189 | ||
3190 | static int cmp_xe_offset(const void *a, const void *b) | |
3191 | { | |
3192 | const struct ocfs2_xattr_entry *l = a, *r = b; | |
3193 | u32 l_name_offset = le16_to_cpu(l->xe_name_offset); | |
3194 | u32 r_name_offset = le16_to_cpu(r->xe_name_offset); | |
3195 | ||
3196 | if (l_name_offset < r_name_offset) | |
3197 | return 1; | |
3198 | if (l_name_offset > r_name_offset) | |
3199 | return -1; | |
3200 | return 0; | |
3201 | } | |
3202 | ||
3203 | /* | |
3204 | * defrag a xattr bucket if we find that the bucket has some | |
3205 | * holes beteen name/value pairs. | |
3206 | * We will move all the name/value pairs to the end of the bucket | |
3207 | * so that we can spare some space for insertion. | |
3208 | */ | |
3209 | static int ocfs2_defrag_xattr_bucket(struct inode *inode, | |
85db90e7 | 3210 | handle_t *handle, |
01225596 TM |
3211 | struct ocfs2_xattr_bucket *bucket) |
3212 | { | |
3213 | int ret, i; | |
3214 | size_t end, offset, len, value_len; | |
3215 | struct ocfs2_xattr_header *xh; | |
3216 | char *entries, *buf, *bucket_buf = NULL; | |
9c7759aa | 3217 | u64 blkno = bucket_blkno(bucket); |
01225596 | 3218 | u16 xh_free_start; |
01225596 | 3219 | size_t blocksize = inode->i_sb->s_blocksize; |
01225596 | 3220 | struct ocfs2_xattr_entry *xe; |
01225596 TM |
3221 | |
3222 | /* | |
3223 | * In order to make the operation more efficient and generic, | |
3224 | * we copy all the blocks into a contiguous memory and do the | |
3225 | * defragment there, so if anything is error, we will not touch | |
3226 | * the real block. | |
3227 | */ | |
3228 | bucket_buf = kmalloc(OCFS2_XATTR_BUCKET_SIZE, GFP_NOFS); | |
3229 | if (!bucket_buf) { | |
3230 | ret = -EIO; | |
3231 | goto out; | |
3232 | } | |
3233 | ||
3234 | buf = bucket_buf; | |
1c32a2fd TM |
3235 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
3236 | memcpy(buf, bucket_block(bucket, i), blocksize); | |
01225596 | 3237 | |
1c32a2fd | 3238 | ret = ocfs2_xattr_bucket_journal_access(handle, bucket, |
161d6f30 JB |
3239 | OCFS2_JOURNAL_ACCESS_WRITE); |
3240 | if (ret < 0) { | |
3241 | mlog_errno(ret); | |
85db90e7 | 3242 | goto out; |
01225596 TM |
3243 | } |
3244 | ||
3245 | xh = (struct ocfs2_xattr_header *)bucket_buf; | |
3246 | entries = (char *)xh->xh_entries; | |
3247 | xh_free_start = le16_to_cpu(xh->xh_free_start); | |
3248 | ||
3249 | mlog(0, "adjust xattr bucket in %llu, count = %u, " | |
3250 | "xh_free_start = %u, xh_name_value_len = %u.\n", | |
de29c085 MF |
3251 | (unsigned long long)blkno, le16_to_cpu(xh->xh_count), |
3252 | xh_free_start, le16_to_cpu(xh->xh_name_value_len)); | |
01225596 TM |
3253 | |
3254 | /* | |
3255 | * sort all the entries by their offset. | |
3256 | * the largest will be the first, so that we can | |
3257 | * move them to the end one by one. | |
3258 | */ | |
3259 | sort(entries, le16_to_cpu(xh->xh_count), | |
3260 | sizeof(struct ocfs2_xattr_entry), | |
3261 | cmp_xe_offset, swap_xe); | |
3262 | ||
3263 | /* Move all name/values to the end of the bucket. */ | |
3264 | xe = xh->xh_entries; | |
3265 | end = OCFS2_XATTR_BUCKET_SIZE; | |
3266 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { | |
3267 | offset = le16_to_cpu(xe->xe_name_offset); | |
3268 | if (ocfs2_xattr_is_local(xe)) | |
3269 | value_len = OCFS2_XATTR_SIZE( | |
3270 | le64_to_cpu(xe->xe_value_size)); | |
3271 | else | |
3272 | value_len = OCFS2_XATTR_ROOT_SIZE; | |
3273 | len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len; | |
3274 | ||
3275 | /* | |
3276 | * We must make sure that the name/value pair | |
3277 | * exist in the same block. So adjust end to | |
3278 | * the previous block end if needed. | |
3279 | */ | |
3280 | if (((end - len) / blocksize != | |
3281 | (end - 1) / blocksize)) | |
3282 | end = end - end % blocksize; | |
3283 | ||
3284 | if (end > offset + len) { | |
3285 | memmove(bucket_buf + end - len, | |
3286 | bucket_buf + offset, len); | |
3287 | xe->xe_name_offset = cpu_to_le16(end - len); | |
3288 | } | |
3289 | ||
3290 | mlog_bug_on_msg(end < offset + len, "Defrag check failed for " | |
3291 | "bucket %llu\n", (unsigned long long)blkno); | |
3292 | ||
3293 | end -= len; | |
3294 | } | |
3295 | ||
3296 | mlog_bug_on_msg(xh_free_start > end, "Defrag check failed for " | |
3297 | "bucket %llu\n", (unsigned long long)blkno); | |
3298 | ||
3299 | if (xh_free_start == end) | |
85db90e7 | 3300 | goto out; |
01225596 TM |
3301 | |
3302 | memset(bucket_buf + xh_free_start, 0, end - xh_free_start); | |
3303 | xh->xh_free_start = cpu_to_le16(end); | |
3304 | ||
3305 | /* sort the entries by their name_hash. */ | |
3306 | sort(entries, le16_to_cpu(xh->xh_count), | |
3307 | sizeof(struct ocfs2_xattr_entry), | |
3308 | cmp_xe, swap_xe); | |
3309 | ||
3310 | buf = bucket_buf; | |
1c32a2fd TM |
3311 | for (i = 0; i < bucket->bu_blocks; i++, buf += blocksize) |
3312 | memcpy(bucket_block(bucket, i), buf, blocksize); | |
3313 | ocfs2_xattr_bucket_journal_dirty(handle, bucket); | |
01225596 | 3314 | |
01225596 | 3315 | out: |
01225596 TM |
3316 | kfree(bucket_buf); |
3317 | return ret; | |
3318 | } | |
3319 | ||
3320 | /* | |
3321 | * Move half nums of the xattr bucket in the previous cluster to this new | |
3322 | * cluster. We only touch the last cluster of the previous extend record. | |
3323 | * | |
3324 | * first_bh is the first buffer_head of a series of bucket in the same | |
3325 | * extent rec and header_bh is the header of one bucket in this cluster. | |
3326 | * They will be updated if we move the data header_bh contains to the new | |
3327 | * cluster. first_hash will be set as the 1st xe's name_hash of the new cluster. | |
3328 | */ | |
3329 | static int ocfs2_mv_xattr_bucket_cross_cluster(struct inode *inode, | |
3330 | handle_t *handle, | |
3331 | struct buffer_head **first_bh, | |
3332 | struct buffer_head **header_bh, | |
3333 | u64 new_blkno, | |
3334 | u64 prev_blkno, | |
3335 | u32 num_clusters, | |
3336 | u32 *first_hash) | |
3337 | { | |
3338 | int i, ret, credits; | |
3339 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
3340 | int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); | |
3341 | int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); | |
3342 | int blocksize = inode->i_sb->s_blocksize; | |
3343 | struct buffer_head *old_bh, *new_bh, *prev_bh, *new_first_bh = NULL; | |
3344 | struct ocfs2_xattr_header *new_xh; | |
3345 | struct ocfs2_xattr_header *xh = | |
3346 | (struct ocfs2_xattr_header *)((*first_bh)->b_data); | |
3347 | ||
3348 | BUG_ON(le16_to_cpu(xh->xh_num_buckets) < num_buckets); | |
3349 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE == osb->s_clustersize); | |
3350 | ||
3351 | prev_bh = *first_bh; | |
3352 | get_bh(prev_bh); | |
3353 | xh = (struct ocfs2_xattr_header *)prev_bh->b_data; | |
3354 | ||
3355 | prev_blkno += (num_clusters - 1) * bpc + bpc / 2; | |
3356 | ||
3357 | mlog(0, "move half of xattrs in cluster %llu to %llu\n", | |
de29c085 | 3358 | (unsigned long long)prev_blkno, (unsigned long long)new_blkno); |
01225596 TM |
3359 | |
3360 | /* | |
3361 | * We need to update the 1st half of the new cluster and | |
3362 | * 1 more for the update of the 1st bucket of the previous | |
3363 | * extent record. | |
3364 | */ | |
85db90e7 | 3365 | credits = bpc / 2 + 1 + handle->h_buffer_credits; |
01225596 TM |
3366 | ret = ocfs2_extend_trans(handle, credits); |
3367 | if (ret) { | |
3368 | mlog_errno(ret); | |
3369 | goto out; | |
3370 | } | |
3371 | ||
3372 | ret = ocfs2_journal_access(handle, inode, prev_bh, | |
3373 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3374 | if (ret) { | |
3375 | mlog_errno(ret); | |
3376 | goto out; | |
3377 | } | |
3378 | ||
3379 | for (i = 0; i < bpc / 2; i++, prev_blkno++, new_blkno++) { | |
3380 | old_bh = new_bh = NULL; | |
3381 | new_bh = sb_getblk(inode->i_sb, new_blkno); | |
3382 | if (!new_bh) { | |
3383 | ret = -EIO; | |
3384 | mlog_errno(ret); | |
3385 | goto out; | |
3386 | } | |
3387 | ||
3388 | ocfs2_set_new_buffer_uptodate(inode, new_bh); | |
3389 | ||
3390 | ret = ocfs2_journal_access(handle, inode, new_bh, | |
3391 | OCFS2_JOURNAL_ACCESS_CREATE); | |
3392 | if (ret < 0) { | |
3393 | mlog_errno(ret); | |
3394 | brelse(new_bh); | |
3395 | goto out; | |
3396 | } | |
3397 | ||
0fcaa56a | 3398 | ret = ocfs2_read_block(inode, prev_blkno, &old_bh); |
01225596 TM |
3399 | if (ret < 0) { |
3400 | mlog_errno(ret); | |
3401 | brelse(new_bh); | |
3402 | goto out; | |
3403 | } | |
3404 | ||
3405 | memcpy(new_bh->b_data, old_bh->b_data, blocksize); | |
3406 | ||
3407 | if (i == 0) { | |
3408 | new_xh = (struct ocfs2_xattr_header *)new_bh->b_data; | |
3409 | new_xh->xh_num_buckets = cpu_to_le16(num_buckets / 2); | |
3410 | ||
3411 | if (first_hash) | |
3412 | *first_hash = le32_to_cpu( | |
3413 | new_xh->xh_entries[0].xe_name_hash); | |
3414 | new_first_bh = new_bh; | |
3415 | get_bh(new_first_bh); | |
3416 | } | |
3417 | ||
3418 | ocfs2_journal_dirty(handle, new_bh); | |
3419 | ||
3420 | if (*header_bh == old_bh) { | |
3421 | brelse(*header_bh); | |
3422 | *header_bh = new_bh; | |
3423 | get_bh(*header_bh); | |
3424 | ||
3425 | brelse(*first_bh); | |
3426 | *first_bh = new_first_bh; | |
3427 | get_bh(*first_bh); | |
3428 | } | |
3429 | brelse(new_bh); | |
3430 | brelse(old_bh); | |
3431 | } | |
3432 | ||
3433 | le16_add_cpu(&xh->xh_num_buckets, -(num_buckets / 2)); | |
3434 | ||
3435 | ocfs2_journal_dirty(handle, prev_bh); | |
3436 | out: | |
3437 | brelse(prev_bh); | |
3438 | brelse(new_first_bh); | |
3439 | return ret; | |
3440 | } | |
3441 | ||
01225596 | 3442 | /* |
80bcaf34 TM |
3443 | * Find the suitable pos when we divide a bucket into 2. |
3444 | * We have to make sure the xattrs with the same hash value exist | |
3445 | * in the same bucket. | |
3446 | * | |
3447 | * If this ocfs2_xattr_header covers more than one hash value, find a | |
3448 | * place where the hash value changes. Try to find the most even split. | |
3449 | * The most common case is that all entries have different hash values, | |
3450 | * and the first check we make will find a place to split. | |
3451 | */ | |
3452 | static int ocfs2_xattr_find_divide_pos(struct ocfs2_xattr_header *xh) | |
3453 | { | |
3454 | struct ocfs2_xattr_entry *entries = xh->xh_entries; | |
3455 | int count = le16_to_cpu(xh->xh_count); | |
3456 | int delta, middle = count / 2; | |
3457 | ||
3458 | /* | |
3459 | * We start at the middle. Each step gets farther away in both | |
3460 | * directions. We therefore hit the change in hash value | |
3461 | * nearest to the middle. Note that this loop does not execute for | |
3462 | * count < 2. | |
3463 | */ | |
3464 | for (delta = 0; delta < middle; delta++) { | |
3465 | /* Let's check delta earlier than middle */ | |
3466 | if (cmp_xe(&entries[middle - delta - 1], | |
3467 | &entries[middle - delta])) | |
3468 | return middle - delta; | |
3469 | ||
3470 | /* For even counts, don't walk off the end */ | |
3471 | if ((middle + delta + 1) == count) | |
3472 | continue; | |
3473 | ||
3474 | /* Now try delta past middle */ | |
3475 | if (cmp_xe(&entries[middle + delta], | |
3476 | &entries[middle + delta + 1])) | |
3477 | return middle + delta + 1; | |
3478 | } | |
3479 | ||
3480 | /* Every entry had the same hash */ | |
3481 | return count; | |
3482 | } | |
3483 | ||
3484 | /* | |
3485 | * Move some xattrs in old bucket(blk) to new bucket(new_blk). | |
01225596 | 3486 | * first_hash will record the 1st hash of the new bucket. |
80bcaf34 TM |
3487 | * |
3488 | * Normally half of the xattrs will be moved. But we have to make | |
3489 | * sure that the xattrs with the same hash value are stored in the | |
3490 | * same bucket. If all the xattrs in this bucket have the same hash | |
3491 | * value, the new bucket will be initialized as an empty one and the | |
3492 | * first_hash will be initialized as (hash_value+1). | |
01225596 | 3493 | */ |
80bcaf34 TM |
3494 | static int ocfs2_divide_xattr_bucket(struct inode *inode, |
3495 | handle_t *handle, | |
3496 | u64 blk, | |
3497 | u64 new_blk, | |
3498 | u32 *first_hash, | |
3499 | int new_bucket_head) | |
01225596 TM |
3500 | { |
3501 | int ret, i; | |
80bcaf34 | 3502 | int count, start, len, name_value_len = 0, xe_len, name_offset = 0; |
ba937127 | 3503 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
01225596 TM |
3504 | struct ocfs2_xattr_header *xh; |
3505 | struct ocfs2_xattr_entry *xe; | |
3506 | int blocksize = inode->i_sb->s_blocksize; | |
3507 | ||
80bcaf34 | 3508 | mlog(0, "move some of xattrs from bucket %llu to %llu\n", |
de29c085 | 3509 | (unsigned long long)blk, (unsigned long long)new_blk); |
01225596 | 3510 | |
ba937127 JB |
3511 | s_bucket = ocfs2_xattr_bucket_new(inode); |
3512 | t_bucket = ocfs2_xattr_bucket_new(inode); | |
3513 | if (!s_bucket || !t_bucket) { | |
3514 | ret = -ENOMEM; | |
3515 | mlog_errno(ret); | |
3516 | goto out; | |
3517 | } | |
01225596 | 3518 | |
ba937127 | 3519 | ret = ocfs2_read_xattr_bucket(s_bucket, blk); |
01225596 TM |
3520 | if (ret) { |
3521 | mlog_errno(ret); | |
3522 | goto out; | |
3523 | } | |
3524 | ||
ba937127 | 3525 | ret = ocfs2_xattr_bucket_journal_access(handle, s_bucket, |
1224be02 | 3526 | OCFS2_JOURNAL_ACCESS_WRITE); |
01225596 TM |
3527 | if (ret) { |
3528 | mlog_errno(ret); | |
3529 | goto out; | |
3530 | } | |
3531 | ||
784b816a JB |
3532 | /* |
3533 | * Even if !new_bucket_head, we're overwriting t_bucket. Thus, | |
3534 | * there's no need to read it. | |
3535 | */ | |
ba937127 | 3536 | ret = ocfs2_init_xattr_bucket(t_bucket, new_blk); |
01225596 TM |
3537 | if (ret) { |
3538 | mlog_errno(ret); | |
3539 | goto out; | |
3540 | } | |
3541 | ||
ba937127 | 3542 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
1224be02 JB |
3543 | new_bucket_head ? |
3544 | OCFS2_JOURNAL_ACCESS_CREATE : | |
3545 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3546 | if (ret) { | |
3547 | mlog_errno(ret); | |
3548 | goto out; | |
01225596 TM |
3549 | } |
3550 | ||
ba937127 | 3551 | xh = bucket_xh(s_bucket); |
80bcaf34 TM |
3552 | count = le16_to_cpu(xh->xh_count); |
3553 | start = ocfs2_xattr_find_divide_pos(xh); | |
3554 | ||
3555 | if (start == count) { | |
3556 | xe = &xh->xh_entries[start-1]; | |
3557 | ||
3558 | /* | |
3559 | * initialized a new empty bucket here. | |
3560 | * The hash value is set as one larger than | |
3561 | * that of the last entry in the previous bucket. | |
3562 | */ | |
ba937127 JB |
3563 | for (i = 0; i < t_bucket->bu_blocks; i++) |
3564 | memset(bucket_block(t_bucket, i), 0, blocksize); | |
80bcaf34 | 3565 | |
ba937127 | 3566 | xh = bucket_xh(t_bucket); |
80bcaf34 TM |
3567 | xh->xh_free_start = cpu_to_le16(blocksize); |
3568 | xh->xh_entries[0].xe_name_hash = xe->xe_name_hash; | |
3569 | le32_add_cpu(&xh->xh_entries[0].xe_name_hash, 1); | |
3570 | ||
3571 | goto set_num_buckets; | |
3572 | } | |
3573 | ||
01225596 | 3574 | /* copy the whole bucket to the new first. */ |
ba937127 | 3575 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
01225596 TM |
3576 | |
3577 | /* update the new bucket. */ | |
ba937127 | 3578 | xh = bucket_xh(t_bucket); |
01225596 TM |
3579 | |
3580 | /* | |
3581 | * Calculate the total name/value len and xh_free_start for | |
3582 | * the old bucket first. | |
3583 | */ | |
3584 | name_offset = OCFS2_XATTR_BUCKET_SIZE; | |
3585 | name_value_len = 0; | |
3586 | for (i = 0; i < start; i++) { | |
3587 | xe = &xh->xh_entries[i]; | |
3588 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); | |
3589 | if (ocfs2_xattr_is_local(xe)) | |
3590 | xe_len += | |
3591 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
3592 | else | |
3593 | xe_len += OCFS2_XATTR_ROOT_SIZE; | |
3594 | name_value_len += xe_len; | |
3595 | if (le16_to_cpu(xe->xe_name_offset) < name_offset) | |
3596 | name_offset = le16_to_cpu(xe->xe_name_offset); | |
3597 | } | |
3598 | ||
3599 | /* | |
3600 | * Now begin the modification to the new bucket. | |
3601 | * | |
3602 | * In the new bucket, We just move the xattr entry to the beginning | |
3603 | * and don't touch the name/value. So there will be some holes in the | |
3604 | * bucket, and they will be removed when ocfs2_defrag_xattr_bucket is | |
3605 | * called. | |
3606 | */ | |
3607 | xe = &xh->xh_entries[start]; | |
3608 | len = sizeof(struct ocfs2_xattr_entry) * (count - start); | |
3609 | mlog(0, "mv xattr entry len %d from %d to %d\n", len, | |
ff1ec20e MF |
3610 | (int)((char *)xe - (char *)xh), |
3611 | (int)((char *)xh->xh_entries - (char *)xh)); | |
01225596 TM |
3612 | memmove((char *)xh->xh_entries, (char *)xe, len); |
3613 | xe = &xh->xh_entries[count - start]; | |
3614 | len = sizeof(struct ocfs2_xattr_entry) * start; | |
3615 | memset((char *)xe, 0, len); | |
3616 | ||
3617 | le16_add_cpu(&xh->xh_count, -start); | |
3618 | le16_add_cpu(&xh->xh_name_value_len, -name_value_len); | |
3619 | ||
3620 | /* Calculate xh_free_start for the new bucket. */ | |
3621 | xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); | |
3622 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { | |
3623 | xe = &xh->xh_entries[i]; | |
3624 | xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); | |
3625 | if (ocfs2_xattr_is_local(xe)) | |
3626 | xe_len += | |
3627 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
3628 | else | |
3629 | xe_len += OCFS2_XATTR_ROOT_SIZE; | |
3630 | if (le16_to_cpu(xe->xe_name_offset) < | |
3631 | le16_to_cpu(xh->xh_free_start)) | |
3632 | xh->xh_free_start = xe->xe_name_offset; | |
3633 | } | |
3634 | ||
80bcaf34 | 3635 | set_num_buckets: |
01225596 TM |
3636 | /* set xh->xh_num_buckets for the new xh. */ |
3637 | if (new_bucket_head) | |
3638 | xh->xh_num_buckets = cpu_to_le16(1); | |
3639 | else | |
3640 | xh->xh_num_buckets = 0; | |
3641 | ||
ba937127 | 3642 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); |
01225596 TM |
3643 | |
3644 | /* store the first_hash of the new bucket. */ | |
3645 | if (first_hash) | |
3646 | *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); | |
3647 | ||
3648 | /* | |
80bcaf34 TM |
3649 | * Now only update the 1st block of the old bucket. If we |
3650 | * just added a new empty bucket, there is no need to modify | |
3651 | * it. | |
01225596 | 3652 | */ |
80bcaf34 TM |
3653 | if (start == count) |
3654 | goto out; | |
3655 | ||
ba937127 | 3656 | xh = bucket_xh(s_bucket); |
01225596 TM |
3657 | memset(&xh->xh_entries[start], 0, |
3658 | sizeof(struct ocfs2_xattr_entry) * (count - start)); | |
3659 | xh->xh_count = cpu_to_le16(start); | |
3660 | xh->xh_free_start = cpu_to_le16(name_offset); | |
3661 | xh->xh_name_value_len = cpu_to_le16(name_value_len); | |
3662 | ||
ba937127 | 3663 | ocfs2_xattr_bucket_journal_dirty(handle, s_bucket); |
01225596 TM |
3664 | |
3665 | out: | |
ba937127 JB |
3666 | ocfs2_xattr_bucket_free(s_bucket); |
3667 | ocfs2_xattr_bucket_free(t_bucket); | |
01225596 TM |
3668 | |
3669 | return ret; | |
3670 | } | |
3671 | ||
3672 | /* | |
3673 | * Copy xattr from one bucket to another bucket. | |
3674 | * | |
3675 | * The caller must make sure that the journal transaction | |
3676 | * has enough space for journaling. | |
3677 | */ | |
3678 | static int ocfs2_cp_xattr_bucket(struct inode *inode, | |
3679 | handle_t *handle, | |
3680 | u64 s_blkno, | |
3681 | u64 t_blkno, | |
3682 | int t_is_new) | |
3683 | { | |
4980c6da | 3684 | int ret; |
ba937127 | 3685 | struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; |
01225596 TM |
3686 | |
3687 | BUG_ON(s_blkno == t_blkno); | |
3688 | ||
3689 | mlog(0, "cp bucket %llu to %llu, target is %d\n", | |
de29c085 MF |
3690 | (unsigned long long)s_blkno, (unsigned long long)t_blkno, |
3691 | t_is_new); | |
01225596 | 3692 | |
ba937127 JB |
3693 | s_bucket = ocfs2_xattr_bucket_new(inode); |
3694 | t_bucket = ocfs2_xattr_bucket_new(inode); | |
3695 | if (!s_bucket || !t_bucket) { | |
3696 | ret = -ENOMEM; | |
3697 | mlog_errno(ret); | |
3698 | goto out; | |
3699 | } | |
3700 | ||
3701 | ret = ocfs2_read_xattr_bucket(s_bucket, s_blkno); | |
01225596 TM |
3702 | if (ret) |
3703 | goto out; | |
3704 | ||
784b816a JB |
3705 | /* |
3706 | * Even if !t_is_new, we're overwriting t_bucket. Thus, | |
3707 | * there's no need to read it. | |
3708 | */ | |
ba937127 | 3709 | ret = ocfs2_init_xattr_bucket(t_bucket, t_blkno); |
01225596 TM |
3710 | if (ret) |
3711 | goto out; | |
3712 | ||
ba937127 | 3713 | ret = ocfs2_xattr_bucket_journal_access(handle, t_bucket, |
1224be02 JB |
3714 | t_is_new ? |
3715 | OCFS2_JOURNAL_ACCESS_CREATE : | |
3716 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3717 | if (ret) | |
3718 | goto out; | |
01225596 | 3719 | |
ba937127 JB |
3720 | ocfs2_xattr_bucket_copy_data(t_bucket, s_bucket); |
3721 | ocfs2_xattr_bucket_journal_dirty(handle, t_bucket); | |
01225596 TM |
3722 | |
3723 | out: | |
ba937127 JB |
3724 | ocfs2_xattr_bucket_free(t_bucket); |
3725 | ocfs2_xattr_bucket_free(s_bucket); | |
01225596 TM |
3726 | |
3727 | return ret; | |
3728 | } | |
3729 | ||
3730 | /* | |
3731 | * Copy one xattr cluster from src_blk to to_blk. | |
3732 | * The to_blk will become the first bucket header of the cluster, so its | |
3733 | * xh_num_buckets will be initialized as the bucket num in the cluster. | |
3734 | */ | |
3735 | static int ocfs2_cp_xattr_cluster(struct inode *inode, | |
3736 | handle_t *handle, | |
3737 | struct buffer_head *first_bh, | |
3738 | u64 src_blk, | |
3739 | u64 to_blk, | |
3740 | u32 *first_hash) | |
3741 | { | |
3742 | int i, ret, credits; | |
3743 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
3744 | int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); | |
3745 | int num_buckets = ocfs2_xattr_buckets_per_cluster(osb); | |
3746 | struct buffer_head *bh = NULL; | |
3747 | struct ocfs2_xattr_header *xh; | |
3748 | u64 to_blk_start = to_blk; | |
3749 | ||
de29c085 MF |
3750 | mlog(0, "cp xattrs from cluster %llu to %llu\n", |
3751 | (unsigned long long)src_blk, (unsigned long long)to_blk); | |
01225596 TM |
3752 | |
3753 | /* | |
3754 | * We need to update the new cluster and 1 more for the update of | |
3755 | * the 1st bucket of the previous extent rec. | |
3756 | */ | |
85db90e7 | 3757 | credits = bpc + 1 + handle->h_buffer_credits; |
01225596 TM |
3758 | ret = ocfs2_extend_trans(handle, credits); |
3759 | if (ret) { | |
3760 | mlog_errno(ret); | |
3761 | goto out; | |
3762 | } | |
3763 | ||
3764 | ret = ocfs2_journal_access(handle, inode, first_bh, | |
3765 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3766 | if (ret) { | |
3767 | mlog_errno(ret); | |
3768 | goto out; | |
3769 | } | |
3770 | ||
3771 | for (i = 0; i < num_buckets; i++) { | |
3772 | ret = ocfs2_cp_xattr_bucket(inode, handle, | |
3773 | src_blk, to_blk, 1); | |
3774 | if (ret) { | |
3775 | mlog_errno(ret); | |
3776 | goto out; | |
3777 | } | |
3778 | ||
3779 | src_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
3780 | to_blk += ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
3781 | } | |
3782 | ||
3783 | /* update the old bucket header. */ | |
3784 | xh = (struct ocfs2_xattr_header *)first_bh->b_data; | |
3785 | le16_add_cpu(&xh->xh_num_buckets, -num_buckets); | |
3786 | ||
3787 | ocfs2_journal_dirty(handle, first_bh); | |
3788 | ||
3789 | /* update the new bucket header. */ | |
0fcaa56a | 3790 | ret = ocfs2_read_block(inode, to_blk_start, &bh); |
01225596 TM |
3791 | if (ret < 0) { |
3792 | mlog_errno(ret); | |
3793 | goto out; | |
3794 | } | |
3795 | ||
3796 | ret = ocfs2_journal_access(handle, inode, bh, | |
3797 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3798 | if (ret) { | |
3799 | mlog_errno(ret); | |
3800 | goto out; | |
3801 | } | |
3802 | ||
3803 | xh = (struct ocfs2_xattr_header *)bh->b_data; | |
3804 | xh->xh_num_buckets = cpu_to_le16(num_buckets); | |
3805 | ||
3806 | ocfs2_journal_dirty(handle, bh); | |
3807 | ||
3808 | if (first_hash) | |
3809 | *first_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); | |
3810 | out: | |
3811 | brelse(bh); | |
3812 | return ret; | |
3813 | } | |
3814 | ||
3815 | /* | |
80bcaf34 | 3816 | * Move some xattrs in this cluster to the new cluster. |
01225596 TM |
3817 | * This function should only be called when bucket size == cluster size. |
3818 | * Otherwise ocfs2_mv_xattr_bucket_cross_cluster should be used instead. | |
3819 | */ | |
80bcaf34 TM |
3820 | static int ocfs2_divide_xattr_cluster(struct inode *inode, |
3821 | handle_t *handle, | |
3822 | u64 prev_blk, | |
3823 | u64 new_blk, | |
3824 | u32 *first_hash) | |
01225596 TM |
3825 | { |
3826 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
85db90e7 | 3827 | int ret, credits = 2 * blk_per_bucket + handle->h_buffer_credits; |
01225596 TM |
3828 | |
3829 | BUG_ON(OCFS2_XATTR_BUCKET_SIZE < OCFS2_SB(inode->i_sb)->s_clustersize); | |
3830 | ||
3831 | ret = ocfs2_extend_trans(handle, credits); | |
3832 | if (ret) { | |
3833 | mlog_errno(ret); | |
3834 | return ret; | |
3835 | } | |
3836 | ||
3837 | /* Move half of the xattr in start_blk to the next bucket. */ | |
80bcaf34 TM |
3838 | return ocfs2_divide_xattr_bucket(inode, handle, prev_blk, |
3839 | new_blk, first_hash, 1); | |
01225596 TM |
3840 | } |
3841 | ||
3842 | /* | |
3843 | * Move some xattrs from the old cluster to the new one since they are not | |
3844 | * contiguous in ocfs2 xattr tree. | |
3845 | * | |
3846 | * new_blk starts a new separate cluster, and we will move some xattrs from | |
3847 | * prev_blk to it. v_start will be set as the first name hash value in this | |
3848 | * new cluster so that it can be used as e_cpos during tree insertion and | |
3849 | * don't collide with our original b-tree operations. first_bh and header_bh | |
3850 | * will also be updated since they will be used in ocfs2_extend_xattr_bucket | |
3851 | * to extend the insert bucket. | |
3852 | * | |
3853 | * The problem is how much xattr should we move to the new one and when should | |
3854 | * we update first_bh and header_bh? | |
3855 | * 1. If cluster size > bucket size, that means the previous cluster has more | |
3856 | * than 1 bucket, so just move half nums of bucket into the new cluster and | |
3857 | * update the first_bh and header_bh if the insert bucket has been moved | |
3858 | * to the new cluster. | |
3859 | * 2. If cluster_size == bucket_size: | |
3860 | * a) If the previous extent rec has more than one cluster and the insert | |
3861 | * place isn't in the last cluster, copy the entire last cluster to the | |
3862 | * new one. This time, we don't need to upate the first_bh and header_bh | |
3863 | * since they will not be moved into the new cluster. | |
3864 | * b) Otherwise, move the bottom half of the xattrs in the last cluster into | |
3865 | * the new one. And we set the extend flag to zero if the insert place is | |
3866 | * moved into the new allocated cluster since no extend is needed. | |
3867 | */ | |
3868 | static int ocfs2_adjust_xattr_cross_cluster(struct inode *inode, | |
3869 | handle_t *handle, | |
3870 | struct buffer_head **first_bh, | |
3871 | struct buffer_head **header_bh, | |
3872 | u64 new_blk, | |
3873 | u64 prev_blk, | |
3874 | u32 prev_clusters, | |
3875 | u32 *v_start, | |
3876 | int *extend) | |
3877 | { | |
3878 | int ret = 0; | |
3879 | int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); | |
3880 | ||
3881 | mlog(0, "adjust xattrs from cluster %llu len %u to %llu\n", | |
de29c085 MF |
3882 | (unsigned long long)prev_blk, prev_clusters, |
3883 | (unsigned long long)new_blk); | |
01225596 TM |
3884 | |
3885 | if (ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb)) > 1) | |
3886 | ret = ocfs2_mv_xattr_bucket_cross_cluster(inode, | |
3887 | handle, | |
3888 | first_bh, | |
3889 | header_bh, | |
3890 | new_blk, | |
3891 | prev_blk, | |
3892 | prev_clusters, | |
3893 | v_start); | |
3894 | else { | |
3895 | u64 last_blk = prev_blk + bpc * (prev_clusters - 1); | |
3896 | ||
3897 | if (prev_clusters > 1 && (*header_bh)->b_blocknr != last_blk) | |
3898 | ret = ocfs2_cp_xattr_cluster(inode, handle, *first_bh, | |
3899 | last_blk, new_blk, | |
3900 | v_start); | |
3901 | else { | |
80bcaf34 TM |
3902 | ret = ocfs2_divide_xattr_cluster(inode, handle, |
3903 | last_blk, new_blk, | |
3904 | v_start); | |
01225596 TM |
3905 | |
3906 | if ((*header_bh)->b_blocknr == last_blk && extend) | |
3907 | *extend = 0; | |
3908 | } | |
3909 | } | |
3910 | ||
3911 | return ret; | |
3912 | } | |
3913 | ||
3914 | /* | |
3915 | * Add a new cluster for xattr storage. | |
3916 | * | |
3917 | * If the new cluster is contiguous with the previous one, it will be | |
3918 | * appended to the same extent record, and num_clusters will be updated. | |
3919 | * If not, we will insert a new extent for it and move some xattrs in | |
3920 | * the last cluster into the new allocated one. | |
3921 | * We also need to limit the maximum size of a btree leaf, otherwise we'll | |
3922 | * lose the benefits of hashing because we'll have to search large leaves. | |
3923 | * So now the maximum size is OCFS2_MAX_XATTR_TREE_LEAF_SIZE(or clustersize, | |
3924 | * if it's bigger). | |
3925 | * | |
3926 | * first_bh is the first block of the previous extent rec and header_bh | |
3927 | * indicates the bucket we will insert the new xattrs. They will be updated | |
3928 | * when the header_bh is moved into the new cluster. | |
3929 | */ | |
3930 | static int ocfs2_add_new_xattr_cluster(struct inode *inode, | |
3931 | struct buffer_head *root_bh, | |
3932 | struct buffer_head **first_bh, | |
3933 | struct buffer_head **header_bh, | |
3934 | u32 *num_clusters, | |
3935 | u32 prev_cpos, | |
3936 | u64 prev_blkno, | |
78f30c31 TM |
3937 | int *extend, |
3938 | struct ocfs2_xattr_set_ctxt *ctxt) | |
01225596 | 3939 | { |
85db90e7 | 3940 | int ret; |
01225596 TM |
3941 | u16 bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1); |
3942 | u32 prev_clusters = *num_clusters; | |
3943 | u32 clusters_to_add = 1, bit_off, num_bits, v_start = 0; | |
3944 | u64 block; | |
85db90e7 | 3945 | handle_t *handle = ctxt->handle; |
01225596 | 3946 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
f99b9b7c | 3947 | struct ocfs2_extent_tree et; |
01225596 TM |
3948 | |
3949 | mlog(0, "Add new xattr cluster for %llu, previous xattr hash = %u, " | |
3950 | "previous xattr blkno = %llu\n", | |
3951 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
de29c085 | 3952 | prev_cpos, (unsigned long long)prev_blkno); |
01225596 | 3953 | |
8d6220d6 | 3954 | ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh); |
f99b9b7c | 3955 | |
01225596 TM |
3956 | ret = ocfs2_journal_access(handle, inode, root_bh, |
3957 | OCFS2_JOURNAL_ACCESS_WRITE); | |
3958 | if (ret < 0) { | |
3959 | mlog_errno(ret); | |
3960 | goto leave; | |
3961 | } | |
3962 | ||
78f30c31 | 3963 | ret = __ocfs2_claim_clusters(osb, handle, ctxt->data_ac, 1, |
01225596 TM |
3964 | clusters_to_add, &bit_off, &num_bits); |
3965 | if (ret < 0) { | |
3966 | if (ret != -ENOSPC) | |
3967 | mlog_errno(ret); | |
3968 | goto leave; | |
3969 | } | |
3970 | ||
3971 | BUG_ON(num_bits > clusters_to_add); | |
3972 | ||
3973 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); | |
3974 | mlog(0, "Allocating %u clusters at block %u for xattr in inode %llu\n", | |
3975 | num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); | |
3976 | ||
3977 | if (prev_blkno + prev_clusters * bpc == block && | |
3978 | (prev_clusters + num_bits) << osb->s_clustersize_bits <= | |
3979 | OCFS2_MAX_XATTR_TREE_LEAF_SIZE) { | |
3980 | /* | |
3981 | * If this cluster is contiguous with the old one and | |
3982 | * adding this new cluster, we don't surpass the limit of | |
3983 | * OCFS2_MAX_XATTR_TREE_LEAF_SIZE, cool. We will let it be | |
3984 | * initialized and used like other buckets in the previous | |
3985 | * cluster. | |
3986 | * So add it as a contiguous one. The caller will handle | |
3987 | * its init process. | |
3988 | */ | |
3989 | v_start = prev_cpos + prev_clusters; | |
3990 | *num_clusters = prev_clusters + num_bits; | |
3991 | mlog(0, "Add contiguous %u clusters to previous extent rec.\n", | |
3992 | num_bits); | |
3993 | } else { | |
3994 | ret = ocfs2_adjust_xattr_cross_cluster(inode, | |
3995 | handle, | |
3996 | first_bh, | |
3997 | header_bh, | |
3998 | block, | |
3999 | prev_blkno, | |
4000 | prev_clusters, | |
4001 | &v_start, | |
4002 | extend); | |
4003 | if (ret) { | |
4004 | mlog_errno(ret); | |
4005 | goto leave; | |
4006 | } | |
4007 | } | |
4008 | ||
4009 | mlog(0, "Insert %u clusters at block %llu for xattr at %u\n", | |
de29c085 | 4010 | num_bits, (unsigned long long)block, v_start); |
f99b9b7c | 4011 | ret = ocfs2_insert_extent(osb, handle, inode, &et, v_start, block, |
78f30c31 | 4012 | num_bits, 0, ctxt->meta_ac); |
01225596 TM |
4013 | if (ret < 0) { |
4014 | mlog_errno(ret); | |
4015 | goto leave; | |
4016 | } | |
4017 | ||
4018 | ret = ocfs2_journal_dirty(handle, root_bh); | |
85db90e7 | 4019 | if (ret < 0) |
01225596 | 4020 | mlog_errno(ret); |
01225596 TM |
4021 | |
4022 | leave: | |
01225596 TM |
4023 | return ret; |
4024 | } | |
4025 | ||
4026 | /* | |
4027 | * Extend a new xattr bucket and move xattrs to the end one by one until | |
4028 | * We meet with start_bh. Only move half of the xattrs to the bucket after it. | |
4029 | */ | |
4030 | static int ocfs2_extend_xattr_bucket(struct inode *inode, | |
85db90e7 | 4031 | handle_t *handle, |
01225596 TM |
4032 | struct buffer_head *first_bh, |
4033 | struct buffer_head *start_bh, | |
4034 | u32 num_clusters) | |
4035 | { | |
4036 | int ret, credits; | |
4037 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4038 | u16 blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb); | |
4039 | u64 start_blk = start_bh->b_blocknr, end_blk; | |
4040 | u32 num_buckets = num_clusters * ocfs2_xattr_buckets_per_cluster(osb); | |
01225596 TM |
4041 | struct ocfs2_xattr_header *first_xh = |
4042 | (struct ocfs2_xattr_header *)first_bh->b_data; | |
4043 | u16 bucket = le16_to_cpu(first_xh->xh_num_buckets); | |
4044 | ||
4045 | mlog(0, "extend xattr bucket in %llu, xattr extend rec starting " | |
de29c085 | 4046 | "from %llu, len = %u\n", (unsigned long long)start_blk, |
01225596 TM |
4047 | (unsigned long long)first_bh->b_blocknr, num_clusters); |
4048 | ||
4049 | BUG_ON(bucket >= num_buckets); | |
4050 | ||
4051 | end_blk = first_bh->b_blocknr + (bucket - 1) * blk_per_bucket; | |
4052 | ||
4053 | /* | |
4054 | * We will touch all the buckets after the start_bh(include it). | |
1224be02 | 4055 | * Then we add one more bucket. |
01225596 | 4056 | */ |
85db90e7 TM |
4057 | credits = end_blk - start_blk + 3 * blk_per_bucket + 1 + |
4058 | handle->h_buffer_credits; | |
4059 | ret = ocfs2_extend_trans(handle, credits); | |
4060 | if (ret) { | |
01225596 TM |
4061 | mlog_errno(ret); |
4062 | goto out; | |
4063 | } | |
4064 | ||
4065 | ret = ocfs2_journal_access(handle, inode, first_bh, | |
4066 | OCFS2_JOURNAL_ACCESS_WRITE); | |
4067 | if (ret) { | |
4068 | mlog_errno(ret); | |
85db90e7 | 4069 | goto out; |
01225596 TM |
4070 | } |
4071 | ||
4072 | while (end_blk != start_blk) { | |
4073 | ret = ocfs2_cp_xattr_bucket(inode, handle, end_blk, | |
4074 | end_blk + blk_per_bucket, 0); | |
4075 | if (ret) | |
85db90e7 | 4076 | goto out; |
01225596 TM |
4077 | end_blk -= blk_per_bucket; |
4078 | } | |
4079 | ||
4080 | /* Move half of the xattr in start_blk to the next bucket. */ | |
80bcaf34 TM |
4081 | ret = ocfs2_divide_xattr_bucket(inode, handle, start_blk, |
4082 | start_blk + blk_per_bucket, NULL, 0); | |
01225596 TM |
4083 | |
4084 | le16_add_cpu(&first_xh->xh_num_buckets, 1); | |
4085 | ocfs2_journal_dirty(handle, first_bh); | |
4086 | ||
01225596 TM |
4087 | out: |
4088 | return ret; | |
4089 | } | |
4090 | ||
4091 | /* | |
4092 | * Add new xattr bucket in an extent record and adjust the buckets accordingly. | |
4093 | * xb_bh is the ocfs2_xattr_block. | |
4094 | * We will move all the buckets starting from header_bh to the next place. As | |
4095 | * for this one, half num of its xattrs will be moved to the next one. | |
4096 | * | |
4097 | * We will allocate a new cluster if current cluster is full and adjust | |
4098 | * header_bh and first_bh if the insert place is moved to the new cluster. | |
4099 | */ | |
4100 | static int ocfs2_add_new_xattr_bucket(struct inode *inode, | |
4101 | struct buffer_head *xb_bh, | |
78f30c31 TM |
4102 | struct buffer_head *header_bh, |
4103 | struct ocfs2_xattr_set_ctxt *ctxt) | |
01225596 TM |
4104 | { |
4105 | struct ocfs2_xattr_header *first_xh = NULL; | |
4106 | struct buffer_head *first_bh = NULL; | |
4107 | struct ocfs2_xattr_block *xb = | |
4108 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
4109 | struct ocfs2_xattr_tree_root *xb_root = &xb->xb_attrs.xb_root; | |
4110 | struct ocfs2_extent_list *el = &xb_root->xt_list; | |
4111 | struct ocfs2_xattr_header *xh = | |
4112 | (struct ocfs2_xattr_header *)header_bh->b_data; | |
4113 | u32 name_hash = le32_to_cpu(xh->xh_entries[0].xe_name_hash); | |
4114 | struct super_block *sb = inode->i_sb; | |
4115 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
4116 | int ret, num_buckets, extend = 1; | |
4117 | u64 p_blkno; | |
4118 | u32 e_cpos, num_clusters; | |
4119 | ||
4120 | mlog(0, "Add new xattr bucket starting form %llu\n", | |
4121 | (unsigned long long)header_bh->b_blocknr); | |
4122 | ||
4123 | /* | |
4124 | * Add refrence for header_bh here because it may be | |
4125 | * changed in ocfs2_add_new_xattr_cluster and we need | |
4126 | * to free it in the end. | |
4127 | */ | |
4128 | get_bh(header_bh); | |
4129 | ||
4130 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, &e_cpos, | |
4131 | &num_clusters, el); | |
4132 | if (ret) { | |
4133 | mlog_errno(ret); | |
4134 | goto out; | |
4135 | } | |
4136 | ||
0fcaa56a | 4137 | ret = ocfs2_read_block(inode, p_blkno, &first_bh); |
01225596 TM |
4138 | if (ret) { |
4139 | mlog_errno(ret); | |
4140 | goto out; | |
4141 | } | |
4142 | ||
4143 | num_buckets = ocfs2_xattr_buckets_per_cluster(osb) * num_clusters; | |
4144 | first_xh = (struct ocfs2_xattr_header *)first_bh->b_data; | |
4145 | ||
4146 | if (num_buckets == le16_to_cpu(first_xh->xh_num_buckets)) { | |
4147 | ret = ocfs2_add_new_xattr_cluster(inode, | |
4148 | xb_bh, | |
4149 | &first_bh, | |
4150 | &header_bh, | |
4151 | &num_clusters, | |
4152 | e_cpos, | |
4153 | p_blkno, | |
78f30c31 TM |
4154 | &extend, |
4155 | ctxt); | |
01225596 TM |
4156 | if (ret) { |
4157 | mlog_errno(ret); | |
4158 | goto out; | |
4159 | } | |
4160 | } | |
4161 | ||
4162 | if (extend) | |
4163 | ret = ocfs2_extend_xattr_bucket(inode, | |
85db90e7 | 4164 | ctxt->handle, |
01225596 TM |
4165 | first_bh, |
4166 | header_bh, | |
4167 | num_clusters); | |
4168 | if (ret) | |
4169 | mlog_errno(ret); | |
4170 | out: | |
4171 | brelse(first_bh); | |
4172 | brelse(header_bh); | |
4173 | return ret; | |
4174 | } | |
4175 | ||
4176 | static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode, | |
4177 | struct ocfs2_xattr_bucket *bucket, | |
4178 | int offs) | |
4179 | { | |
4180 | int block_off = offs >> inode->i_sb->s_blocksize_bits; | |
4181 | ||
4182 | offs = offs % inode->i_sb->s_blocksize; | |
51def39f | 4183 | return bucket_block(bucket, block_off) + offs; |
01225596 TM |
4184 | } |
4185 | ||
4186 | /* | |
4187 | * Handle the normal xattr set, including replace, delete and new. | |
01225596 TM |
4188 | * |
4189 | * Note: "local" indicates the real data's locality. So we can't | |
4190 | * just its bucket locality by its length. | |
4191 | */ | |
4192 | static void ocfs2_xattr_set_entry_normal(struct inode *inode, | |
4193 | struct ocfs2_xattr_info *xi, | |
4194 | struct ocfs2_xattr_search *xs, | |
4195 | u32 name_hash, | |
5a095611 | 4196 | int local) |
01225596 TM |
4197 | { |
4198 | struct ocfs2_xattr_entry *last, *xe; | |
4199 | int name_len = strlen(xi->name); | |
4200 | struct ocfs2_xattr_header *xh = xs->header; | |
4201 | u16 count = le16_to_cpu(xh->xh_count), start; | |
4202 | size_t blocksize = inode->i_sb->s_blocksize; | |
4203 | char *val; | |
4204 | size_t offs, size, new_size; | |
4205 | ||
4206 | last = &xh->xh_entries[count]; | |
4207 | if (!xs->not_found) { | |
4208 | xe = xs->here; | |
4209 | offs = le16_to_cpu(xe->xe_name_offset); | |
4210 | if (ocfs2_xattr_is_local(xe)) | |
4211 | size = OCFS2_XATTR_SIZE(name_len) + | |
4212 | OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
4213 | else | |
4214 | size = OCFS2_XATTR_SIZE(name_len) + | |
4215 | OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); | |
4216 | ||
4217 | /* | |
4218 | * If the new value will be stored outside, xi->value has been | |
4219 | * initalized as an empty ocfs2_xattr_value_root, and the same | |
4220 | * goes with xi->value_len, so we can set new_size safely here. | |
4221 | * See ocfs2_xattr_set_in_bucket. | |
4222 | */ | |
4223 | new_size = OCFS2_XATTR_SIZE(name_len) + | |
4224 | OCFS2_XATTR_SIZE(xi->value_len); | |
4225 | ||
4226 | le16_add_cpu(&xh->xh_name_value_len, -size); | |
4227 | if (xi->value) { | |
4228 | if (new_size > size) | |
4229 | goto set_new_name_value; | |
4230 | ||
4231 | /* Now replace the old value with new one. */ | |
4232 | if (local) | |
4233 | xe->xe_value_size = cpu_to_le64(xi->value_len); | |
4234 | else | |
4235 | xe->xe_value_size = 0; | |
4236 | ||
4237 | val = ocfs2_xattr_bucket_get_val(inode, | |
ba937127 | 4238 | xs->bucket, offs); |
01225596 TM |
4239 | memset(val + OCFS2_XATTR_SIZE(name_len), 0, |
4240 | size - OCFS2_XATTR_SIZE(name_len)); | |
4241 | if (OCFS2_XATTR_SIZE(xi->value_len) > 0) | |
4242 | memcpy(val + OCFS2_XATTR_SIZE(name_len), | |
4243 | xi->value, xi->value_len); | |
4244 | ||
4245 | le16_add_cpu(&xh->xh_name_value_len, new_size); | |
4246 | ocfs2_xattr_set_local(xe, local); | |
4247 | return; | |
4248 | } else { | |
5a095611 TM |
4249 | /* |
4250 | * Remove the old entry if there is more than one. | |
4251 | * We don't remove the last entry so that we can | |
4252 | * use it to indicate the hash value of the empty | |
4253 | * bucket. | |
4254 | */ | |
01225596 | 4255 | last -= 1; |
01225596 | 4256 | le16_add_cpu(&xh->xh_count, -1); |
5a095611 TM |
4257 | if (xh->xh_count) { |
4258 | memmove(xe, xe + 1, | |
4259 | (void *)last - (void *)xe); | |
4260 | memset(last, 0, | |
4261 | sizeof(struct ocfs2_xattr_entry)); | |
4262 | } else | |
4263 | xh->xh_free_start = | |
4264 | cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); | |
4265 | ||
01225596 TM |
4266 | return; |
4267 | } | |
4268 | } else { | |
4269 | /* find a new entry for insert. */ | |
4270 | int low = 0, high = count - 1, tmp; | |
4271 | struct ocfs2_xattr_entry *tmp_xe; | |
4272 | ||
5a095611 | 4273 | while (low <= high && count) { |
01225596 TM |
4274 | tmp = (low + high) / 2; |
4275 | tmp_xe = &xh->xh_entries[tmp]; | |
4276 | ||
4277 | if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash)) | |
4278 | low = tmp + 1; | |
4279 | else if (name_hash < | |
4280 | le32_to_cpu(tmp_xe->xe_name_hash)) | |
4281 | high = tmp - 1; | |
06b240d8 TM |
4282 | else { |
4283 | low = tmp; | |
01225596 | 4284 | break; |
06b240d8 | 4285 | } |
01225596 TM |
4286 | } |
4287 | ||
4288 | xe = &xh->xh_entries[low]; | |
4289 | if (low != count) | |
4290 | memmove(xe + 1, xe, (void *)last - (void *)xe); | |
4291 | ||
4292 | le16_add_cpu(&xh->xh_count, 1); | |
4293 | memset(xe, 0, sizeof(struct ocfs2_xattr_entry)); | |
4294 | xe->xe_name_hash = cpu_to_le32(name_hash); | |
4295 | xe->xe_name_len = name_len; | |
4296 | ocfs2_xattr_set_type(xe, xi->name_index); | |
4297 | } | |
4298 | ||
4299 | set_new_name_value: | |
4300 | /* Insert the new name+value. */ | |
4301 | size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len); | |
4302 | ||
4303 | /* | |
4304 | * We must make sure that the name/value pair | |
4305 | * exists in the same block. | |
4306 | */ | |
4307 | offs = le16_to_cpu(xh->xh_free_start); | |
4308 | start = offs - size; | |
4309 | ||
4310 | if (start >> inode->i_sb->s_blocksize_bits != | |
4311 | (offs - 1) >> inode->i_sb->s_blocksize_bits) { | |
4312 | offs = offs - offs % blocksize; | |
4313 | xh->xh_free_start = cpu_to_le16(offs); | |
4314 | } | |
4315 | ||
ba937127 | 4316 | val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size); |
01225596 TM |
4317 | xe->xe_name_offset = cpu_to_le16(offs - size); |
4318 | ||
4319 | memset(val, 0, size); | |
4320 | memcpy(val, xi->name, name_len); | |
4321 | memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len); | |
4322 | ||
4323 | xe->xe_value_size = cpu_to_le64(xi->value_len); | |
4324 | ocfs2_xattr_set_local(xe, local); | |
4325 | xs->here = xe; | |
4326 | le16_add_cpu(&xh->xh_free_start, -size); | |
4327 | le16_add_cpu(&xh->xh_name_value_len, size); | |
4328 | ||
4329 | return; | |
4330 | } | |
4331 | ||
01225596 TM |
4332 | /* |
4333 | * Set the xattr entry in the specified bucket. | |
4334 | * The bucket is indicated by xs->bucket and it should have the enough | |
4335 | * space for the xattr insertion. | |
4336 | */ | |
4337 | static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode, | |
85db90e7 | 4338 | handle_t *handle, |
01225596 TM |
4339 | struct ocfs2_xattr_info *xi, |
4340 | struct ocfs2_xattr_search *xs, | |
4341 | u32 name_hash, | |
5a095611 | 4342 | int local) |
01225596 | 4343 | { |
1224be02 | 4344 | int ret; |
02dbf38d | 4345 | u64 blkno; |
01225596 | 4346 | |
ff1ec20e MF |
4347 | mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n", |
4348 | (unsigned long)xi->value_len, xi->name_index, | |
ba937127 | 4349 | (unsigned long long)bucket_blkno(xs->bucket)); |
01225596 | 4350 | |
ba937127 | 4351 | if (!xs->bucket->bu_bhs[1]) { |
02dbf38d JB |
4352 | blkno = bucket_blkno(xs->bucket); |
4353 | ocfs2_xattr_bucket_relse(xs->bucket); | |
4354 | ret = ocfs2_read_xattr_bucket(xs->bucket, blkno); | |
01225596 TM |
4355 | if (ret) { |
4356 | mlog_errno(ret); | |
4357 | goto out; | |
4358 | } | |
4359 | } | |
4360 | ||
ba937127 | 4361 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
1224be02 JB |
4362 | OCFS2_JOURNAL_ACCESS_WRITE); |
4363 | if (ret < 0) { | |
4364 | mlog_errno(ret); | |
4365 | goto out; | |
01225596 TM |
4366 | } |
4367 | ||
5a095611 | 4368 | ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local); |
ba937127 | 4369 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
01225596 | 4370 | |
01225596 | 4371 | out: |
01225596 TM |
4372 | return ret; |
4373 | } | |
4374 | ||
4375 | static int ocfs2_xattr_value_update_size(struct inode *inode, | |
85db90e7 | 4376 | handle_t *handle, |
01225596 TM |
4377 | struct buffer_head *xe_bh, |
4378 | struct ocfs2_xattr_entry *xe, | |
4379 | u64 new_size) | |
4380 | { | |
4381 | int ret; | |
01225596 TM |
4382 | |
4383 | ret = ocfs2_journal_access(handle, inode, xe_bh, | |
4384 | OCFS2_JOURNAL_ACCESS_WRITE); | |
4385 | if (ret < 0) { | |
4386 | mlog_errno(ret); | |
85db90e7 | 4387 | goto out; |
01225596 TM |
4388 | } |
4389 | ||
4390 | xe->xe_value_size = cpu_to_le64(new_size); | |
4391 | ||
4392 | ret = ocfs2_journal_dirty(handle, xe_bh); | |
4393 | if (ret < 0) | |
4394 | mlog_errno(ret); | |
4395 | ||
01225596 TM |
4396 | out: |
4397 | return ret; | |
4398 | } | |
4399 | ||
4400 | /* | |
4401 | * Truncate the specified xe_off entry in xattr bucket. | |
4402 | * bucket is indicated by header_bh and len is the new length. | |
4403 | * Both the ocfs2_xattr_value_root and the entry will be updated here. | |
4404 | * | |
4405 | * Copy the new updated xe and xe_value_root to new_xe and new_xv if needed. | |
4406 | */ | |
4407 | static int ocfs2_xattr_bucket_value_truncate(struct inode *inode, | |
4408 | struct buffer_head *header_bh, | |
4409 | int xe_off, | |
78f30c31 TM |
4410 | int len, |
4411 | struct ocfs2_xattr_set_ctxt *ctxt) | |
01225596 TM |
4412 | { |
4413 | int ret, offset; | |
4414 | u64 value_blk; | |
4415 | struct buffer_head *value_bh = NULL; | |
4416 | struct ocfs2_xattr_value_root *xv; | |
4417 | struct ocfs2_xattr_entry *xe; | |
4418 | struct ocfs2_xattr_header *xh = | |
4419 | (struct ocfs2_xattr_header *)header_bh->b_data; | |
4420 | size_t blocksize = inode->i_sb->s_blocksize; | |
4421 | ||
4422 | xe = &xh->xh_entries[xe_off]; | |
4423 | ||
4424 | BUG_ON(!xe || ocfs2_xattr_is_local(xe)); | |
4425 | ||
4426 | offset = le16_to_cpu(xe->xe_name_offset) + | |
4427 | OCFS2_XATTR_SIZE(xe->xe_name_len); | |
4428 | ||
4429 | value_blk = offset / blocksize; | |
4430 | ||
4431 | /* We don't allow ocfs2_xattr_value to be stored in different block. */ | |
4432 | BUG_ON(value_blk != (offset + OCFS2_XATTR_ROOT_SIZE - 1) / blocksize); | |
4433 | value_blk += header_bh->b_blocknr; | |
4434 | ||
0fcaa56a | 4435 | ret = ocfs2_read_block(inode, value_blk, &value_bh); |
01225596 TM |
4436 | if (ret) { |
4437 | mlog_errno(ret); | |
4438 | goto out; | |
4439 | } | |
4440 | ||
4441 | xv = (struct ocfs2_xattr_value_root *) | |
4442 | (value_bh->b_data + offset % blocksize); | |
4443 | ||
4444 | mlog(0, "truncate %u in xattr bucket %llu to %d bytes.\n", | |
4445 | xe_off, (unsigned long long)header_bh->b_blocknr, len); | |
78f30c31 | 4446 | ret = ocfs2_xattr_value_truncate(inode, value_bh, xv, len, ctxt); |
01225596 TM |
4447 | if (ret) { |
4448 | mlog_errno(ret); | |
4449 | goto out; | |
4450 | } | |
4451 | ||
85db90e7 TM |
4452 | ret = ocfs2_xattr_value_update_size(inode, ctxt->handle, |
4453 | header_bh, xe, len); | |
01225596 TM |
4454 | if (ret) { |
4455 | mlog_errno(ret); | |
4456 | goto out; | |
4457 | } | |
4458 | ||
4459 | out: | |
4460 | brelse(value_bh); | |
4461 | return ret; | |
4462 | } | |
4463 | ||
4464 | static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode, | |
78f30c31 TM |
4465 | struct ocfs2_xattr_search *xs, |
4466 | int len, | |
4467 | struct ocfs2_xattr_set_ctxt *ctxt) | |
01225596 TM |
4468 | { |
4469 | int ret, offset; | |
4470 | struct ocfs2_xattr_entry *xe = xs->here; | |
4471 | struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base; | |
4472 | ||
ba937127 | 4473 | BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe)); |
01225596 TM |
4474 | |
4475 | offset = xe - xh->xh_entries; | |
ba937127 | 4476 | ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket->bu_bhs[0], |
78f30c31 | 4477 | offset, len, ctxt); |
01225596 TM |
4478 | if (ret) |
4479 | mlog_errno(ret); | |
4480 | ||
4481 | return ret; | |
4482 | } | |
4483 | ||
4484 | static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode, | |
85db90e7 | 4485 | handle_t *handle, |
01225596 TM |
4486 | struct ocfs2_xattr_search *xs, |
4487 | char *val, | |
4488 | int value_len) | |
4489 | { | |
4490 | int offset; | |
4491 | struct ocfs2_xattr_value_root *xv; | |
4492 | struct ocfs2_xattr_entry *xe = xs->here; | |
4493 | ||
4494 | BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe)); | |
4495 | ||
4496 | offset = le16_to_cpu(xe->xe_name_offset) + | |
4497 | OCFS2_XATTR_SIZE(xe->xe_name_len); | |
4498 | ||
4499 | xv = (struct ocfs2_xattr_value_root *)(xs->base + offset); | |
4500 | ||
85db90e7 TM |
4501 | return __ocfs2_xattr_set_value_outside(inode, handle, |
4502 | xv, val, value_len); | |
01225596 TM |
4503 | } |
4504 | ||
01225596 TM |
4505 | static int ocfs2_rm_xattr_cluster(struct inode *inode, |
4506 | struct buffer_head *root_bh, | |
4507 | u64 blkno, | |
4508 | u32 cpos, | |
4509 | u32 len) | |
4510 | { | |
4511 | int ret; | |
4512 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
4513 | struct inode *tl_inode = osb->osb_tl_inode; | |
4514 | handle_t *handle; | |
4515 | struct ocfs2_xattr_block *xb = | |
4516 | (struct ocfs2_xattr_block *)root_bh->b_data; | |
01225596 TM |
4517 | struct ocfs2_alloc_context *meta_ac = NULL; |
4518 | struct ocfs2_cached_dealloc_ctxt dealloc; | |
f99b9b7c JB |
4519 | struct ocfs2_extent_tree et; |
4520 | ||
8d6220d6 | 4521 | ocfs2_init_xattr_tree_extent_tree(&et, inode, root_bh); |
01225596 TM |
4522 | |
4523 | ocfs2_init_dealloc_ctxt(&dealloc); | |
4524 | ||
4525 | mlog(0, "rm xattr extent rec at %u len = %u, start from %llu\n", | |
4526 | cpos, len, (unsigned long long)blkno); | |
4527 | ||
4528 | ocfs2_remove_xattr_clusters_from_cache(inode, blkno, len); | |
4529 | ||
f99b9b7c | 4530 | ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac); |
01225596 TM |
4531 | if (ret) { |
4532 | mlog_errno(ret); | |
4533 | return ret; | |
4534 | } | |
4535 | ||
4536 | mutex_lock(&tl_inode->i_mutex); | |
4537 | ||
4538 | if (ocfs2_truncate_log_needs_flush(osb)) { | |
4539 | ret = __ocfs2_flush_truncate_log(osb); | |
4540 | if (ret < 0) { | |
4541 | mlog_errno(ret); | |
4542 | goto out; | |
4543 | } | |
4544 | } | |
4545 | ||
4546 | handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); | |
d3264799 | 4547 | if (IS_ERR(handle)) { |
01225596 TM |
4548 | ret = -ENOMEM; |
4549 | mlog_errno(ret); | |
4550 | goto out; | |
4551 | } | |
4552 | ||
4553 | ret = ocfs2_journal_access(handle, inode, root_bh, | |
4554 | OCFS2_JOURNAL_ACCESS_WRITE); | |
4555 | if (ret) { | |
4556 | mlog_errno(ret); | |
4557 | goto out_commit; | |
4558 | } | |
4559 | ||
f99b9b7c JB |
4560 | ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac, |
4561 | &dealloc); | |
01225596 TM |
4562 | if (ret) { |
4563 | mlog_errno(ret); | |
4564 | goto out_commit; | |
4565 | } | |
4566 | ||
4567 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, -len); | |
4568 | ||
4569 | ret = ocfs2_journal_dirty(handle, root_bh); | |
4570 | if (ret) { | |
4571 | mlog_errno(ret); | |
4572 | goto out_commit; | |
4573 | } | |
4574 | ||
4575 | ret = ocfs2_truncate_log_append(osb, handle, blkno, len); | |
4576 | if (ret) | |
4577 | mlog_errno(ret); | |
4578 | ||
4579 | out_commit: | |
4580 | ocfs2_commit_trans(osb, handle); | |
4581 | out: | |
4582 | ocfs2_schedule_truncate_log_flush(osb, 1); | |
4583 | ||
4584 | mutex_unlock(&tl_inode->i_mutex); | |
4585 | ||
4586 | if (meta_ac) | |
4587 | ocfs2_free_alloc_context(meta_ac); | |
4588 | ||
4589 | ocfs2_run_deallocs(osb, &dealloc); | |
4590 | ||
4591 | return ret; | |
4592 | } | |
4593 | ||
01225596 | 4594 | static void ocfs2_xattr_bucket_remove_xs(struct inode *inode, |
85db90e7 | 4595 | handle_t *handle, |
01225596 TM |
4596 | struct ocfs2_xattr_search *xs) |
4597 | { | |
ba937127 | 4598 | struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket); |
01225596 TM |
4599 | struct ocfs2_xattr_entry *last = &xh->xh_entries[ |
4600 | le16_to_cpu(xh->xh_count) - 1]; | |
4601 | int ret = 0; | |
4602 | ||
ba937127 | 4603 | ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket, |
1224be02 | 4604 | OCFS2_JOURNAL_ACCESS_WRITE); |
01225596 TM |
4605 | if (ret) { |
4606 | mlog_errno(ret); | |
85db90e7 | 4607 | return; |
01225596 TM |
4608 | } |
4609 | ||
4610 | /* Remove the old entry. */ | |
4611 | memmove(xs->here, xs->here + 1, | |
4612 | (void *)last - (void *)xs->here); | |
4613 | memset(last, 0, sizeof(struct ocfs2_xattr_entry)); | |
4614 | le16_add_cpu(&xh->xh_count, -1); | |
4615 | ||
ba937127 | 4616 | ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket); |
01225596 TM |
4617 | } |
4618 | ||
4619 | /* | |
4620 | * Set the xattr name/value in the bucket specified in xs. | |
4621 | * | |
4622 | * As the new value in xi may be stored in the bucket or in an outside cluster, | |
4623 | * we divide the whole process into 3 steps: | |
4624 | * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket) | |
4625 | * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs) | |
4626 | * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside) | |
4627 | * 4. If the clusters for the new outside value can't be allocated, we need | |
4628 | * to free the xattr we allocated in set. | |
4629 | */ | |
4630 | static int ocfs2_xattr_set_in_bucket(struct inode *inode, | |
4631 | struct ocfs2_xattr_info *xi, | |
78f30c31 TM |
4632 | struct ocfs2_xattr_search *xs, |
4633 | struct ocfs2_xattr_set_ctxt *ctxt) | |
01225596 | 4634 | { |
5a095611 | 4635 | int ret, local = 1; |
01225596 TM |
4636 | size_t value_len; |
4637 | char *val = (char *)xi->value; | |
4638 | struct ocfs2_xattr_entry *xe = xs->here; | |
2057e5c6 TM |
4639 | u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name, |
4640 | strlen(xi->name)); | |
01225596 TM |
4641 | |
4642 | if (!xs->not_found && !ocfs2_xattr_is_local(xe)) { | |
4643 | /* | |
4644 | * We need to truncate the xattr storage first. | |
4645 | * | |
4646 | * If both the old and new value are stored to | |
4647 | * outside block, we only need to truncate | |
4648 | * the storage and then set the value outside. | |
4649 | * | |
4650 | * If the new value should be stored within block, | |
4651 | * we should free all the outside block first and | |
4652 | * the modification to the xattr block will be done | |
4653 | * by following steps. | |
4654 | */ | |
4655 | if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) | |
4656 | value_len = xi->value_len; | |
4657 | else | |
4658 | value_len = 0; | |
4659 | ||
4660 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, | |
78f30c31 TM |
4661 | value_len, |
4662 | ctxt); | |
01225596 TM |
4663 | if (ret) |
4664 | goto out; | |
4665 | ||
4666 | if (value_len) | |
4667 | goto set_value_outside; | |
4668 | } | |
4669 | ||
4670 | value_len = xi->value_len; | |
4671 | /* So we have to handle the inside block change now. */ | |
4672 | if (value_len > OCFS2_XATTR_INLINE_SIZE) { | |
4673 | /* | |
4674 | * If the new value will be stored outside of block, | |
4675 | * initalize a new empty value root and insert it first. | |
4676 | */ | |
4677 | local = 0; | |
4678 | xi->value = &def_xv; | |
4679 | xi->value_len = OCFS2_XATTR_ROOT_SIZE; | |
4680 | } | |
4681 | ||
85db90e7 TM |
4682 | ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs, |
4683 | name_hash, local); | |
01225596 TM |
4684 | if (ret) { |
4685 | mlog_errno(ret); | |
4686 | goto out; | |
4687 | } | |
4688 | ||
5a095611 TM |
4689 | if (value_len <= OCFS2_XATTR_INLINE_SIZE) |
4690 | goto out; | |
01225596 | 4691 | |
5a095611 TM |
4692 | /* allocate the space now for the outside block storage. */ |
4693 | ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs, | |
78f30c31 | 4694 | value_len, ctxt); |
5a095611 TM |
4695 | if (ret) { |
4696 | mlog_errno(ret); | |
4697 | ||
4698 | if (xs->not_found) { | |
4699 | /* | |
4700 | * We can't allocate enough clusters for outside | |
4701 | * storage and we have allocated xattr already, | |
4702 | * so need to remove it. | |
4703 | */ | |
85db90e7 | 4704 | ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs); |
01225596 | 4705 | } |
01225596 TM |
4706 | goto out; |
4707 | } | |
4708 | ||
4709 | set_value_outside: | |
85db90e7 TM |
4710 | ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle, |
4711 | xs, val, value_len); | |
01225596 TM |
4712 | out: |
4713 | return ret; | |
4714 | } | |
4715 | ||
80bcaf34 TM |
4716 | /* |
4717 | * check whether the xattr bucket is filled up with the same hash value. | |
4718 | * If we want to insert the xattr with the same hash, return -ENOSPC. | |
4719 | * If we want to insert a xattr with different hash value, go ahead | |
4720 | * and ocfs2_divide_xattr_bucket will handle this. | |
4721 | */ | |
01225596 | 4722 | static int ocfs2_check_xattr_bucket_collision(struct inode *inode, |
80bcaf34 TM |
4723 | struct ocfs2_xattr_bucket *bucket, |
4724 | const char *name) | |
01225596 | 4725 | { |
3e632946 | 4726 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
80bcaf34 TM |
4727 | u32 name_hash = ocfs2_xattr_name_hash(inode, name, strlen(name)); |
4728 | ||
4729 | if (name_hash != le32_to_cpu(xh->xh_entries[0].xe_name_hash)) | |
4730 | return 0; | |
01225596 TM |
4731 | |
4732 | if (xh->xh_entries[le16_to_cpu(xh->xh_count) - 1].xe_name_hash == | |
4733 | xh->xh_entries[0].xe_name_hash) { | |
4734 | mlog(ML_ERROR, "Too much hash collision in xattr bucket %llu, " | |
4735 | "hash = %u\n", | |
9c7759aa | 4736 | (unsigned long long)bucket_blkno(bucket), |
01225596 TM |
4737 | le32_to_cpu(xh->xh_entries[0].xe_name_hash)); |
4738 | return -ENOSPC; | |
4739 | } | |
4740 | ||
4741 | return 0; | |
4742 | } | |
4743 | ||
4744 | static int ocfs2_xattr_set_entry_index_block(struct inode *inode, | |
4745 | struct ocfs2_xattr_info *xi, | |
78f30c31 TM |
4746 | struct ocfs2_xattr_search *xs, |
4747 | struct ocfs2_xattr_set_ctxt *ctxt) | |
01225596 TM |
4748 | { |
4749 | struct ocfs2_xattr_header *xh; | |
4750 | struct ocfs2_xattr_entry *xe; | |
4751 | u16 count, header_size, xh_free_start; | |
6dde41d9 | 4752 | int free, max_free, need, old; |
01225596 TM |
4753 | size_t value_size = 0, name_len = strlen(xi->name); |
4754 | size_t blocksize = inode->i_sb->s_blocksize; | |
4755 | int ret, allocation = 0; | |
01225596 TM |
4756 | |
4757 | mlog_entry("Set xattr %s in xattr index block\n", xi->name); | |
4758 | ||
4759 | try_again: | |
4760 | xh = xs->header; | |
4761 | count = le16_to_cpu(xh->xh_count); | |
4762 | xh_free_start = le16_to_cpu(xh->xh_free_start); | |
4763 | header_size = sizeof(struct ocfs2_xattr_header) + | |
4764 | count * sizeof(struct ocfs2_xattr_entry); | |
4765 | max_free = OCFS2_XATTR_BUCKET_SIZE - | |
4766 | le16_to_cpu(xh->xh_name_value_len) - header_size; | |
4767 | ||
4768 | mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size " | |
4769 | "of %u which exceed block size\n", | |
ba937127 | 4770 | (unsigned long long)bucket_blkno(xs->bucket), |
01225596 TM |
4771 | header_size); |
4772 | ||
4773 | if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) | |
4774 | value_size = OCFS2_XATTR_ROOT_SIZE; | |
4775 | else if (xi->value) | |
4776 | value_size = OCFS2_XATTR_SIZE(xi->value_len); | |
4777 | ||
4778 | if (xs->not_found) | |
4779 | need = sizeof(struct ocfs2_xattr_entry) + | |
4780 | OCFS2_XATTR_SIZE(name_len) + value_size; | |
4781 | else { | |
4782 | need = value_size + OCFS2_XATTR_SIZE(name_len); | |
4783 | ||
4784 | /* | |
4785 | * We only replace the old value if the new length is smaller | |
4786 | * than the old one. Otherwise we will allocate new space in the | |
4787 | * bucket to store it. | |
4788 | */ | |
4789 | xe = xs->here; | |
4790 | if (ocfs2_xattr_is_local(xe)) | |
4791 | old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size)); | |
4792 | else | |
4793 | old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE); | |
4794 | ||
4795 | if (old >= value_size) | |
4796 | need = 0; | |
4797 | } | |
4798 | ||
4799 | free = xh_free_start - header_size; | |
4800 | /* | |
4801 | * We need to make sure the new name/value pair | |
4802 | * can exist in the same block. | |
4803 | */ | |
4804 | if (xh_free_start % blocksize < need) | |
4805 | free -= xh_free_start % blocksize; | |
4806 | ||
4807 | mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, " | |
4808 | "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len =" | |
4809 | " %u\n", xs->not_found, | |
ba937127 | 4810 | (unsigned long long)bucket_blkno(xs->bucket), |
01225596 TM |
4811 | free, need, max_free, le16_to_cpu(xh->xh_free_start), |
4812 | le16_to_cpu(xh->xh_name_value_len)); | |
4813 | ||
976331d8 TM |
4814 | if (free < need || |
4815 | (xs->not_found && | |
4816 | count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) { | |
01225596 TM |
4817 | if (need <= max_free && |
4818 | count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) { | |
4819 | /* | |
4820 | * We can create the space by defragment. Since only the | |
4821 | * name/value will be moved, the xe shouldn't be changed | |
4822 | * in xs. | |
4823 | */ | |
85db90e7 TM |
4824 | ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle, |
4825 | xs->bucket); | |
01225596 TM |
4826 | if (ret) { |
4827 | mlog_errno(ret); | |
4828 | goto out; | |
4829 | } | |
4830 | ||
4831 | xh_free_start = le16_to_cpu(xh->xh_free_start); | |
4832 | free = xh_free_start - header_size; | |
4833 | if (xh_free_start % blocksize < need) | |
4834 | free -= xh_free_start % blocksize; | |
4835 | ||
4836 | if (free >= need) | |
4837 | goto xattr_set; | |
4838 | ||
4839 | mlog(0, "Can't get enough space for xattr insert by " | |
4840 | "defragment. Need %u bytes, but we have %d, so " | |
4841 | "allocate new bucket for it.\n", need, free); | |
4842 | } | |
4843 | ||
4844 | /* | |
4845 | * We have to add new buckets or clusters and one | |
4846 | * allocation should leave us enough space for insert. | |
4847 | */ | |
4848 | BUG_ON(allocation); | |
4849 | ||
4850 | /* | |
4851 | * We do not allow for overlapping ranges between buckets. And | |
4852 | * the maximum number of collisions we will allow for then is | |
4853 | * one bucket's worth, so check it here whether we need to | |
4854 | * add a new bucket for the insert. | |
4855 | */ | |
80bcaf34 | 4856 | ret = ocfs2_check_xattr_bucket_collision(inode, |
ba937127 | 4857 | xs->bucket, |
80bcaf34 | 4858 | xi->name); |
01225596 TM |
4859 | if (ret) { |
4860 | mlog_errno(ret); | |
4861 | goto out; | |
4862 | } | |
4863 | ||
4864 | ret = ocfs2_add_new_xattr_bucket(inode, | |
4865 | xs->xattr_bh, | |
78f30c31 TM |
4866 | xs->bucket->bu_bhs[0], |
4867 | ctxt); | |
01225596 TM |
4868 | if (ret) { |
4869 | mlog_errno(ret); | |
4870 | goto out; | |
4871 | } | |
4872 | ||
ba937127 | 4873 | ocfs2_xattr_bucket_relse(xs->bucket); |
01225596 TM |
4874 | |
4875 | ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh, | |
4876 | xi->name_index, | |
4877 | xi->name, xs); | |
4878 | if (ret && ret != -ENODATA) | |
4879 | goto out; | |
4880 | xs->not_found = ret; | |
4881 | allocation = 1; | |
4882 | goto try_again; | |
4883 | } | |
4884 | ||
4885 | xattr_set: | |
78f30c31 | 4886 | ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt); |
01225596 TM |
4887 | out: |
4888 | mlog_exit(ret); | |
4889 | return ret; | |
4890 | } | |
a3944256 TM |
4891 | |
4892 | static int ocfs2_delete_xattr_in_bucket(struct inode *inode, | |
4893 | struct ocfs2_xattr_bucket *bucket, | |
4894 | void *para) | |
4895 | { | |
4896 | int ret = 0; | |
3e632946 | 4897 | struct ocfs2_xattr_header *xh = bucket_xh(bucket); |
a3944256 TM |
4898 | u16 i; |
4899 | struct ocfs2_xattr_entry *xe; | |
78f30c31 TM |
4900 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
4901 | struct ocfs2_xattr_set_ctxt ctxt = {NULL, NULL,}; | |
4902 | ||
4903 | ocfs2_init_dealloc_ctxt(&ctxt.dealloc); | |
a3944256 | 4904 | |
85db90e7 TM |
4905 | ctxt.handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS); |
4906 | if (IS_ERR(ctxt.handle)) { | |
4907 | ret = PTR_ERR(ctxt.handle); | |
4908 | mlog_errno(ret); | |
4909 | goto out; | |
4910 | } | |
4911 | ||
a3944256 TM |
4912 | for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { |
4913 | xe = &xh->xh_entries[i]; | |
4914 | if (ocfs2_xattr_is_local(xe)) | |
4915 | continue; | |
4916 | ||
4917 | ret = ocfs2_xattr_bucket_value_truncate(inode, | |
4ac6032d | 4918 | bucket->bu_bhs[0], |
78f30c31 | 4919 | i, 0, &ctxt); |
a3944256 TM |
4920 | if (ret) { |
4921 | mlog_errno(ret); | |
4922 | break; | |
4923 | } | |
4924 | } | |
4925 | ||
85db90e7 | 4926 | ret = ocfs2_commit_trans(osb, ctxt.handle); |
78f30c31 TM |
4927 | ocfs2_schedule_truncate_log_flush(osb, 1); |
4928 | ocfs2_run_deallocs(osb, &ctxt.dealloc); | |
85db90e7 | 4929 | out: |
a3944256 TM |
4930 | return ret; |
4931 | } | |
4932 | ||
4933 | static int ocfs2_delete_xattr_index_block(struct inode *inode, | |
4934 | struct buffer_head *xb_bh) | |
4935 | { | |
4936 | struct ocfs2_xattr_block *xb = | |
4937 | (struct ocfs2_xattr_block *)xb_bh->b_data; | |
4938 | struct ocfs2_extent_list *el = &xb->xb_attrs.xb_root.xt_list; | |
4939 | int ret = 0; | |
4940 | u32 name_hash = UINT_MAX, e_cpos, num_clusters; | |
4941 | u64 p_blkno; | |
4942 | ||
4943 | if (le16_to_cpu(el->l_next_free_rec) == 0) | |
4944 | return 0; | |
4945 | ||
4946 | while (name_hash > 0) { | |
4947 | ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno, | |
4948 | &e_cpos, &num_clusters, el); | |
4949 | if (ret) { | |
4950 | mlog_errno(ret); | |
4951 | goto out; | |
4952 | } | |
4953 | ||
4954 | ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters, | |
4955 | ocfs2_delete_xattr_in_bucket, | |
4956 | NULL); | |
4957 | if (ret) { | |
4958 | mlog_errno(ret); | |
4959 | goto out; | |
4960 | } | |
4961 | ||
4962 | ret = ocfs2_rm_xattr_cluster(inode, xb_bh, | |
4963 | p_blkno, e_cpos, num_clusters); | |
4964 | if (ret) { | |
4965 | mlog_errno(ret); | |
4966 | break; | |
4967 | } | |
4968 | ||
4969 | if (e_cpos == 0) | |
4970 | break; | |
4971 | ||
4972 | name_hash = e_cpos - 1; | |
4973 | } | |
4974 | ||
4975 | out: | |
4976 | return ret; | |
4977 | } | |
99219aea MF |
4978 | |
4979 | /* | |
4980 | * 'trusted' attributes support | |
4981 | */ | |
99219aea MF |
4982 | static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list, |
4983 | size_t list_size, const char *name, | |
4984 | size_t name_len) | |
4985 | { | |
ceb1eba3 | 4986 | const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN; |
99219aea MF |
4987 | const size_t total_len = prefix_len + name_len + 1; |
4988 | ||
4989 | if (list && total_len <= list_size) { | |
4990 | memcpy(list, XATTR_TRUSTED_PREFIX, prefix_len); | |
4991 | memcpy(list + prefix_len, name, name_len); | |
4992 | list[prefix_len + name_len] = '\0'; | |
4993 | } | |
4994 | return total_len; | |
4995 | } | |
4996 | ||
4997 | static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name, | |
4998 | void *buffer, size_t size) | |
4999 | { | |
5000 | if (strcmp(name, "") == 0) | |
5001 | return -EINVAL; | |
5002 | return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name, | |
5003 | buffer, size); | |
5004 | } | |
5005 | ||
5006 | static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name, | |
5007 | const void *value, size_t size, int flags) | |
5008 | { | |
5009 | if (strcmp(name, "") == 0) | |
5010 | return -EINVAL; | |
5011 | ||
5012 | return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value, | |
5013 | size, flags); | |
5014 | } | |
5015 | ||
5016 | struct xattr_handler ocfs2_xattr_trusted_handler = { | |
5017 | .prefix = XATTR_TRUSTED_PREFIX, | |
5018 | .list = ocfs2_xattr_trusted_list, | |
5019 | .get = ocfs2_xattr_trusted_get, | |
5020 | .set = ocfs2_xattr_trusted_set, | |
5021 | }; | |
5022 | ||
99219aea MF |
5023 | /* |
5024 | * 'user' attributes support | |
5025 | */ | |
99219aea MF |
5026 | static size_t ocfs2_xattr_user_list(struct inode *inode, char *list, |
5027 | size_t list_size, const char *name, | |
5028 | size_t name_len) | |
5029 | { | |
ceb1eba3 | 5030 | const size_t prefix_len = XATTR_USER_PREFIX_LEN; |
99219aea MF |
5031 | const size_t total_len = prefix_len + name_len + 1; |
5032 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
5033 | ||
5034 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) | |
5035 | return 0; | |
5036 | ||
5037 | if (list && total_len <= list_size) { | |
5038 | memcpy(list, XATTR_USER_PREFIX, prefix_len); | |
5039 | memcpy(list + prefix_len, name, name_len); | |
5040 | list[prefix_len + name_len] = '\0'; | |
5041 | } | |
5042 | return total_len; | |
5043 | } | |
5044 | ||
5045 | static int ocfs2_xattr_user_get(struct inode *inode, const char *name, | |
5046 | void *buffer, size_t size) | |
5047 | { | |
5048 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
5049 | ||
5050 | if (strcmp(name, "") == 0) | |
5051 | return -EINVAL; | |
5052 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) | |
5053 | return -EOPNOTSUPP; | |
5054 | return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name, | |
5055 | buffer, size); | |
5056 | } | |
5057 | ||
5058 | static int ocfs2_xattr_user_set(struct inode *inode, const char *name, | |
5059 | const void *value, size_t size, int flags) | |
5060 | { | |
5061 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
5062 | ||
5063 | if (strcmp(name, "") == 0) | |
5064 | return -EINVAL; | |
5065 | if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) | |
5066 | return -EOPNOTSUPP; | |
5067 | ||
5068 | return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value, | |
5069 | size, flags); | |
5070 | } | |
5071 | ||
5072 | struct xattr_handler ocfs2_xattr_user_handler = { | |
5073 | .prefix = XATTR_USER_PREFIX, | |
5074 | .list = ocfs2_xattr_user_list, | |
5075 | .get = ocfs2_xattr_user_get, | |
5076 | .set = ocfs2_xattr_user_set, | |
5077 | }; |