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