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