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