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