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