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