1 /* $OpenBSD: msdosfs_vfsops.c,v 1.36 2005/03/02 00:46:10 tom Exp $ */
2 /* $NetBSD: msdosfs_vfsops.c,v 1.48 1997/10/18 02:54:57 briggs Exp $ */
3
4 /*-
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * All rights reserved.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /*
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 *
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
40 *
41 * This software is provided "as is".
42 *
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
47 *
48 * October 1992
49 */
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/namei.h>
54 #include <sys/proc.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */
58 #include <sys/mount.h>
59 #include <sys/buf.h>
60 #include <sys/file.h>
61 #include <sys/disklabel.h>
62 #include <sys/ioctl.h>
63 #include <sys/malloc.h>
64 #include <sys/dirent.h>
65
66 #include <msdosfs/bpb.h>
67 #include <msdosfs/bootsect.h>
68 #include <msdosfs/direntry.h>
69 #include <msdosfs/denode.h>
70 #include <msdosfs/msdosfsmount.h>
71 #include <msdosfs/fat.h>
72
73 int msdosfs_mount(struct mount *, const char *, void *, struct nameidata *,
74 struct proc *);
75 int msdosfs_start(struct mount *, int, struct proc *);
76 int msdosfs_unmount(struct mount *, int, struct proc *);
77 int msdosfs_root(struct mount *, struct vnode **);
78 int msdosfs_statfs(struct mount *, struct statfs *, struct proc *);
79 int msdosfs_sync(struct mount *, int, struct ucred *, struct proc *);
80 int msdosfs_fhtovp(struct mount *, struct fid *, struct vnode **);
81 int msdosfs_vptofh(struct vnode *, struct fid *);
82 int msdosfs_check_export(struct mount *mp, struct mbuf *nam,
83 int *extflagsp, struct ucred **credanonp);
84
85 int msdosfs_mountfs(struct vnode *, struct mount *, struct proc *,
86 struct msdosfs_args *);
87
88 int msdosfs_sync_vnode(struct vnode *, void *);
89
90 /*
91 * mp - path - addr in user space of mount point (ie /usr or whatever)
92 * data - addr in user space of mount params including the name of the block
93 * special file to treat as a filesystem.
94 */
95 int
msdosfs_mount(mp,path,data,ndp,p)96 msdosfs_mount(mp, path, data, ndp, p)
97 struct mount *mp;
98 const char *path;
99 void *data;
100 struct nameidata *ndp;
101 struct proc *p;
102 {
103 struct vnode *devvp; /* vnode for blk device to mount */
104 struct msdosfs_args args; /* will hold data from mount request */
105 /* msdosfs specific mount control block */
106 struct msdosfsmount *pmp = NULL;
107 size_t size;
108 int error, flags;
109 mode_t accessmode;
110
111 error = copyin(data, &args, sizeof(struct msdosfs_args));
112 if (error)
113 return (error);
114 /*
115 * If updating, check whether changing from read-only to
116 * read/write; if there is no device name, that's all we do.
117 */
118 if (mp->mnt_flag & MNT_UPDATE) {
119 pmp = VFSTOMSDOSFS(mp);
120 error = 0;
121 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
122 flags = WRITECLOSE;
123 if (mp->mnt_flag & MNT_FORCE)
124 flags |= FORCECLOSE;
125 error = vflush(mp, NULLVP, flags);
126 }
127 if (!error && (mp->mnt_flag & MNT_RELOAD))
128 /* not yet implemented */
129 error = EOPNOTSUPP;
130 if (error)
131 return (error);
132 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_WANTRDWR)) {
133 /*
134 * If upgrade to read-write by non-root, then verify
135 * that user has necessary permissions on the device.
136 */
137 if (p->p_ucred->cr_uid != 0) {
138 devvp = pmp->pm_devvp;
139 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
140 error = VOP_ACCESS(devvp, VREAD | VWRITE,
141 p->p_ucred, p);
142 if (error) {
143 VOP_UNLOCK(devvp, 0, p);
144 return (error);
145 }
146 VOP_UNLOCK(devvp, 0, p);
147 }
148 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
149 }
150 if (args.fspec == 0) {
151 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
152 if (args.flags & MSDOSFSMNT_MNTOPT) {
153 XXX see below for diff
154 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
155 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
156 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
157 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
158 }
159 #endif
160 /*
161 * Process export requests.
162 */
163 return (vfs_export(mp, &pmp->pm_export,
164 &args.export_info));
165 }
166 }
167 /*
168 * Not an update, or updating the name: look up the name
169 * and verify that it refers to a sensible block device.
170 */
171 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
172 if ((error = namei(ndp)) != 0)
173 return (error);
174 devvp = ndp->ni_vp;
175
176 if (devvp->v_type != VBLK) {
177 vrele(devvp);
178 return (ENOTBLK);
179 }
180 if (major(devvp->v_rdev) >= nblkdev) {
181 vrele(devvp);
182 return (ENXIO);
183 }
184 /*
185 * If mount by non-root, then verify that user has necessary
186 * permissions on the device.
187 */
188 if (p->p_ucred->cr_uid != 0) {
189 accessmode = VREAD;
190 if ((mp->mnt_flag & MNT_RDONLY) == 0)
191 accessmode |= VWRITE;
192 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
193 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
194 if (error) {
195 vput(devvp);
196 return (error);
197 }
198 VOP_UNLOCK(devvp, 0, p);
199 }
200 if ((mp->mnt_flag & MNT_UPDATE) == 0)
201 error = msdosfs_mountfs(devvp, mp, p, &args);
202 else {
203 if (devvp != pmp->pm_devvp)
204 error = EINVAL; /* XXX needs translation */
205 else
206 vrele(devvp);
207 }
208 if (error) {
209 vrele(devvp);
210 return (error);
211 }
212 pmp = VFSTOMSDOSFS(mp);
213 pmp->pm_gid = args.gid;
214 pmp->pm_uid = args.uid;
215 pmp->pm_mask = args.mask;
216 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
217
218 /*
219 * GEMDOS knows nothing (yet) about win95
220 */
221 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
222 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
223
224 /*
225 * Avoid US patents 5,579,517 and 5,758,352
226 */
227 #ifdef MSDOSFS_NO_LFN
228 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
229 #endif
230
231 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) {
232 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
233 pmp->pm_flags &= ~MSDOSFSMNT_LONGNAME;
234 } else if (!(pmp->pm_flags & (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
235 struct vnode *rvp;
236
237 /*
238 * Try to divine whether to support Win'95 long filenames
239 */
240 if (FAT32(pmp))
241 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
242 else {
243 if ((error = msdosfs_root(mp, &rvp)) != 0) {
244 msdosfs_unmount(mp, MNT_FORCE, p);
245 return (error);
246 }
247 pmp->pm_flags |= findwin95(VTODE(rvp))
248 ? MSDOSFSMNT_LONGNAME
249 : MSDOSFSMNT_SHORTNAME;
250 vput(rvp);
251 }
252 }
253 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
254 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
255 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
256 &size);
257 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
258 bcopy(&args, &mp->mnt_stat.mount_info.msdosfs_args, sizeof(args));
259 #ifdef MSDOSFS_DEBUG
260 printf("msdosfs_mount(): mp %x, pmp %x, inusemap %x\n", mp, pmp, pmp->pm_inusemap);
261 #endif
262 return (0);
263 }
264
265 int
msdosfs_mountfs(devvp,mp,p,argp)266 msdosfs_mountfs(devvp, mp, p, argp)
267 struct vnode *devvp;
268 struct mount *mp;
269 struct proc *p;
270 struct msdosfs_args *argp;
271 {
272 struct msdosfsmount *pmp;
273 struct buf *bp;
274 dev_t dev = devvp->v_rdev;
275 struct partinfo dpart;
276 union bootsector *bsp;
277 struct byte_bpb33 *b33;
278 struct byte_bpb50 *b50;
279 struct byte_bpb710 *b710;
280 extern struct vnode *rootvp;
281 u_int8_t SecPerClust;
282 int ronly, error;
283 int bsize = 0, dtype = 0, tmp;
284 uint32_t dirsperblk;
285
286 /*
287 * Disallow multiple mounts of the same device.
288 * Disallow mounting of a device that is currently in use
289 * (except for root, which might share swap device for miniroot).
290 * Flush out any old buffers remaining from a previous use.
291 */
292 if ((error = vfs_mountedon(devvp)) != 0)
293 return (error);
294 if (vcount(devvp) > 1 && devvp != rootvp)
295 return (EBUSY);
296 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
297 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
298 VOP_UNLOCK(devvp, 0, p);
299 if (error)
300 return (error);
301
302 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
303 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
304 if (error)
305 return (error);
306
307 bp = NULL; /* both used in error_exit */
308 pmp = NULL;
309
310 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
311 /*
312 * We need the disklabel to calculate the size of a FAT entry
313 * later on. Also make sure the partition contains a filesystem
314 * of type FS_MSDOS. This doesn't work for floppies, so we have
315 * to check for them too.
316 *
317 * At least some parts of the msdos fs driver seem to assume
318 * that the size of a disk block will always be 512 bytes.
319 * Let's check it...
320 */
321 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
322 FREAD, NOCRED, p);
323 if (error)
324 goto error_exit;
325 tmp = dpart.part->p_fstype;
326 dtype = dpart.disklab->d_type;
327 bsize = dpart.disklab->d_secsize;
328 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
329 error = EFTYPE;
330 goto error_exit;
331 }
332 }
333
334 /*
335 * Read the boot sector of the filesystem, and then check the
336 * boot signature. If not a dos boot sector then error out.
337 */
338 if ((error = bread(devvp, 0, 512, NOCRED, &bp)) != 0)
339 goto error_exit;
340 bp->b_flags |= B_AGE;
341 bsp = (union bootsector *)bp->b_data;
342 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
343 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
344 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
345
346 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
347 bzero((caddr_t)pmp, sizeof *pmp);
348 pmp->pm_mountp = mp;
349
350 /*
351 * Compute several useful quantities from the bpb in the
352 * bootsector. Copy in the dos 5 variant of the bpb then fix up
353 * the fields that are different between dos 5 and dos 3.3.
354 */
355 SecPerClust = b50->bpbSecPerClust;
356 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
357 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
358 pmp->pm_FATs = b50->bpbFATs;
359 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
360 pmp->pm_Sectors = getushort(b50->bpbSectors);
361 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
362 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
363 pmp->pm_Heads = getushort(b50->bpbHeads);
364 pmp->pm_Media = b50->bpbMedia;
365
366 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
367 /* XXX - We should probably check more values here */
368 if (!pmp->pm_BytesPerSec || !SecPerClust
369 || pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
370 error = EFTYPE;
371 goto error_exit;
372 }
373 #ifdef DIAGNOSTIC
374 if (pmp->pm_BytesPerSec != 512 && pmp->pm_BytesPerSec != 1024 &&
375 pmp->pm_BytesPerSec != 2048 && pmp->pm_BytesPerSec != 4096)
376 printf("msdosfs: WARNING: invalid bytes per sector %u,"
377 " mounting anyway\n", (u_int)pmp->pm_BytesPerSec);
378 if (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
379 printf("msdosfs: WARNING: bytes per sector not a power"
380 " of two, mounting anyway\n");
381 if (SecPerClust & (SecPerClust - 1))
382 printf("msdosfs: WARNING: sectors per cluster %u not a"
383 " power of two, mounting anyway\n",
384 (u_int)SecPerClust);
385 else if ((((uint32_t)pmp->pm_BytesPerSec) *
386 ((uint32_t)SecPerClust)) > 32768)
387 printf("msdosfs: WARNING: bytes per cluster %u (%u*%u)"
388 " larger than allowed 32768, mounting anyway\n",
389 (u_int)pmp->pm_BytesPerSec, (u_int)SecPerClust,
390 (u_int)(((uint32_t)pmp->pm_BytesPerSec) *
391 ((uint32_t)SecPerClust)));
392 #endif
393 }
394
395 if (pmp->pm_Sectors == 0) {
396 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
397 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
398 } else {
399 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
400 pmp->pm_HugeSectors = pmp->pm_Sectors;
401 }
402
403 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
404
405 if (pmp->pm_RootDirEnts == 0) {
406 if (pmp->pm_Sectors || pmp->pm_FATsecs ||
407 getushort(b710->bpbFSVers)) {
408 error = EINVAL;
409 goto error_exit;
410 }
411 pmp->pm_fatmask = FAT32_MASK;
412 pmp->pm_fatmult = 4;
413 pmp->pm_fatdiv = 1;
414 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
415 if (getushort(b710->bpbExtFlags) & FATMIRROR)
416 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
417 else
418 pmp->pm_flags |= MSDOSFS_FATMIRROR;
419 } else
420 pmp->pm_flags |= MSDOSFS_FATMIRROR;
421
422 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
423 if (FAT32(pmp)) {
424 /*
425 * GEMDOS doesn't know fat32.
426 */
427 error = EINVAL;
428 goto error_exit;
429 }
430
431 /*
432 * Check a few values (could do some more):
433 * - logical sector size: power of 2, >= block size
434 * - sectors per cluster: power of 2, >= 1
435 * - number of sectors: >= 1, <= size of partition
436 */
437 if ( (SecPerClust == 0)
438 || (SecPerClust & (SecPerClust - 1))
439 || (pmp->pm_BytesPerSec < bsize)
440 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
441 || (pmp->pm_HugeSectors == 0)
442 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
443 > dpart.part->p_size)
444 ) {
445 error = EFTYPE;
446 goto error_exit;
447 }
448 /*
449 * XXX - Many parts of the msdos fs driver seem to assume that
450 * the number of bytes per logical sector (BytesPerSec) will
451 * always be the same as the number of bytes per disk block
452 * Let's pretend it is.
453 */
454 tmp = pmp->pm_BytesPerSec / bsize;
455 pmp->pm_BytesPerSec = bsize;
456 pmp->pm_HugeSectors *= tmp;
457 pmp->pm_HiddenSects *= tmp;
458 pmp->pm_ResSectors *= tmp;
459 pmp->pm_Sectors *= tmp;
460 pmp->pm_FATsecs *= tmp;
461 SecPerClust *= tmp;
462 }
463 pmp->pm_fatblk = pmp->pm_ResSectors;
464 if (FAT32(pmp)) {
465 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
466 pmp->pm_firstcluster = pmp->pm_fatblk
467 + (pmp->pm_FATs * pmp->pm_FATsecs);
468 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
469 } else {
470 pmp->pm_rootdirblk = pmp->pm_fatblk +
471 (pmp->pm_FATs * pmp->pm_FATsecs);
472 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
473 + pmp->pm_BytesPerSec - 1)
474 / pmp->pm_BytesPerSec;/* in sectors */
475 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
476 }
477
478 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
479 SecPerClust;
480 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
481 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
482
483 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
484 if ((pmp->pm_nmbrofclusters <= (0xff0 - 2))
485 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
486 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
487 ) {
488 pmp->pm_fatmask = FAT12_MASK;
489 pmp->pm_fatmult = 3;
490 pmp->pm_fatdiv = 2;
491 } else {
492 pmp->pm_fatmask = FAT16_MASK;
493 pmp->pm_fatmult = 2;
494 pmp->pm_fatdiv = 1;
495 }
496 } else if (pmp->pm_fatmask == 0) {
497 if (pmp->pm_maxcluster
498 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
499 /*
500 * This will usually be a floppy disk. This size makes
501 * sure that one fat entry will not be split across
502 * multiple blocks.
503 */
504 pmp->pm_fatmask = FAT12_MASK;
505 pmp->pm_fatmult = 3;
506 pmp->pm_fatdiv = 2;
507 } else {
508 pmp->pm_fatmask = FAT16_MASK;
509 pmp->pm_fatmult = 2;
510 pmp->pm_fatdiv = 1;
511 }
512 }
513 if (FAT12(pmp))
514 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
515 else
516 pmp->pm_fatblocksize = MAXBSIZE;
517
518 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
519 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
520
521 /*
522 * Compute mask and shift value for isolating cluster relative byte
523 * offsets and cluster numbers from a file offset.
524 */
525 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
526 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
527 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
528
529 /*
530 * Check for valid cluster size
531 * must be a power of 2
532 */
533 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
534 error = EFTYPE;
535 goto error_exit;
536 }
537
538 /*
539 * Release the bootsector buffer.
540 */
541 brelse(bp);
542 bp = NULL;
543
544 /*
545 * Check FSInfo
546 */
547 if (pmp->pm_fsinfo) {
548 struct fsinfo *fp;
549
550 if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
551 goto error_exit;
552 fp = (struct fsinfo *)bp->b_data;
553 if (!bcmp(fp->fsisig1, "RRaA", 4)
554 && !bcmp(fp->fsisig2, "rrAa", 4)
555 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
556 && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
557 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
558 else
559 pmp->pm_fsinfo = 0;
560 brelse(bp);
561 bp = NULL;
562 }
563
564 /*
565 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
566 */
567
568 /*
569 * Allocate memory for the bitmap of allocated clusters, and then
570 * fill it in.
571 */
572 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
573 / N_INUSEBITS)
574 * sizeof(*pmp->pm_inusemap),
575 M_MSDOSFSFAT, M_WAITOK);
576
577 /*
578 * fillinusemap() needs pm_devvp.
579 */
580 pmp->pm_dev = dev;
581 pmp->pm_devvp = devvp;
582
583 /*
584 * Have the inuse map filled in.
585 */
586 if ((error = fillinusemap(pmp)) != 0)
587 goto error_exit;
588
589 /*
590 * If they want fat updates to be synchronous then let them suffer
591 * the performance degradation in exchange for the on disk copy of
592 * the fat being correct just about all the time. I suppose this
593 * would be a good thing to turn on if the kernel is still flakey.
594 */
595 if (mp->mnt_flag & MNT_SYNCHRONOUS)
596 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
597
598 /*
599 * Finish up.
600 */
601 if (ronly)
602 pmp->pm_flags |= MSDOSFSMNT_RONLY;
603 else
604 pmp->pm_fmod = 1;
605 mp->mnt_data = (qaddr_t)pmp;
606 mp->mnt_stat.f_fsid.val[0] = (long)dev;
607 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
608 #ifdef QUOTA
609 /*
610 * If we ever do quotas for DOS filesystems this would be a place
611 * to fill in the info in the msdosfsmount structure. You dolt,
612 * quotas on dos filesystems make no sense because files have no
613 * owners on dos filesystems. of course there is some empty space
614 * in the directory entry where we could put uid's and gid's.
615 */
616 #endif
617 devvp->v_specmountpoint = mp;
618
619 return (0);
620
621 error_exit:
622 devvp->v_specmountpoint = NULL;
623 if (bp)
624 brelse(bp);
625 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
626 if (pmp) {
627 if (pmp->pm_inusemap)
628 free(pmp->pm_inusemap, M_MSDOSFSFAT);
629 free(pmp, M_MSDOSFSMNT);
630 mp->mnt_data = (qaddr_t)0;
631 }
632 return (error);
633 }
634
635 int
msdosfs_start(mp,flags,p)636 msdosfs_start(mp, flags, p)
637 struct mount *mp;
638 int flags;
639 struct proc *p;
640 {
641
642 return (0);
643 }
644
645 /*
646 * Unmount the filesystem described by mp.
647 */
648 int
msdosfs_unmount(mp,mntflags,p)649 msdosfs_unmount(mp, mntflags, p)
650 struct mount *mp;
651 int mntflags;
652 struct proc *p;
653 {
654 struct msdosfsmount *pmp;
655 int error, flags;
656 struct vnode *vp;
657
658 flags = 0;
659 if (mntflags & MNT_FORCE)
660 flags |= FORCECLOSE;
661 #ifdef QUOTA
662 #endif
663 if ((error = vflush(mp, NULLVP, flags)) != 0)
664 return (error);
665 pmp = VFSTOMSDOSFS(mp);
666 pmp->pm_devvp->v_specmountpoint = NULL;
667 vp = pmp->pm_devvp;
668 #ifdef MSDOSFS_DEBUG
669 vprint("msdosfs_umount(): just before calling VOP_CLOSE()\n", vp);
670 #endif
671 error = VOP_CLOSE(vp,
672 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
673 vrele(vp);
674 free(pmp->pm_inusemap, M_MSDOSFSFAT);
675 free(pmp, M_MSDOSFSMNT);
676 mp->mnt_data = (qaddr_t)0;
677 mp->mnt_flag &= ~MNT_LOCAL;
678 return (error);
679 }
680
681 int
msdosfs_root(mp,vpp)682 msdosfs_root(mp, vpp)
683 struct mount *mp;
684 struct vnode **vpp;
685 {
686 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
687 struct denode *ndep;
688 int error;
689
690 if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
691 return (error);
692
693 #ifdef MSDOSFS_DEBUG
694 printf("msdosfs_root(); mp %08x, pmp %08x, ndep %08x, vp %08x\n",
695 mp, pmp, ndep, DETOV(ndep));
696 #endif
697
698 *vpp = DETOV(ndep);
699 return (0);
700 }
701
702 int
msdosfs_statfs(mp,sbp,p)703 msdosfs_statfs(mp, sbp, p)
704 struct mount *mp;
705 struct statfs *sbp;
706 struct proc *p;
707 {
708 struct msdosfsmount *pmp;
709
710 pmp = VFSTOMSDOSFS(mp);
711 sbp->f_bsize = pmp->pm_bpcluster;
712 sbp->f_iosize = pmp->pm_bpcluster;
713 sbp->f_blocks = pmp->pm_nmbrofclusters;
714 sbp->f_bfree = pmp->pm_freeclustercount;
715 sbp->f_bavail = pmp->pm_freeclustercount;
716 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
717 sbp->f_ffree = 0; /* what to put in here? */
718 if (sbp != &mp->mnt_stat) {
719 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
720 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
721 bcopy(&mp->mnt_stat.mount_info.msdosfs_args,
722 &sbp->mount_info.msdosfs_args, sizeof(struct msdosfs_args));
723 }
724 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
725 return (0);
726 }
727
728
729 struct msdosfs_sync_arg {
730 struct proc *p;
731 struct ucred *cred;
732 int allerror;
733 int waitfor;
734 };
735
736 int
msdosfs_sync_vnode(struct vnode * vp,void * arg)737 msdosfs_sync_vnode(struct vnode *vp, void *arg)
738 {
739 struct msdosfs_sync_arg *msa = arg;
740 int error;
741 struct denode *dep;
742
743 dep = VTODE(vp);
744 if (vp->v_type == VNON ||
745 ((dep->de_flag & (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0
746 && LIST_EMPTY(&vp->v_dirtyblkhd)) ||
747 msa->waitfor == MNT_LAZY) {
748 simple_unlock(&vp->v_interlock);
749 return (0);
750 }
751
752 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, msa->p))
753 return (0);
754
755 if ((error = VOP_FSYNC(vp, msa->cred, msa->waitfor, msa->p)) != 0)
756 msa->allerror = error;
757 VOP_UNLOCK(vp, 0, msa->p);
758 vrele(vp);
759
760 return (0);
761 }
762
763
764 int
msdosfs_sync(mp,waitfor,cred,p)765 msdosfs_sync(mp, waitfor, cred, p)
766 struct mount *mp;
767 int waitfor;
768 struct ucred *cred;
769 struct proc *p;
770 {
771 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
772 struct msdosfs_sync_arg msa;
773 int error;
774
775 msa.allerror = 0;
776 msa.p = p;
777 msa.cred = cred;
778 msa.waitfor = waitfor;
779
780 /*
781 * If we ever switch to not updating all of the fats all the time,
782 * this would be the place to update them from the first one.
783 */
784 if (pmp->pm_fmod != 0) {
785 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
786 panic("msdosfs_sync: rofs mod");
787 else {
788 /* update fats here */
789 }
790 }
791 /*
792 * Write back each (modified) denode.
793 */
794 vfs_mount_foreach_vnode(mp, msdosfs_sync_vnode, &msa);
795
796 /*
797 * Force stale file system control information to be flushed.
798 */
799 if (waitfor != MNT_LAZY) {
800 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
801 if ((error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p)) != 0)
802 msa.allerror = error;
803 VOP_UNLOCK(pmp->pm_devvp, 0, p);
804 }
805
806 return (msa.allerror);
807 }
808
809 int
msdosfs_fhtovp(mp,fhp,vpp)810 msdosfs_fhtovp(mp, fhp, vpp)
811 struct mount *mp;
812 struct fid *fhp;
813 struct vnode **vpp;
814 {
815 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
816 struct defid *defhp = (struct defid *) fhp;
817 struct denode *dep;
818 int error;
819
820 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
821 if (error) {
822 *vpp = NULLVP;
823 return (error);
824 }
825 *vpp = DETOV(dep);
826 return (0);
827 }
828
829 int
msdosfs_vptofh(vp,fhp)830 msdosfs_vptofh(vp, fhp)
831 struct vnode *vp;
832 struct fid *fhp;
833 {
834 struct denode *dep;
835 struct defid *defhp;
836
837 dep = VTODE(vp);
838 defhp = (struct defid *)fhp;
839 defhp->defid_len = sizeof(struct defid);
840 defhp->defid_dirclust = dep->de_dirclust;
841 defhp->defid_dirofs = dep->de_diroffset;
842 /* defhp->defid_gen = dep->de_gen; */
843 return (0);
844 }
845
846 int
msdosfs_check_export(mp,nam,exflagsp,credanonp)847 msdosfs_check_export(mp, nam, exflagsp, credanonp)
848 register struct mount *mp;
849 struct mbuf *nam;
850 int *exflagsp;
851 struct ucred **credanonp;
852 {
853 register struct netcred *np;
854 register struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
855
856 /*
857 * Get the export permission structure for this <mp, client> tuple.
858 */
859 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
860 if (np == NULL)
861 return (EACCES);
862
863 *exflagsp = np->netc_exflags;
864 *credanonp = &np->netc_anon;
865 return (0);
866 }
867
868 #define msdosfs_vget ((int (*)(struct mount *, ino_t, struct vnode **)) \
869 eopnotsupp)
870
871 #define msdosfs_quotactl ((int (*)(struct mount *, int, uid_t, caddr_t, \
872 struct proc *))eopnotsupp)
873
874 #define msdosfs_sysctl ((int (*)(int *, u_int, void *, size_t *, void *, \
875 size_t, struct proc *))eopnotsupp)
876
877 const struct vfsops msdosfs_vfsops = {
878 msdosfs_mount,
879 msdosfs_start,
880 msdosfs_unmount,
881 msdosfs_root,
882 msdosfs_quotactl,
883 msdosfs_statfs,
884 msdosfs_sync,
885 msdosfs_vget,
886 msdosfs_fhtovp,
887 msdosfs_vptofh,
888 msdosfs_init,
889 msdosfs_sysctl,
890 msdosfs_check_export
891 };
892