OSDN Git Service

xfs: devirtualize ->m_dirnameops
[tomoyo/tomoyo-test1.git] / fs / xfs / libxfs / xfs_dir2_data.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #include "xfs.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_dir2.h"
16 #include "xfs_dir2_priv.h"
17 #include "xfs_error.h"
18 #include "xfs_trans.h"
19 #include "xfs_buf_item.h"
20 #include "xfs_log.h"
21 #include "xfs_dir2_priv.h"
22
23 static xfs_failaddr_t xfs_dir2_data_freefind_verify(
24                 struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf,
25                 struct xfs_dir2_data_unused *dup,
26                 struct xfs_dir2_data_free **bf_ent);
27
28 struct xfs_dir2_data_free *
29 xfs_dir2_data_bestfree_p(
30         struct xfs_mount                *mp,
31         struct xfs_dir2_data_hdr        *hdr)
32 {
33         if (xfs_sb_version_hascrc(&mp->m_sb))
34                 return ((struct xfs_dir3_data_hdr *)hdr)->best_free;
35         return hdr->bestfree;
36 }
37
38 /*
39  * Pointer to an entry's tag word.
40  */
41 __be16 *
42 xfs_dir2_data_entry_tag_p(
43         struct xfs_mount                *mp,
44         struct xfs_dir2_data_entry      *dep)
45 {
46         return (__be16 *)((char *)dep +
47                 xfs_dir2_data_entsize(mp, dep->namelen) - sizeof(__be16));
48 }
49
50 uint8_t
51 xfs_dir2_data_get_ftype(
52         struct xfs_mount                *mp,
53         struct xfs_dir2_data_entry      *dep)
54 {
55         if (xfs_sb_version_hasftype(&mp->m_sb)) {
56                 uint8_t                 ftype = dep->name[dep->namelen];
57
58                 if (likely(ftype < XFS_DIR3_FT_MAX))
59                         return ftype;
60         }
61
62         return XFS_DIR3_FT_UNKNOWN;
63 }
64
65 void
66 xfs_dir2_data_put_ftype(
67         struct xfs_mount                *mp,
68         struct xfs_dir2_data_entry      *dep,
69         uint8_t                         ftype)
70 {
71         ASSERT(ftype < XFS_DIR3_FT_MAX);
72         ASSERT(dep->namelen != 0);
73
74         if (xfs_sb_version_hasftype(&mp->m_sb))
75                 dep->name[dep->namelen] = ftype;
76 }
77
78 /*
79  * The number of leaf entries is limited by the size of the block and the amount
80  * of space used by the data entries.  We don't know how much space is used by
81  * the data entries yet, so just ensure that the count falls somewhere inside
82  * the block right now.
83  */
84 static inline unsigned int
85 xfs_dir2_data_max_leaf_entries(
86         struct xfs_da_geometry          *geo)
87 {
88         return (geo->blksize - sizeof(struct xfs_dir2_block_tail) -
89                 geo->data_entry_offset) /
90                         sizeof(struct xfs_dir2_leaf_entry);
91 }
92
93 /*
94  * Check the consistency of the data block.
95  * The input can also be a block-format directory.
96  * Return NULL if the buffer is good, otherwise the address of the error.
97  */
98 xfs_failaddr_t
99 __xfs_dir3_data_check(
100         struct xfs_inode        *dp,            /* incore inode pointer */
101         struct xfs_buf          *bp)            /* data block's buffer */
102 {
103         xfs_dir2_dataptr_t      addr;           /* addr for leaf lookup */
104         xfs_dir2_data_free_t    *bf;            /* bestfree table */
105         xfs_dir2_block_tail_t   *btp=NULL;      /* block tail */
106         int                     count;          /* count of entries found */
107         xfs_dir2_data_hdr_t     *hdr;           /* data block header */
108         xfs_dir2_data_free_t    *dfp;           /* bestfree entry */
109         int                     freeseen;       /* mask of bestfrees seen */
110         xfs_dahash_t            hash;           /* hash of current name */
111         int                     i;              /* leaf index */
112         int                     lastfree;       /* last entry was unused */
113         xfs_dir2_leaf_entry_t   *lep=NULL;      /* block leaf entries */
114         struct xfs_mount        *mp = bp->b_mount;
115         int                     stale;          /* count of stale leaves */
116         struct xfs_name         name;
117         unsigned int            offset;
118         unsigned int            end;
119         struct xfs_da_geometry  *geo = mp->m_dir_geo;
120
121         /*
122          * If this isn't a directory, something is seriously wrong.  Bail out.
123          */
124         if (dp && !S_ISDIR(VFS_I(dp)->i_mode))
125                 return __this_address;
126
127         hdr = bp->b_addr;
128         offset = geo->data_entry_offset;
129
130         switch (hdr->magic) {
131         case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
132         case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
133                 btp = xfs_dir2_block_tail_p(geo, hdr);
134                 lep = xfs_dir2_block_leaf_p(btp);
135
136                 if (be32_to_cpu(btp->count) >=
137                     xfs_dir2_data_max_leaf_entries(geo))
138                         return __this_address;
139                 break;
140         case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
141         case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
142                 break;
143         default:
144                 return __this_address;
145         }
146         end = xfs_dir3_data_end_offset(geo, hdr);
147         if (!end)
148                 return __this_address;
149
150         /*
151          * Account for zero bestfree entries.
152          */
153         bf = xfs_dir2_data_bestfree_p(mp, hdr);
154         count = lastfree = freeseen = 0;
155         if (!bf[0].length) {
156                 if (bf[0].offset)
157                         return __this_address;
158                 freeseen |= 1 << 0;
159         }
160         if (!bf[1].length) {
161                 if (bf[1].offset)
162                         return __this_address;
163                 freeseen |= 1 << 1;
164         }
165         if (!bf[2].length) {
166                 if (bf[2].offset)
167                         return __this_address;
168                 freeseen |= 1 << 2;
169         }
170
171         if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length))
172                 return __this_address;
173         if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length))
174                 return __this_address;
175         /*
176          * Loop over the data/unused entries.
177          */
178         while (offset < end) {
179                 struct xfs_dir2_data_unused     *dup = bp->b_addr + offset;
180                 struct xfs_dir2_data_entry      *dep = bp->b_addr + offset;
181
182                 /*
183                  * If it's unused, look for the space in the bestfree table.
184                  * If we find it, account for that, else make sure it
185                  * doesn't need to be there.
186                  */
187                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
188                         xfs_failaddr_t  fa;
189
190                         if (lastfree != 0)
191                                 return __this_address;
192                         if (offset + be16_to_cpu(dup->length) > end)
193                                 return __this_address;
194                         if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
195                             offset)
196                                 return __this_address;
197                         fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp);
198                         if (fa)
199                                 return fa;
200                         if (dfp) {
201                                 i = (int)(dfp - bf);
202                                 if ((freeseen & (1 << i)) != 0)
203                                         return __this_address;
204                                 freeseen |= 1 << i;
205                         } else {
206                                 if (be16_to_cpu(dup->length) >
207                                     be16_to_cpu(bf[2].length))
208                                         return __this_address;
209                         }
210                         offset += be16_to_cpu(dup->length);
211                         lastfree = 1;
212                         continue;
213                 }
214                 /*
215                  * It's a real entry.  Validate the fields.
216                  * If this is a block directory then make sure it's
217                  * in the leaf section of the block.
218                  * The linear search is crude but this is DEBUG code.
219                  */
220                 if (dep->namelen == 0)
221                         return __this_address;
222                 if (xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)))
223                         return __this_address;
224                 if (offset + xfs_dir2_data_entsize(mp, dep->namelen) > end)
225                         return __this_address;
226                 if (be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)) != offset)
227                         return __this_address;
228                 if (xfs_dir2_data_get_ftype(mp, dep) >= XFS_DIR3_FT_MAX)
229                         return __this_address;
230                 count++;
231                 lastfree = 0;
232                 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
233                     hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
234                         addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk,
235                                                 (xfs_dir2_data_aoff_t)
236                                                 ((char *)dep - (char *)hdr));
237                         name.name = dep->name;
238                         name.len = dep->namelen;
239                         hash = xfs_dir2_hashname(mp, &name);
240                         for (i = 0; i < be32_to_cpu(btp->count); i++) {
241                                 if (be32_to_cpu(lep[i].address) == addr &&
242                                     be32_to_cpu(lep[i].hashval) == hash)
243                                         break;
244                         }
245                         if (i >= be32_to_cpu(btp->count))
246                                 return __this_address;
247                 }
248                 offset += xfs_dir2_data_entsize(mp, dep->namelen);
249         }
250         /*
251          * Need to have seen all the entries and all the bestfree slots.
252          */
253         if (freeseen != 7)
254                 return __this_address;
255         if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
256             hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
257                 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
258                         if (lep[i].address ==
259                             cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
260                                 stale++;
261                         if (i > 0 && be32_to_cpu(lep[i].hashval) <
262                                      be32_to_cpu(lep[i - 1].hashval))
263                                 return __this_address;
264                 }
265                 if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale))
266                         return __this_address;
267                 if (stale != be32_to_cpu(btp->stale))
268                         return __this_address;
269         }
270         return NULL;
271 }
272
273 #ifdef DEBUG
274 void
275 xfs_dir3_data_check(
276         struct xfs_inode        *dp,
277         struct xfs_buf          *bp)
278 {
279         xfs_failaddr_t          fa;
280
281         fa = __xfs_dir3_data_check(dp, bp);
282         if (!fa)
283                 return;
284         xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
285                         bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
286                         fa);
287         ASSERT(0);
288 }
289 #endif
290
291 static xfs_failaddr_t
292 xfs_dir3_data_verify(
293         struct xfs_buf          *bp)
294 {
295         struct xfs_mount        *mp = bp->b_mount;
296         struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
297
298         if (!xfs_verify_magic(bp, hdr3->magic))
299                 return __this_address;
300
301         if (xfs_sb_version_hascrc(&mp->m_sb)) {
302                 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
303                         return __this_address;
304                 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
305                         return __this_address;
306                 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
307                         return __this_address;
308         }
309         return __xfs_dir3_data_check(NULL, bp);
310 }
311
312 /*
313  * Readahead of the first block of the directory when it is opened is completely
314  * oblivious to the format of the directory. Hence we can either get a block
315  * format buffer or a data format buffer on readahead.
316  */
317 static void
318 xfs_dir3_data_reada_verify(
319         struct xfs_buf          *bp)
320 {
321         struct xfs_dir2_data_hdr *hdr = bp->b_addr;
322
323         switch (hdr->magic) {
324         case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
325         case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
326                 bp->b_ops = &xfs_dir3_block_buf_ops;
327                 bp->b_ops->verify_read(bp);
328                 return;
329         case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
330         case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
331                 bp->b_ops = &xfs_dir3_data_buf_ops;
332                 bp->b_ops->verify_read(bp);
333                 return;
334         default:
335                 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
336                 break;
337         }
338 }
339
340 static void
341 xfs_dir3_data_read_verify(
342         struct xfs_buf  *bp)
343 {
344         struct xfs_mount        *mp = bp->b_mount;
345         xfs_failaddr_t          fa;
346
347         if (xfs_sb_version_hascrc(&mp->m_sb) &&
348             !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF))
349                 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
350         else {
351                 fa = xfs_dir3_data_verify(bp);
352                 if (fa)
353                         xfs_verifier_error(bp, -EFSCORRUPTED, fa);
354         }
355 }
356
357 static void
358 xfs_dir3_data_write_verify(
359         struct xfs_buf  *bp)
360 {
361         struct xfs_mount        *mp = bp->b_mount;
362         struct xfs_buf_log_item *bip = bp->b_log_item;
363         struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
364         xfs_failaddr_t          fa;
365
366         fa = xfs_dir3_data_verify(bp);
367         if (fa) {
368                 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
369                 return;
370         }
371
372         if (!xfs_sb_version_hascrc(&mp->m_sb))
373                 return;
374
375         if (bip)
376                 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
377
378         xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF);
379 }
380
381 const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
382         .name = "xfs_dir3_data",
383         .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
384                    cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
385         .verify_read = xfs_dir3_data_read_verify,
386         .verify_write = xfs_dir3_data_write_verify,
387         .verify_struct = xfs_dir3_data_verify,
388 };
389
390 static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
391         .name = "xfs_dir3_data_reada",
392         .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC),
393                    cpu_to_be32(XFS_DIR3_DATA_MAGIC) },
394         .verify_read = xfs_dir3_data_reada_verify,
395         .verify_write = xfs_dir3_data_write_verify,
396 };
397
398
399 int
400 xfs_dir3_data_read(
401         struct xfs_trans        *tp,
402         struct xfs_inode        *dp,
403         xfs_dablk_t             bno,
404         xfs_daddr_t             mapped_bno,
405         struct xfs_buf          **bpp)
406 {
407         int                     err;
408
409         err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
410                                 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
411         if (!err && tp && *bpp)
412                 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF);
413         return err;
414 }
415
416 int
417 xfs_dir3_data_readahead(
418         struct xfs_inode        *dp,
419         xfs_dablk_t             bno,
420         xfs_daddr_t             mapped_bno)
421 {
422         return xfs_da_reada_buf(dp, bno, mapped_bno,
423                                 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
424 }
425
426 /*
427  * Find the bestfree entry that exactly coincides with unused directory space
428  * or a verifier error because the bestfree data are bad.
429  */
430 static xfs_failaddr_t
431 xfs_dir2_data_freefind_verify(
432         struct xfs_dir2_data_hdr        *hdr,
433         struct xfs_dir2_data_free       *bf,
434         struct xfs_dir2_data_unused     *dup,
435         struct xfs_dir2_data_free       **bf_ent)
436 {
437         struct xfs_dir2_data_free       *dfp;
438         xfs_dir2_data_aoff_t            off;
439         bool                            matched = false;
440         bool                            seenzero = false;
441
442         *bf_ent = NULL;
443         off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
444
445         /*
446          * Validate some consistency in the bestfree table.
447          * Check order, non-overlapping entries, and if we find the
448          * one we're looking for it has to be exact.
449          */
450         for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
451                 if (!dfp->offset) {
452                         if (dfp->length)
453                                 return __this_address;
454                         seenzero = true;
455                         continue;
456                 }
457                 if (seenzero)
458                         return __this_address;
459                 if (be16_to_cpu(dfp->offset) == off) {
460                         matched = true;
461                         if (dfp->length != dup->length)
462                                 return __this_address;
463                 } else if (be16_to_cpu(dfp->offset) > off) {
464                         if (off + be16_to_cpu(dup->length) >
465                                         be16_to_cpu(dfp->offset))
466                                 return __this_address;
467                 } else {
468                         if (be16_to_cpu(dfp->offset) +
469                                         be16_to_cpu(dfp->length) > off)
470                                 return __this_address;
471                 }
472                 if (!matched &&
473                     be16_to_cpu(dfp->length) < be16_to_cpu(dup->length))
474                         return __this_address;
475                 if (dfp > &bf[0] &&
476                     be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length))
477                         return __this_address;
478         }
479
480         /* Looks ok so far; now try to match up with a bestfree entry. */
481         *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup);
482         return NULL;
483 }
484
485 /*
486  * Given a data block and an unused entry from that block,
487  * return the bestfree entry if any that corresponds to it.
488  */
489 xfs_dir2_data_free_t *
490 xfs_dir2_data_freefind(
491         struct xfs_dir2_data_hdr *hdr,          /* data block header */
492         struct xfs_dir2_data_free *bf,          /* bestfree table pointer */
493         struct xfs_dir2_data_unused *dup)       /* unused space */
494 {
495         xfs_dir2_data_free_t    *dfp;           /* bestfree entry */
496         xfs_dir2_data_aoff_t    off;            /* offset value needed */
497
498         off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
499
500         /*
501          * If this is smaller than the smallest bestfree entry,
502          * it can't be there since they're sorted.
503          */
504         if (be16_to_cpu(dup->length) <
505             be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
506                 return NULL;
507         /*
508          * Look at the three bestfree entries for our guy.
509          */
510         for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
511                 if (!dfp->offset)
512                         return NULL;
513                 if (be16_to_cpu(dfp->offset) == off)
514                         return dfp;
515         }
516         /*
517          * Didn't find it.  This only happens if there are duplicate lengths.
518          */
519         return NULL;
520 }
521
522 /*
523  * Insert an unused-space entry into the bestfree table.
524  */
525 xfs_dir2_data_free_t *                          /* entry inserted */
526 xfs_dir2_data_freeinsert(
527         struct xfs_dir2_data_hdr *hdr,          /* data block pointer */
528         struct xfs_dir2_data_free *dfp,         /* bestfree table pointer */
529         struct xfs_dir2_data_unused *dup,       /* unused space */
530         int                     *loghead)       /* log the data header (out) */
531 {
532         xfs_dir2_data_free_t    new;            /* new bestfree entry */
533
534         ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
535                hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
536                hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
537                hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
538
539         new.length = dup->length;
540         new.offset = cpu_to_be16((char *)dup - (char *)hdr);
541
542         /*
543          * Insert at position 0, 1, or 2; or not at all.
544          */
545         if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
546                 dfp[2] = dfp[1];
547                 dfp[1] = dfp[0];
548                 dfp[0] = new;
549                 *loghead = 1;
550                 return &dfp[0];
551         }
552         if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
553                 dfp[2] = dfp[1];
554                 dfp[1] = new;
555                 *loghead = 1;
556                 return &dfp[1];
557         }
558         if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
559                 dfp[2] = new;
560                 *loghead = 1;
561                 return &dfp[2];
562         }
563         return NULL;
564 }
565
566 /*
567  * Remove a bestfree entry from the table.
568  */
569 STATIC void
570 xfs_dir2_data_freeremove(
571         struct xfs_dir2_data_hdr *hdr,          /* data block header */
572         struct xfs_dir2_data_free *bf,          /* bestfree table pointer */
573         struct xfs_dir2_data_free *dfp,         /* bestfree entry pointer */
574         int                     *loghead)       /* out: log data header */
575 {
576
577         ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
578                hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
579                hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
580                hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
581
582         /*
583          * It's the first entry, slide the next 2 up.
584          */
585         if (dfp == &bf[0]) {
586                 bf[0] = bf[1];
587                 bf[1] = bf[2];
588         }
589         /*
590          * It's the second entry, slide the 3rd entry up.
591          */
592         else if (dfp == &bf[1])
593                 bf[1] = bf[2];
594         /*
595          * Must be the last entry.
596          */
597         else
598                 ASSERT(dfp == &bf[2]);
599         /*
600          * Clear the 3rd entry, must be zero now.
601          */
602         bf[2].length = 0;
603         bf[2].offset = 0;
604         *loghead = 1;
605 }
606
607 /*
608  * Given a data block, reconstruct its bestfree map.
609  */
610 void
611 xfs_dir2_data_freescan(
612         struct xfs_mount                *mp,
613         struct xfs_dir2_data_hdr        *hdr,
614         int                             *loghead)
615 {
616         struct xfs_da_geometry          *geo = mp->m_dir_geo;
617         struct xfs_dir2_data_free       *bf = xfs_dir2_data_bestfree_p(mp, hdr);
618         void                            *addr = hdr;
619         unsigned int                    offset = geo->data_entry_offset;
620         unsigned int                    end;
621
622         ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
623                hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
624                hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
625                hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
626
627         /*
628          * Start by clearing the table.
629          */
630         memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
631         *loghead = 1;
632
633         end = xfs_dir3_data_end_offset(geo, addr);
634         while (offset < end) {
635                 struct xfs_dir2_data_unused     *dup = addr + offset;
636                 struct xfs_dir2_data_entry      *dep = addr + offset;
637
638                 /*
639                  * If it's a free entry, insert it.
640                  */
641                 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
642                         ASSERT(offset ==
643                                be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
644                         xfs_dir2_data_freeinsert(hdr, bf, dup, loghead);
645                         offset += be16_to_cpu(dup->length);
646                         continue;
647                 }
648
649                 /*
650                  * For active entries, check their tags and skip them.
651                  */
652                 ASSERT(offset ==
653                        be16_to_cpu(*xfs_dir2_data_entry_tag_p(mp, dep)));
654                 offset += xfs_dir2_data_entsize(mp, dep->namelen);
655         }
656 }
657
658 /*
659  * Initialize a data block at the given block number in the directory.
660  * Give back the buffer for the created block.
661  */
662 int                                             /* error */
663 xfs_dir3_data_init(
664         struct xfs_da_args              *args,  /* directory operation args */
665         xfs_dir2_db_t                   blkno,  /* logical dir block number */
666         struct xfs_buf                  **bpp)  /* output block buffer */
667 {
668         struct xfs_trans                *tp = args->trans;
669         struct xfs_inode                *dp = args->dp;
670         struct xfs_mount                *mp = dp->i_mount;
671         struct xfs_da_geometry          *geo = args->geo;
672         struct xfs_buf                  *bp;
673         struct xfs_dir2_data_hdr        *hdr;
674         struct xfs_dir2_data_unused     *dup;
675         struct xfs_dir2_data_free       *bf;
676         int                             error;
677         int                             i;
678
679         /*
680          * Get the buffer set up for the block.
681          */
682         error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno),
683                                -1, &bp, XFS_DATA_FORK);
684         if (error)
685                 return error;
686         bp->b_ops = &xfs_dir3_data_buf_ops;
687         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF);
688
689         /*
690          * Initialize the header.
691          */
692         hdr = bp->b_addr;
693         if (xfs_sb_version_hascrc(&mp->m_sb)) {
694                 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
695
696                 memset(hdr3, 0, sizeof(*hdr3));
697                 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
698                 hdr3->blkno = cpu_to_be64(bp->b_bn);
699                 hdr3->owner = cpu_to_be64(dp->i_ino);
700                 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
701
702         } else
703                 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
704
705         bf = xfs_dir2_data_bestfree_p(mp, hdr);
706         bf[0].offset = cpu_to_be16(geo->data_entry_offset);
707         bf[0].length = cpu_to_be16(geo->blksize - geo->data_entry_offset);
708         for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
709                 bf[i].length = 0;
710                 bf[i].offset = 0;
711         }
712
713         /*
714          * Set up an unused entry for the block's body.
715          */
716         dup = bp->b_addr + geo->data_entry_offset;
717         dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
718         dup->length = bf[0].length;
719         *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
720
721         /*
722          * Log it and return it.
723          */
724         xfs_dir2_data_log_header(args, bp);
725         xfs_dir2_data_log_unused(args, bp, dup);
726         *bpp = bp;
727         return 0;
728 }
729
730 /*
731  * Log an active data entry from the block.
732  */
733 void
734 xfs_dir2_data_log_entry(
735         struct xfs_da_args      *args,
736         struct xfs_buf          *bp,
737         xfs_dir2_data_entry_t   *dep)           /* data entry pointer */
738 {
739         struct xfs_mount        *mp = bp->b_mount;
740         struct xfs_dir2_data_hdr *hdr = bp->b_addr;
741
742         ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
743                hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
744                hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
745                hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
746
747         xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr),
748                 (uint)((char *)(xfs_dir2_data_entry_tag_p(mp, dep) + 1) -
749                        (char *)hdr - 1));
750 }
751
752 /*
753  * Log a data block header.
754  */
755 void
756 xfs_dir2_data_log_header(
757         struct xfs_da_args      *args,
758         struct xfs_buf          *bp)
759 {
760 #ifdef DEBUG
761         struct xfs_dir2_data_hdr *hdr = bp->b_addr;
762
763         ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
764                hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
765                hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
766                hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
767 #endif
768
769         xfs_trans_log_buf(args->trans, bp, 0, args->geo->data_entry_offset - 1);
770 }
771
772 /*
773  * Log a data unused entry.
774  */
775 void
776 xfs_dir2_data_log_unused(
777         struct xfs_da_args      *args,
778         struct xfs_buf          *bp,
779         xfs_dir2_data_unused_t  *dup)           /* data unused pointer */
780 {
781         xfs_dir2_data_hdr_t     *hdr = bp->b_addr;
782
783         ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
784                hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
785                hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
786                hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
787
788         /*
789          * Log the first part of the unused entry.
790          */
791         xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr),
792                 (uint)((char *)&dup->length + sizeof(dup->length) -
793                        1 - (char *)hdr));
794         /*
795          * Log the end (tag) of the unused entry.
796          */
797         xfs_trans_log_buf(args->trans, bp,
798                 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
799                 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
800                        sizeof(xfs_dir2_data_off_t) - 1));
801 }
802
803 /*
804  * Make a byte range in the data block unused.
805  * Its current contents are unimportant.
806  */
807 void
808 xfs_dir2_data_make_free(
809         struct xfs_da_args      *args,
810         struct xfs_buf          *bp,
811         xfs_dir2_data_aoff_t    offset,         /* starting byte offset */
812         xfs_dir2_data_aoff_t    len,            /* length in bytes */
813         int                     *needlogp,      /* out: log header */
814         int                     *needscanp)     /* out: regen bestfree */
815 {
816         xfs_dir2_data_hdr_t     *hdr;           /* data block pointer */
817         xfs_dir2_data_free_t    *dfp;           /* bestfree pointer */
818         int                     needscan;       /* need to regen bestfree */
819         xfs_dir2_data_unused_t  *newdup;        /* new unused entry */
820         xfs_dir2_data_unused_t  *postdup;       /* unused entry after us */
821         xfs_dir2_data_unused_t  *prevdup;       /* unused entry before us */
822         unsigned int            end;
823         struct xfs_dir2_data_free *bf;
824
825         hdr = bp->b_addr;
826
827         /*
828          * Figure out where the end of the data area is.
829          */
830         end = xfs_dir3_data_end_offset(args->geo, hdr);
831         ASSERT(end != 0);
832
833         /*
834          * If this isn't the start of the block, then back up to
835          * the previous entry and see if it's free.
836          */
837         if (offset > args->geo->data_entry_offset) {
838                 __be16                  *tagp;  /* tag just before us */
839
840                 tagp = (__be16 *)((char *)hdr + offset) - 1;
841                 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
842                 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
843                         prevdup = NULL;
844         } else
845                 prevdup = NULL;
846         /*
847          * If this isn't the end of the block, see if the entry after
848          * us is free.
849          */
850         if (offset + len < end) {
851                 postdup =
852                         (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
853                 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
854                         postdup = NULL;
855         } else
856                 postdup = NULL;
857         ASSERT(*needscanp == 0);
858         needscan = 0;
859         /*
860          * Previous and following entries are both free,
861          * merge everything into a single free entry.
862          */
863         bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
864         if (prevdup && postdup) {
865                 xfs_dir2_data_free_t    *dfp2;  /* another bestfree pointer */
866
867                 /*
868                  * See if prevdup and/or postdup are in bestfree table.
869                  */
870                 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
871                 dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup);
872                 /*
873                  * We need a rescan unless there are exactly 2 free entries
874                  * namely our two.  Then we know what's happening, otherwise
875                  * since the third bestfree is there, there might be more
876                  * entries.
877                  */
878                 needscan = (bf[2].length != 0);
879                 /*
880                  * Fix up the new big freespace.
881                  */
882                 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
883                 *xfs_dir2_data_unused_tag_p(prevdup) =
884                         cpu_to_be16((char *)prevdup - (char *)hdr);
885                 xfs_dir2_data_log_unused(args, bp, prevdup);
886                 if (!needscan) {
887                         /*
888                          * Has to be the case that entries 0 and 1 are
889                          * dfp and dfp2 (don't know which is which), and
890                          * entry 2 is empty.
891                          * Remove entry 1 first then entry 0.
892                          */
893                         ASSERT(dfp && dfp2);
894                         if (dfp == &bf[1]) {
895                                 dfp = &bf[0];
896                                 ASSERT(dfp2 == dfp);
897                                 dfp2 = &bf[1];
898                         }
899                         xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp);
900                         xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
901                         /*
902                          * Now insert the new entry.
903                          */
904                         dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup,
905                                                        needlogp);
906                         ASSERT(dfp == &bf[0]);
907                         ASSERT(dfp->length == prevdup->length);
908                         ASSERT(!dfp[1].length);
909                         ASSERT(!dfp[2].length);
910                 }
911         }
912         /*
913          * The entry before us is free, merge with it.
914          */
915         else if (prevdup) {
916                 dfp = xfs_dir2_data_freefind(hdr, bf, prevdup);
917                 be16_add_cpu(&prevdup->length, len);
918                 *xfs_dir2_data_unused_tag_p(prevdup) =
919                         cpu_to_be16((char *)prevdup - (char *)hdr);
920                 xfs_dir2_data_log_unused(args, bp, prevdup);
921                 /*
922                  * If the previous entry was in the table, the new entry
923                  * is longer, so it will be in the table too.  Remove
924                  * the old one and add the new one.
925                  */
926                 if (dfp) {
927                         xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
928                         xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp);
929                 }
930                 /*
931                  * Otherwise we need a scan if the new entry is big enough.
932                  */
933                 else {
934                         needscan = be16_to_cpu(prevdup->length) >
935                                    be16_to_cpu(bf[2].length);
936                 }
937         }
938         /*
939          * The following entry is free, merge with it.
940          */
941         else if (postdup) {
942                 dfp = xfs_dir2_data_freefind(hdr, bf, postdup);
943                 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
944                 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
945                 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
946                 *xfs_dir2_data_unused_tag_p(newdup) =
947                         cpu_to_be16((char *)newdup - (char *)hdr);
948                 xfs_dir2_data_log_unused(args, bp, newdup);
949                 /*
950                  * If the following entry was in the table, the new entry
951                  * is longer, so it will be in the table too.  Remove
952                  * the old one and add the new one.
953                  */
954                 if (dfp) {
955                         xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
956                         xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
957                 }
958                 /*
959                  * Otherwise we need a scan if the new entry is big enough.
960                  */
961                 else {
962                         needscan = be16_to_cpu(newdup->length) >
963                                    be16_to_cpu(bf[2].length);
964                 }
965         }
966         /*
967          * Neither neighbor is free.  Make a new entry.
968          */
969         else {
970                 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
971                 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
972                 newdup->length = cpu_to_be16(len);
973                 *xfs_dir2_data_unused_tag_p(newdup) =
974                         cpu_to_be16((char *)newdup - (char *)hdr);
975                 xfs_dir2_data_log_unused(args, bp, newdup);
976                 xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp);
977         }
978         *needscanp = needscan;
979 }
980
981 /* Check our free data for obvious signs of corruption. */
982 static inline xfs_failaddr_t
983 xfs_dir2_data_check_free(
984         struct xfs_dir2_data_hdr        *hdr,
985         struct xfs_dir2_data_unused     *dup,
986         xfs_dir2_data_aoff_t            offset,
987         xfs_dir2_data_aoff_t            len)
988 {
989         if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) &&
990             hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) &&
991             hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) &&
992             hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC))
993                 return __this_address;
994         if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG)
995                 return __this_address;
996         if (offset < (char *)dup - (char *)hdr)
997                 return __this_address;
998         if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr)
999                 return __this_address;
1000         if ((char *)dup - (char *)hdr !=
1001                         be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)))
1002                 return __this_address;
1003         return NULL;
1004 }
1005
1006 /* Sanity-check a new bestfree entry. */
1007 static inline xfs_failaddr_t
1008 xfs_dir2_data_check_new_free(
1009         struct xfs_dir2_data_hdr        *hdr,
1010         struct xfs_dir2_data_free       *dfp,
1011         struct xfs_dir2_data_unused     *newdup)
1012 {
1013         if (dfp == NULL)
1014                 return __this_address;
1015         if (dfp->length != newdup->length)
1016                 return __this_address;
1017         if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr)
1018                 return __this_address;
1019         return NULL;
1020 }
1021
1022 /*
1023  * Take a byte range out of an existing unused space and make it un-free.
1024  */
1025 int
1026 xfs_dir2_data_use_free(
1027         struct xfs_da_args      *args,
1028         struct xfs_buf          *bp,
1029         xfs_dir2_data_unused_t  *dup,           /* unused entry */
1030         xfs_dir2_data_aoff_t    offset,         /* starting offset to use */
1031         xfs_dir2_data_aoff_t    len,            /* length to use */
1032         int                     *needlogp,      /* out: need to log header */
1033         int                     *needscanp)     /* out: need regen bestfree */
1034 {
1035         xfs_dir2_data_hdr_t     *hdr;           /* data block header */
1036         xfs_dir2_data_free_t    *dfp;           /* bestfree pointer */
1037         xfs_dir2_data_unused_t  *newdup;        /* new unused entry */
1038         xfs_dir2_data_unused_t  *newdup2;       /* another new unused entry */
1039         struct xfs_dir2_data_free *bf;
1040         xfs_failaddr_t          fa;
1041         int                     matchback;      /* matches end of freespace */
1042         int                     matchfront;     /* matches start of freespace */
1043         int                     needscan;       /* need to regen bestfree */
1044         int                     oldlen;         /* old unused entry's length */
1045
1046         hdr = bp->b_addr;
1047         fa = xfs_dir2_data_check_free(hdr, dup, offset, len);
1048         if (fa)
1049                 goto corrupt;
1050         /*
1051          * Look up the entry in the bestfree table.
1052          */
1053         oldlen = be16_to_cpu(dup->length);
1054         bf = xfs_dir2_data_bestfree_p(args->dp->i_mount, hdr);
1055         dfp = xfs_dir2_data_freefind(hdr, bf, dup);
1056         ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
1057         /*
1058          * Check for alignment with front and back of the entry.
1059          */
1060         matchfront = (char *)dup - (char *)hdr == offset;
1061         matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
1062         ASSERT(*needscanp == 0);
1063         needscan = 0;
1064         /*
1065          * If we matched it exactly we just need to get rid of it from
1066          * the bestfree table.
1067          */
1068         if (matchfront && matchback) {
1069                 if (dfp) {
1070                         needscan = (bf[2].offset != 0);
1071                         if (!needscan)
1072                                 xfs_dir2_data_freeremove(hdr, bf, dfp,
1073                                                          needlogp);
1074                 }
1075         }
1076         /*
1077          * We match the first part of the entry.
1078          * Make a new entry with the remaining freespace.
1079          */
1080         else if (matchfront) {
1081                 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1082                 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1083                 newdup->length = cpu_to_be16(oldlen - len);
1084                 *xfs_dir2_data_unused_tag_p(newdup) =
1085                         cpu_to_be16((char *)newdup - (char *)hdr);
1086                 xfs_dir2_data_log_unused(args, bp, newdup);
1087                 /*
1088                  * If it was in the table, remove it and add the new one.
1089                  */
1090                 if (dfp) {
1091                         xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1092                         dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1093                                                        needlogp);
1094                         fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1095                         if (fa)
1096                                 goto corrupt;
1097                         /*
1098                          * If we got inserted at the last slot,
1099                          * that means we don't know if there was a better
1100                          * choice for the last slot, or not.  Rescan.
1101                          */
1102                         needscan = dfp == &bf[2];
1103                 }
1104         }
1105         /*
1106          * We match the last part of the entry.
1107          * Trim the allocated space off the tail of the entry.
1108          */
1109         else if (matchback) {
1110                 newdup = dup;
1111                 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1112                 *xfs_dir2_data_unused_tag_p(newdup) =
1113                         cpu_to_be16((char *)newdup - (char *)hdr);
1114                 xfs_dir2_data_log_unused(args, bp, newdup);
1115                 /*
1116                  * If it was in the table, remove it and add the new one.
1117                  */
1118                 if (dfp) {
1119                         xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp);
1120                         dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup,
1121                                                        needlogp);
1122                         fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup);
1123                         if (fa)
1124                                 goto corrupt;
1125                         /*
1126                          * If we got inserted at the last slot,
1127                          * that means we don't know if there was a better
1128                          * choice for the last slot, or not.  Rescan.
1129                          */
1130                         needscan = dfp == &bf[2];
1131                 }
1132         }
1133         /*
1134          * Poking out the middle of an entry.
1135          * Make two new entries.
1136          */
1137         else {
1138                 newdup = dup;
1139                 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
1140                 *xfs_dir2_data_unused_tag_p(newdup) =
1141                         cpu_to_be16((char *)newdup - (char *)hdr);
1142                 xfs_dir2_data_log_unused(args, bp, newdup);
1143                 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
1144                 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
1145                 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
1146                 *xfs_dir2_data_unused_tag_p(newdup2) =
1147                         cpu_to_be16((char *)newdup2 - (char *)hdr);
1148                 xfs_dir2_data_log_unused(args, bp, newdup2);
1149                 /*
1150                  * If the old entry was in the table, we need to scan
1151                  * if the 3rd entry was valid, since these entries
1152                  * are smaller than the old one.
1153                  * If we don't need to scan that means there were 1 or 2
1154                  * entries in the table, and removing the old and adding
1155                  * the 2 new will work.
1156                  */
1157                 if (dfp) {
1158                         needscan = (bf[2].length != 0);
1159                         if (!needscan) {
1160                                 xfs_dir2_data_freeremove(hdr, bf, dfp,
1161                                                          needlogp);
1162                                 xfs_dir2_data_freeinsert(hdr, bf, newdup,
1163                                                          needlogp);
1164                                 xfs_dir2_data_freeinsert(hdr, bf, newdup2,
1165                                                          needlogp);
1166                         }
1167                 }
1168         }
1169         *needscanp = needscan;
1170         return 0;
1171 corrupt:
1172         xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount,
1173                         hdr, sizeof(*hdr), __FILE__, __LINE__, fa);
1174         return -EFSCORRUPTED;
1175 }
1176
1177 /* Find the end of the entry data in a data/block format dir block. */
1178 unsigned int
1179 xfs_dir3_data_end_offset(
1180         struct xfs_da_geometry          *geo,
1181         struct xfs_dir2_data_hdr        *hdr)
1182 {
1183         void                            *p;
1184
1185         switch (hdr->magic) {
1186         case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
1187         case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
1188                 p = xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr));
1189                 return p - (void *)hdr;
1190         case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
1191         case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
1192                 return geo->blksize;
1193         default:
1194                 return 0;
1195         }
1196 }