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