]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
da23017d | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
a24374f4 | 4 | * Copyright (c) 2013 Red Hat, Inc. |
da23017d | 5 | * All Rights Reserved. |
2bd0ea18 | 6 | */ |
9c799827 | 7 | #include "libxfs_priv.h" |
b626fb59 DC |
8 | #include "xfs_fs.h" |
9 | #include "xfs_shared.h" | |
10 | #include "xfs_format.h" | |
11 | #include "xfs_log_format.h" | |
12 | #include "xfs_trans_resv.h" | |
b626fb59 DC |
13 | #include "xfs_sb.h" |
14 | #include "xfs_mount.h" | |
15 | #include "xfs_da_format.h" | |
b626fb59 DC |
16 | #include "xfs_inode.h" |
17 | #include "xfs_trans.h" | |
18 | #include "xfs_bmap_btree.h" | |
19 | #include "xfs_bmap.h" | |
20 | #include "xfs_attr_sf.h" | |
21 | #include "xfs_attr_remote.h" | |
6778635b | 22 | #include "xfs_attr.h" |
b626fb59 DC |
23 | #include "xfs_attr_leaf.h" |
24 | #include "xfs_trace.h" | |
b626fb59 | 25 | #include "xfs_dir2.h" |
2bd0ea18 | 26 | |
2bd0ea18 NS |
27 | |
28 | /* | |
29 | * xfs_attr_leaf.c | |
30 | * | |
31 | * Routines to implement leaf blocks of attributes as Btrees of hashed names. | |
32 | */ | |
33 | ||
5e656dbb BN |
34 | /*======================================================================== |
35 | * Function prototypes for the kernel. | |
36 | *========================================================================*/ | |
37 | ||
38 | /* | |
39 | * Routines used for growing the Btree. | |
40 | */ | |
a24374f4 DC |
41 | STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args, |
42 | xfs_dablk_t which_block, struct xfs_buf **bpp); | |
43 | STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer, | |
44 | struct xfs_attr3_icleaf_hdr *ichdr, | |
45 | struct xfs_da_args *args, int freemap_index); | |
46 | STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args, | |
47 | struct xfs_attr3_icleaf_hdr *ichdr, | |
48 | struct xfs_buf *leaf_buffer); | |
49 | STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state, | |
5e656dbb BN |
50 | xfs_da_state_blk_t *blk1, |
51 | xfs_da_state_blk_t *blk2); | |
a24374f4 DC |
52 | STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state, |
53 | xfs_da_state_blk_t *leaf_blk_1, | |
54 | struct xfs_attr3_icleaf_hdr *ichdr1, | |
55 | xfs_da_state_blk_t *leaf_blk_2, | |
56 | struct xfs_attr3_icleaf_hdr *ichdr2, | |
57 | int *number_entries_in_blk1, | |
58 | int *number_usedbytes_in_blk1); | |
5e656dbb BN |
59 | |
60 | /* | |
61 | * Utility routines. | |
62 | */ | |
ff105f75 DC |
63 | STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args, |
64 | struct xfs_attr_leafblock *src_leaf, | |
a24374f4 DC |
65 | struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start, |
66 | struct xfs_attr_leafblock *dst_leaf, | |
67 | struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start, | |
ff105f75 | 68 | int move_count); |
f302e9e4 NS |
69 | STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index); |
70 | ||
19ebedcf DC |
71 | /* |
72 | * attr3 block 'firstused' conversion helpers. | |
73 | * | |
74 | * firstused refers to the offset of the first used byte of the nameval region | |
75 | * of an attr leaf block. The region starts at the tail of the block and expands | |
76 | * backwards towards the middle. As such, firstused is initialized to the block | |
77 | * size for an empty leaf block and is reduced from there. | |
78 | * | |
79 | * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k. | |
80 | * The in-core firstused field is 32-bit and thus supports the maximum fsb size. | |
81 | * The on-disk field is only 16-bit, however, and overflows at 64k. Since this | |
82 | * only occurs at exactly 64k, we use zero as a magic on-disk value to represent | |
83 | * the attr block size. The following helpers manage the conversion between the | |
84 | * in-core and on-disk formats. | |
85 | */ | |
86 | ||
87 | static void | |
88 | xfs_attr3_leaf_firstused_from_disk( | |
89 | struct xfs_da_geometry *geo, | |
90 | struct xfs_attr3_icleaf_hdr *to, | |
91 | struct xfs_attr_leafblock *from) | |
92 | { | |
93 | struct xfs_attr3_leaf_hdr *hdr3; | |
94 | ||
95 | if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) { | |
96 | hdr3 = (struct xfs_attr3_leaf_hdr *) from; | |
97 | to->firstused = be16_to_cpu(hdr3->firstused); | |
98 | } else { | |
99 | to->firstused = be16_to_cpu(from->hdr.firstused); | |
100 | } | |
101 | ||
102 | /* | |
103 | * Convert from the magic fsb size value to actual blocksize. This | |
104 | * should only occur for empty blocks when the block size overflows | |
105 | * 16-bits. | |
106 | */ | |
107 | if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) { | |
108 | ASSERT(!to->count && !to->usedbytes); | |
109 | ASSERT(geo->blksize > USHRT_MAX); | |
110 | to->firstused = geo->blksize; | |
111 | } | |
112 | } | |
113 | ||
114 | static void | |
115 | xfs_attr3_leaf_firstused_to_disk( | |
116 | struct xfs_da_geometry *geo, | |
117 | struct xfs_attr_leafblock *to, | |
118 | struct xfs_attr3_icleaf_hdr *from) | |
119 | { | |
120 | struct xfs_attr3_leaf_hdr *hdr3; | |
121 | uint32_t firstused; | |
122 | ||
123 | /* magic value should only be seen on disk */ | |
124 | ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF); | |
125 | ||
126 | /* | |
127 | * Scale down the 32-bit in-core firstused value to the 16-bit on-disk | |
128 | * value. This only overflows at the max supported value of 64k. Use the | |
129 | * magic on-disk value to represent block size in this case. | |
130 | */ | |
131 | firstused = from->firstused; | |
132 | if (firstused > USHRT_MAX) { | |
133 | ASSERT(from->firstused == geo->blksize); | |
134 | firstused = XFS_ATTR3_LEAF_NULLOFF; | |
135 | } | |
136 | ||
137 | if (from->magic == XFS_ATTR3_LEAF_MAGIC) { | |
138 | hdr3 = (struct xfs_attr3_leaf_hdr *) to; | |
139 | hdr3->firstused = cpu_to_be16(firstused); | |
140 | } else { | |
141 | to->hdr.firstused = cpu_to_be16(firstused); | |
142 | } | |
143 | } | |
144 | ||
a24374f4 DC |
145 | void |
146 | xfs_attr3_leaf_hdr_from_disk( | |
19ebedcf | 147 | struct xfs_da_geometry *geo, |
a24374f4 DC |
148 | struct xfs_attr3_icleaf_hdr *to, |
149 | struct xfs_attr_leafblock *from) | |
150 | { | |
151 | int i; | |
152 | ||
153 | ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) || | |
154 | from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)); | |
155 | ||
156 | if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) { | |
157 | struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from; | |
158 | ||
159 | to->forw = be32_to_cpu(hdr3->info.hdr.forw); | |
160 | to->back = be32_to_cpu(hdr3->info.hdr.back); | |
161 | to->magic = be16_to_cpu(hdr3->info.hdr.magic); | |
162 | to->count = be16_to_cpu(hdr3->count); | |
163 | to->usedbytes = be16_to_cpu(hdr3->usedbytes); | |
19ebedcf | 164 | xfs_attr3_leaf_firstused_from_disk(geo, to, from); |
a24374f4 DC |
165 | to->holes = hdr3->holes; |
166 | ||
167 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
168 | to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base); | |
169 | to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size); | |
170 | } | |
171 | return; | |
172 | } | |
173 | to->forw = be32_to_cpu(from->hdr.info.forw); | |
174 | to->back = be32_to_cpu(from->hdr.info.back); | |
175 | to->magic = be16_to_cpu(from->hdr.info.magic); | |
176 | to->count = be16_to_cpu(from->hdr.count); | |
177 | to->usedbytes = be16_to_cpu(from->hdr.usedbytes); | |
19ebedcf | 178 | xfs_attr3_leaf_firstused_from_disk(geo, to, from); |
a24374f4 DC |
179 | to->holes = from->hdr.holes; |
180 | ||
181 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
182 | to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base); | |
183 | to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size); | |
184 | } | |
185 | } | |
186 | ||
187 | void | |
188 | xfs_attr3_leaf_hdr_to_disk( | |
19ebedcf | 189 | struct xfs_da_geometry *geo, |
a24374f4 DC |
190 | struct xfs_attr_leafblock *to, |
191 | struct xfs_attr3_icleaf_hdr *from) | |
192 | { | |
19ebedcf | 193 | int i; |
a24374f4 DC |
194 | |
195 | ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC || | |
196 | from->magic == XFS_ATTR3_LEAF_MAGIC); | |
197 | ||
198 | if (from->magic == XFS_ATTR3_LEAF_MAGIC) { | |
199 | struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to; | |
200 | ||
201 | hdr3->info.hdr.forw = cpu_to_be32(from->forw); | |
202 | hdr3->info.hdr.back = cpu_to_be32(from->back); | |
203 | hdr3->info.hdr.magic = cpu_to_be16(from->magic); | |
204 | hdr3->count = cpu_to_be16(from->count); | |
205 | hdr3->usedbytes = cpu_to_be16(from->usedbytes); | |
19ebedcf | 206 | xfs_attr3_leaf_firstused_to_disk(geo, to, from); |
a24374f4 DC |
207 | hdr3->holes = from->holes; |
208 | hdr3->pad1 = 0; | |
209 | ||
210 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
211 | hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base); | |
212 | hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size); | |
213 | } | |
214 | return; | |
215 | } | |
216 | to->hdr.info.forw = cpu_to_be32(from->forw); | |
217 | to->hdr.info.back = cpu_to_be32(from->back); | |
218 | to->hdr.info.magic = cpu_to_be16(from->magic); | |
219 | to->hdr.count = cpu_to_be16(from->count); | |
220 | to->hdr.usedbytes = cpu_to_be16(from->usedbytes); | |
19ebedcf | 221 | xfs_attr3_leaf_firstused_to_disk(geo, to, from); |
a24374f4 DC |
222 | to->hdr.holes = from->holes; |
223 | to->hdr.pad1 = 0; | |
224 | ||
225 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
226 | to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base); | |
227 | to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size); | |
228 | } | |
229 | } | |
230 | ||
bc01119d | 231 | static xfs_failaddr_t |
a24374f4 | 232 | xfs_attr3_leaf_verify( |
f596a90d | 233 | struct xfs_buf *bp) |
a2ceac1f | 234 | { |
f596a90d | 235 | struct xfs_attr3_icleaf_hdr ichdr; |
7861ef77 | 236 | struct xfs_mount *mp = bp->b_mount; |
f596a90d | 237 | struct xfs_attr_leafblock *leaf = bp->b_addr; |
f596a90d | 238 | struct xfs_attr_leaf_entry *entries; |
f26e485f | 239 | uint32_t end; /* must be 32bit - see below */ |
f43b1b37 | 240 | int i; |
6b27f70a | 241 | xfs_failaddr_t fa; |
a2ceac1f | 242 | |
19ebedcf | 243 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf); |
a24374f4 | 244 | |
6b27f70a BF |
245 | fa = xfs_da3_blkinfo_verify(bp, bp->b_addr); |
246 | if (fa) | |
247 | return fa; | |
a24374f4 | 248 | |
a002f1d8 ES |
249 | /* |
250 | * In recovery there is a transient state where count == 0 is valid | |
251 | * because we may have transitioned an empty shortform attr to a leaf | |
252 | * if the attr didn't fit in shortform. | |
253 | */ | |
d780a2f2 | 254 | if (!xfs_log_in_recovery(mp) && ichdr.count == 0) |
bc01119d | 255 | return __this_address; |
a24374f4 | 256 | |
f596a90d DW |
257 | /* |
258 | * firstused is the block offset of the first name info structure. | |
259 | * Make sure it doesn't go off the block or crash into the header. | |
260 | */ | |
261 | if (ichdr.firstused > mp->m_attr_geo->blksize) | |
262 | return __this_address; | |
263 | if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf)) | |
264 | return __this_address; | |
265 | ||
266 | /* Make sure the entries array doesn't crash into the name info. */ | |
267 | entries = xfs_attr3_leaf_entryp(bp->b_addr); | |
268 | if ((char *)&entries[ichdr.count] > | |
269 | (char *)bp->b_addr + ichdr.firstused) | |
270 | return __this_address; | |
271 | ||
a24374f4 DC |
272 | /* XXX: need to range check rest of attr header values */ |
273 | /* XXX: hash order check? */ | |
274 | ||
f43b1b37 DW |
275 | /* |
276 | * Quickly check the freemap information. Attribute data has to be | |
277 | * aligned to 4-byte boundaries, and likewise for the free space. | |
f26e485f DC |
278 | * |
279 | * Note that for 64k block size filesystems, the freemap entries cannot | |
280 | * overflow as they are only be16 fields. However, when checking end | |
281 | * pointer of the freemap, we have to be careful to detect overflows and | |
282 | * so use uint32_t for those checks. | |
f43b1b37 DW |
283 | */ |
284 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { | |
285 | if (ichdr.freemap[i].base > mp->m_attr_geo->blksize) | |
286 | return __this_address; | |
287 | if (ichdr.freemap[i].base & 0x3) | |
288 | return __this_address; | |
289 | if (ichdr.freemap[i].size > mp->m_attr_geo->blksize) | |
290 | return __this_address; | |
291 | if (ichdr.freemap[i].size & 0x3) | |
292 | return __this_address; | |
f26e485f DC |
293 | |
294 | /* be care of 16 bit overflows here */ | |
295 | end = (uint32_t)ichdr.freemap[i].base + ichdr.freemap[i].size; | |
f43b1b37 DW |
296 | if (end < ichdr.freemap[i].base) |
297 | return __this_address; | |
298 | if (end > mp->m_attr_geo->blksize) | |
299 | return __this_address; | |
300 | } | |
301 | ||
bc01119d | 302 | return NULL; |
a2ceac1f DC |
303 | } |
304 | ||
305 | static void | |
a24374f4 | 306 | xfs_attr3_leaf_write_verify( |
a2ceac1f DC |
307 | struct xfs_buf *bp) |
308 | { | |
7861ef77 | 309 | struct xfs_mount *mp = bp->b_mount; |
37d086ca | 310 | struct xfs_buf_log_item *bip = bp->b_log_item; |
a24374f4 | 311 | struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr; |
1e697959 | 312 | xfs_failaddr_t fa; |
a24374f4 | 313 | |
1e697959 DW |
314 | fa = xfs_attr3_leaf_verify(bp); |
315 | if (fa) { | |
316 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
a24374f4 DC |
317 | return; |
318 | } | |
319 | ||
320 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
321 | return; | |
322 | ||
323 | if (bip) | |
324 | hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
325 | ||
43b5aeed | 326 | xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF); |
a2ceac1f DC |
327 | } |
328 | ||
a24374f4 DC |
329 | /* |
330 | * leaf/node format detection on trees is sketchy, so a node read can be done on | |
331 | * leaf level blocks when detection identifies the tree as a node format tree | |
332 | * incorrectly. In this case, we need to swap the verifier to match the correct | |
333 | * format of the block being read. | |
334 | */ | |
a2ceac1f | 335 | static void |
a24374f4 DC |
336 | xfs_attr3_leaf_read_verify( |
337 | struct xfs_buf *bp) | |
a2ceac1f | 338 | { |
7861ef77 | 339 | struct xfs_mount *mp = bp->b_mount; |
1e697959 | 340 | xfs_failaddr_t fa; |
a24374f4 | 341 | |
45922933 DC |
342 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
343 | !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF)) | |
1e697959 DW |
344 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
345 | else { | |
346 | fa = xfs_attr3_leaf_verify(bp); | |
347 | if (fa) | |
348 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
349 | } | |
a2ceac1f DC |
350 | } |
351 | ||
a24374f4 | 352 | const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = { |
a3fac935 | 353 | .name = "xfs_attr3_leaf", |
9e26de8f DW |
354 | .magic16 = { cpu_to_be16(XFS_ATTR_LEAF_MAGIC), |
355 | cpu_to_be16(XFS_ATTR3_LEAF_MAGIC) }, | |
a24374f4 DC |
356 | .verify_read = xfs_attr3_leaf_read_verify, |
357 | .verify_write = xfs_attr3_leaf_write_verify, | |
95d9582b | 358 | .verify_struct = xfs_attr3_leaf_verify, |
a2ceac1f DC |
359 | }; |
360 | ||
361 | int | |
a24374f4 | 362 | xfs_attr3_leaf_read( |
a2ceac1f DC |
363 | struct xfs_trans *tp, |
364 | struct xfs_inode *dp, | |
365 | xfs_dablk_t bno, | |
366 | xfs_daddr_t mappedbno, | |
367 | struct xfs_buf **bpp) | |
368 | { | |
8b4dc4a9 DC |
369 | int err; |
370 | ||
371 | err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp, | |
a24374f4 | 372 | XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops); |
a6c6b1bd | 373 | if (!err && tp && *bpp) |
bdc16ee5 | 374 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF); |
8b4dc4a9 | 375 | return err; |
a2ceac1f DC |
376 | } |
377 | ||
57c9fccb | 378 | /*======================================================================== |
5e656dbb BN |
379 | * Namespace helper routines |
380 | *========================================================================*/ | |
381 | ||
382 | /* | |
383 | * If namespace bits don't match return 0. | |
384 | * If all match then return 1. | |
385 | */ | |
56b2de80 | 386 | STATIC int |
5e656dbb BN |
387 | xfs_attr_namesp_match(int arg_flags, int ondisk_flags) |
388 | { | |
389 | return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags); | |
390 | } | |
391 | ||
4343d303 DC |
392 | static int |
393 | xfs_attr_copy_value( | |
394 | struct xfs_da_args *args, | |
395 | unsigned char *value, | |
396 | int valuelen) | |
397 | { | |
398 | /* | |
399 | * No copy if all we have to do is get the length | |
400 | */ | |
401 | if (args->flags & ATTR_KERNOVAL) { | |
402 | args->valuelen = valuelen; | |
403 | return 0; | |
404 | } | |
405 | ||
406 | /* | |
407 | * No copy if the length of the existing buffer is too small | |
408 | */ | |
409 | if (args->valuelen < valuelen) { | |
410 | args->valuelen = valuelen; | |
411 | return -ERANGE; | |
412 | } | |
b4b9ad30 DC |
413 | |
414 | if (args->op_flags & XFS_DA_OP_ALLOCVAL) { | |
415 | args->value = kmem_alloc_large(valuelen, 0); | |
416 | if (!args->value) | |
417 | return -ENOMEM; | |
418 | } | |
4343d303 DC |
419 | args->valuelen = valuelen; |
420 | ||
421 | /* remote block xattr requires IO for copy-in */ | |
422 | if (args->rmtblkno) | |
423 | return xfs_attr_rmtval_get(args); | |
424 | ||
425 | /* | |
426 | * This is to prevent a GCC warning because the remote xattr case | |
427 | * doesn't have a value to pass in. In that case, we never reach here, | |
428 | * but GCC can't work that out and so throws a "passing NULL to | |
429 | * memcpy" warning. | |
430 | */ | |
431 | if (!value) | |
432 | return -EINVAL; | |
433 | memcpy(args->value, value, valuelen); | |
434 | return 0; | |
435 | } | |
5e656dbb BN |
436 | |
437 | /*======================================================================== | |
438 | * External routines when attribute fork size < XFS_LITINO(mp). | |
57c9fccb NS |
439 | *========================================================================*/ |
440 | ||
441 | /* | |
6239071d NS |
442 | * Query whether the requested number of additional bytes of extended |
443 | * attribute space will be able to fit inline. | |
a2ceac1f | 444 | * |
ca86e759 NS |
445 | * Returns zero if not, else the di_forkoff fork offset to be used in the |
446 | * literal area for attribute data once the new bytes have been added. | |
6239071d NS |
447 | * |
448 | * di_forkoff must be 8 byte aligned, hence is stored as a >>3 value; | |
449 | * special case for dev/uuid inodes, they have fixed size data forks. | |
57c9fccb NS |
450 | */ |
451 | int | |
ca86e759 NS |
452 | xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes) |
453 | { | |
6239071d NS |
454 | int offset; |
455 | int minforkoff; /* lower limit on valid forkoff locations */ | |
456 | int maxforkoff; /* upper limit on valid forkoff locations */ | |
a2ceac1f | 457 | int dsize; |
6239071d | 458 | xfs_mount_t *mp = dp->i_mount; |
ca86e759 | 459 | |
49f693fa DC |
460 | /* rounded down */ |
461 | offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3; | |
ca86e759 | 462 | |
95817f59 | 463 | if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) { |
ca86e759 NS |
464 | minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3; |
465 | return (offset >= minforkoff) ? minforkoff : 0; | |
ca86e759 NS |
466 | } |
467 | ||
a2ceac1f DC |
468 | /* |
469 | * If the requested numbers of bytes is smaller or equal to the | |
470 | * current attribute fork size we can always proceed. | |
471 | * | |
472 | * Note that if_bytes in the data fork might actually be larger than | |
473 | * the current data fork size is due to delalloc extents. In that | |
474 | * case either the extent count will go down when they are converted | |
475 | * to real extents, or the delalloc conversion will take care of the | |
476 | * literal area rebalancing. | |
477 | */ | |
478 | if (bytes <= XFS_IFORK_ASIZE(dp)) | |
479 | return dp->i_d.di_forkoff; | |
480 | ||
481 | /* | |
482 | * For attr2 we can try to move the forkoff if there is space in the | |
483 | * literal area, but for the old format we are done if there is no | |
484 | * space in the fixed attribute fork. | |
485 | */ | |
486 | if (!(mp->m_flags & XFS_MOUNT_ATTR2)) | |
6239071d | 487 | return 0; |
6239071d | 488 | |
5e656dbb | 489 | dsize = dp->i_df.if_bytes; |
a2ceac1f | 490 | |
5e656dbb BN |
491 | switch (dp->i_d.di_format) { |
492 | case XFS_DINODE_FMT_EXTENTS: | |
a2ceac1f | 493 | /* |
f8149110 | 494 | * If there is no attr fork and the data fork is extents, |
a2ceac1f DC |
495 | * determine if creating the default attr fork will result |
496 | * in the extents form migrating to btree. If so, the | |
497 | * minimum offset only needs to be the space required for | |
5e656dbb | 498 | * the btree root. |
a2ceac1f | 499 | */ |
56b2de80 DC |
500 | if (!dp->i_d.di_forkoff && dp->i_df.if_bytes > |
501 | xfs_default_attroffset(dp)) | |
5e656dbb BN |
502 | dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS); |
503 | break; | |
5e656dbb BN |
504 | case XFS_DINODE_FMT_BTREE: |
505 | /* | |
a2ceac1f DC |
506 | * If we have a data btree then keep forkoff if we have one, |
507 | * otherwise we are adding a new attr, so then we set | |
508 | * minforkoff to where the btree root can finish so we have | |
5e656dbb BN |
509 | * plenty of room for attrs |
510 | */ | |
511 | if (dp->i_d.di_forkoff) { | |
a2ceac1f | 512 | if (offset < dp->i_d.di_forkoff) |
5e656dbb | 513 | return 0; |
a2ceac1f DC |
514 | return dp->i_d.di_forkoff; |
515 | } | |
5dfa5cd2 | 516 | dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot); |
5e656dbb BN |
517 | break; |
518 | } | |
a2ceac1f DC |
519 | |
520 | /* | |
521 | * A data fork btree root must have space for at least | |
5e656dbb BN |
522 | * MINDBTPTRS key/ptr pairs if the data fork is small or empty. |
523 | */ | |
d7ff12b8 | 524 | minforkoff = max(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS)); |
ca86e759 NS |
525 | minforkoff = roundup(minforkoff, 8) >> 3; |
526 | ||
527 | /* attr fork btree root can have at least this many key/ptr pairs */ | |
49f693fa DC |
528 | maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) - |
529 | XFS_BMDR_SPACE_CALC(MINABTPTRS); | |
ca86e759 NS |
530 | maxforkoff = maxforkoff >> 3; /* rounded down */ |
531 | ||
ca86e759 NS |
532 | if (offset >= maxforkoff) |
533 | return maxforkoff; | |
a2ceac1f DC |
534 | if (offset >= minforkoff) |
535 | return offset; | |
ca86e759 NS |
536 | return 0; |
537 | } | |
538 | ||
6239071d NS |
539 | /* |
540 | * Switch on the ATTR2 superblock bit (implies also FEATURES2) | |
541 | */ | |
542 | STATIC void | |
543 | xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp) | |
544 | { | |
5e656dbb BN |
545 | if ((mp->m_flags & XFS_MOUNT_ATTR2) && |
546 | !(xfs_sb_version_hasattr2(&mp->m_sb))) { | |
547 | spin_lock(&mp->m_sb_lock); | |
548 | if (!xfs_sb_version_hasattr2(&mp->m_sb)) { | |
549 | xfs_sb_version_addattr2(&mp->m_sb); | |
550 | spin_unlock(&mp->m_sb_lock); | |
19ebedcf | 551 | xfs_log_sb(tp); |
6239071d | 552 | } else |
5e656dbb | 553 | spin_unlock(&mp->m_sb_lock); |
6239071d NS |
554 | } |
555 | } | |
556 | ||
ca86e759 NS |
557 | /* |
558 | * Create the initial contents of a shortform attribute list. | |
559 | */ | |
560 | void | |
57c9fccb NS |
561 | xfs_attr_shortform_create(xfs_da_args_t *args) |
562 | { | |
563 | xfs_attr_sf_hdr_t *hdr; | |
564 | xfs_inode_t *dp; | |
e07055b8 | 565 | struct xfs_ifork *ifp; |
57c9fccb | 566 | |
a2ceac1f DC |
567 | trace_xfs_attr_sf_create(args); |
568 | ||
57c9fccb NS |
569 | dp = args->dp; |
570 | ASSERT(dp != NULL); | |
571 | ifp = dp->i_afp; | |
572 | ASSERT(ifp != NULL); | |
573 | ASSERT(ifp->if_bytes == 0); | |
574 | if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) { | |
575 | ifp->if_flags &= ~XFS_IFEXTENTS; /* just in case */ | |
576 | dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL; | |
577 | ifp->if_flags |= XFS_IFINLINE; | |
578 | } else { | |
579 | ASSERT(ifp->if_flags & XFS_IFINLINE); | |
580 | } | |
581 | xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK); | |
582 | hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data; | |
583 | hdr->count = 0; | |
5e656dbb | 584 | hdr->totsize = cpu_to_be16(sizeof(*hdr)); |
57c9fccb | 585 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); |
57c9fccb NS |
586 | } |
587 | ||
588 | /* | |
589 | * Add a name/value pair to the shortform attribute list. | |
590 | * Overflow from the inode has already been checked for. | |
591 | */ | |
ca86e759 NS |
592 | void |
593 | xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff) | |
57c9fccb NS |
594 | { |
595 | xfs_attr_shortform_t *sf; | |
596 | xfs_attr_sf_entry_t *sfe; | |
597 | int i, offset, size; | |
ca86e759 | 598 | xfs_mount_t *mp; |
57c9fccb | 599 | xfs_inode_t *dp; |
e07055b8 | 600 | struct xfs_ifork *ifp; |
57c9fccb | 601 | |
a2ceac1f DC |
602 | trace_xfs_attr_sf_add(args); |
603 | ||
57c9fccb | 604 | dp = args->dp; |
ca86e759 NS |
605 | mp = dp->i_mount; |
606 | dp->i_d.di_forkoff = forkoff; | |
ca86e759 | 607 | |
57c9fccb NS |
608 | ifp = dp->i_afp; |
609 | ASSERT(ifp->if_flags & XFS_IFINLINE); | |
610 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
611 | sfe = &sf->list[0]; | |
5e656dbb | 612 | for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
ca86e759 | 613 | #ifdef DEBUG |
57c9fccb NS |
614 | if (sfe->namelen != args->namelen) |
615 | continue; | |
616 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) | |
617 | continue; | |
5e656dbb | 618 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
57c9fccb | 619 | continue; |
ca86e759 NS |
620 | ASSERT(0); |
621 | #endif | |
57c9fccb NS |
622 | } |
623 | ||
624 | offset = (char *)sfe - (char *)sf; | |
625 | size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen); | |
626 | xfs_idata_realloc(dp, size, XFS_ATTR_FORK); | |
627 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
628 | sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset); | |
629 | ||
630 | sfe->namelen = args->namelen; | |
5e656dbb BN |
631 | sfe->valuelen = args->valuelen; |
632 | sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags); | |
57c9fccb NS |
633 | memcpy(sfe->nameval, args->name, args->namelen); |
634 | memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen); | |
5e656dbb BN |
635 | sf->hdr.count++; |
636 | be16_add_cpu(&sf->hdr.totsize, size); | |
57c9fccb NS |
637 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA); |
638 | ||
6239071d | 639 | xfs_sbversion_add_attr2(mp, args->trans); |
57c9fccb NS |
640 | } |
641 | ||
56b2de80 DC |
642 | /* |
643 | * After the last attribute is removed revert to original inode format, | |
644 | * making all literal area available to the data fork once more. | |
645 | */ | |
a37f46d5 DC |
646 | void |
647 | xfs_attr_fork_remove( | |
56b2de80 DC |
648 | struct xfs_inode *ip, |
649 | struct xfs_trans *tp) | |
650 | { | |
651 | xfs_idestroy_fork(ip, XFS_ATTR_FORK); | |
652 | ip->i_d.di_forkoff = 0; | |
653 | ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS; | |
654 | ||
655 | ASSERT(ip->i_d.di_anextents == 0); | |
656 | ASSERT(ip->i_afp == NULL); | |
657 | ||
56b2de80 DC |
658 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); |
659 | } | |
660 | ||
57c9fccb | 661 | /* |
ca86e759 | 662 | * Remove an attribute from the shortform attribute list structure. |
57c9fccb NS |
663 | */ |
664 | int | |
665 | xfs_attr_shortform_remove(xfs_da_args_t *args) | |
666 | { | |
667 | xfs_attr_shortform_t *sf; | |
668 | xfs_attr_sf_entry_t *sfe; | |
669 | int base, size=0, end, totsize, i; | |
ca86e759 | 670 | xfs_mount_t *mp; |
57c9fccb NS |
671 | xfs_inode_t *dp; |
672 | ||
a2ceac1f DC |
673 | trace_xfs_attr_sf_remove(args); |
674 | ||
57c9fccb | 675 | dp = args->dp; |
ca86e759 | 676 | mp = dp->i_mount; |
57c9fccb NS |
677 | base = sizeof(xfs_attr_sf_hdr_t); |
678 | sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data; | |
679 | sfe = &sf->list[0]; | |
5e656dbb | 680 | end = sf->hdr.count; |
ca86e759 | 681 | for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), |
57c9fccb NS |
682 | base += size, i++) { |
683 | size = XFS_ATTR_SF_ENTSIZE(sfe); | |
684 | if (sfe->namelen != args->namelen) | |
685 | continue; | |
686 | if (memcmp(sfe->nameval, args->name, args->namelen) != 0) | |
687 | continue; | |
5e656dbb | 688 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
57c9fccb NS |
689 | continue; |
690 | break; | |
691 | } | |
ca86e759 | 692 | if (i == end) |
12b53197 | 693 | return -ENOATTR; |
57c9fccb | 694 | |
ca86e759 NS |
695 | /* |
696 | * Fix up the attribute fork data, covering the hole | |
697 | */ | |
57c9fccb | 698 | end = base + size; |
5e656dbb | 699 | totsize = be16_to_cpu(sf->hdr.totsize); |
ca86e759 NS |
700 | if (end != totsize) |
701 | memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end); | |
5e656dbb BN |
702 | sf->hdr.count--; |
703 | be16_add_cpu(&sf->hdr.totsize, -size); | |
ca86e759 NS |
704 | |
705 | /* | |
706 | * Fix up the start offset of the attribute fork | |
707 | */ | |
708 | totsize -= size; | |
5e656dbb | 709 | if (totsize == sizeof(xfs_attr_sf_hdr_t) && |
56b2de80 DC |
710 | (mp->m_flags & XFS_MOUNT_ATTR2) && |
711 | (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) && | |
712 | !(args->op_flags & XFS_DA_OP_ADDNAME)) { | |
a37f46d5 | 713 | xfs_attr_fork_remove(dp, args->trans); |
ca86e759 NS |
714 | } else { |
715 | xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); | |
716 | dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize); | |
717 | ASSERT(dp->i_d.di_forkoff); | |
5e656dbb BN |
718 | ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) || |
719 | (args->op_flags & XFS_DA_OP_ADDNAME) || | |
720 | !(mp->m_flags & XFS_MOUNT_ATTR2) || | |
721 | dp->i_d.di_format == XFS_DINODE_FMT_BTREE); | |
ca86e759 NS |
722 | xfs_trans_log_inode(args->trans, dp, |
723 | XFS_ILOG_CORE | XFS_ILOG_ADATA); | |
724 | } | |
725 | ||
6239071d | 726 | xfs_sbversion_add_attr2(mp, args->trans); |
57c9fccb | 727 | |
af43ca9f | 728 | return 0; |
57c9fccb NS |
729 | } |
730 | ||
731 | /* | |
732 | * Look up a name in a shortform attribute list structure. | |
733 | */ | |
734 | /*ARGSUSED*/ | |
735 | int | |
736 | xfs_attr_shortform_lookup(xfs_da_args_t *args) | |
737 | { | |
738 | xfs_attr_shortform_t *sf; | |
739 | xfs_attr_sf_entry_t *sfe; | |
740 | int i; | |
e07055b8 | 741 | struct xfs_ifork *ifp; |
57c9fccb | 742 | |
a2ceac1f DC |
743 | trace_xfs_attr_sf_lookup(args); |
744 | ||
57c9fccb NS |
745 | ifp = args->dp->i_afp; |
746 | ASSERT(ifp->if_flags & XFS_IFINLINE); | |
747 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
748 | sfe = &sf->list[0]; | |
5e656dbb | 749 | for (i = 0; i < sf->hdr.count; |
57c9fccb NS |
750 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { |
751 | if (sfe->namelen != args->namelen) | |
752 | continue; | |
753 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) | |
754 | continue; | |
5e656dbb | 755 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) |
57c9fccb | 756 | continue; |
12b53197 | 757 | return -EEXIST; |
5e656dbb | 758 | } |
12b53197 | 759 | return -ENOATTR; |
5e656dbb BN |
760 | } |
761 | ||
762 | /* | |
42a383ab DC |
763 | * Retreive the attribute value and length. |
764 | * | |
765 | * If ATTR_KERNOVAL is specified, only the length needs to be returned. | |
766 | * Unlike a lookup, we only return an error if the attribute does not | |
767 | * exist or we can't retrieve the value. | |
5e656dbb | 768 | */ |
5e656dbb | 769 | int |
4343d303 DC |
770 | xfs_attr_shortform_getvalue( |
771 | struct xfs_da_args *args) | |
5e656dbb | 772 | { |
4343d303 DC |
773 | struct xfs_attr_shortform *sf; |
774 | struct xfs_attr_sf_entry *sfe; | |
775 | int i; | |
5e656dbb | 776 | |
e6d77a21 | 777 | ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE); |
5e656dbb BN |
778 | sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data; |
779 | sfe = &sf->list[0]; | |
780 | for (i = 0; i < sf->hdr.count; | |
781 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) { | |
782 | if (sfe->namelen != args->namelen) | |
57c9fccb | 783 | continue; |
5e656dbb BN |
784 | if (memcmp(args->name, sfe->nameval, args->namelen) != 0) |
785 | continue; | |
786 | if (!xfs_attr_namesp_match(args->flags, sfe->flags)) | |
787 | continue; | |
4343d303 DC |
788 | return xfs_attr_copy_value(args, &sfe->nameval[args->namelen], |
789 | sfe->valuelen); | |
57c9fccb | 790 | } |
12b53197 | 791 | return -ENOATTR; |
57c9fccb NS |
792 | } |
793 | ||
794 | /* | |
3fbc4d7d DW |
795 | * Convert from using the shortform to the leaf. On success, return the |
796 | * buffer so that we can keep it locked until we're totally done with it. | |
57c9fccb NS |
797 | */ |
798 | int | |
3fbc4d7d | 799 | xfs_attr_shortform_to_leaf( |
a50d2ab0 BF |
800 | struct xfs_da_args *args, |
801 | struct xfs_buf **leaf_bp) | |
57c9fccb | 802 | { |
a50d2ab0 BF |
803 | struct xfs_inode *dp; |
804 | struct xfs_attr_shortform *sf; | |
805 | struct xfs_attr_sf_entry *sfe; | |
806 | struct xfs_da_args nargs; | |
807 | char *tmpbuffer; | |
808 | int error, i, size; | |
809 | xfs_dablk_t blkno; | |
810 | struct xfs_buf *bp; | |
811 | struct xfs_ifork *ifp; | |
57c9fccb | 812 | |
a2ceac1f DC |
813 | trace_xfs_attr_sf_to_leaf(args); |
814 | ||
57c9fccb NS |
815 | dp = args->dp; |
816 | ifp = dp->i_afp; | |
817 | sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data; | |
5e656dbb | 818 | size = be16_to_cpu(sf->hdr.totsize); |
6cd1e6db | 819 | tmpbuffer = kmem_alloc(size, 0); |
57c9fccb NS |
820 | ASSERT(tmpbuffer != NULL); |
821 | memcpy(tmpbuffer, ifp->if_u1.if_data, size); | |
822 | sf = (xfs_attr_shortform_t *)tmpbuffer; | |
823 | ||
824 | xfs_idata_realloc(dp, -size, XFS_ATTR_FORK); | |
3f17ed4b | 825 | xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK); |
2e3614c7 | 826 | xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE); |
3f17ed4b | 827 | |
57c9fccb NS |
828 | bp = NULL; |
829 | error = xfs_da_grow_inode(args, &blkno); | |
a35db947 | 830 | if (error) |
57c9fccb | 831 | goto out; |
57c9fccb NS |
832 | |
833 | ASSERT(blkno == 0); | |
a24374f4 | 834 | error = xfs_attr3_leaf_create(args, blkno, &bp); |
a35db947 | 835 | if (error) |
57c9fccb | 836 | goto out; |
57c9fccb NS |
837 | |
838 | memset((char *)&nargs, 0, sizeof(nargs)); | |
839 | nargs.dp = dp; | |
ff105f75 | 840 | nargs.geo = args->geo; |
57c9fccb NS |
841 | nargs.total = args->total; |
842 | nargs.whichfork = XFS_ATTR_FORK; | |
843 | nargs.trans = args->trans; | |
5e656dbb | 844 | nargs.op_flags = XFS_DA_OP_OKNOENT; |
57c9fccb NS |
845 | |
846 | sfe = &sf->list[0]; | |
5e656dbb | 847 | for (i = 0; i < sf->hdr.count; i++) { |
56b2de80 | 848 | nargs.name = sfe->nameval; |
57c9fccb | 849 | nargs.namelen = sfe->namelen; |
56b2de80 | 850 | nargs.value = &sfe->nameval[nargs.namelen]; |
5e656dbb | 851 | nargs.valuelen = sfe->valuelen; |
56b2de80 | 852 | nargs.hashval = xfs_da_hashname(sfe->nameval, |
57c9fccb | 853 | sfe->namelen); |
5e656dbb | 854 | nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags); |
a24374f4 | 855 | error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */ |
12b53197 | 856 | ASSERT(error == -ENOATTR); |
a24374f4 | 857 | error = xfs_attr3_leaf_add(bp, &nargs); |
12b53197 | 858 | ASSERT(error != -ENOSPC); |
57c9fccb NS |
859 | if (error) |
860 | goto out; | |
861 | sfe = XFS_ATTR_SF_NEXTENTRY(sfe); | |
862 | } | |
863 | error = 0; | |
3fbc4d7d | 864 | *leaf_bp = bp; |
57c9fccb | 865 | out: |
5e656dbb | 866 | kmem_free(tmpbuffer); |
af43ca9f | 867 | return error; |
57c9fccb NS |
868 | } |
869 | ||
870 | /* | |
871 | * Check a leaf attribute block to see if all the entries would fit into | |
872 | * a shortform attribute list. | |
873 | */ | |
874 | int | |
a2ceac1f | 875 | xfs_attr_shortform_allfit( |
bdb34d6b DC |
876 | struct xfs_buf *bp, |
877 | struct xfs_inode *dp) | |
57c9fccb | 878 | { |
bdb34d6b DC |
879 | struct xfs_attr_leafblock *leaf; |
880 | struct xfs_attr_leaf_entry *entry; | |
57c9fccb | 881 | xfs_attr_leaf_name_local_t *name_loc; |
bdb34d6b DC |
882 | struct xfs_attr3_icleaf_hdr leafhdr; |
883 | int bytes; | |
884 | int i; | |
7861ef77 | 885 | struct xfs_mount *mp = bp->b_mount; |
57c9fccb | 886 | |
a2ceac1f | 887 | leaf = bp->b_addr; |
19ebedcf | 888 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf); |
bdb34d6b | 889 | entry = xfs_attr3_leaf_entryp(leaf); |
57c9fccb | 890 | |
57c9fccb | 891 | bytes = sizeof(struct xfs_attr_sf_hdr); |
bdb34d6b | 892 | for (i = 0; i < leafhdr.count; entry++, i++) { |
57c9fccb NS |
893 | if (entry->flags & XFS_ATTR_INCOMPLETE) |
894 | continue; /* don't copy partial entries */ | |
895 | if (!(entry->flags & XFS_ATTR_LOCAL)) | |
af43ca9f | 896 | return 0; |
a24374f4 | 897 | name_loc = xfs_attr3_leaf_name_local(leaf, i); |
57c9fccb | 898 | if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX) |
af43ca9f | 899 | return 0; |
5e656dbb | 900 | if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX) |
af43ca9f | 901 | return 0; |
bdb34d6b | 902 | bytes += sizeof(struct xfs_attr_sf_entry) - 1 |
57c9fccb | 903 | + name_loc->namelen |
5e656dbb | 904 | + be16_to_cpu(name_loc->valuelen); |
57c9fccb | 905 | } |
5e656dbb BN |
906 | if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) && |
907 | (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) && | |
6e3140c7 | 908 | (bytes == sizeof(struct xfs_attr_sf_hdr))) |
bdb34d6b DC |
909 | return -1; |
910 | return xfs_attr_shortform_bytesfit(dp, bytes); | |
57c9fccb NS |
911 | } |
912 | ||
c7a710b7 DW |
913 | /* Verify the consistency of an inline attribute fork. */ |
914 | xfs_failaddr_t | |
915 | xfs_attr_shortform_verify( | |
916 | struct xfs_inode *ip) | |
917 | { | |
918 | struct xfs_attr_shortform *sfp; | |
919 | struct xfs_attr_sf_entry *sfep; | |
920 | struct xfs_attr_sf_entry *next_sfep; | |
921 | char *endp; | |
922 | struct xfs_ifork *ifp; | |
923 | int i; | |
924 | int size; | |
925 | ||
926 | ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL); | |
927 | ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK); | |
928 | sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data; | |
929 | size = ifp->if_bytes; | |
930 | ||
931 | /* | |
932 | * Give up if the attribute is way too short. | |
933 | */ | |
934 | if (size < sizeof(struct xfs_attr_sf_hdr)) | |
935 | return __this_address; | |
936 | ||
937 | endp = (char *)sfp + size; | |
938 | ||
939 | /* Check all reported entries */ | |
940 | sfep = &sfp->list[0]; | |
941 | for (i = 0; i < sfp->hdr.count; i++) { | |
942 | /* | |
943 | * struct xfs_attr_sf_entry has a variable length. | |
944 | * Check the fixed-offset parts of the structure are | |
945 | * within the data buffer. | |
946 | */ | |
947 | if (((char *)sfep + sizeof(*sfep)) >= endp) | |
948 | return __this_address; | |
949 | ||
950 | /* Don't allow names with known bad length. */ | |
951 | if (sfep->namelen == 0) | |
952 | return __this_address; | |
953 | ||
954 | /* | |
955 | * Check that the variable-length part of the structure is | |
956 | * within the data buffer. The next entry starts after the | |
957 | * name component, so nextentry is an acceptable test. | |
958 | */ | |
959 | next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep); | |
960 | if ((char *)next_sfep > endp) | |
961 | return __this_address; | |
962 | ||
963 | /* | |
964 | * Check for unknown flags. Short form doesn't support | |
965 | * the incomplete or local bits, so we can use the namespace | |
966 | * mask here. | |
967 | */ | |
968 | if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK) | |
969 | return __this_address; | |
970 | ||
971 | /* | |
972 | * Check for invalid namespace combinations. We only allow | |
973 | * one namespace flag per xattr, so we can just count the | |
974 | * bits (i.e. hweight) here. | |
975 | */ | |
976 | if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1) | |
977 | return __this_address; | |
978 | ||
979 | sfep = next_sfep; | |
980 | } | |
981 | if ((void *)sfep != (void *)endp) | |
982 | return __this_address; | |
983 | ||
984 | return NULL; | |
985 | } | |
986 | ||
57c9fccb NS |
987 | /* |
988 | * Convert a leaf attribute list to shortform attribute list | |
989 | */ | |
990 | int | |
a24374f4 DC |
991 | xfs_attr3_leaf_to_shortform( |
992 | struct xfs_buf *bp, | |
993 | struct xfs_da_args *args, | |
994 | int forkoff) | |
57c9fccb | 995 | { |
a24374f4 DC |
996 | struct xfs_attr_leafblock *leaf; |
997 | struct xfs_attr3_icleaf_hdr ichdr; | |
998 | struct xfs_attr_leaf_entry *entry; | |
999 | struct xfs_attr_leaf_name_local *name_loc; | |
1000 | struct xfs_da_args nargs; | |
1001 | struct xfs_inode *dp = args->dp; | |
1002 | char *tmpbuffer; | |
1003 | int error; | |
1004 | int i; | |
57c9fccb | 1005 | |
a2ceac1f DC |
1006 | trace_xfs_attr_leaf_to_sf(args); |
1007 | ||
6cd1e6db | 1008 | tmpbuffer = kmem_alloc(args->geo->blksize, 0); |
a24374f4 | 1009 | if (!tmpbuffer) |
12b53197 | 1010 | return -ENOMEM; |
57c9fccb | 1011 | |
ff105f75 | 1012 | memcpy(tmpbuffer, bp->b_addr, args->geo->blksize); |
a24374f4 | 1013 | |
57c9fccb | 1014 | leaf = (xfs_attr_leafblock_t *)tmpbuffer; |
19ebedcf | 1015 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
a24374f4 DC |
1016 | entry = xfs_attr3_leaf_entryp(leaf); |
1017 | ||
1018 | /* XXX (dgc): buffer is about to be marked stale - why zero it? */ | |
ff105f75 | 1019 | memset(bp->b_addr, 0, args->geo->blksize); |
57c9fccb NS |
1020 | |
1021 | /* | |
1022 | * Clean out the prior contents of the attribute list. | |
1023 | */ | |
1024 | error = xfs_da_shrink_inode(args, 0, bp); | |
1025 | if (error) | |
1026 | goto out; | |
ca86e759 NS |
1027 | |
1028 | if (forkoff == -1) { | |
5e656dbb BN |
1029 | ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2); |
1030 | ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE); | |
a37f46d5 | 1031 | xfs_attr_fork_remove(dp, args->trans); |
57c9fccb | 1032 | goto out; |
ca86e759 NS |
1033 | } |
1034 | ||
1035 | xfs_attr_shortform_create(args); | |
57c9fccb NS |
1036 | |
1037 | /* | |
1038 | * Copy the attributes | |
1039 | */ | |
1040 | memset((char *)&nargs, 0, sizeof(nargs)); | |
ff105f75 | 1041 | nargs.geo = args->geo; |
57c9fccb | 1042 | nargs.dp = dp; |
57c9fccb NS |
1043 | nargs.total = args->total; |
1044 | nargs.whichfork = XFS_ATTR_FORK; | |
1045 | nargs.trans = args->trans; | |
5e656dbb | 1046 | nargs.op_flags = XFS_DA_OP_OKNOENT; |
a24374f4 DC |
1047 | |
1048 | for (i = 0; i < ichdr.count; entry++, i++) { | |
57c9fccb NS |
1049 | if (entry->flags & XFS_ATTR_INCOMPLETE) |
1050 | continue; /* don't copy partial entries */ | |
1051 | if (!entry->nameidx) | |
1052 | continue; | |
1053 | ASSERT(entry->flags & XFS_ATTR_LOCAL); | |
a24374f4 | 1054 | name_loc = xfs_attr3_leaf_name_local(leaf, i); |
56b2de80 | 1055 | nargs.name = name_loc->nameval; |
57c9fccb | 1056 | nargs.namelen = name_loc->namelen; |
56b2de80 | 1057 | nargs.value = &name_loc->nameval[nargs.namelen]; |
5e656dbb BN |
1058 | nargs.valuelen = be16_to_cpu(name_loc->valuelen); |
1059 | nargs.hashval = be32_to_cpu(entry->hashval); | |
1060 | nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags); | |
ca86e759 | 1061 | xfs_attr_shortform_add(&nargs, forkoff); |
57c9fccb NS |
1062 | } |
1063 | error = 0; | |
1064 | ||
1065 | out: | |
5e656dbb | 1066 | kmem_free(tmpbuffer); |
a24374f4 | 1067 | return error; |
57c9fccb NS |
1068 | } |
1069 | ||
1070 | /* | |
1071 | * Convert from using a single leaf to a root node and a leaf. | |
1072 | */ | |
1073 | int | |
a24374f4 DC |
1074 | xfs_attr3_leaf_to_node( |
1075 | struct xfs_da_args *args) | |
57c9fccb | 1076 | { |
a24374f4 DC |
1077 | struct xfs_attr_leafblock *leaf; |
1078 | struct xfs_attr3_icleaf_hdr icleafhdr; | |
1079 | struct xfs_attr_leaf_entry *entries; | |
88b32f06 | 1080 | struct xfs_da_node_entry *btree; |
a24374f4 DC |
1081 | struct xfs_da3_icnode_hdr icnodehdr; |
1082 | struct xfs_da_intnode *node; | |
1083 | struct xfs_inode *dp = args->dp; | |
1084 | struct xfs_mount *mp = dp->i_mount; | |
1085 | struct xfs_buf *bp1 = NULL; | |
1086 | struct xfs_buf *bp2 = NULL; | |
1087 | xfs_dablk_t blkno; | |
1088 | int error; | |
57c9fccb | 1089 | |
a2ceac1f DC |
1090 | trace_xfs_attr_leaf_to_node(args); |
1091 | ||
57c9fccb NS |
1092 | error = xfs_da_grow_inode(args, &blkno); |
1093 | if (error) | |
1094 | goto out; | |
a24374f4 | 1095 | error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1); |
57c9fccb NS |
1096 | if (error) |
1097 | goto out; | |
a2ceac1f | 1098 | |
a24374f4 | 1099 | error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK); |
57c9fccb NS |
1100 | if (error) |
1101 | goto out; | |
a24374f4 DC |
1102 | |
1103 | /* copy leaf to new buffer, update identifiers */ | |
bdc16ee5 | 1104 | xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF); |
a2ceac1f | 1105 | bp2->b_ops = bp1->b_ops; |
ff105f75 | 1106 | memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize); |
a24374f4 DC |
1107 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
1108 | struct xfs_da3_blkinfo *hdr3 = bp2->b_addr; | |
1109 | hdr3->blkno = cpu_to_be64(bp2->b_bn); | |
1110 | } | |
ff105f75 | 1111 | xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1); |
57c9fccb NS |
1112 | |
1113 | /* | |
1114 | * Set up the new root node. | |
1115 | */ | |
88b32f06 | 1116 | error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK); |
57c9fccb NS |
1117 | if (error) |
1118 | goto out; | |
a2ceac1f | 1119 | node = bp1->b_addr; |
ff105f75 DC |
1120 | dp->d_ops->node_hdr_from_disk(&icnodehdr, node); |
1121 | btree = dp->d_ops->node_tree_p(node); | |
a24374f4 | 1122 | |
a2ceac1f | 1123 | leaf = bp2->b_addr; |
19ebedcf | 1124 | xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf); |
a24374f4 DC |
1125 | entries = xfs_attr3_leaf_entryp(leaf); |
1126 | ||
57c9fccb | 1127 | /* both on-disk, don't endian-flip twice */ |
a24374f4 | 1128 | btree[0].hashval = entries[icleafhdr.count - 1].hashval; |
88b32f06 | 1129 | btree[0].before = cpu_to_be32(blkno); |
a24374f4 | 1130 | icnodehdr.count = 1; |
ff105f75 DC |
1131 | dp->d_ops->node_hdr_to_disk(node, &icnodehdr); |
1132 | xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1); | |
57c9fccb NS |
1133 | error = 0; |
1134 | out: | |
a24374f4 | 1135 | return error; |
57c9fccb NS |
1136 | } |
1137 | ||
2bd0ea18 NS |
1138 | /*======================================================================== |
1139 | * Routines used for growing the Btree. | |
1140 | *========================================================================*/ | |
1141 | ||
1142 | /* | |
1143 | * Create the initial contents of a leaf attribute list | |
1144 | * or a leaf in a node attribute list. | |
1145 | */ | |
5e656dbb | 1146 | STATIC int |
a24374f4 DC |
1147 | xfs_attr3_leaf_create( |
1148 | struct xfs_da_args *args, | |
1149 | xfs_dablk_t blkno, | |
1150 | struct xfs_buf **bpp) | |
2bd0ea18 | 1151 | { |
a24374f4 DC |
1152 | struct xfs_attr_leafblock *leaf; |
1153 | struct xfs_attr3_icleaf_hdr ichdr; | |
1154 | struct xfs_inode *dp = args->dp; | |
1155 | struct xfs_mount *mp = dp->i_mount; | |
1156 | struct xfs_buf *bp; | |
1157 | int error; | |
2bd0ea18 | 1158 | |
a2ceac1f DC |
1159 | trace_xfs_attr_leaf_create(args); |
1160 | ||
2bd0ea18 NS |
1161 | error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp, |
1162 | XFS_ATTR_FORK); | |
1163 | if (error) | |
a24374f4 DC |
1164 | return error; |
1165 | bp->b_ops = &xfs_attr3_leaf_buf_ops; | |
bdc16ee5 | 1166 | xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF); |
a2ceac1f | 1167 | leaf = bp->b_addr; |
ff105f75 | 1168 | memset(leaf, 0, args->geo->blksize); |
a24374f4 DC |
1169 | |
1170 | memset(&ichdr, 0, sizeof(ichdr)); | |
ff105f75 | 1171 | ichdr.firstused = args->geo->blksize; |
a24374f4 DC |
1172 | |
1173 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
1174 | struct xfs_da3_blkinfo *hdr3 = bp->b_addr; | |
2bd0ea18 | 1175 | |
a24374f4 DC |
1176 | ichdr.magic = XFS_ATTR3_LEAF_MAGIC; |
1177 | ||
1178 | hdr3->blkno = cpu_to_be64(bp->b_bn); | |
1179 | hdr3->owner = cpu_to_be64(dp->i_ino); | |
9c4e12fb | 1180 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid); |
a24374f4 DC |
1181 | |
1182 | ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr); | |
1183 | } else { | |
1184 | ichdr.magic = XFS_ATTR_LEAF_MAGIC; | |
1185 | ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr); | |
1186 | } | |
1187 | ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base; | |
2bd0ea18 | 1188 | |
19ebedcf | 1189 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); |
ff105f75 | 1190 | xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1); |
2bd0ea18 NS |
1191 | |
1192 | *bpp = bp; | |
a24374f4 | 1193 | return 0; |
2bd0ea18 NS |
1194 | } |
1195 | ||
1196 | /* | |
1197 | * Split the leaf node, rebalance, then add the new entry. | |
1198 | */ | |
1199 | int | |
a24374f4 DC |
1200 | xfs_attr3_leaf_split( |
1201 | struct xfs_da_state *state, | |
1202 | struct xfs_da_state_blk *oldblk, | |
1203 | struct xfs_da_state_blk *newblk) | |
2bd0ea18 NS |
1204 | { |
1205 | xfs_dablk_t blkno; | |
1206 | int error; | |
1207 | ||
a2ceac1f DC |
1208 | trace_xfs_attr_leaf_split(state->args); |
1209 | ||
2bd0ea18 NS |
1210 | /* |
1211 | * Allocate space for a new leaf node. | |
1212 | */ | |
1213 | ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC); | |
1214 | error = xfs_da_grow_inode(state->args, &blkno); | |
1215 | if (error) | |
af43ca9f | 1216 | return error; |
a24374f4 | 1217 | error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp); |
2bd0ea18 | 1218 | if (error) |
af43ca9f | 1219 | return error; |
2bd0ea18 NS |
1220 | newblk->blkno = blkno; |
1221 | newblk->magic = XFS_ATTR_LEAF_MAGIC; | |
1222 | ||
1223 | /* | |
1224 | * Rebalance the entries across the two leaves. | |
1225 | * NOTE: rebalance() currently depends on the 2nd block being empty. | |
1226 | */ | |
a24374f4 | 1227 | xfs_attr3_leaf_rebalance(state, oldblk, newblk); |
88b32f06 | 1228 | error = xfs_da3_blk_link(state, oldblk, newblk); |
2bd0ea18 | 1229 | if (error) |
af43ca9f | 1230 | return error; |
2bd0ea18 NS |
1231 | |
1232 | /* | |
1233 | * Save info on "old" attribute for "atomic rename" ops, leaf_add() | |
1234 | * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the | |
1235 | * "new" attrs info. Will need the "old" info to remove it later. | |
1236 | * | |
1237 | * Insert the "new" entry in the correct block. | |
1238 | */ | |
a2ceac1f DC |
1239 | if (state->inleaf) { |
1240 | trace_xfs_attr_leaf_add_old(state->args); | |
a24374f4 | 1241 | error = xfs_attr3_leaf_add(oldblk->bp, state->args); |
a2ceac1f DC |
1242 | } else { |
1243 | trace_xfs_attr_leaf_add_new(state->args); | |
a24374f4 | 1244 | error = xfs_attr3_leaf_add(newblk->bp, state->args); |
a2ceac1f | 1245 | } |
2bd0ea18 NS |
1246 | |
1247 | /* | |
1248 | * Update last hashval in each block since we added the name. | |
1249 | */ | |
1250 | oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL); | |
1251 | newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL); | |
af43ca9f | 1252 | return error; |
2bd0ea18 NS |
1253 | } |
1254 | ||
1255 | /* | |
1256 | * Add a name to the leaf attribute list structure. | |
1257 | */ | |
1258 | int | |
a24374f4 | 1259 | xfs_attr3_leaf_add( |
a2ceac1f DC |
1260 | struct xfs_buf *bp, |
1261 | struct xfs_da_args *args) | |
2bd0ea18 | 1262 | { |
a24374f4 DC |
1263 | struct xfs_attr_leafblock *leaf; |
1264 | struct xfs_attr3_icleaf_hdr ichdr; | |
1265 | int tablesize; | |
1266 | int entsize; | |
1267 | int sum; | |
1268 | int tmp; | |
1269 | int i; | |
2bd0ea18 | 1270 | |
a2ceac1f DC |
1271 | trace_xfs_attr_leaf_add(args); |
1272 | ||
1273 | leaf = bp->b_addr; | |
19ebedcf | 1274 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
a24374f4 | 1275 | ASSERT(args->index >= 0 && args->index <= ichdr.count); |
ff105f75 | 1276 | entsize = xfs_attr_leaf_newentsize(args, NULL); |
2bd0ea18 NS |
1277 | |
1278 | /* | |
1279 | * Search through freemap for first-fit on new name length. | |
1280 | * (may need to figure in size of entry struct too) | |
1281 | */ | |
a24374f4 DC |
1282 | tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t) |
1283 | + xfs_attr3_leaf_hdr_size(leaf); | |
1284 | for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) { | |
1285 | if (tablesize > ichdr.firstused) { | |
1286 | sum += ichdr.freemap[i].size; | |
2bd0ea18 NS |
1287 | continue; |
1288 | } | |
a24374f4 | 1289 | if (!ichdr.freemap[i].size) |
2bd0ea18 NS |
1290 | continue; /* no space in this map */ |
1291 | tmp = entsize; | |
a24374f4 | 1292 | if (ichdr.freemap[i].base < ichdr.firstused) |
2bd0ea18 | 1293 | tmp += sizeof(xfs_attr_leaf_entry_t); |
a24374f4 DC |
1294 | if (ichdr.freemap[i].size >= tmp) { |
1295 | tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i); | |
1296 | goto out_log_hdr; | |
2bd0ea18 | 1297 | } |
a24374f4 | 1298 | sum += ichdr.freemap[i].size; |
2bd0ea18 NS |
1299 | } |
1300 | ||
1301 | /* | |
1302 | * If there are no holes in the address space of the block, | |
1303 | * and we don't have enough freespace, then compaction will do us | |
1304 | * no good and we should just give up. | |
1305 | */ | |
a24374f4 | 1306 | if (!ichdr.holes && sum < entsize) |
12b53197 | 1307 | return -ENOSPC; |
2bd0ea18 NS |
1308 | |
1309 | /* | |
1310 | * Compact the entries to coalesce free space. | |
1311 | * This may change the hdr->count via dropping INCOMPLETE entries. | |
1312 | */ | |
a24374f4 | 1313 | xfs_attr3_leaf_compact(args, &ichdr, bp); |
2bd0ea18 NS |
1314 | |
1315 | /* | |
1316 | * After compaction, the block is guaranteed to have only one | |
dfc130f3 | 1317 | * free region, in freemap[0]. If it is not big enough, give up. |
2bd0ea18 | 1318 | */ |
a24374f4 | 1319 | if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) { |
12b53197 | 1320 | tmp = -ENOSPC; |
a24374f4 DC |
1321 | goto out_log_hdr; |
1322 | } | |
1323 | ||
1324 | tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0); | |
2bd0ea18 | 1325 | |
a24374f4 | 1326 | out_log_hdr: |
19ebedcf | 1327 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); |
a24374f4 DC |
1328 | xfs_trans_log_buf(args->trans, bp, |
1329 | XFS_DA_LOGRANGE(leaf, &leaf->hdr, | |
1330 | xfs_attr3_leaf_hdr_size(leaf))); | |
1331 | return tmp; | |
2bd0ea18 NS |
1332 | } |
1333 | ||
1334 | /* | |
1335 | * Add a name to a leaf attribute list structure. | |
1336 | */ | |
1337 | STATIC int | |
a24374f4 DC |
1338 | xfs_attr3_leaf_add_work( |
1339 | struct xfs_buf *bp, | |
1340 | struct xfs_attr3_icleaf_hdr *ichdr, | |
1341 | struct xfs_da_args *args, | |
1342 | int mapindex) | |
2bd0ea18 | 1343 | { |
a24374f4 DC |
1344 | struct xfs_attr_leafblock *leaf; |
1345 | struct xfs_attr_leaf_entry *entry; | |
1346 | struct xfs_attr_leaf_name_local *name_loc; | |
1347 | struct xfs_attr_leaf_name_remote *name_rmt; | |
a24374f4 DC |
1348 | struct xfs_mount *mp; |
1349 | int tmp; | |
1350 | int i; | |
2bd0ea18 | 1351 | |
a2ceac1f DC |
1352 | trace_xfs_attr_leaf_add_work(args); |
1353 | ||
1354 | leaf = bp->b_addr; | |
a24374f4 DC |
1355 | ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE); |
1356 | ASSERT(args->index >= 0 && args->index <= ichdr->count); | |
2bd0ea18 NS |
1357 | |
1358 | /* | |
1359 | * Force open some space in the entry array and fill it in. | |
1360 | */ | |
a24374f4 DC |
1361 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
1362 | if (args->index < ichdr->count) { | |
1363 | tmp = ichdr->count - args->index; | |
2bd0ea18 | 1364 | tmp *= sizeof(xfs_attr_leaf_entry_t); |
a24374f4 | 1365 | memmove(entry + 1, entry, tmp); |
a2ceac1f | 1366 | xfs_trans_log_buf(args->trans, bp, |
2bd0ea18 NS |
1367 | XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry))); |
1368 | } | |
a24374f4 | 1369 | ichdr->count++; |
2bd0ea18 NS |
1370 | |
1371 | /* | |
1372 | * Allocate space for the new string (at the end of the run). | |
1373 | */ | |
2bd0ea18 | 1374 | mp = args->trans->t_mountp; |
ff105f75 | 1375 | ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize); |
a24374f4 DC |
1376 | ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0); |
1377 | ASSERT(ichdr->freemap[mapindex].size >= | |
ff105f75 DC |
1378 | xfs_attr_leaf_newentsize(args, NULL)); |
1379 | ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize); | |
a24374f4 DC |
1380 | ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0); |
1381 | ||
ff105f75 | 1382 | ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp); |
a24374f4 DC |
1383 | |
1384 | entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base + | |
1385 | ichdr->freemap[mapindex].size); | |
5e656dbb | 1386 | entry->hashval = cpu_to_be32(args->hashval); |
2bd0ea18 | 1387 | entry->flags = tmp ? XFS_ATTR_LOCAL : 0; |
5e656dbb BN |
1388 | entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags); |
1389 | if (args->op_flags & XFS_DA_OP_RENAME) { | |
2bd0ea18 NS |
1390 | entry->flags |= XFS_ATTR_INCOMPLETE; |
1391 | if ((args->blkno2 == args->blkno) && | |
1392 | (args->index2 <= args->index)) { | |
1393 | args->index2++; | |
1394 | } | |
1395 | } | |
a2ceac1f | 1396 | xfs_trans_log_buf(args->trans, bp, |
2bd0ea18 | 1397 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
5e656dbb BN |
1398 | ASSERT((args->index == 0) || |
1399 | (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval))); | |
a24374f4 | 1400 | ASSERT((args->index == ichdr->count - 1) || |
5e656dbb | 1401 | (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval))); |
2bd0ea18 NS |
1402 | |
1403 | /* | |
5000d01d | 1404 | * For "remote" attribute values, simply note that we need to |
2bd0ea18 NS |
1405 | * allocate space for the "remote" value. We can't actually |
1406 | * allocate the extents in this transaction, and we can't decide | |
1407 | * which blocks they should be as we might allocate more blocks | |
1408 | * as part of this transaction (a split operation for example). | |
1409 | */ | |
1410 | if (entry->flags & XFS_ATTR_LOCAL) { | |
a24374f4 | 1411 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
2bd0ea18 | 1412 | name_loc->namelen = args->namelen; |
5e656dbb | 1413 | name_loc->valuelen = cpu_to_be16(args->valuelen); |
32181a02 NS |
1414 | memcpy((char *)name_loc->nameval, args->name, args->namelen); |
1415 | memcpy((char *)&name_loc->nameval[args->namelen], args->value, | |
5e656dbb | 1416 | be16_to_cpu(name_loc->valuelen)); |
2bd0ea18 | 1417 | } else { |
a24374f4 | 1418 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
2bd0ea18 | 1419 | name_rmt->namelen = args->namelen; |
32181a02 | 1420 | memcpy((char *)name_rmt->name, args->name, args->namelen); |
2bd0ea18 NS |
1421 | entry->flags |= XFS_ATTR_INCOMPLETE; |
1422 | /* just in case */ | |
46eca962 NS |
1423 | name_rmt->valuelen = 0; |
1424 | name_rmt->valueblk = 0; | |
2bd0ea18 | 1425 | args->rmtblkno = 1; |
f08bc2a9 | 1426 | args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen); |
ff105f75 | 1427 | args->rmtvaluelen = args->valuelen; |
2bd0ea18 | 1428 | } |
a2ceac1f | 1429 | xfs_trans_log_buf(args->trans, bp, |
a24374f4 | 1430 | XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), |
2bd0ea18 NS |
1431 | xfs_attr_leaf_entsize(leaf, args->index))); |
1432 | ||
1433 | /* | |
1434 | * Update the control info for this leaf node | |
1435 | */ | |
a24374f4 DC |
1436 | if (be16_to_cpu(entry->nameidx) < ichdr->firstused) |
1437 | ichdr->firstused = be16_to_cpu(entry->nameidx); | |
1438 | ||
1439 | ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t) | |
1440 | + xfs_attr3_leaf_hdr_size(leaf)); | |
1441 | tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t) | |
1442 | + xfs_attr3_leaf_hdr_size(leaf); | |
1443 | ||
78d6585c | 1444 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
a24374f4 DC |
1445 | if (ichdr->freemap[i].base == tmp) { |
1446 | ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t); | |
1447 | ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t); | |
2bd0ea18 NS |
1448 | } |
1449 | } | |
a24374f4 DC |
1450 | ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index); |
1451 | return 0; | |
2bd0ea18 NS |
1452 | } |
1453 | ||
1454 | /* | |
1455 | * Garbage collect a leaf attribute list block by copying it to a new buffer. | |
1456 | */ | |
1457 | STATIC void | |
a24374f4 | 1458 | xfs_attr3_leaf_compact( |
a2ceac1f | 1459 | struct xfs_da_args *args, |
33765cd3 | 1460 | struct xfs_attr3_icleaf_hdr *ichdr_dst, |
a2ceac1f | 1461 | struct xfs_buf *bp) |
2bd0ea18 | 1462 | { |
33765cd3 DC |
1463 | struct xfs_attr_leafblock *leaf_src; |
1464 | struct xfs_attr_leafblock *leaf_dst; | |
1465 | struct xfs_attr3_icleaf_hdr ichdr_src; | |
a2ceac1f | 1466 | struct xfs_trans *trans = args->trans; |
a2ceac1f DC |
1467 | char *tmpbuffer; |
1468 | ||
1469 | trace_xfs_attr_leaf_compact(args); | |
2bd0ea18 | 1470 | |
6cd1e6db | 1471 | tmpbuffer = kmem_alloc(args->geo->blksize, 0); |
ff105f75 DC |
1472 | memcpy(tmpbuffer, bp->b_addr, args->geo->blksize); |
1473 | memset(bp->b_addr, 0, args->geo->blksize); | |
33765cd3 DC |
1474 | leaf_src = (xfs_attr_leafblock_t *)tmpbuffer; |
1475 | leaf_dst = bp->b_addr; | |
2bd0ea18 NS |
1476 | |
1477 | /* | |
33765cd3 DC |
1478 | * Copy the on-disk header back into the destination buffer to ensure |
1479 | * all the information in the header that is not part of the incore | |
1480 | * header structure is preserved. | |
2bd0ea18 | 1481 | */ |
33765cd3 DC |
1482 | memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src)); |
1483 | ||
1484 | /* Initialise the incore headers */ | |
1485 | ichdr_src = *ichdr_dst; /* struct copy */ | |
ff105f75 | 1486 | ichdr_dst->firstused = args->geo->blksize; |
33765cd3 DC |
1487 | ichdr_dst->usedbytes = 0; |
1488 | ichdr_dst->count = 0; | |
1489 | ichdr_dst->holes = 0; | |
1490 | ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src); | |
1491 | ichdr_dst->freemap[0].size = ichdr_dst->firstused - | |
1492 | ichdr_dst->freemap[0].base; | |
1493 | ||
33765cd3 | 1494 | /* write the header back to initialise the underlying buffer */ |
19ebedcf | 1495 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst); |
2bd0ea18 NS |
1496 | |
1497 | /* | |
1498 | * Copy all entry's in the same (sorted) order, | |
1499 | * but allocate name/value pairs packed and in sequence. | |
1500 | */ | |
ff105f75 DC |
1501 | xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0, |
1502 | leaf_dst, ichdr_dst, 0, ichdr_src.count); | |
a24374f4 DC |
1503 | /* |
1504 | * this logs the entire buffer, but the caller must write the header | |
1505 | * back to the buffer when it is finished modifying it. | |
1506 | */ | |
ff105f75 | 1507 | xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1); |
2bd0ea18 | 1508 | |
5e656dbb | 1509 | kmem_free(tmpbuffer); |
2bd0ea18 NS |
1510 | } |
1511 | ||
a24374f4 DC |
1512 | /* |
1513 | * Compare two leaf blocks "order". | |
1514 | * Return 0 unless leaf2 should go before leaf1. | |
1515 | */ | |
1516 | static int | |
1517 | xfs_attr3_leaf_order( | |
1518 | struct xfs_buf *leaf1_bp, | |
1519 | struct xfs_attr3_icleaf_hdr *leaf1hdr, | |
1520 | struct xfs_buf *leaf2_bp, | |
1521 | struct xfs_attr3_icleaf_hdr *leaf2hdr) | |
1522 | { | |
1523 | struct xfs_attr_leaf_entry *entries1; | |
1524 | struct xfs_attr_leaf_entry *entries2; | |
1525 | ||
1526 | entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr); | |
1527 | entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr); | |
1528 | if (leaf1hdr->count > 0 && leaf2hdr->count > 0 && | |
1529 | ((be32_to_cpu(entries2[0].hashval) < | |
1530 | be32_to_cpu(entries1[0].hashval)) || | |
1531 | (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) < | |
1532 | be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) { | |
1533 | return 1; | |
1534 | } | |
1535 | return 0; | |
1536 | } | |
1537 | ||
1538 | int | |
1539 | xfs_attr_leaf_order( | |
1540 | struct xfs_buf *leaf1_bp, | |
1541 | struct xfs_buf *leaf2_bp) | |
1542 | { | |
1543 | struct xfs_attr3_icleaf_hdr ichdr1; | |
1544 | struct xfs_attr3_icleaf_hdr ichdr2; | |
7861ef77 | 1545 | struct xfs_mount *mp = leaf1_bp->b_mount; |
a24374f4 | 1546 | |
19ebedcf DC |
1547 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr); |
1548 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr); | |
a24374f4 DC |
1549 | return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2); |
1550 | } | |
1551 | ||
2bd0ea18 NS |
1552 | /* |
1553 | * Redistribute the attribute list entries between two leaf nodes, | |
1554 | * taking into account the size of the new entry. | |
1555 | * | |
1556 | * NOTE: if new block is empty, then it will get the upper half of the | |
1557 | * old block. At present, all (one) callers pass in an empty second block. | |
1558 | * | |
1559 | * This code adjusts the args->index/blkno and args->index2/blkno2 fields | |
1560 | * to match what it is doing in splitting the attribute leaf block. Those | |
dfc130f3 | 1561 | * values are used in "atomic rename" operations on attributes. Note that |
2bd0ea18 NS |
1562 | * the "new" and "old" values can end up in different blocks. |
1563 | */ | |
1564 | STATIC void | |
a24374f4 DC |
1565 | xfs_attr3_leaf_rebalance( |
1566 | struct xfs_da_state *state, | |
1567 | struct xfs_da_state_blk *blk1, | |
1568 | struct xfs_da_state_blk *blk2) | |
2bd0ea18 | 1569 | { |
a24374f4 DC |
1570 | struct xfs_da_args *args; |
1571 | struct xfs_attr_leafblock *leaf1; | |
1572 | struct xfs_attr_leafblock *leaf2; | |
1573 | struct xfs_attr3_icleaf_hdr ichdr1; | |
1574 | struct xfs_attr3_icleaf_hdr ichdr2; | |
1575 | struct xfs_attr_leaf_entry *entries1; | |
1576 | struct xfs_attr_leaf_entry *entries2; | |
1577 | int count; | |
1578 | int totallen; | |
1579 | int max; | |
1580 | int space; | |
1581 | int swap; | |
2bd0ea18 NS |
1582 | |
1583 | /* | |
1584 | * Set up environment. | |
1585 | */ | |
1586 | ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC); | |
1587 | ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC); | |
a2ceac1f DC |
1588 | leaf1 = blk1->bp->b_addr; |
1589 | leaf2 = blk2->bp->b_addr; | |
19ebedcf DC |
1590 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1); |
1591 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2); | |
a24374f4 | 1592 | ASSERT(ichdr2.count == 0); |
2bd0ea18 NS |
1593 | args = state->args; |
1594 | ||
a2ceac1f DC |
1595 | trace_xfs_attr_leaf_rebalance(args); |
1596 | ||
2bd0ea18 NS |
1597 | /* |
1598 | * Check ordering of blocks, reverse if it makes things simpler. | |
1599 | * | |
1600 | * NOTE: Given that all (current) callers pass in an empty | |
1601 | * second block, this code should never set "swap". | |
1602 | */ | |
1603 | swap = 0; | |
a24374f4 | 1604 | if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) { |
50c8d191 | 1605 | swap(blk1, blk2); |
a24374f4 | 1606 | |
50c8d191 GS |
1607 | /* swap structures rather than reconverting them */ |
1608 | swap(ichdr1, ichdr2); | |
a24374f4 | 1609 | |
a2ceac1f DC |
1610 | leaf1 = blk1->bp->b_addr; |
1611 | leaf2 = blk2->bp->b_addr; | |
2bd0ea18 NS |
1612 | swap = 1; |
1613 | } | |
2bd0ea18 NS |
1614 | |
1615 | /* | |
1616 | * Examine entries until we reduce the absolute difference in | |
1617 | * byte usage between the two blocks to a minimum. Then get | |
1618 | * the direction to copy and the number of elements to move. | |
1619 | * | |
1620 | * "inleaf" is true if the new entry should be inserted into blk1. | |
1621 | * If "swap" is also true, then reverse the sense of "inleaf". | |
1622 | */ | |
a24374f4 DC |
1623 | state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1, |
1624 | blk2, &ichdr2, | |
1625 | &count, &totallen); | |
2bd0ea18 NS |
1626 | if (swap) |
1627 | state->inleaf = !state->inleaf; | |
1628 | ||
1629 | /* | |
1630 | * Move any entries required from leaf to leaf: | |
1631 | */ | |
a24374f4 | 1632 | if (count < ichdr1.count) { |
2bd0ea18 NS |
1633 | /* |
1634 | * Figure the total bytes to be added to the destination leaf. | |
1635 | */ | |
1636 | /* number entries being moved */ | |
a24374f4 DC |
1637 | count = ichdr1.count - count; |
1638 | space = ichdr1.usedbytes - totallen; | |
2bd0ea18 NS |
1639 | space += count * sizeof(xfs_attr_leaf_entry_t); |
1640 | ||
1641 | /* | |
1642 | * leaf2 is the destination, compact it if it looks tight. | |
1643 | */ | |
a24374f4 DC |
1644 | max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1); |
1645 | max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t); | |
a2ceac1f | 1646 | if (space > max) |
a24374f4 | 1647 | xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp); |
2bd0ea18 NS |
1648 | |
1649 | /* | |
1650 | * Move high entries from leaf1 to low end of leaf2. | |
1651 | */ | |
ff105f75 DC |
1652 | xfs_attr3_leaf_moveents(args, leaf1, &ichdr1, |
1653 | ichdr1.count - count, leaf2, &ichdr2, 0, count); | |
2bd0ea18 | 1654 | |
a24374f4 | 1655 | } else if (count > ichdr1.count) { |
2bd0ea18 NS |
1656 | /* |
1657 | * I assert that since all callers pass in an empty | |
1658 | * second buffer, this code should never execute. | |
1659 | */ | |
a2ceac1f | 1660 | ASSERT(0); |
2bd0ea18 NS |
1661 | |
1662 | /* | |
1663 | * Figure the total bytes to be added to the destination leaf. | |
1664 | */ | |
1665 | /* number entries being moved */ | |
a24374f4 DC |
1666 | count -= ichdr1.count; |
1667 | space = totallen - ichdr1.usedbytes; | |
2bd0ea18 NS |
1668 | space += count * sizeof(xfs_attr_leaf_entry_t); |
1669 | ||
1670 | /* | |
1671 | * leaf1 is the destination, compact it if it looks tight. | |
1672 | */ | |
a24374f4 DC |
1673 | max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1); |
1674 | max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t); | |
a2ceac1f | 1675 | if (space > max) |
a24374f4 | 1676 | xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp); |
2bd0ea18 NS |
1677 | |
1678 | /* | |
1679 | * Move low entries from leaf2 to high end of leaf1. | |
1680 | */ | |
ff105f75 DC |
1681 | xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1, |
1682 | ichdr1.count, count); | |
2bd0ea18 NS |
1683 | } |
1684 | ||
19ebedcf DC |
1685 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1); |
1686 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2); | |
ff105f75 DC |
1687 | xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1); |
1688 | xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1); | |
a24374f4 | 1689 | |
2bd0ea18 NS |
1690 | /* |
1691 | * Copy out last hashval in each block for B-tree code. | |
1692 | */ | |
a24374f4 DC |
1693 | entries1 = xfs_attr3_leaf_entryp(leaf1); |
1694 | entries2 = xfs_attr3_leaf_entryp(leaf2); | |
1695 | blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval); | |
1696 | blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval); | |
2bd0ea18 NS |
1697 | |
1698 | /* | |
1699 | * Adjust the expected index for insertion. | |
1700 | * NOTE: this code depends on the (current) situation that the | |
1701 | * second block was originally empty. | |
1702 | * | |
1703 | * If the insertion point moved to the 2nd block, we must adjust | |
1704 | * the index. We must also track the entry just following the | |
1705 | * new entry for use in an "atomic rename" operation, that entry | |
1706 | * is always the "old" entry and the "new" entry is what we are | |
1707 | * inserting. The index/blkno fields refer to the "old" entry, | |
1708 | * while the index2/blkno2 fields refer to the "new" entry. | |
1709 | */ | |
a24374f4 | 1710 | if (blk1->index > ichdr1.count) { |
2bd0ea18 | 1711 | ASSERT(state->inleaf == 0); |
a24374f4 | 1712 | blk2->index = blk1->index - ichdr1.count; |
2bd0ea18 NS |
1713 | args->index = args->index2 = blk2->index; |
1714 | args->blkno = args->blkno2 = blk2->blkno; | |
a24374f4 | 1715 | } else if (blk1->index == ichdr1.count) { |
2bd0ea18 NS |
1716 | if (state->inleaf) { |
1717 | args->index = blk1->index; | |
1718 | args->blkno = blk1->blkno; | |
1719 | args->index2 = 0; | |
1720 | args->blkno2 = blk2->blkno; | |
1721 | } else { | |
a2ceac1f DC |
1722 | /* |
1723 | * On a double leaf split, the original attr location | |
1724 | * is already stored in blkno2/index2, so don't | |
1725 | * overwrite it overwise we corrupt the tree. | |
1726 | */ | |
a24374f4 | 1727 | blk2->index = blk1->index - ichdr1.count; |
a2ceac1f DC |
1728 | args->index = blk2->index; |
1729 | args->blkno = blk2->blkno; | |
1730 | if (!state->extravalid) { | |
1731 | /* | |
1732 | * set the new attr location to match the old | |
1733 | * one and let the higher level split code | |
1734 | * decide where in the leaf to place it. | |
1735 | */ | |
1736 | args->index2 = blk2->index; | |
1737 | args->blkno2 = blk2->blkno; | |
1738 | } | |
2bd0ea18 NS |
1739 | } |
1740 | } else { | |
1741 | ASSERT(state->inleaf == 1); | |
1742 | args->index = args->index2 = blk1->index; | |
1743 | args->blkno = args->blkno2 = blk1->blkno; | |
1744 | } | |
1745 | } | |
1746 | ||
1747 | /* | |
1748 | * Examine entries until we reduce the absolute difference in | |
1749 | * byte usage between the two blocks to a minimum. | |
1750 | * GROT: Is this really necessary? With other than a 512 byte blocksize, | |
1751 | * GROT: there will always be enough room in either block for a new entry. | |
1752 | * GROT: Do a double-split for this case? | |
1753 | */ | |
1754 | STATIC int | |
a24374f4 DC |
1755 | xfs_attr3_leaf_figure_balance( |
1756 | struct xfs_da_state *state, | |
1757 | struct xfs_da_state_blk *blk1, | |
1758 | struct xfs_attr3_icleaf_hdr *ichdr1, | |
1759 | struct xfs_da_state_blk *blk2, | |
1760 | struct xfs_attr3_icleaf_hdr *ichdr2, | |
1761 | int *countarg, | |
1762 | int *usedbytesarg) | |
2bd0ea18 | 1763 | { |
a24374f4 DC |
1764 | struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr; |
1765 | struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr; | |
1766 | struct xfs_attr_leaf_entry *entry; | |
1767 | int count; | |
1768 | int max; | |
1769 | int index; | |
1770 | int totallen = 0; | |
1771 | int half; | |
1772 | int lastdelta; | |
1773 | int foundit = 0; | |
1774 | int tmp; | |
2bd0ea18 NS |
1775 | |
1776 | /* | |
1777 | * Examine entries until we reduce the absolute difference in | |
1778 | * byte usage between the two blocks to a minimum. | |
1779 | */ | |
a24374f4 DC |
1780 | max = ichdr1->count + ichdr2->count; |
1781 | half = (max + 1) * sizeof(*entry); | |
1782 | half += ichdr1->usedbytes + ichdr2->usedbytes + | |
ff105f75 | 1783 | xfs_attr_leaf_newentsize(state->args, NULL); |
2bd0ea18 | 1784 | half /= 2; |
ff105f75 | 1785 | lastdelta = state->args->geo->blksize; |
a24374f4 | 1786 | entry = xfs_attr3_leaf_entryp(leaf1); |
2bd0ea18 NS |
1787 | for (count = index = 0; count < max; entry++, index++, count++) { |
1788 | ||
dfc130f3 | 1789 | #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A)) |
2bd0ea18 NS |
1790 | /* |
1791 | * The new entry is in the first block, account for it. | |
1792 | */ | |
1793 | if (count == blk1->index) { | |
1794 | tmp = totallen + sizeof(*entry) + | |
ff105f75 | 1795 | xfs_attr_leaf_newentsize(state->args, NULL); |
2bd0ea18 NS |
1796 | if (XFS_ATTR_ABS(half - tmp) > lastdelta) |
1797 | break; | |
1798 | lastdelta = XFS_ATTR_ABS(half - tmp); | |
1799 | totallen = tmp; | |
1800 | foundit = 1; | |
1801 | } | |
1802 | ||
1803 | /* | |
1804 | * Wrap around into the second block if necessary. | |
1805 | */ | |
a24374f4 | 1806 | if (count == ichdr1->count) { |
2bd0ea18 | 1807 | leaf1 = leaf2; |
a24374f4 | 1808 | entry = xfs_attr3_leaf_entryp(leaf1); |
2bd0ea18 NS |
1809 | index = 0; |
1810 | } | |
1811 | ||
1812 | /* | |
1813 | * Figure out if next leaf entry would be too much. | |
1814 | */ | |
1815 | tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1, | |
1816 | index); | |
1817 | if (XFS_ATTR_ABS(half - tmp) > lastdelta) | |
1818 | break; | |
1819 | lastdelta = XFS_ATTR_ABS(half - tmp); | |
1820 | totallen = tmp; | |
1821 | #undef XFS_ATTR_ABS | |
1822 | } | |
1823 | ||
1824 | /* | |
1825 | * Calculate the number of usedbytes that will end up in lower block. | |
1826 | * If new entry not in lower block, fix up the count. | |
1827 | */ | |
1828 | totallen -= count * sizeof(*entry); | |
1829 | if (foundit) { | |
5000d01d | 1830 | totallen -= sizeof(*entry) + |
ff105f75 | 1831 | xfs_attr_leaf_newentsize(state->args, NULL); |
2bd0ea18 NS |
1832 | } |
1833 | ||
1834 | *countarg = count; | |
1835 | *usedbytesarg = totallen; | |
a24374f4 | 1836 | return foundit; |
2bd0ea18 NS |
1837 | } |
1838 | ||
1839 | /*======================================================================== | |
1840 | * Routines used for shrinking the Btree. | |
1841 | *========================================================================*/ | |
1842 | ||
1843 | /* | |
1844 | * Check a leaf block and its neighbors to see if the block should be | |
1845 | * collapsed into one or the other neighbor. Always keep the block | |
1846 | * with the smaller block number. | |
1847 | * If the current block is over 50% full, don't try to join it, return 0. | |
1848 | * If the block is empty, fill in the state structure and return 2. | |
1849 | * If it can be collapsed, fill in the state structure and return 1. | |
1850 | * If nothing can be done, return 0. | |
1851 | * | |
1852 | * GROT: allow for INCOMPLETE entries in calculation. | |
1853 | */ | |
1854 | int | |
a24374f4 DC |
1855 | xfs_attr3_leaf_toosmall( |
1856 | struct xfs_da_state *state, | |
1857 | int *action) | |
2bd0ea18 | 1858 | { |
a24374f4 DC |
1859 | struct xfs_attr_leafblock *leaf; |
1860 | struct xfs_da_state_blk *blk; | |
1861 | struct xfs_attr3_icleaf_hdr ichdr; | |
1862 | struct xfs_buf *bp; | |
1863 | xfs_dablk_t blkno; | |
1864 | int bytes; | |
1865 | int forward; | |
1866 | int error; | |
1867 | int retval; | |
1868 | int i; | |
a2ceac1f DC |
1869 | |
1870 | trace_xfs_attr_leaf_toosmall(state->args); | |
2bd0ea18 NS |
1871 | |
1872 | /* | |
1873 | * Check for the degenerate case of the block being over 50% full. | |
1874 | * If so, it's not worth even looking to see if we might be able | |
1875 | * to coalesce with a sibling. | |
1876 | */ | |
1877 | blk = &state->path.blk[ state->path.active-1 ]; | |
a24374f4 | 1878 | leaf = blk->bp->b_addr; |
19ebedcf | 1879 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf); |
a24374f4 DC |
1880 | bytes = xfs_attr3_leaf_hdr_size(leaf) + |
1881 | ichdr.count * sizeof(xfs_attr_leaf_entry_t) + | |
1882 | ichdr.usedbytes; | |
ff105f75 | 1883 | if (bytes > (state->args->geo->blksize >> 1)) { |
4ca431fc | 1884 | *action = 0; /* blk over 50%, don't try to join */ |
af43ca9f | 1885 | return 0; |
2bd0ea18 NS |
1886 | } |
1887 | ||
1888 | /* | |
1889 | * Check for the degenerate case of the block being empty. | |
1890 | * If the block is empty, we'll simply delete it, no need to | |
5e656dbb | 1891 | * coalesce it with a sibling block. We choose (arbitrarily) |
2bd0ea18 NS |
1892 | * to merge with the forward block unless it is NULL. |
1893 | */ | |
a24374f4 | 1894 | if (ichdr.count == 0) { |
2bd0ea18 NS |
1895 | /* |
1896 | * Make altpath point to the block we want to keep and | |
1897 | * path point to the block we want to drop (this one). | |
1898 | */ | |
a24374f4 | 1899 | forward = (ichdr.forw != 0); |
32181a02 | 1900 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
88b32f06 | 1901 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
2bd0ea18 NS |
1902 | 0, &retval); |
1903 | if (error) | |
af43ca9f | 1904 | return error; |
2bd0ea18 NS |
1905 | if (retval) { |
1906 | *action = 0; | |
1907 | } else { | |
1908 | *action = 2; | |
1909 | } | |
a24374f4 | 1910 | return 0; |
2bd0ea18 NS |
1911 | } |
1912 | ||
1913 | /* | |
1914 | * Examine each sibling block to see if we can coalesce with | |
1915 | * at least 25% free space to spare. We need to figure out | |
1916 | * whether to merge with the forward or the backward block. | |
1917 | * We prefer coalescing with the lower numbered sibling so as | |
1918 | * to shrink an attribute list over time. | |
1919 | */ | |
1920 | /* start with smaller blk num */ | |
a24374f4 | 1921 | forward = ichdr.forw < ichdr.back; |
2bd0ea18 | 1922 | for (i = 0; i < 2; forward = !forward, i++) { |
a24374f4 | 1923 | struct xfs_attr3_icleaf_hdr ichdr2; |
2bd0ea18 | 1924 | if (forward) |
a24374f4 | 1925 | blkno = ichdr.forw; |
2bd0ea18 | 1926 | else |
a24374f4 | 1927 | blkno = ichdr.back; |
2bd0ea18 NS |
1928 | if (blkno == 0) |
1929 | continue; | |
a24374f4 | 1930 | error = xfs_attr3_leaf_read(state->args->trans, state->args->dp, |
a2ceac1f | 1931 | blkno, -1, &bp); |
2bd0ea18 | 1932 | if (error) |
af43ca9f | 1933 | return error; |
2bd0ea18 | 1934 | |
19ebedcf | 1935 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr); |
a24374f4 | 1936 | |
ff105f75 DC |
1937 | bytes = state->args->geo->blksize - |
1938 | (state->args->geo->blksize >> 2) - | |
a24374f4 DC |
1939 | ichdr.usedbytes - ichdr2.usedbytes - |
1940 | ((ichdr.count + ichdr2.count) * | |
1941 | sizeof(xfs_attr_leaf_entry_t)) - | |
1942 | xfs_attr3_leaf_hdr_size(leaf); | |
1943 | ||
a2ceac1f | 1944 | xfs_trans_brelse(state->args->trans, bp); |
2bd0ea18 NS |
1945 | if (bytes >= 0) |
1946 | break; /* fits with at least 25% to spare */ | |
1947 | } | |
1948 | if (i >= 2) { | |
1949 | *action = 0; | |
af43ca9f | 1950 | return 0; |
2bd0ea18 NS |
1951 | } |
1952 | ||
1953 | /* | |
1954 | * Make altpath point to the block we want to keep (the lower | |
1955 | * numbered block) and path point to the block we want to drop. | |
1956 | */ | |
32181a02 | 1957 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
2bd0ea18 | 1958 | if (blkno < blk->blkno) { |
88b32f06 | 1959 | error = xfs_da3_path_shift(state, &state->altpath, forward, |
2bd0ea18 NS |
1960 | 0, &retval); |
1961 | } else { | |
88b32f06 | 1962 | error = xfs_da3_path_shift(state, &state->path, forward, |
2bd0ea18 NS |
1963 | 0, &retval); |
1964 | } | |
1965 | if (error) | |
af43ca9f | 1966 | return error; |
2bd0ea18 NS |
1967 | if (retval) { |
1968 | *action = 0; | |
1969 | } else { | |
1970 | *action = 1; | |
1971 | } | |
af43ca9f | 1972 | return 0; |
2bd0ea18 NS |
1973 | } |
1974 | ||
57c9fccb NS |
1975 | /* |
1976 | * Remove a name from the leaf attribute list structure. | |
1977 | * | |
1978 | * Return 1 if leaf is less than 37% full, 0 if >= 37% full. | |
1979 | * If two leaves are 37% full, when combined they will leave 25% free. | |
1980 | */ | |
1981 | int | |
a24374f4 DC |
1982 | xfs_attr3_leaf_remove( |
1983 | struct xfs_buf *bp, | |
1984 | struct xfs_da_args *args) | |
57c9fccb | 1985 | { |
a24374f4 DC |
1986 | struct xfs_attr_leafblock *leaf; |
1987 | struct xfs_attr3_icleaf_hdr ichdr; | |
1988 | struct xfs_attr_leaf_entry *entry; | |
a24374f4 DC |
1989 | int before; |
1990 | int after; | |
1991 | int smallest; | |
1992 | int entsize; | |
1993 | int tablesize; | |
1994 | int tmp; | |
1995 | int i; | |
57c9fccb | 1996 | |
a2ceac1f DC |
1997 | trace_xfs_attr_leaf_remove(args); |
1998 | ||
1999 | leaf = bp->b_addr; | |
19ebedcf | 2000 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
a24374f4 | 2001 | |
ff105f75 | 2002 | ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8); |
a24374f4 DC |
2003 | ASSERT(args->index >= 0 && args->index < ichdr.count); |
2004 | ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) + | |
2005 | xfs_attr3_leaf_hdr_size(leaf)); | |
2006 | ||
2007 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; | |
2008 | ||
2009 | ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); | |
ff105f75 | 2010 | ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize); |
57c9fccb NS |
2011 | |
2012 | /* | |
2013 | * Scan through free region table: | |
2014 | * check for adjacency of free'd entry with an existing one, | |
2015 | * find smallest free region in case we need to replace it, | |
2016 | * adjust any map that borders the entry table, | |
2017 | */ | |
a24374f4 DC |
2018 | tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t) |
2019 | + xfs_attr3_leaf_hdr_size(leaf); | |
2020 | tmp = ichdr.freemap[0].size; | |
57c9fccb NS |
2021 | before = after = -1; |
2022 | smallest = XFS_ATTR_LEAF_MAPSIZE - 1; | |
2023 | entsize = xfs_attr_leaf_entsize(leaf, args->index); | |
a24374f4 | 2024 | for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
ff105f75 DC |
2025 | ASSERT(ichdr.freemap[i].base < args->geo->blksize); |
2026 | ASSERT(ichdr.freemap[i].size < args->geo->blksize); | |
a24374f4 DC |
2027 | if (ichdr.freemap[i].base == tablesize) { |
2028 | ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t); | |
2029 | ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t); | |
57c9fccb NS |
2030 | } |
2031 | ||
a24374f4 DC |
2032 | if (ichdr.freemap[i].base + ichdr.freemap[i].size == |
2033 | be16_to_cpu(entry->nameidx)) { | |
57c9fccb | 2034 | before = i; |
a24374f4 DC |
2035 | } else if (ichdr.freemap[i].base == |
2036 | (be16_to_cpu(entry->nameidx) + entsize)) { | |
57c9fccb | 2037 | after = i; |
a24374f4 DC |
2038 | } else if (ichdr.freemap[i].size < tmp) { |
2039 | tmp = ichdr.freemap[i].size; | |
57c9fccb NS |
2040 | smallest = i; |
2041 | } | |
2042 | } | |
2043 | ||
2044 | /* | |
2045 | * Coalesce adjacent freemap regions, | |
2046 | * or replace the smallest region. | |
2047 | */ | |
2048 | if ((before >= 0) || (after >= 0)) { | |
2049 | if ((before >= 0) && (after >= 0)) { | |
a24374f4 DC |
2050 | ichdr.freemap[before].size += entsize; |
2051 | ichdr.freemap[before].size += ichdr.freemap[after].size; | |
2052 | ichdr.freemap[after].base = 0; | |
2053 | ichdr.freemap[after].size = 0; | |
57c9fccb | 2054 | } else if (before >= 0) { |
a24374f4 | 2055 | ichdr.freemap[before].size += entsize; |
57c9fccb | 2056 | } else { |
a24374f4 DC |
2057 | ichdr.freemap[after].base = be16_to_cpu(entry->nameidx); |
2058 | ichdr.freemap[after].size += entsize; | |
57c9fccb NS |
2059 | } |
2060 | } else { | |
2061 | /* | |
2062 | * Replace smallest region (if it is smaller than free'd entry) | |
2063 | */ | |
a24374f4 DC |
2064 | if (ichdr.freemap[smallest].size < entsize) { |
2065 | ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx); | |
2066 | ichdr.freemap[smallest].size = entsize; | |
57c9fccb NS |
2067 | } |
2068 | } | |
2069 | ||
2070 | /* | |
2071 | * Did we remove the first entry? | |
2072 | */ | |
a24374f4 | 2073 | if (be16_to_cpu(entry->nameidx) == ichdr.firstused) |
57c9fccb NS |
2074 | smallest = 1; |
2075 | else | |
2076 | smallest = 0; | |
2077 | ||
2078 | /* | |
2079 | * Compress the remaining entries and zero out the removed stuff. | |
2080 | */ | |
a24374f4 DC |
2081 | memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize); |
2082 | ichdr.usedbytes -= entsize; | |
a2ceac1f | 2083 | xfs_trans_log_buf(args->trans, bp, |
a24374f4 | 2084 | XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), |
57c9fccb NS |
2085 | entsize)); |
2086 | ||
a24374f4 DC |
2087 | tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t); |
2088 | memmove(entry, entry + 1, tmp); | |
2089 | ichdr.count--; | |
a2ceac1f | 2090 | xfs_trans_log_buf(args->trans, bp, |
a24374f4 DC |
2091 | XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t))); |
2092 | ||
2093 | entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count]; | |
2094 | memset(entry, 0, sizeof(xfs_attr_leaf_entry_t)); | |
57c9fccb NS |
2095 | |
2096 | /* | |
2097 | * If we removed the first entry, re-find the first used byte | |
2098 | * in the name area. Note that if the entry was the "firstused", | |
2099 | * then we don't have a "hole" in our block resulting from | |
2100 | * removing the name. | |
2101 | */ | |
2102 | if (smallest) { | |
ff105f75 | 2103 | tmp = args->geo->blksize; |
a24374f4 DC |
2104 | entry = xfs_attr3_leaf_entryp(leaf); |
2105 | for (i = ichdr.count - 1; i >= 0; entry++, i--) { | |
2106 | ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused); | |
ff105f75 | 2107 | ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize); |
5e656dbb BN |
2108 | |
2109 | if (be16_to_cpu(entry->nameidx) < tmp) | |
2110 | tmp = be16_to_cpu(entry->nameidx); | |
57c9fccb | 2111 | } |
a24374f4 | 2112 | ichdr.firstused = tmp; |
19ebedcf | 2113 | ASSERT(ichdr.firstused != 0); |
57c9fccb | 2114 | } else { |
a24374f4 | 2115 | ichdr.holes = 1; /* mark as needing compaction */ |
57c9fccb | 2116 | } |
19ebedcf | 2117 | xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr); |
a2ceac1f | 2118 | xfs_trans_log_buf(args->trans, bp, |
a24374f4 DC |
2119 | XFS_DA_LOGRANGE(leaf, &leaf->hdr, |
2120 | xfs_attr3_leaf_hdr_size(leaf))); | |
57c9fccb NS |
2121 | |
2122 | /* | |
2123 | * Check if leaf is less than 50% full, caller may want to | |
2124 | * "join" the leaf with a sibling if so. | |
2125 | */ | |
a24374f4 DC |
2126 | tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) + |
2127 | ichdr.count * sizeof(xfs_attr_leaf_entry_t); | |
2128 | ||
ff105f75 | 2129 | return tmp < args->geo->magicpct; /* leaf is < 37% full */ |
57c9fccb NS |
2130 | } |
2131 | ||
2bd0ea18 NS |
2132 | /* |
2133 | * Move all the attribute list entries from drop_leaf into save_leaf. | |
2134 | */ | |
2135 | void | |
a24374f4 DC |
2136 | xfs_attr3_leaf_unbalance( |
2137 | struct xfs_da_state *state, | |
2138 | struct xfs_da_state_blk *drop_blk, | |
2139 | struct xfs_da_state_blk *save_blk) | |
2bd0ea18 | 2140 | { |
a24374f4 DC |
2141 | struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr; |
2142 | struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr; | |
2143 | struct xfs_attr3_icleaf_hdr drophdr; | |
2144 | struct xfs_attr3_icleaf_hdr savehdr; | |
2145 | struct xfs_attr_leaf_entry *entry; | |
2bd0ea18 | 2146 | |
a2ceac1f DC |
2147 | trace_xfs_attr_leaf_unbalance(state->args); |
2148 | ||
a2ceac1f DC |
2149 | drop_leaf = drop_blk->bp->b_addr; |
2150 | save_leaf = save_blk->bp->b_addr; | |
19ebedcf DC |
2151 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf); |
2152 | xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf); | |
a24374f4 | 2153 | entry = xfs_attr3_leaf_entryp(drop_leaf); |
2bd0ea18 NS |
2154 | |
2155 | /* | |
2156 | * Save last hashval from dying block for later Btree fixup. | |
2157 | */ | |
a24374f4 | 2158 | drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval); |
2bd0ea18 NS |
2159 | |
2160 | /* | |
2161 | * Check if we need a temp buffer, or can we do it in place. | |
2162 | * Note that we don't check "leaf" for holes because we will | |
2163 | * always be dropping it, toosmall() decided that for us already. | |
2164 | */ | |
a24374f4 | 2165 | if (savehdr.holes == 0) { |
2bd0ea18 NS |
2166 | /* |
2167 | * dest leaf has no holes, so we add there. May need | |
2168 | * to make some room in the entry array. | |
2169 | */ | |
a24374f4 DC |
2170 | if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, |
2171 | drop_blk->bp, &drophdr)) { | |
ff105f75 DC |
2172 | xfs_attr3_leaf_moveents(state->args, |
2173 | drop_leaf, &drophdr, 0, | |
a24374f4 | 2174 | save_leaf, &savehdr, 0, |
ff105f75 | 2175 | drophdr.count); |
2bd0ea18 | 2176 | } else { |
ff105f75 DC |
2177 | xfs_attr3_leaf_moveents(state->args, |
2178 | drop_leaf, &drophdr, 0, | |
a24374f4 | 2179 | save_leaf, &savehdr, |
ff105f75 | 2180 | savehdr.count, drophdr.count); |
2bd0ea18 NS |
2181 | } |
2182 | } else { | |
2183 | /* | |
2184 | * Destination has holes, so we make a temporary copy | |
2185 | * of the leaf and add them both to that. | |
2186 | */ | |
a24374f4 DC |
2187 | struct xfs_attr_leafblock *tmp_leaf; |
2188 | struct xfs_attr3_icleaf_hdr tmphdr; | |
2189 | ||
6cd1e6db | 2190 | tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0); |
fa74cedd DC |
2191 | |
2192 | /* | |
2193 | * Copy the header into the temp leaf so that all the stuff | |
2194 | * not in the incore header is present and gets copied back in | |
2195 | * once we've moved all the entries. | |
2196 | */ | |
2197 | memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf)); | |
a24374f4 | 2198 | |
fa74cedd | 2199 | memset(&tmphdr, 0, sizeof(tmphdr)); |
a24374f4 DC |
2200 | tmphdr.magic = savehdr.magic; |
2201 | tmphdr.forw = savehdr.forw; | |
2202 | tmphdr.back = savehdr.back; | |
ff105f75 | 2203 | tmphdr.firstused = state->args->geo->blksize; |
fa74cedd DC |
2204 | |
2205 | /* write the header to the temp buffer to initialise it */ | |
19ebedcf | 2206 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr); |
fa74cedd | 2207 | |
a24374f4 DC |
2208 | if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, |
2209 | drop_blk->bp, &drophdr)) { | |
ff105f75 DC |
2210 | xfs_attr3_leaf_moveents(state->args, |
2211 | drop_leaf, &drophdr, 0, | |
a24374f4 | 2212 | tmp_leaf, &tmphdr, 0, |
ff105f75 DC |
2213 | drophdr.count); |
2214 | xfs_attr3_leaf_moveents(state->args, | |
2215 | save_leaf, &savehdr, 0, | |
a24374f4 | 2216 | tmp_leaf, &tmphdr, tmphdr.count, |
ff105f75 | 2217 | savehdr.count); |
2bd0ea18 | 2218 | } else { |
ff105f75 DC |
2219 | xfs_attr3_leaf_moveents(state->args, |
2220 | save_leaf, &savehdr, 0, | |
a24374f4 | 2221 | tmp_leaf, &tmphdr, 0, |
ff105f75 DC |
2222 | savehdr.count); |
2223 | xfs_attr3_leaf_moveents(state->args, | |
2224 | drop_leaf, &drophdr, 0, | |
a24374f4 | 2225 | tmp_leaf, &tmphdr, tmphdr.count, |
ff105f75 | 2226 | drophdr.count); |
2bd0ea18 | 2227 | } |
ff105f75 | 2228 | memcpy(save_leaf, tmp_leaf, state->args->geo->blksize); |
a24374f4 DC |
2229 | savehdr = tmphdr; /* struct copy */ |
2230 | kmem_free(tmp_leaf); | |
2bd0ea18 NS |
2231 | } |
2232 | ||
19ebedcf | 2233 | xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr); |
a2ceac1f | 2234 | xfs_trans_log_buf(state->args->trans, save_blk->bp, 0, |
ff105f75 | 2235 | state->args->geo->blksize - 1); |
2bd0ea18 NS |
2236 | |
2237 | /* | |
2238 | * Copy out last hashval in each block for B-tree code. | |
2239 | */ | |
a24374f4 DC |
2240 | entry = xfs_attr3_leaf_entryp(save_leaf); |
2241 | save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval); | |
2bd0ea18 NS |
2242 | } |
2243 | ||
57c9fccb NS |
2244 | /*======================================================================== |
2245 | * Routines used for finding things in the Btree. | |
2246 | *========================================================================*/ | |
2247 | ||
2248 | /* | |
2249 | * Look up a name in a leaf attribute list structure. | |
2250 | * This is the internal routine, it uses the caller's buffer. | |
2251 | * | |
2252 | * Note that duplicate keys are allowed, but only check within the | |
2253 | * current leaf node. The Btree code must check in adjacent leaf nodes. | |
2254 | * | |
2255 | * Return in args->index the index into the entry[] array of either | |
2256 | * the found entry, or where the entry should have been (insert before | |
2257 | * that entry). | |
2258 | * | |
2259 | * Don't change the args->value unless we find the attribute. | |
2260 | */ | |
2261 | int | |
a24374f4 DC |
2262 | xfs_attr3_leaf_lookup_int( |
2263 | struct xfs_buf *bp, | |
2264 | struct xfs_da_args *args) | |
57c9fccb | 2265 | { |
a24374f4 DC |
2266 | struct xfs_attr_leafblock *leaf; |
2267 | struct xfs_attr3_icleaf_hdr ichdr; | |
2268 | struct xfs_attr_leaf_entry *entry; | |
2269 | struct xfs_attr_leaf_entry *entries; | |
2270 | struct xfs_attr_leaf_name_local *name_loc; | |
2271 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2272 | xfs_dahash_t hashval; | |
2273 | int probe; | |
2274 | int span; | |
57c9fccb | 2275 | |
a2ceac1f DC |
2276 | trace_xfs_attr_leaf_lookup(args); |
2277 | ||
2278 | leaf = bp->b_addr; | |
19ebedcf | 2279 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
a24374f4 | 2280 | entries = xfs_attr3_leaf_entryp(leaf); |
e9998c63 DW |
2281 | if (ichdr.count >= args->geo->blksize / 8) |
2282 | return -EFSCORRUPTED; | |
57c9fccb NS |
2283 | |
2284 | /* | |
2285 | * Binary search. (note: small blocks will skip this loop) | |
2286 | */ | |
2287 | hashval = args->hashval; | |
a24374f4 DC |
2288 | probe = span = ichdr.count / 2; |
2289 | for (entry = &entries[probe]; span > 4; entry = &entries[probe]) { | |
57c9fccb | 2290 | span /= 2; |
5e656dbb | 2291 | if (be32_to_cpu(entry->hashval) < hashval) |
57c9fccb | 2292 | probe += span; |
5e656dbb | 2293 | else if (be32_to_cpu(entry->hashval) > hashval) |
57c9fccb NS |
2294 | probe -= span; |
2295 | else | |
2296 | break; | |
2297 | } | |
e9998c63 DW |
2298 | if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count))) |
2299 | return -EFSCORRUPTED; | |
2300 | if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval)) | |
2301 | return -EFSCORRUPTED; | |
57c9fccb NS |
2302 | |
2303 | /* | |
2304 | * Since we may have duplicate hashval's, find the first matching | |
2305 | * hashval in the leaf. | |
2306 | */ | |
a24374f4 | 2307 | while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) { |
57c9fccb NS |
2308 | entry--; |
2309 | probe--; | |
2310 | } | |
a24374f4 DC |
2311 | while (probe < ichdr.count && |
2312 | be32_to_cpu(entry->hashval) < hashval) { | |
57c9fccb NS |
2313 | entry++; |
2314 | probe++; | |
2315 | } | |
a24374f4 | 2316 | if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) { |
57c9fccb | 2317 | args->index = probe; |
12b53197 | 2318 | return -ENOATTR; |
57c9fccb NS |
2319 | } |
2320 | ||
2321 | /* | |
2322 | * Duplicate keys may be present, so search all of them for a match. | |
2323 | */ | |
a24374f4 | 2324 | for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval); |
57c9fccb NS |
2325 | entry++, probe++) { |
2326 | /* | |
2327 | * GROT: Add code to remove incomplete entries. | |
2328 | */ | |
2329 | /* | |
2330 | * If we are looking for INCOMPLETE entries, show only those. | |
2331 | * If we are looking for complete entries, show only those. | |
2332 | */ | |
2333 | if ((args->flags & XFS_ATTR_INCOMPLETE) != | |
2334 | (entry->flags & XFS_ATTR_INCOMPLETE)) { | |
2335 | continue; | |
2336 | } | |
2337 | if (entry->flags & XFS_ATTR_LOCAL) { | |
a24374f4 | 2338 | name_loc = xfs_attr3_leaf_name_local(leaf, probe); |
57c9fccb NS |
2339 | if (name_loc->namelen != args->namelen) |
2340 | continue; | |
a24374f4 DC |
2341 | if (memcmp(args->name, name_loc->nameval, |
2342 | args->namelen) != 0) | |
57c9fccb | 2343 | continue; |
5e656dbb | 2344 | if (!xfs_attr_namesp_match(args->flags, entry->flags)) |
57c9fccb NS |
2345 | continue; |
2346 | args->index = probe; | |
12b53197 | 2347 | return -EEXIST; |
57c9fccb | 2348 | } else { |
a24374f4 | 2349 | name_rmt = xfs_attr3_leaf_name_remote(leaf, probe); |
57c9fccb NS |
2350 | if (name_rmt->namelen != args->namelen) |
2351 | continue; | |
a24374f4 DC |
2352 | if (memcmp(args->name, name_rmt->name, |
2353 | args->namelen) != 0) | |
57c9fccb | 2354 | continue; |
5e656dbb | 2355 | if (!xfs_attr_namesp_match(args->flags, entry->flags)) |
57c9fccb NS |
2356 | continue; |
2357 | args->index = probe; | |
ff105f75 | 2358 | args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen); |
5e656dbb | 2359 | args->rmtblkno = be32_to_cpu(name_rmt->valueblk); |
f08bc2a9 DC |
2360 | args->rmtblkcnt = xfs_attr3_rmt_blocks( |
2361 | args->dp->i_mount, | |
ff105f75 | 2362 | args->rmtvaluelen); |
12b53197 | 2363 | return -EEXIST; |
57c9fccb NS |
2364 | } |
2365 | } | |
2366 | args->index = probe; | |
12b53197 | 2367 | return -ENOATTR; |
57c9fccb NS |
2368 | } |
2369 | ||
5e656dbb BN |
2370 | /* |
2371 | * Get the value associated with an attribute name from a leaf attribute | |
2372 | * list structure. | |
42a383ab DC |
2373 | * |
2374 | * If ATTR_KERNOVAL is specified, only the length needs to be returned. | |
2375 | * Unlike a lookup, we only return an error if the attribute does not | |
2376 | * exist or we can't retrieve the value. | |
5e656dbb BN |
2377 | */ |
2378 | int | |
a24374f4 DC |
2379 | xfs_attr3_leaf_getvalue( |
2380 | struct xfs_buf *bp, | |
2381 | struct xfs_da_args *args) | |
5e656dbb | 2382 | { |
a24374f4 DC |
2383 | struct xfs_attr_leafblock *leaf; |
2384 | struct xfs_attr3_icleaf_hdr ichdr; | |
2385 | struct xfs_attr_leaf_entry *entry; | |
2386 | struct xfs_attr_leaf_name_local *name_loc; | |
2387 | struct xfs_attr_leaf_name_remote *name_rmt; | |
5e656dbb | 2388 | |
a2ceac1f | 2389 | leaf = bp->b_addr; |
19ebedcf | 2390 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
ff105f75 | 2391 | ASSERT(ichdr.count < args->geo->blksize / 8); |
a24374f4 | 2392 | ASSERT(args->index < ichdr.count); |
5e656dbb | 2393 | |
a24374f4 | 2394 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
5e656dbb | 2395 | if (entry->flags & XFS_ATTR_LOCAL) { |
a24374f4 | 2396 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
5e656dbb BN |
2397 | ASSERT(name_loc->namelen == args->namelen); |
2398 | ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0); | |
4343d303 DC |
2399 | return xfs_attr_copy_value(args, |
2400 | &name_loc->nameval[args->namelen], | |
2401 | be16_to_cpu(name_loc->valuelen)); | |
ab0d25d8 DC |
2402 | } |
2403 | ||
2404 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); | |
2405 | ASSERT(name_rmt->namelen == args->namelen); | |
2406 | ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0); | |
2407 | args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen); | |
2408 | args->rmtblkno = be32_to_cpu(name_rmt->valueblk); | |
2409 | args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount, | |
2410 | args->rmtvaluelen); | |
4343d303 | 2411 | return xfs_attr_copy_value(args, NULL, args->rmtvaluelen); |
5e656dbb | 2412 | } |
2bd0ea18 NS |
2413 | |
2414 | /*======================================================================== | |
2415 | * Utility routines. | |
2416 | *========================================================================*/ | |
2417 | ||
2418 | /* | |
2419 | * Move the indicated entries from one leaf to another. | |
2420 | * NOTE: this routine modifies both source and destination leaves. | |
2421 | */ | |
2422 | /*ARGSUSED*/ | |
2423 | STATIC void | |
a24374f4 | 2424 | xfs_attr3_leaf_moveents( |
ff105f75 | 2425 | struct xfs_da_args *args, |
a24374f4 DC |
2426 | struct xfs_attr_leafblock *leaf_s, |
2427 | struct xfs_attr3_icleaf_hdr *ichdr_s, | |
2428 | int start_s, | |
2429 | struct xfs_attr_leafblock *leaf_d, | |
2430 | struct xfs_attr3_icleaf_hdr *ichdr_d, | |
2431 | int start_d, | |
ff105f75 | 2432 | int count) |
2bd0ea18 | 2433 | { |
a24374f4 DC |
2434 | struct xfs_attr_leaf_entry *entry_s; |
2435 | struct xfs_attr_leaf_entry *entry_d; | |
2436 | int desti; | |
2437 | int tmp; | |
2438 | int i; | |
2bd0ea18 NS |
2439 | |
2440 | /* | |
2441 | * Check for nothing to do. | |
2442 | */ | |
2443 | if (count == 0) | |
2444 | return; | |
2445 | ||
2446 | /* | |
2447 | * Set up environment. | |
2448 | */ | |
a24374f4 DC |
2449 | ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC || |
2450 | ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC); | |
2451 | ASSERT(ichdr_s->magic == ichdr_d->magic); | |
ff105f75 | 2452 | ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8); |
a24374f4 DC |
2453 | ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s)) |
2454 | + xfs_attr3_leaf_hdr_size(leaf_s)); | |
ff105f75 | 2455 | ASSERT(ichdr_d->count < args->geo->blksize / 8); |
a24374f4 DC |
2456 | ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d)) |
2457 | + xfs_attr3_leaf_hdr_size(leaf_d)); | |
2458 | ||
2459 | ASSERT(start_s < ichdr_s->count); | |
2460 | ASSERT(start_d <= ichdr_d->count); | |
2461 | ASSERT(count <= ichdr_s->count); | |
2462 | ||
2bd0ea18 NS |
2463 | |
2464 | /* | |
2465 | * Move the entries in the destination leaf up to make a hole? | |
2466 | */ | |
a24374f4 DC |
2467 | if (start_d < ichdr_d->count) { |
2468 | tmp = ichdr_d->count - start_d; | |
2bd0ea18 | 2469 | tmp *= sizeof(xfs_attr_leaf_entry_t); |
a24374f4 DC |
2470 | entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; |
2471 | entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count]; | |
2472 | memmove(entry_d, entry_s, tmp); | |
2bd0ea18 NS |
2473 | } |
2474 | ||
2475 | /* | |
2476 | * Copy all entry's in the same (sorted) order, | |
2477 | * but allocate attribute info packed and in sequence. | |
2478 | */ | |
a24374f4 DC |
2479 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; |
2480 | entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d]; | |
2bd0ea18 NS |
2481 | desti = start_d; |
2482 | for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) { | |
a24374f4 | 2483 | ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused); |
2bd0ea18 NS |
2484 | tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i); |
2485 | #ifdef GROT | |
2486 | /* | |
2487 | * Code to drop INCOMPLETE entries. Difficult to use as we | |
dfc130f3 | 2488 | * may also need to change the insertion index. Code turned |
2bd0ea18 NS |
2489 | * off for 6.2, should be revisited later. |
2490 | */ | |
2491 | if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */ | |
a24374f4 DC |
2492 | memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); |
2493 | ichdr_s->usedbytes -= tmp; | |
2494 | ichdr_s->count -= 1; | |
2bd0ea18 NS |
2495 | entry_d--; /* to compensate for ++ in loop hdr */ |
2496 | desti--; | |
2497 | if ((start_s + i) < offset) | |
2498 | result++; /* insertion index adjustment */ | |
2499 | } else { | |
2500 | #endif /* GROT */ | |
a24374f4 | 2501 | ichdr_d->firstused -= tmp; |
5ce1d1f7 NS |
2502 | /* both on-disk, don't endian flip twice */ |
2503 | entry_d->hashval = entry_s->hashval; | |
a24374f4 | 2504 | entry_d->nameidx = cpu_to_be16(ichdr_d->firstused); |
2bd0ea18 | 2505 | entry_d->flags = entry_s->flags; |
5e656dbb | 2506 | ASSERT(be16_to_cpu(entry_d->nameidx) + tmp |
ff105f75 | 2507 | <= args->geo->blksize); |
a24374f4 DC |
2508 | memmove(xfs_attr3_leaf_name(leaf_d, desti), |
2509 | xfs_attr3_leaf_name(leaf_s, start_s + i), tmp); | |
5e656dbb | 2510 | ASSERT(be16_to_cpu(entry_s->nameidx) + tmp |
ff105f75 | 2511 | <= args->geo->blksize); |
a24374f4 DC |
2512 | memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp); |
2513 | ichdr_s->usedbytes -= tmp; | |
2514 | ichdr_d->usedbytes += tmp; | |
2515 | ichdr_s->count -= 1; | |
2516 | ichdr_d->count += 1; | |
2517 | tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t) | |
2518 | + xfs_attr3_leaf_hdr_size(leaf_d); | |
2519 | ASSERT(ichdr_d->firstused >= tmp); | |
2bd0ea18 NS |
2520 | #ifdef GROT |
2521 | } | |
2522 | #endif /* GROT */ | |
2523 | } | |
2524 | ||
2525 | /* | |
2526 | * Zero out the entries we just copied. | |
2527 | */ | |
a24374f4 | 2528 | if (start_s == ichdr_s->count) { |
2bd0ea18 | 2529 | tmp = count * sizeof(xfs_attr_leaf_entry_t); |
a24374f4 | 2530 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; |
2bd0ea18 | 2531 | ASSERT(((char *)entry_s + tmp) <= |
ff105f75 | 2532 | ((char *)leaf_s + args->geo->blksize)); |
a24374f4 | 2533 | memset(entry_s, 0, tmp); |
2bd0ea18 NS |
2534 | } else { |
2535 | /* | |
2536 | * Move the remaining entries down to fill the hole, | |
2537 | * then zero the entries at the top. | |
2538 | */ | |
78d6585c | 2539 | tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t); |
a24374f4 DC |
2540 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count]; |
2541 | entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s]; | |
2542 | memmove(entry_d, entry_s, tmp); | |
2bd0ea18 NS |
2543 | |
2544 | tmp = count * sizeof(xfs_attr_leaf_entry_t); | |
a24374f4 | 2545 | entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count]; |
2bd0ea18 | 2546 | ASSERT(((char *)entry_s + tmp) <= |
ff105f75 | 2547 | ((char *)leaf_s + args->geo->blksize)); |
a24374f4 | 2548 | memset(entry_s, 0, tmp); |
2bd0ea18 NS |
2549 | } |
2550 | ||
2551 | /* | |
2552 | * Fill in the freemap information | |
2553 | */ | |
a24374f4 DC |
2554 | ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d); |
2555 | ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t); | |
2556 | ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base; | |
2557 | ichdr_d->freemap[1].base = 0; | |
2558 | ichdr_d->freemap[2].base = 0; | |
2559 | ichdr_d->freemap[1].size = 0; | |
2560 | ichdr_d->freemap[2].size = 0; | |
2561 | ichdr_s->holes = 1; /* leaf may not be compact */ | |
2bd0ea18 NS |
2562 | } |
2563 | ||
2564 | /* | |
2565 | * Pick up the last hashvalue from a leaf block. | |
2566 | */ | |
2567 | xfs_dahash_t | |
a2ceac1f DC |
2568 | xfs_attr_leaf_lasthash( |
2569 | struct xfs_buf *bp, | |
2570 | int *count) | |
2bd0ea18 | 2571 | { |
a24374f4 DC |
2572 | struct xfs_attr3_icleaf_hdr ichdr; |
2573 | struct xfs_attr_leaf_entry *entries; | |
7861ef77 | 2574 | struct xfs_mount *mp = bp->b_mount; |
2bd0ea18 | 2575 | |
19ebedcf | 2576 | xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr); |
a24374f4 | 2577 | entries = xfs_attr3_leaf_entryp(bp->b_addr); |
2bd0ea18 | 2578 | if (count) |
a24374f4 DC |
2579 | *count = ichdr.count; |
2580 | if (!ichdr.count) | |
2581 | return 0; | |
2582 | return be32_to_cpu(entries[ichdr.count - 1].hashval); | |
2bd0ea18 NS |
2583 | } |
2584 | ||
2585 | /* | |
2586 | * Calculate the number of bytes used to store the indicated attribute | |
2587 | * (whether local or remote only calculate bytes in this block). | |
2588 | */ | |
5e656dbb | 2589 | STATIC int |
2bd0ea18 NS |
2590 | xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index) |
2591 | { | |
a24374f4 | 2592 | struct xfs_attr_leaf_entry *entries; |
2bd0ea18 NS |
2593 | xfs_attr_leaf_name_local_t *name_loc; |
2594 | xfs_attr_leaf_name_remote_t *name_rmt; | |
2595 | int size; | |
2596 | ||
a24374f4 DC |
2597 | entries = xfs_attr3_leaf_entryp(leaf); |
2598 | if (entries[index].flags & XFS_ATTR_LOCAL) { | |
2599 | name_loc = xfs_attr3_leaf_name_local(leaf, index); | |
56b2de80 | 2600 | size = xfs_attr_leaf_entsize_local(name_loc->namelen, |
5e656dbb | 2601 | be16_to_cpu(name_loc->valuelen)); |
2bd0ea18 | 2602 | } else { |
a24374f4 | 2603 | name_rmt = xfs_attr3_leaf_name_remote(leaf, index); |
56b2de80 | 2604 | size = xfs_attr_leaf_entsize_remote(name_rmt->namelen); |
2bd0ea18 | 2605 | } |
a24374f4 | 2606 | return size; |
2bd0ea18 NS |
2607 | } |
2608 | ||
2609 | /* | |
2610 | * Calculate the number of bytes that would be required to store the new | |
2611 | * attribute (whether local or remote only calculate bytes in this block). | |
2612 | * This routine decides as a side effect whether the attribute will be | |
2613 | * a "local" or a "remote" attribute. | |
2614 | */ | |
2615 | int | |
ff105f75 DC |
2616 | xfs_attr_leaf_newentsize( |
2617 | struct xfs_da_args *args, | |
2618 | int *local) | |
2bd0ea18 | 2619 | { |
ff105f75 | 2620 | int size; |
2bd0ea18 | 2621 | |
ff105f75 DC |
2622 | size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen); |
2623 | if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) { | |
2624 | if (local) | |
2bd0ea18 | 2625 | *local = 1; |
ff105f75 | 2626 | return size; |
2bd0ea18 | 2627 | } |
ff105f75 DC |
2628 | if (local) |
2629 | *local = 0; | |
2630 | return xfs_attr_leaf_entsize_remote(args->namelen); | |
2bd0ea18 | 2631 | } |
57c9fccb | 2632 | |
78d6585c | 2633 | |
57c9fccb NS |
2634 | /*======================================================================== |
2635 | * Manage the INCOMPLETE flag in a leaf entry | |
2636 | *========================================================================*/ | |
2637 | ||
2638 | /* | |
2639 | * Clear the INCOMPLETE flag on an entry in a leaf block. | |
2640 | */ | |
2641 | int | |
a24374f4 DC |
2642 | xfs_attr3_leaf_clearflag( |
2643 | struct xfs_da_args *args) | |
57c9fccb | 2644 | { |
a24374f4 DC |
2645 | struct xfs_attr_leafblock *leaf; |
2646 | struct xfs_attr_leaf_entry *entry; | |
2647 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2648 | struct xfs_buf *bp; | |
2649 | int error; | |
57c9fccb | 2650 | #ifdef DEBUG |
a24374f4 | 2651 | struct xfs_attr3_icleaf_hdr ichdr; |
57c9fccb NS |
2652 | xfs_attr_leaf_name_local_t *name_loc; |
2653 | int namelen; | |
2654 | char *name; | |
2655 | #endif /* DEBUG */ | |
2656 | ||
a2ceac1f | 2657 | trace_xfs_attr_leaf_clearflag(args); |
57c9fccb NS |
2658 | /* |
2659 | * Set up the operation. | |
2660 | */ | |
a24374f4 | 2661 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
a2ceac1f | 2662 | if (error) |
af43ca9f | 2663 | return error; |
57c9fccb | 2664 | |
a2ceac1f | 2665 | leaf = bp->b_addr; |
a24374f4 | 2666 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; |
57c9fccb NS |
2667 | ASSERT(entry->flags & XFS_ATTR_INCOMPLETE); |
2668 | ||
2669 | #ifdef DEBUG | |
19ebedcf | 2670 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
a24374f4 DC |
2671 | ASSERT(args->index < ichdr.count); |
2672 | ASSERT(args->index >= 0); | |
2673 | ||
57c9fccb | 2674 | if (entry->flags & XFS_ATTR_LOCAL) { |
a24374f4 | 2675 | name_loc = xfs_attr3_leaf_name_local(leaf, args->index); |
57c9fccb NS |
2676 | namelen = name_loc->namelen; |
2677 | name = (char *)name_loc->nameval; | |
2678 | } else { | |
a24374f4 | 2679 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
57c9fccb NS |
2680 | namelen = name_rmt->namelen; |
2681 | name = (char *)name_rmt->name; | |
2682 | } | |
5e656dbb | 2683 | ASSERT(be32_to_cpu(entry->hashval) == args->hashval); |
57c9fccb NS |
2684 | ASSERT(namelen == args->namelen); |
2685 | ASSERT(memcmp(name, args->name, namelen) == 0); | |
2686 | #endif /* DEBUG */ | |
2687 | ||
2688 | entry->flags &= ~XFS_ATTR_INCOMPLETE; | |
a2ceac1f | 2689 | xfs_trans_log_buf(args->trans, bp, |
57c9fccb NS |
2690 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
2691 | ||
2692 | if (args->rmtblkno) { | |
2693 | ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0); | |
a24374f4 | 2694 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
5e656dbb | 2695 | name_rmt->valueblk = cpu_to_be32(args->rmtblkno); |
ff105f75 | 2696 | name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen); |
a2ceac1f | 2697 | xfs_trans_log_buf(args->trans, bp, |
57c9fccb NS |
2698 | XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); |
2699 | } | |
57c9fccb NS |
2700 | |
2701 | /* | |
2702 | * Commit the flag value change and start the next trans in series. | |
2703 | */ | |
d67406c9 | 2704 | return xfs_trans_roll_inode(&args->trans, args->dp); |
57c9fccb NS |
2705 | } |
2706 | ||
2707 | /* | |
2708 | * Set the INCOMPLETE flag on an entry in a leaf block. | |
2709 | */ | |
2710 | int | |
a24374f4 DC |
2711 | xfs_attr3_leaf_setflag( |
2712 | struct xfs_da_args *args) | |
57c9fccb | 2713 | { |
a24374f4 DC |
2714 | struct xfs_attr_leafblock *leaf; |
2715 | struct xfs_attr_leaf_entry *entry; | |
2716 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2717 | struct xfs_buf *bp; | |
57c9fccb | 2718 | int error; |
a24374f4 DC |
2719 | #ifdef DEBUG |
2720 | struct xfs_attr3_icleaf_hdr ichdr; | |
2721 | #endif | |
57c9fccb | 2722 | |
a2ceac1f DC |
2723 | trace_xfs_attr_leaf_setflag(args); |
2724 | ||
57c9fccb NS |
2725 | /* |
2726 | * Set up the operation. | |
2727 | */ | |
a24374f4 | 2728 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp); |
a2ceac1f | 2729 | if (error) |
af43ca9f | 2730 | return error; |
57c9fccb | 2731 | |
a2ceac1f | 2732 | leaf = bp->b_addr; |
a24374f4 | 2733 | #ifdef DEBUG |
19ebedcf | 2734 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf); |
a24374f4 | 2735 | ASSERT(args->index < ichdr.count); |
57c9fccb | 2736 | ASSERT(args->index >= 0); |
a24374f4 DC |
2737 | #endif |
2738 | entry = &xfs_attr3_leaf_entryp(leaf)[args->index]; | |
57c9fccb NS |
2739 | |
2740 | ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0); | |
2741 | entry->flags |= XFS_ATTR_INCOMPLETE; | |
a2ceac1f | 2742 | xfs_trans_log_buf(args->trans, bp, |
57c9fccb NS |
2743 | XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry))); |
2744 | if ((entry->flags & XFS_ATTR_LOCAL) == 0) { | |
a24374f4 | 2745 | name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index); |
57c9fccb NS |
2746 | name_rmt->valueblk = 0; |
2747 | name_rmt->valuelen = 0; | |
a2ceac1f | 2748 | xfs_trans_log_buf(args->trans, bp, |
57c9fccb NS |
2749 | XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt))); |
2750 | } | |
57c9fccb NS |
2751 | |
2752 | /* | |
2753 | * Commit the flag value change and start the next trans in series. | |
2754 | */ | |
d67406c9 | 2755 | return xfs_trans_roll_inode(&args->trans, args->dp); |
57c9fccb NS |
2756 | } |
2757 | ||
2758 | /* | |
2759 | * In a single transaction, clear the INCOMPLETE flag on the leaf entry | |
2760 | * given by args->blkno/index and set the INCOMPLETE flag on the leaf | |
2761 | * entry given by args->blkno2/index2. | |
2762 | * | |
2763 | * Note that they could be in different blocks, or in the same block. | |
2764 | */ | |
2765 | int | |
a24374f4 DC |
2766 | xfs_attr3_leaf_flipflags( |
2767 | struct xfs_da_args *args) | |
57c9fccb | 2768 | { |
a24374f4 DC |
2769 | struct xfs_attr_leafblock *leaf1; |
2770 | struct xfs_attr_leafblock *leaf2; | |
2771 | struct xfs_attr_leaf_entry *entry1; | |
2772 | struct xfs_attr_leaf_entry *entry2; | |
2773 | struct xfs_attr_leaf_name_remote *name_rmt; | |
2774 | struct xfs_buf *bp1; | |
2775 | struct xfs_buf *bp2; | |
57c9fccb NS |
2776 | int error; |
2777 | #ifdef DEBUG | |
a24374f4 DC |
2778 | struct xfs_attr3_icleaf_hdr ichdr1; |
2779 | struct xfs_attr3_icleaf_hdr ichdr2; | |
57c9fccb NS |
2780 | xfs_attr_leaf_name_local_t *name_loc; |
2781 | int namelen1, namelen2; | |
2782 | char *name1, *name2; | |
2783 | #endif /* DEBUG */ | |
2784 | ||
a2ceac1f DC |
2785 | trace_xfs_attr_leaf_flipflags(args); |
2786 | ||
57c9fccb NS |
2787 | /* |
2788 | * Read the block containing the "old" attr | |
2789 | */ | |
a24374f4 | 2790 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1); |
a2ceac1f DC |
2791 | if (error) |
2792 | return error; | |
57c9fccb NS |
2793 | |
2794 | /* | |
2795 | * Read the block containing the "new" attr, if it is different | |
2796 | */ | |
2797 | if (args->blkno2 != args->blkno) { | |
a24374f4 | 2798 | error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2, |
a2ceac1f DC |
2799 | -1, &bp2); |
2800 | if (error) | |
2801 | return error; | |
57c9fccb NS |
2802 | } else { |
2803 | bp2 = bp1; | |
2804 | } | |
2805 | ||
a2ceac1f | 2806 | leaf1 = bp1->b_addr; |
a24374f4 | 2807 | entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index]; |
57c9fccb | 2808 | |
a2ceac1f | 2809 | leaf2 = bp2->b_addr; |
a24374f4 | 2810 | entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2]; |
57c9fccb NS |
2811 | |
2812 | #ifdef DEBUG | |
19ebedcf | 2813 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1); |
a24374f4 DC |
2814 | ASSERT(args->index < ichdr1.count); |
2815 | ASSERT(args->index >= 0); | |
2816 | ||
19ebedcf | 2817 | xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2); |
a24374f4 DC |
2818 | ASSERT(args->index2 < ichdr2.count); |
2819 | ASSERT(args->index2 >= 0); | |
2820 | ||
57c9fccb | 2821 | if (entry1->flags & XFS_ATTR_LOCAL) { |
a24374f4 | 2822 | name_loc = xfs_attr3_leaf_name_local(leaf1, args->index); |
57c9fccb NS |
2823 | namelen1 = name_loc->namelen; |
2824 | name1 = (char *)name_loc->nameval; | |
2825 | } else { | |
a24374f4 | 2826 | name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); |
57c9fccb NS |
2827 | namelen1 = name_rmt->namelen; |
2828 | name1 = (char *)name_rmt->name; | |
2829 | } | |
2830 | if (entry2->flags & XFS_ATTR_LOCAL) { | |
a24374f4 | 2831 | name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2); |
57c9fccb NS |
2832 | namelen2 = name_loc->namelen; |
2833 | name2 = (char *)name_loc->nameval; | |
2834 | } else { | |
a24374f4 | 2835 | name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); |
57c9fccb NS |
2836 | namelen2 = name_rmt->namelen; |
2837 | name2 = (char *)name_rmt->name; | |
2838 | } | |
5e656dbb | 2839 | ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval)); |
57c9fccb NS |
2840 | ASSERT(namelen1 == namelen2); |
2841 | ASSERT(memcmp(name1, name2, namelen1) == 0); | |
2842 | #endif /* DEBUG */ | |
2843 | ||
2844 | ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE); | |
2845 | ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0); | |
2846 | ||
2847 | entry1->flags &= ~XFS_ATTR_INCOMPLETE; | |
a2ceac1f | 2848 | xfs_trans_log_buf(args->trans, bp1, |
57c9fccb NS |
2849 | XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1))); |
2850 | if (args->rmtblkno) { | |
2851 | ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0); | |
a24374f4 | 2852 | name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index); |
5e656dbb | 2853 | name_rmt->valueblk = cpu_to_be32(args->rmtblkno); |
ff105f75 | 2854 | name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen); |
a2ceac1f | 2855 | xfs_trans_log_buf(args->trans, bp1, |
57c9fccb NS |
2856 | XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt))); |
2857 | } | |
2858 | ||
2859 | entry2->flags |= XFS_ATTR_INCOMPLETE; | |
a2ceac1f | 2860 | xfs_trans_log_buf(args->trans, bp2, |
57c9fccb NS |
2861 | XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2))); |
2862 | if ((entry2->flags & XFS_ATTR_LOCAL) == 0) { | |
a24374f4 | 2863 | name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2); |
57c9fccb NS |
2864 | name_rmt->valueblk = 0; |
2865 | name_rmt->valuelen = 0; | |
a2ceac1f | 2866 | xfs_trans_log_buf(args->trans, bp2, |
57c9fccb NS |
2867 | XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt))); |
2868 | } | |
57c9fccb NS |
2869 | |
2870 | /* | |
2871 | * Commit the flag value change and start the next trans in series. | |
2872 | */ | |
d67406c9 | 2873 | error = xfs_trans_roll_inode(&args->trans, args->dp); |
57c9fccb | 2874 | |
a24374f4 | 2875 | return error; |
57c9fccb | 2876 | } |