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