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