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