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