+
+chkuse(blkno, cnt)
+ daddr_t blkno;
+ int cnt;
+{
+ int cg;
+ daddr_t fsbn, bn;
+
+ fsbn = dbtofsb(fs, blkno);
+ if ((unsigned)(fsbn+cnt) > fs->fs_size) {
+ printf("block %d out of range of file system\n", blkno);
+ return (1);
+ }
+ cg = dtog(fs, fsbn);
+ if (fsbn < cgdmin(fs, cg)) {
+ if (cg == 0 || (fsbn+cnt) > cgsblock(fs, cg)) {
+ printf("block %d in non-data area: cannot attach\n",
+ blkno);
+ return (1);
+ }
+ } else {
+ if ((fsbn+cnt) > cgbase(fs, cg+1)) {
+ printf("block %d in non-data area: cannot attach\n",
+ blkno);
+ return (1);
+ }
+ }
+ rdfs(fsbtodb(fs, cgtod(fs, cg)), (int)sblock.fs_cgsize,
+ (char *)&acg);
+ if (acg.cg_magic != CG_MAGIC) {
+ fprintf(stderr, "cg %d: bad magic number\n", cg);
+ errs++;
+ return;
+ }
+ bn = dtogd(fs, fsbn);
+ if (isclr(acg.cg_free, bn))
+ printf("Warning: sector %d is in use\n", blkno);
+ return (0);
+}
+
+/*
+ * read a block from the file system
+ */
+rdfs(bno, size, bf)
+ int bno, size;
+ char *bf;
+{
+ int n;
+
+ if (lseek(fsi, bno * DEV_BSIZE, 0) < 0) {
+ printf("seek error: %ld\n", bno);
+ perror("rdfs");
+ exit(1);
+ }
+ n = read(fsi, bf, size);
+ if(n != size) {
+ printf("read error: %ld\n", bno);
+ perror("rdfs");
+ exit(1);
+ }
+}