Home | History | Annotate | Line # | Download | only in libsa
dosfs.c revision 1.1
      1 /*	$NetBSD: dosfs.c,v 1.1 2000/11/02 00:25:05 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1998 Robert Nordier
      5  * 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
     14  *    the documentation and/or other materials provided with the
     15  *    distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Readonly filesystem for Microsoft FAT12/FAT16/FAT32 filesystems,
     32  * also supports VFAT.
     33  */
     34 
     35 /*
     36  * XXX DOES NOT SUPPORT:
     37  *
     38  *	LIBSA_FS_SINGLECOMPONENT
     39  */
     40 
     41 #include <sys/param.h>
     42 
     43 #include <msdosfs/bpb.h>
     44 #include <msdosfs/direntry.h>
     45 
     46 #ifdef _STANDALONE
     47 #include <lib/libkern/libkern.h>
     48 #else
     49 #include <string.h>
     50 #include <stddef.h>
     51 #endif
     52 
     53 #include "stand.h"
     54 #include "dosfs.h"
     55 
     56 #define SECSIZ  512		/* sector size */
     57 #define SSHIFT    9		/* SECSIZ shift */
     58 #define DEPSEC   16		/* directory entries per sector */
     59 #define DSHIFT    4		/* DEPSEC shift */
     60 #define LOCLUS    2		/* lowest cluster number */
     61 
     62 typedef union {
     63 	struct direntry de;	/* standard directory entry */
     64 	struct winentry xde;	/* extended directory entry */
     65 } DOS_DIR;
     66 
     67 typedef struct {
     68 	struct open_file *fd;	/* file descriptor */
     69 	u_char *buf;		/* buffer */
     70 	u_int   bufsec;		/* buffered sector */
     71 	u_int   links;		/* active links to structure */
     72 	u_int   spc;		/* sectors per cluster */
     73 	u_int   bsize;		/* cluster size in bytes */
     74 	u_int   bshift;		/* cluster conversion shift */
     75 	u_int   dirents;	/* root directory entries */
     76 	u_int   spf;		/* sectors per fat */
     77 	u_int   rdcl;		/* root directory start cluster */
     78 	u_int   lsnfat;		/* start of fat */
     79 	u_int   lsndir;		/* start of root dir */
     80 	u_int   lsndta;		/* start of data area */
     81 	u_int   fatsz;		/* FAT entry size */
     82 	u_int   xclus;		/* maximum cluster number */
     83 } DOS_FS;
     84 
     85 typedef struct {
     86 	DOS_FS *fs;		/* associated filesystem */
     87 	struct direntry de;	/* directory entry */
     88 	u_int   offset;		/* current offset */
     89 	u_int   c;		/* last cluster read */
     90 } DOS_FILE;
     91 
     92 /* Initial portion of DOS boot sector */
     93 typedef struct {
     94 	u_char  jmp[3];		/* usually 80x86 'jmp' opcode */
     95 	u_char  oem[8];		/* OEM name and version */
     96 	struct byte_bpb710 bpb;	/* BPB */
     97 } DOS_BS;
     98 
     99 /* Supply missing "." and ".." root directory entries */
    100 static const char *const dotstr[2] = {".", ".."};
    101 static struct direntry dot[2] = {
    102 	{".       ", "   ", ATTR_DIRECTORY,
    103 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
    104 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}},
    105 
    106 	{"..      ", "   ", ATTR_DIRECTORY,
    107 		0, 0, {0, 0}, {0, 0}, {0, 0}, {0, 0},
    108 		{0, 0}, {0x21, 0}, {0, 0}, {0, 0, 0, 0}}
    109 };
    110 
    111 /* The usual conversion macros to avoid multiplication and division */
    112 #define bytsec(n)      ((n) >> SSHIFT)
    113 #define secbyt(s)      ((s) << SSHIFT)
    114 #define entsec(e)      ((e) >> DSHIFT)
    115 #define bytblk(fs, n)  ((n) >> (fs)->bshift)
    116 #define blkbyt(fs, b)  ((b) << (fs)->bshift)
    117 #define secblk(fs, s)  ((s) >> ((fs)->bshift - SSHIFT))
    118 #define blksec(fs, b)  ((b) << ((fs)->bshift - SSHIFT))
    119 
    120 /* Convert cluster number to offset within filesystem */
    121 #define blkoff(fs, b) (secbyt((fs)->lsndta) + blkbyt(fs, (b) - LOCLUS))
    122 
    123 /* Convert cluster number to logical sector number */
    124 #define blklsn(fs, b)  ((fs)->lsndta + blksec(fs, (b) - LOCLUS))
    125 
    126 /* Convert cluster number to offset within FAT */
    127 #define fatoff(sz, c)  ((sz) == 12 ? (c) + ((c) >> 1) :  \
    128                         (sz) == 16 ? (c) << 1 :          \
    129 			(c) << 2)
    130 
    131 /* Does cluster number reference a valid data cluster? */
    132 #define okclus(fs, c)  ((c) >= LOCLUS && (c) <= (fs)->xclus)
    133 
    134 /* Get start cluster from directory entry */
    135 #define stclus(sz, de)  ((sz) != 32 ? getushort((de)->deStartCluster) : \
    136                          ((u_int)getushort((de)->deHighClust) << 16) |  \
    137 			 getushort((de)->deStartCluster))
    138 
    139 static int dosunmount(DOS_FS *);
    140 static int parsebs(DOS_FS *, DOS_BS *);
    141 static int namede(DOS_FS *, const char *, struct direntry **);
    142 static int lookup(DOS_FS *, u_int, const char *, struct direntry **);
    143 static void cp_xdnm(u_char *, struct winentry *);
    144 static void cp_sfn(u_char *, struct direntry *);
    145 static off_t fsize(DOS_FS *, struct direntry *);
    146 static int fatcnt(DOS_FS *, u_int);
    147 static int fatget(DOS_FS *, u_int *);
    148 static int fatend(u_int, u_int);
    149 static int ioread(DOS_FS *, u_int, void *, u_int);
    150 static int iobuf(DOS_FS *, u_int);
    151 static int ioget(struct open_file *, u_int, void *, u_int);
    152 
    153 /*
    154  * Mount DOS filesystem
    155  */
    156 static int
    157 dos_mount(DOS_FS * fs, struct open_file * fd)
    158 {
    159 	int     err;
    160 
    161 	bzero(fs, sizeof(DOS_FS));
    162 	fs->fd = fd;
    163 	if ((err = !(fs->buf = alloc(SECSIZ)) ? errno : 0) ||
    164 	    (err = ioget(fs->fd, 0, fs->buf, 1)) ||
    165 	    (err = parsebs(fs, (DOS_BS *) fs->buf))) {
    166 		(void) dosunmount(fs);
    167 		return (err);
    168 	}
    169 	return 0;
    170 }
    171 
    172 /*
    173  * Unmount mounted filesystem
    174  */
    175 static int
    176 dos_unmount(DOS_FS * fs)
    177 {
    178 	int     err;
    179 
    180 	if (fs->links)
    181 		return (EBUSY);
    182 	if ((err = dosunmount(fs)))
    183 		return (err);
    184 	return 0;
    185 }
    186 
    187 /*
    188  * Common code shared by dos_mount() and dos_unmount()
    189  */
    190 static int
    191 dosunmount(DOS_FS * fs)
    192 {
    193 	if (fs->buf)
    194 		free(fs->buf, SECSIZ);
    195 	free(fs, sizeof(DOS_FS));
    196 	return (0);
    197 }
    198 
    199 /*
    200  * Open DOS file
    201  */
    202 int
    203 dosfs_open(char *path, struct open_file *fd)
    204 {
    205 	struct direntry *de;
    206 	DOS_FILE *f;
    207 	DOS_FS *fs;
    208 	u_int   size, clus;
    209 	int     err = 0;
    210 
    211 	/* Allocate mount structure, associate with open */
    212 	fs = alloc(sizeof(DOS_FS));
    213 
    214 	if ((err = dos_mount(fs, fd)))
    215 		goto out;
    216 
    217 	if ((err = namede(fs, path, &de)))
    218 		goto out;
    219 
    220 	clus = stclus(fs->fatsz, de);
    221 	size = getulong(de->deFileSize);
    222 
    223 	if ((!(de->deAttributes & ATTR_DIRECTORY) && (!clus != !size)) ||
    224 	    ((de->deAttributes & ATTR_DIRECTORY) && size) ||
    225 	    (clus && !okclus(fs, clus))) {
    226 		err = EINVAL;
    227 		goto out;
    228 	}
    229 	f = alloc(sizeof(DOS_FILE));
    230 	bzero(f, sizeof(DOS_FILE));
    231 	f->fs = fs;
    232 	fs->links++;
    233 	f->de = *de;
    234 	fd->f_fsdata = (void *) f;
    235 
    236 out:
    237 	return (err);
    238 }
    239 
    240 /*
    241  * Read from file
    242  */
    243 int
    244 dosfs_read(struct open_file * fd, void *buf, size_t nbyte, size_t * resid)
    245 {
    246 	off_t   size;
    247 	u_int   nb, off, clus, c, cnt, n;
    248 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    249 	int     err = 0;
    250 
    251 	nb = (u_int) nbyte;
    252 	if ((size = fsize(f->fs, &f->de)) == -1)
    253 		return EINVAL;
    254 	if (nb > (n = size - f->offset))
    255 		nb = n;
    256 	off = f->offset;
    257 	if ((clus = stclus(f->fs->fatsz, &f->de)))
    258 		off &= f->fs->bsize - 1;
    259 	c = f->c;
    260 	cnt = nb;
    261 	while (cnt) {
    262 		n = 0;
    263 		if (!c) {
    264 			if ((c = clus))
    265 				n = bytblk(f->fs, f->offset);
    266 		} else if (!off)
    267 			n++;
    268 		while (n--) {
    269 			if ((err = fatget(f->fs, &c)))
    270 				goto out;
    271 			if (!okclus(f->fs, c)) {
    272 				err = EINVAL;
    273 				goto out;
    274 			}
    275 		}
    276 		if (!clus || (n = f->fs->bsize - off) > cnt)
    277 			n = cnt;
    278 		if ((err = ioread(f->fs, (c ? blkoff(f->fs, c) :
    279 				secbyt(f->fs->lsndir)) + off,
    280 			    buf, n)))
    281 			goto out;
    282 		f->offset += n;
    283 		f->c = c;
    284 		off = 0;
    285 		buf += n;
    286 		cnt -= n;
    287 	}
    288 out:
    289 	if (resid)
    290 		*resid = nbyte - nb + cnt;
    291 	return (err);
    292 }
    293 
    294 #ifndef LIBSA_NO_FS_WRITE
    295 /*
    296  * Not implemented.
    297  */
    298 int
    299 dosfs_write(struct open_file *fd, void *start, size_t size, size_t *resid)
    300 {
    301 
    302 	return (EROFS);
    303 }
    304 #endif /* !LIBSA_NO_FS_WRITE */
    305 
    306 #ifndef LIBSA_NO_FS_SEEK
    307 /*
    308  * Reposition within file
    309  */
    310 off_t
    311 dosfs_seek(struct open_file * fd, off_t offset, int whence)
    312 {
    313 	off_t   off;
    314 	u_int   size;
    315 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    316 
    317 	size = getulong(f->de.deFileSize);
    318 	switch (whence) {
    319 	case SEEK_SET:
    320 		off = 0;
    321 		break;
    322 	case SEEK_CUR:
    323 		off = f->offset;
    324 		break;
    325 	case SEEK_END:
    326 		off = size;
    327 		break;
    328 	default:
    329 		return (-1);
    330 	}
    331 	off += offset;
    332 	if (off < 0 || off > size)
    333 		return (-1);
    334 	f->offset = (u_int) off;
    335 	f->c = 0;
    336 	return (off);
    337 }
    338 #endif /* !LIBSA_NO_FS_SEEK */
    339 
    340 #ifndef LIBSA_NO_FS_CLOSE
    341 /*
    342  * Close open file
    343  */
    344 int
    345 dosfs_close(struct open_file * fd)
    346 {
    347 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    348 	DOS_FS *fs = f->fs;
    349 
    350 	f->fs->links--;
    351 	free(f, sizeof(DOS_FILE));
    352 	dos_unmount(fs);
    353 	return 0;
    354 }
    355 #endif /* !LIBSA_NO_FS_CLOSE */
    356 
    357 /*
    358  * Return some stat information on a file.
    359  */
    360 int
    361 dosfs_stat(struct open_file * fd, struct stat * sb)
    362 {
    363 	DOS_FILE *f = (DOS_FILE *) fd->f_fsdata;
    364 
    365 	/* only important stuff */
    366 	sb->st_mode = (f->de.deAttributes & ATTR_DIRECTORY) ?
    367 	    (S_IFDIR | 0555) : (S_IFREG | 0444);
    368 	sb->st_nlink = 1;
    369 	sb->st_uid = 0;
    370 	sb->st_gid = 0;
    371 	if ((sb->st_size = fsize(f->fs, &f->de)) == -1)
    372 		return EINVAL;
    373 	return (0);
    374 }
    375 
    376 /*
    377  * Parse DOS boot sector
    378  */
    379 static int
    380 parsebs(DOS_FS * fs, DOS_BS * bs)
    381 {
    382 	u_int   sc;
    383 
    384 	if ((bs->jmp[0] != 0x69 &&
    385 		bs->jmp[0] != 0xe9 &&
    386 		(bs->jmp[0] != 0xeb || bs->jmp[2] != 0x90)) ||
    387 	    bs->bpb.bpbMedia < 0xf0)
    388 		return EINVAL;
    389 	if (getushort(bs->bpb.bpbBytesPerSec) != SECSIZ)
    390 		return EINVAL;
    391 	if (!(fs->spc = bs->bpb.bpbSecPerClust) || fs->spc & (fs->spc - 1))
    392 		return EINVAL;
    393 	fs->bsize = secbyt(fs->spc);
    394 	fs->bshift = ffs(fs->bsize) - 1;
    395 	if ((fs->spf = getushort(bs->bpb.bpbFATsecs))) {
    396 		if (bs->bpb.bpbFATs != 2)
    397 			return EINVAL;
    398 		if (!(fs->dirents = getushort(bs->bpb.bpbRootDirEnts)))
    399 			return EINVAL;
    400 	} else {
    401 		if (!(fs->spf = getulong(bs->bpb.bpbBigFATsecs)))
    402 			return EINVAL;
    403 		if (!bs->bpb.bpbFATs || bs->bpb.bpbFATs > 16)
    404 			return EINVAL;
    405 		if ((fs->rdcl = getulong(bs->bpb.bpbRootClust)) < LOCLUS)
    406 			return EINVAL;
    407 	}
    408 	if (!(fs->lsnfat = getushort(bs->bpb.bpbResSectors)))
    409 		return EINVAL;
    410 	fs->lsndir = fs->lsnfat + fs->spf * bs->bpb.bpbFATs;
    411 	fs->lsndta = fs->lsndir + entsec(fs->dirents);
    412 	if (!(sc = getushort(bs->bpb.bpbSectors)) &&
    413 	    !(sc = getulong(bs->bpb.bpbHugeSectors)))
    414 		return EINVAL;
    415 	if (fs->lsndta > sc)
    416 		return EINVAL;
    417 	if ((fs->xclus = secblk(fs, sc - fs->lsndta) + 1) < LOCLUS)
    418 		return EINVAL;
    419 	fs->fatsz = fs->dirents ? fs->xclus < 0xff6 ? 12 : 16 : 32;
    420 	sc = (secbyt(fs->spf) << 1) / (fs->fatsz >> 2) - 1;
    421 	if (fs->xclus > sc)
    422 		fs->xclus = sc;
    423 	return 0;
    424 }
    425 
    426 /*
    427  * Return directory entry from path
    428  */
    429 static int
    430 namede(DOS_FS * fs, const char *path, struct direntry ** dep)
    431 {
    432 	char    name[256];
    433 	struct direntry *de;
    434 	char   *s;
    435 	size_t  n;
    436 	int     err;
    437 
    438 	err = 0;
    439 	de = dot;
    440 	if (*path == '/')
    441 		path++;
    442 	while (*path) {
    443 		if (!(s = strchr(path, '/')))
    444 			s = strchr(path, 0);
    445 		if ((n = s - path) > 255)
    446 			return ENAMETOOLONG;
    447 		memcpy(name, path, n);
    448 		name[n] = 0;
    449 		path = s;
    450 		if (!(de->deAttributes & ATTR_DIRECTORY))
    451 			return ENOTDIR;
    452 		if ((err = lookup(fs, stclus(fs->fatsz, de), name, &de)))
    453 			return err;
    454 		if (*path == '/')
    455 			path++;
    456 	}
    457 	*dep = de;
    458 	return 0;
    459 }
    460 
    461 /*
    462  * Lookup path segment
    463  */
    464 static int
    465 lookup(DOS_FS * fs, u_int clus, const char *name, struct direntry ** dep)
    466 {
    467 	DOS_DIR *dir;
    468 	u_char  lfn[261];
    469 	u_char  sfn[13];
    470 	u_int   nsec, lsec, xdn, chk, sec, ent, x;
    471 	int     err = 0, ok, i;
    472 
    473 	if (!clus)
    474 		for (ent = 0; ent < 2; ent++)
    475 			if (!strcasecmp(name, dotstr[ent])) {
    476 				*dep = dot + ent;
    477 				return 0;
    478 			}
    479 
    480 	dir = alloc(sizeof(DOS_DIR) * DEPSEC);
    481 
    482 	if (!clus && fs->fatsz == 32)
    483 		clus = fs->rdcl;
    484 	nsec = !clus ? entsec(fs->dirents) : fs->spc;
    485 	lsec = 0;
    486 	xdn = chk = 0;
    487 	for (;;) {
    488 		if (!clus && !lsec)
    489 			lsec = fs->lsndir;
    490 		else if (okclus(fs, clus))
    491 			lsec = blklsn(fs, clus);
    492 		else {
    493 			err = EINVAL;
    494 			goto out;
    495 		}
    496 		for (sec = 0; sec < nsec; sec++) {
    497 			if ((err = ioget(fs->fd, lsec + sec, dir, 1)))
    498 				goto out;
    499 			for (ent = 0; ent < DEPSEC; ent++) {
    500 				if (!*dir[ent].de.deName) {
    501 					err = ENOENT;
    502 					goto out;
    503 				}
    504 				if (*dir[ent].de.deName != 0xe5) {
    505 					if (dir[ent].de.deAttributes ==
    506 					    ATTR_WIN95) {
    507 						x = dir[ent].xde.weCnt;
    508 						if (x & WIN_LAST ||
    509 						    (x + 1 == xdn &&
    510 						     dir[ent].xde.weChksum ==
    511 						     chk)) {
    512 							if (x & WIN_LAST) {
    513 								chk = dir[ent].xde.weChksum;
    514 								x &= WIN_CNT;
    515 							}
    516 							if (x >= 1 && x <= 20) {
    517 								cp_xdnm(lfn, &dir[ent].xde);
    518 								xdn = x;
    519 								continue;
    520 							}
    521 						}
    522 					} else if (!(dir[ent].de.deAttributes &
    523 						     ATTR_VOLUME)) {
    524 						if ((ok = xdn == 1)) {
    525 							for (x = 0, i = 0;
    526 							     i < 11; i++)
    527 								x = ((((x & 1) << 7) | (x >> 1)) +
    528 								    dir[ent].de.deName[i]) & 0xff;
    529 							ok = chk == x &&
    530 							    !strcasecmp(name, (const char *) lfn);
    531 						}
    532 						if (!ok) {
    533 							cp_sfn(sfn, &dir[ent].de);
    534 							ok = !strcasecmp(name, (const char *) sfn);
    535 						}
    536 						if (ok) {
    537 							*dep = &dir[ent].de;
    538 							goto out;
    539 						}
    540 					}
    541 				}
    542 				xdn = 0;
    543 			}
    544 		}
    545 		if (!clus)
    546 			break;
    547 		if ((err = fatget(fs, &clus)))
    548 			goto out;
    549 		if (fatend(fs->fatsz, clus))
    550 			break;
    551 	}
    552 	err = ENOENT;
    553  out:
    554 	free(dir, sizeof(DOS_DIR) * DEPSEC);
    555 	return (err);
    556 }
    557 
    558 /*
    559  * Copy name from extended directory entry
    560  */
    561 static void
    562 cp_xdnm(u_char * lfn, struct winentry * xde)
    563 {
    564 	static const struct {
    565 		u_int   off;
    566 		u_int   dim;
    567 	} ix[3] = {
    568 		{ offsetof(struct winentry, wePart1),
    569 		    sizeof(xde->wePart1) / 2 },
    570 		{ offsetof(struct winentry, wePart2),
    571 		    sizeof(xde->wePart2) / 2 },
    572 		{ offsetof(struct winentry, wePart3),
    573 		    sizeof(xde->wePart3) / 2 }
    574 	};
    575 	u_char *p;
    576 	u_int   n, x, c;
    577 
    578 	lfn += 13 * ((xde->weCnt & WIN_CNT) - 1);
    579 	for (n = 0; n < 3; n++)
    580 		for (p = (u_char *) xde + ix[n].off, x = ix[n].dim; x;
    581 		    p += 2, x--) {
    582 			if ((c = getushort(p)) && (c < 32 || c > 127))
    583 				c = '?';
    584 			if (!(*lfn++ = c))
    585 				return;
    586 		}
    587 	if (xde->weCnt & WIN_LAST)
    588 		*lfn = 0;
    589 }
    590 
    591 /*
    592  * Copy short filename
    593  */
    594 static void
    595 cp_sfn(u_char * sfn, struct direntry * de)
    596 {
    597 	u_char *p;
    598 	int     j, i;
    599 
    600 	p = sfn;
    601 	if (*de->deName != ' ') {
    602 		for (j = 7; de->deName[j] == ' '; j--);
    603 		for (i = 0; i <= j; i++)
    604 			*p++ = de->deName[i];
    605 		if (*de->deExtension != ' ') {
    606 			*p++ = '.';
    607 			for (j = 2; de->deExtension[j] == ' '; j--);
    608 			for (i = 0; i <= j; i++)
    609 				*p++ = de->deExtension[i];
    610 		}
    611 	}
    612 	*p = 0;
    613 	if (*sfn == 5)
    614 		*sfn = 0xe5;
    615 }
    616 
    617 /*
    618  * Return size of file in bytes
    619  */
    620 static  off_t
    621 fsize(DOS_FS * fs, struct direntry * de)
    622 {
    623 	u_long  size;
    624 	u_int   c;
    625 	int     n;
    626 
    627 	if (!(size = getulong(de->deFileSize)) &&
    628 	    de->deAttributes & ATTR_DIRECTORY) {
    629 		if (!(c = getushort(de->deStartCluster)))
    630 			size = fs->dirents * sizeof(struct direntry);
    631 		else {
    632 			if ((n = fatcnt(fs, c)) == -1)
    633 				return n;
    634 			size = blkbyt(fs, n);
    635 		}
    636 	}
    637 	return size;
    638 }
    639 
    640 /*
    641  * Count number of clusters in chain
    642  */
    643 static int
    644 fatcnt(DOS_FS * fs, u_int c)
    645 {
    646 	int     n;
    647 
    648 	for (n = 0; okclus(fs, c); n++)
    649 		if (fatget(fs, &c))
    650 			return -1;
    651 	return fatend(fs->fatsz, c) ? n : -1;
    652 }
    653 
    654 /*
    655  * Get next cluster in cluster chain
    656  */
    657 static int
    658 fatget(DOS_FS * fs, u_int * c)
    659 {
    660 	u_char  buf[4];
    661 	u_int   x;
    662 	int     err;
    663 
    664 	err = ioread(fs, secbyt(fs->lsnfat) + fatoff(fs->fatsz, *c), buf,
    665 	    fs->fatsz != 32 ? 2 : 4);
    666 	if (err)
    667 		return err;
    668 	x = fs->fatsz != 32 ? getushort(buf) : getulong(buf);
    669 	*c = fs->fatsz == 12 ? *c & 1 ? x >> 4 : x & 0xfff : x;
    670 	return 0;
    671 }
    672 
    673 /*
    674  * Is cluster an end-of-chain marker?
    675  */
    676 static int
    677 fatend(u_int sz, u_int c)
    678 {
    679 	return c > (sz == 12 ? 0xff7U : sz == 16 ? 0xfff7U : 0xffffff7);
    680 }
    681 
    682 /*
    683  * Offset-based I/O primitive
    684  */
    685 static int
    686 ioread(DOS_FS * fs, u_int offset, void *buf, u_int nbyte)
    687 {
    688 	char   *s;
    689 	u_int   off, n;
    690 	int     err;
    691 
    692 	s = buf;
    693 	if ((off = offset & (SECSIZ - 1))) {
    694 		offset -= off;
    695 		if ((err = iobuf(fs, bytsec(offset))))
    696 			return err;
    697 		offset += SECSIZ;
    698 		if ((n = SECSIZ - off) > nbyte)
    699 			n = nbyte;
    700 		memcpy(s, fs->buf + off, n);
    701 		s += n;
    702 		nbyte -= n;
    703 	}
    704 	n = nbyte & (SECSIZ - 1);
    705 	if (nbyte -= n) {
    706 		if ((err = ioget(fs->fd, bytsec(offset), s, bytsec(nbyte))))
    707 			return err;
    708 		offset += nbyte;
    709 		s += nbyte;
    710 	}
    711 	if (n) {
    712 		if ((err = iobuf(fs, bytsec(offset))))
    713 			return err;
    714 		memcpy(s, fs->buf, n);
    715 	}
    716 	return 0;
    717 }
    718 
    719 /*
    720  * Buffered sector-based I/O primitive
    721  */
    722 static int
    723 iobuf(DOS_FS * fs, u_int lsec)
    724 {
    725 	int     err;
    726 
    727 	if (fs->bufsec != lsec) {
    728 		if ((err = ioget(fs->fd, lsec, fs->buf, 1)))
    729 			return err;
    730 		fs->bufsec = lsec;
    731 	}
    732 	return 0;
    733 }
    734 
    735 /*
    736  * Sector-based I/O primitive
    737  */
    738 static int
    739 ioget(struct open_file * fd, u_int lsec, void *buf, u_int nsec)
    740 {
    741 	size_t rsize;
    742 	int err;
    743 
    744 #ifndef LIBSA_NO_TWIDDLE
    745 	twiddle();
    746 #endif
    747 	err = DEV_STRATEGY(fd->f_dev)(fd->f_devdata, F_READ, lsec,
    748 	    secbyt(nsec), buf, &rsize);
    749 	return (err);
    750 }
    751