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