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