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