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