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