]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/metadump.c
metadump: Zero sparse/unused regions of dir2
[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 1000
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_indicies;
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
82 void
83 metadump_init(void)
84 {
85 add_command(&metadump_cmd);
86 }
87
88 static void
89 metadump_help(void)
90 {
91 dbprintf(_(
92 "\n"
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"
96 " Options:\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);
104 }
105
106 static void
107 print_warning(const char *fmt, ...)
108 {
109 char buf[200];
110 va_list ap;
111
112 if (seenint())
113 return;
114
115 va_start(ap, fmt);
116 vsnprintf(buf, sizeof(buf), fmt, ap);
117 va_end(ap);
118 buf[sizeof(buf)-1] = '\0';
119
120 fprintf(stderr, "%s%s: %s\n", progress_since_warning ? "\n" : "",
121 progname, buf);
122 progress_since_warning = 0;
123 }
124
125 static void
126 print_progress(const char *fmt, ...)
127 {
128 char buf[60];
129 va_list ap;
130 FILE *f;
131
132 if (seenint())
133 return;
134
135 va_start(ap, fmt);
136 vsnprintf(buf, sizeof(buf), fmt, ap);
137 va_end(ap);
138 buf[sizeof(buf)-1] = '\0';
139
140 f = (outf == stdout) ? stderr : stdout;
141 fprintf(f, "\r%-59s", buf);
142 fflush(f);
143 progress_since_warning = 1;
144 }
145
146 /*
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.
151 *
152 * Return 0 for success, -1 for failure.
153 */
154
155 static int
156 write_index(void)
157 {
158 /*
159 * write index block and following data blocks (streaming)
160 */
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));
164 return -errno;
165 }
166
167 memset(block_index, 0, num_indicies * sizeof(__be64));
168 cur_index = 0;
169 return 0;
170 }
171
172 /*
173 * Return 0 for success, -errno for failure.
174 */
175 static int
176 write_buf_segment(
177 char *data,
178 __int64_t off,
179 int len)
180 {
181 int i;
182 int ret;
183
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) {
188 ret = write_index();
189 if (ret)
190 return -EIO;
191 }
192 }
193 return 0;
194 }
195
196 /*
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.
200 *
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.
206 */
207 static int
208 write_buf(
209 iocur_t *buf)
210 {
211 struct xfs_buf *bp = buf->bp;
212 int i;
213 int ret;
214
215 /*
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.
219 */
220 if (buf->need_crc && bp && bp->b_ops && !bp->b_error) {
221 bp->b_ops->verify_write(bp);
222 if (bp->b_error) {
223 print_warning(
224 "obfuscation corrupted block at bno 0x%llx/0x%x",
225 (long long)bp->b_bn, bp->b_bcount);
226 }
227 }
228
229 /* handle discontiguous buffers */
230 if (!buf->bbmap) {
231 ret = write_buf_segment(buf->data, buf->bb, buf->blen);
232 if (ret)
233 return ret;
234 } else {
235 int len = 0;
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);
240 if (ret)
241 return ret;
242 len += buf->bbmap->b[i].bm_len;
243 }
244 }
245 return seenint() ? -EINTR : 0;
246 }
247
248
249 static int
250 scan_btree(
251 xfs_agnumber_t agno,
252 xfs_agblock_t agbno,
253 int level,
254 typnm_t btype,
255 void *arg,
256 int (*func)(struct xfs_btree_block *block,
257 xfs_agnumber_t agno,
258 xfs_agblock_t agbno,
259 int level,
260 typnm_t btype,
261 void *arg))
262 {
263 int rval = 0;
264
265 push_cur();
266 set_cur(&typtab[btype], XFS_AGB_TO_DADDR(mp, agno, agbno), blkbb,
267 DB_RING_IGN, NULL);
268 if (iocur_top->data == NULL) {
269 print_warning("cannot read %s block %u/%u", typtab[btype].name,
270 agno, agbno);
271 rval = !stop_on_read_error;
272 goto pop_out;
273 }
274 if (write_buf(iocur_top))
275 goto pop_out;
276
277 if (!(*func)(iocur_top->data, agno, agbno, level - 1, btype, arg))
278 goto pop_out;
279 rval = 1;
280 pop_out:
281 pop_cur();
282 return rval;
283 }
284
285 /* free space tree copy routines */
286
287 static int
288 valid_bno(
289 xfs_agnumber_t agno,
290 xfs_agblock_t agbno)
291 {
292 if (agno < (mp->m_sb.sb_agcount - 1) && agbno > 0 &&
293 agbno <= mp->m_sb.sb_agblocks)
294 return 1;
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))
299 return 1;
300
301 return 0;
302 }
303
304
305 static int
306 scanfunc_freesp(
307 struct xfs_btree_block *block,
308 xfs_agnumber_t agno,
309 xfs_agblock_t agbno,
310 int level,
311 typnm_t btype,
312 void *arg)
313 {
314 xfs_alloc_ptr_t *pp;
315 int i;
316 int numrecs;
317
318 if (level == 0)
319 return 1;
320
321 numrecs = be16_to_cpu(block->bb_numrecs);
322 if (numrecs > mp->m_alloc_mxr[1]) {
323 if (show_warnings)
324 print_warning("invalid numrecs (%u) in %s block %u/%u",
325 numrecs, typtab[btype].name, agno, agbno);
326 return 1;
327 }
328
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]))) {
332 if (show_warnings)
333 print_warning("invalid block number (%u/%u) "
334 "in %s block %u/%u",
335 agno, be32_to_cpu(pp[i]),
336 typtab[btype].name, agno, agbno);
337 continue;
338 }
339 if (!scan_btree(agno, be32_to_cpu(pp[i]), level, btype, arg,
340 scanfunc_freesp))
341 return 0;
342 }
343 return 1;
344 }
345
346 static int
347 copy_free_bno_btree(
348 xfs_agnumber_t agno,
349 xfs_agf_t *agf)
350 {
351 xfs_agblock_t root;
352 int levels;
353
354 root = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
355 levels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
356
357 /* validate root and levels before processing the tree */
358 if (root == 0 || root > mp->m_sb.sb_agblocks) {
359 if (show_warnings)
360 print_warning("invalid block number (%u) in bnobt "
361 "root in agf %u", root, agno);
362 return 1;
363 }
364 if (levels >= XFS_BTREE_MAXLEVELS) {
365 if (show_warnings)
366 print_warning("invalid level (%u) in bnobt root "
367 "in agf %u", levels, agno);
368 return 1;
369 }
370
371 return scan_btree(agno, root, levels, TYP_BNOBT, agf, scanfunc_freesp);
372 }
373
374 static int
375 copy_free_cnt_btree(
376 xfs_agnumber_t agno,
377 xfs_agf_t *agf)
378 {
379 xfs_agblock_t root;
380 int levels;
381
382 root = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
383 levels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
384
385 /* validate root and levels before processing the tree */
386 if (root == 0 || root > mp->m_sb.sb_agblocks) {
387 if (show_warnings)
388 print_warning("invalid block number (%u) in cntbt "
389 "root in agf %u", root, agno);
390 return 1;
391 }
392 if (levels >= XFS_BTREE_MAXLEVELS) {
393 if (show_warnings)
394 print_warning("invalid level (%u) in cntbt root "
395 "in agf %u", levels, agno);
396 return 1;
397 }
398
399 return scan_btree(agno, root, levels, TYP_CNTBT, agf, scanfunc_freesp);
400 }
401
402 /* filename and extended attribute obfuscation routines */
403
404 struct name_ent {
405 struct name_ent *next;
406 xfs_dahash_t hash;
407 int namelen;
408 uchar_t name[1];
409 };
410
411 #define NAME_TABLE_SIZE 4096
412
413 static struct name_ent *nametable[NAME_TABLE_SIZE];
414
415 static void
416 nametable_clear(void)
417 {
418 int i;
419 struct name_ent *ent;
420
421 for (i = 0; i < NAME_TABLE_SIZE; i++) {
422 while ((ent = nametable[i])) {
423 nametable[i] = ent->next;
424 free(ent);
425 }
426 }
427 }
428
429 /*
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.
432 */
433 static struct name_ent *
434 nametable_find(xfs_dahash_t hash, int namelen, uchar_t *name)
435 {
436 struct name_ent *ent;
437
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))
441 return ent;
442 }
443 return NULL;
444 }
445
446 /*
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.
449 */
450 static struct name_ent *
451 nametable_add(xfs_dahash_t hash, int namelen, uchar_t *name)
452 {
453 struct name_ent *ent;
454
455 ent = malloc(sizeof *ent + namelen);
456 if (!ent)
457 return NULL;
458
459 ent->namelen = namelen;
460 memcpy(ent->name, name, namelen);
461 ent->hash = hash;
462 ent->next = nametable[hash % NAME_TABLE_SIZE];
463
464 nametable[hash % NAME_TABLE_SIZE] = ent;
465
466 return ent;
467 }
468
469 #define is_invalid_char(c) ((c) == '/' || (c) == '\0')
470 #define rol32(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
471
472 static inline uchar_t
473 random_filename_char(void)
474 {
475 static uchar_t filename_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
476 "abcdefghijklmnopqrstuvwxyz"
477 "0123456789-_";
478
479 return filename_alphabet[random() % (sizeof filename_alphabet - 1)];
480 }
481
482 #define ORPHANAGE "lost+found"
483 #define ORPHANAGE_LEN (sizeof (ORPHANAGE) - 1)
484
485 static inline int
486 is_orphanage_dir(
487 struct xfs_mount *mp,
488 xfs_ino_t dir_ino,
489 size_t name_len,
490 uchar_t *name)
491 {
492 return dir_ino == mp->m_sb.sb_rootino &&
493 name_len == ORPHANAGE_LEN &&
494 !memcmp(name, ORPHANAGE, ORPHANAGE_LEN);
495 }
496
497 /*
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
501 * processed.
502 *
503 * Returns 1 if the name should NOT be obfuscated or 0 otherwise.
504 */
505 static int
506 in_lost_found(
507 xfs_ino_t ino,
508 int namelen,
509 uchar_t *name)
510 {
511 static xfs_ino_t orphanage_ino = 0;
512 char s[24]; /* 21 is enough (64 bits in decimal) */
513 int slen;
514
515 /* Record the "lost+found" inode if we haven't done so already */
516
517 ASSERT(ino != 0);
518 if (!orphanage_ino && is_orphanage_dir(mp, cur_ino, namelen, name))
519 orphanage_ino = ino;
520
521 /* We don't obfuscate the "lost+found" directory itself */
522
523 if (ino == orphanage_ino)
524 return 1;
525
526 /* Most files aren't in "lost+found" at all */
527
528 if (cur_ino != orphanage_ino)
529 return 0;
530
531 /*
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.
535 */
536 slen = snprintf(s, sizeof (s), "%llu", (unsigned long long) ino);
537
538 return slen == namelen && !memcmp(name, s, namelen);
539 }
540
541 /*
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
544 * same hash value.
545 */
546 static void
547 obfuscate_name(
548 xfs_dahash_t hash,
549 size_t name_len,
550 uchar_t *name)
551 {
552 uchar_t *newp = name;
553 int i;
554 xfs_dahash_t new_hash = 0;
555 uchar_t *first;
556 uchar_t high_bit;
557 int shift;
558
559 /*
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.
565 */
566 if (name_len < 5)
567 return;
568
569 /*
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.
573 */
574 for (i = 0; i < name_len - 5; i++) {
575 *newp = random_filename_char();
576 new_hash = *newp ^ rol32(new_hash, 7);
577 newp++;
578 }
579
580 /*
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.
588 */
589 new_hash = rol32(new_hash, 3) ^ hash;
590
591 first = newp;
592 high_bit = 0;
593 for (shift = 28; shift >= 0; shift -= 7) {
594 *newp = (new_hash >> shift & 0x7f) ^ high_bit;
595 if (is_invalid_char(*newp)) {
596 *newp ^= 1;
597 high_bit = 0x80;
598 } else
599 high_bit = 0;
600 ASSERT(!is_invalid_char(*newp));
601 newp++;
602 }
603
604 /*
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).
610 */
611 if (high_bit) {
612 *first ^= 0x10;
613 ASSERT(!is_invalid_char(*first));
614 }
615 ASSERT(libxfs_da_hashname(name, name_len) == hash);
616 }
617
618 /*
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.
622 *
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
625 * preserve the hash.
626 *
627 * The following diagram aims to show the portion of a computed
628 * hash that a given byte of a name affects.
629 *
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 --->|
638 * . . . and so on
639 *
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).
644 *
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,
649 * actually).
650 *
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.
658 *
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
663 * length.
664 *
665 * Discussion
666 * ----------
667 * (Also see the discussion above find_alternate(), below.)
668 *
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.
673 *
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.
681 *
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.
689 *
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.
693 *
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
700 * so.
701 */
702 static int
703 flip_bit(
704 size_t name_len,
705 uchar_t *name,
706 uint32_t bitseq)
707 {
708 int index;
709 size_t offset;
710 uchar_t *p0, *p1;
711 uchar_t m0, m1;
712 struct {
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. */
730 /* . . . */
731 };
732
733 /* Find the first entry *not* usable for name of this length */
734
735 for (index = 0; index < ARRAY_SIZE(bit_to_flip); index++)
736 if (bit_to_flip[index][1].byte >= name_len)
737 break;
738
739 /*
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.
743 */
744 if (bitseq > --index)
745 return -1;
746
747 /*
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.
751 */
752 offset = name_len - (bit_to_flip[index][1].byte + 1);
753 index -= bitseq; /* Use later table entries first */
754
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;
759
760 /* Only change the bytes if it produces valid characters */
761
762 if (is_invalid_char(*p0 ^ m0) || is_invalid_char(*p1 ^ m1))
763 return 0;
764
765 *p0 ^= m0;
766 *p1 ^= m1;
767
768 return 1;
769 }
770
771 /*
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.
779 *
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
787 * ... and so on.
788 *
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.
793 *
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.
799 *
800 *
801 * Discussion
802 * ----------
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.)
811 *
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.
815 *
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
825 * ...
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.
831 *
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.
839 */
840 static int
841 find_alternate(
842 size_t name_len,
843 uchar_t *name,
844 uint32_t seq)
845 {
846 uint32_t bitseq = 0;
847 uint32_t bits = seq;
848
849 if (!seq)
850 return 1; /* alternate 0 is the original name */
851 if (name_len < 2) /* Must have 2 bytes to flip */
852 return -1;
853
854 for (bitseq = 0; bits; bitseq++) {
855 uint32_t mask = 1 << bitseq;
856 int fb;
857
858 if (!(bits & mask))
859 continue;
860
861 fb = flip_bit(name_len, name, bitseq);
862 if (fb < 1)
863 return fb ? -1 : 0;
864 bits ^= mask;
865 }
866
867 return 1;
868 }
869
870 /*
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.
874 *
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.
878 */
879 static int
880 handle_duplicate_name(xfs_dahash_t hash, size_t name_len, uchar_t *name)
881 {
882 uchar_t new_name[name_len + 1];
883 uint32_t seq = 1;
884
885 if (!nametable_find(hash, name_len, name))
886 return 1; /* No duplicate */
887
888 /* Name is already in use. Need to find an alternate. */
889
890 do {
891 int found;
892
893 /* Only change incoming name if we find an alternate */
894 do {
895 memcpy(new_name, name, name_len);
896 found = find_alternate(name_len, new_name, seq++);
897 if (found < 0)
898 return 0; /* No more to check */
899 } while (!found);
900 } while (nametable_find(hash, name_len, new_name));
901
902 /*
903 * The alternate wasn't in the table already. Pass it back
904 * to the caller.
905 */
906 memcpy(name, new_name, name_len);
907
908 return 1;
909 }
910
911 static void
912 generate_obfuscated_name(
913 xfs_ino_t ino,
914 int namelen,
915 uchar_t *name)
916 {
917 xfs_dahash_t hash;
918
919 /*
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.
924 */
925 if (ino && in_lost_found(ino, namelen, name))
926 return;
927
928 /*
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).
933 */
934 if (*name == '/')
935 name++;
936
937 /* Obfuscate the name (if possible) */
938
939 hash = libxfs_da_hashname(name, namelen);
940 obfuscate_name(hash, namelen, name);
941
942 /*
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.
947 */
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);
953 return;
954 }
955
956 /* Create an entry for the new name in the name table. */
957
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);
963 }
964
965 static void
966 process_sf_dir(
967 xfs_dinode_t *dip)
968 {
969 struct xfs_dir2_sf_hdr *sfp;
970 xfs_dir2_sf_entry_t *sfep;
971 __uint64_t ino_dir_size;
972 int i;
973
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);
978 if (show_warnings)
979 print_warning("invalid size in dir inode %llu",
980 (long long)cur_ino);
981 }
982
983 sfep = xfs_dir2_sf_firstentry(sfp);
984 for (i = 0; (i < sfp->count) &&
985 ((char *)sfep - (char *)sfp < ino_dir_size); i++) {
986
987 /*
988 * first check for bad name lengths. If they are bad, we
989 * have limitations to how much can be obfuscated.
990 */
991 int namelen = sfep->namelen;
992
993 if (namelen == 0) {
994 if (show_warnings)
995 print_warning("zero length entry in dir inode "
996 "%llu", (long long)cur_ino);
997 if (i != sfp->count - 1)
998 break;
999 namelen = ino_dir_size - ((char *)&sfep->name[0] -
1000 (char *)sfp);
1001 } else if ((char *)sfep - (char *)sfp +
1002 xfs_dir3_sf_entsize(mp, sfp, sfep->namelen) >
1003 ino_dir_size) {
1004 if (show_warnings)
1005 print_warning("entry length in dir inode %llu "
1006 "overflows space", (long long)cur_ino);
1007 if (i != sfp->count - 1)
1008 break;
1009 namelen = ino_dir_size - ((char *)&sfep->name[0] -
1010 (char *)sfp);
1011 }
1012
1013 if (obfuscate)
1014 generate_obfuscated_name(
1015 xfs_dir3_sfe_get_ino(mp, sfp, sfep),
1016 namelen, &sfep->name[0]);
1017
1018 sfep = (xfs_dir2_sf_entry_t *)((char *)sfep +
1019 xfs_dir3_sf_entsize(mp, sfp, namelen));
1020 }
1021
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);
1025 }
1026
1027 /*
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.
1033 */
1034 static void
1035 obfuscate_path_components(
1036 char *buf,
1037 __uint64_t len)
1038 {
1039 uchar_t *comp = (uchar_t *)buf;
1040 uchar_t *end = comp + len;
1041 xfs_dahash_t hash;
1042
1043 while (comp < end) {
1044 char *slash;
1045 int namelen;
1046
1047 /* find slash at end of this component */
1048 slash = strchr((char *)comp, '/');
1049 if (!slash) {
1050 /* last (or single) component */
1051 namelen = strnlen((char *)comp, len);
1052 hash = libxfs_da_hashname(comp, namelen);
1053 obfuscate_name(hash, namelen, comp);
1054 break;
1055 }
1056 namelen = slash - (char *)comp;
1057 /* handle leading or consecutive slashes */
1058 if (!namelen) {
1059 comp++;
1060 len--;
1061 continue;
1062 }
1063 hash = libxfs_da_hashname(comp, namelen);
1064 obfuscate_name(hash, namelen, comp);
1065 comp += namelen + 1;
1066 len -= namelen + 1;
1067 }
1068 }
1069
1070 static void
1071 process_sf_symlink(
1072 xfs_dinode_t *dip)
1073 {
1074 __uint64_t len;
1075 char *buf;
1076
1077 len = be64_to_cpu(dip->di_size);
1078 if (len > XFS_DFORK_DSIZE(dip, mp)) {
1079 if (show_warnings)
1080 print_warning("invalid size (%d) in symlink inode %llu",
1081 len, (long long)cur_ino);
1082 len = XFS_DFORK_DSIZE(dip, mp);
1083 }
1084
1085 buf = (char *)XFS_DFORK_DPTR(dip);
1086 if (obfuscate)
1087 obfuscate_path_components(buf, len);
1088
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);
1092 }
1093
1094 static void
1095 process_sf_attr(
1096 xfs_dinode_t *dip)
1097 {
1098 /*
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)
1101 */
1102
1103 xfs_attr_shortform_t *asfp;
1104 xfs_attr_sf_entry_t *asfep;
1105 int ino_attr_size;
1106 int i;
1107
1108 asfp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
1109 if (asfp->hdr.count == 0)
1110 return;
1111
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);
1115 if (show_warnings)
1116 print_warning("invalid attr size in inode %llu",
1117 (long long)cur_ino);
1118 }
1119
1120 asfep = &asfp->list[0];
1121 for (i = 0; (i < asfp->hdr.count) &&
1122 ((char *)asfep - (char *)asfp < ino_attr_size); i++) {
1123
1124 int namelen = asfep->namelen;
1125
1126 if (namelen == 0) {
1127 if (show_warnings)
1128 print_warning("zero length attr entry in inode "
1129 "%llu", (long long)cur_ino);
1130 break;
1131 } else if ((char *)asfep - (char *)asfp +
1132 XFS_ATTR_SF_ENTSIZE(asfep) > ino_attr_size) {
1133 if (show_warnings)
1134 print_warning("attr entry length in inode %llu "
1135 "overflows space", (long long)cur_ino);
1136 break;
1137 }
1138
1139 if (obfuscate) {
1140 generate_obfuscated_name(0, asfep->namelen,
1141 &asfep->nameval[0]);
1142 memset(&asfep->nameval[asfep->namelen], 'v',
1143 asfep->valuelen);
1144 }
1145
1146 asfep = (xfs_attr_sf_entry_t *)((char *)asfep +
1147 XFS_ATTR_SF_ENTSIZE(asfep));
1148 }
1149
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);
1153 }
1154
1155 static void
1156 process_dir_data_block(
1157 char *block,
1158 xfs_dfiloff_t offset,
1159 int is_block_format)
1160 {
1161 /*
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.
1166 */
1167 int dir_offset;
1168 char *ptr;
1169 char *endptr;
1170 int end_of_data;
1171 int wantmagic;
1172 struct xfs_dir2_data_hdr *datahdr;
1173
1174 datahdr = (struct xfs_dir2_data_hdr *)block;
1175
1176 if (is_block_format) {
1177 xfs_dir2_leaf_entry_t *blp;
1178 xfs_dir2_block_tail_t *btp;
1179
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;
1184
1185 end_of_data = (char *)blp - block;
1186 if (xfs_sb_version_hascrc(&mp->m_sb))
1187 wantmagic = XFS_DIR3_BLOCK_MAGIC;
1188 else
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;
1194 else
1195 wantmagic = XFS_DIR2_DATA_MAGIC;
1196 }
1197
1198 if (be32_to_cpu(datahdr->magic) != wantmagic) {
1199 if (show_warnings)
1200 print_warning(
1201 "invalid magic in dir inode %llu block %ld",
1202 (long long)cur_ino, (long)offset);
1203 return;
1204 }
1205
1206 dir_offset = xfs_dir3_data_entry_offset(datahdr);
1207 ptr = block + dir_offset;
1208 endptr = block + mp->m_dirblksize;
1209
1210 while (ptr < endptr && dir_offset < end_of_data) {
1211 xfs_dir2_data_entry_t *dep;
1212 xfs_dir2_data_unused_t *dup;
1213 int length;
1214
1215 dup = (xfs_dir2_data_unused_t *)ptr;
1216
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))) {
1221 if (show_warnings)
1222 print_warning(
1223 "invalid length for dir free space in inode %llu",
1224 (long long)cur_ino);
1225 return;
1226 }
1227 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) !=
1228 dir_offset)
1229 return;
1230 dir_offset += length;
1231 ptr += length;
1232 /*
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
1236 */
1237 if (zero_stale_data) {
1238 int zlen = length -
1239 sizeof(xfs_dir2_data_unused_t);
1240
1241 if (zlen > 0) {
1242 memset(&dup->tag, 0, zlen);
1243 iocur_top->need_crc = 1;
1244 }
1245 }
1246 if (dir_offset >= end_of_data || ptr >= endptr)
1247 return;
1248 }
1249
1250 dep = (xfs_dir2_data_entry_t *)ptr;
1251 length = xfs_dir3_data_entsize(mp, dep->namelen);
1252
1253 if (dir_offset + length > end_of_data ||
1254 ptr + length > endptr) {
1255 if (show_warnings)
1256 print_warning(
1257 "invalid length for dir entry name in inode %llu",
1258 (long long)cur_ino);
1259 return;
1260 }
1261 if (be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp, dep)) !=
1262 dir_offset)
1263 return;
1264
1265 if (obfuscate)
1266 generate_obfuscated_name(be64_to_cpu(dep->inumber),
1267 dep->namelen, &dep->name[0]);
1268 dir_offset += length;
1269 ptr += 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 */
1273 int zlen =
1274 (char *)xfs_dir3_data_entry_tag_p(mp, dep) -
1275 (char *)&dep->name[dep->namelen] - 1;
1276 if (zlen > 0) {
1277 memset(&dep->name[dep->namelen] + 1, 0, zlen);
1278 iocur_top->need_crc = 1;
1279 }
1280 }
1281 }
1282 }
1283
1284 static void
1285 obfuscate_symlink_block(
1286 char *block)
1287 {
1288 if (xfs_sb_version_hascrc(&(mp)->m_sb))
1289 block += sizeof(struct xfs_dsymlink_hdr);
1290
1291 obfuscate_path_components(block,
1292 XFS_SYMLINK_BUF_SPACE(mp,
1293 mp->m_sb.sb_blocksize));
1294 }
1295
1296 #define MAX_REMOTE_VALS 4095
1297
1298 static struct attr_data_s {
1299 int remote_val_count;
1300 xfs_dablk_t remote_vals[MAX_REMOTE_VALS];
1301 } attr_data;
1302
1303 static inline void
1304 add_remote_vals(
1305 xfs_dablk_t blockidx,
1306 int length)
1307 {
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++;
1311 blockidx++;
1312 length -= XFS_LBSIZE(mp);
1313 }
1314
1315 if (attr_data.remote_val_count >= MAX_REMOTE_VALS) {
1316 print_warning(
1317 "Overflowed attr obfuscation array. No longer obfuscating remote attrs.");
1318 }
1319 }
1320
1321 /* Handle remote and leaf attributes */
1322 static void
1323 obfuscate_attr_block(
1324 char *block,
1325 xfs_fileoff_t offset)
1326 {
1327 struct xfs_attr_leafblock *leaf;
1328 struct xfs_attr3_icleaf_hdr hdr;
1329 int i;
1330 int nentries;
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;
1335
1336
1337 leaf = (xfs_attr_leafblock_t *)block;
1338
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 */
1345 memset(block +
1346 (bs - XFS_ATTR3_RMT_BUF_SPACE(mp, bs)),
1347 'v', XFS_ATTR3_RMT_BUF_SPACE(mp, bs));
1348 }
1349 return;
1350 }
1351
1352 /* Ok, it's a leaf - get header; accounts for crc & non-crc */
1353 xfs_attr3_leaf_hdr_from_disk(&hdr, leaf);
1354
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)) {
1359 if (show_warnings)
1360 print_warning("invalid attr count in inode %llu",
1361 (long long)cur_ino);
1362 return;
1363 }
1364
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)) {
1368 if (show_warnings)
1369 print_warning(
1370 "invalid attr nameidx in inode %llu",
1371 (long long)cur_ino);
1372 break;
1373 }
1374 if (entry->flags & XFS_ATTR_LOCAL) {
1375 local = xfs_attr3_leaf_name_local(leaf, i);
1376 if (local->namelen == 0) {
1377 if (show_warnings)
1378 print_warning(
1379 "zero length for attr name in inode %llu",
1380 (long long)cur_ino);
1381 break;
1382 }
1383 generate_obfuscated_name(0, local->namelen,
1384 &local->nameval[0]);
1385 memset(&local->nameval[local->namelen], 'v',
1386 be16_to_cpu(local->valuelen));
1387 } else {
1388 remote = xfs_attr3_leaf_name_remote(leaf, i);
1389 if (remote->namelen == 0 || remote->valueblk == 0) {
1390 if (show_warnings)
1391 print_warning(
1392 "invalid attr entry in inode %llu",
1393 (long long)cur_ino);
1394 break;
1395 }
1396 generate_obfuscated_name(0, remote->namelen,
1397 &remote->name[0]);
1398 add_remote_vals(be32_to_cpu(remote->valueblk),
1399 be32_to_cpu(remote->valuelen));
1400 }
1401 }
1402 }
1403
1404 static int
1405 process_single_fsb_objects(
1406 xfs_dfiloff_t o,
1407 xfs_dfsbno_t s,
1408 xfs_dfilblks_t c,
1409 typnm_t btype,
1410 xfs_dfiloff_t last)
1411 {
1412 char *dp;
1413 int ret = 0;
1414 int i;
1415
1416 for (i = 0; i < c; i++) {
1417 push_cur();
1418 set_cur(&typtab[btype], XFS_FSB_TO_DADDR(mp, s), blkbb,
1419 DB_RING_IGN, NULL);
1420
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);
1424
1425 print_warning("cannot read %s block %u/%u (%llu)",
1426 typtab[btype].name, agno, agbno, s);
1427 if (stop_on_read_error)
1428 ret = -EIO;
1429 goto out_pop;
1430
1431 }
1432
1433 if (!obfuscate && !zero_stale_data)
1434 goto write;
1435
1436 dp = iocur_top->data;
1437 switch (btype) {
1438 case TYP_DIR2:
1439 if (o >= mp->m_dirleafblk)
1440 break;
1441
1442 process_dir_data_block(dp, o,
1443 last == mp->m_dirblkfsbs);
1444 iocur_top->need_crc = 1;
1445 break;
1446 case TYP_SYMLINK:
1447 if (obfuscate) {
1448 obfuscate_symlink_block(dp);
1449 iocur_top->need_crc = 1;
1450 }
1451 break;
1452 case TYP_ATTR:
1453 if (obfuscate) {
1454 obfuscate_attr_block(dp, o);
1455 iocur_top->need_crc = 1;
1456 }
1457 break;
1458 default:
1459 break;
1460 }
1461
1462 write:
1463 ret = write_buf(iocur_top);
1464 out_pop:
1465 pop_cur();
1466 if (ret)
1467 break;
1468 o++;
1469 s++;
1470 }
1471
1472 return ret;
1473 }
1474
1475 /*
1476 * Static map to aggregate multiple extents into a single directory block.
1477 */
1478 static struct bbmap mfsb_map;
1479 static int mfsb_length;
1480
1481 static int
1482 process_multi_fsb_objects(
1483 xfs_dfiloff_t o,
1484 xfs_dfsbno_t s,
1485 xfs_dfilblks_t c,
1486 typnm_t btype,
1487 xfs_dfiloff_t last)
1488 {
1489 int ret = 0;
1490
1491 switch (btype) {
1492 case TYP_DIR2:
1493 break;
1494 default:
1495 print_warning("bad type for multi-fsb object %d", btype);
1496 return -EINVAL;
1497 }
1498
1499 while (c > 0) {
1500 unsigned int bm_len;
1501
1502 if (mfsb_length + c >= mp->m_dirblkfsbs) {
1503 bm_len = mp->m_dirblkfsbs - mfsb_length;
1504 mfsb_length = 0;
1505 } else {
1506 mfsb_length += c;
1507 bm_len = c;
1508 }
1509
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);
1512 mfsb_map.nmaps++;
1513
1514 if (mfsb_length == 0) {
1515 push_cur();
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);
1520
1521 print_warning("cannot read %s block %u/%u (%llu)",
1522 typtab[btype].name, agno, agbno, s);
1523 if (stop_on_read_error)
1524 ret = -1;
1525 goto out_pop;
1526
1527 }
1528
1529 if ((!obfuscate && !zero_stale_data) ||
1530 o >= mp->m_dirleafblk) {
1531 ret = write_buf(iocur_top);
1532 goto out_pop;
1533 }
1534
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);
1539 out_pop:
1540 pop_cur();
1541 mfsb_map.nmaps = 0;
1542 if (ret)
1543 break;
1544 }
1545 c -= bm_len;
1546 s += bm_len;
1547 }
1548
1549 return ret;
1550 }
1551
1552 /* inode copy routines */
1553 static int
1554 process_bmbt_reclist(
1555 xfs_bmbt_rec_t *rp,
1556 int numrecs,
1557 typnm_t btype)
1558 {
1559 int i;
1560 xfs_dfiloff_t o, op = NULLDFILOFF;
1561 xfs_dfsbno_t s;
1562 xfs_dfilblks_t c, cp = NULLDFILOFF;
1563 int f;
1564 xfs_dfiloff_t last;
1565 xfs_agnumber_t agno;
1566 xfs_agblock_t agbno;
1567 int error;
1568
1569 if (btype == TYP_DATA)
1570 return 1;
1571
1572 convert_extent(&rp[numrecs - 1], &o, &s, &c, &f);
1573 last = o + c;
1574
1575 for (i = 0; i < numrecs; i++, rp++) {
1576 convert_extent(rp, &o, &s, &c, &f);
1577
1578 /*
1579 * ignore extents that are clearly bogus, and if a bogus
1580 * one is found, stop processing remaining extents
1581 */
1582 if (i > 0 && op + cp > o) {
1583 if (show_warnings)
1584 print_warning("bmap extent %d in %s ino %llu "
1585 "starts at %llu, previous extent "
1586 "ended at %llu", i,
1587 typtab[btype].name, (long long)cur_ino,
1588 o, op + cp - 1);
1589 break;
1590 }
1591
1592 if (c > max_extent_size) {
1593 /*
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.
1597 */
1598 if (show_warnings)
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);
1602 break;
1603 }
1604
1605 op = o;
1606 cp = c;
1607
1608 agno = XFS_FSB_TO_AGNO(mp, s);
1609 agbno = XFS_FSB_TO_AGBNO(mp, s);
1610
1611 if (!valid_bno(agno, agbno)) {
1612 if (show_warnings)
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);
1617 break;
1618 }
1619
1620 if (!valid_bno(agno, agbno + c - 1)) {
1621 if (show_warnings)
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);
1626 break;
1627 }
1628
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);
1632 } else {
1633 error = process_multi_fsb_objects(o, s, c, btype, last);
1634 }
1635 if (error)
1636 return 0;
1637 }
1638
1639 return 1;
1640 }
1641
1642 static int
1643 scanfunc_bmap(
1644 struct xfs_btree_block *block,
1645 xfs_agnumber_t agno,
1646 xfs_agblock_t agbno,
1647 int level,
1648 typnm_t btype,
1649 void *arg) /* ptr to itype */
1650 {
1651 int i;
1652 xfs_bmbt_ptr_t *pp;
1653 int nrecs;
1654
1655 nrecs = be16_to_cpu(block->bb_numrecs);
1656
1657 if (level == 0) {
1658 if (nrecs > mp->m_bmap_dmxr[0]) {
1659 if (show_warnings)
1660 print_warning("invalid numrecs (%u) in %s "
1661 "block %u/%u", nrecs,
1662 typtab[btype].name, agno, agbno);
1663 return 1;
1664 }
1665 return process_bmbt_reclist(XFS_BMBT_REC_ADDR(mp, block, 1),
1666 nrecs, *(typnm_t*)arg);
1667 }
1668
1669 if (nrecs > mp->m_bmap_dmxr[1]) {
1670 if (show_warnings)
1671 print_warning("invalid numrecs (%u) in %s block %u/%u",
1672 nrecs, typtab[btype].name, agno, agbno);
1673 return 1;
1674 }
1675 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1676 for (i = 0; i < nrecs; i++) {
1677 xfs_agnumber_t ag;
1678 xfs_agblock_t bno;
1679
1680 ag = XFS_FSB_TO_AGNO(mp, be64_to_cpu(pp[i]));
1681 bno = XFS_FSB_TO_AGBNO(mp, be64_to_cpu(pp[i]));
1682
1683 if (bno == 0 || bno > mp->m_sb.sb_agblocks ||
1684 ag > mp->m_sb.sb_agcount) {
1685 if (show_warnings)
1686 print_warning("invalid block number (%u/%u) "
1687 "in %s block %u/%u", ag, bno,
1688 typtab[btype].name, agno, agbno);
1689 continue;
1690 }
1691
1692 if (!scan_btree(ag, bno, level, btype, arg, scanfunc_bmap))
1693 return 0;
1694 }
1695 return 1;
1696 }
1697
1698 static int
1699 process_btinode(
1700 xfs_dinode_t *dip,
1701 typnm_t itype)
1702 {
1703 xfs_bmdr_block_t *dib;
1704 int i;
1705 xfs_bmbt_ptr_t *pp;
1706 int level;
1707 int nrecs;
1708 int maxrecs;
1709 int whichfork;
1710 typnm_t btype;
1711
1712 whichfork = (itype == TYP_ATTR) ? XFS_ATTR_FORK : XFS_DATA_FORK;
1713 btype = (itype == TYP_ATTR) ? TYP_BMAPBTA : TYP_BMAPBTD;
1714
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);
1718
1719 if (level > XFS_BM_MAXLEVELS(mp, whichfork)) {
1720 if (show_warnings)
1721 print_warning("invalid level (%u) in inode %lld %s "
1722 "root", level, (long long)cur_ino,
1723 typtab[btype].name);
1724 return 1;
1725 }
1726
1727 if (level == 0) {
1728 return process_bmbt_reclist(XFS_BMDR_REC_ADDR(dib, 1),
1729 nrecs, itype);
1730 }
1731
1732 maxrecs = xfs_bmdr_maxrecs(mp, XFS_DFORK_SIZE(dip, mp, whichfork), 0);
1733 if (nrecs > maxrecs) {
1734 if (show_warnings)
1735 print_warning("invalid numrecs (%u) in inode %lld %s "
1736 "root", nrecs, (long long)cur_ino,
1737 typtab[btype].name);
1738 return 1;
1739 }
1740
1741 pp = XFS_BMDR_PTR_ADDR(dib, 1, maxrecs);
1742 for (i = 0; i < nrecs; i++) {
1743 xfs_agnumber_t ag;
1744 xfs_agblock_t bno;
1745
1746 ag = XFS_FSB_TO_AGNO(mp, be64_to_cpu(pp[i]));
1747 bno = XFS_FSB_TO_AGBNO(mp, be64_to_cpu(pp[i]));
1748
1749 if (bno == 0 || bno > mp->m_sb.sb_agblocks ||
1750 ag > mp->m_sb.sb_agcount) {
1751 if (show_warnings)
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);
1756 continue;
1757 }
1758
1759 if (!scan_btree(ag, bno, level, btype, &itype, scanfunc_bmap))
1760 return 0;
1761 }
1762 return 1;
1763 }
1764
1765 static int
1766 process_exinode(
1767 xfs_dinode_t *dip,
1768 typnm_t itype)
1769 {
1770 int whichfork;
1771 int used;
1772 xfs_extnum_t nex;
1773
1774 whichfork = (itype == TYP_ATTR) ? XFS_ATTR_FORK : XFS_DATA_FORK;
1775
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)) {
1779 if (show_warnings)
1780 print_warning("bad number of extents %d in inode %lld",
1781 nex, (long long)cur_ino);
1782 return 1;
1783 }
1784
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);
1789
1790
1791 return process_bmbt_reclist((xfs_bmbt_rec_t *)XFS_DFORK_PTR(dip,
1792 whichfork), nex, itype);
1793 }
1794
1795 static int
1796 process_inode_data(
1797 xfs_dinode_t *dip,
1798 typnm_t itype)
1799 {
1800 switch (dip->di_format) {
1801 case XFS_DINODE_FMT_LOCAL:
1802 if (obfuscate || zero_stale_data)
1803 switch (itype) {
1804 case TYP_DIR2:
1805 process_sf_dir(dip);
1806 break;
1807
1808 case TYP_SYMLINK:
1809 process_sf_symlink(dip);
1810 break;
1811
1812 default: ;
1813 }
1814 break;
1815
1816 case XFS_DINODE_FMT_EXTENTS:
1817 return process_exinode(dip, itype);
1818
1819 case XFS_DINODE_FMT_BTREE:
1820 return process_btinode(dip, itype);
1821 }
1822 return 1;
1823 }
1824
1825 /*
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.
1831 */
1832 static int
1833 process_inode(
1834 xfs_agnumber_t agno,
1835 xfs_agino_t agino,
1836 xfs_dinode_t *dip,
1837 bool free_inode)
1838 {
1839 int success;
1840 bool crc_was_ok = false; /* no recalc by default */
1841 bool need_new_crc = false;
1842
1843 success = 1;
1844 cur_ino = XFS_AGINO_TO_INO(mp, agno, agino);
1845
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));
1851 }
1852
1853 if (free_inode) {
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));
1858 }
1859 goto done;
1860 }
1861
1862 /* copy appropriate data fork metadata */
1863 switch (be16_to_cpu(dip->di_mode) & S_IFMT) {
1864 case S_IFDIR:
1865 success = process_inode_data(dip, TYP_DIR2);
1866 if (dip->di_format == XFS_DINODE_FMT_LOCAL)
1867 need_new_crc = 1;
1868 break;
1869 case S_IFLNK:
1870 success = process_inode_data(dip, TYP_SYMLINK);
1871 if (dip->di_format == XFS_DINODE_FMT_LOCAL)
1872 need_new_crc = 1;
1873 break;
1874 case S_IFREG:
1875 success = process_inode_data(dip, TYP_DATA);
1876 break;
1877 default: ;
1878 }
1879 nametable_clear();
1880
1881 /* copy extended attributes if they exist and forkoff is valid */
1882 if (success &&
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:
1887 need_new_crc = 1;
1888 if (obfuscate || zero_stale_data)
1889 process_sf_attr(dip);
1890 break;
1891
1892 case XFS_DINODE_FMT_EXTENTS:
1893 success = process_exinode(dip, TYP_ATTR);
1894 break;
1895
1896 case XFS_DINODE_FMT_BTREE:
1897 success = process_btinode(dip, TYP_ATTR);
1898 break;
1899 }
1900 nametable_clear();
1901 }
1902
1903 done:
1904 /* Heavy handed but low cost; just do it as a catch-all. */
1905 if (zero_stale_data)
1906 need_new_crc = 1;
1907
1908 if (crc_was_ok && need_new_crc)
1909 xfs_dinode_calc_crc(mp, dip);
1910 return success;
1911 }
1912
1913 static __uint32_t inodes_copied = 0;
1914
1915 static int
1916 copy_inode_chunk(
1917 xfs_agnumber_t agno,
1918 xfs_inobt_rec_t *rp)
1919 {
1920 xfs_agino_t agino;
1921 int off;
1922 xfs_agblock_t agbno;
1923 int i;
1924 int rval = 0;
1925
1926 agino = be32_to_cpu(rp->ir_startino);
1927 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
1928 off = XFS_INO_TO_OFFSET(mp, agino);
1929
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))) {
1933 if (show_warnings)
1934 print_warning("bad inode number %llu (%u/%u)",
1935 XFS_AGINO_TO_INO(mp, agno, agino), agno, agino);
1936 return 1;
1937 }
1938
1939 push_cur();
1940 set_cur(&typtab[TYP_INODE], XFS_AGB_TO_DADDR(mp, agno, agbno),
1941 XFS_FSB_TO_BB(mp, XFS_IALLOC_BLOCKS(mp)),
1942 DB_RING_IGN, NULL);
1943 if (iocur_top->data == NULL) {
1944 print_warning("cannot read inode block %u/%u", agno, agbno);
1945 rval = !stop_on_read_error;
1946 goto pop_out;
1947 }
1948
1949 /*
1950 * check for basic assumptions about inode chunks, and if any
1951 * assumptions fail, don't process the inode chunk.
1952 */
1953
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)) {
1960 if (show_warnings)
1961 print_warning("badly aligned inode (start = %llu)",
1962 XFS_AGINO_TO_INO(mp, agno, agino));
1963 goto skip_processing;
1964 }
1965
1966 /*
1967 * scan through inodes and copy any btree extent lists, directory
1968 * contents and extended attributes.
1969 */
1970 for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
1971 xfs_dinode_t *dip;
1972
1973 dip = (xfs_dinode_t *)((char *)iocur_top->data +
1974 ((off + i) << mp->m_sb.sb_inodelog));
1975
1976 /* process_inode handles free inodes, too */
1977 if (!process_inode(agno, agino + i, dip,
1978 XFS_INOBT_IS_FREE_DISK(rp, i)))
1979 goto pop_out;
1980 }
1981 skip_processing:
1982 if (write_buf(iocur_top))
1983 goto pop_out;
1984
1985 inodes_copied += XFS_INODES_PER_CHUNK;
1986
1987 if (show_progress)
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);
1991 rval = 1;
1992 pop_out:
1993 pop_cur();
1994 return rval;
1995 }
1996
1997 static int
1998 scanfunc_ino(
1999 struct xfs_btree_block *block,
2000 xfs_agnumber_t agno,
2001 xfs_agblock_t agbno,
2002 int level,
2003 typnm_t btype,
2004 void *arg)
2005 {
2006 xfs_inobt_rec_t *rp;
2007 xfs_inobt_ptr_t *pp;
2008 int i;
2009 int numrecs;
2010 int finobt = *(int *) arg;
2011
2012 numrecs = be16_to_cpu(block->bb_numrecs);
2013
2014 if (level == 0) {
2015 if (numrecs > mp->m_inobt_mxr[0]) {
2016 if (show_warnings)
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];
2021 }
2022
2023 /*
2024 * Only copy the btree blocks for the finobt. The inobt scan
2025 * copies the inode chunks.
2026 */
2027 if (finobt)
2028 return 1;
2029
2030 rp = XFS_INOBT_REC_ADDR(mp, block, 1);
2031 for (i = 0; i < numrecs; i++, rp++) {
2032 if (!copy_inode_chunk(agno, rp))
2033 return 0;
2034 }
2035 return 1;
2036 }
2037
2038 if (numrecs > mp->m_inobt_mxr[1]) {
2039 if (show_warnings)
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];
2043 }
2044
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]))) {
2048 if (show_warnings)
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);
2053 continue;
2054 }
2055 if (!scan_btree(agno, be32_to_cpu(pp[i]), level,
2056 btype, arg, scanfunc_ino))
2057 return 0;
2058 }
2059 return 1;
2060 }
2061
2062 static int
2063 copy_inodes(
2064 xfs_agnumber_t agno,
2065 xfs_agi_t *agi)
2066 {
2067 xfs_agblock_t root;
2068 int levels;
2069 int finobt = 0;
2070
2071 root = be32_to_cpu(agi->agi_root);
2072 levels = be32_to_cpu(agi->agi_level);
2073
2074 /* validate root and levels before processing the tree */
2075 if (root == 0 || root > mp->m_sb.sb_agblocks) {
2076 if (show_warnings)
2077 print_warning("invalid block number (%u) in inobt "
2078 "root in agi %u", root, agno);
2079 return 1;
2080 }
2081 if (levels >= XFS_BTREE_MAXLEVELS) {
2082 if (show_warnings)
2083 print_warning("invalid level (%u) in inobt root "
2084 "in agi %u", levels, agno);
2085 return 1;
2086 }
2087
2088 if (!scan_btree(agno, root, levels, TYP_INOBT, &finobt, scanfunc_ino))
2089 return 0;
2090
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);
2094
2095 finobt = 1;
2096 if (!scan_btree(agno, root, levels, TYP_INOBT, &finobt,
2097 scanfunc_ino))
2098 return 0;
2099 }
2100
2101 return 1;
2102 }
2103
2104 static int
2105 scan_ag(
2106 xfs_agnumber_t agno)
2107 {
2108 xfs_agf_t *agf;
2109 xfs_agi_t *agi;
2110 int stack_count = 0;
2111 int rval = 0;
2112
2113 /* copy the superblock of the AG */
2114 push_cur();
2115 stack_count++;
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)
2121 goto pop_out;
2122 } else {
2123 /* Replace any filesystem label with "L's" */
2124 if (obfuscate) {
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;
2129 }
2130 if (write_buf(iocur_top))
2131 goto pop_out;
2132 }
2133
2134 /* copy the AG free space btree root */
2135 push_cur();
2136 stack_count++;
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)
2143 goto pop_out;
2144 } else {
2145 if (write_buf(iocur_top))
2146 goto pop_out;
2147 }
2148
2149 /* copy the AG inode btree root */
2150 push_cur();
2151 stack_count++;
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)
2158 goto pop_out;
2159 } else {
2160 if (write_buf(iocur_top))
2161 goto pop_out;
2162 }
2163
2164 /* copy the AG free list header */
2165 push_cur();
2166 stack_count++;
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)
2172 goto pop_out;
2173 } else {
2174 if (agf && zero_stale_data) {
2175 /* Zero out unused bits of agfl */
2176 int i;
2177 __be32 *agfl_bno;
2178
2179 agfl_bno = XFS_BUF_TO_AGFL_BNO(mp, iocur_top->bp);
2180 i = be32_to_cpu(agf->agf_fllast);
2181
2182 for (;;) {
2183 if (++i == XFS_AGFL_SIZE(mp))
2184 i = 0;
2185 if (i == be32_to_cpu(agf->agf_flfirst))
2186 break;
2187 agfl_bno[i] = cpu_to_be32(NULLAGBLOCK);
2188 }
2189 iocur_top->need_crc = 1;
2190 }
2191 if (write_buf(iocur_top))
2192 goto pop_out;
2193 }
2194
2195 /* copy AG free space btrees */
2196 if (agf) {
2197 if (show_progress)
2198 print_progress("Copying free space trees of AG %u",
2199 agno);
2200 if (!copy_free_bno_btree(agno, agf))
2201 goto pop_out;
2202 if (!copy_free_cnt_btree(agno, agf))
2203 goto pop_out;
2204 }
2205
2206 /* copy inode btrees and the inodes and their associated metadata */
2207 if (agi) {
2208 if (!copy_inodes(agno, agi))
2209 goto pop_out;
2210 }
2211 rval = 1;
2212 pop_out:
2213 while (stack_count--)
2214 pop_cur();
2215 return rval;
2216 }
2217
2218 static int
2219 copy_ino(
2220 xfs_ino_t ino,
2221 typnm_t itype)
2222 {
2223 xfs_agnumber_t agno;
2224 xfs_agblock_t agbno;
2225 xfs_agino_t agino;
2226 int offset;
2227 int rval = 0;
2228
2229 if (ino == 0 || ino == NULLFSINO)
2230 return 1;
2231
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);
2236
2237 if (agno >= mp->m_sb.sb_agcount || agbno >= mp->m_sb.sb_agblocks ||
2238 offset >= mp->m_sb.sb_inopblock) {
2239 if (show_warnings)
2240 print_warning("invalid %s inode number (%lld)",
2241 typtab[itype].name, (long long)ino);
2242 return 1;
2243 }
2244
2245 push_cur();
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;
2252 goto pop_out;
2253 }
2254 off_cur(offset << mp->m_sb.sb_inodelog, mp->m_sb.sb_inodesize);
2255
2256 cur_ino = ino;
2257 rval = process_inode_data(iocur_top->data, itype);
2258 pop_out:
2259 pop_cur();
2260 return rval;
2261 }
2262
2263
2264 static int
2265 copy_sb_inodes(void)
2266 {
2267 if (!copy_ino(mp->m_sb.sb_rbmino, TYP_RTBITMAP))
2268 return 0;
2269
2270 if (!copy_ino(mp->m_sb.sb_rsumino, TYP_RTSUMMARY))
2271 return 0;
2272
2273 if (!copy_ino(mp->m_sb.sb_uquotino, TYP_DQBLK))
2274 return 0;
2275
2276 if (!copy_ino(mp->m_sb.sb_gquotino, TYP_DQBLK))
2277 return 0;
2278
2279 return copy_ino(mp->m_sb.sb_pquotino, TYP_DQBLK);
2280 }
2281
2282 static int
2283 copy_log(void)
2284 {
2285 int dirty;
2286
2287 if (show_progress)
2288 print_progress("Copying log");
2289
2290 push_cur();
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) {
2294 pop_cur();
2295 print_warning("cannot read log");
2296 return !stop_on_read_error;
2297 }
2298
2299 /* If not obfuscating or zeroing, just copy the log as it is */
2300 if (!obfuscate && !zero_stale_data)
2301 goto done;
2302
2303 dirty = xlog_is_dirty(mp, &x, 0);
2304
2305 switch (dirty) {
2306 case 0:
2307 /* clear out a clean log */
2308 if (show_progress)
2309 print_progress("Zeroing clean log");
2310 memset(iocur_top->data, 0,
2311 mp->m_sb.sb_logblocks * mp->m_sb.sb_blocksize);
2312 break;
2313 case 1:
2314 /* keep the dirty log */
2315 print_warning(
2316 _("Filesystem log is dirty; image will contain unobfuscated metadata in log."));
2317 break;
2318 case -1:
2319 /* log detection error */
2320 print_warning(
2321 _("Could not discern log; image will contain unobfuscated metadata in log."));
2322 break;
2323 }
2324
2325 done:
2326 return !write_buf(iocur_top);
2327 }
2328
2329 static int
2330 metadump_f(
2331 int argc,
2332 char **argv)
2333 {
2334 xfs_agnumber_t agno;
2335 int c;
2336 int start_iocur_sp;
2337 char *p;
2338
2339 exitcode = 1;
2340 show_progress = 0;
2341 show_warnings = 0;
2342 stop_on_read_error = 0;
2343
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);
2347 return 0;
2348 }
2349
2350 while ((c = getopt(argc, argv, "aegm:ow")) != EOF) {
2351 switch (c) {
2352 case 'a':
2353 zero_stale_data = 0;
2354 break;
2355 case 'e':
2356 stop_on_read_error = 1;
2357 break;
2358 case 'g':
2359 show_progress = 1;
2360 break;
2361 case 'm':
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",
2365 optarg);
2366 return 0;
2367 }
2368 break;
2369 case 'o':
2370 obfuscate = 0;
2371 break;
2372 case 'w':
2373 show_warnings = 1;
2374 break;
2375 default:
2376 print_warning("bad option for metadump command");
2377 return 0;
2378 }
2379 }
2380
2381 if (optind != argc - 1) {
2382 print_warning("too few options for metadump (no filename given)");
2383 return 0;
2384 }
2385
2386 metablock = (xfs_metablock_t *)calloc(BBSIZE + 1, BBSIZE);
2387 if (metablock == NULL) {
2388 print_warning("memory allocation failure");
2389 return 0;
2390 }
2391 metablock->mb_blocklog = BBSHIFT;
2392 metablock->mb_magic = cpu_to_be32(XFS_MD_MAGIC);
2393
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);
2397 cur_index = 0;
2398 start_iocur_sp = iocur_sp;
2399
2400 if (strcmp(argv[optind], "-") == 0) {
2401 if (isatty(fileno(stdout))) {
2402 print_warning("cannot write to a terminal");
2403 free(metablock);
2404 return 0;
2405 }
2406 outf = stdout;
2407 } else {
2408 outf = fopen(argv[optind], "wb");
2409 if (outf == NULL) {
2410 print_warning("cannot create dump file");
2411 free(metablock);
2412 return 0;
2413 }
2414 }
2415
2416 exitcode = 0;
2417
2418 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
2419 if (!scan_ag(agno)) {
2420 exitcode = 1;
2421 break;
2422 }
2423 }
2424
2425 /* copy realtime and quota inode contents */
2426 if (!exitcode)
2427 exitcode = !copy_sb_inodes();
2428
2429 /* copy log if it's internal */
2430 if ((mp->m_sb.sb_logstart != 0) && !exitcode)
2431 exitcode = !copy_log();
2432
2433 /* write the remaining index */
2434 if (!exitcode)
2435 exitcode = write_index() < 0;
2436
2437 if (progress_since_warning)
2438 fputc('\n', (outf == stdout) ? stderr : stdout);
2439
2440 if (outf != stdout)
2441 fclose(outf);
2442
2443 /* cleanup iocur stack */
2444 while (iocur_sp > start_iocur_sp)
2445 pop_cur();
2446
2447 free(metablock);
2448
2449 return 0;
2450 }