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