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