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