]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/metadump.c
xfs_db: print transaction reservation type information
[thirdparty/xfsprogs-dev.git] / db / metadump.c
1 /*
2 * Copyright (c) 2007, 2011 SGI
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libxfs.h"
20 #include "libxlog.h"
21 #include "bmap.h"
22 #include "command.h"
23 #include "metadump.h"
24 #include "io.h"
25 #include "output.h"
26 #include "type.h"
27 #include "init.h"
28 #include "sig.h"
29 #include "xfs_metadump.h"
30 #include "fprint.h"
31 #include "faddr.h"
32 #include "field.h"
33 #include "dir2.h"
34
35 #define DEFAULT_MAX_EXT_SIZE MAXEXTLEN
36
37 /*
38 * It's possible that multiple files in a directory (or attributes
39 * in a file) produce the same obfuscated name. If that happens, we
40 * try to create another one. After several rounds of this though,
41 * we just give up and leave the original name as-is.
42 */
43 #define DUP_MAX 5 /* Max duplicates before we give up */
44
45 /* copy all metadata structures to/from a file */
46
47 static int metadump_f(int argc, char **argv);
48 static void metadump_help(void);
49
50 /*
51 * metadump commands issue info/wornings/errors to standard error as
52 * metadump supports stdout as a destination.
53 *
54 * All static functions return zero on failure, while the public functions
55 * return zero on success.
56 */
57
58 static const cmdinfo_t metadump_cmd =
59 { "metadump", NULL, metadump_f, 0, -1, 0,
60 N_("[-a] [-e] [-g] [-m max_extent] [-w] [-o] filename"),
61 N_("dump metadata to a file"), metadump_help };
62
63 static FILE *outf; /* metadump file */
64
65 static xfs_metablock_t *metablock; /* header + index + buffers */
66 static __be64 *block_index;
67 static char *block_buffer;
68
69 static int num_indices;
70 static int cur_index;
71
72 static xfs_ino_t cur_ino;
73
74 static int show_progress = 0;
75 static int stop_on_read_error = 0;
76 static int max_extent_size = DEFAULT_MAX_EXT_SIZE;
77 static int obfuscate = 1;
78 static int zero_stale_data = 1;
79 static int show_warnings = 0;
80 static int progress_since_warning = 0;
81 static bool stdout_metadump;
82
83 void
84 metadump_init(void)
85 {
86 add_command(&metadump_cmd);
87 }
88
89 static void
90 metadump_help(void)
91 {
92 dbprintf(_(
93 "\n"
94 " The 'metadump' command dumps the known metadata to a compact file suitable\n"
95 " for compressing and sending to an XFS maintainer for corruption analysis \n"
96 " or xfs_repair failures.\n\n"
97 " Options:\n"
98 " -a -- Copy full metadata blocks without zeroing unused space\n"
99 " -e -- Ignore read errors and keep going\n"
100 " -g -- Display dump progress\n"
101 " -m -- Specify max extent size in blocks to copy (default = %d blocks)\n"
102 " -o -- Don't obfuscate names and extended attributes\n"
103 " -w -- Show warnings of bad metadata information\n"
104 "\n"), DEFAULT_MAX_EXT_SIZE);
105 }
106
107 static void
108 print_warning(const char *fmt, ...)
109 {
110 char buf[200];
111 va_list ap;
112
113 if (seenint())
114 return;
115
116 va_start(ap, fmt);
117 vsnprintf(buf, sizeof(buf), fmt, ap);
118 va_end(ap);
119 buf[sizeof(buf)-1] = '\0';
120
121 fprintf(stderr, "%s%s: %s\n", progress_since_warning ? "\n" : "",
122 progname, buf);
123 progress_since_warning = 0;
124 }
125
126 static void
127 print_progress(const char *fmt, ...)
128 {
129 char buf[60];
130 va_list ap;
131 FILE *f;
132
133 if (seenint())
134 return;
135
136 va_start(ap, fmt);
137 vsnprintf(buf, sizeof(buf), fmt, ap);
138 va_end(ap);
139 buf[sizeof(buf)-1] = '\0';
140
141 f = stdout_metadump ? stderr : stdout;
142 fprintf(f, "\r%-59s", buf);
143 fflush(f);
144 progress_since_warning = 1;
145 }
146
147 /*
148 * A complete dump file will have a "zero" entry in the last index block,
149 * even if the dump is exactly aligned, the last index will be full of
150 * zeros. If the last index entry is non-zero, the dump is incomplete.
151 * Correspondingly, the last chunk will have a count < num_indices.
152 *
153 * Return 0 for success, -1 for failure.
154 */
155
156 static int
157 write_index(void)
158 {
159 /*
160 * write index block and following data blocks (streaming)
161 */
162 metablock->mb_count = cpu_to_be16(cur_index);
163 if (fwrite(metablock, (cur_index + 1) << BBSHIFT, 1, outf) != 1) {
164 print_warning("error writing to file: %s", strerror(errno));
165 return -errno;
166 }
167
168 memset(block_index, 0, num_indices * sizeof(__be64));
169 cur_index = 0;
170 return 0;
171 }
172
173 /*
174 * Return 0 for success, -errno for failure.
175 */
176 static int
177 write_buf_segment(
178 char *data,
179 int64_t off,
180 int len)
181 {
182 int i;
183 int ret;
184
185 for (i = 0; i < len; i++, off++, data += BBSIZE) {
186 block_index[cur_index] = cpu_to_be64(off);
187 memcpy(&block_buffer[cur_index << BBSHIFT], data, BBSIZE);
188 if (++cur_index == num_indices) {
189 ret = write_index();
190 if (ret)
191 return -EIO;
192 }
193 }
194 return 0;
195 }
196
197 /*
198 * we want to preserve the state of the metadata in the dump - whether it is
199 * intact or corrupt, so even if the buffer has a verifier attached to it we
200 * don't want to run it prior to writing the buffer to the metadump image.
201 *
202 * The only reason for running the verifier is to recalculate the CRCs on a
203 * buffer that has been obfuscated. i.e. a buffer than metadump modified itself.
204 * In this case, we only run the verifier if the buffer was not corrupt to begin
205 * with so that we don't accidentally correct buffers with CRC or errors in them
206 * when we are obfuscating them.
207 */
208 static int
209 write_buf(
210 iocur_t *buf)
211 {
212 struct xfs_buf *bp = buf->bp;
213 int i;
214 int ret;
215
216 /*
217 * Run the write verifier to recalculate the buffer CRCs and check
218 * metadump didn't introduce a new corruption. Warn if the verifier
219 * failed, but still continue to dump it into the output file.
220 */
221 if (buf->need_crc && bp && bp->b_ops && !bp->b_error) {
222 bp->b_ops->verify_write(bp);
223 if (bp->b_error) {
224 print_warning(
225 "obfuscation corrupted block at %s bno 0x%llx/0x%x",
226 bp->b_ops->name,
227 (long long)bp->b_bn, bp->b_bcount);
228 }
229 }
230
231 /* handle discontiguous buffers */
232 if (!buf->bbmap) {
233 ret = write_buf_segment(buf->data, buf->bb, buf->blen);
234 if (ret)
235 return ret;
236 } else {
237 int len = 0;
238 for (i = 0; i < buf->bbmap->nmaps; i++) {
239 ret = write_buf_segment(buf->data + BBTOB(len),
240 buf->bbmap->b[i].bm_bn,
241 buf->bbmap->b[i].bm_len);
242 if (ret)
243 return ret;
244 len += buf->bbmap->b[i].bm_len;
245 }
246 }
247 return seenint() ? -EINTR : 0;
248 }
249
250 /*
251 * We could be processing a corrupt block, so we can't trust any of
252 * the offsets or lengths to be within the buffer range. Hence check
253 * carefully!
254 */
255 static void
256 zero_btree_node(
257 struct xfs_btree_block *block,
258 typnm_t btype)
259 {
260 int nrecs;
261 xfs_bmbt_ptr_t *bpp;
262 xfs_bmbt_key_t *bkp;
263 xfs_inobt_ptr_t *ipp;
264 xfs_inobt_key_t *ikp;
265 xfs_alloc_ptr_t *app;
266 xfs_alloc_key_t *akp;
267 char *zp1, *zp2;
268 char *key_end;
269
270 nrecs = be16_to_cpu(block->bb_numrecs);
271 if (nrecs < 0)
272 return;
273
274 switch (btype) {
275 case TYP_BMAPBTA:
276 case TYP_BMAPBTD:
277 if (nrecs > mp->m_bmap_dmxr[1])
278 return;
279
280 bkp = XFS_BMBT_KEY_ADDR(mp, block, 1);
281 bpp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
282 zp1 = (char *)&bkp[nrecs];
283 zp2 = (char *)&bpp[nrecs];
284 key_end = (char *)bpp;
285 break;
286 case TYP_INOBT:
287 case TYP_FINOBT:
288 if (nrecs > mp->m_inobt_mxr[1])
289 return;
290
291 ikp = XFS_INOBT_KEY_ADDR(mp, block, 1);
292 ipp = XFS_INOBT_PTR_ADDR(mp, block, 1, mp->m_inobt_mxr[1]);
293 zp1 = (char *)&ikp[nrecs];
294 zp2 = (char *)&ipp[nrecs];
295 key_end = (char *)ipp;
296 break;
297 case TYP_BNOBT:
298 case TYP_CNTBT:
299 if (nrecs > mp->m_alloc_mxr[1])
300 return;
301
302 akp = XFS_ALLOC_KEY_ADDR(mp, block, 1);
303 app = XFS_ALLOC_PTR_ADDR(mp, block, 1, mp->m_alloc_mxr[1]);
304 zp1 = (char *)&akp[nrecs];
305 zp2 = (char *)&app[nrecs];
306 key_end = (char *)app;
307 break;
308 default:
309 return;
310 }
311
312
313 /* Zero from end of keys to beginning of pointers */
314 memset(zp1, 0, key_end - zp1);
315
316 /* Zero from end of pointers to end of block */
317 memset(zp2, 0, (char *)block + mp->m_sb.sb_blocksize - zp2);
318 }
319
320 /*
321 * We could be processing a corrupt block, so we can't trust any of
322 * the offsets or lengths to be within the buffer range. Hence check
323 * carefully!
324 */
325 static void
326 zero_btree_leaf(
327 struct xfs_btree_block *block,
328 typnm_t btype)
329 {
330 int nrecs;
331 struct xfs_bmbt_rec *brp;
332 struct xfs_inobt_rec *irp;
333 struct xfs_alloc_rec *arp;
334 char *zp;
335
336 nrecs = be16_to_cpu(block->bb_numrecs);
337 if (nrecs < 0)
338 return;
339
340 switch (btype) {
341 case TYP_BMAPBTA:
342 case TYP_BMAPBTD:
343 if (nrecs > mp->m_bmap_dmxr[0])
344 return;
345
346 brp = XFS_BMBT_REC_ADDR(mp, block, 1);
347 zp = (char *)&brp[nrecs];
348 break;
349 case TYP_INOBT:
350 case TYP_FINOBT:
351 if (nrecs > mp->m_inobt_mxr[0])
352 return;
353
354 irp = XFS_INOBT_REC_ADDR(mp, block, 1);
355 zp = (char *)&irp[nrecs];
356 break;
357 case TYP_BNOBT:
358 case TYP_CNTBT:
359 if (nrecs > mp->m_alloc_mxr[0])
360 return;
361
362 arp = XFS_ALLOC_REC_ADDR(mp, block, 1);
363 zp = (char *)&arp[nrecs];
364 break;
365 default:
366 return;
367 }
368
369 /* Zero from end of records to end of block */
370 memset(zp, 0, (char *)block + mp->m_sb.sb_blocksize - zp);
371 }
372
373 static void
374 zero_btree_block(
375 struct xfs_btree_block *block,
376 typnm_t btype)
377 {
378 int level;
379
380 level = be16_to_cpu(block->bb_level);
381
382 if (level > 0)
383 zero_btree_node(block, btype);
384 else
385 zero_btree_leaf(block, btype);
386 }
387
388 static int
389 scan_btree(
390 xfs_agnumber_t agno,
391 xfs_agblock_t agbno,
392 int level,
393 typnm_t btype,
394 void *arg,
395 int (*func)(struct xfs_btree_block *block,
396 xfs_agnumber_t agno,
397 xfs_agblock_t agbno,
398 int level,
399 typnm_t btype,
400 void *arg))
401 {
402 int rval = 0;
403
404 push_cur();
405 set_cur(&typtab[btype], XFS_AGB_TO_DADDR(mp, agno, agbno), blkbb,
406 DB_RING_IGN, NULL);
407 if (iocur_top->data == NULL) {
408 print_warning("cannot read %s block %u/%u", typtab[btype].name,
409 agno, agbno);
410 rval = !stop_on_read_error;
411 goto pop_out;
412 }
413
414 if (zero_stale_data) {
415 zero_btree_block(iocur_top->data, btype);
416 iocur_top->need_crc = 1;
417 }
418
419 if (write_buf(iocur_top))
420 goto pop_out;
421
422 if (!(*func)(iocur_top->data, agno, agbno, level - 1, btype, arg))
423 goto pop_out;
424 rval = 1;
425 pop_out:
426 pop_cur();
427 return rval;
428 }
429
430 /* free space tree copy routines */
431
432 static int
433 valid_bno(
434 xfs_agnumber_t agno,
435 xfs_agblock_t agbno)
436 {
437 if (agno < (mp->m_sb.sb_agcount - 1) && agbno > 0 &&
438 agbno <= mp->m_sb.sb_agblocks)
439 return 1;
440 if (agno == (mp->m_sb.sb_agcount - 1) && agbno > 0 &&
441 agbno <= (mp->m_sb.sb_dblocks -
442 (xfs_rfsblock_t)(mp->m_sb.sb_agcount - 1) *
443 mp->m_sb.sb_agblocks))
444 return 1;
445
446 return 0;
447 }
448
449
450 static int
451 scanfunc_freesp(
452 struct xfs_btree_block *block,
453 xfs_agnumber_t agno,
454 xfs_agblock_t agbno,
455 int level,
456 typnm_t btype,
457 void *arg)
458 {
459 xfs_alloc_ptr_t *pp;
460 int i;
461 int numrecs;
462
463 if (level == 0)
464 return 1;
465
466 numrecs = be16_to_cpu(block->bb_numrecs);
467 if (numrecs > mp->m_alloc_mxr[1]) {
468 if (show_warnings)
469 print_warning("invalid numrecs (%u) in %s block %u/%u",
470 numrecs, typtab[btype].name, agno, agbno);
471 return 1;
472 }
473
474 pp = XFS_ALLOC_PTR_ADDR(mp, block, 1, mp->m_alloc_mxr[1]);
475 for (i = 0; i < numrecs; i++) {
476 if (!valid_bno(agno, be32_to_cpu(pp[i]))) {
477 if (show_warnings)
478 print_warning("invalid block number (%u/%u) "
479 "in %s block %u/%u",
480 agno, be32_to_cpu(pp[i]),
481 typtab[btype].name, agno, agbno);
482 continue;
483 }
484 if (!scan_btree(agno, be32_to_cpu(pp[i]), level, btype, arg,
485 scanfunc_freesp))
486 return 0;
487 }
488 return 1;
489 }
490
491 static int
492 copy_free_bno_btree(
493 xfs_agnumber_t agno,
494 xfs_agf_t *agf)
495 {
496 xfs_agblock_t root;
497 int levels;
498
499 root = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
500 levels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
501
502 /* validate root and levels before processing the tree */
503 if (root == 0 || root > mp->m_sb.sb_agblocks) {
504 if (show_warnings)
505 print_warning("invalid block number (%u) in bnobt "
506 "root in agf %u", root, agno);
507 return 1;
508 }
509 if (levels >= XFS_BTREE_MAXLEVELS) {
510 if (show_warnings)
511 print_warning("invalid level (%u) in bnobt root "
512 "in agf %u", levels, agno);
513 return 1;
514 }
515
516 return scan_btree(agno, root, levels, TYP_BNOBT, agf, scanfunc_freesp);
517 }
518
519 static int
520 copy_free_cnt_btree(
521 xfs_agnumber_t agno,
522 xfs_agf_t *agf)
523 {
524 xfs_agblock_t root;
525 int levels;
526
527 root = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
528 levels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
529
530 /* validate root and levels before processing the tree */
531 if (root == 0 || root > mp->m_sb.sb_agblocks) {
532 if (show_warnings)
533 print_warning("invalid block number (%u) in cntbt "
534 "root in agf %u", root, agno);
535 return 1;
536 }
537 if (levels >= XFS_BTREE_MAXLEVELS) {
538 if (show_warnings)
539 print_warning("invalid level (%u) in cntbt root "
540 "in agf %u", levels, agno);
541 return 1;
542 }
543
544 return scan_btree(agno, root, levels, TYP_CNTBT, agf, scanfunc_freesp);
545 }
546
547 static int
548 scanfunc_rmapbt(
549 struct xfs_btree_block *block,
550 xfs_agnumber_t agno,
551 xfs_agblock_t agbno,
552 int level,
553 typnm_t btype,
554 void *arg)
555 {
556 xfs_rmap_ptr_t *pp;
557 int i;
558 int numrecs;
559
560 if (level == 0)
561 return 1;
562
563 numrecs = be16_to_cpu(block->bb_numrecs);
564 if (numrecs > mp->m_rmap_mxr[1]) {
565 if (show_warnings)
566 print_warning("invalid numrecs (%u) in %s block %u/%u",
567 numrecs, typtab[btype].name, agno, agbno);
568 return 1;
569 }
570
571 pp = XFS_RMAP_PTR_ADDR(block, 1, mp->m_rmap_mxr[1]);
572 for (i = 0; i < numrecs; i++) {
573 if (!valid_bno(agno, be32_to_cpu(pp[i]))) {
574 if (show_warnings)
575 print_warning("invalid block number (%u/%u) "
576 "in %s block %u/%u",
577 agno, be32_to_cpu(pp[i]),
578 typtab[btype].name, agno, agbno);
579 continue;
580 }
581 if (!scan_btree(agno, be32_to_cpu(pp[i]), level, btype, arg,
582 scanfunc_rmapbt))
583 return 0;
584 }
585 return 1;
586 }
587
588 static int
589 copy_rmap_btree(
590 xfs_agnumber_t agno,
591 struct xfs_agf *agf)
592 {
593 xfs_agblock_t root;
594 int levels;
595
596 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
597 return 1;
598
599 root = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
600 levels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
601
602 /* validate root and levels before processing the tree */
603 if (root == 0 || root > mp->m_sb.sb_agblocks) {
604 if (show_warnings)
605 print_warning("invalid block number (%u) in rmapbt "
606 "root in agf %u", root, agno);
607 return 1;
608 }
609 if (levels >= XFS_BTREE_MAXLEVELS) {
610 if (show_warnings)
611 print_warning("invalid level (%u) in rmapbt root "
612 "in agf %u", levels, agno);
613 return 1;
614 }
615
616 return scan_btree(agno, root, levels, TYP_RMAPBT, agf, scanfunc_rmapbt);
617 }
618
619 static int
620 scanfunc_refcntbt(
621 struct xfs_btree_block *block,
622 xfs_agnumber_t agno,
623 xfs_agblock_t agbno,
624 int level,
625 typnm_t btype,
626 void *arg)
627 {
628 xfs_refcount_ptr_t *pp;
629 int i;
630 int numrecs;
631
632 if (level == 0)
633 return 1;
634
635 numrecs = be16_to_cpu(block->bb_numrecs);
636 if (numrecs > mp->m_refc_mxr[1]) {
637 if (show_warnings)
638 print_warning("invalid numrecs (%u) in %s block %u/%u",
639 numrecs, typtab[btype].name, agno, agbno);
640 return 1;
641 }
642
643 pp = XFS_REFCOUNT_PTR_ADDR(block, 1, mp->m_refc_mxr[1]);
644 for (i = 0; i < numrecs; i++) {
645 if (!valid_bno(agno, be32_to_cpu(pp[i]))) {
646 if (show_warnings)
647 print_warning("invalid block number (%u/%u) "
648 "in %s block %u/%u",
649 agno, be32_to_cpu(pp[i]),
650 typtab[btype].name, agno, agbno);
651 continue;
652 }
653 if (!scan_btree(agno, be32_to_cpu(pp[i]), level, btype, arg,
654 scanfunc_refcntbt))
655 return 0;
656 }
657 return 1;
658 }
659
660 static int
661 copy_refcount_btree(
662 xfs_agnumber_t agno,
663 struct xfs_agf *agf)
664 {
665 xfs_agblock_t root;
666 int levels;
667
668 if (!xfs_sb_version_hasreflink(&mp->m_sb))
669 return 1;
670
671 root = be32_to_cpu(agf->agf_refcount_root);
672 levels = be32_to_cpu(agf->agf_refcount_level);
673
674 /* validate root and levels before processing the tree */
675 if (root == 0 || root > mp->m_sb.sb_agblocks) {
676 if (show_warnings)
677 print_warning("invalid block number (%u) in refcntbt "
678 "root in agf %u", root, agno);
679 return 1;
680 }
681 if (levels >= XFS_BTREE_MAXLEVELS) {
682 if (show_warnings)
683 print_warning("invalid level (%u) in refcntbt root "
684 "in agf %u", levels, agno);
685 return 1;
686 }
687
688 return scan_btree(agno, root, levels, TYP_REFCBT, agf, scanfunc_refcntbt);
689 }
690
691 /* filename and extended attribute obfuscation routines */
692
693 struct name_ent {
694 struct name_ent *next;
695 xfs_dahash_t hash;
696 int namelen;
697 unsigned char name[1];
698 };
699
700 #define NAME_TABLE_SIZE 4096
701
702 static struct name_ent *nametable[NAME_TABLE_SIZE];
703
704 static void
705 nametable_clear(void)
706 {
707 int i;
708 struct name_ent *ent;
709
710 for (i = 0; i < NAME_TABLE_SIZE; i++) {
711 while ((ent = nametable[i])) {
712 nametable[i] = ent->next;
713 free(ent);
714 }
715 }
716 }
717
718 /*
719 * See if the given name is already in the name table. If so,
720 * return a pointer to its entry, otherwise return a null pointer.
721 */
722 static struct name_ent *
723 nametable_find(xfs_dahash_t hash, int namelen, unsigned char *name)
724 {
725 struct name_ent *ent;
726
727 for (ent = nametable[hash % NAME_TABLE_SIZE]; ent; ent = ent->next) {
728 if (ent->hash == hash && ent->namelen == namelen &&
729 !memcmp(ent->name, name, namelen))
730 return ent;
731 }
732 return NULL;
733 }
734
735 /*
736 * Add the given name to the name table. Returns a pointer to the
737 * name's new entry, or a null pointer if an error occurs.
738 */
739 static struct name_ent *
740 nametable_add(xfs_dahash_t hash, int namelen, unsigned char *name)
741 {
742 struct name_ent *ent;
743
744 ent = malloc(sizeof *ent + namelen);
745 if (!ent)
746 return NULL;
747
748 ent->namelen = namelen;
749 memcpy(ent->name, name, namelen);
750 ent->hash = hash;
751 ent->next = nametable[hash % NAME_TABLE_SIZE];
752
753 nametable[hash % NAME_TABLE_SIZE] = ent;
754
755 return ent;
756 }
757
758 #define is_invalid_char(c) ((c) == '/' || (c) == '\0')
759 #define rol32(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
760
761 static inline unsigned char
762 random_filename_char(void)
763 {
764 static unsigned char filename_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
765 "abcdefghijklmnopqrstuvwxyz"
766 "0123456789-_";
767
768 return filename_alphabet[random() % (sizeof filename_alphabet - 1)];
769 }
770
771 #define ORPHANAGE "lost+found"
772 #define ORPHANAGE_LEN (sizeof (ORPHANAGE) - 1)
773
774 static inline int
775 is_orphanage_dir(
776 struct xfs_mount *mp,
777 xfs_ino_t dir_ino,
778 size_t name_len,
779 unsigned char *name)
780 {
781 return dir_ino == mp->m_sb.sb_rootino &&
782 name_len == ORPHANAGE_LEN &&
783 !memcmp(name, ORPHANAGE, ORPHANAGE_LEN);
784 }
785
786 /*
787 * Determine whether a name is one we shouldn't obfuscate because
788 * it's an orphan (or the "lost+found" directory itself). Note
789 * "cur_ino" is the inode for the directory currently being
790 * processed.
791 *
792 * Returns 1 if the name should NOT be obfuscated or 0 otherwise.
793 */
794 static int
795 in_lost_found(
796 xfs_ino_t ino,
797 int namelen,
798 unsigned char *name)
799 {
800 static xfs_ino_t orphanage_ino = 0;
801 char s[24]; /* 21 is enough (64 bits in decimal) */
802 int slen;
803
804 /* Record the "lost+found" inode if we haven't done so already */
805
806 ASSERT(ino != 0);
807 if (!orphanage_ino && is_orphanage_dir(mp, cur_ino, namelen, name))
808 orphanage_ino = ino;
809
810 /* We don't obfuscate the "lost+found" directory itself */
811
812 if (ino == orphanage_ino)
813 return 1;
814
815 /* Most files aren't in "lost+found" at all */
816
817 if (cur_ino != orphanage_ino)
818 return 0;
819
820 /*
821 * Within "lost+found", we don't obfuscate any file whose
822 * name is the same as its inode number. Any others are
823 * stray files and can be obfuscated.
824 */
825 slen = snprintf(s, sizeof (s), "%llu", (unsigned long long) ino);
826
827 return slen == namelen && !memcmp(name, s, namelen);
828 }
829
830 /*
831 * Given a name and its hash value, massage the name in such a way
832 * that the result is another name of equal length which shares the
833 * same hash value.
834 */
835 static void
836 obfuscate_name(
837 xfs_dahash_t hash,
838 size_t name_len,
839 unsigned char *name)
840 {
841 unsigned char *newp = name;
842 int i;
843 xfs_dahash_t new_hash = 0;
844 unsigned char *first;
845 unsigned char high_bit;
846 int shift;
847
848 /*
849 * Our obfuscation algorithm requires at least 5-character
850 * names, so don't bother if the name is too short. We
851 * work backward from a hash value to determine the last
852 * five bytes in a name required to produce a new name
853 * with the same hash.
854 */
855 if (name_len < 5)
856 return;
857
858 /*
859 * The beginning of the obfuscated name can be pretty much
860 * anything, so fill it in with random characters.
861 * Accumulate its new hash value as we go.
862 */
863 for (i = 0; i < name_len - 5; i++) {
864 *newp = random_filename_char();
865 new_hash = *newp ^ rol32(new_hash, 7);
866 newp++;
867 }
868
869 /*
870 * Compute which five bytes need to be used at the end of
871 * the name so the hash of the obfuscated name is the same
872 * as the hash of the original. If any result in an invalid
873 * character, flip a bit and arrange for a corresponding bit
874 * in a neighboring byte to be flipped as well. For the
875 * last byte, the "neighbor" to change is the first byte
876 * we're computing here.
877 */
878 new_hash = rol32(new_hash, 3) ^ hash;
879
880 first = newp;
881 high_bit = 0;
882 for (shift = 28; shift >= 0; shift -= 7) {
883 *newp = (new_hash >> shift & 0x7f) ^ high_bit;
884 if (is_invalid_char(*newp)) {
885 *newp ^= 1;
886 high_bit = 0x80;
887 } else
888 high_bit = 0;
889 ASSERT(!is_invalid_char(*newp));
890 newp++;
891 }
892
893 /*
894 * If we flipped a bit on the last byte, we need to fix up
895 * the matching bit in the first byte. The result will
896 * be a valid character, because we know that first byte
897 * has 0's in its upper four bits (it was produced by a
898 * 28-bit right-shift of a 32-bit unsigned value).
899 */
900 if (high_bit) {
901 *first ^= 0x10;
902 ASSERT(!is_invalid_char(*first));
903 }
904 ASSERT(libxfs_da_hashname(name, name_len) == hash);
905 }
906
907 /*
908 * Flip a bit in each of two bytes at the end of the given name.
909 * This is used in generating a series of alternate names to be used
910 * in the event a duplicate is found.
911 *
912 * The bits flipped are selected such that they both affect the same
913 * bit in the name's computed hash value, so flipping them both will
914 * preserve the hash.
915 *
916 * The following diagram aims to show the portion of a computed
917 * hash that a given byte of a name affects.
918 *
919 * 31 28 24 21 14 8 7 3 0
920 * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
921 * hash: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
922 * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
923 * last-4 ->| |<-- last-2 --->| |<--- last ---->|
924 * |<-- last-3 --->| |<-- last-1 --->| |<- last-4
925 * |<-- last-7 --->| |<-- last-5 --->|
926 * |<-- last-8 --->| |<-- last-6 --->|
927 * . . . and so on
928 *
929 * The last byte of the name directly affects the low-order byte of
930 * the hash. The next-to-last affects bits 7-14, the next one back
931 * affects bits 14-21, and so on. The effect wraps around when it
932 * goes beyond the top of the hash (as happens for byte last-4).
933 *
934 * Bits that are flipped together "overlap" on the hash value. As
935 * an example of overlap, the last two bytes both affect bit 7 in
936 * the hash. That pair of bytes (and their overlapping bits) can be
937 * used for this "flip bit" operation (it's the first pair tried,
938 * actually).
939 *
940 * A table defines overlapping pairs--the bytes involved and bits
941 * within them--that can be used this way. The byte offset is
942 * relative to a starting point within the name, which will be set
943 * to affect the bytes at the end of the name. The function is
944 * called with a "bitseq" value which indicates which bit flip is
945 * desired, and this translates directly into selecting which entry
946 * in the bit_to_flip[] table to apply.
947 *
948 * The function returns 1 if the operation was successful. It
949 * returns 0 if the result produced a character that's not valid in
950 * a name (either '/' or a '\0'). Finally, it returns -1 if the bit
951 * sequence number is beyond what is supported for a name of this
952 * length.
953 *
954 * Discussion
955 * ----------
956 * (Also see the discussion above find_alternate(), below.)
957 *
958 * In order to make this function work for any length name, the
959 * table is ordered by increasing byte offset, so that the earliest
960 * entries can apply to the shortest strings. This way all names
961 * are done consistently.
962 *
963 * When bit flips occur, they can convert printable characters
964 * into non-printable ones. In an effort to reduce the impact of
965 * this, the first bit flips are chosen to affect bytes the end of
966 * the name (and furthermore, toward the low bits of a byte). Those
967 * bytes are often non-printable anyway because of the way they are
968 * initially selected by obfuscate_name()). This is accomplished,
969 * using later table entries first.
970 *
971 * Each row in the table doubles the number of alternates that
972 * can be generated. A two-byte name is limited to using only
973 * the first row, so it's possible to generate two alternates
974 * (the original name, plus the alternate produced by flipping
975 * the one pair of bits). In a 5-byte name, the effect of the
976 * first byte overlaps the last by 4 its, and there are 8 bits
977 * to flip, allowing for 256 possible alternates.
978 *
979 * Short names (less than 5 bytes) are never even obfuscated, so for
980 * such names the relatively small number of alternates should never
981 * really be a problem.
982 *
983 * Long names (more than 6 bytes, say) are not likely to exhaust
984 * the number of available alternates. In fact, the table could
985 * probably have stopped at 8 entries, on the assumption that 256
986 * alternates should be enough for most any situation. The entries
987 * beyond those are present mostly for demonstration of how it could
988 * be populated with more entries, should it ever be necessary to do
989 * so.
990 */
991 static int
992 flip_bit(
993 size_t name_len,
994 unsigned char *name,
995 uint32_t bitseq)
996 {
997 int index;
998 size_t offset;
999 unsigned char *p0, *p1;
1000 unsigned char m0, m1;
1001 struct {
1002 int byte; /* Offset from start within name */
1003 unsigned char bit; /* Bit within that byte */
1004 } bit_to_flip[][2] = { /* Sorted by second entry's byte */
1005 { { 0, 0 }, { 1, 7 } }, /* Each row defines a pair */
1006 { { 1, 0 }, { 2, 7 } }, /* of bytes and a bit within */
1007 { { 2, 0 }, { 3, 7 } }, /* each byte. Each bit in */
1008 { { 0, 4 }, { 4, 0 } }, /* a pair affects the same */
1009 { { 0, 5 }, { 4, 1 } }, /* bit in the hash, so flipping */
1010 { { 0, 6 }, { 4, 2 } }, /* both will change the name */
1011 { { 0, 7 }, { 4, 3 } }, /* while preserving the hash. */
1012 { { 3, 0 }, { 4, 7 } },
1013 { { 0, 0 }, { 5, 3 } }, /* The first entry's byte offset */
1014 { { 0, 1 }, { 5, 4 } }, /* must be less than the second. */
1015 { { 0, 2 }, { 5, 5 } },
1016 { { 0, 3 }, { 5, 6 } }, /* The table can be extended to */
1017 { { 0, 4 }, { 5, 7 } }, /* an arbitrary number of entries */
1018 { { 4, 0 }, { 5, 7 } }, /* but there's not much point. */
1019 /* . . . */
1020 };
1021
1022 /* Find the first entry *not* usable for name of this length */
1023
1024 for (index = 0; index < ARRAY_SIZE(bit_to_flip); index++)
1025 if (bit_to_flip[index][1].byte >= name_len)
1026 break;
1027
1028 /*
1029 * Back up to the last usable entry. If that number is
1030 * smaller than the bit sequence number, inform the caller
1031 * that nothing this large (or larger) will work.
1032 */
1033 if (bitseq > --index)
1034 return -1;
1035
1036 /*
1037 * We will be switching bits at the end of name, with a
1038 * preference for affecting the last bytes first. Compute
1039 * where in the name we'll start applying the changes.
1040 */
1041 offset = name_len - (bit_to_flip[index][1].byte + 1);
1042 index -= bitseq; /* Use later table entries first */
1043
1044 p0 = name + offset + bit_to_flip[index][0].byte;
1045 p1 = name + offset + bit_to_flip[index][1].byte;
1046 m0 = 1 << bit_to_flip[index][0].bit;
1047 m1 = 1 << bit_to_flip[index][1].bit;
1048
1049 /* Only change the bytes if it produces valid characters */
1050
1051 if (is_invalid_char(*p0 ^ m0) || is_invalid_char(*p1 ^ m1))
1052 return 0;
1053
1054 *p0 ^= m0;
1055 *p1 ^= m1;
1056
1057 return 1;
1058 }
1059
1060 /*
1061 * This function generates a well-defined sequence of "alternate"
1062 * names for a given name. An alternate is a name having the same
1063 * length and same hash value as the original name. This is needed
1064 * because the algorithm produces only one obfuscated name to use
1065 * for a given original name, and it's possible that result matches
1066 * a name already seen. This function checks for this, and if it
1067 * occurs, finds another suitable obfuscated name to use.
1068 *
1069 * Each bit in the binary representation of the sequence number is
1070 * used to select one possible "bit flip" operation to perform on
1071 * the name. So for example:
1072 * seq = 0: selects no bits to flip
1073 * seq = 1: selects the 0th bit to flip
1074 * seq = 2: selects the 1st bit to flip
1075 * seq = 3: selects the 0th and 1st bit to flip
1076 * ... and so on.
1077 *
1078 * The flip_bit() function takes care of the details of the bit
1079 * flipping within the name. Note that the "1st bit" in this
1080 * context is a bit sequence number; i.e. it doesn't necessarily
1081 * mean bit 0x02 will be changed.
1082 *
1083 * If a valid name (one that contains no '/' or '\0' characters) is
1084 * produced by this process for the given sequence number, this
1085 * function returns 1. If the result is not valid, it returns 0.
1086 * Returns -1 if the sequence number is beyond the the maximum for
1087 * names of the given length.
1088 *
1089 *
1090 * Discussion
1091 * ----------
1092 * The number of alternates available for a given name is dependent
1093 * on its length. A "bit flip" involves inverting two bits in
1094 * a name--the two bits being selected such that their values
1095 * affect the name's hash value in the same way. Alternates are
1096 * thus generated by inverting the value of pairs of such
1097 * "overlapping" bits in the original name. Each byte after the
1098 * first in a name adds at least one bit of overlap to work with.
1099 * (See comments above flip_bit() for more discussion on this.)
1100 *
1101 * So the number of alternates is dependent on the number of such
1102 * overlapping bits in a name. If there are N bit overlaps, there
1103 * 2^N alternates for that hash value.
1104 *
1105 * Here are the number of overlapping bits available for generating
1106 * alternates for names of specific lengths:
1107 * 1 0 (must have 2 bytes to have any overlap)
1108 * 2 1 One bit overlaps--so 2 possible alternates
1109 * 3 2 Two bits overlap--so 4 possible alternates
1110 * 4 4 Three bits overlap, so 2^3 alternates
1111 * 5 8 8 bits overlap (due to wrapping), 256 alternates
1112 * 6 18 2^18 alternates
1113 * 7 28 2^28 alternates
1114 * ...
1115 * It's clear that the number of alternates grows very quickly with
1116 * the length of the name. But note that the set of alternates
1117 * includes invalid names. And for certain (contrived) names, the
1118 * number of valid names is a fairly small fraction of the total
1119 * number of alternates.
1120 *
1121 * The main driver for this infrastructure for coming up with
1122 * alternate names is really related to names 5 (or possibly 6)
1123 * bytes in length. 5-byte obfuscated names contain no randomly-
1124 * generated bytes in them, and the chance of an obfuscated name
1125 * matching an already-seen name is too high to just ignore. This
1126 * methodical selection of alternates ensures we don't produce
1127 * duplicate names unless we have exhausted our options.
1128 */
1129 static int
1130 find_alternate(
1131 size_t name_len,
1132 unsigned char *name,
1133 uint32_t seq)
1134 {
1135 uint32_t bitseq = 0;
1136 uint32_t bits = seq;
1137
1138 if (!seq)
1139 return 1; /* alternate 0 is the original name */
1140 if (name_len < 2) /* Must have 2 bytes to flip */
1141 return -1;
1142
1143 for (bitseq = 0; bits; bitseq++) {
1144 uint32_t mask = 1 << bitseq;
1145 int fb;
1146
1147 if (!(bits & mask))
1148 continue;
1149
1150 fb = flip_bit(name_len, name, bitseq);
1151 if (fb < 1)
1152 return fb ? -1 : 0;
1153 bits ^= mask;
1154 }
1155
1156 return 1;
1157 }
1158
1159 /*
1160 * Look up the given name in the name table. If it is already
1161 * present, iterate through a well-defined sequence of alternate
1162 * names and attempt to use an alternate name instead.
1163 *
1164 * Returns 1 if the (possibly modified) name is not present in the
1165 * name table. Returns 0 if the name and all possible alternates
1166 * are already in the table.
1167 */
1168 static int
1169 handle_duplicate_name(xfs_dahash_t hash, size_t name_len, unsigned char *name)
1170 {
1171 unsigned char new_name[name_len + 1];
1172 uint32_t seq = 1;
1173
1174 if (!nametable_find(hash, name_len, name))
1175 return 1; /* No duplicate */
1176
1177 /* Name is already in use. Need to find an alternate. */
1178
1179 do {
1180 int found;
1181
1182 /* Only change incoming name if we find an alternate */
1183 do {
1184 memcpy(new_name, name, name_len);
1185 found = find_alternate(name_len, new_name, seq++);
1186 if (found < 0)
1187 return 0; /* No more to check */
1188 } while (!found);
1189 } while (nametable_find(hash, name_len, new_name));
1190
1191 /*
1192 * The alternate wasn't in the table already. Pass it back
1193 * to the caller.
1194 */
1195 memcpy(name, new_name, name_len);
1196
1197 return 1;
1198 }
1199
1200 static void
1201 generate_obfuscated_name(
1202 xfs_ino_t ino,
1203 int namelen,
1204 unsigned char *name)
1205 {
1206 xfs_dahash_t hash;
1207
1208 /*
1209 * We don't obfuscate "lost+found" or any orphan files
1210 * therein. When the name table is used for extended
1211 * attributes, the inode number provided is 0, in which
1212 * case we don't need to make this check.
1213 */
1214 if (ino && in_lost_found(ino, namelen, name))
1215 return;
1216
1217 /*
1218 * If the name starts with a slash, just skip over it. It
1219 * isn't included in the hash and we don't record it in the
1220 * name table. Note that the namelen value passed in does
1221 * not count the leading slash (if one is present).
1222 */
1223 if (*name == '/')
1224 name++;
1225
1226 /* Obfuscate the name (if possible) */
1227
1228 hash = libxfs_da_hashname(name, namelen);
1229 obfuscate_name(hash, namelen, name);
1230
1231 /*
1232 * Make sure the name is not something already seen. If we
1233 * fail to find a suitable alternate, we're dealing with a
1234 * very pathological situation, and we may end up creating
1235 * a duplicate name in the metadump, so issue a warning.
1236 */
1237 if (!handle_duplicate_name(hash, namelen, name)) {
1238 print_warning("duplicate name for inode %llu "
1239 "in dir inode %llu\n",
1240 (unsigned long long) ino,
1241 (unsigned long long) cur_ino);
1242 return;
1243 }
1244
1245 /* Create an entry for the new name in the name table. */
1246
1247 if (!nametable_add(hash, namelen, name))
1248 print_warning("unable to record name for inode %llu "
1249 "in dir inode %llu\n",
1250 (unsigned long long) ino,
1251 (unsigned long long) cur_ino);
1252 }
1253
1254 static void
1255 process_sf_dir(
1256 xfs_dinode_t *dip)
1257 {
1258 struct xfs_dir2_sf_hdr *sfp;
1259 xfs_dir2_sf_entry_t *sfep;
1260 uint64_t ino_dir_size;
1261 int i;
1262
1263 sfp = (struct xfs_dir2_sf_hdr *)XFS_DFORK_DPTR(dip);
1264 ino_dir_size = be64_to_cpu(dip->di_size);
1265 if (ino_dir_size > XFS_DFORK_DSIZE(dip, mp)) {
1266 ino_dir_size = XFS_DFORK_DSIZE(dip, mp);
1267 if (show_warnings)
1268 print_warning("invalid size in dir inode %llu",
1269 (long long)cur_ino);
1270 }
1271
1272 sfep = xfs_dir2_sf_firstentry(sfp);
1273 for (i = 0; (i < sfp->count) &&
1274 ((char *)sfep - (char *)sfp < ino_dir_size); i++) {
1275
1276 /*
1277 * first check for bad name lengths. If they are bad, we
1278 * have limitations to how much can be obfuscated.
1279 */
1280 int namelen = sfep->namelen;
1281
1282 if (namelen == 0) {
1283 if (show_warnings)
1284 print_warning("zero length entry in dir inode "
1285 "%llu", (long long)cur_ino);
1286 if (i != sfp->count - 1)
1287 break;
1288 namelen = ino_dir_size - ((char *)&sfep->name[0] -
1289 (char *)sfp);
1290 } else if ((char *)sfep - (char *)sfp +
1291 M_DIROPS(mp)->sf_entsize(sfp, sfep->namelen) >
1292 ino_dir_size) {
1293 if (show_warnings)
1294 print_warning("entry length in dir inode %llu "
1295 "overflows space", (long long)cur_ino);
1296 if (i != sfp->count - 1)
1297 break;
1298 namelen = ino_dir_size - ((char *)&sfep->name[0] -
1299 (char *)sfp);
1300 }
1301
1302 if (obfuscate)
1303 generate_obfuscated_name(
1304 M_DIROPS(mp)->sf_get_ino(sfp, sfep),
1305 namelen, &sfep->name[0]);
1306
1307 sfep = (xfs_dir2_sf_entry_t *)((char *)sfep +
1308 M_DIROPS(mp)->sf_entsize(sfp, namelen));
1309 }
1310
1311 /* zero stale data in rest of space in data fork, if any */
1312 if (zero_stale_data && (ino_dir_size < XFS_DFORK_DSIZE(dip, mp)))
1313 memset(sfep, 0, XFS_DFORK_DSIZE(dip, mp) - ino_dir_size);
1314 }
1315
1316 /*
1317 * The pathname may not be null terminated. It may be terminated by the end of
1318 * a buffer or inode literal area, and the start of the next region contains
1319 * unknown data. Therefore, when we get to the last component of the symlink, we
1320 * cannot assume that strlen() will give us the right result. Hence we need to
1321 * track the remaining pathname length and use that instead.
1322 */
1323 static void
1324 obfuscate_path_components(
1325 char *buf,
1326 uint64_t len)
1327 {
1328 unsigned char *comp = (unsigned char *)buf;
1329 unsigned char *end = comp + len;
1330 xfs_dahash_t hash;
1331
1332 while (comp < end) {
1333 char *slash;
1334 int namelen;
1335
1336 /* find slash at end of this component */
1337 slash = strchr((char *)comp, '/');
1338 if (!slash) {
1339 /* last (or single) component */
1340 namelen = strnlen((char *)comp, len);
1341 hash = libxfs_da_hashname(comp, namelen);
1342 obfuscate_name(hash, namelen, comp);
1343 break;
1344 }
1345 namelen = slash - (char *)comp;
1346 /* handle leading or consecutive slashes */
1347 if (!namelen) {
1348 comp++;
1349 len--;
1350 continue;
1351 }
1352 hash = libxfs_da_hashname(comp, namelen);
1353 obfuscate_name(hash, namelen, comp);
1354 comp += namelen + 1;
1355 len -= namelen + 1;
1356 }
1357 }
1358
1359 static void
1360 process_sf_symlink(
1361 xfs_dinode_t *dip)
1362 {
1363 uint64_t len;
1364 char *buf;
1365
1366 len = be64_to_cpu(dip->di_size);
1367 if (len > XFS_DFORK_DSIZE(dip, mp)) {
1368 if (show_warnings)
1369 print_warning("invalid size (%d) in symlink inode %llu",
1370 len, (long long)cur_ino);
1371 len = XFS_DFORK_DSIZE(dip, mp);
1372 }
1373
1374 buf = (char *)XFS_DFORK_DPTR(dip);
1375 if (obfuscate)
1376 obfuscate_path_components(buf, len);
1377
1378 /* zero stale data in rest of space in data fork, if any */
1379 if (zero_stale_data && len < XFS_DFORK_DSIZE(dip, mp))
1380 memset(&buf[len], 0, XFS_DFORK_DSIZE(dip, mp) - len);
1381 }
1382
1383 static void
1384 process_sf_attr(
1385 xfs_dinode_t *dip)
1386 {
1387 /*
1388 * with extended attributes, obfuscate the names and fill the actual
1389 * values with 'v' (to see a valid string length, as opposed to NULLs)
1390 */
1391
1392 xfs_attr_shortform_t *asfp;
1393 xfs_attr_sf_entry_t *asfep;
1394 int ino_attr_size;
1395 int i;
1396
1397 asfp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
1398 if (asfp->hdr.count == 0)
1399 return;
1400
1401 ino_attr_size = be16_to_cpu(asfp->hdr.totsize);
1402 if (ino_attr_size > XFS_DFORK_ASIZE(dip, mp)) {
1403 ino_attr_size = XFS_DFORK_ASIZE(dip, mp);
1404 if (show_warnings)
1405 print_warning("invalid attr size in inode %llu",
1406 (long long)cur_ino);
1407 }
1408
1409 asfep = &asfp->list[0];
1410 for (i = 0; (i < asfp->hdr.count) &&
1411 ((char *)asfep - (char *)asfp < ino_attr_size); i++) {
1412
1413 int namelen = asfep->namelen;
1414
1415 if (namelen == 0) {
1416 if (show_warnings)
1417 print_warning("zero length attr entry in inode "
1418 "%llu", (long long)cur_ino);
1419 break;
1420 } else if ((char *)asfep - (char *)asfp +
1421 XFS_ATTR_SF_ENTSIZE(asfep) > ino_attr_size) {
1422 if (show_warnings)
1423 print_warning("attr entry length in inode %llu "
1424 "overflows space", (long long)cur_ino);
1425 break;
1426 }
1427
1428 if (obfuscate) {
1429 generate_obfuscated_name(0, asfep->namelen,
1430 &asfep->nameval[0]);
1431 memset(&asfep->nameval[asfep->namelen], 'v',
1432 asfep->valuelen);
1433 }
1434
1435 asfep = (xfs_attr_sf_entry_t *)((char *)asfep +
1436 XFS_ATTR_SF_ENTSIZE(asfep));
1437 }
1438
1439 /* zero stale data in rest of space in attr fork, if any */
1440 if (zero_stale_data && (ino_attr_size < XFS_DFORK_ASIZE(dip, mp)))
1441 memset(asfep, 0, XFS_DFORK_ASIZE(dip, mp) - ino_attr_size);
1442 }
1443
1444 static void
1445 process_dir_leaf_block(
1446 char *block)
1447 {
1448 struct xfs_dir2_leaf *leaf;
1449 struct xfs_dir3_icleaf_hdr leafhdr;
1450
1451 if (!zero_stale_data)
1452 return;
1453
1454 /* Yes, this works for dir2 & dir3. Difference is padding. */
1455 leaf = (struct xfs_dir2_leaf *)block;
1456 M_DIROPS(mp)->leaf_hdr_from_disk(&leafhdr, leaf);
1457
1458 /* Zero out space from end of ents[] to bests */
1459 if (leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
1460 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
1461 struct xfs_dir2_leaf_tail *ltp;
1462 __be16 *lbp;
1463 struct xfs_dir2_leaf_entry *ents;
1464 char *free; /* end of ents */
1465
1466 ents = M_DIROPS(mp)->leaf_ents_p(leaf);
1467 free = (char *)&ents[leafhdr.count];
1468 ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
1469 lbp = xfs_dir2_leaf_bests_p(ltp);
1470 memset(free, 0, (char *)lbp - free);
1471 iocur_top->need_crc = 1;
1472 }
1473 }
1474
1475 static void
1476 process_dir_data_block(
1477 char *block,
1478 xfs_fileoff_t offset,
1479 int is_block_format)
1480 {
1481 /*
1482 * we have to rely on the fileoffset and signature of the block to
1483 * handle it's contents. If it's invalid, leave it alone.
1484 * for multi-fsblock dir blocks, if a name crosses an extent boundary,
1485 * ignore it and continue.
1486 */
1487 int dir_offset;
1488 char *ptr;
1489 char *endptr;
1490 int end_of_data;
1491 int wantmagic;
1492 struct xfs_dir2_data_hdr *datahdr;
1493
1494 datahdr = (struct xfs_dir2_data_hdr *)block;
1495
1496 if (is_block_format) {
1497 xfs_dir2_leaf_entry_t *blp;
1498 xfs_dir2_block_tail_t *btp;
1499
1500 btp = xfs_dir2_block_tail_p(mp->m_dir_geo, datahdr);
1501 blp = xfs_dir2_block_leaf_p(btp);
1502 if ((char *)blp > (char *)btp)
1503 blp = (xfs_dir2_leaf_entry_t *)btp;
1504
1505 end_of_data = (char *)blp - block;
1506 if (xfs_sb_version_hascrc(&mp->m_sb))
1507 wantmagic = XFS_DIR3_BLOCK_MAGIC;
1508 else
1509 wantmagic = XFS_DIR2_BLOCK_MAGIC;
1510 } else { /* leaf/node format */
1511 end_of_data = mp->m_dir_geo->fsbcount << mp->m_sb.sb_blocklog;
1512 if (xfs_sb_version_hascrc(&mp->m_sb))
1513 wantmagic = XFS_DIR3_DATA_MAGIC;
1514 else
1515 wantmagic = XFS_DIR2_DATA_MAGIC;
1516 }
1517
1518 if (be32_to_cpu(datahdr->magic) != wantmagic) {
1519 if (show_warnings)
1520 print_warning(
1521 "invalid magic in dir inode %llu block %ld",
1522 (long long)cur_ino, (long)offset);
1523 return;
1524 }
1525
1526 dir_offset = M_DIROPS(mp)->data_entry_offset;
1527 ptr = block + dir_offset;
1528 endptr = block + mp->m_dir_geo->blksize;
1529
1530 while (ptr < endptr && dir_offset < end_of_data) {
1531 xfs_dir2_data_entry_t *dep;
1532 xfs_dir2_data_unused_t *dup;
1533 int length;
1534
1535 dup = (xfs_dir2_data_unused_t *)ptr;
1536
1537 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1538 int length = be16_to_cpu(dup->length);
1539 if (dir_offset + length > end_of_data ||
1540 !length || (length & (XFS_DIR2_DATA_ALIGN - 1))) {
1541 if (show_warnings)
1542 print_warning(
1543 "invalid length for dir free space in inode %llu",
1544 (long long)cur_ino);
1545 return;
1546 }
1547 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
1548 dir_offset)
1549 return;
1550 dir_offset += length;
1551 ptr += length;
1552 /*
1553 * Zero the unused space up to the tag - the tag is
1554 * actually at a variable offset, so zeroing &dup->tag
1555 * is zeroing the free space in between
1556 */
1557 if (zero_stale_data) {
1558 int zlen = length -
1559 sizeof(xfs_dir2_data_unused_t);
1560
1561 if (zlen > 0) {
1562 memset(&dup->tag, 0, zlen);
1563 iocur_top->need_crc = 1;
1564 }
1565 }
1566 if (dir_offset >= end_of_data || ptr >= endptr)
1567 return;
1568 }
1569
1570 dep = (xfs_dir2_data_entry_t *)ptr;
1571 length = M_DIROPS(mp)->data_entsize(dep->namelen);
1572
1573 if (dir_offset + length > end_of_data ||
1574 ptr + length > endptr) {
1575 if (show_warnings)
1576 print_warning(
1577 "invalid length for dir entry name in inode %llu",
1578 (long long)cur_ino);
1579 return;
1580 }
1581 if (be16_to_cpu(*M_DIROPS(mp)->data_entry_tag_p(dep)) !=
1582 dir_offset)
1583 return;
1584
1585 if (obfuscate)
1586 generate_obfuscated_name(be64_to_cpu(dep->inumber),
1587 dep->namelen, &dep->name[0]);
1588 dir_offset += length;
1589 ptr += length;
1590 /* Zero the unused space after name, up to the tag */
1591 if (zero_stale_data) {
1592 /* 1 byte for ftype; don't bother with conditional */
1593 int zlen =
1594 (char *)M_DIROPS(mp)->data_entry_tag_p(dep) -
1595 (char *)&dep->name[dep->namelen] - 1;
1596 if (zlen > 0) {
1597 memset(&dep->name[dep->namelen] + 1, 0, zlen);
1598 iocur_top->need_crc = 1;
1599 }
1600 }
1601 }
1602 }
1603
1604 static void
1605 process_symlink_block(
1606 char *block)
1607 {
1608 char *link = block;
1609
1610 if (xfs_sb_version_hascrc(&(mp)->m_sb))
1611 link += sizeof(struct xfs_dsymlink_hdr);
1612
1613 if (obfuscate)
1614 obfuscate_path_components(link, XFS_SYMLINK_BUF_SPACE(mp,
1615 mp->m_sb.sb_blocksize));
1616 if (zero_stale_data) {
1617 size_t linklen, zlen;
1618
1619 linklen = strlen(link);
1620 zlen = mp->m_sb.sb_blocksize - linklen;
1621 if (xfs_sb_version_hascrc(&mp->m_sb))
1622 zlen -= sizeof(struct xfs_dsymlink_hdr);
1623 if (zlen < mp->m_sb.sb_blocksize)
1624 memset(link + linklen, 0, zlen);
1625 }
1626 }
1627
1628 #define MAX_REMOTE_VALS 4095
1629
1630 static struct attr_data_s {
1631 int remote_val_count;
1632 xfs_dablk_t remote_vals[MAX_REMOTE_VALS];
1633 } attr_data;
1634
1635 static inline void
1636 add_remote_vals(
1637 xfs_dablk_t blockidx,
1638 int length)
1639 {
1640 while (length > 0 && attr_data.remote_val_count < MAX_REMOTE_VALS) {
1641 attr_data.remote_vals[attr_data.remote_val_count] = blockidx;
1642 attr_data.remote_val_count++;
1643 blockidx++;
1644 length -= XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
1645 }
1646
1647 if (attr_data.remote_val_count >= MAX_REMOTE_VALS) {
1648 print_warning(
1649 "Overflowed attr obfuscation array. No longer obfuscating remote attrs.");
1650 }
1651 }
1652
1653 /* Handle remote and leaf attributes */
1654 static void
1655 process_attr_block(
1656 char *block,
1657 xfs_fileoff_t offset)
1658 {
1659 struct xfs_attr_leafblock *leaf;
1660 struct xfs_attr3_icleaf_hdr hdr;
1661 int i;
1662 int nentries;
1663 xfs_attr_leaf_entry_t *entry;
1664 xfs_attr_leaf_name_local_t *local;
1665 xfs_attr_leaf_name_remote_t *remote;
1666 uint32_t bs = mp->m_sb.sb_blocksize;
1667 char *first_name;
1668
1669
1670 leaf = (xfs_attr_leafblock_t *)block;
1671
1672 /* Remote attributes - attr3 has XFS_ATTR3_RMT_MAGIC, attr has none */
1673 if ((be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC) &&
1674 (be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR3_LEAF_MAGIC)) {
1675 for (i = 0; i < attr_data.remote_val_count; i++) {
1676 if (obfuscate && attr_data.remote_vals[i] == offset)
1677 /* Macros to handle both attr and attr3 */
1678 memset(block +
1679 (bs - XFS_ATTR3_RMT_BUF_SPACE(mp, bs)),
1680 'v', XFS_ATTR3_RMT_BUF_SPACE(mp, bs));
1681 }
1682 return;
1683 }
1684
1685 /* Ok, it's a leaf - get header; accounts for crc & non-crc */
1686 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &hdr, leaf);
1687
1688 nentries = hdr.count;
1689 if (nentries == 0 ||
1690 nentries * sizeof(xfs_attr_leaf_entry_t) +
1691 xfs_attr3_leaf_hdr_size(leaf) >
1692 XFS_ATTR3_RMT_BUF_SPACE(mp, bs)) {
1693 if (show_warnings)
1694 print_warning("invalid attr count in inode %llu",
1695 (long long)cur_ino);
1696 return;
1697 }
1698
1699 entry = xfs_attr3_leaf_entryp(leaf);
1700 /* We will move this as we parse */
1701 first_name = NULL;
1702 for (i = 0; i < nentries; i++, entry++) {
1703 int nlen, vlen, zlen;
1704
1705 /* Grows up; if this name is topmost, move first_name */
1706 if (!first_name || xfs_attr3_leaf_name(leaf, i) < first_name)
1707 first_name = xfs_attr3_leaf_name(leaf, i);
1708
1709 if (be16_to_cpu(entry->nameidx) > mp->m_sb.sb_blocksize) {
1710 if (show_warnings)
1711 print_warning(
1712 "invalid attr nameidx in inode %llu",
1713 (long long)cur_ino);
1714 break;
1715 }
1716 if (entry->flags & XFS_ATTR_LOCAL) {
1717 local = xfs_attr3_leaf_name_local(leaf, i);
1718 if (local->namelen == 0) {
1719 if (show_warnings)
1720 print_warning(
1721 "zero length for attr name in inode %llu",
1722 (long long)cur_ino);
1723 break;
1724 }
1725 if (obfuscate) {
1726 generate_obfuscated_name(0, local->namelen,
1727 &local->nameval[0]);
1728 memset(&local->nameval[local->namelen], 'v',
1729 be16_to_cpu(local->valuelen));
1730 }
1731 /* zero from end of nameval[] to next name start */
1732 nlen = local->namelen;
1733 vlen = be16_to_cpu(local->valuelen);
1734 zlen = xfs_attr_leaf_entsize_local(nlen, vlen) -
1735 (sizeof(xfs_attr_leaf_name_local_t) - 1 +
1736 nlen + vlen);
1737 if (zero_stale_data)
1738 memset(&local->nameval[nlen + vlen], 0, zlen);
1739 } else {
1740 remote = xfs_attr3_leaf_name_remote(leaf, i);
1741 if (remote->namelen == 0 || remote->valueblk == 0) {
1742 if (show_warnings)
1743 print_warning(
1744 "invalid attr entry in inode %llu",
1745 (long long)cur_ino);
1746 break;
1747 }
1748 if (obfuscate) {
1749 generate_obfuscated_name(0, remote->namelen,
1750 &remote->name[0]);
1751 add_remote_vals(be32_to_cpu(remote->valueblk),
1752 be32_to_cpu(remote->valuelen));
1753 }
1754 /* zero from end of name[] to next name start */
1755 nlen = remote->namelen;
1756 zlen = xfs_attr_leaf_entsize_remote(nlen) -
1757 (sizeof(xfs_attr_leaf_name_remote_t) - 1 +
1758 nlen);
1759 if (zero_stale_data)
1760 memset(&remote->name[nlen], 0, zlen);
1761 }
1762 }
1763
1764 /* Zero from end of entries array to the first name/val */
1765 if (zero_stale_data) {
1766 struct xfs_attr_leaf_entry *entries;
1767
1768 entries = xfs_attr3_leaf_entryp(leaf);
1769 memset(&entries[nentries], 0,
1770 first_name - (char *)&entries[nentries]);
1771 }
1772 }
1773
1774 /* Processes symlinks, attrs, directories ... */
1775 static int
1776 process_single_fsb_objects(
1777 xfs_fileoff_t o,
1778 xfs_fsblock_t s,
1779 xfs_filblks_t c,
1780 typnm_t btype,
1781 xfs_fileoff_t last)
1782 {
1783 char *dp;
1784 int ret = 0;
1785 int i;
1786
1787 for (i = 0; i < c; i++) {
1788 push_cur();
1789 set_cur(&typtab[btype], XFS_FSB_TO_DADDR(mp, s), blkbb,
1790 DB_RING_IGN, NULL);
1791
1792 if (!iocur_top->data) {
1793 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, s);
1794 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, s);
1795
1796 print_warning("cannot read %s block %u/%u (%llu)",
1797 typtab[btype].name, agno, agbno, s);
1798 if (stop_on_read_error)
1799 ret = -EIO;
1800 goto out_pop;
1801
1802 }
1803
1804 if (!obfuscate && !zero_stale_data)
1805 goto write;
1806
1807 /* Zero unused part of interior nodes */
1808 if (zero_stale_data) {
1809 xfs_da_intnode_t *node = iocur_top->data;
1810 int magic = be16_to_cpu(node->hdr.info.magic);
1811
1812 if (magic == XFS_DA_NODE_MAGIC ||
1813 magic == XFS_DA3_NODE_MAGIC) {
1814 struct xfs_da3_icnode_hdr hdr;
1815 int used;
1816
1817 M_DIROPS(mp)->node_hdr_from_disk(&hdr, node);
1818 used = M_DIROPS(mp)->node_hdr_size;
1819
1820 used += hdr.count
1821 * sizeof(struct xfs_da_node_entry);
1822
1823 if (used < mp->m_sb.sb_blocksize) {
1824 memset((char *)node + used, 0,
1825 mp->m_sb.sb_blocksize - used);
1826 iocur_top->need_crc = 1;
1827 }
1828 }
1829 }
1830
1831 /* Handle leaf nodes */
1832 dp = iocur_top->data;
1833 switch (btype) {
1834 case TYP_DIR2:
1835 if (o >= mp->m_dir_geo->freeblk) {
1836 /* TODO, zap any stale data */
1837 break;
1838 } else if (o >= mp->m_dir_geo->leafblk) {
1839 process_dir_leaf_block(dp);
1840 } else {
1841 process_dir_data_block(dp, o,
1842 last == mp->m_dir_geo->fsbcount);
1843 }
1844 iocur_top->need_crc = 1;
1845 break;
1846 case TYP_SYMLINK:
1847 process_symlink_block(dp);
1848 iocur_top->need_crc = 1;
1849 break;
1850 case TYP_ATTR:
1851 process_attr_block(dp, o);
1852 iocur_top->need_crc = 1;
1853 break;
1854 default:
1855 break;
1856 }
1857
1858 write:
1859 ret = write_buf(iocur_top);
1860 out_pop:
1861 pop_cur();
1862 if (ret)
1863 break;
1864 o++;
1865 s++;
1866 }
1867
1868 return ret;
1869 }
1870
1871 /*
1872 * Static map to aggregate multiple extents into a single directory block.
1873 */
1874 static struct bbmap mfsb_map;
1875 static int mfsb_length;
1876
1877 static int
1878 process_multi_fsb_objects(
1879 xfs_fileoff_t o,
1880 xfs_fsblock_t s,
1881 xfs_filblks_t c,
1882 typnm_t btype,
1883 xfs_fileoff_t last)
1884 {
1885 int ret = 0;
1886
1887 switch (btype) {
1888 case TYP_DIR2:
1889 break;
1890 default:
1891 print_warning("bad type for multi-fsb object %d", btype);
1892 return -EINVAL;
1893 }
1894
1895 while (c > 0) {
1896 unsigned int bm_len;
1897
1898 if (mfsb_length + c >= mp->m_dir_geo->fsbcount) {
1899 bm_len = mp->m_dir_geo->fsbcount - mfsb_length;
1900 mfsb_length = 0;
1901 } else {
1902 mfsb_length += c;
1903 bm_len = c;
1904 }
1905
1906 mfsb_map.b[mfsb_map.nmaps].bm_bn = XFS_FSB_TO_DADDR(mp, s);
1907 mfsb_map.b[mfsb_map.nmaps].bm_len = XFS_FSB_TO_BB(mp, bm_len);
1908 mfsb_map.nmaps++;
1909
1910 if (mfsb_length == 0) {
1911 push_cur();
1912 set_cur(&typtab[btype], 0, 0, DB_RING_IGN, &mfsb_map);
1913 if (!iocur_top->data) {
1914 xfs_agnumber_t agno = XFS_FSB_TO_AGNO(mp, s);
1915 xfs_agblock_t agbno = XFS_FSB_TO_AGBNO(mp, s);
1916
1917 print_warning("cannot read %s block %u/%u (%llu)",
1918 typtab[btype].name, agno, agbno, s);
1919 if (stop_on_read_error)
1920 ret = -1;
1921 goto out_pop;
1922
1923 }
1924
1925 if ((!obfuscate && !zero_stale_data) ||
1926 o >= mp->m_dir_geo->leafblk) {
1927 ret = write_buf(iocur_top);
1928 goto out_pop;
1929 }
1930
1931 process_dir_data_block(iocur_top->data, o,
1932 last == mp->m_dir_geo->fsbcount);
1933 iocur_top->need_crc = 1;
1934 ret = write_buf(iocur_top);
1935 out_pop:
1936 pop_cur();
1937 mfsb_map.nmaps = 0;
1938 if (ret)
1939 break;
1940 }
1941 c -= bm_len;
1942 s += bm_len;
1943 }
1944
1945 return ret;
1946 }
1947
1948 /* inode copy routines */
1949 static int
1950 process_bmbt_reclist(
1951 xfs_bmbt_rec_t *rp,
1952 int numrecs,
1953 typnm_t btype)
1954 {
1955 int i;
1956 xfs_fileoff_t o, op = NULLFILEOFF;
1957 xfs_fsblock_t s;
1958 xfs_filblks_t c, cp = NULLFILEOFF;
1959 int f;
1960 xfs_fileoff_t last;
1961 xfs_agnumber_t agno;
1962 xfs_agblock_t agbno;
1963 int error;
1964
1965 if (btype == TYP_DATA)
1966 return 1;
1967
1968 convert_extent(&rp[numrecs - 1], &o, &s, &c, &f);
1969 last = o + c;
1970
1971 for (i = 0; i < numrecs; i++, rp++) {
1972 convert_extent(rp, &o, &s, &c, &f);
1973
1974 /*
1975 * ignore extents that are clearly bogus, and if a bogus
1976 * one is found, stop processing remaining extents
1977 */
1978 if (i > 0 && op + cp > o) {
1979 if (show_warnings)
1980 print_warning("bmap extent %d in %s ino %llu "
1981 "starts at %llu, previous extent "
1982 "ended at %llu", i,
1983 typtab[btype].name, (long long)cur_ino,
1984 o, op + cp - 1);
1985 break;
1986 }
1987
1988 if (c > max_extent_size) {
1989 /*
1990 * since we are only processing non-data extents,
1991 * large numbers of blocks in a metadata extent is
1992 * extremely rare and more than likely to be corrupt.
1993 */
1994 if (show_warnings)
1995 print_warning("suspicious count %u in bmap "
1996 "extent %d in %s ino %llu", c, i,
1997 typtab[btype].name, (long long)cur_ino);
1998 break;
1999 }
2000
2001 op = o;
2002 cp = c;
2003
2004 agno = XFS_FSB_TO_AGNO(mp, s);
2005 agbno = XFS_FSB_TO_AGBNO(mp, s);
2006
2007 if (!valid_bno(agno, agbno)) {
2008 if (show_warnings)
2009 print_warning("invalid block number %u/%u "
2010 "(%llu) in bmap extent %d in %s ino "
2011 "%llu", agno, agbno, s, i,
2012 typtab[btype].name, (long long)cur_ino);
2013 break;
2014 }
2015
2016 if (!valid_bno(agno, agbno + c - 1)) {
2017 if (show_warnings)
2018 print_warning("bmap extent %i in %s inode %llu "
2019 "overflows AG (end is %u/%u)", i,
2020 typtab[btype].name, (long long)cur_ino,
2021 agno, agbno + c - 1);
2022 break;
2023 }
2024
2025 /* multi-extent blocks require special handling */
2026 if (btype != TYP_DIR2 || mp->m_dir_geo->fsbcount == 1) {
2027 error = process_single_fsb_objects(o, s, c, btype, last);
2028 } else {
2029 error = process_multi_fsb_objects(o, s, c, btype, last);
2030 }
2031 if (error)
2032 return 0;
2033 }
2034
2035 return 1;
2036 }
2037
2038 static int
2039 scanfunc_bmap(
2040 struct xfs_btree_block *block,
2041 xfs_agnumber_t agno,
2042 xfs_agblock_t agbno,
2043 int level,
2044 typnm_t btype,
2045 void *arg) /* ptr to itype */
2046 {
2047 int i;
2048 xfs_bmbt_ptr_t *pp;
2049 int nrecs;
2050
2051 nrecs = be16_to_cpu(block->bb_numrecs);
2052
2053 if (level == 0) {
2054 if (nrecs > mp->m_bmap_dmxr[0]) {
2055 if (show_warnings)
2056 print_warning("invalid numrecs (%u) in %s "
2057 "block %u/%u", nrecs,
2058 typtab[btype].name, agno, agbno);
2059 return 1;
2060 }
2061 return process_bmbt_reclist(XFS_BMBT_REC_ADDR(mp, block, 1),
2062 nrecs, *(typnm_t*)arg);
2063 }
2064
2065 if (nrecs > mp->m_bmap_dmxr[1]) {
2066 if (show_warnings)
2067 print_warning("invalid numrecs (%u) in %s block %u/%u",
2068 nrecs, typtab[btype].name, agno, agbno);
2069 return 1;
2070 }
2071 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
2072 for (i = 0; i < nrecs; i++) {
2073 xfs_agnumber_t ag;
2074 xfs_agblock_t bno;
2075
2076 ag = XFS_FSB_TO_AGNO(mp, get_unaligned_be64(&pp[i]));
2077 bno = XFS_FSB_TO_AGBNO(mp, get_unaligned_be64(&pp[i]));
2078
2079 if (bno == 0 || bno > mp->m_sb.sb_agblocks ||
2080 ag > mp->m_sb.sb_agcount) {
2081 if (show_warnings)
2082 print_warning("invalid block number (%u/%u) "
2083 "in %s block %u/%u", ag, bno,
2084 typtab[btype].name, agno, agbno);
2085 continue;
2086 }
2087
2088 if (!scan_btree(ag, bno, level, btype, arg, scanfunc_bmap))
2089 return 0;
2090 }
2091 return 1;
2092 }
2093
2094 static int
2095 process_btinode(
2096 xfs_dinode_t *dip,
2097 typnm_t itype)
2098 {
2099 xfs_bmdr_block_t *dib;
2100 int i;
2101 xfs_bmbt_ptr_t *pp;
2102 int level;
2103 int nrecs;
2104 int maxrecs;
2105 int whichfork;
2106 typnm_t btype;
2107
2108 whichfork = (itype == TYP_ATTR) ? XFS_ATTR_FORK : XFS_DATA_FORK;
2109 btype = (itype == TYP_ATTR) ? TYP_BMAPBTA : TYP_BMAPBTD;
2110
2111 dib = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
2112 level = be16_to_cpu(dib->bb_level);
2113 nrecs = be16_to_cpu(dib->bb_numrecs);
2114
2115 if (level > XFS_BM_MAXLEVELS(mp, whichfork)) {
2116 if (show_warnings)
2117 print_warning("invalid level (%u) in inode %lld %s "
2118 "root", level, (long long)cur_ino,
2119 typtab[btype].name);
2120 return 1;
2121 }
2122
2123 if (level == 0) {
2124 return process_bmbt_reclist(XFS_BMDR_REC_ADDR(dib, 1),
2125 nrecs, itype);
2126 }
2127
2128 maxrecs = libxfs_bmdr_maxrecs(XFS_DFORK_SIZE(dip, mp, whichfork), 0);
2129 if (nrecs > maxrecs) {
2130 if (show_warnings)
2131 print_warning("invalid numrecs (%u) in inode %lld %s "
2132 "root", nrecs, (long long)cur_ino,
2133 typtab[btype].name);
2134 return 1;
2135 }
2136
2137 pp = XFS_BMDR_PTR_ADDR(dib, 1, maxrecs);
2138 for (i = 0; i < nrecs; i++) {
2139 xfs_agnumber_t ag;
2140 xfs_agblock_t bno;
2141
2142 ag = XFS_FSB_TO_AGNO(mp, get_unaligned_be64(&pp[i]));
2143 bno = XFS_FSB_TO_AGBNO(mp, get_unaligned_be64(&pp[i]));
2144
2145 if (bno == 0 || bno > mp->m_sb.sb_agblocks ||
2146 ag > mp->m_sb.sb_agcount) {
2147 if (show_warnings)
2148 print_warning("invalid block number (%u/%u) "
2149 "in inode %llu %s root", ag,
2150 bno, (long long)cur_ino,
2151 typtab[btype].name);
2152 continue;
2153 }
2154
2155 if (!scan_btree(ag, bno, level, btype, &itype, scanfunc_bmap))
2156 return 0;
2157 }
2158 return 1;
2159 }
2160
2161 static int
2162 process_exinode(
2163 xfs_dinode_t *dip,
2164 typnm_t itype)
2165 {
2166 int whichfork;
2167 int used;
2168 xfs_extnum_t nex;
2169
2170 whichfork = (itype == TYP_ATTR) ? XFS_ATTR_FORK : XFS_DATA_FORK;
2171
2172 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
2173 used = nex * sizeof(xfs_bmbt_rec_t);
2174 if (nex < 0 || used > XFS_DFORK_SIZE(dip, mp, whichfork)) {
2175 if (show_warnings)
2176 print_warning("bad number of extents %d in inode %lld",
2177 nex, (long long)cur_ino);
2178 return 1;
2179 }
2180
2181 /* Zero unused data fork past used extents */
2182 if (zero_stale_data && (used < XFS_DFORK_SIZE(dip, mp, whichfork)))
2183 memset(XFS_DFORK_PTR(dip, whichfork) + used, 0,
2184 XFS_DFORK_SIZE(dip, mp, whichfork) - used);
2185
2186
2187 return process_bmbt_reclist((xfs_bmbt_rec_t *)XFS_DFORK_PTR(dip,
2188 whichfork), nex, itype);
2189 }
2190
2191 static int
2192 process_inode_data(
2193 xfs_dinode_t *dip,
2194 typnm_t itype)
2195 {
2196 switch (dip->di_format) {
2197 case XFS_DINODE_FMT_LOCAL:
2198 if (obfuscate || zero_stale_data)
2199 switch (itype) {
2200 case TYP_DIR2:
2201 process_sf_dir(dip);
2202 break;
2203
2204 case TYP_SYMLINK:
2205 process_sf_symlink(dip);
2206 break;
2207
2208 default: ;
2209 }
2210 break;
2211
2212 case XFS_DINODE_FMT_EXTENTS:
2213 return process_exinode(dip, itype);
2214
2215 case XFS_DINODE_FMT_BTREE:
2216 return process_btinode(dip, itype);
2217 }
2218 return 1;
2219 }
2220
2221 /*
2222 * when we process the inode, we may change the data in the data and/or
2223 * attribute fork if they are in short form and we are obfuscating names.
2224 * In this case we need to recalculate the CRC of the inode, but we should
2225 * only do that if the CRC in the inode is good to begin with. If the crc
2226 * is not ok, we just leave it alone.
2227 */
2228 static int
2229 process_inode(
2230 xfs_agnumber_t agno,
2231 xfs_agino_t agino,
2232 xfs_dinode_t *dip,
2233 bool free_inode)
2234 {
2235 int success;
2236 bool crc_was_ok = false; /* no recalc by default */
2237 bool need_new_crc = false;
2238
2239 success = 1;
2240 cur_ino = XFS_AGINO_TO_INO(mp, agno, agino);
2241
2242 /* we only care about crc recalculation if we will modify the inode. */
2243 if (obfuscate || zero_stale_data) {
2244 crc_was_ok = libxfs_verify_cksum((char *)dip,
2245 mp->m_sb.sb_inodesize,
2246 offsetof(struct xfs_dinode, di_crc));
2247 }
2248
2249 if (free_inode) {
2250 if (zero_stale_data) {
2251 /* Zero all of the inode literal area */
2252 memset(XFS_DFORK_DPTR(dip), 0,
2253 XFS_LITINO(mp, dip->di_version));
2254 }
2255 goto done;
2256 }
2257
2258 /* copy appropriate data fork metadata */
2259 switch (be16_to_cpu(dip->di_mode) & S_IFMT) {
2260 case S_IFDIR:
2261 success = process_inode_data(dip, TYP_DIR2);
2262 if (dip->di_format == XFS_DINODE_FMT_LOCAL)
2263 need_new_crc = 1;
2264 break;
2265 case S_IFLNK:
2266 success = process_inode_data(dip, TYP_SYMLINK);
2267 if (dip->di_format == XFS_DINODE_FMT_LOCAL)
2268 need_new_crc = 1;
2269 break;
2270 case S_IFREG:
2271 success = process_inode_data(dip, TYP_DATA);
2272 break;
2273 default: ;
2274 }
2275 nametable_clear();
2276
2277 /* copy extended attributes if they exist and forkoff is valid */
2278 if (success &&
2279 XFS_DFORK_DSIZE(dip, mp) < XFS_LITINO(mp, dip->di_version)) {
2280 attr_data.remote_val_count = 0;
2281 switch (dip->di_aformat) {
2282 case XFS_DINODE_FMT_LOCAL:
2283 need_new_crc = 1;
2284 if (obfuscate || zero_stale_data)
2285 process_sf_attr(dip);
2286 break;
2287
2288 case XFS_DINODE_FMT_EXTENTS:
2289 success = process_exinode(dip, TYP_ATTR);
2290 break;
2291
2292 case XFS_DINODE_FMT_BTREE:
2293 success = process_btinode(dip, TYP_ATTR);
2294 break;
2295 }
2296 nametable_clear();
2297 }
2298
2299 done:
2300 /* Heavy handed but low cost; just do it as a catch-all. */
2301 if (zero_stale_data)
2302 need_new_crc = 1;
2303
2304 if (crc_was_ok && need_new_crc)
2305 libxfs_dinode_calc_crc(mp, dip);
2306 return success;
2307 }
2308
2309 static uint32_t inodes_copied;
2310
2311 static int
2312 copy_inode_chunk(
2313 xfs_agnumber_t agno,
2314 xfs_inobt_rec_t *rp)
2315 {
2316 xfs_agino_t agino;
2317 int off;
2318 xfs_agblock_t agbno;
2319 xfs_agblock_t end_agbno;
2320 int i;
2321 int rval = 0;
2322 int blks_per_buf;
2323 int inodes_per_buf;
2324 int ioff;
2325
2326 agino = be32_to_cpu(rp->ir_startino);
2327 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2328 end_agbno = agbno + mp->m_ialloc_blks;
2329 off = XFS_INO_TO_OFFSET(mp, agino);
2330
2331 /*
2332 * If the fs supports sparse inode records, we must process inodes a
2333 * cluster at a time because that is the sparse allocation granularity.
2334 * Otherwise, we risk CRC corruption errors on reads of inode chunks.
2335 *
2336 * Also make sure that that we don't process more than the single record
2337 * we've been passed (large block sizes can hold multiple inode chunks).
2338 */
2339 if (xfs_sb_version_hassparseinodes(&mp->m_sb))
2340 blks_per_buf = xfs_icluster_size_fsb(mp);
2341 else
2342 blks_per_buf = mp->m_ialloc_blks;
2343 inodes_per_buf = min(blks_per_buf << mp->m_sb.sb_inopblog,
2344 XFS_INODES_PER_CHUNK);
2345
2346 /*
2347 * Sanity check that we only process a single buffer if ir_startino has
2348 * a buffer offset. A non-zero offset implies that the entire chunk lies
2349 * within a block.
2350 */
2351 if (off && inodes_per_buf != XFS_INODES_PER_CHUNK) {
2352 print_warning("bad starting inode offset %d", off);
2353 return 0;
2354 }
2355
2356 if (agino == 0 || agino == NULLAGINO || !valid_bno(agno, agbno) ||
2357 !valid_bno(agno, XFS_AGINO_TO_AGBNO(mp,
2358 agino + XFS_INODES_PER_CHUNK - 1))) {
2359 if (show_warnings)
2360 print_warning("bad inode number %llu (%u/%u)",
2361 XFS_AGINO_TO_INO(mp, agno, agino), agno, agino);
2362 return 1;
2363 }
2364
2365 /*
2366 * check for basic assumptions about inode chunks, and if any
2367 * assumptions fail, don't process the inode chunk.
2368 */
2369 if ((mp->m_sb.sb_inopblock <= XFS_INODES_PER_CHUNK && off != 0) ||
2370 (mp->m_sb.sb_inopblock > XFS_INODES_PER_CHUNK &&
2371 off % XFS_INODES_PER_CHUNK != 0) ||
2372 (xfs_sb_version_hasalign(&mp->m_sb) &&
2373 mp->m_sb.sb_inoalignmt != 0 &&
2374 agbno % mp->m_sb.sb_inoalignmt != 0)) {
2375 if (show_warnings)
2376 print_warning("badly aligned inode (start = %llu)",
2377 XFS_AGINO_TO_INO(mp, agno, agino));
2378 return 1;
2379 }
2380
2381 push_cur();
2382 ioff = 0;
2383 while (agbno < end_agbno && ioff < XFS_INODES_PER_CHUNK) {
2384 if (xfs_inobt_is_sparse_disk(rp, ioff))
2385 goto next_bp;
2386
2387 set_cur(&typtab[TYP_INODE], XFS_AGB_TO_DADDR(mp, agno, agbno),
2388 XFS_FSB_TO_BB(mp, blks_per_buf), DB_RING_IGN, NULL);
2389 if (iocur_top->data == NULL) {
2390 print_warning("cannot read inode block %u/%u",
2391 agno, agbno);
2392 rval = !stop_on_read_error;
2393 goto pop_out;
2394 }
2395
2396 for (i = 0; i < inodes_per_buf; i++) {
2397 xfs_dinode_t *dip;
2398
2399 dip = (xfs_dinode_t *)((char *)iocur_top->data +
2400 ((off + i) << mp->m_sb.sb_inodelog));
2401
2402 /* process_inode handles free inodes, too */
2403 if (!process_inode(agno, agino + ioff + i, dip,
2404 XFS_INOBT_IS_FREE_DISK(rp, ioff + i)))
2405 goto pop_out;
2406
2407 inodes_copied++;
2408 }
2409
2410 if (write_buf(iocur_top))
2411 goto pop_out;
2412
2413 next_bp:
2414 agbno += blks_per_buf;
2415 ioff += inodes_per_buf;
2416 }
2417
2418 if (show_progress)
2419 print_progress("Copied %u of %u inodes (%u of %u AGs)",
2420 inodes_copied, mp->m_sb.sb_icount, agno,
2421 mp->m_sb.sb_agcount);
2422 rval = 1;
2423 pop_out:
2424 pop_cur();
2425 return rval;
2426 }
2427
2428 static int
2429 scanfunc_ino(
2430 struct xfs_btree_block *block,
2431 xfs_agnumber_t agno,
2432 xfs_agblock_t agbno,
2433 int level,
2434 typnm_t btype,
2435 void *arg)
2436 {
2437 xfs_inobt_rec_t *rp;
2438 xfs_inobt_ptr_t *pp;
2439 int i;
2440 int numrecs;
2441 int finobt = *(int *) arg;
2442
2443 numrecs = be16_to_cpu(block->bb_numrecs);
2444
2445 if (level == 0) {
2446 if (numrecs > mp->m_inobt_mxr[0]) {
2447 if (show_warnings)
2448 print_warning("invalid numrecs %d in %s "
2449 "block %u/%u", numrecs,
2450 typtab[btype].name, agno, agbno);
2451 numrecs = mp->m_inobt_mxr[0];
2452 }
2453
2454 /*
2455 * Only copy the btree blocks for the finobt. The inobt scan
2456 * copies the inode chunks.
2457 */
2458 if (finobt)
2459 return 1;
2460
2461 rp = XFS_INOBT_REC_ADDR(mp, block, 1);
2462 for (i = 0; i < numrecs; i++, rp++) {
2463 if (!copy_inode_chunk(agno, rp))
2464 return 0;
2465 }
2466 return 1;
2467 }
2468
2469 if (numrecs > mp->m_inobt_mxr[1]) {
2470 if (show_warnings)
2471 print_warning("invalid numrecs %d in %s block %u/%u",
2472 numrecs, typtab[btype].name, agno, agbno);
2473 numrecs = mp->m_inobt_mxr[1];
2474 }
2475
2476 pp = XFS_INOBT_PTR_ADDR(mp, block, 1, mp->m_inobt_mxr[1]);
2477 for (i = 0; i < numrecs; i++) {
2478 if (!valid_bno(agno, be32_to_cpu(pp[i]))) {
2479 if (show_warnings)
2480 print_warning("invalid block number (%u/%u) "
2481 "in %s block %u/%u",
2482 agno, be32_to_cpu(pp[i]),
2483 typtab[btype].name, agno, agbno);
2484 continue;
2485 }
2486 if (!scan_btree(agno, be32_to_cpu(pp[i]), level,
2487 btype, arg, scanfunc_ino))
2488 return 0;
2489 }
2490 return 1;
2491 }
2492
2493 static int
2494 copy_inodes(
2495 xfs_agnumber_t agno,
2496 xfs_agi_t *agi)
2497 {
2498 xfs_agblock_t root;
2499 int levels;
2500 int finobt = 0;
2501
2502 root = be32_to_cpu(agi->agi_root);
2503 levels = be32_to_cpu(agi->agi_level);
2504
2505 /* validate root and levels before processing the tree */
2506 if (root == 0 || root > mp->m_sb.sb_agblocks) {
2507 if (show_warnings)
2508 print_warning("invalid block number (%u) in inobt "
2509 "root in agi %u", root, agno);
2510 return 1;
2511 }
2512 if (levels >= XFS_BTREE_MAXLEVELS) {
2513 if (show_warnings)
2514 print_warning("invalid level (%u) in inobt root "
2515 "in agi %u", levels, agno);
2516 return 1;
2517 }
2518
2519 if (!scan_btree(agno, root, levels, TYP_INOBT, &finobt, scanfunc_ino))
2520 return 0;
2521
2522 if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
2523 root = be32_to_cpu(agi->agi_free_root);
2524 levels = be32_to_cpu(agi->agi_free_level);
2525
2526 finobt = 1;
2527 if (!scan_btree(agno, root, levels, TYP_INOBT, &finobt,
2528 scanfunc_ino))
2529 return 0;
2530 }
2531
2532 return 1;
2533 }
2534
2535 static int
2536 scan_ag(
2537 xfs_agnumber_t agno)
2538 {
2539 xfs_agf_t *agf;
2540 xfs_agi_t *agi;
2541 int stack_count = 0;
2542 int rval = 0;
2543
2544 /* copy the superblock of the AG */
2545 push_cur();
2546 stack_count++;
2547 set_cur(&typtab[TYP_SB], XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
2548 XFS_FSS_TO_BB(mp, 1), DB_RING_IGN, NULL);
2549 if (!iocur_top->data) {
2550 print_warning("cannot read superblock for ag %u", agno);
2551 if (stop_on_read_error)
2552 goto pop_out;
2553 } else {
2554 /* Replace any filesystem label with "L's" */
2555 if (obfuscate) {
2556 struct xfs_sb *sb = iocur_top->data;
2557 memset(sb->sb_fname, 'L',
2558 min(strlen(sb->sb_fname), sizeof(sb->sb_fname)));
2559 iocur_top->need_crc = 1;
2560 }
2561 if (write_buf(iocur_top))
2562 goto pop_out;
2563 }
2564
2565 /* copy the AG free space btree root */
2566 push_cur();
2567 stack_count++;
2568 set_cur(&typtab[TYP_AGF], XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
2569 XFS_FSS_TO_BB(mp, 1), DB_RING_IGN, NULL);
2570 agf = iocur_top->data;
2571 if (iocur_top->data == NULL) {
2572 print_warning("cannot read agf block for ag %u", agno);
2573 if (stop_on_read_error)
2574 goto pop_out;
2575 } else {
2576 if (write_buf(iocur_top))
2577 goto pop_out;
2578 }
2579
2580 /* copy the AG inode btree root */
2581 push_cur();
2582 stack_count++;
2583 set_cur(&typtab[TYP_AGI], XFS_AG_DADDR(mp, agno, XFS_AGI_DADDR(mp)),
2584 XFS_FSS_TO_BB(mp, 1), DB_RING_IGN, NULL);
2585 agi = iocur_top->data;
2586 if (iocur_top->data == NULL) {
2587 print_warning("cannot read agi block for ag %u", agno);
2588 if (stop_on_read_error)
2589 goto pop_out;
2590 } else {
2591 if (write_buf(iocur_top))
2592 goto pop_out;
2593 }
2594
2595 /* copy the AG free list header */
2596 push_cur();
2597 stack_count++;
2598 set_cur(&typtab[TYP_AGFL], XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
2599 XFS_FSS_TO_BB(mp, 1), DB_RING_IGN, NULL);
2600 if (iocur_top->data == NULL) {
2601 print_warning("cannot read agfl block for ag %u", agno);
2602 if (stop_on_read_error)
2603 goto pop_out;
2604 } else {
2605 if (agf && zero_stale_data) {
2606 /* Zero out unused bits of agfl */
2607 int i;
2608 __be32 *agfl_bno;
2609
2610 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, iocur_top->bp);
2611 i = be32_to_cpu(agf->agf_fllast);
2612
2613 for (;;) {
2614 if (++i == XFS_AGFL_SIZE(mp))
2615 i = 0;
2616 if (i == be32_to_cpu(agf->agf_flfirst))
2617 break;
2618 agfl_bno[i] = cpu_to_be32(NULLAGBLOCK);
2619 }
2620 iocur_top->need_crc = 1;
2621 }
2622 if (write_buf(iocur_top))
2623 goto pop_out;
2624 }
2625
2626 /* copy AG free space btrees */
2627 if (agf) {
2628 if (show_progress)
2629 print_progress("Copying free space trees of AG %u",
2630 agno);
2631 if (!copy_free_bno_btree(agno, agf))
2632 goto pop_out;
2633 if (!copy_free_cnt_btree(agno, agf))
2634 goto pop_out;
2635 if (!copy_rmap_btree(agno, agf))
2636 goto pop_out;
2637 if (!copy_refcount_btree(agno, agf))
2638 goto pop_out;
2639 }
2640
2641 /* copy inode btrees and the inodes and their associated metadata */
2642 if (agi) {
2643 if (!copy_inodes(agno, agi))
2644 goto pop_out;
2645 }
2646 rval = 1;
2647 pop_out:
2648 while (stack_count--)
2649 pop_cur();
2650 return rval;
2651 }
2652
2653 static int
2654 copy_ino(
2655 xfs_ino_t ino,
2656 typnm_t itype)
2657 {
2658 xfs_agnumber_t agno;
2659 xfs_agblock_t agbno;
2660 xfs_agino_t agino;
2661 int offset;
2662 int rval = 0;
2663
2664 if (ino == 0 || ino == NULLFSINO)
2665 return 1;
2666
2667 agno = XFS_INO_TO_AGNO(mp, ino);
2668 agino = XFS_INO_TO_AGINO(mp, ino);
2669 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
2670 offset = XFS_AGINO_TO_OFFSET(mp, agino);
2671
2672 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
2673 offset >= mp->m_sb.sb_inopblock) {
2674 if (show_warnings)
2675 print_warning("invalid %s inode number (%lld)",
2676 typtab[itype].name, (long long)ino);
2677 return 1;
2678 }
2679
2680 push_cur();
2681 set_cur(&typtab[TYP_INODE], XFS_AGB_TO_DADDR(mp, agno, agbno),
2682 blkbb, DB_RING_IGN, NULL);
2683 if (iocur_top->data == NULL) {
2684 print_warning("cannot read %s inode %lld",
2685 typtab[itype].name, (long long)ino);
2686 rval = !stop_on_read_error;
2687 goto pop_out;
2688 }
2689 off_cur(offset << mp->m_sb.sb_inodelog, mp->m_sb.sb_inodesize);
2690
2691 cur_ino = ino;
2692 rval = process_inode_data(iocur_top->data, itype);
2693 pop_out:
2694 pop_cur();
2695 return rval;
2696 }
2697
2698
2699 static int
2700 copy_sb_inodes(void)
2701 {
2702 if (!copy_ino(mp->m_sb.sb_rbmino, TYP_RTBITMAP))
2703 return 0;
2704
2705 if (!copy_ino(mp->m_sb.sb_rsumino, TYP_RTSUMMARY))
2706 return 0;
2707
2708 if (!copy_ino(mp->m_sb.sb_uquotino, TYP_DQBLK))
2709 return 0;
2710
2711 if (!copy_ino(mp->m_sb.sb_gquotino, TYP_DQBLK))
2712 return 0;
2713
2714 return copy_ino(mp->m_sb.sb_pquotino, TYP_DQBLK);
2715 }
2716
2717 static int
2718 copy_log(void)
2719 {
2720 struct xlog log;
2721 int dirty;
2722 xfs_daddr_t logstart;
2723 int logblocks;
2724 int logversion;
2725 int cycle = XLOG_INIT_CYCLE;
2726
2727 if (show_progress)
2728 print_progress("Copying log");
2729
2730 push_cur();
2731 set_cur(&typtab[TYP_LOG], XFS_FSB_TO_DADDR(mp, mp->m_sb.sb_logstart),
2732 mp->m_sb.sb_logblocks * blkbb, DB_RING_IGN, NULL);
2733 if (iocur_top->data == NULL) {
2734 pop_cur();
2735 print_warning("cannot read log");
2736 return !stop_on_read_error;
2737 }
2738
2739 /* If not obfuscating or zeroing, just copy the log as it is */
2740 if (!obfuscate && !zero_stale_data)
2741 goto done;
2742
2743 dirty = xlog_is_dirty(mp, &log, &x, 0);
2744
2745 switch (dirty) {
2746 case 0:
2747 /* clear out a clean log */
2748 if (show_progress)
2749 print_progress("Zeroing clean log");
2750
2751 logstart = XFS_FSB_TO_DADDR(mp, mp->m_sb.sb_logstart);
2752 logblocks = XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
2753 logversion = xfs_sb_version_haslogv2(&mp->m_sb) ? 2 : 1;
2754 if (xfs_sb_version_hascrc(&mp->m_sb))
2755 cycle = log.l_curr_cycle + 1;
2756
2757 libxfs_log_clear(NULL, iocur_top->data, logstart, logblocks,
2758 &mp->m_sb.sb_uuid, logversion,
2759 mp->m_sb.sb_logsunit, XLOG_FMT, cycle, true);
2760 break;
2761 case 1:
2762 /* keep the dirty log */
2763 if (obfuscate)
2764 print_warning(
2765 _("Warning: log recovery of an obfuscated metadata image can leak "
2766 "unobfuscated metadata and/or cause image corruption. If possible, "
2767 "please mount the filesystem to clean the log, or disable obfuscation."));
2768 break;
2769 case -1:
2770 /* log detection error */
2771 if (obfuscate)
2772 print_warning(
2773 _("Could not discern log; image will contain unobfuscated metadata in log."));
2774 break;
2775 }
2776
2777 done:
2778 return !write_buf(iocur_top);
2779 }
2780
2781 static int
2782 metadump_f(
2783 int argc,
2784 char **argv)
2785 {
2786 xfs_agnumber_t agno;
2787 int c;
2788 int start_iocur_sp;
2789 int outfd = -1;
2790 int ret;
2791 char *p;
2792
2793 exitcode = 1;
2794 show_progress = 0;
2795 show_warnings = 0;
2796 stop_on_read_error = 0;
2797
2798 if (mp->m_sb.sb_magicnum != XFS_SB_MAGIC) {
2799 print_warning("bad superblock magic number %x, giving up",
2800 mp->m_sb.sb_magicnum);
2801 return 0;
2802 }
2803
2804 /*
2805 * on load, we sanity-checked agcount and possibly set to 1
2806 * if it was corrupted and large.
2807 */
2808 if (mp->m_sb.sb_agcount == 1 &&
2809 XFS_MAX_DBLOCKS(&mp->m_sb) < mp->m_sb.sb_dblocks) {
2810 print_warning("truncated agcount, giving up");
2811 return 0;
2812 }
2813
2814 while ((c = getopt(argc, argv, "aegm:ow")) != EOF) {
2815 switch (c) {
2816 case 'a':
2817 zero_stale_data = 0;
2818 break;
2819 case 'e':
2820 stop_on_read_error = 1;
2821 break;
2822 case 'g':
2823 show_progress = 1;
2824 break;
2825 case 'm':
2826 max_extent_size = (int)strtol(optarg, &p, 0);
2827 if (*p != '\0' || max_extent_size <= 0) {
2828 print_warning("bad max extent size %s",
2829 optarg);
2830 return 0;
2831 }
2832 break;
2833 case 'o':
2834 obfuscate = 0;
2835 break;
2836 case 'w':
2837 show_warnings = 1;
2838 break;
2839 default:
2840 print_warning("bad option for metadump command");
2841 return 0;
2842 }
2843 }
2844
2845 if (optind != argc - 1) {
2846 print_warning("too few options for metadump (no filename given)");
2847 return 0;
2848 }
2849
2850 metablock = (xfs_metablock_t *)calloc(BBSIZE + 1, BBSIZE);
2851 if (metablock == NULL) {
2852 print_warning("memory allocation failure");
2853 return 0;
2854 }
2855 metablock->mb_blocklog = BBSHIFT;
2856 metablock->mb_magic = cpu_to_be32(XFS_MD_MAGIC);
2857
2858 /* Set flags about state of metadump */
2859 metablock->mb_info = XFS_METADUMP_INFO_FLAGS;
2860 if (obfuscate)
2861 metablock->mb_info |= XFS_METADUMP_OBFUSCATED;
2862 if (!zero_stale_data)
2863 metablock->mb_info |= XFS_METADUMP_FULLBLOCKS;
2864
2865 /* If we'll copy the log, see if the log is dirty */
2866 if (mp->m_sb.sb_logstart) {
2867 push_cur();
2868 set_cur(&typtab[TYP_LOG],
2869 XFS_FSB_TO_DADDR(mp, mp->m_sb.sb_logstart),
2870 mp->m_sb.sb_logblocks * blkbb, DB_RING_IGN, NULL);
2871 if (iocur_top->data) { /* best effort */
2872 struct xlog log;
2873
2874 if (xlog_is_dirty(mp, &log, &x, 0))
2875 metablock->mb_info |= XFS_METADUMP_DIRTYLOG;
2876 }
2877 pop_cur();
2878 }
2879
2880 block_index = (__be64 *)((char *)metablock + sizeof(xfs_metablock_t));
2881 block_buffer = (char *)metablock + BBSIZE;
2882 num_indices = (BBSIZE - sizeof(xfs_metablock_t)) / sizeof(__be64);
2883
2884 /*
2885 * A metadump block can hold at most num_indices of BBSIZE sectors;
2886 * do not try to dump a filesystem with a sector size which does not
2887 * fit within num_indices (i.e. within a single metablock).
2888 */
2889 if (mp->m_sb.sb_sectsize > num_indices * BBSIZE) {
2890 print_warning("Cannot dump filesystem with sector size %u",
2891 mp->m_sb.sb_sectsize);
2892 free(metablock);
2893 return 0;
2894 }
2895
2896 cur_index = 0;
2897 start_iocur_sp = iocur_sp;
2898
2899 if (strcmp(argv[optind], "-") == 0) {
2900 if (isatty(fileno(stdout))) {
2901 print_warning("cannot write to a terminal");
2902 free(metablock);
2903 return 0;
2904 }
2905 /*
2906 * Redirect stdout to stderr for the duration of the
2907 * metadump operation so that dbprintf and other messages
2908 * are sent to the console instead of polluting the
2909 * metadump stream.
2910 *
2911 * We get to do this the hard way because musl doesn't
2912 * allow reassignment of stdout.
2913 */
2914 fflush(stdout);
2915 outfd = dup(STDOUT_FILENO);
2916 if (outfd < 0) {
2917 perror("opening dump stream");
2918 goto out;
2919 }
2920 ret = dup2(STDERR_FILENO, STDOUT_FILENO);
2921 if (ret < 0) {
2922 perror("redirecting stdout");
2923 close(outfd);
2924 goto out;
2925 }
2926 outf = fdopen(outfd, "a");
2927 if (outf == NULL) {
2928 fprintf(stderr, "cannot create dump stream\n");
2929 dup2(outfd, STDOUT_FILENO);
2930 close(outfd);
2931 goto out;
2932 }
2933 stdout_metadump = true;
2934 } else {
2935 outf = fopen(argv[optind], "wb");
2936 if (outf == NULL) {
2937 print_warning("cannot create dump file");
2938 goto out;
2939 }
2940 }
2941
2942 exitcode = 0;
2943
2944 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
2945 if (!scan_ag(agno)) {
2946 exitcode = 1;
2947 break;
2948 }
2949 }
2950
2951 /* copy realtime and quota inode contents */
2952 if (!exitcode)
2953 exitcode = !copy_sb_inodes();
2954
2955 /* copy log if it's internal */
2956 if ((mp->m_sb.sb_logstart != 0) && !exitcode)
2957 exitcode = !copy_log();
2958
2959 /* write the remaining index */
2960 if (!exitcode)
2961 exitcode = write_index() < 0;
2962
2963 if (progress_since_warning)
2964 fputc('\n', stdout_metadump ? stderr : stdout);
2965
2966 if (stdout_metadump) {
2967 fflush(outf);
2968 fflush(stdout);
2969 ret = dup2(outfd, STDOUT_FILENO);
2970 if (ret < 0)
2971 perror("un-redirecting stdout");
2972 stdout_metadump = false;
2973 }
2974 fclose(outf);
2975
2976 /* cleanup iocur stack */
2977 while (iocur_sp > start_iocur_sp)
2978 pop_cur();
2979 out:
2980 free(metablock);
2981
2982 return 0;
2983 }