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