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