1 /*-
2 * Copyright (c) 1980, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)traverse.c 8.7 (Berkeley) 6/15/95";
33 #endif
34 static const char rcsid[] =
35 "$FreeBSD$";
36 #endif /* not lint */
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40
41 #include <ufs/ufs/dir.h>
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ffs/fs.h>
44
45 #include <protocols/dumprestore.h>
46
47 #include <ctype.h>
48 #include <errno.h>
49 #include <inttypes.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <timeconv.h>
55 #include <unistd.h>
56
57 #include "dump.h"
58
59 union dinode {
60 struct ufs1_dinode dp1;
61 struct ufs2_dinode dp2;
62 };
63 #define DIP(dp, field) \
64 ((sblock->fs_magic == FS_UFS1_MAGIC) ? \
65 (dp)->dp1.field : (dp)->dp2.field)
66 #define DIP_SET(dp, field, val) do {\
67 if (sblock->fs_magic == FS_UFS1_MAGIC) \
68 (dp)->dp1.field = (val); \
69 else \
70 (dp)->dp2.field = (val); \
71 } while (0)
72
73 #define HASDUMPEDFILE 0x1
74 #define HASSUBDIRS 0x2
75
76 static int dirindir(ino_t ino, ufs2_daddr_t blkno, int level, long *size,
77 long *tapesize, int nodump, ino_t maxino);
78 static void dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int level,
79 off_t *size);
80 static void ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino);
81 static void ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags,
82 ino_t ino, int last);
83 static int appendextdata(union dinode *dp);
84 static void writeextdata(union dinode *dp, ino_t ino, int added);
85 static int searchdir(ino_t ino, ufs2_daddr_t blkno, long size, long filesize,
86 long *tapesize, int nodump, ino_t maxino);
87 static long blockest(union dinode *dp);
88
89 /*
90 * This is an estimation of the number of TP_BSIZE blocks in the file.
91 * It estimates the number of blocks in files with holes by assuming
92 * that all of the blocks accounted for by di_blocks are data blocks
93 * (when some of the blocks are usually used for indirect pointers);
94 * hence the estimate may be high.
95 */
96 static long
blockest(union dinode * dp)97 blockest(union dinode *dp)
98 {
99 long blkest, sizeest;
100
101 /*
102 * dp->di_size is the size of the file in bytes.
103 * dp->di_blocks stores the number of sectors actually in the file.
104 * If there are more sectors than the size would indicate, this just
105 * means that there are indirect blocks in the file or unused
106 * sectors in the last file block; we can safely ignore these
107 * (blkest = sizeest below).
108 * If the file is bigger than the number of sectors would indicate,
109 * then the file has holes in it. In this case we must use the
110 * block count to estimate the number of data blocks used, but
111 * we use the actual size for estimating the number of indirect
112 * dump blocks (sizeest vs. blkest in the indirect block
113 * calculation).
114 */
115 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0)
116 return (1);
117 blkest = howmany(dbtob(DIP(dp, di_blocks)), TP_BSIZE);
118 sizeest = howmany(DIP(dp, di_size), TP_BSIZE);
119 if (blkest > sizeest)
120 blkest = sizeest;
121 if (DIP(dp, di_size) > sblock->fs_bsize * NDADDR) {
122 /* calculate the number of indirect blocks on the dump tape */
123 blkest +=
124 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
125 TP_NINDIR);
126 }
127 return (blkest + 1);
128 }
129
130 /* Auxiliary macro to pick up files changed since previous dump. */
131 #define CHANGEDSINCE(dp, t) \
132 (DIP(dp, di_mtime) >= (t) || DIP(dp, di_ctime) >= (t))
133
134 /* The WANTTODUMP macro decides whether a file should be dumped. */
135 #ifdef UF_NODUMP
136 #define WANTTODUMP(dp) \
137 (CHANGEDSINCE(dp, spcl.c_ddate) && \
138 (nonodump || (DIP(dp, di_flags) & UF_NODUMP) != UF_NODUMP))
139 #else
140 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
141 #endif
142
143 /*
144 * Dump pass 1.
145 *
146 * Walk the inode list for a file system to find all allocated inodes
147 * that have been modified since the previous dump time. Also, find all
148 * the directories in the file system.
149 */
150 int
mapfiles(ino_t maxino,long * tapesize)151 mapfiles(ino_t maxino, long *tapesize)
152 {
153 int i, cg, mode, inosused;
154 int anydirskipped = 0;
155 union dinode *dp;
156 struct cg *cgp;
157 ino_t ino;
158 u_char *cp;
159
160 if ((cgp = malloc(sblock->fs_cgsize)) == NULL)
161 quit("mapfiles: cannot allocate memory.\n");
162 for (cg = 0; cg < sblock->fs_ncg; cg++) {
163 ino = cg * sblock->fs_ipg;
164 bread(fsbtodb(sblock, cgtod(sblock, cg)), (char *)cgp,
165 sblock->fs_cgsize);
166 if (sblock->fs_magic == FS_UFS2_MAGIC)
167 inosused = cgp->cg_initediblk;
168 else
169 inosused = sblock->fs_ipg;
170 /*
171 * If we are using soft updates, then we can trust the
172 * cylinder group inode allocation maps to tell us which
173 * inodes are allocated. We will scan the used inode map
174 * to find the inodes that are really in use, and then
175 * read only those inodes in from disk.
176 */
177 if (sblock->fs_flags & FS_DOSOFTDEP) {
178 if (!cg_chkmagic(cgp))
179 quit("mapfiles: cg %d: bad magic number\n", cg);
180 cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
181 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
182 if (*cp == 0)
183 continue;
184 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
185 if (*cp & i)
186 break;
187 inosused--;
188 }
189 break;
190 }
191 if (inosused <= 0)
192 continue;
193 }
194 for (i = 0; i < inosused; i++, ino++) {
195 if (ino < ROOTINO ||
196 (dp = getino(ino, &mode)) == NULL ||
197 (mode & IFMT) == 0)
198 continue;
199 if (ino >= maxino) {
200 msg("Skipping inode %ju >= maxino %ju\n",
201 (uintmax_t)ino, (uintmax_t)maxino);
202 continue;
203 }
204 /*
205 * Everything must go in usedinomap so that a check
206 * for "in dumpdirmap but not in usedinomap" to detect
207 * dirs with nodump set has a chance of succeeding
208 * (this is used in mapdirs()).
209 */
210 SETINO(ino, usedinomap);
211 if (mode == IFDIR)
212 SETINO(ino, dumpdirmap);
213 if (WANTTODUMP(dp)) {
214 SETINO(ino, dumpinomap);
215 if (mode != IFREG &&
216 mode != IFDIR &&
217 mode != IFLNK)
218 *tapesize += 1;
219 else
220 *tapesize += blockest(dp);
221 continue;
222 }
223 if (mode == IFDIR) {
224 if (!nonodump &&
225 (DIP(dp, di_flags) & UF_NODUMP))
226 CLRINO(ino, usedinomap);
227 anydirskipped = 1;
228 }
229 }
230 }
231 /*
232 * Restore gets very upset if the root is not dumped,
233 * so ensure that it always is dumped.
234 */
235 SETINO(ROOTINO, dumpinomap);
236 return (anydirskipped);
237 }
238
239 /*
240 * Dump pass 2.
241 *
242 * Scan each directory on the file system to see if it has any modified
243 * files in it. If it does, and has not already been added to the dump
244 * list (because it was itself modified), then add it. If a directory
245 * has not been modified itself, contains no modified files and has no
246 * subdirectories, then it can be deleted from the dump list and from
247 * the list of directories. By deleting it from the list of directories,
248 * its parent may now qualify for the same treatment on this or a later
249 * pass using this algorithm.
250 */
251 int
mapdirs(ino_t maxino,long * tapesize)252 mapdirs(ino_t maxino, long *tapesize)
253 {
254 union dinode *dp;
255 int i, isdir, nodump;
256 char *map;
257 ino_t ino;
258 union dinode di;
259 long filesize;
260 int ret, change = 0;
261
262 isdir = 0; /* XXX just to get gcc to shut up */
263 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
264 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */
265 isdir = *map++;
266 else
267 isdir >>= 1;
268 /*
269 * If a directory has been removed from usedinomap, it
270 * either has the nodump flag set, or has inherited
271 * it. Although a directory can't be in dumpinomap if
272 * it isn't in usedinomap, we have to go through it to
273 * propagate the nodump flag.
274 */
275 nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
276 if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
277 continue;
278 dp = getino(ino, &i);
279 /*
280 * inode buf may change in searchdir().
281 */
282 if (sblock->fs_magic == FS_UFS1_MAGIC)
283 di.dp1 = dp->dp1;
284 else
285 di.dp2 = dp->dp2;
286 filesize = DIP(&di, di_size);
287 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
288 if (DIP(&di, di_db[i]) != 0)
289 ret |= searchdir(ino, DIP(&di, di_db[i]),
290 (long)sblksize(sblock, DIP(&di, di_size),
291 i), filesize, tapesize, nodump, maxino);
292 if (ret & HASDUMPEDFILE)
293 filesize = 0;
294 else
295 filesize -= sblock->fs_bsize;
296 }
297 for (i = 0; filesize > 0 && i < NIADDR; i++) {
298 if (DIP(&di, di_ib[i]) == 0)
299 continue;
300 ret |= dirindir(ino, DIP(&di, di_ib[i]), i, &filesize,
301 tapesize, nodump, maxino);
302 }
303 if (ret & HASDUMPEDFILE) {
304 SETINO(ino, dumpinomap);
305 *tapesize += blockest(&di);
306 change = 1;
307 continue;
308 }
309 if (nodump) {
310 if (ret & HASSUBDIRS)
311 change = 1; /* subdirs inherit nodump */
312 CLRINO(ino, dumpdirmap);
313 } else if ((ret & HASSUBDIRS) == 0)
314 if (!TSTINO(ino, dumpinomap)) {
315 CLRINO(ino, dumpdirmap);
316 change = 1;
317 }
318 }
319 return (change);
320 }
321
322 /*
323 * Read indirect blocks, and pass the data blocks to be searched
324 * as directories. Quit as soon as any entry is found that will
325 * require the directory to be dumped.
326 */
327 static int
dirindir(ino_t ino,ufs2_daddr_t blkno,int ind_level,long * filesize,long * tapesize,int nodump,ino_t maxino)328 dirindir(
329 ino_t ino,
330 ufs2_daddr_t blkno,
331 int ind_level,
332 long *filesize,
333 long *tapesize,
334 int nodump,
335 ino_t maxino)
336 {
337 union {
338 ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)];
339 ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)];
340 } idblk;
341 int ret = 0;
342 int i;
343
344 bread(fsbtodb(sblock, blkno), (char *)&idblk, (int)sblock->fs_bsize);
345 if (ind_level <= 0) {
346 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
347 if (sblock->fs_magic == FS_UFS1_MAGIC)
348 blkno = idblk.ufs1[i];
349 else
350 blkno = idblk.ufs2[i];
351 if (blkno != 0)
352 ret |= searchdir(ino, blkno, sblock->fs_bsize,
353 *filesize, tapesize, nodump, maxino);
354 if (ret & HASDUMPEDFILE)
355 *filesize = 0;
356 else
357 *filesize -= sblock->fs_bsize;
358 }
359 return (ret);
360 }
361 ind_level--;
362 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
363 if (sblock->fs_magic == FS_UFS1_MAGIC)
364 blkno = idblk.ufs1[i];
365 else
366 blkno = idblk.ufs2[i];
367 if (blkno != 0)
368 ret |= dirindir(ino, blkno, ind_level, filesize,
369 tapesize, nodump, maxino);
370 }
371 return (ret);
372 }
373
374 /*
375 * Scan a disk block containing directory information looking to see if
376 * any of the entries are on the dump list and to see if the directory
377 * contains any subdirectories.
378 */
379 static int
searchdir(ino_t ino,ufs2_daddr_t blkno,long size,long filesize,long * tapesize,int nodump,ino_t maxino)380 searchdir(
381 ino_t ino,
382 ufs2_daddr_t blkno,
383 long size,
384 long filesize,
385 long *tapesize,
386 int nodump,
387 ino_t maxino)
388 {
389 int mode;
390 struct direct *dp;
391 union dinode *ip;
392 long loc, ret = 0;
393 static caddr_t dblk;
394
395 if (dblk == NULL && (dblk = malloc(sblock->fs_bsize)) == NULL)
396 quit("searchdir: cannot allocate indirect memory.\n");
397 bread(fsbtodb(sblock, blkno), dblk, (int)size);
398 if (filesize < size)
399 size = filesize;
400 for (loc = 0; loc < size; ) {
401 dp = (struct direct *)(dblk + loc);
402 if (dp->d_reclen == 0) {
403 msg("corrupted directory, inumber %ju\n",
404 (uintmax_t)ino);
405 break;
406 }
407 loc += dp->d_reclen;
408 if (dp->d_ino == 0)
409 continue;
410 if (dp->d_ino >= maxino) {
411 msg("corrupted directory entry, d_ino %ju >= %ju\n",
412 (uintmax_t)dp->d_ino, (uintmax_t)maxino);
413 break;
414 }
415 if (dp->d_name[0] == '.') {
416 if (dp->d_name[1] == '\0')
417 continue;
418 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
419 continue;
420 }
421 if (nodump) {
422 ip = getino(dp->d_ino, &mode);
423 if (TSTINO(dp->d_ino, dumpinomap)) {
424 CLRINO(dp->d_ino, dumpinomap);
425 *tapesize -= blockest(ip);
426 }
427 /*
428 * Add back to dumpdirmap and remove from usedinomap
429 * to propagate nodump.
430 */
431 if (mode == IFDIR) {
432 SETINO(dp->d_ino, dumpdirmap);
433 CLRINO(dp->d_ino, usedinomap);
434 ret |= HASSUBDIRS;
435 }
436 } else {
437 if (TSTINO(dp->d_ino, dumpinomap)) {
438 ret |= HASDUMPEDFILE;
439 if (ret & HASSUBDIRS)
440 break;
441 }
442 if (TSTINO(dp->d_ino, dumpdirmap)) {
443 ret |= HASSUBDIRS;
444 if (ret & HASDUMPEDFILE)
445 break;
446 }
447 }
448 }
449 return (ret);
450 }
451
452 /*
453 * Dump passes 3 and 4.
454 *
455 * Dump the contents of an inode to tape.
456 */
457 void
dumpino(union dinode * dp,ino_t ino)458 dumpino(union dinode *dp, ino_t ino)
459 {
460 int ind_level, cnt, last, added;
461 off_t size;
462 char buf[TP_BSIZE];
463
464 if (newtape) {
465 newtape = 0;
466 dumpmap(dumpinomap, TS_BITS, ino);
467 }
468 CLRINO(ino, dumpinomap);
469 /*
470 * Zero out the size of a snapshot so that it will be dumped
471 * as a zero length file.
472 */
473 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) {
474 DIP_SET(dp, di_size, 0);
475 DIP_SET(dp, di_flags, DIP(dp, di_flags) & ~SF_SNAPSHOT);
476 }
477 if (sblock->fs_magic == FS_UFS1_MAGIC) {
478 spcl.c_mode = dp->dp1.di_mode;
479 spcl.c_size = dp->dp1.di_size;
480 spcl.c_extsize = 0;
481 spcl.c_atime = _time32_to_time(dp->dp1.di_atime);
482 spcl.c_atimensec = dp->dp1.di_atimensec;
483 spcl.c_mtime = _time32_to_time(dp->dp1.di_mtime);
484 spcl.c_mtimensec = dp->dp1.di_mtimensec;
485 spcl.c_birthtime = 0;
486 spcl.c_birthtimensec = 0;
487 spcl.c_rdev = dp->dp1.di_rdev;
488 spcl.c_file_flags = dp->dp1.di_flags;
489 spcl.c_uid = dp->dp1.di_uid;
490 spcl.c_gid = dp->dp1.di_gid;
491 } else {
492 spcl.c_mode = dp->dp2.di_mode;
493 spcl.c_size = dp->dp2.di_size;
494 spcl.c_extsize = dp->dp2.di_extsize;
495 spcl.c_atime = _time64_to_time(dp->dp2.di_atime);
496 spcl.c_atimensec = dp->dp2.di_atimensec;
497 spcl.c_mtime = _time64_to_time(dp->dp2.di_mtime);
498 spcl.c_mtimensec = dp->dp2.di_mtimensec;
499 spcl.c_birthtime = _time64_to_time(dp->dp2.di_birthtime);
500 spcl.c_birthtimensec = dp->dp2.di_birthnsec;
501 spcl.c_rdev = dp->dp2.di_rdev;
502 spcl.c_file_flags = dp->dp2.di_flags;
503 spcl.c_uid = dp->dp2.di_uid;
504 spcl.c_gid = dp->dp2.di_gid;
505 }
506 spcl.c_type = TS_INODE;
507 spcl.c_count = 0;
508 switch (DIP(dp, di_mode) & S_IFMT) {
509
510 case 0:
511 /*
512 * Freed inode.
513 */
514 return;
515
516 case S_IFLNK:
517 /*
518 * Check for short symbolic link.
519 */
520 if (DIP(dp, di_size) > 0 &&
521 DIP(dp, di_size) < sblock->fs_maxsymlinklen) {
522 spcl.c_addr[0] = 1;
523 spcl.c_count = 1;
524 added = appendextdata(dp);
525 writeheader(ino);
526 if (sblock->fs_magic == FS_UFS1_MAGIC)
527 memmove(buf, (caddr_t)dp->dp1.di_db,
528 (u_long)DIP(dp, di_size));
529 else
530 memmove(buf, (caddr_t)dp->dp2.di_db,
531 (u_long)DIP(dp, di_size));
532 buf[DIP(dp, di_size)] = '\0';
533 writerec(buf, 0);
534 writeextdata(dp, ino, added);
535 return;
536 }
537 /* FALLTHROUGH */
538
539 case S_IFDIR:
540 case S_IFREG:
541 if (DIP(dp, di_size) > 0)
542 break;
543 /* FALLTHROUGH */
544
545 case S_IFIFO:
546 case S_IFSOCK:
547 case S_IFCHR:
548 case S_IFBLK:
549 added = appendextdata(dp);
550 writeheader(ino);
551 writeextdata(dp, ino, added);
552 return;
553
554 default:
555 msg("Warning: undefined file type 0%o\n",
556 DIP(dp, di_mode) & IFMT);
557 return;
558 }
559 if (DIP(dp, di_size) > NDADDR * sblock->fs_bsize) {
560 cnt = NDADDR * sblock->fs_frag;
561 last = 0;
562 } else {
563 cnt = howmany(DIP(dp, di_size), sblock->fs_fsize);
564 last = 1;
565 }
566 if (sblock->fs_magic == FS_UFS1_MAGIC)
567 ufs1_blksout(&dp->dp1.di_db[0], cnt, ino);
568 else
569 ufs2_blksout(dp, &dp->dp2.di_db[0], cnt, ino, last);
570 if ((size = DIP(dp, di_size) - NDADDR * sblock->fs_bsize) <= 0)
571 return;
572 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
573 dmpindir(dp, ino, DIP(dp, di_ib[ind_level]), ind_level, &size);
574 if (size <= 0)
575 return;
576 }
577 }
578
579 /*
580 * Read indirect blocks, and pass the data blocks to be dumped.
581 */
582 static void
dmpindir(union dinode * dp,ino_t ino,ufs2_daddr_t blk,int ind_level,off_t * size)583 dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int ind_level,
584 off_t *size)
585 {
586 union {
587 ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)];
588 ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)];
589 } idblk;
590 int i, cnt, last;
591
592 if (blk != 0)
593 bread(fsbtodb(sblock, blk), (char *)&idblk,
594 (int)sblock->fs_bsize);
595 else
596 memset(&idblk, 0, sblock->fs_bsize);
597 if (ind_level <= 0) {
598 if (*size > NINDIR(sblock) * sblock->fs_bsize) {
599 cnt = NINDIR(sblock) * sblock->fs_frag;
600 last = 0;
601 } else {
602 cnt = howmany(*size, sblock->fs_fsize);
603 last = 1;
604 }
605 *size -= NINDIR(sblock) * sblock->fs_bsize;
606 if (sblock->fs_magic == FS_UFS1_MAGIC)
607 ufs1_blksout(idblk.ufs1, cnt, ino);
608 else
609 ufs2_blksout(dp, idblk.ufs2, cnt, ino, last);
610 return;
611 }
612 ind_level--;
613 for (i = 0; i < NINDIR(sblock); i++) {
614 if (sblock->fs_magic == FS_UFS1_MAGIC)
615 dmpindir(dp, ino, idblk.ufs1[i], ind_level, size);
616 else
617 dmpindir(dp, ino, idblk.ufs2[i], ind_level, size);
618 if (*size <= 0)
619 return;
620 }
621 }
622
623 /*
624 * Collect up the data into tape record sized buffers and output them.
625 */
626 static void
ufs1_blksout(ufs1_daddr_t * blkp,int frags,ino_t ino)627 ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino)
628 {
629 ufs1_daddr_t *bp;
630 int i, j, count, blks, tbperdb;
631
632 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
633 tbperdb = sblock->fs_bsize >> tp_bshift;
634 for (i = 0; i < blks; i += TP_NINDIR) {
635 if (i + TP_NINDIR > blks)
636 count = blks;
637 else
638 count = i + TP_NINDIR;
639 for (j = i; j < count; j++)
640 if (blkp[j / tbperdb] != 0)
641 spcl.c_addr[j - i] = 1;
642 else
643 spcl.c_addr[j - i] = 0;
644 spcl.c_count = count - i;
645 writeheader(ino);
646 bp = &blkp[i / tbperdb];
647 for (j = i; j < count; j += tbperdb, bp++)
648 if (*bp != 0) {
649 if (j + tbperdb <= count)
650 dumpblock(*bp, (int)sblock->fs_bsize);
651 else
652 dumpblock(*bp, (count - j) * TP_BSIZE);
653 }
654 spcl.c_type = TS_ADDR;
655 }
656 }
657
658 /*
659 * Collect up the data into tape record sized buffers and output them.
660 */
661 static void
ufs2_blksout(union dinode * dp,ufs2_daddr_t * blkp,int frags,ino_t ino,int last)662 ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags, ino_t ino,
663 int last)
664 {
665 ufs2_daddr_t *bp;
666 int i, j, count, resid, blks, tbperdb, added;
667 static int writingextdata = 0;
668
669 /*
670 * Calculate the number of TP_BSIZE blocks to be dumped.
671 * For filesystems with a fragment size bigger than TP_BSIZE,
672 * only part of the final fragment may need to be dumped.
673 */
674 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
675 if (last) {
676 if (writingextdata)
677 resid = howmany(fragoff(sblock, spcl.c_extsize),
678 TP_BSIZE);
679 else
680 resid = howmany(fragoff(sblock, dp->dp2.di_size),
681 TP_BSIZE);
682 if (resid > 0)
683 blks -= howmany(sblock->fs_fsize, TP_BSIZE) - resid;
684 }
685 tbperdb = sblock->fs_bsize >> tp_bshift;
686 for (i = 0; i < blks; i += TP_NINDIR) {
687 if (i + TP_NINDIR > blks)
688 count = blks;
689 else
690 count = i + TP_NINDIR;
691 for (j = i; j < count; j++)
692 if (blkp[j / tbperdb] != 0)
693 spcl.c_addr[j - i] = 1;
694 else
695 spcl.c_addr[j - i] = 0;
696 spcl.c_count = count - i;
697 if (last && count == blks && !writingextdata)
698 added = appendextdata(dp);
699 writeheader(ino);
700 bp = &blkp[i / tbperdb];
701 for (j = i; j < count; j += tbperdb, bp++)
702 if (*bp != 0) {
703 if (j + tbperdb <= count)
704 dumpblock(*bp, (int)sblock->fs_bsize);
705 else
706 dumpblock(*bp, (count - j) * TP_BSIZE);
707 }
708 spcl.c_type = TS_ADDR;
709 spcl.c_count = 0;
710 if (last && count == blks && !writingextdata) {
711 writingextdata = 1;
712 writeextdata(dp, ino, added);
713 writingextdata = 0;
714 }
715 }
716 }
717
718 /*
719 * If there is room in the current block for the extended attributes
720 * as well as the file data, update the header to reflect the added
721 * attribute data at the end. Attributes are placed at the end so that
722 * old versions of restore will correctly restore the file and simply
723 * discard the extra data at the end that it does not understand.
724 * The attribute data is dumped following the file data by the
725 * writeextdata() function (below).
726 */
727 static int
appendextdata(union dinode * dp)728 appendextdata(union dinode *dp)
729 {
730 int i, blks, tbperdb;
731
732 /*
733 * If no extended attributes, there is nothing to do.
734 */
735 if (spcl.c_extsize == 0)
736 return (0);
737 /*
738 * If there is not enough room at the end of this block
739 * to add the extended attributes, then rather than putting
740 * part of them here, we simply push them entirely into a
741 * new block rather than putting some here and some later.
742 */
743 if (spcl.c_extsize > NXADDR * sblock->fs_bsize)
744 blks = howmany(NXADDR * sblock->fs_bsize, TP_BSIZE);
745 else
746 blks = howmany(spcl.c_extsize, TP_BSIZE);
747 if (spcl.c_count + blks > TP_NINDIR)
748 return (0);
749 /*
750 * Update the block map in the header to indicate the added
751 * extended attribute. They will be appended after the file
752 * data by the writeextdata() routine.
753 */
754 tbperdb = sblock->fs_bsize >> tp_bshift;
755 for (i = 0; i < blks; i++)
756 if (&dp->dp2.di_extb[i / tbperdb] != 0)
757 spcl.c_addr[spcl.c_count + i] = 1;
758 else
759 spcl.c_addr[spcl.c_count + i] = 0;
760 spcl.c_count += blks;
761 return (blks);
762 }
763
764 /*
765 * Dump the extended attribute data. If there was room in the file
766 * header, then all we need to do is output the data blocks. If there
767 * was not room in the file header, then an additional TS_ADDR header
768 * is created to hold the attribute data.
769 */
770 static void
writeextdata(union dinode * dp,ino_t ino,int added)771 writeextdata(union dinode *dp, ino_t ino, int added)
772 {
773 int i, frags, blks, tbperdb, last;
774 ufs2_daddr_t *bp;
775 off_t size;
776
777 /*
778 * If no extended attributes, there is nothing to do.
779 */
780 if (spcl.c_extsize == 0)
781 return;
782 /*
783 * If there was no room in the file block for the attributes,
784 * dump them out in a new block, otherwise just dump the data.
785 */
786 if (added == 0) {
787 if (spcl.c_extsize > NXADDR * sblock->fs_bsize) {
788 frags = NXADDR * sblock->fs_frag;
789 last = 0;
790 } else {
791 frags = howmany(spcl.c_extsize, sblock->fs_fsize);
792 last = 1;
793 }
794 ufs2_blksout(dp, &dp->dp2.di_extb[0], frags, ino, last);
795 } else {
796 if (spcl.c_extsize > NXADDR * sblock->fs_bsize)
797 blks = howmany(NXADDR * sblock->fs_bsize, TP_BSIZE);
798 else
799 blks = howmany(spcl.c_extsize, TP_BSIZE);
800 tbperdb = sblock->fs_bsize >> tp_bshift;
801 for (i = 0; i < blks; i += tbperdb) {
802 bp = &dp->dp2.di_extb[i / tbperdb];
803 if (*bp != 0) {
804 if (i + tbperdb <= blks)
805 dumpblock(*bp, (int)sblock->fs_bsize);
806 else
807 dumpblock(*bp, (blks - i) * TP_BSIZE);
808 }
809 }
810
811 }
812 /*
813 * If an indirect block is added for extended attributes, then
814 * di_exti below should be changed to the structure element
815 * that references the extended attribute indirect block. This
816 * definition is here only to make it compile without complaint.
817 */
818 #define di_exti di_spare[0]
819 /*
820 * If the extended attributes fall into an indirect block,
821 * dump it as well.
822 */
823 if ((size = spcl.c_extsize - NXADDR * sblock->fs_bsize) > 0)
824 dmpindir(dp, ino, dp->dp2.di_exti, 0, &size);
825 }
826
827 /*
828 * Dump a map to the tape.
829 */
830 void
dumpmap(char * map,int type,ino_t ino)831 dumpmap(char *map, int type, ino_t ino)
832 {
833 int i;
834 char *cp;
835
836 spcl.c_type = type;
837 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
838 writeheader(ino);
839 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
840 writerec(cp, 0);
841 }
842
843 /*
844 * Write a header record to the dump tape.
845 */
846 void
writeheader(ino_t ino)847 writeheader(ino_t ino)
848 {
849 int32_t sum, cnt, *lp;
850
851 if (rsync_friendly >= 2) {
852 /* don't track changes to access time */
853 spcl.c_atime = spcl.c_mtime;
854 spcl.c_atimensec = spcl.c_mtimensec;
855 }
856 spcl.c_inumber = ino;
857 spcl.c_magic = FS_UFS2_MAGIC;
858 spcl.c_checksum = 0;
859 lp = (int32_t *)&spcl;
860 sum = 0;
861 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
862 while (--cnt >= 0) {
863 sum += *lp++;
864 sum += *lp++;
865 sum += *lp++;
866 sum += *lp++;
867 }
868 spcl.c_checksum = CHECKSUM - sum;
869 writerec((char *)&spcl, 1);
870 }
871
872 union dinode *
getino(ino_t inum,int * modep)873 getino(ino_t inum, int *modep)
874 {
875 static ino_t minino, maxino;
876 static caddr_t inoblock;
877 struct ufs1_dinode *dp1;
878 struct ufs2_dinode *dp2;
879
880 if (inoblock == NULL && (inoblock = malloc(sblock->fs_bsize)) == NULL)
881 quit("cannot allocate inode memory.\n");
882 curino = inum;
883 if (inum >= minino && inum < maxino)
884 goto gotit;
885 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), inoblock,
886 (int)sblock->fs_bsize);
887 minino = inum - (inum % INOPB(sblock));
888 maxino = minino + INOPB(sblock);
889 gotit:
890 if (sblock->fs_magic == FS_UFS1_MAGIC) {
891 dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino];
892 *modep = (dp1->di_mode & IFMT);
893 return ((union dinode *)dp1);
894 }
895 dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino];
896 *modep = (dp2->di_mode & IFMT);
897 return ((union dinode *)dp2);
898 }
899
900 /*
901 * Read a chunk of data from the disk.
902 * Try to recover from hard errors by reading in sector sized pieces.
903 * Error recovery is attempted at most BREADEMAX times before seeking
904 * consent from the operator to continue.
905 */
906 int breaderrors = 0;
907 #define BREADEMAX 32
908
909 void
bread(ufs2_daddr_t blkno,char * buf,int size)910 bread(ufs2_daddr_t blkno, char *buf, int size)
911 {
912 int secsize, bytes, resid, xfer, base, cnt, i;
913 static char *tmpbuf;
914 off_t offset;
915
916 loop:
917 offset = blkno << dev_bshift;
918 secsize = sblock->fs_fsize;
919 base = offset % secsize;
920 resid = size % secsize;
921 /*
922 * If the transfer request starts or ends on a non-sector
923 * boundary, we must read the entire sector and copy out
924 * just the part that we need.
925 */
926 if (base == 0 && resid == 0) {
927 cnt = cread(diskfd, buf, size, offset);
928 if (cnt == size)
929 return;
930 } else {
931 if (tmpbuf == NULL && (tmpbuf = malloc(secsize)) == 0)
932 quit("buffer malloc failed\n");
933 xfer = 0;
934 bytes = size;
935 if (base != 0) {
936 cnt = cread(diskfd, tmpbuf, secsize, offset - base);
937 if (cnt != secsize)
938 goto bad;
939 xfer = MIN(secsize - base, size);
940 offset += xfer;
941 bytes -= xfer;
942 resid = bytes % secsize;
943 memcpy(buf, &tmpbuf[base], xfer);
944 }
945 if (bytes >= secsize) {
946 cnt = cread(diskfd, &buf[xfer], bytes - resid, offset);
947 if (cnt != bytes - resid)
948 goto bad;
949 xfer += cnt;
950 offset += cnt;
951 }
952 if (resid == 0)
953 return;
954 cnt = cread(diskfd, tmpbuf, secsize, offset);
955 if (cnt == secsize) {
956 memcpy(&buf[xfer], tmpbuf, resid);
957 return;
958 }
959 }
960 bad:
961 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
962 /*
963 * Trying to read the final fragment.
964 *
965 * NB - dump only works in TP_BSIZE blocks, hence
966 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
967 * It should be smarter about not actually trying to
968 * read more than it can get, but for the time being
969 * we punt and scale back the read only when it gets
970 * us into trouble. (mkm 9/25/83)
971 */
972 size -= dev_bsize;
973 goto loop;
974 }
975 if (cnt == -1)
976 msg("read error from %s: %s: [block %jd]: count=%d\n",
977 disk, strerror(errno), (intmax_t)blkno, size);
978 else
979 msg("short read error from %s: [block %jd]: count=%d, got=%d\n",
980 disk, (intmax_t)blkno, size, cnt);
981 if (++breaderrors > BREADEMAX) {
982 msg("More than %d block read errors from %s\n",
983 BREADEMAX, disk);
984 broadcast("DUMP IS AILING!\n");
985 msg("This is an unrecoverable error.\n");
986 if (!query("Do you want to attempt to continue?")){
987 dumpabort(0);
988 /*NOTREACHED*/
989 } else
990 breaderrors = 0;
991 }
992 /*
993 * Zero buffer, then try to read each sector of buffer separately,
994 * and bypass the cache.
995 */
996 memset(buf, 0, size);
997 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
998 if ((cnt = pread(diskfd, buf, (int)dev_bsize,
999 ((off_t)blkno << dev_bshift))) == dev_bsize)
1000 continue;
1001 if (cnt == -1) {
1002 msg("read error from %s: %s: [sector %jd]: count=%ld\n",
1003 disk, strerror(errno), (intmax_t)blkno, dev_bsize);
1004 continue;
1005 }
1006 msg("short read from %s: [sector %jd]: count=%ld, got=%d\n",
1007 disk, (intmax_t)blkno, dev_bsize, cnt);
1008 }
1009 }
1010