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.2 1999/10/11 12:53:22 stelian Exp $";
50 #include <sys/param.h>
53 #include <linux/ext2_fs.h>
54 #include <bsdcompat.h>
57 #define swab32(x) ext2fs_swab32(x)
60 #include <sys/vnode.h>
63 #include <ufs/fsdir.h>
64 #include <ufs/inode.h>
66 #include <ufs/ufs/dir.h>
67 #include <ufs/ufs/dinode.h>
68 #include <ufs/ffs/fs.h>
70 #endif /* __linux__ */
72 #include <protocols/dumprestore.h>
82 #include <ext2fs/ext2fs.h>
87 #define HASDUMPEDFILE 0x1
88 #define HASSUBDIRS 0x2
91 typedef quad_t fsizeT;
97 static int searchdir __P((struct ext2_dir_entry *dp, int offset,
98 int blocksize, char *buf, void *private));
99 long long llseek(int fildes, long long offset, int whence);
101 static int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
102 static void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
103 static int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
107 * This is an estimation of the number of TP_BSIZE blocks in the file.
108 * It estimates the number of blocks in files with holes by assuming
109 * that all of the blocks accounted for by di_blocks are data blocks
110 * (when some of the blocks are usually used for indirect pointers);
111 * hence the estimate may be high.
115 register 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(maxino, tapesize)
182 register struct dinode *dp;
183 int anydirskipped = 0;
185 for (ino = ROOTINO; ino < maxino; ino++) {
187 if ((mode = (dp->di_mode & IFMT)) == 0)
190 if (dp->di_nlink == 0 || dp->di_dtime != 0)
193 SETINO(ino, usedinomap);
195 SETINO(ino, dumpdirmap);
196 if (WANTTODUMP(dp)) {
197 SETINO(ino, dumpinomap);
198 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
201 *tapesize += blockest(dp);
208 * Restore gets very upset if the root is not dumped,
209 * so ensure that it always is dumped.
211 SETINO(ROOTINO, dumpinomap);
212 return (anydirskipped);
216 struct mapfile_context {
222 mapfilesindir(dirent, offset, blocksize, buf, private)
223 struct ext2_dir_entry *dirent;
229 register struct dinode *dp;
233 struct mapfile_context *mfc;
236 mfc = (struct mapfile_context *)private;
238 if ((mode = (dp->di_mode & IFMT)) != 0 &&
239 dp->di_nlink != 0 && dp->di_dtime == 0) {
240 SETINO(ino, usedinomap);
242 SETINO(ino, dumpdirmap);
243 if (WANTTODUMP(dp)) {
244 SETINO(ino, dumpinomap);
245 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
248 *mfc->tapesize += blockest(dp);
251 mfc->anydirskipped = 1;
252 if ((dirent->name[0] != '.' || dirent->name_len != 1) &&
253 (dirent->name[0] != '.' || dirent->name[1] != '.' ||
254 dirent->name_len != 2)) {
255 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
256 mapfilesindir, private);
268 * Walk the inode list for a filesystem to find all allocated inodes
269 * that have been modified since the previous dump time. Also, find all
270 * the directories in the filesystem.
273 mapfilesfromdir(maxino, tapesize, directory)
279 struct mapfile_context mfc;
281 char dir_name [MAXPATHLEN];
285 * Mark every directory in the path as being dumped
287 for (i = 0; i < strlen (directory); i++) {
288 if (directory[i] == '/') {
289 strncpy (dir_name, directory, i);
291 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
294 com_err(disk, retval, "while translating %s",
298 /* SETINO(dir_ino, dumpinomap); */
299 SETINO(dir_ino, dumpdirmap);
303 * Mark the final directory
305 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
307 com_err(disk, retval, "while translating %s", directory);
310 /* SETINO(dir_ino, dumpinomap); */
311 SETINO(dir_ino, dumpdirmap);
313 mfc.tapesize = tapesize;
314 mfc.anydirskipped = 0;
315 retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
319 com_err(disk, retval, "while mapping files in %s", directory);
323 * Restore gets very upset if the root is not dumped,
324 * so ensure that it always is dumped.
326 /* SETINO(ROOTINO, dumpinomap); */
327 SETINO(ROOTINO, dumpdirmap);
328 return (mfc.anydirskipped);
335 * Scan each directory on the filesystem to see if it has any modified
336 * files in it. If it does, and has not already been added to the dump
337 * list (because it was itself modified), then add it. If a directory
338 * has not been modified itself, contains no modified files and has no
339 * subdirectories, then it can be deleted from the dump list and from
340 * the list of directories. By deleting it from the list of directories,
341 * its parent may now qualify for the same treatment on this or a later
342 * pass using this algorithm.
345 mapdirs(maxino, tapesize)
349 register struct dinode *dp;
359 isdir = 0; /* XXX just to get gcc to shut up */
360 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
361 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
365 if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
370 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &ret);
371 #else /* __linux__ */
372 filesize = dp->di_size;
373 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
374 if (dp->di_db[i] != 0)
375 ret |= searchdir(ino, dp->di_db[i],
376 (long)dblksize(sblock, dp, i),
378 if (ret & HASDUMPEDFILE)
381 filesize -= sblock->fs_bsize;
383 for (i = 0; filesize > 0 && i < NIADDR; i++) {
384 if (dp->di_ib[i] == 0)
386 ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
388 #endif /* __linux__ */
389 if (ret & HASDUMPEDFILE) {
390 SETINO(ino, dumpinomap);
391 *tapesize += blockest(dp);
395 if ((ret & HASSUBDIRS) == 0) {
396 if (!TSTINO(ino, dumpinomap)) {
397 CLRINO(ino, dumpdirmap);
407 * Read indirect blocks, and pass the data blocks to be searched
408 * as directories. Quit as soon as any entry is found that will
409 * require the directory to be dumped.
412 dirindir(ino, blkno, ind_level, filesize)
420 daddr_t idblk[MAXNINDIR];
422 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
423 if (ind_level <= 0) {
424 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
427 ret |= searchdir(ino, blkno, sblock->fs_bsize,
429 if (ret & HASDUMPEDFILE)
432 *filesize -= sblock->fs_bsize;
437 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
440 ret |= dirindir(ino, blkno, ind_level, filesize);
444 #endif /* !__linux__ */
447 * Scan a disk block containing directory information looking to see if
448 * any of the entries are on the dump list and to see if the directory
449 * contains any subdirectories.
453 searchdir(dp, offset, blocksize, buf, private)
454 struct ext2_dir_entry *dp;
460 int *ret = (int *) private;
464 if (dp->name[0] == '.') {
465 if (dp->name_len == 1)
467 if (dp->name[1] == '.' && dp->name_len == 2)
470 if (TSTINO(dp->inode, dumpinomap)) {
471 *ret |= HASDUMPEDFILE;
472 if (*ret & HASSUBDIRS)
475 if (TSTINO(dp->inode, dumpdirmap)) {
477 if (*ret & HASDUMPEDFILE)
483 #else /* __linux__ */
486 searchdir(ino, blkno, size, filesize)
492 register struct direct *dp;
493 register long loc, ret = 0;
496 bread(fsbtodb(sblock, blkno), dblk, (int)size);
499 for (loc = 0; loc < size; ) {
500 dp = (struct direct *)(dblk + loc);
501 if (dp->d_reclen == 0) {
502 msg("corrupted directory, inumber %d\n", ino);
508 if (dp->d_name[0] == '.') {
509 if (dp->d_name[1] == '\0')
511 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
514 if (TSTINO(dp->d_ino, dumpinomap)) {
515 ret |= HASDUMPEDFILE;
516 if (ret & HASSUBDIRS)
519 if (TSTINO(dp->d_ino, dumpdirmap)) {
521 if (ret & HASDUMPEDFILE)
527 #endif /* __linux__ */
531 struct block_context {
540 * Dump a block to the tape
543 dumponeblock(ext2_filsys fs, blk_t *blocknr, int blockcnt, void *private)
545 struct block_context *p;
548 if (blockcnt < NDADDR)
550 p = (struct block_context *)private;
551 for (i = p->next_block; i < blockcnt; i++) {
552 p->buf[p->cnt++] = 0;
553 if (p->cnt == p->max) {
554 blksout (p->buf, p->cnt, p->ino);
558 p->buf[p->cnt++] = *blocknr;
559 if (p->cnt == p->max) {
560 blksout (p->buf, p->cnt, p->ino);
563 p->next_block = blockcnt + 1;
569 * Dump passes 3 and 4.
571 * Dump the contents of an inode to tape.
575 register struct dinode *dp;
581 struct old_bsd_inode obi;
583 struct block_context bc;
590 dumpmap(dumpinomap, TS_BITS, ino);
592 CLRINO(ino, dumpinomap);
594 memset(&obi, 0, sizeof(obi));
595 obi.di_mode = dp->di_mode;
596 obi.di_uid = dp->di_uid;
597 obi.di_gid = dp->di_gid;
598 obi.di_qsize.v = (u_quad_t)dp->di_size;
599 obi.di_atime = dp->di_atime;
600 obi.di_mtime = dp->di_mtime;
601 obi.di_ctime = dp->di_ctime;
602 obi.di_nlink = dp->di_nlink;
603 obi.di_blocks = dp->di_blocks;
604 obi.di_flags = dp->di_flags;
605 obi.di_gen = dp->di_gen;
606 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
607 if (dp->di_file_acl || dp->di_dir_acl)
608 warn("ACLs in inode #%d won't be dumped", ino);
609 memmove(&spcl.c_dinode, &obi, sizeof(obi));
610 #else /* __linux__ */
612 #endif /* __linux__ */
613 spcl.c_type = TS_INODE;
615 switch (dp->di_mode & S_IFMT) {
625 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
631 * Check for short symbolic link.
634 if (dp->di_size > 0 &&
635 dp->di_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
639 memmove(buf, dp->di_db, (u_long)dp->di_size);
640 buf[dp->di_size] = '\0';
644 #endif /* __linux__ */
646 if (dp->di_size > 0 &&
647 dp->di_size < sblock->fs_maxsymlinklen) {
651 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
652 buf[dp->di_size] = '\0';
675 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
678 if (dp->di_size > NDADDR * sblock->fs_bsize)
680 cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super);
682 cnt = NDADDR * sblock->fs_frag;
685 cnt = howmany(dp->di_size, sblock->fs_fsize);
686 blksout(&dp->di_db[0], cnt, ino);
687 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
690 bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
691 bc.buf = (int *)malloc (bc.max * sizeof (int));
694 bc.next_block = NDADDR;
696 ext2fs_block_iterate (fs, ino, 0, NULL, dumponeblock, (void *)&bc);
698 blksout (bc.buf, bc.cnt, bc.ino);
701 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
702 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
711 struct convert_dir_context {
719 * This function converts an ext2fs directory entry to the BSD format.
721 * Basically, it adds a null-character at the end of the name, recomputes the
722 * size of the entry, and creates it in a temporary buffer
725 convert_dir(dirent, offset, blocksize, buf, private)
726 struct ext2_dir_entry *dirent;
732 struct convert_dir_context *p;
736 p = (struct convert_dir_context *)private;
738 reclen = EXT2_DIR_REC_LEN(dirent->name_len + 1);
739 if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
740 dp = (struct direct *)(p->buf + p->prev_offset);
741 dp->d_reclen += p->bs - (p->offset % p->bs);
742 p->offset += p->bs - (p->offset % p->bs);
745 dp = (struct direct *)(p->buf + p->offset);
746 dp->d_ino = dirent->inode;
747 dp->d_reclen = reclen;
749 dp->d_namlen = dirent->name_len;
750 strncpy(dp->d_name, dirent->name, dirent->name_len);
751 dp->d_name[dp->d_namlen] = '\0';
752 p->prev_offset = p->offset;
761 * Dumps a directory to tape after converting it to the BSD format
765 register struct dinode *dp;
770 struct old_bsd_inode obi;
771 struct convert_dir_context cdc;
773 struct ext2_dir_entry *de;
778 dumpmap(dumpinomap, TS_BITS, ino);
780 CLRINO(ino, dumpinomap);
783 * Convert the directory to the BSD format
785 /* Allocate a buffer for the conversion (twice the size of the
786 ext2fs directory to avoid problems ;-) */
787 cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
789 err(1, "Cannot allocate buffer to convert directory #%ld\n",
793 cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
794 /* Do the conversion */
795 retval = ext2fs_dir_iterate(fs, ino, 0, NULL, convert_dir, (void *)&cdc);
797 com_err(disk, retval, "while converting directory #%ld\n", ino);
800 /* Fix the last entry */
801 if ((cdc.offset % cdc.bs) != 0) {
802 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
803 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
804 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
807 dir_size = cdc.offset;
810 memset(&obi, 0, sizeof(obi));
811 obi.di_mode = dp->di_mode;
812 obi.di_uid = dp->di_uid;
813 obi.di_gid = dp->di_gid;
814 obi.di_qsize.v = dir_size; /* (u_quad_t)dp->di_size; */
815 obi.di_atime = dp->di_atime;
816 obi.di_mtime = dp->di_mtime;
817 obi.di_ctime = dp->di_ctime;
818 obi.di_nlink = dp->di_nlink;
819 obi.di_blocks = dp->di_blocks;
820 obi.di_flags = dp->di_flags;
821 obi.di_gen = dp->di_gen;
822 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
823 if (dp->di_file_acl || dp->di_dir_acl)
824 warn("ACLs in inode #%d won't be dumped", ino);
825 memmove(&spcl.c_dinode, &obi, sizeof(obi));
826 #else /* __linux__ */
828 #endif /* __linux__ */
829 spcl.c_type = TS_INODE;
831 switch (dp->di_mode & S_IFMT) {
842 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
847 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
848 dp->di_mode & IFMT, ino);
851 for (size = 0; size < dir_size; size += TP_BSIZE) {
855 memmove(buf, cdc.buf + size, TP_BSIZE);
857 spcl.c_type = TS_ADDR;
862 #endif /* __linux__ */
866 * Read indirect blocks, and pass the data blocks to be dumped.
869 dmpindir(ino, blk, ind_level, size)
880 daddr_t idblk[MAXNINDIR];
883 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
886 * My RedHat 4.0 system doesn't have these flags; I haven't
887 * upgraded e2fsprogs yet
889 #if defined(EXT2_FLAG_SWAP_BYTES)
890 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
891 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) {
893 max = sblock->fs_bsize >> 2;
894 swapme = (blk_t *) idblk;
895 for (i = 0; i < max; i++, swapme++)
896 *swapme = swab32(*swapme);
897 #if defined(EXT2_FLAG_SWAP_BYTES)
902 memset(idblk, 0, (int)sblock->fs_bsize);
903 if (ind_level <= 0) {
904 if (*size < NINDIR(sblock) * sblock->fs_bsize)
905 cnt = howmany(*size, sblock->fs_fsize);
908 cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
910 cnt = NINDIR(sblock) * sblock->fs_frag;
912 *size -= NINDIR(sblock) * sblock->fs_bsize;
913 blksout(&idblk[0], cnt, ino);
917 for (i = 0; i < NINDIR(sblock); i++) {
918 dmpindir(ino, idblk[i], ind_level, size);
926 * Collect up the data into tape record sized buffers and output them.
929 blksout(blkp, frags, ino)
934 register daddr_t *bp;
935 int i, j, count, blks, tbperdb;
937 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
938 tbperdb = sblock->fs_bsize >> tp_bshift;
939 for (i = 0; i < blks; i += TP_NINDIR) {
940 if (i + TP_NINDIR > blks)
943 count = i + TP_NINDIR;
944 for (j = i; j < count; j++)
945 if (blkp[j / tbperdb] != 0)
946 spcl.c_addr[j - i] = 1;
948 spcl.c_addr[j - i] = 0;
949 spcl.c_count = count - i;
951 bp = &blkp[i / tbperdb];
952 for (j = i; j < count; j += tbperdb, bp++)
954 if (j + tbperdb <= count)
955 dumpblock(*bp, (int)sblock->fs_bsize);
957 dumpblock(*bp, (count - j) * TP_BSIZE);
958 spcl.c_type = TS_ADDR;
963 * Dump a map to the tape.
966 dumpmap(map, type, ino)
975 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
977 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
982 * Write a header record to the dump tape.
989 register __s32 sum, cnt, *lp;
991 register int32_t sum, cnt, *lp;
994 spcl.c_inumber = ino;
995 spcl.c_magic = NFS_MAGIC;
1000 lp = (int32_t *)&spcl;
1004 cnt = sizeof(union u_spcl) / (4 * sizeof(__s32));
1006 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
1008 while (--cnt >= 0) {
1014 spcl.c_checksum = CHECKSUM - sum;
1015 writerec((char *)&spcl, 1);
1023 static struct dinode dinode;
1026 ext2fs_read_inode(fs, inum, (struct ext2_inode *) &dinode);
1029 #else /* __linux__ */
1034 static daddr_t minino, maxino;
1035 static struct dinode inoblock[MAXINOPB];
1038 if (inum >= minino && inum < maxino)
1039 return (&inoblock[inum - minino]);
1040 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
1041 (int)sblock->fs_bsize);
1042 minino = inum - (inum % INOPB(sblock));
1043 maxino = minino + INOPB(sblock);
1044 return (&inoblock[inum - minino]);
1046 #endif /* __linux__ */
1049 * Read a chunk of data from the disk.
1050 * Try to recover from hard errors by reading in sector sized pieces.
1051 * Error recovery is attempted at most BREADEMAX times before seeking
1052 * consent from the operator to continue.
1054 int breaderrors = 0;
1055 #define BREADEMAX 32
1058 bread(blkno, buf, size)
1068 if (llseek(diskfd, ((ext2_loff_t)blkno << dev_bshift), 0) !=
1069 ((ext2_loff_t)blkno << dev_bshift))
1071 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1072 ((off_t)blkno << dev_bshift))
1074 msg("bread: lseek fails\n");
1075 if ((cnt = read(diskfd, buf, size)) == size)
1077 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1079 * Trying to read the final fragment.
1081 * NB - dump only works in TP_BSIZE blocks, hence
1082 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1083 * It should be smarter about not actually trying to
1084 * read more than it can get, but for the time being
1085 * we punt and scale back the read only when it gets
1086 * us into trouble. (mkm 9/25/83)
1092 msg("read error from %s: %s: [block %d]: count=%d\n",
1093 disk, strerror(errno), blkno, size);
1095 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1096 disk, blkno, size, cnt);
1097 if (++breaderrors > BREADEMAX) {
1098 msg("More than %d block read errors from %d\n",
1100 broadcast("DUMP IS AILING!\n");
1101 msg("This is an unrecoverable error.\n");
1102 if (!query("Do you want to attempt to continue?")){
1109 * Zero buffer, then try to read each sector of buffer separately.
1111 memset(buf, 0, size);
1112 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1114 if (llseek(diskfd, ((ext2_loff_t)blkno << dev_bshift), 0) !=
1115 ((ext2_loff_t)blkno << dev_bshift))
1117 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1118 ((off_t)blkno << dev_bshift))
1120 msg("bread: lseek2 fails!\n");
1121 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1124 msg("read error from %s: %s: [sector %d]: count=%d\n",
1125 disk, strerror(errno), blkno, dev_bsize);
1128 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1129 disk, blkno, dev_bsize, cnt);