2 * Copyright (c) 2007, 2011 SGI
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.
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.
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
29 #include "xfs_metadump.h"
35 #define DEFAULT_MAX_EXT_SIZE 1000
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.
43 #define DUP_MAX 5 /* Max duplicates before we give up */
45 /* copy all metadata structures to/from a file */
47 static int metadump_f(int argc
, char **argv
);
48 static void metadump_help(void);
51 * metadump commands issue info/wornings/errors to standard error as
52 * metadump supports stdout as a destination.
54 * All static functions return zero on failure, while the public functions
55 * return zero on success.
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
};
63 static FILE *outf
; /* metadump file */
65 static xfs_metablock_t
*metablock
; /* header + index + buffers */
66 static __be64
*block_index
;
67 static char *block_buffer
;
69 static int num_indicies
;
72 static xfs_ino_t cur_ino
;
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;
85 add_command(&metadump_cmd
);
93 " The 'metadump' command dumps the known metadata to a compact file suitable\n"
94 " for compressing and sending to an XFS maintainer for corruption analysis \n"
95 " or xfs_repair failures.\n\n"
97 " -a -- Copy full metadata blocks without zeroing unused space\n"
98 " -e -- Ignore read errors and keep going\n"
99 " -g -- Display dump progress\n"
100 " -m -- Specify max extent size in blocks to copy (default = %d blocks)\n"
101 " -o -- Don't obfuscate names and extended attributes\n"
102 " -w -- Show warnings of bad metadata information\n"
103 "\n"), DEFAULT_MAX_EXT_SIZE
);
107 print_warning(const char *fmt
, ...)
116 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
118 buf
[sizeof(buf
)-1] = '\0';
120 fprintf(stderr
, "%s%s: %s\n", progress_since_warning
? "\n" : "",
122 progress_since_warning
= 0;
126 print_progress(const char *fmt
, ...)
136 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
138 buf
[sizeof(buf
)-1] = '\0';
140 f
= (outf
== stdout
) ? stderr
: stdout
;
141 fprintf(f
, "\r%-59s", buf
);
143 progress_since_warning
= 1;
147 * A complete dump file will have a "zero" entry in the last index block,
148 * even if the dump is exactly aligned, the last index will be full of
149 * zeros. If the last index entry is non-zero, the dump is incomplete.
150 * Correspondingly, the last chunk will have a count < num_indicies.
152 * Return 0 for success, -1 for failure.
159 * write index block and following data blocks (streaming)
161 metablock
->mb_count
= cpu_to_be16(cur_index
);
162 if (fwrite(metablock
, (cur_index
+ 1) << BBSHIFT
, 1, outf
) != 1) {
163 print_warning("error writing to file: %s", strerror(errno
));
167 memset(block_index
, 0, num_indicies
* sizeof(__be64
));
173 * Return 0 for success, -errno for failure.
184 for (i
= 0; i
< len
; i
++, off
++, data
+= BBSIZE
) {
185 block_index
[cur_index
] = cpu_to_be64(off
);
186 memcpy(&block_buffer
[cur_index
<< BBSHIFT
], data
, BBSIZE
);
187 if (++cur_index
== num_indicies
) {
197 * we want to preserve the state of the metadata in the dump - whether it is
198 * intact or corrupt, so even if the buffer has a verifier attached to it we
199 * don't want to run it prior to writing the buffer to the metadump image.
201 * The only reason for running the verifier is to recalculate the CRCs on a
202 * buffer that has been obfuscated. i.e. a buffer than metadump modified itself.
203 * In this case, we only run the verifier if the buffer was not corrupt to begin
204 * with so that we don't accidentally correct buffers with CRC or errors in them
205 * when we are obfuscating them.
211 struct xfs_buf
*bp
= buf
->bp
;
216 * Run the write verifier to recalculate the buffer CRCs and check
217 * metadump didn't introduce a new corruption. Warn if the verifier
218 * failed, but still continue to dump it into the output file.
220 if (buf
->need_crc
&& bp
&& bp
->b_ops
&& !bp
->b_error
) {
221 bp
->b_ops
->verify_write(bp
);
224 "obfuscation corrupted block at bno 0x%llx/0x%x",
225 (long long)bp
->b_bn
, bp
->b_bcount
);
229 /* handle discontiguous buffers */
231 ret
= write_buf_segment(buf
->data
, buf
->bb
, buf
->blen
);
236 for (i
= 0; i
< buf
->bbmap
->nmaps
; i
++) {
237 ret
= write_buf_segment(buf
->data
+ BBTOB(len
),
238 buf
->bbmap
->b
[i
].bm_bn
,
239 buf
->bbmap
->b
[i
].bm_len
);
242 len
+= buf
->bbmap
->b
[i
].bm_len
;
245 return seenint() ? -EINTR
: 0;
256 int (*func
)(struct xfs_btree_block
*block
,
266 set_cur(&typtab
[btype
], XFS_AGB_TO_DADDR(mp
, agno
, agbno
), blkbb
,
268 if (iocur_top
->data
== NULL
) {
269 print_warning("cannot read %s block %u/%u", typtab
[btype
].name
,
271 rval
= !stop_on_read_error
;
274 if (write_buf(iocur_top
))
277 if (!(*func
)(iocur_top
->data
, agno
, agbno
, level
- 1, btype
, arg
))
285 /* free space tree copy routines */
292 if (agno
< (mp
->m_sb
.sb_agcount
- 1) && agbno
> 0 &&
293 agbno
<= mp
->m_sb
.sb_agblocks
)
295 if (agno
== (mp
->m_sb
.sb_agcount
- 1) && agbno
> 0 &&
296 agbno
<= (mp
->m_sb
.sb_dblocks
-
297 (xfs_drfsbno_t
)(mp
->m_sb
.sb_agcount
- 1) *
298 mp
->m_sb
.sb_agblocks
))
307 struct xfs_btree_block
*block
,
321 numrecs
= be16_to_cpu(block
->bb_numrecs
);
322 if (numrecs
> mp
->m_alloc_mxr
[1]) {
324 print_warning("invalid numrecs (%u) in %s block %u/%u",
325 numrecs
, typtab
[btype
].name
, agno
, agbno
);
329 pp
= XFS_ALLOC_PTR_ADDR(mp
, block
, 1, mp
->m_alloc_mxr
[1]);
330 for (i
= 0; i
< numrecs
; i
++) {
331 if (!valid_bno(agno
, be32_to_cpu(pp
[i
]))) {
333 print_warning("invalid block number (%u/%u) "
335 agno
, be32_to_cpu(pp
[i
]),
336 typtab
[btype
].name
, agno
, agbno
);
339 if (!scan_btree(agno
, be32_to_cpu(pp
[i
]), level
, btype
, arg
,
354 root
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_BNO
]);
355 levels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]);
357 /* validate root and levels before processing the tree */
358 if (root
== 0 || root
> mp
->m_sb
.sb_agblocks
) {
360 print_warning("invalid block number (%u) in bnobt "
361 "root in agf %u", root
, agno
);
364 if (levels
>= XFS_BTREE_MAXLEVELS
) {
366 print_warning("invalid level (%u) in bnobt root "
367 "in agf %u", levels
, agno
);
371 return scan_btree(agno
, root
, levels
, TYP_BNOBT
, agf
, scanfunc_freesp
);
382 root
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_CNT
]);
383 levels
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]);
385 /* validate root and levels before processing the tree */
386 if (root
== 0 || root
> mp
->m_sb
.sb_agblocks
) {
388 print_warning("invalid block number (%u) in cntbt "
389 "root in agf %u", root
, agno
);
392 if (levels
>= XFS_BTREE_MAXLEVELS
) {
394 print_warning("invalid level (%u) in cntbt root "
395 "in agf %u", levels
, agno
);
399 return scan_btree(agno
, root
, levels
, TYP_CNTBT
, agf
, scanfunc_freesp
);
402 /* filename and extended attribute obfuscation routines */
405 struct name_ent
*next
;
411 #define NAME_TABLE_SIZE 4096
413 static struct name_ent
*nametable
[NAME_TABLE_SIZE
];
416 nametable_clear(void)
419 struct name_ent
*ent
;
421 for (i
= 0; i
< NAME_TABLE_SIZE
; i
++) {
422 while ((ent
= nametable
[i
])) {
423 nametable
[i
] = ent
->next
;
430 * See if the given name is already in the name table. If so,
431 * return a pointer to its entry, otherwise return a null pointer.
433 static struct name_ent
*
434 nametable_find(xfs_dahash_t hash
, int namelen
, uchar_t
*name
)
436 struct name_ent
*ent
;
438 for (ent
= nametable
[hash
% NAME_TABLE_SIZE
]; ent
; ent
= ent
->next
) {
439 if (ent
->hash
== hash
&& ent
->namelen
== namelen
&&
440 !memcmp(ent
->name
, name
, namelen
))
447 * Add the given name to the name table. Returns a pointer to the
448 * name's new entry, or a null pointer if an error occurs.
450 static struct name_ent
*
451 nametable_add(xfs_dahash_t hash
, int namelen
, uchar_t
*name
)
453 struct name_ent
*ent
;
455 ent
= malloc(sizeof *ent
+ namelen
);
459 ent
->namelen
= namelen
;
460 memcpy(ent
->name
, name
, namelen
);
462 ent
->next
= nametable
[hash
% NAME_TABLE_SIZE
];
464 nametable
[hash
% NAME_TABLE_SIZE
] = ent
;
469 #define is_invalid_char(c) ((c) == '/' || (c) == '\0')
470 #define rol32(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
472 static inline uchar_t
473 random_filename_char(void)
475 static uchar_t filename_alphabet
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
476 "abcdefghijklmnopqrstuvwxyz"
479 return filename_alphabet
[random() % (sizeof filename_alphabet
- 1)];
482 #define ORPHANAGE "lost+found"
483 #define ORPHANAGE_LEN (sizeof (ORPHANAGE) - 1)
487 struct xfs_mount
*mp
,
492 return dir_ino
== mp
->m_sb
.sb_rootino
&&
493 name_len
== ORPHANAGE_LEN
&&
494 !memcmp(name
, ORPHANAGE
, ORPHANAGE_LEN
);
498 * Determine whether a name is one we shouldn't obfuscate because
499 * it's an orphan (or the "lost+found" directory itself). Note
500 * "cur_ino" is the inode for the directory currently being
503 * Returns 1 if the name should NOT be obfuscated or 0 otherwise.
511 static xfs_ino_t orphanage_ino
= 0;
512 char s
[24]; /* 21 is enough (64 bits in decimal) */
515 /* Record the "lost+found" inode if we haven't done so already */
518 if (!orphanage_ino
&& is_orphanage_dir(mp
, cur_ino
, namelen
, name
))
521 /* We don't obfuscate the "lost+found" directory itself */
523 if (ino
== orphanage_ino
)
526 /* Most files aren't in "lost+found" at all */
528 if (cur_ino
!= orphanage_ino
)
532 * Within "lost+found", we don't obfuscate any file whose
533 * name is the same as its inode number. Any others are
534 * stray files and can be obfuscated.
536 slen
= snprintf(s
, sizeof (s
), "%llu", (unsigned long long) ino
);
538 return slen
== namelen
&& !memcmp(name
, s
, namelen
);
542 * Given a name and its hash value, massage the name in such a way
543 * that the result is another name of equal length which shares the
552 uchar_t
*newp
= name
;
554 xfs_dahash_t new_hash
= 0;
560 * Our obfuscation algorithm requires at least 5-character
561 * names, so don't bother if the name is too short. We
562 * work backward from a hash value to determine the last
563 * five bytes in a name required to produce a new name
564 * with the same hash.
570 * The beginning of the obfuscated name can be pretty much
571 * anything, so fill it in with random characters.
572 * Accumulate its new hash value as we go.
574 for (i
= 0; i
< name_len
- 5; i
++) {
575 *newp
= random_filename_char();
576 new_hash
= *newp
^ rol32(new_hash
, 7);
581 * Compute which five bytes need to be used at the end of
582 * the name so the hash of the obfuscated name is the same
583 * as the hash of the original. If any result in an invalid
584 * character, flip a bit and arrange for a corresponding bit
585 * in a neighboring byte to be flipped as well. For the
586 * last byte, the "neighbor" to change is the first byte
587 * we're computing here.
589 new_hash
= rol32(new_hash
, 3) ^ hash
;
593 for (shift
= 28; shift
>= 0; shift
-= 7) {
594 *newp
= (new_hash
>> shift
& 0x7f) ^ high_bit
;
595 if (is_invalid_char(*newp
)) {
600 ASSERT(!is_invalid_char(*newp
));
605 * If we flipped a bit on the last byte, we need to fix up
606 * the matching bit in the first byte. The result will
607 * be a valid character, because we know that first byte
608 * has 0's in its upper four bits (it was produced by a
609 * 28-bit right-shift of a 32-bit unsigned value).
613 ASSERT(!is_invalid_char(*first
));
615 ASSERT(libxfs_da_hashname(name
, name_len
) == hash
);
619 * Flip a bit in each of two bytes at the end of the given name.
620 * This is used in generating a series of alternate names to be used
621 * in the event a duplicate is found.
623 * The bits flipped are selected such that they both affect the same
624 * bit in the name's computed hash value, so flipping them both will
627 * The following diagram aims to show the portion of a computed
628 * hash that a given byte of a name affects.
630 * 31 28 24 21 14 8 7 3 0
631 * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
632 * hash: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
633 * +-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-|-+-+-+-+-+-+-+-+
634 * last-4 ->| |<-- last-2 --->| |<--- last ---->|
635 * |<-- last-3 --->| |<-- last-1 --->| |<- last-4
636 * |<-- last-7 --->| |<-- last-5 --->|
637 * |<-- last-8 --->| |<-- last-6 --->|
640 * The last byte of the name directly affects the low-order byte of
641 * the hash. The next-to-last affects bits 7-14, the next one back
642 * affects bits 14-21, and so on. The effect wraps around when it
643 * goes beyond the top of the hash (as happens for byte last-4).
645 * Bits that are flipped together "overlap" on the hash value. As
646 * an example of overlap, the last two bytes both affect bit 7 in
647 * the hash. That pair of bytes (and their overlapping bits) can be
648 * used for this "flip bit" operation (it's the first pair tried,
651 * A table defines overlapping pairs--the bytes involved and bits
652 * within them--that can be used this way. The byte offset is
653 * relative to a starting point within the name, which will be set
654 * to affect the bytes at the end of the name. The function is
655 * called with a "bitseq" value which indicates which bit flip is
656 * desired, and this translates directly into selecting which entry
657 * in the bit_to_flip[] table to apply.
659 * The function returns 1 if the operation was successful. It
660 * returns 0 if the result produced a character that's not valid in
661 * a name (either '/' or a '\0'). Finally, it returns -1 if the bit
662 * sequence number is beyond what is supported for a name of this
667 * (Also see the discussion above find_alternate(), below.)
669 * In order to make this function work for any length name, the
670 * table is ordered by increasing byte offset, so that the earliest
671 * entries can apply to the shortest strings. This way all names
672 * are done consistently.
674 * When bit flips occur, they can convert printable characters
675 * into non-printable ones. In an effort to reduce the impact of
676 * this, the first bit flips are chosen to affect bytes the end of
677 * the name (and furthermore, toward the low bits of a byte). Those
678 * bytes are often non-printable anyway because of the way they are
679 * initially selected by obfuscate_name()). This is accomplished,
680 * using later table entries first.
682 * Each row in the table doubles the number of alternates that
683 * can be generated. A two-byte name is limited to using only
684 * the first row, so it's possible to generate two alternates
685 * (the original name, plus the alternate produced by flipping
686 * the one pair of bits). In a 5-byte name, the effect of the
687 * first byte overlaps the last by 4 its, and there are 8 bits
688 * to flip, allowing for 256 possible alternates.
690 * Short names (less than 5 bytes) are never even obfuscated, so for
691 * such names the relatively small number of alternates should never
692 * really be a problem.
694 * Long names (more than 6 bytes, say) are not likely to exhaust
695 * the number of available alternates. In fact, the table could
696 * probably have stopped at 8 entries, on the assumption that 256
697 * alternates should be enough for most any situation. The entries
698 * beyond those are present mostly for demonstration of how it could
699 * be populated with more entries, should it ever be necessary to do
713 int byte
; /* Offset from start within name */
714 uchar_t bit
; /* Bit within that byte */
715 } bit_to_flip
[][2] = { /* Sorted by second entry's byte */
716 { { 0, 0 }, { 1, 7 } }, /* Each row defines a pair */
717 { { 1, 0 }, { 2, 7 } }, /* of bytes and a bit within */
718 { { 2, 0 }, { 3, 7 } }, /* each byte. Each bit in */
719 { { 0, 4 }, { 4, 0 } }, /* a pair affects the same */
720 { { 0, 5 }, { 4, 1 } }, /* bit in the hash, so flipping */
721 { { 0, 6 }, { 4, 2 } }, /* both will change the name */
722 { { 0, 7 }, { 4, 3 } }, /* while preserving the hash. */
723 { { 3, 0 }, { 4, 7 } },
724 { { 0, 0 }, { 5, 3 } }, /* The first entry's byte offset */
725 { { 0, 1 }, { 5, 4 } }, /* must be less than the second. */
726 { { 0, 2 }, { 5, 5 } },
727 { { 0, 3 }, { 5, 6 } }, /* The table can be extended to */
728 { { 0, 4 }, { 5, 7 } }, /* an arbitrary number of entries */
729 { { 4, 0 }, { 5, 7 } }, /* but there's not much point. */
733 /* Find the first entry *not* usable for name of this length */
735 for (index
= 0; index
< ARRAY_SIZE(bit_to_flip
); index
++)
736 if (bit_to_flip
[index
][1].byte
>= name_len
)
740 * Back up to the last usable entry. If that number is
741 * smaller than the bit sequence number, inform the caller
742 * that nothing this large (or larger) will work.
744 if (bitseq
> --index
)
748 * We will be switching bits at the end of name, with a
749 * preference for affecting the last bytes first. Compute
750 * where in the name we'll start applying the changes.
752 offset
= name_len
- (bit_to_flip
[index
][1].byte
+ 1);
753 index
-= bitseq
; /* Use later table entries first */
755 p0
= name
+ offset
+ bit_to_flip
[index
][0].byte
;
756 p1
= name
+ offset
+ bit_to_flip
[index
][1].byte
;
757 m0
= 1 << bit_to_flip
[index
][0].bit
;
758 m1
= 1 << bit_to_flip
[index
][1].bit
;
760 /* Only change the bytes if it produces valid characters */
762 if (is_invalid_char(*p0
^ m0
) || is_invalid_char(*p1
^ m1
))
772 * This function generates a well-defined sequence of "alternate"
773 * names for a given name. An alternate is a name having the same
774 * length and same hash value as the original name. This is needed
775 * because the algorithm produces only one obfuscated name to use
776 * for a given original name, and it's possible that result matches
777 * a name already seen. This function checks for this, and if it
778 * occurs, finds another suitable obfuscated name to use.
780 * Each bit in the binary representation of the sequence number is
781 * used to select one possible "bit flip" operation to perform on
782 * the name. So for example:
783 * seq = 0: selects no bits to flip
784 * seq = 1: selects the 0th bit to flip
785 * seq = 2: selects the 1st bit to flip
786 * seq = 3: selects the 0th and 1st bit to flip
789 * The flip_bit() function takes care of the details of the bit
790 * flipping within the name. Note that the "1st bit" in this
791 * context is a bit sequence number; i.e. it doesn't necessarily
792 * mean bit 0x02 will be changed.
794 * If a valid name (one that contains no '/' or '\0' characters) is
795 * produced by this process for the given sequence number, this
796 * function returns 1. If the result is not valid, it returns 0.
797 * Returns -1 if the sequence number is beyond the the maximum for
798 * names of the given length.
803 * The number of alternates available for a given name is dependent
804 * on its length. A "bit flip" involves inverting two bits in
805 * a name--the two bits being selected such that their values
806 * affect the name's hash value in the same way. Alternates are
807 * thus generated by inverting the value of pairs of such
808 * "overlapping" bits in the original name. Each byte after the
809 * first in a name adds at least one bit of overlap to work with.
810 * (See comments above flip_bit() for more discussion on this.)
812 * So the number of alternates is dependent on the number of such
813 * overlapping bits in a name. If there are N bit overlaps, there
814 * 2^N alternates for that hash value.
816 * Here are the number of overlapping bits available for generating
817 * alternates for names of specific lengths:
818 * 1 0 (must have 2 bytes to have any overlap)
819 * 2 1 One bit overlaps--so 2 possible alternates
820 * 3 2 Two bits overlap--so 4 possible alternates
821 * 4 4 Three bits overlap, so 2^3 alternates
822 * 5 8 8 bits overlap (due to wrapping), 256 alternates
823 * 6 18 2^18 alternates
824 * 7 28 2^28 alternates
826 * It's clear that the number of alternates grows very quickly with
827 * the length of the name. But note that the set of alternates
828 * includes invalid names. And for certain (contrived) names, the
829 * number of valid names is a fairly small fraction of the total
830 * number of alternates.
832 * The main driver for this infrastructure for coming up with
833 * alternate names is really related to names 5 (or possibly 6)
834 * bytes in length. 5-byte obfuscated names contain no randomly-
835 * generated bytes in them, and the chance of an obfuscated name
836 * matching an already-seen name is too high to just ignore. This
837 * methodical selection of alternates ensures we don't produce
838 * duplicate names unless we have exhausted our options.
850 return 1; /* alternate 0 is the original name */
851 if (name_len
< 2) /* Must have 2 bytes to flip */
854 for (bitseq
= 0; bits
; bitseq
++) {
855 uint32_t mask
= 1 << bitseq
;
861 fb
= flip_bit(name_len
, name
, bitseq
);
871 * Look up the given name in the name table. If it is already
872 * present, iterate through a well-defined sequence of alternate
873 * names and attempt to use an alternate name instead.
875 * Returns 1 if the (possibly modified) name is not present in the
876 * name table. Returns 0 if the name and all possible alternates
877 * are already in the table.
880 handle_duplicate_name(xfs_dahash_t hash
, size_t name_len
, uchar_t
*name
)
882 uchar_t new_name
[name_len
+ 1];
885 if (!nametable_find(hash
, name_len
, name
))
886 return 1; /* No duplicate */
888 /* Name is already in use. Need to find an alternate. */
893 /* Only change incoming name if we find an alternate */
895 memcpy(new_name
, name
, name_len
);
896 found
= find_alternate(name_len
, new_name
, seq
++);
898 return 0; /* No more to check */
900 } while (nametable_find(hash
, name_len
, new_name
));
903 * The alternate wasn't in the table already. Pass it back
906 memcpy(name
, new_name
, name_len
);
912 generate_obfuscated_name(
920 * We don't obfuscate "lost+found" or any orphan files
921 * therein. When the name table is used for extended
922 * attributes, the inode number provided is 0, in which
923 * case we don't need to make this check.
925 if (ino
&& in_lost_found(ino
, namelen
, name
))
929 * If the name starts with a slash, just skip over it. It
930 * isn't included in the hash and we don't record it in the
931 * name table. Note that the namelen value passed in does
932 * not count the leading slash (if one is present).
937 /* Obfuscate the name (if possible) */
939 hash
= libxfs_da_hashname(name
, namelen
);
940 obfuscate_name(hash
, namelen
, name
);
943 * Make sure the name is not something already seen. If we
944 * fail to find a suitable alternate, we're dealing with a
945 * very pathological situation, and we may end up creating
946 * a duplicate name in the metadump, so issue a warning.
948 if (!handle_duplicate_name(hash
, namelen
, name
)) {
949 print_warning("duplicate name for inode %llu "
950 "in dir inode %llu\n",
951 (unsigned long long) ino
,
952 (unsigned long long) cur_ino
);
956 /* Create an entry for the new name in the name table. */
958 if (!nametable_add(hash
, namelen
, name
))
959 print_warning("unable to record name for inode %llu "
960 "in dir inode %llu\n",
961 (unsigned long long) ino
,
962 (unsigned long long) cur_ino
);
969 struct xfs_dir2_sf_hdr
*sfp
;
970 xfs_dir2_sf_entry_t
*sfep
;
971 __uint64_t ino_dir_size
;
974 sfp
= (struct xfs_dir2_sf_hdr
*)XFS_DFORK_DPTR(dip
);
975 ino_dir_size
= be64_to_cpu(dip
->di_size
);
976 if (ino_dir_size
> XFS_DFORK_DSIZE(dip
, mp
)) {
977 ino_dir_size
= XFS_DFORK_DSIZE(dip
, mp
);
979 print_warning("invalid size in dir inode %llu",
983 sfep
= xfs_dir2_sf_firstentry(sfp
);
984 for (i
= 0; (i
< sfp
->count
) &&
985 ((char *)sfep
- (char *)sfp
< ino_dir_size
); i
++) {
988 * first check for bad name lengths. If they are bad, we
989 * have limitations to how much can be obfuscated.
991 int namelen
= sfep
->namelen
;
995 print_warning("zero length entry in dir inode "
996 "%llu", (long long)cur_ino
);
997 if (i
!= sfp
->count
- 1)
999 namelen
= ino_dir_size
- ((char *)&sfep
->name
[0] -
1001 } else if ((char *)sfep
- (char *)sfp
+
1002 xfs_dir3_sf_entsize(mp
, sfp
, sfep
->namelen
) >
1005 print_warning("entry length in dir inode %llu "
1006 "overflows space", (long long)cur_ino
);
1007 if (i
!= sfp
->count
- 1)
1009 namelen
= ino_dir_size
- ((char *)&sfep
->name
[0] -
1014 generate_obfuscated_name(
1015 xfs_dir3_sfe_get_ino(mp
, sfp
, sfep
),
1016 namelen
, &sfep
->name
[0]);
1018 sfep
= (xfs_dir2_sf_entry_t
*)((char *)sfep
+
1019 xfs_dir3_sf_entsize(mp
, sfp
, namelen
));
1022 /* zero stale data in rest of space in data fork, if any */
1023 if (zero_stale_data
&& (ino_dir_size
< XFS_DFORK_DSIZE(dip
, mp
)))
1024 memset(sfep
, 0, XFS_DFORK_DSIZE(dip
, mp
) - ino_dir_size
);
1028 * The pathname may not be null terminated. It may be terminated by the end of
1029 * a buffer or inode literal area, and the start of the next region contains
1030 * unknown data. Therefore, when we get to the last component of the symlink, we
1031 * cannot assume that strlen() will give us the right result. Hence we need to
1032 * track the remaining pathname length and use that instead.
1035 obfuscate_path_components(
1039 uchar_t
*comp
= (uchar_t
*)buf
;
1040 uchar_t
*end
= comp
+ len
;
1043 while (comp
< end
) {
1047 /* find slash at end of this component */
1048 slash
= strchr((char *)comp
, '/');
1050 /* last (or single) component */
1051 namelen
= strnlen((char *)comp
, len
);
1052 hash
= libxfs_da_hashname(comp
, namelen
);
1053 obfuscate_name(hash
, namelen
, comp
);
1056 namelen
= slash
- (char *)comp
;
1057 /* handle leading or consecutive slashes */
1063 hash
= libxfs_da_hashname(comp
, namelen
);
1064 obfuscate_name(hash
, namelen
, comp
);
1065 comp
+= namelen
+ 1;
1077 len
= be64_to_cpu(dip
->di_size
);
1078 if (len
> XFS_DFORK_DSIZE(dip
, mp
)) {
1080 print_warning("invalid size (%d) in symlink inode %llu",
1081 len
, (long long)cur_ino
);
1082 len
= XFS_DFORK_DSIZE(dip
, mp
);
1085 buf
= (char *)XFS_DFORK_DPTR(dip
);
1087 obfuscate_path_components(buf
, len
);
1089 /* zero stale data in rest of space in data fork, if any */
1090 if (zero_stale_data
&& len
< XFS_DFORK_DSIZE(dip
, mp
))
1091 memset(&buf
[len
], 0, XFS_DFORK_DSIZE(dip
, mp
) - len
);
1099 * with extended attributes, obfuscate the names and fill the actual
1100 * values with 'v' (to see a valid string length, as opposed to NULLs)
1103 xfs_attr_shortform_t
*asfp
;
1104 xfs_attr_sf_entry_t
*asfep
;
1108 asfp
= (xfs_attr_shortform_t
*)XFS_DFORK_APTR(dip
);
1109 if (asfp
->hdr
.count
== 0)
1112 ino_attr_size
= be16_to_cpu(asfp
->hdr
.totsize
);
1113 if (ino_attr_size
> XFS_DFORK_ASIZE(dip
, mp
)) {
1114 ino_attr_size
= XFS_DFORK_ASIZE(dip
, mp
);
1116 print_warning("invalid attr size in inode %llu",
1117 (long long)cur_ino
);
1120 asfep
= &asfp
->list
[0];
1121 for (i
= 0; (i
< asfp
->hdr
.count
) &&
1122 ((char *)asfep
- (char *)asfp
< ino_attr_size
); i
++) {
1124 int namelen
= asfep
->namelen
;
1128 print_warning("zero length attr entry in inode "
1129 "%llu", (long long)cur_ino
);
1131 } else if ((char *)asfep
- (char *)asfp
+
1132 XFS_ATTR_SF_ENTSIZE(asfep
) > ino_attr_size
) {
1134 print_warning("attr entry length in inode %llu "
1135 "overflows space", (long long)cur_ino
);
1140 generate_obfuscated_name(0, asfep
->namelen
,
1141 &asfep
->nameval
[0]);
1142 memset(&asfep
->nameval
[asfep
->namelen
], 'v',
1146 asfep
= (xfs_attr_sf_entry_t
*)((char *)asfep
+
1147 XFS_ATTR_SF_ENTSIZE(asfep
));
1150 /* zero stale data in rest of space in attr fork, if any */
1151 if (zero_stale_data
&& (ino_attr_size
< XFS_DFORK_ASIZE(dip
, mp
)))
1152 memset(asfep
, 0, XFS_DFORK_ASIZE(dip
, mp
) - ino_attr_size
);
1156 process_dir_data_block(
1158 xfs_dfiloff_t offset
,
1159 int is_block_format
)
1162 * we have to rely on the fileoffset and signature of the block to
1163 * handle it's contents. If it's invalid, leave it alone.
1164 * for multi-fsblock dir blocks, if a name crosses an extent boundary,
1165 * ignore it and continue.
1172 struct xfs_dir2_data_hdr
*datahdr
;
1174 datahdr
= (struct xfs_dir2_data_hdr
*)block
;
1176 if (is_block_format
) {
1177 xfs_dir2_leaf_entry_t
*blp
;
1178 xfs_dir2_block_tail_t
*btp
;
1180 btp
= xfs_dir2_block_tail_p(mp
, datahdr
);
1181 blp
= xfs_dir2_block_leaf_p(btp
);
1182 if ((char *)blp
> (char *)btp
)
1183 blp
= (xfs_dir2_leaf_entry_t
*)btp
;
1185 end_of_data
= (char *)blp
- block
;
1186 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1187 wantmagic
= XFS_DIR3_BLOCK_MAGIC
;
1189 wantmagic
= XFS_DIR2_BLOCK_MAGIC
;
1190 } else { /* leaf/node format */
1191 end_of_data
= mp
->m_dirblkfsbs
<< mp
->m_sb
.sb_blocklog
;
1192 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1193 wantmagic
= XFS_DIR3_DATA_MAGIC
;
1195 wantmagic
= XFS_DIR2_DATA_MAGIC
;
1198 if (be32_to_cpu(datahdr
->magic
) != wantmagic
) {
1201 "invalid magic in dir inode %llu block %ld",
1202 (long long)cur_ino
, (long)offset
);
1206 dir_offset
= xfs_dir3_data_entry_offset(datahdr
);
1207 ptr
= block
+ dir_offset
;
1208 endptr
= block
+ mp
->m_dirblksize
;
1210 while (ptr
< endptr
&& dir_offset
< end_of_data
) {
1211 xfs_dir2_data_entry_t
*dep
;
1212 xfs_dir2_data_unused_t
*dup
;
1215 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1217 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
1218 int length
= be16_to_cpu(dup
->length
);
1219 if (dir_offset
+ length
> end_of_data
||
1220 !length
|| (length
& (XFS_DIR2_DATA_ALIGN
- 1))) {
1223 "invalid length for dir free space in inode %llu",
1224 (long long)cur_ino
);
1227 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) !=
1230 dir_offset
+= length
;
1233 * Zero the unused space up to the tag - the tag is
1234 * actually at a variable offset, so zeroing &dup->tag
1235 * is zeroing the free space in between
1237 if (zero_stale_data
) {
1239 sizeof(xfs_dir2_data_unused_t
);
1242 memset(&dup
->tag
, 0, zlen
);
1243 iocur_top
->need_crc
= 1;
1246 if (dir_offset
>= end_of_data
|| ptr
>= endptr
)
1250 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1251 length
= xfs_dir3_data_entsize(mp
, dep
->namelen
);
1253 if (dir_offset
+ length
> end_of_data
||
1254 ptr
+ length
> endptr
) {
1257 "invalid length for dir entry name in inode %llu",
1258 (long long)cur_ino
);
1261 if (be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp
, dep
)) !=
1266 generate_obfuscated_name(be64_to_cpu(dep
->inumber
),
1267 dep
->namelen
, &dep
->name
[0]);
1268 dir_offset
+= length
;
1270 /* Zero the unused space after name, up to the tag */
1271 if (zero_stale_data
) {
1272 /* 1 byte for ftype; don't bother with conditional */
1274 (char *)xfs_dir3_data_entry_tag_p(mp
, dep
) -
1275 (char *)&dep
->name
[dep
->namelen
] - 1;
1277 memset(&dep
->name
[dep
->namelen
] + 1, 0, zlen
);
1278 iocur_top
->need_crc
= 1;
1285 obfuscate_symlink_block(
1288 if (xfs_sb_version_hascrc(&(mp
)->m_sb
))
1289 block
+= sizeof(struct xfs_dsymlink_hdr
);
1291 obfuscate_path_components(block
,
1292 XFS_SYMLINK_BUF_SPACE(mp
,
1293 mp
->m_sb
.sb_blocksize
));
1296 #define MAX_REMOTE_VALS 4095
1298 static struct attr_data_s
{
1299 int remote_val_count
;
1300 xfs_dablk_t remote_vals
[MAX_REMOTE_VALS
];
1305 xfs_dablk_t blockidx
,
1308 while (length
> 0 && attr_data
.remote_val_count
< MAX_REMOTE_VALS
) {
1309 attr_data
.remote_vals
[attr_data
.remote_val_count
] = blockidx
;
1310 attr_data
.remote_val_count
++;
1312 length
-= XFS_LBSIZE(mp
);
1315 if (attr_data
.remote_val_count
>= MAX_REMOTE_VALS
) {
1317 "Overflowed attr obfuscation array. No longer obfuscating remote attrs.");
1321 /* Handle remote and leaf attributes */
1323 obfuscate_attr_block(
1325 xfs_fileoff_t offset
)
1327 struct xfs_attr_leafblock
*leaf
;
1328 struct xfs_attr3_icleaf_hdr hdr
;
1331 xfs_attr_leaf_entry_t
*entry
;
1332 xfs_attr_leaf_name_local_t
*local
;
1333 xfs_attr_leaf_name_remote_t
*remote
;
1334 __uint32_t bs
= mp
->m_sb
.sb_blocksize
;
1337 leaf
= (xfs_attr_leafblock_t
*)block
;
1339 /* Remote attributes - attr3 has XFS_ATTR3_RMT_MAGIC, attr has none */
1340 if ((be16_to_cpu(leaf
->hdr
.info
.magic
) != XFS_ATTR_LEAF_MAGIC
) &&
1341 (be16_to_cpu(leaf
->hdr
.info
.magic
) != XFS_ATTR3_LEAF_MAGIC
)) {
1342 for (i
= 0; i
< attr_data
.remote_val_count
; i
++) {
1343 if (attr_data
.remote_vals
[i
] == offset
)
1344 /* Macros to handle both attr and attr3 */
1346 (bs
- XFS_ATTR3_RMT_BUF_SPACE(mp
, bs
)),
1347 'v', XFS_ATTR3_RMT_BUF_SPACE(mp
, bs
));
1352 /* Ok, it's a leaf - get header; accounts for crc & non-crc */
1353 xfs_attr3_leaf_hdr_from_disk(&hdr
, leaf
);
1355 nentries
= hdr
.count
;
1356 if (nentries
* sizeof(xfs_attr_leaf_entry_t
) +
1357 xfs_attr3_leaf_hdr_size(leaf
) >
1358 XFS_ATTR3_RMT_BUF_SPACE(mp
, bs
)) {
1360 print_warning("invalid attr count in inode %llu",
1361 (long long)cur_ino
);
1365 entry
= xfs_attr3_leaf_entryp(leaf
);
1366 for (i
= 0; i
< nentries
; i
++, entry
++) {
1367 if (be16_to_cpu(entry
->nameidx
) > XFS_LBSIZE(mp
)) {
1370 "invalid attr nameidx in inode %llu",
1371 (long long)cur_ino
);
1374 if (entry
->flags
& XFS_ATTR_LOCAL
) {
1375 local
= xfs_attr3_leaf_name_local(leaf
, i
);
1376 if (local
->namelen
== 0) {
1379 "zero length for attr name in inode %llu",
1380 (long long)cur_ino
);
1383 generate_obfuscated_name(0, local
->namelen
,
1384 &local
->nameval
[0]);
1385 memset(&local
->nameval
[local
->namelen
], 'v',
1386 be16_to_cpu(local
->valuelen
));
1388 remote
= xfs_attr3_leaf_name_remote(leaf
, i
);
1389 if (remote
->namelen
== 0 || remote
->valueblk
== 0) {
1392 "invalid attr entry in inode %llu",
1393 (long long)cur_ino
);
1396 generate_obfuscated_name(0, remote
->namelen
,
1398 add_remote_vals(be32_to_cpu(remote
->valueblk
),
1399 be32_to_cpu(remote
->valuelen
));
1405 process_single_fsb_objects(
1416 for (i
= 0; i
< c
; i
++) {
1418 set_cur(&typtab
[btype
], XFS_FSB_TO_DADDR(mp
, s
), blkbb
,
1421 if (!iocur_top
->data
) {
1422 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, s
);
1423 xfs_agblock_t agbno
= XFS_FSB_TO_AGBNO(mp
, s
);
1425 print_warning("cannot read %s block %u/%u (%llu)",
1426 typtab
[btype
].name
, agno
, agbno
, s
);
1427 if (stop_on_read_error
)
1433 if (!obfuscate
&& !zero_stale_data
)
1436 dp
= iocur_top
->data
;
1439 if (o
>= mp
->m_dirleafblk
)
1442 process_dir_data_block(dp
, o
,
1443 last
== mp
->m_dirblkfsbs
);
1444 iocur_top
->need_crc
= 1;
1448 obfuscate_symlink_block(dp
);
1449 iocur_top
->need_crc
= 1;
1454 obfuscate_attr_block(dp
, o
);
1455 iocur_top
->need_crc
= 1;
1463 ret
= write_buf(iocur_top
);
1476 * Static map to aggregate multiple extents into a single directory block.
1478 static struct bbmap mfsb_map
;
1479 static int mfsb_length
;
1482 process_multi_fsb_objects(
1495 print_warning("bad type for multi-fsb object %d", btype
);
1500 unsigned int bm_len
;
1502 if (mfsb_length
+ c
>= mp
->m_dirblkfsbs
) {
1503 bm_len
= mp
->m_dirblkfsbs
- mfsb_length
;
1510 mfsb_map
.b
[mfsb_map
.nmaps
].bm_bn
= XFS_FSB_TO_DADDR(mp
, s
);
1511 mfsb_map
.b
[mfsb_map
.nmaps
].bm_len
= XFS_FSB_TO_BB(mp
, bm_len
);
1514 if (mfsb_length
== 0) {
1516 set_cur(&typtab
[btype
], 0, 0, DB_RING_IGN
, &mfsb_map
);
1517 if (!iocur_top
->data
) {
1518 xfs_agnumber_t agno
= XFS_FSB_TO_AGNO(mp
, s
);
1519 xfs_agblock_t agbno
= XFS_FSB_TO_AGBNO(mp
, s
);
1521 print_warning("cannot read %s block %u/%u (%llu)",
1522 typtab
[btype
].name
, agno
, agbno
, s
);
1523 if (stop_on_read_error
)
1529 if ((!obfuscate
&& !zero_stale_data
) ||
1530 o
>= mp
->m_dirleafblk
) {
1531 ret
= write_buf(iocur_top
);
1535 process_dir_data_block(iocur_top
->data
, o
,
1536 last
== mp
->m_dirblkfsbs
);
1537 iocur_top
->need_crc
= 1;
1538 ret
= write_buf(iocur_top
);
1552 /* inode copy routines */
1554 process_bmbt_reclist(
1560 xfs_dfiloff_t o
, op
= NULLDFILOFF
;
1562 xfs_dfilblks_t c
, cp
= NULLDFILOFF
;
1565 xfs_agnumber_t agno
;
1566 xfs_agblock_t agbno
;
1569 if (btype
== TYP_DATA
)
1572 convert_extent(&rp
[numrecs
- 1], &o
, &s
, &c
, &f
);
1575 for (i
= 0; i
< numrecs
; i
++, rp
++) {
1576 convert_extent(rp
, &o
, &s
, &c
, &f
);
1579 * ignore extents that are clearly bogus, and if a bogus
1580 * one is found, stop processing remaining extents
1582 if (i
> 0 && op
+ cp
> o
) {
1584 print_warning("bmap extent %d in %s ino %llu "
1585 "starts at %llu, previous extent "
1587 typtab
[btype
].name
, (long long)cur_ino
,
1592 if (c
> max_extent_size
) {
1594 * since we are only processing non-data extents,
1595 * large numbers of blocks in a metadata extent is
1596 * extremely rare and more than likely to be corrupt.
1599 print_warning("suspicious count %u in bmap "
1600 "extent %d in %s ino %llu", c
, i
,
1601 typtab
[btype
].name
, (long long)cur_ino
);
1608 agno
= XFS_FSB_TO_AGNO(mp
, s
);
1609 agbno
= XFS_FSB_TO_AGBNO(mp
, s
);
1611 if (!valid_bno(agno
, agbno
)) {
1613 print_warning("invalid block number %u/%u "
1614 "(%llu) in bmap extent %d in %s ino "
1615 "%llu", agno
, agbno
, s
, i
,
1616 typtab
[btype
].name
, (long long)cur_ino
);
1620 if (!valid_bno(agno
, agbno
+ c
- 1)) {
1622 print_warning("bmap extent %i in %s inode %llu "
1623 "overflows AG (end is %u/%u)", i
,
1624 typtab
[btype
].name
, (long long)cur_ino
,
1625 agno
, agbno
+ c
- 1);
1629 /* multi-extent blocks require special handling */
1630 if (btype
!= TYP_DIR2
|| mp
->m_dirblkfsbs
== 1) {
1631 error
= process_single_fsb_objects(o
, s
, c
, btype
, last
);
1633 error
= process_multi_fsb_objects(o
, s
, c
, btype
, last
);
1644 struct xfs_btree_block
*block
,
1645 xfs_agnumber_t agno
,
1646 xfs_agblock_t agbno
,
1649 void *arg
) /* ptr to itype */
1655 nrecs
= be16_to_cpu(block
->bb_numrecs
);
1658 if (nrecs
> mp
->m_bmap_dmxr
[0]) {
1660 print_warning("invalid numrecs (%u) in %s "
1661 "block %u/%u", nrecs
,
1662 typtab
[btype
].name
, agno
, agbno
);
1665 return process_bmbt_reclist(XFS_BMBT_REC_ADDR(mp
, block
, 1),
1666 nrecs
, *(typnm_t
*)arg
);
1669 if (nrecs
> mp
->m_bmap_dmxr
[1]) {
1671 print_warning("invalid numrecs (%u) in %s block %u/%u",
1672 nrecs
, typtab
[btype
].name
, agno
, agbno
);
1675 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
1676 for (i
= 0; i
< nrecs
; i
++) {
1680 ag
= XFS_FSB_TO_AGNO(mp
, be64_to_cpu(pp
[i
]));
1681 bno
= XFS_FSB_TO_AGBNO(mp
, be64_to_cpu(pp
[i
]));
1683 if (bno
== 0 || bno
> mp
->m_sb
.sb_agblocks
||
1684 ag
> mp
->m_sb
.sb_agcount
) {
1686 print_warning("invalid block number (%u/%u) "
1687 "in %s block %u/%u", ag
, bno
,
1688 typtab
[btype
].name
, agno
, agbno
);
1692 if (!scan_btree(ag
, bno
, level
, btype
, arg
, scanfunc_bmap
))
1703 xfs_bmdr_block_t
*dib
;
1712 whichfork
= (itype
== TYP_ATTR
) ? XFS_ATTR_FORK
: XFS_DATA_FORK
;
1713 btype
= (itype
== TYP_ATTR
) ? TYP_BMAPBTA
: TYP_BMAPBTD
;
1715 dib
= (xfs_bmdr_block_t
*)XFS_DFORK_PTR(dip
, whichfork
);
1716 level
= be16_to_cpu(dib
->bb_level
);
1717 nrecs
= be16_to_cpu(dib
->bb_numrecs
);
1719 if (level
> XFS_BM_MAXLEVELS(mp
, whichfork
)) {
1721 print_warning("invalid level (%u) in inode %lld %s "
1722 "root", level
, (long long)cur_ino
,
1723 typtab
[btype
].name
);
1728 return process_bmbt_reclist(XFS_BMDR_REC_ADDR(dib
, 1),
1732 maxrecs
= xfs_bmdr_maxrecs(mp
, XFS_DFORK_SIZE(dip
, mp
, whichfork
), 0);
1733 if (nrecs
> maxrecs
) {
1735 print_warning("invalid numrecs (%u) in inode %lld %s "
1736 "root", nrecs
, (long long)cur_ino
,
1737 typtab
[btype
].name
);
1741 pp
= XFS_BMDR_PTR_ADDR(dib
, 1, maxrecs
);
1742 for (i
= 0; i
< nrecs
; i
++) {
1746 ag
= XFS_FSB_TO_AGNO(mp
, be64_to_cpu(pp
[i
]));
1747 bno
= XFS_FSB_TO_AGBNO(mp
, be64_to_cpu(pp
[i
]));
1749 if (bno
== 0 || bno
> mp
->m_sb
.sb_agblocks
||
1750 ag
> mp
->m_sb
.sb_agcount
) {
1752 print_warning("invalid block number (%u/%u) "
1753 "in inode %llu %s root", ag
,
1754 bno
, (long long)cur_ino
,
1755 typtab
[btype
].name
);
1759 if (!scan_btree(ag
, bno
, level
, btype
, &itype
, scanfunc_bmap
))
1774 whichfork
= (itype
== TYP_ATTR
) ? XFS_ATTR_FORK
: XFS_DATA_FORK
;
1776 nex
= XFS_DFORK_NEXTENTS(dip
, whichfork
);
1777 used
= nex
* sizeof(xfs_bmbt_rec_t
);
1778 if (nex
< 0 || used
> XFS_DFORK_SIZE(dip
, mp
, whichfork
)) {
1780 print_warning("bad number of extents %d in inode %lld",
1781 nex
, (long long)cur_ino
);
1785 /* Zero unused data fork past used extents */
1786 if (zero_stale_data
&& (used
< XFS_DFORK_SIZE(dip
, mp
, whichfork
)))
1787 memset(XFS_DFORK_PTR(dip
, whichfork
) + used
, 0,
1788 XFS_DFORK_SIZE(dip
, mp
, whichfork
) - used
);
1791 return process_bmbt_reclist((xfs_bmbt_rec_t
*)XFS_DFORK_PTR(dip
,
1792 whichfork
), nex
, itype
);
1800 switch (dip
->di_format
) {
1801 case XFS_DINODE_FMT_LOCAL
:
1802 if (obfuscate
|| zero_stale_data
)
1805 process_sf_dir(dip
);
1809 process_sf_symlink(dip
);
1816 case XFS_DINODE_FMT_EXTENTS
:
1817 return process_exinode(dip
, itype
);
1819 case XFS_DINODE_FMT_BTREE
:
1820 return process_btinode(dip
, itype
);
1826 * when we process the inode, we may change the data in the data and/or
1827 * attribute fork if they are in short form and we are obfuscating names.
1828 * In this case we need to recalculate the CRC of the inode, but we should
1829 * only do that if the CRC in the inode is good to begin with. If the crc
1830 * is not ok, we just leave it alone.
1834 xfs_agnumber_t agno
,
1840 bool crc_was_ok
= false; /* no recalc by default */
1841 bool need_new_crc
= false;
1844 cur_ino
= XFS_AGINO_TO_INO(mp
, agno
, agino
);
1846 /* we only care about crc recalculation if we will modify the inode. */
1847 if (obfuscate
|| zero_stale_data
) {
1848 crc_was_ok
= xfs_verify_cksum((char *)dip
,
1849 mp
->m_sb
.sb_inodesize
,
1850 offsetof(struct xfs_dinode
, di_crc
));
1854 if (zero_stale_data
) {
1855 /* Zero all of the inode literal area */
1856 memset(XFS_DFORK_DPTR(dip
), 0,
1857 XFS_LITINO(mp
, dip
->di_version
));
1862 /* copy appropriate data fork metadata */
1863 switch (be16_to_cpu(dip
->di_mode
) & S_IFMT
) {
1865 success
= process_inode_data(dip
, TYP_DIR2
);
1866 if (dip
->di_format
== XFS_DINODE_FMT_LOCAL
)
1870 success
= process_inode_data(dip
, TYP_SYMLINK
);
1871 if (dip
->di_format
== XFS_DINODE_FMT_LOCAL
)
1875 success
= process_inode_data(dip
, TYP_DATA
);
1881 /* copy extended attributes if they exist and forkoff is valid */
1883 XFS_DFORK_DSIZE(dip
, mp
) < XFS_LITINO(mp
, dip
->di_version
)) {
1884 attr_data
.remote_val_count
= 0;
1885 switch (dip
->di_aformat
) {
1886 case XFS_DINODE_FMT_LOCAL
:
1888 if (obfuscate
|| zero_stale_data
)
1889 process_sf_attr(dip
);
1892 case XFS_DINODE_FMT_EXTENTS
:
1893 success
= process_exinode(dip
, TYP_ATTR
);
1896 case XFS_DINODE_FMT_BTREE
:
1897 success
= process_btinode(dip
, TYP_ATTR
);
1904 /* Heavy handed but low cost; just do it as a catch-all. */
1905 if (zero_stale_data
)
1908 if (crc_was_ok
&& need_new_crc
)
1909 xfs_dinode_calc_crc(mp
, dip
);
1913 static __uint32_t inodes_copied
= 0;
1917 xfs_agnumber_t agno
,
1918 xfs_inobt_rec_t
*rp
)
1922 xfs_agblock_t agbno
;
1926 agino
= be32_to_cpu(rp
->ir_startino
);
1927 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
1928 off
= XFS_INO_TO_OFFSET(mp
, agino
);
1930 if (agino
== 0 || agino
== NULLAGINO
|| !valid_bno(agno
, agbno
) ||
1931 !valid_bno(agno
, XFS_AGINO_TO_AGBNO(mp
,
1932 agino
+ XFS_INODES_PER_CHUNK
- 1))) {
1934 print_warning("bad inode number %llu (%u/%u)",
1935 XFS_AGINO_TO_INO(mp
, agno
, agino
), agno
, agino
);
1940 set_cur(&typtab
[TYP_INODE
], XFS_AGB_TO_DADDR(mp
, agno
, agbno
),
1941 XFS_FSB_TO_BB(mp
, XFS_IALLOC_BLOCKS(mp
)),
1943 if (iocur_top
->data
== NULL
) {
1944 print_warning("cannot read inode block %u/%u", agno
, agbno
);
1945 rval
= !stop_on_read_error
;
1950 * check for basic assumptions about inode chunks, and if any
1951 * assumptions fail, don't process the inode chunk.
1954 if ((mp
->m_sb
.sb_inopblock
<= XFS_INODES_PER_CHUNK
&& off
!= 0) ||
1955 (mp
->m_sb
.sb_inopblock
> XFS_INODES_PER_CHUNK
&&
1956 off
% XFS_INODES_PER_CHUNK
!= 0) ||
1957 (xfs_sb_version_hasalign(&mp
->m_sb
) &&
1958 mp
->m_sb
.sb_inoalignmt
!= 0 &&
1959 agbno
% mp
->m_sb
.sb_inoalignmt
!= 0)) {
1961 print_warning("badly aligned inode (start = %llu)",
1962 XFS_AGINO_TO_INO(mp
, agno
, agino
));
1963 goto skip_processing
;
1967 * scan through inodes and copy any btree extent lists, directory
1968 * contents and extended attributes.
1970 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++) {
1973 dip
= (xfs_dinode_t
*)((char *)iocur_top
->data
+
1974 ((off
+ i
) << mp
->m_sb
.sb_inodelog
));
1976 /* process_inode handles free inodes, too */
1977 if (!process_inode(agno
, agino
+ i
, dip
,
1978 XFS_INOBT_IS_FREE_DISK(rp
, i
)))
1982 if (write_buf(iocur_top
))
1985 inodes_copied
+= XFS_INODES_PER_CHUNK
;
1988 print_progress("Copied %u of %u inodes (%u of %u AGs)",
1989 inodes_copied
, mp
->m_sb
.sb_icount
, agno
,
1990 mp
->m_sb
.sb_agcount
);
1999 struct xfs_btree_block
*block
,
2000 xfs_agnumber_t agno
,
2001 xfs_agblock_t agbno
,
2006 xfs_inobt_rec_t
*rp
;
2007 xfs_inobt_ptr_t
*pp
;
2010 int finobt
= *(int *) arg
;
2012 numrecs
= be16_to_cpu(block
->bb_numrecs
);
2015 if (numrecs
> mp
->m_inobt_mxr
[0]) {
2017 print_warning("invalid numrecs %d in %s "
2018 "block %u/%u", numrecs
,
2019 typtab
[btype
].name
, agno
, agbno
);
2020 numrecs
= mp
->m_inobt_mxr
[0];
2024 * Only copy the btree blocks for the finobt. The inobt scan
2025 * copies the inode chunks.
2030 rp
= XFS_INOBT_REC_ADDR(mp
, block
, 1);
2031 for (i
= 0; i
< numrecs
; i
++, rp
++) {
2032 if (!copy_inode_chunk(agno
, rp
))
2038 if (numrecs
> mp
->m_inobt_mxr
[1]) {
2040 print_warning("invalid numrecs %d in %s block %u/%u",
2041 numrecs
, typtab
[btype
].name
, agno
, agbno
);
2042 numrecs
= mp
->m_inobt_mxr
[1];
2045 pp
= XFS_INOBT_PTR_ADDR(mp
, block
, 1, mp
->m_inobt_mxr
[1]);
2046 for (i
= 0; i
< numrecs
; i
++) {
2047 if (!valid_bno(agno
, be32_to_cpu(pp
[i
]))) {
2049 print_warning("invalid block number (%u/%u) "
2050 "in %s block %u/%u",
2051 agno
, be32_to_cpu(pp
[i
]),
2052 typtab
[btype
].name
, agno
, agbno
);
2055 if (!scan_btree(agno
, be32_to_cpu(pp
[i
]), level
,
2056 btype
, arg
, scanfunc_ino
))
2064 xfs_agnumber_t agno
,
2071 root
= be32_to_cpu(agi
->agi_root
);
2072 levels
= be32_to_cpu(agi
->agi_level
);
2074 /* validate root and levels before processing the tree */
2075 if (root
== 0 || root
> mp
->m_sb
.sb_agblocks
) {
2077 print_warning("invalid block number (%u) in inobt "
2078 "root in agi %u", root
, agno
);
2081 if (levels
>= XFS_BTREE_MAXLEVELS
) {
2083 print_warning("invalid level (%u) in inobt root "
2084 "in agi %u", levels
, agno
);
2088 if (!scan_btree(agno
, root
, levels
, TYP_INOBT
, &finobt
, scanfunc_ino
))
2091 if (xfs_sb_version_hasfinobt(&mp
->m_sb
)) {
2092 root
= be32_to_cpu(agi
->agi_free_root
);
2093 levels
= be32_to_cpu(agi
->agi_free_level
);
2096 if (!scan_btree(agno
, root
, levels
, TYP_INOBT
, &finobt
,
2106 xfs_agnumber_t agno
)
2110 int stack_count
= 0;
2113 /* copy the superblock of the AG */
2116 set_cur(&typtab
[TYP_SB
], XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
),
2117 XFS_FSS_TO_BB(mp
, 1), DB_RING_IGN
, NULL
);
2118 if (!iocur_top
->data
) {
2119 print_warning("cannot read superblock for ag %u", agno
);
2120 if (stop_on_read_error
)
2123 /* Replace any filesystem label with "L's" */
2125 struct xfs_sb
*sb
= iocur_top
->data
;
2126 memset(sb
->sb_fname
, 'L',
2127 min(strlen(sb
->sb_fname
), sizeof(sb
->sb_fname
)));
2128 iocur_top
->need_crc
= 1;
2130 if (write_buf(iocur_top
))
2134 /* copy the AG free space btree root */
2137 set_cur(&typtab
[TYP_AGF
], XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
2138 XFS_FSS_TO_BB(mp
, 1), DB_RING_IGN
, NULL
);
2139 agf
= iocur_top
->data
;
2140 if (iocur_top
->data
== NULL
) {
2141 print_warning("cannot read agf block for ag %u", agno
);
2142 if (stop_on_read_error
)
2145 if (write_buf(iocur_top
))
2149 /* copy the AG inode btree root */
2152 set_cur(&typtab
[TYP_AGI
], XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
2153 XFS_FSS_TO_BB(mp
, 1), DB_RING_IGN
, NULL
);
2154 agi
= iocur_top
->data
;
2155 if (iocur_top
->data
== NULL
) {
2156 print_warning("cannot read agi block for ag %u", agno
);
2157 if (stop_on_read_error
)
2160 if (write_buf(iocur_top
))
2164 /* copy the AG free list header */
2167 set_cur(&typtab
[TYP_AGFL
], XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
2168 XFS_FSS_TO_BB(mp
, 1), DB_RING_IGN
, NULL
);
2169 if (iocur_top
->data
== NULL
) {
2170 print_warning("cannot read agfl block for ag %u", agno
);
2171 if (stop_on_read_error
)
2174 if (agf
&& zero_stale_data
) {
2175 /* Zero out unused bits of agfl */
2179 agfl_bno
= XFS_BUF_TO_AGFL_BNO(mp
, iocur_top
->bp
);
2180 i
= be32_to_cpu(agf
->agf_fllast
);
2183 if (++i
== XFS_AGFL_SIZE(mp
))
2185 if (i
== be32_to_cpu(agf
->agf_flfirst
))
2187 agfl_bno
[i
] = cpu_to_be32(NULLAGBLOCK
);
2189 iocur_top
->need_crc
= 1;
2191 if (write_buf(iocur_top
))
2195 /* copy AG free space btrees */
2198 print_progress("Copying free space trees of AG %u",
2200 if (!copy_free_bno_btree(agno
, agf
))
2202 if (!copy_free_cnt_btree(agno
, agf
))
2206 /* copy inode btrees and the inodes and their associated metadata */
2208 if (!copy_inodes(agno
, agi
))
2213 while (stack_count
--)
2223 xfs_agnumber_t agno
;
2224 xfs_agblock_t agbno
;
2229 if (ino
== 0 || ino
== NULLFSINO
)
2232 agno
= XFS_INO_TO_AGNO(mp
, ino
);
2233 agino
= XFS_INO_TO_AGINO(mp
, ino
);
2234 agbno
= XFS_AGINO_TO_AGBNO(mp
, agino
);
2235 offset
= XFS_AGINO_TO_OFFSET(mp
, agino
);
2237 if (agno
>= mp
->m_sb
.sb_agcount
|| agbno
>= mp
->m_sb
.sb_agblocks
||
2238 offset
>= mp
->m_sb
.sb_inopblock
) {
2240 print_warning("invalid %s inode number (%lld)",
2241 typtab
[itype
].name
, (long long)ino
);
2246 set_cur(&typtab
[TYP_INODE
], XFS_AGB_TO_DADDR(mp
, agno
, agbno
),
2247 blkbb
, DB_RING_IGN
, NULL
);
2248 if (iocur_top
->data
== NULL
) {
2249 print_warning("cannot read %s inode %lld",
2250 typtab
[itype
].name
, (long long)ino
);
2251 rval
= !stop_on_read_error
;
2254 off_cur(offset
<< mp
->m_sb
.sb_inodelog
, mp
->m_sb
.sb_inodesize
);
2257 rval
= process_inode_data(iocur_top
->data
, itype
);
2265 copy_sb_inodes(void)
2267 if (!copy_ino(mp
->m_sb
.sb_rbmino
, TYP_RTBITMAP
))
2270 if (!copy_ino(mp
->m_sb
.sb_rsumino
, TYP_RTSUMMARY
))
2273 if (!copy_ino(mp
->m_sb
.sb_uquotino
, TYP_DQBLK
))
2276 if (!copy_ino(mp
->m_sb
.sb_gquotino
, TYP_DQBLK
))
2279 return copy_ino(mp
->m_sb
.sb_pquotino
, TYP_DQBLK
);
2288 print_progress("Copying log");
2291 set_cur(&typtab
[TYP_LOG
], XFS_FSB_TO_DADDR(mp
, mp
->m_sb
.sb_logstart
),
2292 mp
->m_sb
.sb_logblocks
* blkbb
, DB_RING_IGN
, NULL
);
2293 if (iocur_top
->data
== NULL
) {
2295 print_warning("cannot read log");
2296 return !stop_on_read_error
;
2299 /* If not obfuscating or zeroing, just copy the log as it is */
2300 if (!obfuscate
&& !zero_stale_data
)
2303 dirty
= xlog_is_dirty(mp
, &x
, 0);
2307 /* clear out a clean log */
2309 print_progress("Zeroing clean log");
2310 memset(iocur_top
->data
, 0,
2311 mp
->m_sb
.sb_logblocks
* mp
->m_sb
.sb_blocksize
);
2314 /* keep the dirty log */
2316 _("Filesystem log is dirty; image will contain unobfuscated metadata in log."));
2319 /* log detection error */
2321 _("Could not discern log; image will contain unobfuscated metadata in log."));
2326 return !write_buf(iocur_top
);
2334 xfs_agnumber_t agno
;
2342 stop_on_read_error
= 0;
2344 if (mp
->m_sb
.sb_magicnum
!= XFS_SB_MAGIC
) {
2345 print_warning("bad superblock magic number %x, giving up",
2346 mp
->m_sb
.sb_magicnum
);
2350 while ((c
= getopt(argc
, argv
, "aegm:ow")) != EOF
) {
2353 zero_stale_data
= 0;
2356 stop_on_read_error
= 1;
2362 max_extent_size
= (int)strtol(optarg
, &p
, 0);
2363 if (*p
!= '\0' || max_extent_size
<= 0) {
2364 print_warning("bad max extent size %s",
2376 print_warning("bad option for metadump command");
2381 if (optind
!= argc
- 1) {
2382 print_warning("too few options for metadump (no filename given)");
2386 metablock
= (xfs_metablock_t
*)calloc(BBSIZE
+ 1, BBSIZE
);
2387 if (metablock
== NULL
) {
2388 print_warning("memory allocation failure");
2391 metablock
->mb_blocklog
= BBSHIFT
;
2392 metablock
->mb_magic
= cpu_to_be32(XFS_MD_MAGIC
);
2394 block_index
= (__be64
*)((char *)metablock
+ sizeof(xfs_metablock_t
));
2395 block_buffer
= (char *)metablock
+ BBSIZE
;
2396 num_indicies
= (BBSIZE
- sizeof(xfs_metablock_t
)) / sizeof(__be64
);
2398 start_iocur_sp
= iocur_sp
;
2400 if (strcmp(argv
[optind
], "-") == 0) {
2401 if (isatty(fileno(stdout
))) {
2402 print_warning("cannot write to a terminal");
2408 outf
= fopen(argv
[optind
], "wb");
2410 print_warning("cannot create dump file");
2418 for (agno
= 0; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
2419 if (!scan_ag(agno
)) {
2425 /* copy realtime and quota inode contents */
2427 exitcode
= !copy_sb_inodes();
2429 /* copy log if it's internal */
2430 if ((mp
->m_sb
.sb_logstart
!= 0) && !exitcode
)
2431 exitcode
= !copy_log();
2433 /* write the remaining index */
2435 exitcode
= write_index() < 0;
2437 if (progress_since_warning
)
2438 fputc('\n', (outf
== stdout
) ? stderr
: stdout
);
2443 /* cleanup iocur stack */
2444 while (iocur_sp
> start_iocur_sp
)