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