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