Lines Matching refs:fs

111 #define bytblk(fs, n)  ((n) >> (fs)->bshift)
112 #define blkbyt(fs, b) ((b) << (fs)->bshift)
113 #define secblk(fs, s) ((s) >> ((fs)->bshift - SSHIFT))
114 #define blksec(fs, b) ((b) << ((fs)->bshift - SSHIFT))
117 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
120 #define blklsn(fs, b) ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
128 #define okclus(fs, c) ((c) >= LOCLUS && (c) <= (fs)->xclus)
148 dos_read_fatblk(DOS_FS *fs, struct open_file *fd, u_int blknum)
155 max_offset_in_fat = secbyt(fs->spf);
163 err = ioget(fd, fs->lsnfat + bytsec(offset_in_fat),
164 fs->fatbuf, io_size);
166 fs->fatbuf_blknum = ((u_int)(-1));
172 memset(fs->fatbuf + io_size, 0, FATBLKSZ - io_size);
174 fs->fatbuf_blknum = blknum;
182 dos_mount(DOS_FS *fs, struct open_file *fd)
187 bzero(fs, sizeof(DOS_FS));
188 fs->fd = fd;
192 if ((err = ioget(fs->fd, 0, buf, secbyt(1))) ||
193 (err = parsebs(fs, (DOS_BS *)buf))) {
199 if ((fs->fatbuf = malloc(FATBLKSZ)) == NULL)
201 err = dos_read_fatblk(fs, fd, 0);
203 free(fs->fatbuf);
207 fs->root = dot[0];
208 fs->root.name[0] = ' ';
209 if (fs->fatsz == 32) {
210 fs->root.clus[0] = fs->rdcl & 0xff;
211 fs->root.clus[1] = (fs->rdcl >> 8) & 0xff;
212 fs->root.dex.h_clus[0] = (fs->rdcl >> 16) & 0xff;
213 fs->root.dex.h_clus[1] = (fs->rdcl >> 24) & 0xff;
222 dos_unmount(DOS_FS *fs)
224 if (fs->links)
226 free(fs->fatbuf);
227 free(fs);
239 DOS_FS *fs;
244 if ((fs = malloc(sizeof(DOS_FS))) == NULL)
246 if ((err = dos_mount(fs, fd))) {
247 free(fs);
251 if ((err = namede(fs, path, &de))) {
252 dos_unmount(fs);
256 clus = stclus(fs->fatsz, de);
261 (clus && !okclus(fs, clus))) {
262 dos_unmount(fs);
267 dos_unmount(fs);
271 f->fs = fs;
272 fs->links++;
296 if ((size = fsize(f->fs, &f->de)) == -1)
301 if ((clus = stclus(f->fs->fatsz, &f->de)))
302 off &= f->fs->bsize - 1;
309 n = bytblk(f->fs, f->offset);
313 if ((err = fatget(f->fs, &c)))
315 if (!okclus(f->fs, c)) {
320 if (!clus || (n = f->fs->bsize - off) > cnt)
322 if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
323 secbyt(f->fs->lsndir)) + off, buf, n)))
379 DOS_FS *fs = f->fs;
381 f->fs->links--;
383 dos_unmount(fs);
400 if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
498 parsebs(DOS_FS *fs, DOS_BS *bs)
509 if (!(fs->spc = bs->bpb.spc) || fs->spc & (fs->spc - 1))
511 fs->bsize = secbyt(fs->spc);
512 fs->bshift = ffs(fs->bsize) - 1;
513 if ((fs->spf = cv2(bs->bpb.spf))) {
516 if (!(fs->dirents = cv2(bs->bpb.dirents)))
519 if (!(fs->spf = cv4(bs->bpb.lspf)))
523 if ((fs->rdcl = cv4(bs->bpb.rdcl)) < LOCLUS)
526 if (!(fs->lsnfat = cv2(bs->bpb.ressec)))
528 fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.fats;
529 fs->lsndta = fs->lsndir + entsec(fs->dirents);
532 if (fs->lsndta > sc)
534 if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
536 fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
537 sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
538 if (fs->xclus > sc)
539 fs->xclus = sc;
547 namede(DOS_FS *fs, const char *path, DOS_DE **dep)
556 de = &fs->root;
571 if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
582 lookup(DOS_FS *fs, u_int clus, const char *name, DOS_DE **dep)
596 if (!clus && fs->fatsz == 32)
597 clus = fs->rdcl;
598 nsec = !clus ? entsec(fs->dirents) : fs->spc;
603 lsec = fs->lsndir;
604 else if (okclus(fs, clus))
605 lsec = blklsn(fs, clus);
609 if ((err = ioget(fs->fd, lsec + sec, dir, secbyt(1))))
650 if ((err = fatget(fs, &clus)))
652 if (fatend(fs->fatsz, clus))
718 fsize(DOS_FS *fs, DOS_DE *de)
726 size = fs->dirents * sizeof(DOS_DE);
728 if ((n = fatcnt(fs, c)) == -1)
730 size = blkbyt(fs, n);
740 fatcnt(DOS_FS *fs, u_int c)
744 for (n = 0; okclus(fs, c); n++)
745 if (fatget(fs, &c))
747 return (fatend(fs->fatsz, c) ? n : -1);
755 fatget(DOS_FS *fs, u_int *c)
767 offset = fatoff(fs->fatsz, val_in);
768 nbyte = fs->fatsz != 32 ? 2 : 4;
769 if (offset + nbyte > secbyt(fs->spf))
775 if (blknum != fs->fatbuf_blknum) {
776 err = dos_read_fatblk(fs, fs->fd, blknum);
780 p_entry = fs->fatbuf + offset;
783 switch (fs->fatsz) {
818 ioread(DOS_FS *fs, u_int offset, void *buf, size_t nbyte)
830 if ((err = ioget(fs->fd, bytsec(offset), local_buf, sizeof(local_buf))))
839 if ((err = ioget(fs->fd, bytsec(offset), s, nbyte)))
845 if ((err = ioget(fs->fd, bytsec(offset), local_buf, sizeof(local_buf))))