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