]> git.wh0rd.org - dump.git/blob - dump/traverse.c
Use the libext2fs inode-scanning functions.
[dump.git] / dump / traverse.c
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
7 */
8
9 /*-
10 * Copyright (c) 1980, 1988, 1991, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #ifndef lint
43 static const char rcsid[] =
44 "$Id: traverse.c,v 1.24 2000/12/05 16:57:38 stelian Exp $";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #ifdef __linux__
50 #include <linux/ext2_fs.h>
51 #include <bsdcompat.h>
52 #include <compaterr.h>
53 #include <stdlib.h>
54 #define swab32(x) ext2fs_swab32(x)
55 #else /* __linux__ */
56 #define swab32(x) x
57 #ifdef sunos
58 #include <sys/vnode.h>
59
60 #include <ufs/fs.h>
61 #include <ufs/fsdir.h>
62 #include <ufs/inode.h>
63 #else
64 #include <ufs/ufs/dir.h>
65 #include <ufs/ufs/dinode.h>
66 #include <ufs/ffs/fs.h>
67 #endif
68 #endif /* __linux__ */
69
70 #include <protocols/dumprestore.h>
71
72 #include <ctype.h>
73 #include <stdio.h>
74 #ifdef __STDC__
75 #include <string.h>
76 #include <unistd.h>
77 #endif
78
79 #ifdef __linux__
80 #include <ext2fs/ext2fs.h>
81 #endif
82
83 #include "dump.h"
84
85 #define HASDUMPEDFILE 0x1
86 #define HASSUBDIRS 0x2
87
88 #ifdef __linux__
89 typedef u_quad_t fsizeT;
90 #else
91 #ifdef FS_44INODEFMT
92 typedef quad_t fsizeT;
93 #else
94 typedef long fsizeT;
95 #endif
96 #endif
97
98 #ifdef __linux__
99 static int searchdir __P((struct ext2_dir_entry *dp, int offset,
100 int blocksize, char *buf, void *private));
101 #else
102 static int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
103 static void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
104 static int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
105 #endif
106 static void mapfileino __P((ino_t ino, struct dinode const *dp, long *tapesize, int *dirskipped));
107 static int exclude_ino __P((ino_t ino));
108
109 /* #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 */
110 #ifdef EXT3_FEATURE_INCOMPAT_RECOVER
111 #define FORCE_OPEN EXT2_FLAG_FORCE
112 #else
113 #define FORCE_OPEN 0
114 #endif
115
116 int dump_fs_open(const char *disk, ext2_filsys *fs)
117 {
118 int retval;
119 struct ext2fs_sb *s;
120
121 retval = ext2fs_open(disk, FORCE_OPEN, 0, 0, unix_io_manager, fs);
122 #if defined(EXT2_LIB_FEATURE_COMPAT_SUPP) && defined(EXT2_LIB_FEATURE_INCOMPAT_SUPP) && defined(EXT2_LIB_FEATURE_RO_COMPAT_SUPP) && defined(EXT2_ET_UNSUPP_FEATURE) && defined(EXT2_ET_RO_UNSUPP_FEATURE)
123 if (!retval) {
124 s = (struct ext2fs_sb *) (*fs)->super;
125 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
126 #ifdef EXT3_FEATURE_INCOMPAT_RECOVER
127 (s->s_feature_incompat & ~(EXT3_FEATURE_INCOMPAT_RECOVER | EXT2_LIB_FEATURE_INCOMPAT_SUPP))) {
128 #else
129 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
130 #endif
131 retval = EXT2_ET_UNSUPP_FEATURE;
132 }
133 else if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
134 retval = EXT2_ET_RO_UNSUPP_FEATURE;
135 }
136 }
137 #endif /* defined && defined && defined... */
138 return retval;
139 }
140
141 /*
142 * This is an estimation of the number of TP_BSIZE blocks in the file.
143 * It estimates the number of blocks in files with holes by assuming
144 * that all of the blocks accounted for by di_blocks are data blocks
145 * (when some of the blocks are usually used for indirect pointers);
146 * hence the estimate may be high.
147 */
148 long
149 blockest(struct dinode const *dp)
150 {
151 long blkest, sizeest;
152
153 /*
154 * dp->di_size is the size of the file in bytes.
155 * dp->di_blocks stores the number of sectors actually in the file.
156 * If there are more sectors than the size would indicate, this just
157 * means that there are indirect blocks in the file or unused
158 * sectors in the last file block; we can safely ignore these
159 * (blkest = sizeest below).
160 * If the file is bigger than the number of sectors would indicate,
161 * then the file has holes in it. In this case we must use the
162 * block count to estimate the number of data blocks used, but
163 * we use the actual size for estimating the number of indirect
164 * dump blocks (sizeest vs. blkest in the indirect block
165 * calculation).
166 */
167 blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
168 sizeest = howmany(dp->di_size, TP_BSIZE);
169 if (blkest > sizeest)
170 blkest = sizeest;
171 #ifdef __linux__
172 if (dp->di_size > fs->blocksize * NDADDR) {
173 /* calculate the number of indirect blocks on the dump tape */
174 blkest +=
175 howmany(sizeest - NDADDR * fs->blocksize / TP_BSIZE,
176 NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super));
177 }
178 #else
179 if (dp->di_size > sblock->fs_bsize * NDADDR) {
180 /* calculate the number of indirect blocks on the dump tape */
181 blkest +=
182 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
183 TP_NINDIR);
184 }
185 #endif
186 return (blkest + 1);
187 }
188
189 extern ino_t iexclude_list[IEXCLUDE_MAXNUM]; /* the inode exclude list */
190 extern int iexclude_num; /* number of elements in the list */
191
192 /*
193 * This tests whether an inode is in the exclude list
194 */
195 int
196 exclude_ino(ino_t ino)
197 {
198 /* 04-Feb-00 ILC */
199 if (iexclude_num) { /* if there are inodes in the exclude list */
200 int idx; /* then check this inode against it */
201 for (idx = 0; idx < iexclude_num; idx++)
202 if (ino == iexclude_list[idx])
203 return 1;
204 }
205 return 0;
206 }
207
208 /* Auxiliary macro to pick up files changed since previous dump. */
209 #define CHANGEDSINCE(dp, t) \
210 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
211
212 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
213 #ifdef UF_NODUMP
214 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
215 #else
216 #define NODUMP_FLAG(dp) 0
217 #endif
218
219 /* The WANTTODUMP macro decides whether a file should be dumped. */
220 #define WANTTODUMP(dp, ino) \
221 (CHANGEDSINCE(dp, spcl.c_ddate) && \
222 (!NODUMP_FLAG(dp)) && \
223 (!exclude_ino(ino)))
224
225 /*
226 * Determine if given inode should be dumped. "dp" must either point to a
227 * copy of the given inode, or be NULL (in which case it is fetched.)
228 */
229 static void
230 mapfileino(ino_t ino, struct dinode const *dp, long *tapesize, int *dirskipped)
231 {
232 register int mode;
233
234 /*
235 * Skip inode if we've already marked it for dumping
236 */
237 if (TSTINO(ino, usedinomap))
238 return;
239 if (!dp)
240 dp = getino(ino);
241 if ((mode = (dp->di_mode & IFMT)) == 0)
242 return;
243 #ifdef __linux__
244 if (dp->di_nlink == 0 || dp->di_dtime != 0)
245 return;
246 #endif
247 /*
248 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
249 * used map. All inode but dirs who have the nodump attribute go
250 * to the usedinomap.
251 */
252 SETINO(ino, usedinomap);
253
254 if (mode == IFDIR)
255 SETINO(ino, dumpdirmap);
256 if (WANTTODUMP(dp, ino)) {
257 SETINO(ino, dumpinomap);
258 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
259 *tapesize += 1;
260 else
261 *tapesize += blockest(dp);
262 return;
263 }
264 if (mode == IFDIR) {
265 if ( NODUMP_FLAG(dp) || exclude_ino(ino) )
266 CLRINO(ino, usedinomap);
267 *dirskipped = 1;
268 }
269 }
270
271 /*
272 * Dump pass 1.
273 *
274 * Walk the inode list for a filesystem to find all allocated inodes
275 * that have been modified since the previous dump time. Also, find all
276 * the directories in the filesystem.
277 */
278 #ifdef __linux__
279 int
280 mapfiles(ino_t maxino, long *tapesize)
281 {
282 ino_t ino;
283 int anydirskipped = 0;
284 ext2_inode_scan scan;
285 errcode_t err;
286 struct ext2_inode inode;
287
288 /*
289 * We use libext2fs's inode scanning routines, which are particularly
290 * robust. (Note that getino cannot return an error.)
291 */
292 err = ext2fs_open_inode_scan(fs, 0, &scan);
293 if (err) {
294 com_err(disk, err, "while opening inodes\n");
295 exit(X_ABORT);
296 }
297 for (;;) {
298 err = ext2fs_get_next_inode(scan, &ino, &inode);
299 if (err == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
300 continue;
301 if (err) {
302 com_err(disk, err, "while scanning inode #%ld\n",
303 (long)ino);
304 exit(X_ABORT);
305 }
306 if (ino == 0)
307 break;
308
309 curino = ino;
310 mapfileino(ino, (struct dinode const *)&inode, tapesize,
311 &anydirskipped);
312 }
313 ext2fs_close_inode_scan(scan);
314
315 /*
316 * Restore gets very upset if the root is not dumped,
317 * so ensure that it always is dumped.
318 */
319 SETINO(ROOTINO, dumpinomap);
320 return (anydirskipped);
321 }
322 #else
323 int
324 mapfiles(ino_t maxino, long *tapesize)
325 {
326 register ino_t ino;
327 int anydirskipped = 0;
328
329 for (ino = ROOTINO; ino < maxino; ino++)
330 mapfileino(ino, tapesize, &anydirskipped);
331
332 /*
333 * Restore gets very upset if the root is not dumped,
334 * so ensure that it always is dumped.
335 */
336 SETINO(ROOTINO, dumpinomap);
337 return (anydirskipped);
338 }
339 #endif /* __linux__ */
340
341 #ifdef __linux__
342 struct mapfile_context {
343 long *tapesize;
344 int *anydirskipped;
345 };
346
347 static int
348 mapfilesindir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
349 {
350 register struct dinode const *dp;
351 register int mode;
352 errcode_t retval;
353 struct mapfile_context *mfc;
354 ino_t ino;
355
356 ino = dirent->inode;
357 mfc = (struct mapfile_context *)private;
358 dp = getino(dirent->inode);
359
360 mapfileino(dirent->inode, dp, mfc->tapesize, mfc->anydirskipped);
361
362 mode = dp->di_mode & IFMT;
363 if (mode == IFDIR && dp->di_nlink != 0 && dp->di_dtime == 0) {
364 if ((dirent->name[0] != '.' || ( dirent->name_len & 0xFF ) != 1) &&
365 (dirent->name[0] != '.' || dirent->name[1] != '.' ||
366 ( dirent->name_len & 0xFF ) != 2)) {
367 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
368 mapfilesindir, private);
369 if (retval)
370 return retval;
371 }
372 }
373 return 0;
374 }
375
376 /*
377 * Dump pass 1.
378 *
379 * Walk the inode list for a filesystem to find all allocated inodes
380 * that have been modified since the previous dump time. Also, find all
381 * the directories in the filesystem.
382 */
383 int
384 mapfilesfromdir(ino_t maxino, long *tapesize, char *directory)
385 {
386 errcode_t retval;
387 struct mapfile_context mfc;
388 ino_t dir_ino;
389 char dir_name [MAXPATHLEN];
390 int i, anydirskipped = 0;
391
392 /*
393 * Mark every directory in the path as being dumped
394 */
395 for (i = 0; i < strlen (directory); i++) {
396 if (directory[i] == '/') {
397 strncpy (dir_name, directory, i);
398 dir_name[i] = '\0';
399 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
400 &dir_ino);
401 if (retval) {
402 com_err(disk, retval, "while translating %s",
403 dir_name);
404 exit(X_ABORT);
405 }
406 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
407 }
408 }
409 /*
410 * Mark the final directory
411 */
412 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
413 if (retval) {
414 com_err(disk, retval, "while translating %s", directory);
415 exit(X_ABORT);
416 }
417 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
418
419 mfc.tapesize = tapesize;
420 mfc.anydirskipped = &anydirskipped;
421 retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
422 (void *)&mfc);
423
424 if (retval) {
425 com_err(disk, retval, "while mapping files in %s", directory);
426 exit(X_ABORT);
427 }
428 /*
429 * Ensure that the root inode actually appears in the file list
430 * for a subdir
431 */
432 mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
433 /*
434 * Restore gets very upset if the root is not dumped,
435 * so ensure that it always is dumped.
436 */
437 SETINO(ROOTINO, dumpinomap);
438 return anydirskipped;
439 }
440 #endif
441
442 #ifdef __linux__
443 struct mapdirs_context {
444 int *ret;
445 int nodump;
446 long *tapesize;
447 };
448 #endif
449
450 /*
451 * Dump pass 2.
452 *
453 * Scan each directory on the filesystem to see if it has any modified
454 * files in it. If it does, and has not already been added to the dump
455 * list (because it was itself modified), then add it. If a directory
456 * has not been modified itself, contains no modified files and has no
457 * subdirectories, then it can be deleted from the dump list and from
458 * the list of directories. By deleting it from the list of directories,
459 * its parent may now qualify for the same treatment on this or a later
460 * pass using this algorithm.
461 */
462 int
463 mapdirs(ino_t maxino, long *tapesize)
464 {
465 register struct dinode *dp;
466 register int isdir;
467 register char *map;
468 register ino_t ino;
469 #ifndef __linux__
470 register int i;
471 long filesize;
472 #else
473 struct mapdirs_context mdc;
474 #endif
475 int ret, change = 0, nodump;
476
477 isdir = 0; /* XXX just to get gcc to shut up */
478 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
479 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
480 isdir = *map++;
481 else
482 isdir >>= 1;
483 /*
484 * If dir has been removed from the used map, it's either
485 * because it had the nodump flag, or it herited it from
486 * its parent. A directory can't be in dumpinomap if not
487 * in usedinomap, but we have to go through it anyway
488 * to propagate the nodump attribute.
489 */
490 nodump = (TSTINO(ino, usedinomap) == 0);
491 if ((isdir & 1) == 0 ||
492 (TSTINO(ino, dumpinomap) && nodump == 0))
493 continue;
494 dp = getino(ino);
495 #ifdef __linux__
496 ret = 0;
497 mdc.ret = &ret;
498 mdc.nodump = nodump;
499 mdc.tapesize = tapesize;
500 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &mdc);
501 #else /* __linux__ */
502 filesize = dp->di_size;
503 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
504 if (dp->di_db[i] != 0)
505 ret |= searchdir(ino, dp->di_db[i],
506 (long)dblksize(sblock, dp, i),
507 filesize);
508 if (ret & HASDUMPEDFILE)
509 filesize = 0;
510 else
511 filesize -= sblock->fs_bsize;
512 }
513 for (i = 0; filesize > 0 && i < NIADDR; i++) {
514 if (dp->di_ib[i] == 0)
515 continue;
516 ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
517 }
518 #endif /* __linux__ */
519 if (ret & HASDUMPEDFILE) {
520 SETINO(ino, dumpinomap);
521 *tapesize += blockest(dp);
522 change = 1;
523 continue;
524 }
525 if (nodump) {
526 if (ret & HASSUBDIRS)
527 change = 1; /* subdirs have inherited nodump */
528 CLRINO(ino, dumpdirmap);
529 } else if ((ret & HASSUBDIRS) == 0) {
530 if (!TSTINO(ino, dumpinomap)) {
531 CLRINO(ino, dumpdirmap);
532 change = 1;
533 }
534 }
535 }
536 return (change);
537 }
538
539 #ifndef __linux__
540 /*
541 * Read indirect blocks, and pass the data blocks to be searched
542 * as directories. Quit as soon as any entry is found that will
543 * require the directory to be dumped.
544 */
545 static int
546 dirindir(ino_t ino, daddr_t blkno, int ind_level, long *filesize)
547 {
548 int ret = 0;
549 register int i;
550 daddr_t idblk[MAXNINDIR];
551
552 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
553 if (ind_level <= 0) {
554 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
555 blkno = idblk[i];
556 if (blkno != 0)
557 ret |= searchdir(ino, blkno, sblock->fs_bsize,
558 *filesize);
559 if (ret & HASDUMPEDFILE)
560 *filesize = 0;
561 else
562 *filesize -= sblock->fs_bsize;
563 }
564 return (ret);
565 }
566 ind_level--;
567 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
568 blkno = idblk[i];
569 if (blkno != 0)
570 ret |= dirindir(ino, blkno, ind_level, filesize);
571 }
572 return (ret);
573 }
574 #endif /* !__linux__ */
575
576 /*
577 * Scan a disk block containing directory information looking to see if
578 * any of the entries are on the dump list and to see if the directory
579 * contains any subdirectories.
580 */
581 #ifdef __linux__
582 static int
583 searchdir(struct ext2_dir_entry *dp, int offset, int blocksize, char *buf, void *private)
584 {
585 struct mapdirs_context *mdc;
586 int *ret;
587 long *tapesize;
588 struct dinode *ip;
589
590 mdc = (struct mapdirs_context *)private;
591 ret = mdc->ret;
592 tapesize = mdc->tapesize;
593
594 if (dp->inode == 0)
595 return 0;
596 if (dp->name[0] == '.') {
597 if (( dp->name_len & 0xFF ) == 1)
598 return 0;
599 if (dp->name[1] == '.' && ( dp->name_len & 0xFF ) == 2)
600 return 0;
601 }
602 if (mdc->nodump) {
603 ip = getino(dp->inode);
604 if (TSTINO(dp->inode, dumpinomap)) {
605 CLRINO(dp->inode, dumpinomap);
606 CLRINO(dp->inode, usedinomap);
607 *tapesize -= blockest(ip);
608 }
609 /* Add dir back to the dir map, to propagate nodump */
610 if ((ip->di_mode & IFMT) == IFDIR) {
611 SETINO(dp->inode, dumpdirmap);
612 *ret |= HASSUBDIRS;
613 }
614 } else {
615 if (TSTINO(dp->inode, dumpinomap)) {
616 *ret |= HASDUMPEDFILE;
617 if (*ret & HASSUBDIRS)
618 return DIRENT_ABORT;
619 }
620 if (TSTINO(dp->inode, dumpdirmap)) {
621 *ret |= HASSUBDIRS;
622 if (*ret & HASDUMPEDFILE)
623 return DIRENT_ABORT;
624 }
625 }
626 return 0;
627 }
628
629 #else /* __linux__ */
630
631 static int
632 searchdir(ino_t ino, daddr_t blkno, long size, long filesize)
633 {
634 register struct direct *dp;
635 register long loc, ret = 0;
636 char dblk[MAXBSIZE];
637
638 bread(fsbtodb(sblock, blkno), dblk, (int)size);
639 if (filesize < size)
640 size = filesize;
641 for (loc = 0; loc < size; ) {
642 dp = (struct direct *)(dblk + loc);
643 if (dp->d_reclen == 0) {
644 msg("corrupted directory, inumber %d\n", ino);
645 break;
646 }
647 loc += dp->d_reclen;
648 if (dp->d_ino == 0)
649 continue;
650 if (dp->d_name[0] == '.') {
651 if (dp->d_name[1] == '\0')
652 continue;
653 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
654 continue;
655 }
656 if (TSTINO(dp->d_ino, dumpinomap)) {
657 ret |= HASDUMPEDFILE;
658 if (ret & HASSUBDIRS)
659 break;
660 }
661 if (TSTINO(dp->d_ino, dumpdirmap)) {
662 ret |= HASSUBDIRS;
663 if (ret & HASDUMPEDFILE)
664 break;
665 }
666 }
667 return (ret);
668 }
669 #endif /* __linux__ */
670
671 #ifdef __linux__
672
673 struct block_context {
674 ino_t ino;
675 int *buf;
676 int cnt;
677 int max;
678 int next_block;
679 };
680
681 /*
682 * Dump a block to the tape
683 */
684 static int
685 dumponeblock(ext2_filsys fs, blk_t *blocknr, int blockcnt, void *private)
686 {
687 struct block_context *p;
688 int i;
689
690 if (blockcnt < NDADDR)
691 return 0;
692 p = (struct block_context *)private;
693 for (i = p->next_block; i < blockcnt; i++) {
694 p->buf[p->cnt++] = 0;
695 if (p->cnt == p->max) {
696 blksout (p->buf, p->cnt, p->ino);
697 p->cnt = 0;
698 }
699 }
700 p->buf[p->cnt++] = *blocknr;
701 if (p->cnt == p->max) {
702 blksout (p->buf, p->cnt, p->ino);
703 p->cnt = 0;
704 }
705 p->next_block = blockcnt + 1;
706 return 0;
707 }
708 #endif
709
710 /*
711 * Dump passes 3 and 4.
712 *
713 * Dump the contents of an inode to tape.
714 */
715 void
716 dumpino(struct dinode *dp, ino_t ino)
717 {
718 int cnt;
719 fsizeT size;
720 char buf[TP_BSIZE];
721 struct old_bsd_inode obi;
722 #ifdef __linux__
723 struct block_context bc;
724 #else
725 int ind_level;
726 #endif
727
728 if (newtape) {
729 newtape = 0;
730 dumpmap(dumpinomap, TS_BITS, ino);
731 }
732 CLRINO(ino, dumpinomap);
733 #ifdef __linux__
734 memset(&obi, 0, sizeof(obi));
735 obi.di_mode = dp->di_mode;
736 obi.di_uid = dp->di_uid;
737 obi.di_gid = dp->di_gid;
738 obi.di_qsize.v = (u_quad_t)dp->di_size;
739 obi.di_atime = dp->di_atime;
740 obi.di_mtime = dp->di_mtime;
741 obi.di_ctime = dp->di_ctime;
742 obi.di_nlink = dp->di_nlink;
743 obi.di_blocks = dp->di_blocks;
744 obi.di_flags = dp->di_flags;
745 obi.di_gen = dp->di_gen;
746 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
747 if (dp->di_file_acl || dp->di_dir_acl)
748 warn("ACLs in inode #%ld won't be dumped", (long)ino);
749 memmove(&spcl.c_dinode, &obi, sizeof(obi));
750 #else /* __linux__ */
751 spcl.c_dinode = *dp;
752 #endif /* __linux__ */
753 spcl.c_type = TS_INODE;
754 spcl.c_count = 0;
755 switch (dp->di_mode & S_IFMT) {
756
757 case 0:
758 /*
759 * Freed inode.
760 */
761 return;
762
763 #ifdef __linux__
764 case S_IFDIR:
765 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
766 return;
767 #endif
768
769 case S_IFLNK:
770 /*
771 * Check for short symbolic link.
772 */
773 #ifdef __linux__
774 if (dp->di_size > 0 &&
775 dp->di_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
776 spcl.c_addr[0] = 1;
777 spcl.c_count = 1;
778 writeheader(ino);
779 memmove(buf, dp->di_db, (u_long)dp->di_size);
780 buf[dp->di_size] = '\0';
781 writerec(buf, 0);
782 return;
783 }
784 #endif /* __linux__ */
785 #ifdef FS_44INODEFMT
786 if (dp->di_size > 0 &&
787 dp->di_size < sblock->fs_maxsymlinklen) {
788 spcl.c_addr[0] = 1;
789 spcl.c_count = 1;
790 writeheader(ino);
791 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
792 buf[dp->di_size] = '\0';
793 writerec(buf, 0);
794 return;
795 }
796 #endif
797 /* fall through */
798
799 #ifndef __linux__
800 case S_IFDIR:
801 #endif
802 case S_IFREG:
803 if (dp->di_size > 0)
804 break;
805 /* fall through */
806
807 case S_IFIFO:
808 case S_IFSOCK:
809 case S_IFCHR:
810 case S_IFBLK:
811 writeheader(ino);
812 return;
813
814 default:
815 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
816 return;
817 }
818 if (dp->di_size > NDADDR * sblock->fs_bsize)
819 #ifdef __linux__
820 cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super);
821 #else
822 cnt = NDADDR * sblock->fs_frag;
823 #endif
824 else
825 cnt = howmany(dp->di_size, sblock->fs_fsize);
826 blksout(&dp->di_db[0], cnt, ino);
827 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
828 return;
829 #ifdef __linux__
830 bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
831 bc.buf = (int *)malloc (bc.max * sizeof (int));
832 bc.cnt = 0;
833 bc.ino = ino;
834 bc.next_block = NDADDR;
835
836 ext2fs_block_iterate (fs, ino, 0, NULL, dumponeblock, (void *)&bc);
837 if (bc.cnt > 0) {
838 blksout (bc.buf, bc.cnt, bc.ino);
839 }
840 free(bc.buf);
841 #else
842 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
843 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
844 if (size <= 0)
845 return;
846 }
847 #endif
848 }
849
850 #ifdef __linux__
851
852 struct convert_dir_context {
853 char *buf;
854 int prev_offset;
855 int offset;
856 int bs;
857 };
858
859 /*
860 * This function converts an ext2fs directory entry to the BSD format.
861 *
862 * Basically, it adds a null-character at the end of the name, recomputes the
863 * size of the entry, and creates it in a temporary buffer
864 */
865 static int
866 convert_dir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
867 {
868 struct convert_dir_context *p;
869 struct olddirect *dp;
870 int reclen;
871
872 p = (struct convert_dir_context *)private;
873
874 reclen = EXT2_DIR_REC_LEN((dirent->name_len & 0xFF) + 1);
875 if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
876 dp = (struct olddirect *)(p->buf + p->prev_offset);
877 dp->d_reclen += p->bs - (p->offset % p->bs);
878 p->offset += p->bs - (p->offset % p->bs);
879 }
880
881 dp = (struct olddirect *)(p->buf + p->offset);
882 dp->d_ino = dirent->inode;
883 dp->d_reclen = reclen;
884 dp->d_namlen = dirent->name_len & 0xFF;
885 strncpy(dp->d_name, dirent->name, dp->d_namlen);
886 dp->d_name[dp->d_namlen] = '\0';
887 p->prev_offset = p->offset;
888 p->offset += reclen;
889
890 return 0;
891 }
892
893 /*
894 * Dump pass 3
895 *
896 * Dumps a directory to tape after converting it to the BSD format
897 */
898 void
899 dumpdirino(struct dinode *dp, ino_t ino)
900 {
901 fsizeT size;
902 char buf[TP_BSIZE];
903 struct old_bsd_inode obi;
904 struct convert_dir_context cdc;
905 errcode_t retval;
906 struct ext2_dir_entry *de;
907 fsizeT dir_size;
908
909 if (newtape) {
910 newtape = 0;
911 dumpmap(dumpinomap, TS_BITS, ino);
912 }
913 CLRINO(ino, dumpinomap);
914
915 /*
916 * Convert the directory to the BSD format
917 */
918 /* Allocate a buffer for the conversion (twice the size of the
919 ext2fs directory to avoid problems ;-) */
920 cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
921 if (cdc.buf == NULL)
922 err(1, "Cannot allocate buffer to convert directory #%lu\n",
923 (unsigned long)ino);
924 cdc.offset = 0;
925 cdc.prev_offset = 0;
926 cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
927 /* Do the conversion */
928 retval = ext2fs_dir_iterate(fs, ino, 0, NULL, convert_dir, (void *)&cdc);
929 if (retval) {
930 com_err(disk, retval, "while converting directory #%ld\n", (long)ino);
931 exit(X_ABORT);
932 }
933 /* Fix the last entry */
934 if ((cdc.offset % cdc.bs) != 0) {
935 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
936 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
937 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
938 }
939
940 dir_size = cdc.offset;
941
942 #ifdef __linux__
943 memset(&obi, 0, sizeof(obi));
944 obi.di_mode = dp->di_mode;
945 obi.di_uid = dp->di_uid;
946 obi.di_gid = dp->di_gid;
947 obi.di_qsize.v = dir_size; /* (u_quad_t)dp->di_size; */
948 obi.di_atime = dp->di_atime;
949 obi.di_mtime = dp->di_mtime;
950 obi.di_ctime = dp->di_ctime;
951 obi.di_nlink = dp->di_nlink;
952 obi.di_blocks = dp->di_blocks;
953 obi.di_flags = dp->di_flags;
954 obi.di_gen = dp->di_gen;
955 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
956 if (dp->di_file_acl || dp->di_dir_acl)
957 warn("ACLs in inode #%ld won't be dumped", (long)ino);
958 memmove(&spcl.c_dinode, &obi, sizeof(obi));
959 #else /* __linux__ */
960 spcl.c_dinode = *dp;
961 #endif /* __linux__ */
962 spcl.c_type = TS_INODE;
963 spcl.c_count = 0;
964 switch (dp->di_mode & S_IFMT) {
965
966 case 0:
967 /*
968 * Freed inode.
969 */
970 return;
971
972 case S_IFDIR:
973 if (dir_size > 0)
974 break;
975 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
976 ino, dir_size);
977 return;
978
979 default:
980 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
981 dp->di_mode & IFMT, ino);
982 return;
983 }
984 for (size = 0; size < dir_size; size += TP_BSIZE) {
985 spcl.c_addr[0] = 1;
986 spcl.c_count = 1;
987 writeheader(ino);
988 memmove(buf, cdc.buf + size, TP_BSIZE);
989 writerec(buf, 0);
990 spcl.c_type = TS_ADDR;
991 }
992
993 (void)free(cdc.buf);
994 }
995 #endif /* __linux__ */
996
997 #ifndef __linux__
998 /*
999 * Read indirect blocks, and pass the data blocks to be dumped.
1000 */
1001 static void
1002 dmpindir(ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
1003 {
1004 int i, cnt;
1005 #ifdef __linux__
1006 int max;
1007 blk_t *swapme;
1008 #endif
1009 daddr_t idblk[MAXNINDIR];
1010
1011 if (blk != 0) {
1012 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
1013 #ifdef __linux__
1014 /*
1015 * My RedHat 4.0 system doesn't have these flags; I haven't
1016 * upgraded e2fsprogs yet
1017 */
1018 #if defined(EXT2_FLAG_SWAP_BYTES)
1019 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
1020 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) {
1021 #endif
1022 max = sblock->fs_bsize >> 2;
1023 swapme = (blk_t *) idblk;
1024 for (i = 0; i < max; i++, swapme++)
1025 *swapme = swab32(*swapme);
1026 #if defined(EXT2_FLAG_SWAP_BYTES)
1027 }
1028 #endif
1029 #endif
1030 else
1031 memset(idblk, 0, (int)sblock->fs_bsize);
1032 if (ind_level <= 0) {
1033 if (*size < NINDIR(sblock) * sblock->fs_bsize)
1034 cnt = howmany(*size, sblock->fs_fsize);
1035 else
1036 #ifdef __linux__
1037 cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
1038 #else
1039 cnt = NINDIR(sblock) * sblock->fs_frag;
1040 #endif
1041 *size -= NINDIR(sblock) * sblock->fs_bsize;
1042 blksout(&idblk[0], cnt, ino);
1043 return;
1044 }
1045 ind_level--;
1046 for (i = 0; i < NINDIR(sblock); i++) {
1047 dmpindir(ino, idblk[i], ind_level, size);
1048 if (*size <= 0)
1049 return;
1050 }
1051 }
1052 #endif
1053
1054 /*
1055 * Collect up the data into tape record sized buffers and output them.
1056 */
1057 void
1058 blksout(daddr_t *blkp, int frags, ino_t ino)
1059 {
1060 register daddr_t *bp;
1061 int i, j, count, blks, tbperdb;
1062
1063 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
1064 tbperdb = sblock->fs_bsize >> tp_bshift;
1065 for (i = 0; i < blks; i += TP_NINDIR) {
1066 if (i + TP_NINDIR > blks)
1067 count = blks;
1068 else
1069 count = i + TP_NINDIR;
1070 for (j = i; j < count; j++)
1071 if (blkp[j / tbperdb] != 0)
1072 spcl.c_addr[j - i] = 1;
1073 else
1074 spcl.c_addr[j - i] = 0;
1075 spcl.c_count = count - i;
1076 writeheader(ino);
1077 bp = &blkp[i / tbperdb];
1078 for (j = i; j < count; j += tbperdb, bp++) {
1079 if (*bp != 0) {
1080 if (j + tbperdb <= count)
1081 dumpblock(*bp, (int)sblock->fs_bsize);
1082 else
1083 dumpblock(*bp, (count - j) * TP_BSIZE);
1084 }
1085 }
1086 spcl.c_type = TS_ADDR;
1087 }
1088 }
1089
1090 /*
1091 * Dump a map to the tape.
1092 */
1093 void
1094 dumpmap(char *map, int type, ino_t ino)
1095 {
1096 register int i;
1097 char *cp;
1098
1099 spcl.c_type = type;
1100 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
1101 writeheader(ino);
1102 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
1103 writerec(cp, 0);
1104 }
1105
1106 /*
1107 * Write a header record to the dump tape.
1108 */
1109 void
1110 writeheader(ino_t ino)
1111 {
1112 #ifdef __linux__
1113 register __s32 sum, cnt, *lp;
1114 #else
1115 register int32_t sum, cnt, *lp;
1116 #endif
1117
1118 spcl.c_inumber = ino;
1119 spcl.c_magic = NFS_MAGIC;
1120 spcl.c_checksum = 0;
1121 #ifdef __linux__
1122 lp = (__s32 *)&spcl;
1123 #else
1124 lp = (int32_t *)&spcl;
1125 #endif
1126 sum = 0;
1127 #ifdef __linux__
1128 cnt = sizeof(union u_spcl) / (4 * sizeof(__s32));
1129 #else
1130 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
1131 #endif
1132 while (--cnt >= 0) {
1133 sum += *lp++;
1134 sum += *lp++;
1135 sum += *lp++;
1136 sum += *lp++;
1137 }
1138 spcl.c_checksum = CHECKSUM - sum;
1139 writerec((char *)&spcl, 1);
1140 }
1141
1142 #ifdef __linux__
1143 struct dinode *
1144 getino(ino_t inum)
1145 {
1146 static struct dinode dinode;
1147 errcode_t err;
1148
1149 curino = inum;
1150 err = ext2fs_read_inode(fs, inum, (struct ext2_inode *) &dinode);
1151 if (err) {
1152 com_err(disk, err, "while reading inode #%ld\n", (long)inum);
1153 exit(X_ABORT);
1154 }
1155 return &dinode;
1156 }
1157 #else /* __linux__ */
1158 struct dinode *
1159 getino(ino_t inum)
1160 {
1161 static daddr_t minino, maxino;
1162 static struct dinode inoblock[MAXINOPB];
1163
1164 curino = inum;
1165 if (inum >= minino && inum < maxino)
1166 return (&inoblock[inum - minino]);
1167 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
1168 (int)sblock->fs_bsize);
1169 minino = inum - (inum % INOPB(sblock));
1170 maxino = minino + INOPB(sblock);
1171 return (&inoblock[inum - minino]);
1172 }
1173 #endif /* __linux__ */
1174
1175 /*
1176 * Read a chunk of data from the disk.
1177 * Try to recover from hard errors by reading in sector sized pieces.
1178 * Error recovery is attempted at most BREADEMAX times before seeking
1179 * consent from the operator to continue.
1180 */
1181 int breaderrors = 0;
1182 #define BREADEMAX 32
1183
1184 void
1185 bread(daddr_t blkno, char *buf, int size)
1186 {
1187 int cnt, i;
1188 extern int errno;
1189
1190 loop:
1191 #ifdef __linux__
1192 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1193 (((ext2_loff_t)blkno) << dev_bshift))
1194 #else
1195 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1196 ((off_t)blkno << dev_bshift))
1197 #endif
1198 msg("bread: lseek fails\n");
1199 if ((cnt = read(diskfd, buf, size)) == size)
1200 return;
1201 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1202 /*
1203 * Trying to read the final fragment.
1204 *
1205 * NB - dump only works in TP_BSIZE blocks, hence
1206 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1207 * It should be smarter about not actually trying to
1208 * read more than it can get, but for the time being
1209 * we punt and scale back the read only when it gets
1210 * us into trouble. (mkm 9/25/83)
1211 */
1212 size -= dev_bsize;
1213 goto loop;
1214 }
1215 if (cnt == -1)
1216 msg("read error from %s: %s: [block %d]: count=%d\n",
1217 disk, strerror(errno), blkno, size);
1218 else
1219 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1220 disk, blkno, size, cnt);
1221 if (++breaderrors > BREADEMAX) {
1222 msg("More than %d block read errors from %d\n",
1223 BREADEMAX, disk);
1224 broadcast("DUMP IS AILING!\n");
1225 msg("This is an unrecoverable error.\n");
1226 if (!query("Do you want to attempt to continue?")){
1227 dumpabort(0);
1228 /*NOTREACHED*/
1229 } else
1230 breaderrors = 0;
1231 }
1232 /*
1233 * Zero buffer, then try to read each sector of buffer separately.
1234 */
1235 memset(buf, 0, size);
1236 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1237 #ifdef __linux__
1238 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1239 (((ext2_loff_t)blkno) << dev_bshift))
1240 #else
1241 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1242 ((off_t)blkno << dev_bshift))
1243 #endif
1244 msg("bread: lseek2 fails!\n");
1245 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1246 continue;
1247 if (cnt == -1) {
1248 msg("read error from %s: %s: [sector %d]: count=%d\n",
1249 disk, strerror(errno), blkno, dev_bsize);
1250 continue;
1251 }
1252 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1253 disk, blkno, dev_bsize, cnt);
1254 }
1255 }