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