]>
Commit | Line | Data |
---|---|---|
37b3b4d6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
2bd0ea18 | 2 | /* |
5e656dbb | 3 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
65b80c98 | 4 | * Copyright (c) 2013 Red Hat, Inc. |
da23017d | 5 | * All Rights Reserved. |
2bd0ea18 | 6 | */ |
9c799827 | 7 | #include "libxfs_priv.h" |
b626fb59 | 8 | #include "xfs_fs.h" |
27846843 | 9 | #include "xfs_shared.h" |
b626fb59 DC |
10 | #include "xfs_format.h" |
11 | #include "xfs_log_format.h" | |
12 | #include "xfs_trans_resv.h" | |
13 | #include "xfs_mount.h" | |
b626fb59 DC |
14 | #include "xfs_inode.h" |
15 | #include "xfs_bmap.h" | |
16 | #include "xfs_dir2.h" | |
17 | #include "xfs_dir2_priv.h" | |
18 | #include "xfs_trace.h" | |
19 | #include "xfs_trans.h" | |
bcf1c967 | 20 | #include "xfs_health.h" |
5e656dbb | 21 | |
2bd0ea18 | 22 | /* |
5e656dbb | 23 | * Local function declarations. |
2bd0ea18 | 24 | */ |
a2ceac1f | 25 | static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp, |
a2279497 CH |
26 | int *indexp, struct xfs_buf **dbpp, |
27 | struct xfs_dir3_icleaf_hdr *leafhdr); | |
ff105f75 DC |
28 | static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args, |
29 | struct xfs_buf *bp, int first, int last); | |
30 | static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args, | |
31 | struct xfs_buf *bp); | |
2bd0ea18 | 32 | |
9db68faf CH |
33 | void |
34 | xfs_dir2_leaf_hdr_from_disk( | |
35 | struct xfs_mount *mp, | |
36 | struct xfs_dir3_icleaf_hdr *to, | |
37 | struct xfs_dir2_leaf *from) | |
38 | { | |
94541a16 | 39 | if (xfs_has_crc(mp)) { |
9db68faf CH |
40 | struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from; |
41 | ||
42 | to->forw = be32_to_cpu(from3->hdr.info.hdr.forw); | |
43 | to->back = be32_to_cpu(from3->hdr.info.hdr.back); | |
44 | to->magic = be16_to_cpu(from3->hdr.info.hdr.magic); | |
45 | to->count = be16_to_cpu(from3->hdr.count); | |
46 | to->stale = be16_to_cpu(from3->hdr.stale); | |
a2279497 | 47 | to->ents = from3->__ents; |
9db68faf CH |
48 | |
49 | ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC || | |
50 | to->magic == XFS_DIR3_LEAFN_MAGIC); | |
51 | } else { | |
52 | to->forw = be32_to_cpu(from->hdr.info.forw); | |
53 | to->back = be32_to_cpu(from->hdr.info.back); | |
54 | to->magic = be16_to_cpu(from->hdr.info.magic); | |
55 | to->count = be16_to_cpu(from->hdr.count); | |
56 | to->stale = be16_to_cpu(from->hdr.stale); | |
a2279497 | 57 | to->ents = from->__ents; |
9db68faf CH |
58 | |
59 | ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC || | |
60 | to->magic == XFS_DIR2_LEAFN_MAGIC); | |
61 | } | |
62 | } | |
63 | ||
7adfbcf6 CH |
64 | void |
65 | xfs_dir2_leaf_hdr_to_disk( | |
66 | struct xfs_mount *mp, | |
67 | struct xfs_dir2_leaf *to, | |
68 | struct xfs_dir3_icleaf_hdr *from) | |
69 | { | |
94541a16 | 70 | if (xfs_has_crc(mp)) { |
7adfbcf6 CH |
71 | struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to; |
72 | ||
73 | ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC || | |
74 | from->magic == XFS_DIR3_LEAFN_MAGIC); | |
75 | ||
76 | to3->hdr.info.hdr.forw = cpu_to_be32(from->forw); | |
77 | to3->hdr.info.hdr.back = cpu_to_be32(from->back); | |
78 | to3->hdr.info.hdr.magic = cpu_to_be16(from->magic); | |
79 | to3->hdr.count = cpu_to_be16(from->count); | |
80 | to3->hdr.stale = cpu_to_be16(from->stale); | |
81 | } else { | |
82 | ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC || | |
83 | from->magic == XFS_DIR2_LEAFN_MAGIC); | |
84 | ||
85 | to->hdr.info.forw = cpu_to_be32(from->forw); | |
86 | to->hdr.info.back = cpu_to_be32(from->back); | |
87 | to->hdr.info.magic = cpu_to_be16(from->magic); | |
88 | to->hdr.count = cpu_to_be16(from->count); | |
89 | to->hdr.stale = cpu_to_be16(from->stale); | |
90 | } | |
91 | } | |
92 | ||
65b80c98 DC |
93 | /* |
94 | * Check the internal consistency of a leaf1 block. | |
95 | * Pop an assert if something is wrong. | |
96 | */ | |
97 | #ifdef DEBUG | |
bc01119d | 98 | static xfs_failaddr_t |
65b80c98 | 99 | xfs_dir3_leaf1_check( |
ff105f75 | 100 | struct xfs_inode *dp, |
65b80c98 DC |
101 | struct xfs_buf *bp) |
102 | { | |
103 | struct xfs_dir2_leaf *leaf = bp->b_addr; | |
104 | struct xfs_dir3_icleaf_hdr leafhdr; | |
105 | ||
9db68faf | 106 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); |
65b80c98 DC |
107 | |
108 | if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) { | |
109 | struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; | |
f1208396 | 110 | if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp)) |
bc01119d | 111 | return __this_address; |
65b80c98 | 112 | } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC) |
bc01119d | 113 | return __this_address; |
65b80c98 | 114 | |
f5e56725 | 115 | return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false); |
65b80c98 | 116 | } |
bc01119d DW |
117 | |
118 | static inline void | |
119 | xfs_dir3_leaf_check( | |
120 | struct xfs_inode *dp, | |
121 | struct xfs_buf *bp) | |
122 | { | |
123 | xfs_failaddr_t fa; | |
124 | ||
125 | fa = xfs_dir3_leaf1_check(dp, bp); | |
126 | if (!fa) | |
127 | return; | |
128 | xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount, | |
3177b915 DW |
129 | bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__, |
130 | fa); | |
bc01119d DW |
131 | ASSERT(0); |
132 | } | |
65b80c98 | 133 | #else |
ff105f75 | 134 | #define xfs_dir3_leaf_check(dp, bp) |
65b80c98 DC |
135 | #endif |
136 | ||
bc01119d | 137 | xfs_failaddr_t |
65b80c98 | 138 | xfs_dir3_leaf_check_int( |
7bba6d84 CH |
139 | struct xfs_mount *mp, |
140 | struct xfs_dir3_icleaf_hdr *hdr, | |
f5e56725 DC |
141 | struct xfs_dir2_leaf *leaf, |
142 | bool expensive_checking) | |
65b80c98 | 143 | { |
7bba6d84 CH |
144 | struct xfs_da_geometry *geo = mp->m_dir_geo; |
145 | xfs_dir2_leaf_tail_t *ltp; | |
146 | int stale; | |
147 | int i; | |
20798cc0 GX |
148 | bool isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC || |
149 | hdr->magic == XFS_DIR3_LEAF1_MAGIC); | |
ff105f75 | 150 | |
ff105f75 | 151 | ltp = xfs_dir2_leaf_tail_p(geo, leaf); |
65b80c98 DC |
152 | |
153 | /* | |
154 | * XXX (dgc): This value is not restrictive enough. | |
155 | * Should factor in the size of the bests table as well. | |
509dcb4b | 156 | * We can deduce a value for that from i_disk_size. |
65b80c98 | 157 | */ |
d106a3e0 | 158 | if (hdr->count > geo->leaf_max_ents) |
bc01119d | 159 | return __this_address; |
65b80c98 DC |
160 | |
161 | /* Leaves and bests don't overlap in leaf format. */ | |
20798cc0 | 162 | if (isleaf1 && |
a2279497 | 163 | (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp)) |
bc01119d | 164 | return __this_address; |
65b80c98 | 165 | |
f5e56725 DC |
166 | if (!expensive_checking) |
167 | return NULL; | |
168 | ||
65b80c98 DC |
169 | /* Check hash value order, count stale entries. */ |
170 | for (i = stale = 0; i < hdr->count; i++) { | |
171 | if (i + 1 < hdr->count) { | |
a2279497 CH |
172 | if (be32_to_cpu(hdr->ents[i].hashval) > |
173 | be32_to_cpu(hdr->ents[i + 1].hashval)) | |
bc01119d | 174 | return __this_address; |
65b80c98 | 175 | } |
a2279497 | 176 | if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
65b80c98 | 177 | stale++; |
20798cc0 GX |
178 | if (isleaf1 && xfs_dir2_dataptr_to_db(geo, |
179 | be32_to_cpu(hdr->ents[i].address)) >= | |
180 | be32_to_cpu(ltp->bestcount)) | |
181 | return __this_address; | |
65b80c98 DC |
182 | } |
183 | if (hdr->stale != stale) | |
bc01119d DW |
184 | return __this_address; |
185 | return NULL; | |
65b80c98 DC |
186 | } |
187 | ||
36bca899 DC |
188 | /* |
189 | * We verify the magic numbers before decoding the leaf header so that on debug | |
190 | * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due | |
191 | * to incorrect magic numbers. | |
192 | */ | |
bc01119d | 193 | static xfs_failaddr_t |
65b80c98 | 194 | xfs_dir3_leaf_verify( |
7bba6d84 | 195 | struct xfs_buf *bp) |
a2ceac1f | 196 | { |
7bba6d84 CH |
197 | struct xfs_mount *mp = bp->b_mount; |
198 | struct xfs_dir3_icleaf_hdr leafhdr; | |
199 | xfs_failaddr_t fa; | |
65b80c98 | 200 | |
6b27f70a BF |
201 | fa = xfs_da3_blkinfo_verify(bp, bp->b_addr); |
202 | if (fa) | |
203 | return fa; | |
36bca899 | 204 | |
7bba6d84 | 205 | xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr); |
f5e56725 | 206 | return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true); |
65b80c98 DC |
207 | } |
208 | ||
17e0d66d DW |
209 | xfs_failaddr_t |
210 | xfs_dir3_leaf_header_check( | |
211 | struct xfs_buf *bp, | |
212 | xfs_ino_t owner) | |
213 | { | |
214 | struct xfs_mount *mp = bp->b_mount; | |
215 | ||
216 | if (xfs_has_crc(mp)) { | |
217 | struct xfs_dir3_leaf *hdr3 = bp->b_addr; | |
218 | ||
219 | if (hdr3->hdr.info.hdr.magic != | |
220 | cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) && | |
221 | hdr3->hdr.info.hdr.magic != | |
222 | cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) | |
223 | return __this_address; | |
224 | ||
225 | if (be64_to_cpu(hdr3->hdr.info.owner) != owner) | |
226 | return __this_address; | |
227 | } | |
228 | ||
229 | return NULL; | |
230 | } | |
231 | ||
65b80c98 | 232 | static void |
26b7b8fa BF |
233 | xfs_dir3_leaf_read_verify( |
234 | struct xfs_buf *bp) | |
65b80c98 | 235 | { |
7861ef77 | 236 | struct xfs_mount *mp = bp->b_mount; |
1e697959 | 237 | xfs_failaddr_t fa; |
65b80c98 | 238 | |
b16a427a | 239 | if (xfs_has_crc(mp) && |
45922933 | 240 | !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF)) |
1e697959 DW |
241 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
242 | else { | |
26b7b8fa | 243 | fa = xfs_dir3_leaf_verify(bp); |
1e697959 DW |
244 | if (fa) |
245 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
246 | } | |
a2ceac1f DC |
247 | } |
248 | ||
249 | static void | |
26b7b8fa BF |
250 | xfs_dir3_leaf_write_verify( |
251 | struct xfs_buf *bp) | |
65b80c98 | 252 | { |
7861ef77 | 253 | struct xfs_mount *mp = bp->b_mount; |
37d086ca | 254 | struct xfs_buf_log_item *bip = bp->b_log_item; |
65b80c98 | 255 | struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr; |
1e697959 | 256 | xfs_failaddr_t fa; |
65b80c98 | 257 | |
26b7b8fa | 258 | fa = xfs_dir3_leaf_verify(bp); |
1e697959 DW |
259 | if (fa) { |
260 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
65b80c98 DC |
261 | return; |
262 | } | |
263 | ||
b16a427a | 264 | if (!xfs_has_crc(mp)) |
65b80c98 DC |
265 | return; |
266 | ||
267 | if (bip) | |
268 | hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
269 | ||
43b5aeed | 270 | xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF); |
65b80c98 DC |
271 | } |
272 | ||
65b80c98 | 273 | const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = { |
a3fac935 | 274 | .name = "xfs_dir3_leaf1", |
9e26de8f DW |
275 | .magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC), |
276 | cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) }, | |
26b7b8fa BF |
277 | .verify_read = xfs_dir3_leaf_read_verify, |
278 | .verify_write = xfs_dir3_leaf_write_verify, | |
279 | .verify_struct = xfs_dir3_leaf_verify, | |
a2ceac1f DC |
280 | }; |
281 | ||
65b80c98 | 282 | const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = { |
a3fac935 | 283 | .name = "xfs_dir3_leafn", |
9e26de8f DW |
284 | .magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC), |
285 | cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) }, | |
26b7b8fa BF |
286 | .verify_read = xfs_dir3_leaf_read_verify, |
287 | .verify_write = xfs_dir3_leaf_write_verify, | |
288 | .verify_struct = xfs_dir3_leaf_verify, | |
a2ceac1f DC |
289 | }; |
290 | ||
50bb67d6 | 291 | int |
65b80c98 | 292 | xfs_dir3_leaf_read( |
a2ceac1f DC |
293 | struct xfs_trans *tp, |
294 | struct xfs_inode *dp, | |
17e0d66d | 295 | xfs_ino_t owner, |
a2ceac1f | 296 | xfs_dablk_t fbno, |
a2ceac1f DC |
297 | struct xfs_buf **bpp) |
298 | { | |
17e0d66d | 299 | xfs_failaddr_t fa; |
8b4dc4a9 DC |
300 | int err; |
301 | ||
5f356ae6 | 302 | err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK, |
0939d90b | 303 | &xfs_dir3_leaf1_buf_ops); |
17e0d66d DW |
304 | if (err || !(*bpp)) |
305 | return err; | |
306 | ||
307 | fa = xfs_dir3_leaf_header_check(*bpp, owner); | |
308 | if (fa) { | |
309 | __xfs_buf_mark_corrupt(*bpp, fa); | |
310 | xfs_trans_brelse(tp, *bpp); | |
311 | *bpp = NULL; | |
312 | xfs_dirattr_mark_sick(dp, XFS_DATA_FORK); | |
313 | return -EFSCORRUPTED; | |
314 | } | |
315 | ||
316 | if (tp) | |
bdc16ee5 | 317 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF); |
17e0d66d | 318 | return 0; |
a2ceac1f DC |
319 | } |
320 | ||
321 | int | |
65b80c98 | 322 | xfs_dir3_leafn_read( |
a2ceac1f DC |
323 | struct xfs_trans *tp, |
324 | struct xfs_inode *dp, | |
17e0d66d | 325 | xfs_ino_t owner, |
a2ceac1f | 326 | xfs_dablk_t fbno, |
a2ceac1f DC |
327 | struct xfs_buf **bpp) |
328 | { | |
17e0d66d | 329 | xfs_failaddr_t fa; |
8b4dc4a9 DC |
330 | int err; |
331 | ||
5f356ae6 | 332 | err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK, |
5fcb9cbc | 333 | &xfs_dir3_leafn_buf_ops); |
17e0d66d DW |
334 | if (err || !(*bpp)) |
335 | return err; | |
336 | ||
337 | fa = xfs_dir3_leaf_header_check(*bpp, owner); | |
338 | if (fa) { | |
339 | __xfs_buf_mark_corrupt(*bpp, fa); | |
340 | xfs_trans_brelse(tp, *bpp); | |
341 | *bpp = NULL; | |
342 | xfs_dirattr_mark_sick(dp, XFS_DATA_FORK); | |
343 | return -EFSCORRUPTED; | |
344 | } | |
345 | ||
346 | if (tp) | |
bdc16ee5 | 347 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF); |
17e0d66d | 348 | return 0; |
65b80c98 DC |
349 | } |
350 | ||
351 | /* | |
352 | * Initialize a new leaf block, leaf1 or leafn magic accepted. | |
353 | */ | |
354 | static void | |
355 | xfs_dir3_leaf_init( | |
da833bae | 356 | struct xfs_da_args *args, |
65b80c98 | 357 | struct xfs_buf *bp, |
4a492e72 | 358 | uint16_t type) |
65b80c98 | 359 | { |
da833bae DW |
360 | struct xfs_mount *mp = args->dp->i_mount; |
361 | struct xfs_trans *tp = args->trans; | |
65b80c98 DC |
362 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
363 | ||
364 | ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC); | |
365 | ||
b16a427a | 366 | if (xfs_has_crc(mp)) { |
65b80c98 DC |
367 | struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; |
368 | ||
369 | memset(leaf3, 0, sizeof(*leaf3)); | |
370 | ||
371 | leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC) | |
372 | ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) | |
373 | : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); | |
f1208396 | 374 | leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp)); |
da833bae | 375 | leaf3->info.owner = cpu_to_be64(args->owner); |
9c4e12fb | 376 | uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid); |
65b80c98 DC |
377 | } else { |
378 | memset(leaf, 0, sizeof(*leaf)); | |
379 | leaf->hdr.info.magic = cpu_to_be16(type); | |
380 | } | |
381 | ||
382 | /* | |
383 | * If it's a leaf-format directory initialize the tail. | |
384 | * Caller is responsible for initialising the bests table. | |
385 | */ | |
386 | if (type == XFS_DIR2_LEAF1_MAGIC) { | |
387 | struct xfs_dir2_leaf_tail *ltp; | |
388 | ||
ff105f75 | 389 | ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf); |
65b80c98 DC |
390 | ltp->bestcount = 0; |
391 | bp->b_ops = &xfs_dir3_leaf1_buf_ops; | |
bdc16ee5 | 392 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF); |
8b4dc4a9 | 393 | } else { |
65b80c98 | 394 | bp->b_ops = &xfs_dir3_leafn_buf_ops; |
bdc16ee5 | 395 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF); |
8b4dc4a9 | 396 | } |
65b80c98 DC |
397 | } |
398 | ||
399 | int | |
400 | xfs_dir3_leaf_get_buf( | |
401 | xfs_da_args_t *args, | |
402 | xfs_dir2_db_t bno, | |
403 | struct xfs_buf **bpp, | |
4a492e72 | 404 | uint16_t magic) |
65b80c98 DC |
405 | { |
406 | struct xfs_inode *dp = args->dp; | |
407 | struct xfs_trans *tp = args->trans; | |
65b80c98 DC |
408 | struct xfs_buf *bp; |
409 | int error; | |
410 | ||
411 | ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC); | |
ff105f75 DC |
412 | ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) && |
413 | bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); | |
65b80c98 | 414 | |
ff105f75 | 415 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno), |
c1d19744 | 416 | &bp, XFS_DATA_FORK); |
65b80c98 DC |
417 | if (error) |
418 | return error; | |
419 | ||
da833bae | 420 | xfs_dir3_leaf_init(args, bp, magic); |
ff105f75 | 421 | xfs_dir3_leaf_log_header(args, bp); |
65b80c98 | 422 | if (magic == XFS_DIR2_LEAF1_MAGIC) |
ff105f75 | 423 | xfs_dir3_leaf_log_tail(args, bp); |
65b80c98 DC |
424 | *bpp = bp; |
425 | return 0; | |
a2ceac1f | 426 | } |
2bd0ea18 NS |
427 | |
428 | /* | |
429 | * Convert a block form directory to a leaf form directory. | |
430 | */ | |
431 | int /* error */ | |
432 | xfs_dir2_block_to_leaf( | |
433 | xfs_da_args_t *args, /* operation arguments */ | |
a2ceac1f | 434 | struct xfs_buf *dbp) /* input block's buffer */ |
2bd0ea18 | 435 | { |
5e656dbb | 436 | __be16 *bestsp; /* leaf's bestsp entries */ |
2bd0ea18 | 437 | xfs_dablk_t blkno; /* leaf block's bno */ |
a2ceac1f | 438 | xfs_dir2_data_hdr_t *hdr; /* block header */ |
2bd0ea18 NS |
439 | xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */ |
440 | xfs_dir2_block_tail_t *btp; /* block's tail */ | |
441 | xfs_inode_t *dp; /* incore directory inode */ | |
442 | int error; /* error return code */ | |
a2ceac1f | 443 | struct xfs_buf *lbp; /* leaf block's buffer */ |
2bd0ea18 NS |
444 | xfs_dir2_db_t ldb; /* leaf block's bno */ |
445 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
446 | xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */ | |
2bd0ea18 NS |
447 | int needlog; /* need to log block header */ |
448 | int needscan; /* need to rescan bestfree */ | |
449 | xfs_trans_t *tp; /* transaction pointer */ | |
90ea28c3 | 450 | struct xfs_dir2_data_free *bf; |
65b80c98 | 451 | struct xfs_dir3_icleaf_hdr leafhdr; |
2bd0ea18 | 452 | |
56b2de80 DC |
453 | trace_xfs_dir2_block_to_leaf(args); |
454 | ||
2bd0ea18 | 455 | dp = args->dp; |
2bd0ea18 NS |
456 | tp = args->trans; |
457 | /* | |
458 | * Add the leaf block to the inode. | |
459 | * This interface will only put blocks in the leaf/node range. | |
460 | * Since that's empty now, we'll get the root (block 0 in range). | |
461 | */ | |
0e266570 | 462 | if ((error = xfs_da_grow_inode(args, &blkno))) { |
2bd0ea18 NS |
463 | return error; |
464 | } | |
ff105f75 DC |
465 | ldb = xfs_dir2_da_to_db(args->geo, blkno); |
466 | ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET)); | |
2bd0ea18 NS |
467 | /* |
468 | * Initialize the leaf block, get a buffer for it. | |
469 | */ | |
65b80c98 DC |
470 | error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC); |
471 | if (error) | |
2bd0ea18 | 472 | return error; |
65b80c98 | 473 | |
a2ceac1f DC |
474 | leaf = lbp->b_addr; |
475 | hdr = dbp->b_addr; | |
90ea28c3 | 476 | xfs_dir3_data_check(dp, dbp); |
ff105f75 | 477 | btp = xfs_dir2_block_tail_p(args->geo, hdr); |
5e656dbb | 478 | blp = xfs_dir2_block_leaf_p(btp); |
04f6f354 | 479 | bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); |
65b80c98 | 480 | |
2bd0ea18 NS |
481 | /* |
482 | * Set the counts in the leaf header. | |
483 | */ | |
9db68faf | 484 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); |
65b80c98 DC |
485 | leafhdr.count = be32_to_cpu(btp->count); |
486 | leafhdr.stale = be32_to_cpu(btp->stale); | |
7adfbcf6 | 487 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); |
ff105f75 | 488 | xfs_dir3_leaf_log_header(args, lbp); |
65b80c98 | 489 | |
2bd0ea18 NS |
490 | /* |
491 | * Could compact these but I think we always do the conversion | |
492 | * after squeezing out stale entries. | |
493 | */ | |
a2279497 CH |
494 | memcpy(leafhdr.ents, blp, |
495 | be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry)); | |
496 | xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1); | |
2bd0ea18 NS |
497 | needscan = 0; |
498 | needlog = 1; | |
499 | /* | |
500 | * Make the space formerly occupied by the leaf entries and block | |
501 | * tail be free. | |
502 | */ | |
ff105f75 | 503 | xfs_dir2_data_make_free(args, dbp, |
a2ceac1f | 504 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), |
ff105f75 | 505 | (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize - |
2bd0ea18 NS |
506 | (char *)blp), |
507 | &needlog, &needscan); | |
508 | /* | |
509 | * Fix up the block header, make it a data block. | |
510 | */ | |
90ea28c3 | 511 | dbp->b_ops = &xfs_dir3_data_buf_ops; |
bdc16ee5 | 512 | xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF); |
90ea28c3 DC |
513 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) |
514 | hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); | |
515 | else | |
516 | hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC); | |
517 | ||
2bd0ea18 | 518 | if (needscan) |
d85595d0 | 519 | xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); |
2bd0ea18 NS |
520 | /* |
521 | * Set up leaf tail and bests table. | |
522 | */ | |
ff105f75 | 523 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
5e656dbb BN |
524 | ltp->bestcount = cpu_to_be32(1); |
525 | bestsp = xfs_dir2_leaf_bests_p(ltp); | |
693fc8f6 | 526 | bestsp[0] = bf[0].length; |
2bd0ea18 NS |
527 | /* |
528 | * Log the data header and leaf bests table. | |
529 | */ | |
530 | if (needlog) | |
ff105f75 DC |
531 | xfs_dir2_data_log_header(args, dbp); |
532 | xfs_dir3_leaf_check(dp, lbp); | |
90ea28c3 | 533 | xfs_dir3_data_check(dp, dbp); |
ff105f75 | 534 | xfs_dir3_leaf_log_bests(args, lbp, 0, 0); |
2bd0ea18 NS |
535 | return 0; |
536 | } | |
537 | ||
a2ceac1f | 538 | STATIC void |
65b80c98 DC |
539 | xfs_dir3_leaf_find_stale( |
540 | struct xfs_dir3_icleaf_hdr *leafhdr, | |
541 | struct xfs_dir2_leaf_entry *ents, | |
a2ceac1f DC |
542 | int index, |
543 | int *lowstale, | |
544 | int *highstale) | |
545 | { | |
546 | /* | |
547 | * Find the first stale entry before our index, if any. | |
548 | */ | |
549 | for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) { | |
65b80c98 | 550 | if (ents[*lowstale].address == |
a2ceac1f DC |
551 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
552 | break; | |
553 | } | |
554 | ||
555 | /* | |
556 | * Find the first stale entry at or after our index, if any. | |
557 | * Stop if the result would require moving more entries than using | |
558 | * lowstale. | |
559 | */ | |
65b80c98 DC |
560 | for (*highstale = index; *highstale < leafhdr->count; ++*highstale) { |
561 | if (ents[*highstale].address == | |
a2ceac1f DC |
562 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
563 | break; | |
564 | if (*lowstale >= 0 && index - *lowstale <= *highstale - index) | |
565 | break; | |
566 | } | |
567 | } | |
568 | ||
569 | struct xfs_dir2_leaf_entry * | |
65b80c98 DC |
570 | xfs_dir3_leaf_find_entry( |
571 | struct xfs_dir3_icleaf_hdr *leafhdr, | |
572 | struct xfs_dir2_leaf_entry *ents, | |
a2ceac1f DC |
573 | int index, /* leaf table position */ |
574 | int compact, /* need to compact leaves */ | |
575 | int lowstale, /* index of prev stale leaf */ | |
576 | int highstale, /* index of next stale leaf */ | |
577 | int *lfloglow, /* low leaf logging index */ | |
578 | int *lfloghigh) /* high leaf logging index */ | |
579 | { | |
65b80c98 | 580 | if (!leafhdr->stale) { |
a2ceac1f DC |
581 | xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */ |
582 | ||
583 | /* | |
584 | * Now we need to make room to insert the leaf entry. | |
585 | * | |
586 | * If there are no stale entries, just insert a hole at index. | |
587 | */ | |
65b80c98 DC |
588 | lep = &ents[index]; |
589 | if (index < leafhdr->count) | |
a2ceac1f | 590 | memmove(lep + 1, lep, |
65b80c98 | 591 | (leafhdr->count - index) * sizeof(*lep)); |
a2ceac1f DC |
592 | |
593 | /* | |
594 | * Record low and high logging indices for the leaf. | |
595 | */ | |
596 | *lfloglow = index; | |
65b80c98 | 597 | *lfloghigh = leafhdr->count++; |
a2ceac1f DC |
598 | return lep; |
599 | } | |
600 | ||
601 | /* | |
602 | * There are stale entries. | |
603 | * | |
604 | * We will use one of them for the new entry. It's probably not at | |
605 | * the right location, so we'll have to shift some up or down first. | |
606 | * | |
607 | * If we didn't compact before, we need to find the nearest stale | |
608 | * entries before and after our insertion point. | |
609 | */ | |
610 | if (compact == 0) | |
65b80c98 DC |
611 | xfs_dir3_leaf_find_stale(leafhdr, ents, index, |
612 | &lowstale, &highstale); | |
a2ceac1f DC |
613 | |
614 | /* | |
615 | * If the low one is better, use it. | |
616 | */ | |
617 | if (lowstale >= 0 && | |
65b80c98 | 618 | (highstale == leafhdr->count || |
a2ceac1f DC |
619 | index - lowstale - 1 < highstale - index)) { |
620 | ASSERT(index - lowstale - 1 >= 0); | |
65b80c98 | 621 | ASSERT(ents[lowstale].address == |
a2ceac1f DC |
622 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)); |
623 | ||
624 | /* | |
625 | * Copy entries up to cover the stale entry and make room | |
626 | * for the new entry. | |
627 | */ | |
628 | if (index - lowstale - 1 > 0) { | |
65b80c98 | 629 | memmove(&ents[lowstale], &ents[lowstale + 1], |
a2ceac1f | 630 | (index - lowstale - 1) * |
65b80c98 | 631 | sizeof(xfs_dir2_leaf_entry_t)); |
a2ceac1f | 632 | } |
d7ff12b8 DC |
633 | *lfloglow = min(lowstale, *lfloglow); |
634 | *lfloghigh = max(index - 1, *lfloghigh); | |
65b80c98 DC |
635 | leafhdr->stale--; |
636 | return &ents[index - 1]; | |
a2ceac1f DC |
637 | } |
638 | ||
639 | /* | |
640 | * The high one is better, so use that one. | |
641 | */ | |
642 | ASSERT(highstale - index >= 0); | |
65b80c98 | 643 | ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)); |
a2ceac1f DC |
644 | |
645 | /* | |
646 | * Copy entries down to cover the stale entry and make room for the | |
647 | * new entry. | |
648 | */ | |
649 | if (highstale - index > 0) { | |
65b80c98 | 650 | memmove(&ents[index + 1], &ents[index], |
a2ceac1f DC |
651 | (highstale - index) * sizeof(xfs_dir2_leaf_entry_t)); |
652 | } | |
d7ff12b8 DC |
653 | *lfloglow = min(index, *lfloglow); |
654 | *lfloghigh = max(highstale, *lfloghigh); | |
65b80c98 DC |
655 | leafhdr->stale--; |
656 | return &ents[index]; | |
a2ceac1f DC |
657 | } |
658 | ||
2bd0ea18 NS |
659 | /* |
660 | * Add an entry to a leaf form directory. | |
661 | */ | |
662 | int /* error */ | |
663 | xfs_dir2_leaf_addname( | |
c01c6d62 | 664 | struct xfs_da_args *args) /* operation arguments */ |
2bd0ea18 | 665 | { |
c01c6d62 DW |
666 | struct xfs_dir3_icleaf_hdr leafhdr; |
667 | struct xfs_trans *tp = args->trans; | |
5e656dbb | 668 | __be16 *bestsp; /* freespace table in leaf */ |
c01c6d62 | 669 | __be16 *tagp; /* end of data entry */ |
a2ceac1f | 670 | struct xfs_buf *dbp; /* data block buffer */ |
c01c6d62 DW |
671 | struct xfs_buf *lbp; /* leaf's buffer */ |
672 | struct xfs_dir2_leaf *leaf; /* leaf structure */ | |
673 | struct xfs_inode *dp = args->dp; /* incore directory inode */ | |
674 | struct xfs_dir2_data_hdr *hdr; /* data block header */ | |
675 | struct xfs_dir2_data_entry *dep; /* data block entry */ | |
676 | struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */ | |
677 | struct xfs_dir2_leaf_entry *ents; | |
678 | struct xfs_dir2_data_unused *dup; /* data unused entry */ | |
679 | struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */ | |
680 | struct xfs_dir2_data_free *bf; /* bestfree table */ | |
681 | int compact; /* need to compact leaves */ | |
2bd0ea18 NS |
682 | int error; /* error return value */ |
683 | int grown; /* allocated new data block */ | |
732a9d49 | 684 | int highstale = 0; /* index of next stale leaf */ |
2bd0ea18 NS |
685 | int i; /* temporary, index */ |
686 | int index; /* leaf table position */ | |
2bd0ea18 | 687 | int length; /* length of new entry */ |
2bd0ea18 NS |
688 | int lfloglow; /* low leaf logging index */ |
689 | int lfloghigh; /* high leaf logging index */ | |
732a9d49 | 690 | int lowstale = 0; /* index of prev stale leaf */ |
2bd0ea18 NS |
691 | int needbytes; /* leaf block bytes needed */ |
692 | int needlog; /* need to log data header */ | |
693 | int needscan; /* need to rescan data free */ | |
2bd0ea18 NS |
694 | xfs_dir2_db_t use_block; /* data block number */ |
695 | ||
56b2de80 DC |
696 | trace_xfs_dir2_leaf_addname(args); |
697 | ||
17e0d66d DW |
698 | error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk, |
699 | &lbp); | |
a2ceac1f | 700 | if (error) |
2bd0ea18 | 701 | return error; |
a2ceac1f | 702 | |
2bd0ea18 NS |
703 | /* |
704 | * Look up the entry by hash value and name. | |
705 | * We know it's not there, our caller has already done a lookup. | |
706 | * So the index is of the entry to insert in front of. | |
707 | * But if there are dup hash values the index is of the first of those. | |
708 | */ | |
709 | index = xfs_dir2_leaf_search_hash(args, lbp); | |
a2ceac1f | 710 | leaf = lbp->b_addr; |
ff105f75 | 711 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
9db68faf | 712 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); |
a2279497 | 713 | ents = leafhdr.ents; |
5e656dbb | 714 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
271a654f | 715 | length = xfs_dir2_data_entsize(dp->i_mount, args->namelen); |
65b80c98 | 716 | |
2bd0ea18 NS |
717 | /* |
718 | * See if there are any entries with the same hash value | |
719 | * and space in their block for the new entry. | |
720 | * This is good because it puts multiple same-hash value entries | |
721 | * in a data block, improving the lookup of those entries. | |
722 | */ | |
65b80c98 DC |
723 | for (use_block = -1, lep = &ents[index]; |
724 | index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; | |
2bd0ea18 | 725 | index++, lep++) { |
5e656dbb | 726 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
2bd0ea18 | 727 | continue; |
ff105f75 | 728 | i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); |
5e656dbb | 729 | ASSERT(i < be32_to_cpu(ltp->bestcount)); |
a2ceac1f | 730 | ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF)); |
5e656dbb | 731 | if (be16_to_cpu(bestsp[i]) >= length) { |
2bd0ea18 NS |
732 | use_block = i; |
733 | break; | |
734 | } | |
735 | } | |
736 | /* | |
737 | * Didn't find a block yet, linear search all the data blocks. | |
738 | */ | |
739 | if (use_block == -1) { | |
5e656dbb | 740 | for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) { |
2bd0ea18 NS |
741 | /* |
742 | * Remember a block we see that's missing. | |
743 | */ | |
a2ceac1f DC |
744 | if (bestsp[i] == cpu_to_be16(NULLDATAOFF) && |
745 | use_block == -1) | |
2bd0ea18 | 746 | use_block = i; |
5e656dbb | 747 | else if (be16_to_cpu(bestsp[i]) >= length) { |
2bd0ea18 NS |
748 | use_block = i; |
749 | break; | |
750 | } | |
751 | } | |
752 | } | |
753 | /* | |
754 | * How many bytes do we need in the leaf block? | |
755 | */ | |
a2ceac1f | 756 | needbytes = 0; |
65b80c98 | 757 | if (!leafhdr.stale) |
a2ceac1f DC |
758 | needbytes += sizeof(xfs_dir2_leaf_entry_t); |
759 | if (use_block == -1) | |
760 | needbytes += sizeof(xfs_dir2_data_off_t); | |
761 | ||
2bd0ea18 NS |
762 | /* |
763 | * Now kill use_block if it refers to a missing block, so we | |
764 | * can use it as an indication of allocation needed. | |
765 | */ | |
a2ceac1f | 766 | if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF)) |
2bd0ea18 NS |
767 | use_block = -1; |
768 | /* | |
769 | * If we don't have enough free bytes but we can make enough | |
770 | * by compacting out stale entries, we'll do that. | |
771 | */ | |
65b80c98 DC |
772 | if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes && |
773 | leafhdr.stale > 1) | |
2bd0ea18 | 774 | compact = 1; |
65b80c98 | 775 | |
2bd0ea18 NS |
776 | /* |
777 | * Otherwise if we don't have enough free bytes we need to | |
778 | * convert to node form. | |
779 | */ | |
65b80c98 | 780 | else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) { |
2bd0ea18 NS |
781 | /* |
782 | * Just checking or no space reservation, give up. | |
783 | */ | |
5e656dbb BN |
784 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || |
785 | args->total == 0) { | |
a2ceac1f | 786 | xfs_trans_brelse(tp, lbp); |
12b53197 | 787 | return -ENOSPC; |
2bd0ea18 NS |
788 | } |
789 | /* | |
790 | * Convert to node form. | |
791 | */ | |
792 | error = xfs_dir2_leaf_to_node(args, lbp); | |
2bd0ea18 NS |
793 | if (error) |
794 | return error; | |
795 | /* | |
796 | * Then add the new entry. | |
797 | */ | |
798 | return xfs_dir2_node_addname(args); | |
799 | } | |
800 | /* | |
801 | * Otherwise it will fit without compaction. | |
802 | */ | |
803 | else | |
804 | compact = 0; | |
805 | /* | |
806 | * If just checking, then it will fit unless we needed to allocate | |
807 | * a new data block. | |
808 | */ | |
5e656dbb | 809 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
a2ceac1f | 810 | xfs_trans_brelse(tp, lbp); |
12b53197 | 811 | return use_block == -1 ? -ENOSPC : 0; |
2bd0ea18 NS |
812 | } |
813 | /* | |
814 | * If no allocations are allowed, return now before we've | |
815 | * changed anything. | |
816 | */ | |
817 | if (args->total == 0 && use_block == -1) { | |
a2ceac1f | 818 | xfs_trans_brelse(tp, lbp); |
12b53197 | 819 | return -ENOSPC; |
2bd0ea18 NS |
820 | } |
821 | /* | |
822 | * Need to compact the leaf entries, removing stale ones. | |
823 | * Leave one stale entry behind - the one closest to our | |
824 | * insertion index - and we'll shift that one to our insertion | |
825 | * point later. | |
826 | */ | |
827 | if (compact) { | |
65b80c98 DC |
828 | xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, |
829 | &highstale, &lfloglow, &lfloghigh); | |
2bd0ea18 NS |
830 | } |
831 | /* | |
832 | * There are stale entries, so we'll need log-low and log-high | |
833 | * impossibly bad values later. | |
834 | */ | |
65b80c98 DC |
835 | else if (leafhdr.stale) { |
836 | lfloglow = leafhdr.count; | |
2bd0ea18 NS |
837 | lfloghigh = -1; |
838 | } | |
839 | /* | |
840 | * If there was no data block space found, we need to allocate | |
841 | * a new one. | |
842 | */ | |
843 | if (use_block == -1) { | |
2bd0ea18 NS |
844 | /* |
845 | * Add the new data block. | |
846 | */ | |
0e266570 NS |
847 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, |
848 | &use_block))) { | |
a2ceac1f | 849 | xfs_trans_brelse(tp, lbp); |
2bd0ea18 NS |
850 | return error; |
851 | } | |
852 | /* | |
853 | * Initialize the block. | |
854 | */ | |
693fc8f6 | 855 | if ((error = xfs_dir3_data_init(args, use_block, &dbp))) { |
a2ceac1f | 856 | xfs_trans_brelse(tp, lbp); |
2bd0ea18 NS |
857 | return error; |
858 | } | |
859 | /* | |
860 | * If we're adding a new data block on the end we need to | |
861 | * extend the bests table. Copy it up one entry. | |
862 | */ | |
5e656dbb | 863 | if (use_block >= be32_to_cpu(ltp->bestcount)) { |
2bd0ea18 | 864 | bestsp--; |
32181a02 | 865 | memmove(&bestsp[0], &bestsp[1], |
5e656dbb BN |
866 | be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0])); |
867 | be32_add_cpu(<p->bestcount, 1); | |
ff105f75 DC |
868 | xfs_dir3_leaf_log_tail(args, lbp); |
869 | xfs_dir3_leaf_log_bests(args, lbp, 0, | |
870 | be32_to_cpu(ltp->bestcount) - 1); | |
2bd0ea18 NS |
871 | } |
872 | /* | |
873 | * If we're filling in a previously empty block just log it. | |
874 | */ | |
875 | else | |
ff105f75 | 876 | xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block); |
a2ceac1f | 877 | hdr = dbp->b_addr; |
04f6f354 | 878 | bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); |
90ea28c3 | 879 | bestsp[use_block] = bf[0].length; |
2bd0ea18 | 880 | grown = 1; |
a2ceac1f DC |
881 | } else { |
882 | /* | |
883 | * Already had space in some data block. | |
884 | * Just read that one in. | |
885 | */ | |
72386ff0 DW |
886 | error = xfs_dir3_data_read(tp, dp, args->owner, |
887 | xfs_dir2_db_to_da(args->geo, use_block), 0, | |
888 | &dbp); | |
a2ceac1f DC |
889 | if (error) { |
890 | xfs_trans_brelse(tp, lbp); | |
2bd0ea18 NS |
891 | return error; |
892 | } | |
a2ceac1f | 893 | hdr = dbp->b_addr; |
04f6f354 | 894 | bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); |
2bd0ea18 NS |
895 | grown = 0; |
896 | } | |
2bd0ea18 NS |
897 | /* |
898 | * Point to the biggest freespace in our data block. | |
899 | */ | |
900 | dup = (xfs_dir2_data_unused_t *) | |
90ea28c3 | 901 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
2bd0ea18 NS |
902 | needscan = needlog = 0; |
903 | /* | |
904 | * Mark the initial part of our freespace in use for the new entry. | |
905 | */ | |
15348e04 DW |
906 | error = xfs_dir2_data_use_free(args, dbp, dup, |
907 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), | |
908 | length, &needlog, &needscan); | |
909 | if (error) { | |
910 | xfs_trans_brelse(tp, lbp); | |
911 | return error; | |
912 | } | |
2bd0ea18 NS |
913 | /* |
914 | * Initialize our new entry (at last). | |
915 | */ | |
916 | dep = (xfs_dir2_data_entry_t *)dup; | |
5e656dbb | 917 | dep->inumber = cpu_to_be64(args->inumber); |
2bd0ea18 | 918 | dep->namelen = args->namelen; |
32181a02 | 919 | memcpy(dep->name, args->name, dep->namelen); |
28e6e614 | 920 | xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype); |
823711f2 | 921 | tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep); |
a2ceac1f | 922 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
2bd0ea18 NS |
923 | /* |
924 | * Need to scan fix up the bestfree table. | |
925 | */ | |
926 | if (needscan) | |
d85595d0 | 927 | xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); |
2bd0ea18 NS |
928 | /* |
929 | * Need to log the data block's header. | |
930 | */ | |
931 | if (needlog) | |
ff105f75 DC |
932 | xfs_dir2_data_log_header(args, dbp); |
933 | xfs_dir2_data_log_entry(args, dbp, dep); | |
2bd0ea18 NS |
934 | /* |
935 | * If the bests table needs to be changed, do it. | |
936 | * Log the change unless we've already done that. | |
937 | */ | |
90ea28c3 DC |
938 | if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) { |
939 | bestsp[use_block] = bf[0].length; | |
2bd0ea18 | 940 | if (!grown) |
ff105f75 | 941 | xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block); |
2bd0ea18 | 942 | } |
a2ceac1f | 943 | |
65b80c98 | 944 | lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, |
a2ceac1f DC |
945 | highstale, &lfloglow, &lfloghigh); |
946 | ||
2bd0ea18 NS |
947 | /* |
948 | * Fill in the new leaf entry. | |
949 | */ | |
5e656dbb | 950 | lep->hashval = cpu_to_be32(args->hashval); |
ff105f75 DC |
951 | lep->address = cpu_to_be32( |
952 | xfs_dir2_db_off_to_dataptr(args->geo, use_block, | |
5e656dbb | 953 | be16_to_cpu(*tagp))); |
2bd0ea18 NS |
954 | /* |
955 | * Log the leaf fields and give up the buffers. | |
956 | */ | |
7adfbcf6 | 957 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); |
ff105f75 | 958 | xfs_dir3_leaf_log_header(args, lbp); |
a2279497 | 959 | xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh); |
ff105f75 | 960 | xfs_dir3_leaf_check(dp, lbp); |
90ea28c3 | 961 | xfs_dir3_data_check(dp, dbp); |
2bd0ea18 NS |
962 | return 0; |
963 | } | |
964 | ||
2bd0ea18 NS |
965 | /* |
966 | * Compact out any stale entries in the leaf. | |
967 | * Log the header and changed leaf entries, if any. | |
968 | */ | |
969 | void | |
65b80c98 | 970 | xfs_dir3_leaf_compact( |
2bd0ea18 | 971 | xfs_da_args_t *args, /* operation arguments */ |
65b80c98 | 972 | struct xfs_dir3_icleaf_hdr *leafhdr, |
a2ceac1f | 973 | struct xfs_buf *bp) /* leaf buffer */ |
2bd0ea18 NS |
974 | { |
975 | int from; /* source leaf index */ | |
dfc130f3 | 976 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
2bd0ea18 NS |
977 | int loglow; /* first leaf entry to log */ |
978 | int to; /* target leaf index */ | |
ff105f75 | 979 | struct xfs_inode *dp = args->dp; |
2bd0ea18 | 980 | |
a2ceac1f | 981 | leaf = bp->b_addr; |
65b80c98 | 982 | if (!leafhdr->stale) |
2bd0ea18 | 983 | return; |
65b80c98 | 984 | |
2bd0ea18 NS |
985 | /* |
986 | * Compress out the stale entries in place. | |
987 | */ | |
65b80c98 | 988 | for (from = to = 0, loglow = -1; from < leafhdr->count; from++) { |
a2279497 CH |
989 | if (leafhdr->ents[from].address == |
990 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | |
2bd0ea18 NS |
991 | continue; |
992 | /* | |
993 | * Only actually copy the entries that are different. | |
994 | */ | |
995 | if (from > to) { | |
996 | if (loglow == -1) | |
997 | loglow = to; | |
a2279497 | 998 | leafhdr->ents[to] = leafhdr->ents[from]; |
2bd0ea18 NS |
999 | } |
1000 | to++; | |
1001 | } | |
1002 | /* | |
1003 | * Update and log the header, log the leaf entries. | |
1004 | */ | |
65b80c98 DC |
1005 | ASSERT(leafhdr->stale == from - to); |
1006 | leafhdr->count -= leafhdr->stale; | |
1007 | leafhdr->stale = 0; | |
1008 | ||
7adfbcf6 | 1009 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr); |
ff105f75 | 1010 | xfs_dir3_leaf_log_header(args, bp); |
2bd0ea18 | 1011 | if (loglow != -1) |
a2279497 | 1012 | xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1); |
2bd0ea18 NS |
1013 | } |
1014 | ||
1015 | /* | |
1016 | * Compact the leaf entries, removing stale ones. | |
1017 | * Leave one stale entry behind - the one closest to our | |
1018 | * insertion index - and the caller will shift that one to our insertion | |
1019 | * point later. | |
1020 | * Return new insertion index, where the remaining stale entry is, | |
1021 | * and leaf logging indices. | |
1022 | */ | |
1023 | void | |
65b80c98 DC |
1024 | xfs_dir3_leaf_compact_x1( |
1025 | struct xfs_dir3_icleaf_hdr *leafhdr, | |
1026 | struct xfs_dir2_leaf_entry *ents, | |
2bd0ea18 NS |
1027 | int *indexp, /* insertion index */ |
1028 | int *lowstalep, /* out: stale entry before us */ | |
1029 | int *highstalep, /* out: stale entry after us */ | |
1030 | int *lowlogp, /* out: low log index */ | |
1031 | int *highlogp) /* out: high log index */ | |
1032 | { | |
1033 | int from; /* source copy index */ | |
1034 | int highstale; /* stale entry at/after index */ | |
1035 | int index; /* insertion index */ | |
1036 | int keepstale; /* source index of kept stale */ | |
2bd0ea18 | 1037 | int lowstale; /* stale entry before index */ |
0e266570 | 1038 | int newindex=0; /* new insertion index */ |
2bd0ea18 NS |
1039 | int to; /* destination copy index */ |
1040 | ||
65b80c98 | 1041 | ASSERT(leafhdr->stale > 1); |
2bd0ea18 | 1042 | index = *indexp; |
a2ceac1f | 1043 | |
65b80c98 | 1044 | xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale); |
a2ceac1f | 1045 | |
2bd0ea18 NS |
1046 | /* |
1047 | * Pick the better of lowstale and highstale. | |
1048 | */ | |
1049 | if (lowstale >= 0 && | |
65b80c98 | 1050 | (highstale == leafhdr->count || |
2bd0ea18 NS |
1051 | index - lowstale <= highstale - index)) |
1052 | keepstale = lowstale; | |
1053 | else | |
1054 | keepstale = highstale; | |
1055 | /* | |
1056 | * Copy the entries in place, removing all the stale entries | |
1057 | * except keepstale. | |
1058 | */ | |
65b80c98 | 1059 | for (from = to = 0; from < leafhdr->count; from++) { |
2bd0ea18 NS |
1060 | /* |
1061 | * Notice the new value of index. | |
1062 | */ | |
1063 | if (index == from) | |
1064 | newindex = to; | |
1065 | if (from != keepstale && | |
65b80c98 | 1066 | ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { |
2bd0ea18 NS |
1067 | if (from == to) |
1068 | *lowlogp = to; | |
1069 | continue; | |
1070 | } | |
1071 | /* | |
1072 | * Record the new keepstale value for the insertion. | |
1073 | */ | |
1074 | if (from == keepstale) | |
1075 | lowstale = highstale = to; | |
1076 | /* | |
1077 | * Copy only the entries that have moved. | |
1078 | */ | |
1079 | if (from > to) | |
65b80c98 | 1080 | ents[to] = ents[from]; |
2bd0ea18 NS |
1081 | to++; |
1082 | } | |
1083 | ASSERT(from > to); | |
1084 | /* | |
1085 | * If the insertion point was past the last entry, | |
1086 | * set the new insertion point accordingly. | |
1087 | */ | |
1088 | if (index == from) | |
1089 | newindex = to; | |
1090 | *indexp = newindex; | |
1091 | /* | |
1092 | * Adjust the leaf header values. | |
1093 | */ | |
65b80c98 DC |
1094 | leafhdr->count -= from - to; |
1095 | leafhdr->stale = 1; | |
2bd0ea18 NS |
1096 | /* |
1097 | * Remember the low/high stale value only in the "right" | |
1098 | * direction. | |
1099 | */ | |
1100 | if (lowstale >= newindex) | |
1101 | lowstale = -1; | |
1102 | else | |
65b80c98 DC |
1103 | highstale = leafhdr->count; |
1104 | *highlogp = leafhdr->count - 1; | |
2bd0ea18 NS |
1105 | *lowstalep = lowstale; |
1106 | *highstalep = highstale; | |
1107 | } | |
1108 | ||
2bd0ea18 NS |
1109 | /* |
1110 | * Log the bests entries indicated from a leaf1 block. | |
1111 | */ | |
5e656dbb | 1112 | static void |
65b80c98 | 1113 | xfs_dir3_leaf_log_bests( |
ff105f75 | 1114 | struct xfs_da_args *args, |
a2ceac1f | 1115 | struct xfs_buf *bp, /* leaf buffer */ |
2bd0ea18 NS |
1116 | int first, /* first entry to log */ |
1117 | int last) /* last entry to log */ | |
1118 | { | |
5e656dbb BN |
1119 | __be16 *firstb; /* pointer to first entry */ |
1120 | __be16 *lastb; /* pointer to last entry */ | |
65b80c98 | 1121 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
2bd0ea18 NS |
1122 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
1123 | ||
65b80c98 DC |
1124 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || |
1125 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)); | |
1126 | ||
ff105f75 | 1127 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
5e656dbb BN |
1128 | firstb = xfs_dir2_leaf_bests_p(ltp) + first; |
1129 | lastb = xfs_dir2_leaf_bests_p(ltp) + last; | |
ff105f75 DC |
1130 | xfs_trans_log_buf(args->trans, bp, |
1131 | (uint)((char *)firstb - (char *)leaf), | |
2bd0ea18 NS |
1132 | (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1)); |
1133 | } | |
1134 | ||
1135 | /* | |
1136 | * Log the leaf entries indicated from a leaf1 or leafn block. | |
1137 | */ | |
1138 | void | |
65b80c98 | 1139 | xfs_dir3_leaf_log_ents( |
ff105f75 | 1140 | struct xfs_da_args *args, |
a2279497 | 1141 | struct xfs_dir3_icleaf_hdr *hdr, |
ff105f75 DC |
1142 | struct xfs_buf *bp, |
1143 | int first, | |
1144 | int last) | |
2bd0ea18 NS |
1145 | { |
1146 | xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */ | |
1147 | xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */ | |
65b80c98 | 1148 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
2bd0ea18 | 1149 | |
a2ceac1f | 1150 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || |
65b80c98 DC |
1151 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || |
1152 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || | |
1153 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); | |
1154 | ||
a2279497 CH |
1155 | firstlep = &hdr->ents[first]; |
1156 | lastlep = &hdr->ents[last]; | |
ff105f75 DC |
1157 | xfs_trans_log_buf(args->trans, bp, |
1158 | (uint)((char *)firstlep - (char *)leaf), | |
2bd0ea18 NS |
1159 | (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1)); |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * Log the header of the leaf1 or leafn block. | |
1164 | */ | |
1165 | void | |
65b80c98 | 1166 | xfs_dir3_leaf_log_header( |
ff105f75 | 1167 | struct xfs_da_args *args, |
a2ceac1f | 1168 | struct xfs_buf *bp) |
2bd0ea18 | 1169 | { |
65b80c98 | 1170 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
2bd0ea18 | 1171 | |
a2ceac1f | 1172 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || |
65b80c98 DC |
1173 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || |
1174 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || | |
1175 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); | |
1176 | ||
ff105f75 DC |
1177 | xfs_trans_log_buf(args->trans, bp, |
1178 | (uint)((char *)&leaf->hdr - (char *)leaf), | |
99e7b975 | 1179 | args->geo->leaf_hdr_size - 1); |
2bd0ea18 NS |
1180 | } |
1181 | ||
1182 | /* | |
1183 | * Log the tail of the leaf1 block. | |
1184 | */ | |
5e656dbb | 1185 | STATIC void |
65b80c98 | 1186 | xfs_dir3_leaf_log_tail( |
ff105f75 | 1187 | struct xfs_da_args *args, |
a2ceac1f | 1188 | struct xfs_buf *bp) |
2bd0ea18 | 1189 | { |
65b80c98 | 1190 | struct xfs_dir2_leaf *leaf = bp->b_addr; |
2bd0ea18 | 1191 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
65b80c98 DC |
1192 | |
1193 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) || | |
1194 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) || | |
1195 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) || | |
1196 | leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)); | |
2bd0ea18 | 1197 | |
ff105f75 DC |
1198 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
1199 | xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf), | |
1200 | (uint)(args->geo->blksize - 1)); | |
2bd0ea18 NS |
1201 | } |
1202 | ||
1203 | /* | |
1204 | * Look up the entry referred to by args in the leaf format directory. | |
1205 | * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which | |
1206 | * is also used by the node-format code. | |
1207 | */ | |
1208 | int | |
1209 | xfs_dir2_leaf_lookup( | |
1210 | xfs_da_args_t *args) /* operation arguments */ | |
1211 | { | |
a2ceac1f | 1212 | struct xfs_buf *dbp; /* data block buffer */ |
2bd0ea18 NS |
1213 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
1214 | xfs_inode_t *dp; /* incore directory inode */ | |
1215 | int error; /* error return code */ | |
1216 | int index; /* found entry index */ | |
a2ceac1f | 1217 | struct xfs_buf *lbp; /* leaf buffer */ |
2bd0ea18 NS |
1218 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
1219 | xfs_trans_t *tp; /* transaction pointer */ | |
a2279497 | 1220 | struct xfs_dir3_icleaf_hdr leafhdr; |
2bd0ea18 | 1221 | |
56b2de80 DC |
1222 | trace_xfs_dir2_leaf_lookup(args); |
1223 | ||
2bd0ea18 NS |
1224 | /* |
1225 | * Look up name in the leaf block, returning both buffers and index. | |
1226 | */ | |
a2279497 CH |
1227 | error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr); |
1228 | if (error) | |
2bd0ea18 | 1229 | return error; |
a2279497 | 1230 | |
2bd0ea18 NS |
1231 | tp = args->trans; |
1232 | dp = args->dp; | |
ff105f75 | 1233 | xfs_dir3_leaf_check(dp, lbp); |
a2279497 | 1234 | |
2bd0ea18 NS |
1235 | /* |
1236 | * Get to the leaf entry and contained data entry address. | |
1237 | */ | |
a2279497 | 1238 | lep = &leafhdr.ents[index]; |
65b80c98 | 1239 | |
2bd0ea18 NS |
1240 | /* |
1241 | * Point to the data entry. | |
1242 | */ | |
1243 | dep = (xfs_dir2_data_entry_t *) | |
a2ceac1f | 1244 | ((char *)dbp->b_addr + |
ff105f75 | 1245 | xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address))); |
2bd0ea18 | 1246 | /* |
5e656dbb | 1247 | * Return the found inode number & CI name if appropriate |
2bd0ea18 | 1248 | */ |
5e656dbb | 1249 | args->inumber = be64_to_cpu(dep->inumber); |
28e6e614 | 1250 | args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep); |
5e656dbb | 1251 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
a2ceac1f DC |
1252 | xfs_trans_brelse(tp, dbp); |
1253 | xfs_trans_brelse(tp, lbp); | |
1e68581b | 1254 | return error; |
2bd0ea18 NS |
1255 | } |
1256 | ||
1257 | /* | |
1258 | * Look up name/hash in the leaf block. | |
1259 | * Fill in indexp with the found index, and dbpp with the data buffer. | |
1260 | * If not found dbpp will be NULL, and ENOENT comes back. | |
1261 | * lbpp will always be filled in with the leaf buffer unless there's an error. | |
1262 | */ | |
5e656dbb | 1263 | static int /* error */ |
2bd0ea18 NS |
1264 | xfs_dir2_leaf_lookup_int( |
1265 | xfs_da_args_t *args, /* operation arguments */ | |
a2ceac1f | 1266 | struct xfs_buf **lbpp, /* out: leaf buffer */ |
2bd0ea18 | 1267 | int *indexp, /* out: index in leaf block */ |
a2279497 CH |
1268 | struct xfs_buf **dbpp, /* out: data buffer */ |
1269 | struct xfs_dir3_icleaf_hdr *leafhdr) | |
2bd0ea18 | 1270 | { |
5e656dbb | 1271 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
a2ceac1f | 1272 | struct xfs_buf *dbp = NULL; /* data buffer */ |
2bd0ea18 NS |
1273 | xfs_dir2_data_entry_t *dep; /* data entry */ |
1274 | xfs_inode_t *dp; /* incore directory inode */ | |
1275 | int error; /* error return code */ | |
1276 | int index; /* index in leaf block */ | |
a2ceac1f | 1277 | struct xfs_buf *lbp; /* leaf buffer */ |
2bd0ea18 NS |
1278 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
1279 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1280 | xfs_mount_t *mp; /* filesystem mount point */ | |
1281 | xfs_dir2_db_t newdb; /* new data block number */ | |
1282 | xfs_trans_t *tp; /* transaction pointer */ | |
5e656dbb BN |
1283 | xfs_dir2_db_t cidb = -1; /* case match data block no. */ |
1284 | enum xfs_dacmp cmp; /* name compare result */ | |
2bd0ea18 NS |
1285 | |
1286 | dp = args->dp; | |
1287 | tp = args->trans; | |
1288 | mp = dp->i_mount; | |
a2ceac1f | 1289 | |
17e0d66d DW |
1290 | error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk, |
1291 | &lbp); | |
5e656dbb | 1292 | if (error) |
2bd0ea18 | 1293 | return error; |
a2ceac1f | 1294 | |
2bd0ea18 | 1295 | *lbpp = lbp; |
a2ceac1f | 1296 | leaf = lbp->b_addr; |
ff105f75 | 1297 | xfs_dir3_leaf_check(dp, lbp); |
a2279497 | 1298 | xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf); |
65b80c98 | 1299 | |
2bd0ea18 NS |
1300 | /* |
1301 | * Look for the first leaf entry with our hash value. | |
1302 | */ | |
1303 | index = xfs_dir2_leaf_search_hash(args, lbp); | |
1304 | /* | |
1305 | * Loop over all the entries with the right hash value | |
1306 | * looking to match the name. | |
1307 | */ | |
a2279497 CH |
1308 | for (lep = &leafhdr->ents[index]; |
1309 | index < leafhdr->count && | |
1310 | be32_to_cpu(lep->hashval) == args->hashval; | |
65b80c98 | 1311 | lep++, index++) { |
2bd0ea18 NS |
1312 | /* |
1313 | * Skip over stale leaf entries. | |
1314 | */ | |
5e656dbb | 1315 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
2bd0ea18 NS |
1316 | continue; |
1317 | /* | |
1318 | * Get the new data block number. | |
1319 | */ | |
ff105f75 DC |
1320 | newdb = xfs_dir2_dataptr_to_db(args->geo, |
1321 | be32_to_cpu(lep->address)); | |
2bd0ea18 NS |
1322 | /* |
1323 | * If it's not the same as the old data block number, | |
1324 | * need to pitch the old one and read the new one. | |
1325 | */ | |
1326 | if (newdb != curdb) { | |
1327 | if (dbp) | |
a2ceac1f | 1328 | xfs_trans_brelse(tp, dbp); |
72386ff0 DW |
1329 | error = xfs_dir3_data_read(tp, dp, args->owner, |
1330 | xfs_dir2_db_to_da(args->geo, newdb), 0, | |
1331 | &dbp); | |
5e656dbb | 1332 | if (error) { |
a2ceac1f | 1333 | xfs_trans_brelse(tp, lbp); |
2bd0ea18 NS |
1334 | return error; |
1335 | } | |
2bd0ea18 NS |
1336 | curdb = newdb; |
1337 | } | |
1338 | /* | |
1339 | * Point to the data entry. | |
1340 | */ | |
a2ceac1f | 1341 | dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr + |
ff105f75 DC |
1342 | xfs_dir2_dataptr_to_off(args->geo, |
1343 | be32_to_cpu(lep->address))); | |
2bd0ea18 | 1344 | /* |
5e656dbb BN |
1345 | * Compare name and if it's an exact match, return the index |
1346 | * and buffer. If it's the first case-insensitive match, store | |
1347 | * the index and buffer and continue looking for an exact match. | |
2bd0ea18 | 1348 | */ |
e169cc9b | 1349 | cmp = xfs_dir2_compname(args, dep->name, dep->namelen); |
5e656dbb BN |
1350 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { |
1351 | args->cmpresult = cmp; | |
2bd0ea18 | 1352 | *indexp = index; |
5e656dbb BN |
1353 | /* case exact match: return the current buffer. */ |
1354 | if (cmp == XFS_CMP_EXACT) { | |
1355 | *dbpp = dbp; | |
1356 | return 0; | |
1357 | } | |
1358 | cidb = curdb; | |
2bd0ea18 NS |
1359 | } |
1360 | } | |
5e656dbb BN |
1361 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
1362 | /* | |
1363 | * Here, we can only be doing a lookup (not a rename or remove). | |
1364 | * If a case-insensitive match was found earlier, re-read the | |
1365 | * appropriate data block if required and return it. | |
1366 | */ | |
1367 | if (args->cmpresult == XFS_CMP_CASE) { | |
1368 | ASSERT(cidb != -1); | |
1369 | if (cidb != curdb) { | |
a2ceac1f | 1370 | xfs_trans_brelse(tp, dbp); |
72386ff0 DW |
1371 | error = xfs_dir3_data_read(tp, dp, args->owner, |
1372 | xfs_dir2_db_to_da(args->geo, cidb), 0, | |
1373 | &dbp); | |
5e656dbb | 1374 | if (error) { |
a2ceac1f | 1375 | xfs_trans_brelse(tp, lbp); |
5e656dbb BN |
1376 | return error; |
1377 | } | |
1378 | } | |
1379 | *dbpp = dbp; | |
1380 | return 0; | |
1381 | } | |
2bd0ea18 | 1382 | /* |
12b53197 | 1383 | * No match found, return -ENOENT. |
2bd0ea18 | 1384 | */ |
5e656dbb | 1385 | ASSERT(cidb == -1); |
2bd0ea18 | 1386 | if (dbp) |
a2ceac1f DC |
1387 | xfs_trans_brelse(tp, dbp); |
1388 | xfs_trans_brelse(tp, lbp); | |
12b53197 | 1389 | return -ENOENT; |
2bd0ea18 NS |
1390 | } |
1391 | ||
1392 | /* | |
1393 | * Remove an entry from a leaf format directory. | |
1394 | */ | |
1395 | int /* error */ | |
1396 | xfs_dir2_leaf_removename( | |
1397 | xfs_da_args_t *args) /* operation arguments */ | |
1398 | { | |
58a1d356 | 1399 | struct xfs_da_geometry *geo = args->geo; |
5e656dbb | 1400 | __be16 *bestsp; /* leaf block best freespace */ |
a2ceac1f | 1401 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
2bd0ea18 | 1402 | xfs_dir2_db_t db; /* data block number */ |
a2ceac1f | 1403 | struct xfs_buf *dbp; /* data block buffer */ |
2bd0ea18 NS |
1404 | xfs_dir2_data_entry_t *dep; /* data entry structure */ |
1405 | xfs_inode_t *dp; /* incore directory inode */ | |
1406 | int error; /* error return code */ | |
1407 | xfs_dir2_db_t i; /* temporary data block # */ | |
1408 | int index; /* index into leaf entries */ | |
a2ceac1f | 1409 | struct xfs_buf *lbp; /* leaf buffer */ |
2bd0ea18 NS |
1410 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
1411 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1412 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
2bd0ea18 NS |
1413 | int needlog; /* need to log data header */ |
1414 | int needscan; /* need to rescan data frees */ | |
1415 | xfs_dir2_data_off_t oldbest; /* old value of best free */ | |
90ea28c3 | 1416 | struct xfs_dir2_data_free *bf; /* bestfree table */ |
65b80c98 | 1417 | struct xfs_dir3_icleaf_hdr leafhdr; |
2bd0ea18 | 1418 | |
56b2de80 DC |
1419 | trace_xfs_dir2_leaf_removename(args); |
1420 | ||
2bd0ea18 NS |
1421 | /* |
1422 | * Lookup the leaf entry, get the leaf and data blocks read in. | |
1423 | */ | |
a2279497 CH |
1424 | error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr); |
1425 | if (error) | |
2bd0ea18 | 1426 | return error; |
a2279497 | 1427 | |
2bd0ea18 | 1428 | dp = args->dp; |
a2ceac1f DC |
1429 | leaf = lbp->b_addr; |
1430 | hdr = dbp->b_addr; | |
90ea28c3 | 1431 | xfs_dir3_data_check(dp, dbp); |
04f6f354 | 1432 | bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr); |
a2279497 | 1433 | |
2bd0ea18 NS |
1434 | /* |
1435 | * Point to the leaf entry, use that to point to the data entry. | |
1436 | */ | |
a2279497 | 1437 | lep = &leafhdr.ents[index]; |
58a1d356 | 1438 | db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address)); |
ff105f75 | 1439 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + |
58a1d356 | 1440 | xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address))); |
2bd0ea18 | 1441 | needscan = needlog = 0; |
90ea28c3 | 1442 | oldbest = be16_to_cpu(bf[0].length); |
58a1d356 | 1443 | ltp = xfs_dir2_leaf_tail_p(geo, leaf); |
5e656dbb | 1444 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
a0264b73 | 1445 | if (be16_to_cpu(bestsp[db]) != oldbest) { |
1be76d11 | 1446 | xfs_buf_mark_corrupt(lbp); |
bcf1c967 | 1447 | xfs_da_mark_sick(args); |
6da71a85 | 1448 | return -EFSCORRUPTED; |
a0264b73 | 1449 | } |
bcf1c967 | 1450 | |
2bd0ea18 NS |
1451 | /* |
1452 | * Mark the former data entry unused. | |
1453 | */ | |
ff105f75 | 1454 | xfs_dir2_data_make_free(args, dbp, |
a2ceac1f | 1455 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), |
271a654f CH |
1456 | xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog, |
1457 | &needscan); | |
2bd0ea18 NS |
1458 | /* |
1459 | * We just mark the leaf entry stale by putting a null in it. | |
1460 | */ | |
65b80c98 | 1461 | leafhdr.stale++; |
7adfbcf6 | 1462 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); |
ff105f75 | 1463 | xfs_dir3_leaf_log_header(args, lbp); |
65b80c98 | 1464 | |
5e656dbb | 1465 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
a2279497 | 1466 | xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index); |
65b80c98 | 1467 | |
2bd0ea18 NS |
1468 | /* |
1469 | * Scan the freespace in the data block again if necessary, | |
1470 | * log the data block header if necessary. | |
1471 | */ | |
1472 | if (needscan) | |
d85595d0 | 1473 | xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog); |
2bd0ea18 | 1474 | if (needlog) |
ff105f75 | 1475 | xfs_dir2_data_log_header(args, dbp); |
2bd0ea18 NS |
1476 | /* |
1477 | * If the longest freespace in the data block has changed, | |
1478 | * put the new value in the bests table and log that. | |
1479 | */ | |
90ea28c3 DC |
1480 | if (be16_to_cpu(bf[0].length) != oldbest) { |
1481 | bestsp[db] = bf[0].length; | |
ff105f75 | 1482 | xfs_dir3_leaf_log_bests(args, lbp, db, db); |
2bd0ea18 | 1483 | } |
90ea28c3 | 1484 | xfs_dir3_data_check(dp, dbp); |
2bd0ea18 NS |
1485 | /* |
1486 | * If the data block is now empty then get rid of the data block. | |
1487 | */ | |
90ea28c3 | 1488 | if (be16_to_cpu(bf[0].length) == |
58a1d356 CH |
1489 | geo->blksize - geo->data_entry_offset) { |
1490 | ASSERT(db != geo->datablk); | |
0e266570 | 1491 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
2bd0ea18 NS |
1492 | /* |
1493 | * Nope, can't get rid of it because it caused | |
1494 | * allocation of a bmap btree block to do so. | |
1495 | * Just go on, returning success, leaving the | |
1496 | * empty block in place. | |
1497 | */ | |
12b53197 | 1498 | if (error == -ENOSPC && args->total == 0) |
2bd0ea18 | 1499 | error = 0; |
ff105f75 | 1500 | xfs_dir3_leaf_check(dp, lbp); |
2bd0ea18 NS |
1501 | return error; |
1502 | } | |
1503 | dbp = NULL; | |
1504 | /* | |
1505 | * If this is the last data block then compact the | |
1506 | * bests table by getting rid of entries. | |
1507 | */ | |
5e656dbb | 1508 | if (db == be32_to_cpu(ltp->bestcount) - 1) { |
2bd0ea18 NS |
1509 | /* |
1510 | * Look for the last active entry (i). | |
1511 | */ | |
1512 | for (i = db - 1; i > 0; i--) { | |
a2ceac1f | 1513 | if (bestsp[i] != cpu_to_be16(NULLDATAOFF)) |
2bd0ea18 NS |
1514 | break; |
1515 | } | |
1516 | /* | |
1517 | * Copy the table down so inactive entries at the | |
1518 | * end are removed. | |
1519 | */ | |
32181a02 | 1520 | memmove(&bestsp[db - i], bestsp, |
5e656dbb BN |
1521 | (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp)); |
1522 | be32_add_cpu(<p->bestcount, -(db - i)); | |
ff105f75 DC |
1523 | xfs_dir3_leaf_log_tail(args, lbp); |
1524 | xfs_dir3_leaf_log_bests(args, lbp, 0, | |
1525 | be32_to_cpu(ltp->bestcount) - 1); | |
2bd0ea18 | 1526 | } else |
5e656dbb | 1527 | bestsp[db] = cpu_to_be16(NULLDATAOFF); |
2bd0ea18 NS |
1528 | } |
1529 | /* | |
1530 | * If the data block was not the first one, drop it. | |
1531 | */ | |
58a1d356 | 1532 | else if (db != geo->datablk) |
2bd0ea18 | 1533 | dbp = NULL; |
a2ceac1f | 1534 | |
ff105f75 | 1535 | xfs_dir3_leaf_check(dp, lbp); |
2bd0ea18 NS |
1536 | /* |
1537 | * See if we can convert to block form. | |
1538 | */ | |
1539 | return xfs_dir2_leaf_to_block(args, lbp, dbp); | |
1540 | } | |
1541 | ||
1542 | /* | |
1543 | * Replace the inode number in a leaf format directory entry. | |
1544 | */ | |
1545 | int /* error */ | |
1546 | xfs_dir2_leaf_replace( | |
1547 | xfs_da_args_t *args) /* operation arguments */ | |
1548 | { | |
a2ceac1f | 1549 | struct xfs_buf *dbp; /* data block buffer */ |
2bd0ea18 NS |
1550 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
1551 | xfs_inode_t *dp; /* incore directory inode */ | |
1552 | int error; /* error return code */ | |
1553 | int index; /* index of leaf entry */ | |
a2ceac1f | 1554 | struct xfs_buf *lbp; /* leaf buffer */ |
2bd0ea18 NS |
1555 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
1556 | xfs_trans_t *tp; /* transaction pointer */ | |
a2279497 | 1557 | struct xfs_dir3_icleaf_hdr leafhdr; |
2bd0ea18 | 1558 | |
56b2de80 DC |
1559 | trace_xfs_dir2_leaf_replace(args); |
1560 | ||
2bd0ea18 NS |
1561 | /* |
1562 | * Look up the entry. | |
1563 | */ | |
a2279497 CH |
1564 | error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr); |
1565 | if (error) | |
2bd0ea18 | 1566 | return error; |
a2279497 | 1567 | |
2bd0ea18 | 1568 | dp = args->dp; |
2bd0ea18 NS |
1569 | /* |
1570 | * Point to the leaf entry, get data address from it. | |
1571 | */ | |
a2279497 | 1572 | lep = &leafhdr.ents[index]; |
2bd0ea18 NS |
1573 | /* |
1574 | * Point to the data entry. | |
1575 | */ | |
1576 | dep = (xfs_dir2_data_entry_t *) | |
a2ceac1f | 1577 | ((char *)dbp->b_addr + |
ff105f75 | 1578 | xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address))); |
5e656dbb | 1579 | ASSERT(args->inumber != be64_to_cpu(dep->inumber)); |
2bd0ea18 NS |
1580 | /* |
1581 | * Put the new inode number in, log it. | |
1582 | */ | |
5e656dbb | 1583 | dep->inumber = cpu_to_be64(args->inumber); |
28e6e614 | 1584 | xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype); |
2bd0ea18 | 1585 | tp = args->trans; |
ff105f75 DC |
1586 | xfs_dir2_data_log_entry(args, dbp, dep); |
1587 | xfs_dir3_leaf_check(dp, lbp); | |
a2ceac1f | 1588 | xfs_trans_brelse(tp, lbp); |
2bd0ea18 NS |
1589 | return 0; |
1590 | } | |
1591 | ||
1592 | /* | |
1593 | * Return index in the leaf block (lbp) which is either the first | |
1594 | * one with this hash value, or if there are none, the insert point | |
1595 | * for that hash value. | |
1596 | */ | |
1597 | int /* index value */ | |
1598 | xfs_dir2_leaf_search_hash( | |
1599 | xfs_da_args_t *args, /* operation arguments */ | |
a2ceac1f | 1600 | struct xfs_buf *lbp) /* leaf buffer */ |
2bd0ea18 | 1601 | { |
0e266570 | 1602 | xfs_dahash_t hash=0; /* hash from this entry */ |
2bd0ea18 NS |
1603 | xfs_dahash_t hashwant; /* hash value looking for */ |
1604 | int high; /* high leaf index */ | |
1605 | int low; /* low leaf index */ | |
2bd0ea18 | 1606 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
0e266570 | 1607 | int mid=0; /* current leaf index */ |
65b80c98 | 1608 | struct xfs_dir3_icleaf_hdr leafhdr; |
2bd0ea18 | 1609 | |
a2279497 | 1610 | xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr); |
65b80c98 | 1611 | |
2bd0ea18 NS |
1612 | /* |
1613 | * Note, the table cannot be empty, so we have to go through the loop. | |
1614 | * Binary search the leaf entries looking for our hash value. | |
1615 | */ | |
a2279497 | 1616 | for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1, |
2bd0ea18 NS |
1617 | hashwant = args->hashval; |
1618 | low <= high; ) { | |
1619 | mid = (low + high) >> 1; | |
5e656dbb | 1620 | if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant) |
2bd0ea18 NS |
1621 | break; |
1622 | if (hash < hashwant) | |
1623 | low = mid + 1; | |
1624 | else | |
1625 | high = mid - 1; | |
1626 | } | |
1627 | /* | |
1628 | * Found one, back up through all the equal hash values. | |
1629 | */ | |
1630 | if (hash == hashwant) { | |
5e656dbb | 1631 | while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) { |
2bd0ea18 NS |
1632 | mid--; |
1633 | } | |
1634 | } | |
1635 | /* | |
1636 | * Need to point to an entry higher than ours. | |
1637 | */ | |
1638 | else if (hash < hashwant) | |
1639 | mid++; | |
1640 | return mid; | |
1641 | } | |
1642 | ||
1643 | /* | |
1644 | * Trim off a trailing data block. We know it's empty since the leaf | |
1645 | * freespace table says so. | |
1646 | */ | |
1647 | int /* error */ | |
1648 | xfs_dir2_leaf_trim_data( | |
1649 | xfs_da_args_t *args, /* operation arguments */ | |
a2ceac1f | 1650 | struct xfs_buf *lbp, /* leaf buffer */ |
2bd0ea18 NS |
1651 | xfs_dir2_db_t db) /* data block number */ |
1652 | { | |
58a1d356 | 1653 | struct xfs_da_geometry *geo = args->geo; |
5e656dbb | 1654 | __be16 *bestsp; /* leaf bests table */ |
a2ceac1f | 1655 | struct xfs_buf *dbp; /* data block buffer */ |
2bd0ea18 NS |
1656 | xfs_inode_t *dp; /* incore directory inode */ |
1657 | int error; /* error return value */ | |
1658 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1659 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
2bd0ea18 NS |
1660 | xfs_trans_t *tp; /* transaction pointer */ |
1661 | ||
1662 | dp = args->dp; | |
2bd0ea18 NS |
1663 | tp = args->trans; |
1664 | /* | |
1665 | * Read the offending data block. We need its buffer. | |
1666 | */ | |
72386ff0 DW |
1667 | error = xfs_dir3_data_read(tp, dp, args->owner, |
1668 | xfs_dir2_db_to_da(geo, db), 0, &dbp); | |
a2ceac1f | 1669 | if (error) |
2bd0ea18 | 1670 | return error; |
2bd0ea18 | 1671 | |
a2ceac1f | 1672 | leaf = lbp->b_addr; |
58a1d356 | 1673 | ltp = xfs_dir2_leaf_tail_p(geo, leaf); |
a2ceac1f DC |
1674 | |
1675 | #ifdef DEBUG | |
1676 | { | |
1677 | struct xfs_dir2_data_hdr *hdr = dbp->b_addr; | |
04f6f354 CH |
1678 | struct xfs_dir2_data_free *bf = |
1679 | xfs_dir2_data_bestfree_p(dp->i_mount, hdr); | |
a2ceac1f | 1680 | |
90ea28c3 DC |
1681 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
1682 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); | |
1683 | ASSERT(be16_to_cpu(bf[0].length) == | |
58a1d356 | 1684 | geo->blksize - geo->data_entry_offset); |
5e656dbb | 1685 | ASSERT(db == be32_to_cpu(ltp->bestcount) - 1); |
a2ceac1f DC |
1686 | } |
1687 | #endif | |
1688 | ||
2bd0ea18 NS |
1689 | /* |
1690 | * Get rid of the data block. | |
1691 | */ | |
0e266570 | 1692 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
12b53197 | 1693 | ASSERT(error != -ENOSPC); |
a2ceac1f | 1694 | xfs_trans_brelse(tp, dbp); |
2bd0ea18 NS |
1695 | return error; |
1696 | } | |
1697 | /* | |
1698 | * Eliminate the last bests entry from the table. | |
1699 | */ | |
5e656dbb BN |
1700 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
1701 | be32_add_cpu(<p->bestcount, -1); | |
1702 | memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp)); | |
ff105f75 DC |
1703 | xfs_dir3_leaf_log_tail(args, lbp); |
1704 | xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); | |
2bd0ea18 NS |
1705 | return 0; |
1706 | } | |
1707 | ||
a2ceac1f | 1708 | static inline size_t |
65b80c98 DC |
1709 | xfs_dir3_leaf_size( |
1710 | struct xfs_dir3_icleaf_hdr *hdr, | |
a2ceac1f DC |
1711 | int counts) |
1712 | { | |
65b80c98 DC |
1713 | int entries; |
1714 | int hdrsize; | |
1715 | ||
1716 | entries = hdr->count - hdr->stale; | |
1717 | if (hdr->magic == XFS_DIR2_LEAF1_MAGIC || | |
1718 | hdr->magic == XFS_DIR2_LEAFN_MAGIC) | |
1719 | hdrsize = sizeof(struct xfs_dir2_leaf_hdr); | |
1720 | else | |
1721 | hdrsize = sizeof(struct xfs_dir3_leaf_hdr); | |
a2ceac1f | 1722 | |
65b80c98 DC |
1723 | return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t) |
1724 | + counts * sizeof(xfs_dir2_data_off_t) | |
1725 | + sizeof(xfs_dir2_leaf_tail_t); | |
a2ceac1f DC |
1726 | } |
1727 | ||
2bd0ea18 NS |
1728 | /* |
1729 | * Convert node form directory to leaf form directory. | |
1730 | * The root of the node form dir needs to already be a LEAFN block. | |
1731 | * Just return if we can't do anything. | |
1732 | */ | |
1733 | int /* error */ | |
1734 | xfs_dir2_node_to_leaf( | |
1735 | xfs_da_state_t *state) /* directory operation state */ | |
1736 | { | |
1737 | xfs_da_args_t *args; /* operation arguments */ | |
1738 | xfs_inode_t *dp; /* incore directory inode */ | |
1739 | int error; /* error return code */ | |
a2ceac1f | 1740 | struct xfs_buf *fbp; /* buffer for freespace block */ |
2bd0ea18 | 1741 | xfs_fileoff_t fo; /* freespace file offset */ |
a2ceac1f | 1742 | struct xfs_buf *lbp; /* buffer for leaf block */ |
2bd0ea18 NS |
1743 | xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */ |
1744 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1745 | xfs_mount_t *mp; /* filesystem mount point */ | |
1746 | int rval; /* successful free trim? */ | |
1747 | xfs_trans_t *tp; /* transaction pointer */ | |
65b80c98 | 1748 | struct xfs_dir3_icleaf_hdr leafhdr; |
3303b09f | 1749 | struct xfs_dir3_icfree_hdr freehdr; |
2bd0ea18 NS |
1750 | |
1751 | /* | |
1752 | * There's more than a leaf level in the btree, so there must | |
1753 | * be multiple leafn blocks. Give up. | |
1754 | */ | |
1755 | if (state->path.active > 1) | |
1756 | return 0; | |
1757 | args = state->args; | |
56b2de80 DC |
1758 | |
1759 | trace_xfs_dir2_node_to_leaf(args); | |
1760 | ||
2bd0ea18 NS |
1761 | mp = state->mp; |
1762 | dp = args->dp; | |
1763 | tp = args->trans; | |
1764 | /* | |
1765 | * Get the last offset in the file. | |
1766 | */ | |
ff105f75 | 1767 | if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) { |
2bd0ea18 NS |
1768 | return error; |
1769 | } | |
ff105f75 | 1770 | fo -= args->geo->fsbcount; |
2bd0ea18 NS |
1771 | /* |
1772 | * If there are freespace blocks other than the first one, | |
1773 | * take this opportunity to remove trailing empty freespace blocks | |
1774 | * that may have been left behind during no-space-reservation | |
1775 | * operations. | |
1776 | */ | |
ff105f75 | 1777 | while (fo > args->geo->freeblk) { |
0e266570 | 1778 | if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) { |
2bd0ea18 NS |
1779 | return error; |
1780 | } | |
1781 | if (rval) | |
ff105f75 | 1782 | fo -= args->geo->fsbcount; |
2bd0ea18 NS |
1783 | else |
1784 | return 0; | |
1785 | } | |
1786 | /* | |
1787 | * Now find the block just before the freespace block. | |
1788 | */ | |
0e266570 | 1789 | if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) { |
2bd0ea18 NS |
1790 | return error; |
1791 | } | |
1792 | /* | |
1793 | * If it's not the single leaf block, give up. | |
1794 | */ | |
ff105f75 | 1795 | if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize) |
2bd0ea18 NS |
1796 | return 0; |
1797 | lbp = state->path.blk[0].bp; | |
a2ceac1f | 1798 | leaf = lbp->b_addr; |
9db68faf | 1799 | xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); |
65b80c98 DC |
1800 | |
1801 | ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || | |
1802 | leafhdr.magic == XFS_DIR3_LEAFN_MAGIC); | |
1803 | ||
2bd0ea18 NS |
1804 | /* |
1805 | * Read the freespace block. | |
1806 | */ | |
92244687 DW |
1807 | error = xfs_dir2_free_read(tp, dp, args->owner, args->geo->freeblk, |
1808 | &fbp); | |
a2ceac1f | 1809 | if (error) |
2bd0ea18 | 1810 | return error; |
cb5d1930 | 1811 | xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr); |
3303b09f DC |
1812 | |
1813 | ASSERT(!freehdr.firstdb); | |
a2ceac1f | 1814 | |
2bd0ea18 NS |
1815 | /* |
1816 | * Now see if the leafn and free data will fit in a leaf1. | |
1817 | * If not, release the buffer and give up. | |
1818 | */ | |
ff105f75 | 1819 | if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) { |
a2ceac1f | 1820 | xfs_trans_brelse(tp, fbp); |
2bd0ea18 NS |
1821 | return 0; |
1822 | } | |
a2ceac1f | 1823 | |
2bd0ea18 NS |
1824 | /* |
1825 | * If the leaf has any stale entries in it, compress them out. | |
2bd0ea18 | 1826 | */ |
65b80c98 DC |
1827 | if (leafhdr.stale) |
1828 | xfs_dir3_leaf_compact(args, &leafhdr, lbp); | |
a2ceac1f | 1829 | |
65b80c98 | 1830 | lbp->b_ops = &xfs_dir3_leaf1_buf_ops; |
bdc16ee5 | 1831 | xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF); |
65b80c98 DC |
1832 | leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC) |
1833 | ? XFS_DIR2_LEAF1_MAGIC | |
1834 | : XFS_DIR3_LEAF1_MAGIC; | |
a2ceac1f | 1835 | |
2bd0ea18 NS |
1836 | /* |
1837 | * Set up the leaf tail from the freespace block. | |
1838 | */ | |
ff105f75 | 1839 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
3303b09f | 1840 | ltp->bestcount = cpu_to_be32(freehdr.nvalid); |
65b80c98 | 1841 | |
2bd0ea18 NS |
1842 | /* |
1843 | * Set up the leaf bests table. | |
1844 | */ | |
cb5d1930 | 1845 | memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests, |
3303b09f | 1846 | freehdr.nvalid * sizeof(xfs_dir2_data_off_t)); |
65b80c98 | 1847 | |
7adfbcf6 | 1848 | xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr); |
ff105f75 DC |
1849 | xfs_dir3_leaf_log_header(args, lbp); |
1850 | xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); | |
1851 | xfs_dir3_leaf_log_tail(args, lbp); | |
1852 | xfs_dir3_leaf_check(dp, lbp); | |
65b80c98 | 1853 | |
2bd0ea18 NS |
1854 | /* |
1855 | * Get rid of the freespace block. | |
1856 | */ | |
ff105f75 DC |
1857 | error = xfs_dir2_shrink_inode(args, |
1858 | xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET), | |
1859 | fbp); | |
2bd0ea18 | 1860 | if (error) { |
2bd0ea18 NS |
1861 | /* |
1862 | * This can't fail here because it can only happen when | |
1863 | * punching out the middle of an extent, and this is an | |
1864 | * isolated block. | |
1865 | */ | |
12b53197 | 1866 | ASSERT(error != -ENOSPC); |
2bd0ea18 NS |
1867 | return error; |
1868 | } | |
1869 | fbp = NULL; | |
1870 | /* | |
1871 | * Now see if we can convert the single-leaf directory | |
1872 | * down to a block form directory. | |
1873 | * This routine always kills the dabuf for the leaf, so | |
1874 | * eliminate it from the path. | |
1875 | */ | |
1876 | error = xfs_dir2_leaf_to_block(args, lbp, NULL); | |
1877 | state->path.blk[0].bp = NULL; | |
1878 | return error; | |
1879 | } |