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