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