traverse.c revision 1.53 1 /* $NetBSD: traverse.c,v 1.53 2021/06/19 13:56:34 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)traverse.c 8.7 (Berkeley) 6/15/95";
36 #else
37 __RCSID("$NetBSD: traverse.c,v 1.53 2021/06/19 13:56:34 christos Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ffs/fs.h>
46 #include <ufs/ffs/ffs_extern.h>
47
48 #include <assert.h>
49 #include <ctype.h>
50 #include <errno.h>
51 #include <fts.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include "dump.h"
58
59 #define HASDUMPEDFILE 0x1
60 #define HASSUBDIRS 0x2
61
62 static int appendextdata(union dinode *dp);
63 static void writeextdata(union dinode *dp, ino_t ino, int added);
64 static int dirindir(ino_t, daddr_t, int, off_t *, u_int64_t *, int);
65 static void dmpindir(union dinode *dp, ino_t, daddr_t, int, off_t *);
66 static int searchdir(ino_t, daddr_t, long, off_t, u_int64_t *, int);
67
68 /*
69 * This is an estimation of the number of TP_BSIZE blocks in the file.
70 * It estimates the number of blocks in files with holes by assuming
71 * that all of the blocks accounted for by di_blocks are data blocks
72 * (when some of the blocks are usually used for indirect pointers);
73 * hence the estimate may be high.
74 */
75 int64_t
76 blockest(union dinode *dp)
77 {
78 int64_t blkest, sizeest;
79
80 /*
81 * dp->di_size is the size of the file in bytes.
82 * dp->di_blocks stores the number of sectors actually in the file.
83 * If there are more sectors than the size would indicate, this just
84 * means that there are indirect blocks in the file or unused
85 * sectors in the last file block; we can safely ignore these
86 * (blkest = sizeest below).
87 * If the file is bigger than the number of sectors would indicate,
88 * then the file has holes in it. In this case we must use the
89 * block count to estimate the number of data blocks used, but
90 * we use the actual size for estimating the number of indirect
91 * dump blocks (sizeest vs. blkest in the indirect block
92 * calculation).
93 */
94 if (DIP(dp, flags) & SF_SNAPSHOT)
95 return (1);
96 blkest = howmany(ufs_fragroundup(ufsib,
97 dbtob((u_int64_t)DIP(dp, blocks))), TP_BSIZE);
98 sizeest = howmany(ufs_fragroundup(ufsib, DIP(dp, size)), TP_BSIZE);
99 if (blkest > sizeest)
100 blkest = sizeest;
101 if (DIP(dp, size) > ufsib->ufs_bsize * UFS_NDADDR) {
102 /* calculate the number of indirect blocks on the dump tape */
103 blkest +=
104 howmany(sizeest - UFS_NDADDR * ufsib->ufs_bsize / TP_BSIZE,
105 TP_NINDIR);
106 }
107 return (blkest + 1);
108 }
109
110 /* Auxiliary macro to pick up files changed since previous dump. */
111 #define CHANGEDSINCE(dp, t) \
112 (DIP((dp), mtime) >= (t) || DIP((dp), ctime) >= (t))
113
114 /* The WANTTODUMP macro decides whether a file should be dumped. */
115 #ifdef UF_NODUMP
116 #define WANTTODUMP(dp) \
117 (CHANGEDSINCE(dp, iswap64(spcl.c_ddate)) && \
118 (nonodump || (DIP((dp), flags) & UF_NODUMP) != UF_NODUMP))
119 #else
120 #define WANTTODUMP(dp) CHANGEDSINCE(dp, iswap64(spcl.c_ddate))
121 #endif
122
123 /*
124 * Determine if given inode should be dumped
125 */
126 void
127 mapfileino(ino_t ino, u_int64_t *tape_size, int *dirskipped)
128 {
129 int mode;
130 union dinode *dp;
131
132 /*
133 * Skip inode if we've already marked it for dumping
134 */
135 if (TSTINO(ino, usedinomap))
136 return;
137 dp = getino(ino);
138 if (dp == NULL || (mode = (DIP(dp, mode) & IFMT)) == 0)
139 return;
140 /*
141 * Skip WAPBL log file inodes.
142 */
143 if (DIP(dp, flags) & SF_LOG)
144 return;
145 /*
146 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
147 * used map. All inode but dirs who have the nodump attribute go
148 * to the usedinomap.
149 */
150 SETINO(ino, usedinomap);
151 if (mode == IFDIR)
152 SETINO(ino, dumpdirmap);
153 if (WANTTODUMP(dp)) {
154 SETINO(ino, dumpinomap);
155 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
156 *tape_size += 1;
157 else
158 *tape_size += blockest(dp);
159 return;
160 }
161 if (mode == IFDIR) {
162 #ifdef UF_NODUMP
163 if (!nonodump && (DIP(dp, flags) & UF_NODUMP))
164 CLRINO(ino, usedinomap);
165 #endif
166 *dirskipped = 1;
167 }
168 }
169
170 /*
171 * Dump pass 1.
172 *
173 * Walk the inode list for a file system to find all allocated inodes
174 * that have been modified since the previous dump time. Also, find all
175 * the directories in the file system.
176 * disk may be NULL if dirv is NULL.
177 */
178 int
179 mapfiles(ino_t maxino, u_int64_t *tape_size, char *diskname, char * const *dirv)
180 {
181 int anydirskipped = 0;
182
183 if (dirv != NULL) {
184 char curdir[MAXPATHLEN];
185 FTS *dirh;
186 FTSENT *entry;
187 int d;
188
189 if (getcwd(curdir, sizeof(curdir)) == NULL) {
190 msg("Can't determine cwd: %s\n", strerror(errno));
191 dumpabort(0);
192 }
193 if ((dirh = fts_open(dirv, FTS_PHYSICAL|FTS_SEEDOT|FTS_XDEV,
194 NULL)) == NULL) {
195 msg("fts_open failed: %s\n", strerror(errno));
196 dumpabort(0);
197 }
198 while ((entry = fts_read(dirh)) != NULL) {
199 switch (entry->fts_info) {
200 case FTS_DNR: /* an error */
201 case FTS_ERR:
202 case FTS_NS:
203 msg("Can't fts_read %s: %s\n", entry->fts_path,
204 strerror(errno));
205 /* FALLTHROUGH */
206 case FTS_DP: /* already seen dir */
207 continue;
208 }
209 mapfileino(entry->fts_statp->st_ino, tape_size,
210 &anydirskipped);
211 }
212 (void)fts_close(dirh);
213
214 /*
215 * Add any parent directories
216 */
217 for (d = 0 ; dirv[d] != NULL ; d++) {
218 char path[MAXPATHLEN];
219
220 if (dirv[d][0] != '/')
221 (void)snprintf(path, sizeof(path), "%s/%s",
222 curdir, dirv[d]);
223 else
224 (void)snprintf(path, sizeof(path), "%s",
225 dirv[d]);
226 while (strcmp(path, diskname) != 0) {
227 char *p;
228 struct stat sb;
229
230 if (*path == '\0')
231 break;
232 if ((p = strrchr(path, '/')) == NULL)
233 break;
234 if (p == path)
235 break;
236 *p = '\0';
237 if (stat(path, &sb) == -1) {
238 msg("Can't stat %s: %s\n", path,
239 strerror(errno));
240 break;
241 }
242 mapfileino(sb.st_ino, tape_size, &anydirskipped);
243 }
244 }
245
246 /*
247 * Ensure that the root inode actually appears in the
248 * file list for a subdir
249 */
250 mapfileino(UFS_ROOTINO, tape_size, &anydirskipped);
251 } else {
252 fs_mapinodes(maxino, tape_size, &anydirskipped);
253 }
254 /*
255 * Restore gets very upset if the root is not dumped,
256 * so ensure that it always is dumped.
257 */
258 SETINO(UFS_ROOTINO, dumpinomap);
259 return (anydirskipped);
260 }
261
262 /*
263 * Dump pass 2.
264 *
265 * Scan each directory on the file system to see if it has any modified
266 * files in it. If it does, and has not already been added to the dump
267 * list (because it was itself modified), then add it. If a directory
268 * has not been modified itself, contains no modified files and has no
269 * subdirectories, then it can be deleted from the dump list and from
270 * the list of directories. By deleting it from the list of directories,
271 * its parent may now qualify for the same treatment on this or a later
272 * pass using this algorithm.
273 */
274 int
275 mapdirs(ino_t maxino, u_int64_t *tape_size)
276 {
277 union dinode *dp, di;
278 int i, isdir, nodump;
279 char *map;
280 ino_t ino;
281 off_t filesize;
282 int ret, change = 0;
283 daddr_t blk;
284
285 isdir = 0; /* XXX just to get gcc to shut up */
286 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
287 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
288 isdir = *map++;
289 else
290 isdir >>= 1;
291 /*
292 * If dir has been removed from the used map, it's either
293 * because it had the nodump flag, or it inherited it from
294 * its parent. A directory can't be in dumpinomap if
295 * not in usedinomap, but we have to go throuh it anyway
296 * to propagate the nodump attribute.
297 */
298 nodump = (TSTINO(ino, usedinomap) == 0);
299 if ((isdir & 1) == 0 ||
300 (TSTINO(ino, dumpinomap) && nodump == 0))
301 continue;
302
303 dp = getino(ino);
304 /*
305 * inode buf may be changed in searchdir().
306 */
307 if (is_ufs2)
308 di.dp2 = dp->dp2;
309 else
310 di.dp1 = dp->dp1;
311 filesize = DIP(&di, size);
312 for (ret = 0, i = 0; filesize > 0 && i < UFS_NDADDR; i++) {
313 if (is_ufs2)
314 blk = iswap64(di.dp2.di_db[i]);
315 else
316 blk = iswap32(di.dp1.di_db[i]);
317 if (blk != 0)
318 ret |= searchdir(ino, blk,
319 (long)ufs_dblksize(ufsib, &di, i),
320 filesize, tape_size, nodump);
321 if (ret & HASDUMPEDFILE)
322 filesize = 0;
323 else
324 filesize -= ufsib->ufs_bsize;
325 }
326 for (i = 0; filesize > 0 && i < UFS_NIADDR; i++) {
327 if (is_ufs2)
328 blk = iswap64(di.dp2.di_ib[i]);
329 else
330 blk = iswap32(di.dp1.di_ib[i]);
331 if (blk == 0)
332 continue;
333 ret |= dirindir(ino, blk, i, &filesize,
334 tape_size, nodump);
335 }
336 if (ret & HASDUMPEDFILE) {
337 SETINO(ino, dumpinomap);
338 *tape_size += blockest(&di);
339 change = 1;
340 continue;
341 }
342 if (nodump) {
343 if (ret & HASSUBDIRS)
344 change = 1; /* subdirs have inherited nodump */
345 CLRINO(ino, dumpdirmap);
346 } else if ((ret & HASSUBDIRS) == 0) {
347 if (!TSTINO(ino, dumpinomap)) {
348 CLRINO(ino, dumpdirmap);
349 change = 1;
350 }
351 }
352 }
353 return (change);
354 }
355
356 /*
357 * Read indirect blocks, and pass the data blocks to be searched
358 * as directories. Quit as soon as any entry is found that will
359 * require the directory to be dumped.
360 */
361 static int
362 dirindir(ino_t ino, daddr_t blkno, int ind_level, off_t *filesize,
363 u_int64_t *tape_size, int nodump)
364 {
365 int ret = 0;
366 int i;
367 union {
368 int64_t i64[MAXBSIZE / sizeof (int64_t)];
369 int32_t i32[MAXBSIZE / sizeof (int32_t)];
370 } idblk;
371
372 bread(fsatoda(ufsib, blkno), (char *)&idblk, (int)ufsib->ufs_bsize);
373 if (ind_level <= 0) {
374 for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
375 if (is_ufs2)
376 blkno = iswap64(idblk.i64[i]);
377 else
378 blkno = iswap32(idblk.i32[i]);
379 if (blkno != 0)
380 ret |= searchdir(ino, blkno,
381 ufsib->ufs_bsize, *filesize,
382 tape_size, nodump);
383 if (ret & HASDUMPEDFILE)
384 *filesize = 0;
385 else
386 *filesize -= ufsib->ufs_bsize;
387 }
388 return (ret);
389 }
390 ind_level--;
391 for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
392 if (is_ufs2)
393 blkno = iswap64(idblk.i64[i]);
394 else
395 blkno = iswap32(idblk.i32[i]);
396 if (blkno != 0)
397 ret |= dirindir(ino, blkno, ind_level, filesize,
398 tape_size, nodump);
399 }
400 return (ret);
401 }
402
403 /*
404 * Scan a disk block containing directory information looking to see if
405 * any of the entries are on the dump list and to see if the directory
406 * contains any subdirectories.
407 */
408 static int
409 searchdir(ino_t dino, daddr_t blkno, long size, off_t filesize,
410 u_int64_t *tape_size, int nodump)
411 {
412 struct direct *dp;
413 union dinode *ip;
414 long loc, ret = 0;
415 char *dblk;
416 ino_t ino;
417
418 dblk = malloc(size);
419 if (dblk == NULL)
420 quit("%s: cannot allocate directory memory", __func__);
421 bread(fsatoda(ufsib, blkno), dblk, (int)size);
422 if (filesize < size)
423 size = filesize;
424 for (loc = 0; loc < size; ) {
425 dp = (struct direct *)(dblk + loc);
426 if (dp->d_reclen == 0) {
427 msg("corrupted directory, inumber %llu\n",
428 (unsigned long long)dino);
429 break;
430 }
431 loc += iswap16(dp->d_reclen);
432 if (dp->d_ino == 0)
433 continue;
434 if (dp->d_name[0] == '.') {
435 if (dp->d_name[1] == '\0')
436 continue;
437 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
438 continue;
439 }
440 ino = iswap32(dp->d_ino);
441 if (nodump) {
442 ip = getino(ino);
443 if (TSTINO(ino, dumpinomap)) {
444 CLRINO(ino, dumpinomap);
445 *tape_size -= blockest(ip);
446 }
447 /*
448 * Add back to dumpdirmap and remove from usedinomap
449 * to propagate nodump.
450 */
451 if ((DIP(ip, mode) & IFMT) == IFDIR) {
452 SETINO(ino, dumpdirmap);
453 CLRINO(ino, usedinomap);
454 ret |= HASSUBDIRS;
455 }
456 } else {
457 if (TSTINO(ino, dumpinomap)) {
458 ret |= HASDUMPEDFILE;
459 if (ret & HASSUBDIRS)
460 break;
461 }
462 if (TSTINO(ino, dumpdirmap)) {
463 ret |= HASSUBDIRS;
464 if (ret & HASDUMPEDFILE)
465 break;
466 }
467 }
468 }
469 free(dblk);
470 return (ret);
471 }
472
473 /*
474 * Dump passes 3 and 4.
475 *
476 * Dump the contents of an inode to tape.
477 */
478 void
479 dumpino(union dinode *dp, ino_t ino)
480 {
481 int ind_level, cnt, last, added;
482 off_t size;
483 char buf[TP_BSIZE];
484 daddr_t blk;
485 void *shortlink;
486
487 if (newtape) {
488 newtape = 0;
489 dumpmap(dumpinomap, TS_BITS, ino);
490 }
491 CLRINO(ino, dumpinomap);
492 /*
493 * Zero out the size of a snapshot so that it will be dumped
494 * as a zero length file.
495 */
496 if (DIP(dp, flags) & SF_SNAPSHOT) {
497 DIP_SET(dp, size, 0);
498 DIP_SET(dp, flags, DIP(dp, flags) & ~SF_SNAPSHOT);
499 }
500 if (!is_ufs2) {
501 if (needswap)
502 ffs_dinode1_swap(&dp->dp1, &spcl.c_dinode);
503 else
504 spcl.c_dinode = dp->dp1;
505 } else {
506 if (needswap)
507 ffs_dinode2_swap(&dp->dp2, &dp->dp2);
508 spcl.c_mode = dp->dp2.di_mode;
509 spcl.c_size = dp->dp2.di_size;
510 spcl.c_extsize = dp->dp2.di_extsize;
511 spcl.c_atime = dp->dp2.di_atime;
512 spcl.c_atimensec = dp->dp2.di_atimensec;
513 spcl.c_mtime = dp->dp2.di_mtime;
514 spcl.c_mtimensec = dp->dp2.di_mtimensec;
515 spcl.c_birthtime = dp->dp2.di_birthtime;
516 spcl.c_birthtimensec = dp->dp2.di_birthnsec;
517 spcl.c_rdev = dp->dp2.di_rdev;
518 spcl.c_file_flags = dp->dp2.di_flags;
519 spcl.c_uid = dp->dp2.di_uid;
520 spcl.c_gid = dp->dp2.di_gid;
521 }
522 spcl.c_type = iswap32(TS_INODE);
523 spcl.c_count = 0;
524 switch (DIP(dp, mode) & IFMT) {
525
526 case 0:
527 /*
528 * Freed inode.
529 */
530 return;
531
532 case IFLNK:
533 /*
534 * Check for short symbolic link.
535 */
536 if (DIP(dp, size) > 0 &&
537 #ifdef FS_44INODEFMT
538 (DIP(dp, size) < ufsib->ufs_maxsymlinklen ||
539 (ufsib->ufs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))
540 #else
541 DIP(dp, blocks) == 0
542 #endif
543 ) {
544 spcl.c_addr[0] = 1;
545 spcl.c_count = iswap32(1);
546 added = appendextdata(dp);
547 writeheader(ino);
548 if (is_ufs2)
549 shortlink = dp->dp2.di_db;
550 else
551 shortlink = dp->dp1.di_db;
552 memmove(buf, shortlink, DIP(dp, size));
553 buf[DIP(dp, size)] = '\0';
554 writerec(buf, 0);
555 writeextdata(dp, ino, added);
556 return;
557 }
558 /* fall through */
559
560 case IFDIR:
561 case IFREG:
562 if (DIP(dp, size) > 0)
563 break;
564 /* fall through */
565
566 case IFIFO:
567 case IFSOCK:
568 case IFCHR:
569 case IFBLK:
570 added = appendextdata(dp);
571 writeheader(ino);
572 writeextdata(dp, ino, added);
573 return;
574
575 default:
576 msg("Warning: undefined file type 0%o\n", DIP(dp, mode) & IFMT);
577 return;
578 }
579 if (DIP(dp, size) > UFS_NDADDR * ufsib->ufs_bsize) {
580 cnt = UFS_NDADDR * ufsib->ufs_frag;
581 last = 0;
582 } else {
583 cnt = howmany(DIP(dp, size), ufsib->ufs_fsize);
584 last = 1;
585 }
586 if (is_ufs2)
587 blksout64(dp, &dp->dp2.di_db[0], cnt, ino, last);
588 else
589 blksout32(&dp->dp1.di_db[0], cnt, ino);
590
591 if ((size = DIP(dp, size) - UFS_NDADDR * ufsib->ufs_bsize) <= 0)
592 return;
593 for (ind_level = 0; ind_level < UFS_NIADDR; ind_level++) {
594 if (is_ufs2)
595 blk = iswap64(dp->dp2.di_ib[ind_level]);
596 else
597 blk = iswap32(dp->dp1.di_ib[ind_level]);
598 dmpindir(dp, ino, blk, ind_level, &size);
599 if (size <= 0)
600 return;
601 }
602 }
603
604 /*
605 * Read indirect blocks, and pass the data blocks to be dumped.
606 */
607 static void
608 dmpindir(union dinode *dp, ino_t ino, daddr_t blk, int ind_level, off_t *size)
609 {
610 int i, cnt, last;
611 union {
612 int32_t i32[MAXBSIZE / sizeof (int32_t)];
613 int64_t i64[MAXBSIZE / sizeof (int64_t)];
614 } idblk;
615 daddr_t iblk;
616
617
618 if (blk != 0)
619 bread(fsatoda(ufsib, blk), (char *)&idblk,
620 (int) ufsib->ufs_bsize);
621 else
622 memset(&idblk, 0, (int)ufsib->ufs_bsize);
623 if (ind_level <= 0) {
624 if (*size < ufsib->ufs_nindir * ufsib->ufs_bsize) {
625 cnt = howmany(*size, ufsib->ufs_fsize);
626 last = 0;
627 } else {
628 cnt = ufsib->ufs_nindir * ufsib->ufs_frag;
629 last = 1;
630 }
631 *size -= ufsib->ufs_nindir * ufsib->ufs_bsize;
632 if (is_ufs2)
633 blksout64(dp, &idblk.i64[0], cnt, ino, last);
634 else
635 blksout32(&idblk.i32[0], cnt, ino);
636 return;
637 }
638 ind_level--;
639 for (i = 0; i < ufsib->ufs_nindir; i++) {
640 if (is_ufs2)
641 iblk = iswap64(idblk.i64[i]);
642 else
643 iblk = iswap32(idblk.i32[i]);
644 dmpindir(dp, ino, iblk, ind_level, size);
645 if (*size <= 0)
646 return;
647 }
648 }
649
650 /*
651 * Collect up the data into tape record sized buffers and output them.
652 */
653 void
654 blksout32(int32_t *blkp, int frags, ino_t ino)
655 {
656 int32_t *bp;
657 int i, j, count, blks, tbperdb;
658
659 blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
660 tbperdb = ufsib->ufs_bsize >> tp_bshift;
661 for (i = 0; i < blks; i += TP_NINDIR) {
662 if (i + TP_NINDIR > blks)
663 count = blks;
664 else
665 count = i + TP_NINDIR;
666 for (j = i; j < count; j++)
667 if (blkp[j / tbperdb] != 0)
668 spcl.c_addr[j - i] = 1;
669 else
670 spcl.c_addr[j - i] = 0;
671 spcl.c_count = iswap32(count - i);
672 writeheader(ino);
673 bp = &blkp[i / tbperdb];
674 for (j = i; j < count; j += tbperdb, bp++)
675 if (*bp != 0) {
676 if (j + tbperdb <= count)
677 dumpblock(iswap32(*bp), (int)ufsib->ufs_bsize);
678 else
679 dumpblock(iswap32(*bp), (count - j) * TP_BSIZE);
680 }
681 spcl.c_type = iswap32(TS_ADDR);
682 }
683 }
684
685 void
686 blksout64(union dinode *dp, int64_t *blkp, int frags, ino_t ino, int last)
687 {
688 int64_t *bp;
689 int i, j, count, blks, tbperdb, added = 0;
690 static int writingextdata = 0;
691
692 blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
693 if (last) {
694 int resid;
695 int extsize = iswap32(spcl.c_extsize);
696 if (writingextdata)
697 resid = howmany(ufsib->ufs_qfmask & extsize,
698 TP_BSIZE);
699 else
700 resid = howmany(ufsib->ufs_qfmask & dp->dp2.di_size,
701 TP_BSIZE);
702 if (resid > 0)
703 blks -= howmany(ufsib->ufs_fsize, TP_BSIZE) - resid;
704 }
705 tbperdb = ufsib->ufs_bsize >> tp_bshift;
706 for (i = 0; i < blks; i += TP_NINDIR) {
707 if (i + TP_NINDIR > blks)
708 count = blks;
709 else
710 count = i + TP_NINDIR;
711 assert(count <= TP_NINDIR + i);
712 for (j = i; j < count; j++)
713 if (blkp[j / tbperdb] != 0)
714 spcl.c_addr[j - i] = 1;
715 else
716 spcl.c_addr[j - i] = 0;
717 spcl.c_count = iswap32(count - i);
718 if (last && count == blks && !writingextdata)
719 added = appendextdata(dp);
720 writeheader(ino);
721 bp = &blkp[i / tbperdb];
722 for (j = i; j < count; j += tbperdb, bp++)
723 if (*bp != 0) {
724 if (j + tbperdb <= count)
725 dumpblock(iswap64(*bp), (int)ufsib->ufs_bsize);
726 else
727 dumpblock(iswap64(*bp), (count - j) * TP_BSIZE);
728 }
729 spcl.c_type = iswap32(TS_ADDR);
730 spcl.c_count = 0;
731 if (last && count == blks && !writingextdata) {
732 writingextdata = 1;
733 writeextdata(dp, ino, added);
734 writingextdata = 0;
735 }
736 }
737 }
738
739 /*
740 * If there is room in the current block for the extended attributes
741 * as well as the file data, update the header to reflect the added
742 * attribute data at the end. Attributes are placed at the end so that
743 * old versions of restore will correctly restore the file and simply
744 * discard the extra data at the end that it does not understand.
745 * The attribute data is dumped following the file data by the
746 * writeextdata() function (below).
747 */
748 static int
749 appendextdata(union dinode *dp)
750 {
751 int i, blks, tbperdb, count, extsize;
752
753 count = iswap32(spcl.c_count);
754 extsize = iswap32(spcl.c_extsize);
755 /*
756 * If no extended attributes, there is nothing to do.
757 */
758 if (extsize == 0)
759 return (0);
760 /*
761 * If there is not enough room at the end of this block
762 * to add the extended attributes, then rather than putting
763 * part of them here, we simply push them entirely into a
764 * new block rather than putting some here and some later.
765 */
766 if (extsize > UFS_NXADDR * ufsib->ufs_bsize)
767 blks = howmany(UFS_NXADDR * ufsib->ufs_bsize, TP_BSIZE);
768 else
769 blks = howmany(extsize, TP_BSIZE);
770 if (count + blks > TP_NINDIR)
771 return (0);
772 /*
773 * Update the block map in the header to indicate the added
774 * extended attribute. They will be appended after the file
775 * data by the writeextdata() routine.
776 */
777 tbperdb = ufsib->ufs_bsize >> tp_bshift;
778 assert(count + blks < TP_NINDIR);
779 for (i = 0; i < blks; i++)
780 if (&dp->dp2.di_extb[i / tbperdb] != 0)
781 spcl.c_addr[count + i] = 1;
782 else
783 spcl.c_addr[count + i] = 0;
784 spcl.c_count = iswap32(count + blks);
785 return (blks);
786 }
787
788 /*
789 * Dump the extended attribute data. If there was room in the file
790 * header, then all we need to do is output the data blocks. If there
791 * was not room in the file header, then an additional TS_ADDR header
792 * is created to hold the attribute data.
793 */
794 static void
795 writeextdata(union dinode *dp, ino_t ino, int added)
796 {
797 int i, frags, blks, tbperdb, last, extsize;
798 int64_t *bp;
799 off_t size;
800
801 extsize = iswap32(spcl.c_extsize);
802
803 /*
804 * If no extended attributes, there is nothing to do.
805 */
806 if (extsize == 0)
807 return;
808 /*
809 * If there was no room in the file block for the attributes,
810 * dump them out in a new block, otherwise just dump the data.
811 */
812 if (added == 0) {
813 if (extsize > UFS_NXADDR * ufsib->ufs_bsize) {
814 frags = UFS_NXADDR * ufsib->ufs_frag;
815 last = 0;
816 } else {
817 frags = howmany(extsize, ufsib->ufs_fsize);
818 last = 1;
819 }
820 blksout64(dp, &dp->dp2.di_extb[0], frags, ino, last);
821 } else {
822 if (extsize > UFS_NXADDR * ufsib->ufs_bsize)
823 blks = howmany(UFS_NXADDR * ufsib->ufs_bsize, TP_BSIZE);
824 else
825 blks = howmany(extsize, TP_BSIZE);
826 tbperdb = ufsib->ufs_bsize >> tp_bshift;
827 for (i = 0; i < blks; i += tbperdb) {
828 bp = &dp->dp2.di_extb[i / tbperdb];
829 if (*bp != 0) {
830 if (i + tbperdb <= blks)
831 dumpblock(iswap64(*bp), (int)ufsib->ufs_bsize);
832 else
833 dumpblock(iswap64(*bp), (blks - i) * TP_BSIZE);
834 }
835 }
836
837 }
838 /*
839 * If an indirect block is added for extended attributes, then
840 * di_exti below should be changed to the structure element
841 * that references the extended attribute indirect block. This
842 * definition is here only to make it compile without complaint.
843 */
844 #define di_exti di_spare[0]
845 /*
846 * If the extended attributes fall into an indirect block,
847 * dump it as well.
848 */
849 if ((size = extsize - UFS_NXADDR * ufsib->ufs_bsize) > 0)
850 dmpindir(dp, ino, dp->dp2.di_exti, 0, &size);
851 }
852
853 /*
854 * Dump a map to the tape.
855 */
856 void
857 dumpmap(char *map, int type, ino_t ino)
858 {
859 int i;
860 char *cp;
861
862 spcl.c_type = iswap32(type);
863 spcl.c_count = iswap32(howmany(mapsize * sizeof(char), TP_BSIZE));
864 writeheader(ino);
865 for (i = 0, cp = map; i < iswap32(spcl.c_count); i++, cp += TP_BSIZE)
866 writerec(cp, 0);
867 }
868
869 /*
870 * Write a header record to the dump tape.
871 */
872 void
873 writeheader(ino_t ino)
874 {
875 int32_t sum, cnt, *lp;
876
877 spcl.c_inumber = iswap32(ino);
878 if (is_ufs2)
879 spcl.c_magic = iswap32(FS_UFS2_MAGIC);
880 else {
881 spcl.c_magic = iswap32(NFS_MAGIC);
882 spcl.c_old_date = iswap32(iswap64(spcl.c_date));
883 spcl.c_old_ddate = iswap32(iswap64(spcl.c_ddate));
884 spcl.c_old_tapea = iswap32(iswap64(spcl.c_tapea));
885 spcl.c_old_firstrec = iswap32(iswap64(spcl.c_firstrec));
886 }
887 spcl.c_checksum = 0;
888 lp = (int32_t *)&spcl;
889 sum = 0;
890 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
891 while (--cnt >= 0) {
892 sum += iswap32(*lp++);
893 sum += iswap32(*lp++);
894 sum += iswap32(*lp++);
895 sum += iswap32(*lp++);
896 }
897 spcl.c_checksum = iswap32(CHECKSUM - sum);
898 writerec((char *)&spcl, 1);
899 }
900