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