2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
10 * Copyright (c) 1980, 1988, 1991, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
44 static char sccsid[] = "@(#)traverse.c 8.7 (Berkeley) 6/15/95";
46 static const char rcsid[] =
47 "$Id: traverse.c,v 1.4 1999/10/11 13:08:08 stelian Exp $";
50 #include <sys/param.h>
53 #include <linux/ext2_fs.h>
54 #include <bsdcompat.h>
55 #include <compaterr.h>
57 #define swab32(x) ext2fs_swab32(x)
61 #include <sys/vnode.h>
64 #include <ufs/fsdir.h>
65 #include <ufs/inode.h>
67 #include <ufs/ufs/dir.h>
68 #include <ufs/ufs/dinode.h>
69 #include <ufs/ffs/fs.h>
71 #endif /* __linux__ */
73 #include <protocols/dumprestore.h>
83 #include <ext2fs/ext2fs.h>
88 #define HASDUMPEDFILE 0x1
89 #define HASSUBDIRS 0x2
92 typedef quad_t fsizeT;
98 static int searchdir __P((struct ext2_dir_entry *dp, int offset,
99 int blocksize, char *buf, void *private));
100 loff_t llseek (int fd, loff_t offset, int origin);
102 static int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
103 static void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
104 static int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
108 * This is an estimation of the number of TP_BSIZE blocks in the file.
109 * It estimates the number of blocks in files with holes by assuming
110 * that all of the blocks accounted for by di_blocks are data blocks
111 * (when some of the blocks are usually used for indirect pointers);
112 * hence the estimate may be high.
115 blockest(struct dinode *dp)
117 long blkest, sizeest;
120 * dp->di_size is the size of the file in bytes.
121 * dp->di_blocks stores the number of sectors actually in the file.
122 * If there are more sectors than the size would indicate, this just
123 * means that there are indirect blocks in the file or unused
124 * sectors in the last file block; we can safely ignore these
125 * (blkest = sizeest below).
126 * If the file is bigger than the number of sectors would indicate,
127 * then the file has holes in it. In this case we must use the
128 * block count to estimate the number of data blocks used, but
129 * we use the actual size for estimating the number of indirect
130 * dump blocks (sizeest vs. blkest in the indirect block
133 blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
134 sizeest = howmany(dp->di_size, TP_BSIZE);
135 if (blkest > sizeest)
138 if (dp->di_size > fs->blocksize * NDADDR) {
139 /* calculate the number of indirect blocks on the dump tape */
141 howmany(sizeest - NDADDR * fs->blocksize / TP_BSIZE,
145 if (dp->di_size > sblock->fs_bsize * NDADDR) {
146 /* calculate the number of indirect blocks on the dump tape */
148 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
155 /* Auxiliary macro to pick up files changed since previous dump. */
156 #define CHANGEDSINCE(dp, t) \
157 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
159 /* The WANTTODUMP macro decides whether a file should be dumped. */
161 #define WANTTODUMP(dp) \
162 (CHANGEDSINCE(dp, spcl.c_ddate) && \
163 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
165 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
171 * Walk the inode list for a filesystem to find all allocated inodes
172 * that have been modified since the previous dump time. Also, find all
173 * the directories in the filesystem.
176 mapfiles(ino_t maxino, long *tapesize)
180 register struct dinode *dp;
181 int anydirskipped = 0;
183 for (ino = ROOTINO; ino < maxino; ino++) {
185 if ((mode = (dp->di_mode & IFMT)) == 0)
188 if (dp->di_nlink == 0 || dp->di_dtime != 0)
191 SETINO(ino, usedinomap);
193 SETINO(ino, dumpdirmap);
194 if (WANTTODUMP(dp)) {
195 SETINO(ino, dumpinomap);
196 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
199 *tapesize += blockest(dp);
206 * Restore gets very upset if the root is not dumped,
207 * so ensure that it always is dumped.
209 SETINO(ROOTINO, dumpinomap);
210 return (anydirskipped);
214 struct mapfile_context {
220 mapfilesindir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
222 register struct dinode *dp;
226 struct mapfile_context *mfc;
229 mfc = (struct mapfile_context *)private;
231 if ((mode = (dp->di_mode & IFMT)) != 0 &&
232 dp->di_nlink != 0 && dp->di_dtime == 0) {
233 SETINO(ino, usedinomap);
235 SETINO(ino, dumpdirmap);
236 if (WANTTODUMP(dp)) {
237 SETINO(ino, dumpinomap);
238 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
241 *mfc->tapesize += blockest(dp);
244 mfc->anydirskipped = 1;
245 if ((dirent->name[0] != '.' || dirent->name_len != 1) &&
246 (dirent->name[0] != '.' || dirent->name[1] != '.' ||
247 dirent->name_len != 2)) {
248 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
249 mapfilesindir, private);
261 * Walk the inode list for a filesystem to find all allocated inodes
262 * that have been modified since the previous dump time. Also, find all
263 * the directories in the filesystem.
266 mapfilesfromdir(ino_t maxino, long *tapesize, char *directory)
269 struct mapfile_context mfc;
271 char dir_name [MAXPATHLEN];
275 * Mark every directory in the path as being dumped
277 for (i = 0; i < strlen (directory); i++) {
278 if (directory[i] == '/') {
279 strncpy (dir_name, directory, i);
281 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
284 com_err(disk, retval, "while translating %s",
288 /* SETINO(dir_ino, dumpinomap); */
289 SETINO(dir_ino, dumpdirmap);
293 * Mark the final directory
295 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
297 com_err(disk, retval, "while translating %s", directory);
300 /* SETINO(dir_ino, dumpinomap); */
301 SETINO(dir_ino, dumpdirmap);
303 mfc.tapesize = tapesize;
304 mfc.anydirskipped = 0;
305 retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
309 com_err(disk, retval, "while mapping files in %s", directory);
313 * Restore gets very upset if the root is not dumped,
314 * so ensure that it always is dumped.
316 /* SETINO(ROOTINO, dumpinomap); */
317 SETINO(ROOTINO, dumpdirmap);
318 return (mfc.anydirskipped);
325 * Scan each directory on the filesystem to see if it has any modified
326 * files in it. If it does, and has not already been added to the dump
327 * list (because it was itself modified), then add it. If a directory
328 * has not been modified itself, contains no modified files and has no
329 * subdirectories, then it can be deleted from the dump list and from
330 * the list of directories. By deleting it from the list of directories,
331 * its parent may now qualify for the same treatment on this or a later
332 * pass using this algorithm.
335 mapdirs(ino_t maxino, long *tapesize)
337 register struct dinode *dp;
347 isdir = 0; /* XXX just to get gcc to shut up */
348 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
349 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
353 if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
358 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &ret);
359 #else /* __linux__ */
360 filesize = dp->di_size;
361 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
362 if (dp->di_db[i] != 0)
363 ret |= searchdir(ino, dp->di_db[i],
364 (long)dblksize(sblock, dp, i),
366 if (ret & HASDUMPEDFILE)
369 filesize -= sblock->fs_bsize;
371 for (i = 0; filesize > 0 && i < NIADDR; i++) {
372 if (dp->di_ib[i] == 0)
374 ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
376 #endif /* __linux__ */
377 if (ret & HASDUMPEDFILE) {
378 SETINO(ino, dumpinomap);
379 *tapesize += blockest(dp);
383 if ((ret & HASSUBDIRS) == 0) {
384 if (!TSTINO(ino, dumpinomap)) {
385 CLRINO(ino, dumpdirmap);
395 * Read indirect blocks, and pass the data blocks to be searched
396 * as directories. Quit as soon as any entry is found that will
397 * require the directory to be dumped.
400 dirindir(ino_t ino, daddr_t blkno, int ind_level, long *filesize)
404 daddr_t idblk[MAXNINDIR];
406 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
407 if (ind_level <= 0) {
408 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
411 ret |= searchdir(ino, blkno, sblock->fs_bsize,
413 if (ret & HASDUMPEDFILE)
416 *filesize -= sblock->fs_bsize;
421 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
424 ret |= dirindir(ino, blkno, ind_level, filesize);
428 #endif /* !__linux__ */
431 * Scan a disk block containing directory information looking to see if
432 * any of the entries are on the dump list and to see if the directory
433 * contains any subdirectories.
437 searchdir(struct ext2_dir_entry *dp, int offset, int blocksize, char *buf, void *private)
439 int *ret = (int *) private;
443 if (dp->name[0] == '.') {
444 if (dp->name_len == 1)
446 if (dp->name[1] == '.' && dp->name_len == 2)
449 if (TSTINO(dp->inode, dumpinomap)) {
450 *ret |= HASDUMPEDFILE;
451 if (*ret & HASSUBDIRS)
454 if (TSTINO(dp->inode, dumpdirmap)) {
456 if (*ret & HASDUMPEDFILE)
462 #else /* __linux__ */
465 searchdir(ino_t ino, daddr_t blkno, long size, long filesize)
467 register struct direct *dp;
468 register long loc, ret = 0;
471 bread(fsbtodb(sblock, blkno), dblk, (int)size);
474 for (loc = 0; loc < size; ) {
475 dp = (struct direct *)(dblk + loc);
476 if (dp->d_reclen == 0) {
477 msg("corrupted directory, inumber %d\n", ino);
483 if (dp->d_name[0] == '.') {
484 if (dp->d_name[1] == '\0')
486 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
489 if (TSTINO(dp->d_ino, dumpinomap)) {
490 ret |= HASDUMPEDFILE;
491 if (ret & HASSUBDIRS)
494 if (TSTINO(dp->d_ino, dumpdirmap)) {
496 if (ret & HASDUMPEDFILE)
502 #endif /* __linux__ */
506 struct block_context {
515 * Dump a block to the tape
518 dumponeblock(ext2_filsys fs, blk_t *blocknr, int blockcnt, void *private)
520 struct block_context *p;
523 if (blockcnt < NDADDR)
525 p = (struct block_context *)private;
526 for (i = p->next_block; i < blockcnt; i++) {
527 p->buf[p->cnt++] = 0;
528 if (p->cnt == p->max) {
529 blksout (p->buf, p->cnt, p->ino);
533 p->buf[p->cnt++] = *blocknr;
534 if (p->cnt == p->max) {
535 blksout (p->buf, p->cnt, p->ino);
538 p->next_block = blockcnt + 1;
544 * Dump passes 3 and 4.
546 * Dump the contents of an inode to tape.
549 dumpino(struct dinode *dp, ino_t ino)
554 struct old_bsd_inode obi;
556 struct block_context bc;
563 dumpmap(dumpinomap, TS_BITS, ino);
565 CLRINO(ino, dumpinomap);
567 memset(&obi, 0, sizeof(obi));
568 obi.di_mode = dp->di_mode;
569 obi.di_uid = dp->di_uid;
570 obi.di_gid = dp->di_gid;
571 obi.di_qsize.v = (u_quad_t)dp->di_size;
572 obi.di_atime = dp->di_atime;
573 obi.di_mtime = dp->di_mtime;
574 obi.di_ctime = dp->di_ctime;
575 obi.di_nlink = dp->di_nlink;
576 obi.di_blocks = dp->di_blocks;
577 obi.di_flags = dp->di_flags;
578 obi.di_gen = dp->di_gen;
579 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
580 if (dp->di_file_acl || dp->di_dir_acl)
581 warn("ACLs in inode #%ld won't be dumped", ino);
582 memmove(&spcl.c_dinode, &obi, sizeof(obi));
583 #else /* __linux__ */
585 #endif /* __linux__ */
586 spcl.c_type = TS_INODE;
588 switch (dp->di_mode & S_IFMT) {
598 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
604 * Check for short symbolic link.
607 if (dp->di_size > 0 &&
608 dp->di_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
612 memmove(buf, dp->di_db, (u_long)dp->di_size);
613 buf[dp->di_size] = '\0';
617 #endif /* __linux__ */
619 if (dp->di_size > 0 &&
620 dp->di_size < sblock->fs_maxsymlinklen) {
624 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
625 buf[dp->di_size] = '\0';
648 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
651 if (dp->di_size > NDADDR * sblock->fs_bsize)
653 cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super);
655 cnt = NDADDR * sblock->fs_frag;
658 cnt = howmany(dp->di_size, sblock->fs_fsize);
659 blksout(&dp->di_db[0], cnt, ino);
660 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
663 bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
664 bc.buf = (int *)malloc (bc.max * sizeof (int));
667 bc.next_block = NDADDR;
669 ext2fs_block_iterate (fs, ino, 0, NULL, dumponeblock, (void *)&bc);
671 blksout (bc.buf, bc.cnt, bc.ino);
674 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
675 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
684 struct convert_dir_context {
692 * This function converts an ext2fs directory entry to the BSD format.
694 * Basically, it adds a null-character at the end of the name, recomputes the
695 * size of the entry, and creates it in a temporary buffer
698 convert_dir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
700 struct convert_dir_context *p;
704 p = (struct convert_dir_context *)private;
706 reclen = EXT2_DIR_REC_LEN(dirent->name_len + 1);
707 if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
708 dp = (struct direct *)(p->buf + p->prev_offset);
709 dp->d_reclen += p->bs - (p->offset % p->bs);
710 p->offset += p->bs - (p->offset % p->bs);
713 dp = (struct direct *)(p->buf + p->offset);
714 dp->d_ino = dirent->inode;
715 dp->d_reclen = reclen;
717 dp->d_namlen = dirent->name_len;
718 strncpy(dp->d_name, dirent->name, dirent->name_len);
719 dp->d_name[dp->d_namlen] = '\0';
720 p->prev_offset = p->offset;
729 * Dumps a directory to tape after converting it to the BSD format
732 dumpdirino(struct dinode *dp, ino_t ino)
736 struct old_bsd_inode obi;
737 struct convert_dir_context cdc;
739 struct ext2_dir_entry *de;
744 dumpmap(dumpinomap, TS_BITS, ino);
746 CLRINO(ino, dumpinomap);
749 * Convert the directory to the BSD format
751 /* Allocate a buffer for the conversion (twice the size of the
752 ext2fs directory to avoid problems ;-) */
753 cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
755 err(1, "Cannot allocate buffer to convert directory #%lu\n",
759 cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
760 /* Do the conversion */
761 retval = ext2fs_dir_iterate(fs, ino, 0, NULL, convert_dir, (void *)&cdc);
763 com_err(disk, retval, "while converting directory #%ld\n", ino);
766 /* Fix the last entry */
767 if ((cdc.offset % cdc.bs) != 0) {
768 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
769 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
770 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
773 dir_size = cdc.offset;
776 memset(&obi, 0, sizeof(obi));
777 obi.di_mode = dp->di_mode;
778 obi.di_uid = dp->di_uid;
779 obi.di_gid = dp->di_gid;
780 obi.di_qsize.v = dir_size; /* (u_quad_t)dp->di_size; */
781 obi.di_atime = dp->di_atime;
782 obi.di_mtime = dp->di_mtime;
783 obi.di_ctime = dp->di_ctime;
784 obi.di_nlink = dp->di_nlink;
785 obi.di_blocks = dp->di_blocks;
786 obi.di_flags = dp->di_flags;
787 obi.di_gen = dp->di_gen;
788 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
789 if (dp->di_file_acl || dp->di_dir_acl)
790 warn("ACLs in inode #%ld won't be dumped", ino);
791 memmove(&spcl.c_dinode, &obi, sizeof(obi));
792 #else /* __linux__ */
794 #endif /* __linux__ */
795 spcl.c_type = TS_INODE;
797 switch (dp->di_mode & S_IFMT) {
808 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
813 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
814 dp->di_mode & IFMT, ino);
817 for (size = 0; size < dir_size; size += TP_BSIZE) {
821 memmove(buf, cdc.buf + size, TP_BSIZE);
823 spcl.c_type = TS_ADDR;
828 #endif /* __linux__ */
832 * Read indirect blocks, and pass the data blocks to be dumped.
835 dmpindir(ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
842 daddr_t idblk[MAXNINDIR];
845 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
848 * My RedHat 4.0 system doesn't have these flags; I haven't
849 * upgraded e2fsprogs yet
851 #if defined(EXT2_FLAG_SWAP_BYTES)
852 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
853 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) {
855 max = sblock->fs_bsize >> 2;
856 swapme = (blk_t *) idblk;
857 for (i = 0; i < max; i++, swapme++)
858 *swapme = swab32(*swapme);
859 #if defined(EXT2_FLAG_SWAP_BYTES)
864 memset(idblk, 0, (int)sblock->fs_bsize);
865 if (ind_level <= 0) {
866 if (*size < NINDIR(sblock) * sblock->fs_bsize)
867 cnt = howmany(*size, sblock->fs_fsize);
870 cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
872 cnt = NINDIR(sblock) * sblock->fs_frag;
874 *size -= NINDIR(sblock) * sblock->fs_bsize;
875 blksout(&idblk[0], cnt, ino);
879 for (i = 0; i < NINDIR(sblock); i++) {
880 dmpindir(ino, idblk[i], ind_level, size);
888 * Collect up the data into tape record sized buffers and output them.
891 blksout(daddr_t *blkp, int frags, ino_t ino)
893 register daddr_t *bp;
894 int i, j, count, blks, tbperdb;
896 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
897 tbperdb = sblock->fs_bsize >> tp_bshift;
898 for (i = 0; i < blks; i += TP_NINDIR) {
899 if (i + TP_NINDIR > blks)
902 count = i + TP_NINDIR;
903 for (j = i; j < count; j++)
904 if (blkp[j / tbperdb] != 0)
905 spcl.c_addr[j - i] = 1;
907 spcl.c_addr[j - i] = 0;
908 spcl.c_count = count - i;
910 bp = &blkp[i / tbperdb];
911 for (j = i; j < count; j += tbperdb, bp++) {
913 if (j + tbperdb <= count)
914 dumpblock(*bp, (int)sblock->fs_bsize);
916 dumpblock(*bp, (count - j) * TP_BSIZE);
919 spcl.c_type = TS_ADDR;
924 * Dump a map to the tape.
927 dumpmap(char *map, int type, ino_t ino)
933 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
935 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
940 * Write a header record to the dump tape.
943 writeheader(ino_t ino)
946 register __s32 sum, cnt, *lp;
948 register int32_t sum, cnt, *lp;
951 spcl.c_inumber = ino;
952 spcl.c_magic = NFS_MAGIC;
957 lp = (int32_t *)&spcl;
961 cnt = sizeof(union u_spcl) / (4 * sizeof(__s32));
963 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
971 spcl.c_checksum = CHECKSUM - sum;
972 writerec((char *)&spcl, 1);
979 static struct dinode dinode;
982 ext2fs_read_inode(fs, inum, (struct ext2_inode *) &dinode);
985 #else /* __linux__ */
989 static daddr_t minino, maxino;
990 static struct dinode inoblock[MAXINOPB];
993 if (inum >= minino && inum < maxino)
994 return (&inoblock[inum - minino]);
995 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
996 (int)sblock->fs_bsize);
997 minino = inum - (inum % INOPB(sblock));
998 maxino = minino + INOPB(sblock);
999 return (&inoblock[inum - minino]);
1001 #endif /* __linux__ */
1004 * Read a chunk of data from the disk.
1005 * Try to recover from hard errors by reading in sector sized pieces.
1006 * Error recovery is attempted at most BREADEMAX times before seeking
1007 * consent from the operator to continue.
1009 int breaderrors = 0;
1010 #define BREADEMAX 32
1013 bread(daddr_t blkno, char *buf, int size)
1020 if (llseek(diskfd, ((ext2_loff_t)blkno << dev_bshift), 0) !=
1021 ((ext2_loff_t)blkno << dev_bshift))
1023 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1024 ((off_t)blkno << dev_bshift))
1026 msg("bread: lseek fails\n");
1027 if ((cnt = read(diskfd, buf, size)) == size)
1029 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1031 * Trying to read the final fragment.
1033 * NB - dump only works in TP_BSIZE blocks, hence
1034 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1035 * It should be smarter about not actually trying to
1036 * read more than it can get, but for the time being
1037 * we punt and scale back the read only when it gets
1038 * us into trouble. (mkm 9/25/83)
1044 msg("read error from %s: %s: [block %d]: count=%d\n",
1045 disk, strerror(errno), blkno, size);
1047 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1048 disk, blkno, size, cnt);
1049 if (++breaderrors > BREADEMAX) {
1050 msg("More than %d block read errors from %d\n",
1052 broadcast("DUMP IS AILING!\n");
1053 msg("This is an unrecoverable error.\n");
1054 if (!query("Do you want to attempt to continue?")){
1061 * Zero buffer, then try to read each sector of buffer separately.
1063 memset(buf, 0, size);
1064 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1066 if (llseek(diskfd, ((ext2_loff_t)blkno << dev_bshift), 0) !=
1067 ((ext2_loff_t)blkno << dev_bshift))
1069 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1070 ((off_t)blkno << dev_bshift))
1072 msg("bread: lseek2 fails!\n");
1073 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1076 msg("read error from %s: %s: [sector %d]: count=%d\n",
1077 disk, strerror(errno), blkno, dev_bsize);
1080 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1081 disk, blkno, dev_bsize, cnt);