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