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