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