1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vfsops.c,v 1.13.2.4 1999/09/05 08:17:21 peter Exp $ */
2 /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws 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/fcntl.h>
61 #include <sys/malloc.h>
62 #include <sys/stat.h> /* defines ALLPERMS */
63
64 #include <msdosfs/bpb.h>
65 #include <msdosfs/bootsect.h>
66 #include <msdosfs/direntry.h>
67 #include <msdosfs/denode.h>
68 #include <msdosfs/msdosfsmount.h>
69 #include <msdosfs/fat.h>
70
71 #ifdef __FreeBSD_version
72 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
73 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
74 #endif
75
76 static int update_mp __P((struct mount *mp, struct msdosfs_args *argp));
77 static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
78 struct proc *p, struct msdosfs_args *argp));
79 #ifdef __FreeBSD_version
80 static int msdosfs_fhtovp __P((struct mount *, struct fid *,
81 struct sockaddr *, struct vnode **, int *,
82 struct ucred **));
83 #else
84 static int msdosfs_fhtovp __P((struct mount *, struct fid *,
85 struct mbuf *, struct vnode **, int *,
86 struct ucred **));
87 #endif
88 static int msdosfs_mount __P((struct mount *, char *, caddr_t,
89 struct nameidata *, struct proc *));
90 static int msdosfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
91 struct proc *));
92 static int msdosfs_root __P((struct mount *, struct vnode **));
93 static int msdosfs_start __P((struct mount *, int, struct proc *));
94 static int msdosfs_statfs __P((struct mount *, struct statfs *,
95 struct proc *));
96 static int msdosfs_sync __P((struct mount *, int, struct ucred *,
97 struct proc *));
98 static int msdosfs_unmount __P((struct mount *, int, struct proc *));
99 static int msdosfs_vget __P((struct mount *mp, ino_t ino,
100 struct vnode **vpp));
101 static int msdosfs_vptofh __P((struct vnode *, struct fid *));
102
103 #ifndef __FreeBSD_version
104 #define simple_lock(a)
105 #define simple_unlock(a)
106 #endif
107 static int
108 update_mp(mp, argp)
109 struct mount *mp;
110 struct msdosfs_args *argp;
111 {
112 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
113 int error;
114
115 pmp->pm_gid = argp->gid;
116 pmp->pm_uid = argp->uid;
117 pmp->pm_mask = argp->mask & ALLPERMS;
118 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
119 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
120 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
121 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
122 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
123 }
124 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
125 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
126 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
127 }
128
129 #ifndef __FreeBSD__
130 /*
131 * GEMDOS knows nothing (yet) about win95
132 */
133 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
134 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
135 #endif
136
137 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
138 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
139 else if (!(pmp->pm_flags &
140 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
141 struct vnode *rootvp;
142
143 /*
144 * Try to divine whether to support Win'95 long filenames
145 */
146 if (FAT32(pmp))
147 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
148 else {
149 if ((error = msdosfs_root(mp, &rootvp)) != 0)
150 return error;
151 pmp->pm_flags |= findwin95(VTODE(rootvp))
152 ? MSDOSFSMNT_LONGNAME
153 : MSDOSFSMNT_SHORTNAME;
154 vput(rootvp);
155 }
156 }
157 return 0;
158 }
159
160 #ifndef __FreeBSD__
161 int
162 msdosfs_mountroot()
163 {
164 register struct mount *mp;
165 struct proc *p = curproc; /* XXX */
166 size_t size;
167 int error;
168 struct msdosfs_args args;
169
170 if (root_device->dv_class != DV_DISK)
171 return (ENODEV);
172
173 /*
174 * Get vnodes for swapdev and rootdev.
175 */
176 if (bdevvp(rootdev, &rootvp))
177 panic("msdosfs_mountroot: can't setup rootvp");
178
179 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
180 bzero((char *)mp, (u_long)sizeof(struct mount));
181 #ifdef __FreeBSD_version
182 mp->mnt_op = &msdosfs_vfsops;
183 #endif
184 mp->mnt_flag = 0;
185 LIST_INIT(&mp->mnt_vnodelist);
186
187 args.flags = 0;
188 args.uid = 0;
189 args.gid = 0;
190 args.mask = 0777;
191
192 if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
193 free(mp, M_MOUNT);
194 return (error);
195 }
196
197 if ((error = update_mp(mp, &args)) != 0) {
198 (void)msdosfs_unmount(mp, 0, p);
199 free(mp, M_MOUNT);
200 return (error);
201 }
202
203 if ((error = vfs_lock(mp)) != 0) {
204 (void)msdosfs_unmount(mp, 0, p);
205 free(mp, M_MOUNT);
206 return (error);
207 }
208
209 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
210 mp->mnt_vnodecovered = NULLVP;
211 (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
212 &size);
213 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
214 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
215 &size);
216 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
217 (void)msdosfs_statfs(mp, &mp->mnt_stat, p);
218 vfs_unlock(mp);
219 return (0);
220 }
221 #endif
222
223 /*
224 * mp - path - addr in user space of mount point (ie /usr or whatever)
225 * data - addr in user space of mount params including the name of the block
226 * special file to treat as a filesystem.
227 */
228 static int
229 msdosfs_mount(mp, path, data, ndp, p)
230 struct mount *mp;
231 char *path;
232 caddr_t data;
233 struct nameidata *ndp;
234 struct proc *p;
235 {
236 struct vnode *devvp; /* vnode for blk device to mount */
237 struct msdosfs_args args; /* will hold data from mount request */
238 /* msdosfs specific mount control block */
239 struct msdosfsmount *pmp = NULL;
240 size_t size;
241 int error, flags;
242 mode_t accessmode;
243
244 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
245 if (error)
246 return (error);
247 #ifdef __FreeBSD_version
248 if (args.magic != MSDOSFS_ARGSMAGIC)
249 args.flags = 0;
250 #endif
251 /*
252 * If updating, check whether changing from read-only to
253 * read/write; if there is no device name, that's all we do.
254 */
255 if (mp->mnt_flag & MNT_UPDATE) {
256 pmp = VFSTOMSDOSFS(mp);
257 error = 0;
258 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
259 flags = WRITECLOSE;
260 if (mp->mnt_flag & MNT_FORCE)
261 flags |= FORCECLOSE;
262 #ifndef __FreeBSD_version
263 if (vfs_busy(mp))
264 return EBUSY;
265 #endif
266 error = vflush(mp, NULLVP, flags);
267 #ifndef __FreeBSD_version
268 vfs_unbusy(mp);
269 #endif
270 }
271 if (!error && (mp->mnt_flag & MNT_RELOAD))
272 /* not yet implemented */
273 error = EOPNOTSUPP;
274 if (error)
275 return (error);
276 #ifdef __FreeBSD_version
277 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
278 #else
279 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_WANTRDWR)) {
280 #endif
281 /*
282 * If upgrade to read-write by non-root, then verify
283 * that user has necessary permissions on the device.
284 */
285 if (p->p_ucred->cr_uid != 0) {
286 devvp = pmp->pm_devvp;
287 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
288 error = VOP_ACCESS(devvp, VREAD | VWRITE,
289 p->p_ucred, p);
290 if (error) {
291 #ifdef __FreeBSD_version
292 VOP_UNLOCK(devvp, 0, p);
293 #else
294 VOP_UNLOCK(devvp);
295 #endif
296 return (error);
297 }
298 #ifdef __FreeBSD_version
299 VOP_UNLOCK(devvp, 0, p);
300 #else
301 VOP_UNLOCK(devvp);
302 #endif
303 }
304 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
305 }
306 if (args.fspec == 0) {
307 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
308 if (args.flags & MSDOSFSMNT_MNTOPT) {
309 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
310 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
311 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
312 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
313 }
314 #endif
315 /*
316 * Process export requests.
317 */
318 return (vfs_export(mp, &pmp->pm_export, &args.export));
319 }
320 }
321 /*
322 * Not an update, or updating the name: look up the name
323 * and verify that it refers to a sensible block device.
324 */
325 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
326 error = namei(ndp);
327 if (error)
328 return (error);
329 devvp = ndp->ni_vp;
330
331 if (devvp->v_type != VBLK) {
332 vrele(devvp);
333 return (ENOTBLK);
334 }
335 if (major(devvp->v_rdev) >= nblkdev) {
336 vrele(devvp);
337 return (ENXIO);
338 }
339 /*
340 * If mount by non-root, then verify that user has necessary
341 * permissions on the device.
342 */
343 if (p->p_ucred->cr_uid != 0) {
344 accessmode = VREAD;
345 if ((mp->mnt_flag & MNT_RDONLY) == 0)
346 accessmode |= VWRITE;
347 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
348 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
349 if (error) {
350 vput(devvp);
351 return (error);
352 }
353 #ifdef __FreeBSD_version
354 VOP_UNLOCK(devvp, 0, p);
355 #else
356 VOP_UNLOCK(devvp);
357 #endif
358 }
359 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
360 error = mountmsdosfs(devvp, mp, p, &args);
361 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
362 pmp = VFSTOMSDOSFS(mp);
363 #endif
364 } else {
365 if (devvp != pmp->pm_devvp)
366 error = EINVAL; /* XXX needs translation */
367 else
368 vrele(devvp);
369 }
370 if (error) {
371 vrele(devvp);
372 return (error);
373 }
374
375 error = update_mp(mp, &args);
376 if (error) {
377 msdosfs_unmount(mp, MNT_FORCE, p);
378 return error;
379 }
380
381 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
382 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
383 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
384 &size);
385 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
386 (void) msdosfs_statfs(mp, &mp->mnt_stat, p);
387 #ifdef MSDOSFS_DEBUG
388 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
389 #endif
390 return (0);
391 }
392
393 static int
394 mountmsdosfs(devvp, mp, p, argp)
395 struct vnode *devvp;
396 struct mount *mp;
397 struct proc *p;
398 struct msdosfs_args *argp;
399 {
400 struct msdosfsmount *pmp;
401 struct buf *bp;
402 dev_t dev = devvp->v_rdev;
403 #ifndef __FreeBSD__
404 struct partinfo dpart;
405 #endif
406 union bootsector *bsp;
407 struct byte_bpb33 *b33;
408 struct byte_bpb50 *b50;
409 #ifdef PC98
410 u_int pc98_wrk;
411 u_int Phy_Sector_Size;
412 #endif
413 struct byte_bpb710 *b710;
414 u_int8_t SecPerClust;
415 int ronly, error;
416 int bsize = 0, dtype = 0, tmp;
417
418 /*
419 * Disallow multiple mounts of the same device.
420 * Disallow mounting of a device that is currently in use
421 * (except for root, which might share swap device for miniroot).
422 * Flush out any old buffers remaining from a previous use.
423 */
424 error = vfs_mountedon(devvp);
425 if (error)
426 return (error);
427 if (vcount(devvp) > 1 && devvp != rootvp)
428 return (EBUSY);
429 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
430 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
431 #ifdef __FreeBSD_version
432 VOP_UNLOCK(devvp, 0, p);
433 #else
434 VOP_UNLOCK(devvp);
435 #endif
436 if (error)
437 return (error);
438
439 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
440 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
441 if (error)
442 return (error);
443
444 bp = NULL; /* both used in error_exit */
445 pmp = NULL;
446
447 #ifndef __FreeBSD__
448 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
449 /*
450 * We need the disklabel to calculate the size of a FAT entry
451 * later on. Also make sure the partition contains a filesystem
452 * of type FS_MSDOS. This doesn't work for floppies, so we have
453 * to check for them too.
454 *
455 * At least some parts of the msdos fs driver seem to assume
456 * that the size of a disk block will always be 512 bytes.
457 * Let's check it...
458 */
459 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
460 FREAD, NOCRED, p);
461 if (error)
462 goto error_exit;
463 tmp = dpart.part->p_fstype;
464 dtype = dpart.disklab->d_type;
465 bsize = dpart.disklab->d_secsize;
466 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
467 error = EINVAL;
468 goto error_exit;
469 }
470 }
471 #endif
472
473 /*
474 * Read the boot sector of the filesystem, and then check the
475 * boot signature. If not a dos boot sector then error out.
476 */
477 #ifdef PC98
478 devvp->v_flag &= 0xffff;
479 error = bread(devvp, 0, 1024, NOCRED, &bp);
480 #else
481 error = bread(devvp, 0, 512, NOCRED, &bp);
482 #endif
483 if (error)
484 goto error_exit;
485 bp->b_flags |= B_AGE;
486 bsp = (union bootsector *)bp->b_data;
487 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
488 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
489 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
490
491 #ifndef __FreeBSD__
492 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
493 #endif
494 #ifdef PC98
495 if ((bsp->bs50.bsBootSectSig0 != BOOTSIG0
496 || bsp->bs50.bsBootSectSig1 != BOOTSIG1)
497 && (bsp->bs50.bsBootSectSig0 != 0 /* PC98 DOS 3.3x */
498 || bsp->bs50.bsBootSectSig1 != 0)
499 && (bsp->bs50.bsBootSectSig0 != 0x90 /* PC98 DOS 5.0 */
500 || bsp->bs50.bsBootSectSig1 != 0x3d)
501 && (bsp->bs50.bsBootSectSig0 != 0x46 /* PC98 DOS 3.3B */
502 || bsp->bs50.bsBootSectSig1 != 0xfa)) {
503 #else
504 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
505 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
506 #endif
507 error = EINVAL;
508 printf("mountmsdosfs(): bad signature\n");
509 goto error_exit;
510 }
511 #ifndef __FreeBSD__
512 }
513 #endif
514
515 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
516 bzero((caddr_t)pmp, sizeof *pmp);
517 pmp->pm_mountp = mp;
518
519 /*
520 * Compute several useful quantities from the bpb in the
521 * bootsector. Copy in the dos 5 variant of the bpb then fix up
522 * the fields that are different between dos 5 and dos 3.3.
523 */
524 SecPerClust = b50->bpbSecPerClust;
525 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
526 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
527 pmp->pm_FATs = b50->bpbFATs;
528 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
529 pmp->pm_Sectors = getushort(b50->bpbSectors);
530 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
531 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
532 pmp->pm_Heads = getushort(b50->bpbHeads);
533 pmp->pm_Media = b50->bpbMedia;
534
535 #ifndef __FreeBSD__
536 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
537 #endif
538 /* XXX - We should probably check more values here */
539 if (!pmp->pm_BytesPerSec || !SecPerClust
540 || !pmp->pm_Heads || pmp->pm_Heads > 255
541 #ifdef PC98
542 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
543 #else
544 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
545 #endif
546 error = EINVAL;
547 printf("mountmsdosfs(): bad bpb\n");
548 goto error_exit;
549 }
550 #ifndef __FreeBSD__
551 }
552 #endif
553
554 if (pmp->pm_Sectors == 0) {
555 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
556 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
557 } else {
558 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
559 pmp->pm_HugeSectors = pmp->pm_Sectors;
560 }
561 #ifdef PC98 /* for PC98 added Satoshi Yasuda */
562 Phy_Sector_Size = 512;
563 if ((devvp->v_rdev>>8) == 2) { /* floppy check */
564 if (((devvp->v_rdev&077) == 2) && (pmp->pm_HugeSectors == 1232)) {
565 Phy_Sector_Size = 1024; /* 2HD */
566 /*
567 * 1024byte/sector support
568 */
569 devvp->v_flag |= 0x10000;
570 } else {
571 if ((((devvp->v_rdev&077) == 3) /* 2DD 8 or 9 sector */
572 && (pmp->pm_HugeSectors == 1440)) /* 9 sector */
573 || (((devvp->v_rdev&077) == 4)
574 && (pmp->pm_HugeSectors == 1280)) /* 8 sector */
575 || (((devvp->v_rdev&077) == 5)
576 && (pmp->pm_HugeSectors == 2880))) { /* 1.44M */
577 Phy_Sector_Size = 512;
578 } else {
579 if (((devvp->v_rdev&077) != 1)
580 && ((devvp->v_rdev&077) != 0)) { /* 2HC */
581 error = EINVAL;
582 goto error_exit;
583 }
584 }
585 }
586 }
587 pc98_wrk = pmp->pm_BytesPerSec / Phy_Sector_Size;
588 pmp->pm_BytesPerSec = Phy_Sector_Size;
589 SecPerClust = SecPerClust * pc98_wrk;
590 pmp->pm_HugeSectors = pmp->pm_HugeSectors * pc98_wrk;
591 pmp->pm_ResSectors = pmp->pm_ResSectors * pc98_wrk;
592 pmp->pm_FATsecs = pmp->pm_FATsecs * pc98_wrk;
593 pmp->pm_SecPerTrack = pmp->pm_SecPerTrack * pc98_wrk;
594 pmp->pm_HiddenSects = pmp->pm_HiddenSects * pc98_wrk;
595 #endif
596 if (pmp->pm_HugeSectors > 0xffffffff /
597 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
598 /*
599 * We cannot deal currently with this size of disk
600 * due to fileid limitations (see msdosfs_getattr and
601 * msdosfs_readdir)
602 */
603 error = EINVAL;
604 printf("mountmsdosfs(): disk too big, sorry\n");
605 goto error_exit;
606 }
607
608 if (pmp->pm_RootDirEnts == 0) {
609 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
610 || bsp->bs710.bsBootSectSig3 != BOOTSIG3
611 || pmp->pm_Sectors
612 || pmp->pm_FATsecs
613 || getushort(b710->bpbFSVers)) {
614 error = EINVAL;
615 printf("mountmsdosfs(): bad FAT32 filesystem\n");
616 goto error_exit;
617 }
618 pmp->pm_fatmask = FAT32_MASK;
619 pmp->pm_fatmult = 4;
620 pmp->pm_fatdiv = 1;
621 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
622 if (getushort(b710->bpbExtFlags) & FATMIRROR)
623 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
624 else
625 pmp->pm_flags |= MSDOSFS_FATMIRROR;
626 } else
627 pmp->pm_flags |= MSDOSFS_FATMIRROR;
628
629 #ifndef __FreeBSD__
630 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
631 if (FAT32(pmp)) {
632 /*
633 * GEMDOS doesn't know fat32.
634 */
635 error = EINVAL;
636 goto error_exit;
637 }
638
639 /*
640 * Check a few values (could do some more):
641 * - logical sector size: power of 2, >= block size
642 * - sectors per cluster: power of 2, >= 1
643 * - number of sectors: >= 1, <= size of partition
644 */
645 if ( (SecPerClust == 0)
646 || (SecPerClust & (SecPerClust - 1))
647 || (pmp->pm_BytesPerSec < bsize)
648 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
649 || (pmp->pm_HugeSectors == 0)
650 || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
651 > dpart.part->p_size)
652 ) {
653 error = EINVAL;
654 goto error_exit;
655 }
656 /*
657 * XXX - Many parts of the msdos fs driver seem to assume that
658 * the number of bytes per logical sector (BytesPerSec) will
659 * always be the same as the number of bytes per disk block
660 * Let's pretend it is.
661 */
662 tmp = pmp->pm_BytesPerSec / bsize;
663 pmp->pm_BytesPerSec = bsize;
664 pmp->pm_HugeSectors *= tmp;
665 pmp->pm_HiddenSects *= tmp;
666 pmp->pm_ResSectors *= tmp;
667 pmp->pm_Sectors *= tmp;
668 pmp->pm_FATsecs *= tmp;
669 SecPerClust *= tmp;
670 }
671 #endif
672 pmp->pm_fatblk = pmp->pm_ResSectors;
673 if (FAT32(pmp)) {
674 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
675 pmp->pm_firstcluster = pmp->pm_fatblk
676 + (pmp->pm_FATs * pmp->pm_FATsecs);
677 pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
678 } else {
679 pmp->pm_rootdirblk = pmp->pm_fatblk +
680 (pmp->pm_FATs * pmp->pm_FATsecs);
681 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
682 + pmp->pm_BytesPerSec - 1)
683 / pmp->pm_BytesPerSec;/* in sectors */
684 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
685 }
686
687 pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
688 SecPerClust;
689 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
690 pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
691
692 #ifndef __FreeBSD__
693 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
694 if ((pmp->pm_nmbrofclusters <= (0xff0 - 2))
695 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
696 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
697 ) {
698 pmp->pm_fatmask = FAT12_MASK;
699 pmp->pm_fatmult = 3;
700 pmp->pm_fatdiv = 2;
701 } else {
702 pmp->pm_fatmask = FAT16_MASK;
703 pmp->pm_fatmult = 2;
704 pmp->pm_fatdiv = 1;
705 }
706 } else
707 #endif
708 if (pmp->pm_fatmask == 0) {
709 if (pmp->pm_maxcluster
710 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
711 /*
712 * This will usually be a floppy disk. This size makes
713 * sure that one fat entry will not be split across
714 * multiple blocks.
715 */
716 pmp->pm_fatmask = FAT12_MASK;
717 pmp->pm_fatmult = 3;
718 pmp->pm_fatdiv = 2;
719 } else {
720 pmp->pm_fatmask = FAT16_MASK;
721 pmp->pm_fatmult = 2;
722 pmp->pm_fatdiv = 1;
723 }
724 }
725 if (FAT12(pmp))
726 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
727 else
728 pmp->pm_fatblocksize = DFLTBSIZE;
729
730 pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
731 pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
732
733 /*
734 * Compute mask and shift value for isolating cluster relative byte
735 * offsets and cluster numbers from a file offset.
736 */
737 pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
738 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
739 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
740
741 /*
742 * Check for valid cluster size
743 * must be a power of 2
744 */
745 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
746 error = EINVAL;
747 goto error_exit;
748 }
749
750 /*
751 * Release the bootsector buffer.
752 */
753 brelse(bp);
754 bp = NULL;
755
756 /*
757 * Check FSInfo.
758 */
759 if (pmp->pm_fsinfo) {
760 struct fsinfo *fp;
761
762 if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
763 goto error_exit;
764 fp = (struct fsinfo *)bp->b_data;
765 if (!bcmp(fp->fsisig1, "RRaA", 4)
766 && !bcmp(fp->fsisig2, "rrAa", 4)
767 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
768 && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
769 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
770 else
771 pmp->pm_fsinfo = 0;
772 brelse(bp);
773 bp = NULL;
774 }
775
776 /*
777 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
778 */
779
780 /*
781 * Allocate memory for the bitmap of allocated clusters, and then
782 * fill it in.
783 */
784 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
785 / N_INUSEBITS)
786 * sizeof(*pmp->pm_inusemap),
787 M_MSDOSFSFAT, M_WAITOK);
788
789 /*
790 * fillinusemap() needs pm_devvp.
791 */
792 pmp->pm_dev = dev;
793 pmp->pm_devvp = devvp;
794
795 /*
796 * Have the inuse map filled in.
797 */
798 if ((error = fillinusemap(pmp)) != 0)
799 goto error_exit;
800
801 /*
802 * If they want fat updates to be synchronous then let them suffer
803 * the performance degradation in exchange for the on disk copy of
804 * the fat being correct just about all the time. I suppose this
805 * would be a good thing to turn on if the kernel is still flakey.
806 */
807 if (mp->mnt_flag & MNT_SYNCHRONOUS)
808 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
809
810 /*
811 * Finish up.
812 */
813 if (ronly)
814 pmp->pm_flags |= MSDOSFSMNT_RONLY;
815 else
816 pmp->pm_fmod = 1;
817 mp->mnt_data = (qaddr_t) pmp;
818 mp->mnt_stat.f_fsid.val[0] = (long)dev;
819 #ifdef __FreeBSD_version
820 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
821 #else
822 mp->mnt_stat.f_fsid.val[1] = MOUNT_MSDOS;
823 #endif
824 mp->mnt_flag |= MNT_LOCAL;
825 #ifdef __FreeBSD_version
826 devvp->v_specmountpoint = mp;
827 #else
828 devvp->v_specflags |= SI_MOUNTEDON;
829 #endif
830
831 return 0;
832
833 error_exit:
834 if (bp)
835 brelse(bp);
836 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
837 if (pmp) {
838 if (pmp->pm_inusemap)
839 free(pmp->pm_inusemap, M_MSDOSFSFAT);
840 free(pmp, M_MSDOSFSMNT);
841 mp->mnt_data = (qaddr_t)0;
842 }
843 return (error);
844 }
845
846 static int
847 msdosfs_start(mp, flags, p)
848 struct mount *mp;
849 int flags;
850 struct proc *p;
851 {
852
853 return (0);
854 }
855
856 /*
857 * Unmount the filesystem described by mp.
858 */
859 static int
860 msdosfs_unmount(mp, mntflags, p)
861 struct mount *mp;
862 int mntflags;
863 struct proc *p;
864 {
865 struct msdosfsmount *pmp;
866 int error, flags;
867
868 flags = 0;
869 if (mntflags & MNT_FORCE)
870 flags |= FORCECLOSE;
871 error = vflush(mp, NULLVP, flags);
872 if (error)
873 return error;
874 pmp = VFSTOMSDOSFS(mp);
875 #ifdef __FreeBSD_version
876 pmp->pm_devvp->v_specmountpoint = NULL;
877 #else
878 pmp->pm_devvp->v_specflags &= ~SI_MOUNTEDON;
879 #endif
880 #ifdef MSDOSFS_DEBUG
881 {
882 struct vnode *vp = pmp->pm_devvp;
883
884 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
885 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
886 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
887 printf("lastr %d, id %lu, mount %p, op %p\n",
888 vp->v_lastr, vp->v_id, vp->v_mount, vp->v_op);
889 printf("freef %p, freeb %p, mount %p\n",
890 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
891 vp->v_mount);
892 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
893 vp->v_cleanblkhd.lh_first,
894 vp->v_dirtyblkhd.lh_first,
895 vp->v_numoutput, vp->v_type);
896 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
897 vp->v_socket, vp->v_tag,
898 ((u_int *)vp->v_data)[0],
899 ((u_int *)vp->v_data)[1]);
900 }
901 #endif
902 error = VOP_CLOSE(pmp->pm_devvp,
903 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
904 NOCRED, p);
905 vrele(pmp->pm_devvp);
906 free(pmp->pm_inusemap, M_MSDOSFSFAT);
907 free(pmp, M_MSDOSFSMNT);
908 mp->mnt_data = (qaddr_t)0;
909 mp->mnt_flag &= ~MNT_LOCAL;
910 return (error);
911 }
912
913 static int
914 msdosfs_root(mp, vpp)
915 struct mount *mp;
916 struct vnode **vpp;
917 {
918 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
919 struct denode *ndep;
920 int error;
921
922 #ifdef MSDOSFS_DEBUG
923 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
924 #endif
925 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
926 if (error)
927 return (error);
928 *vpp = DETOV(ndep);
929 return (0);
930 }
931
932 static int
933 msdosfs_quotactl(mp, cmds, uid, arg, p)
934 struct mount *mp;
935 int cmds;
936 uid_t uid;
937 caddr_t arg;
938 struct proc *p;
939 {
940 return EOPNOTSUPP;
941 }
942
943 static int
944 msdosfs_statfs(mp, sbp, p)
945 struct mount *mp;
946 struct statfs *sbp;
947 struct proc *p;
948 {
949 struct msdosfsmount *pmp;
950
951 pmp = VFSTOMSDOSFS(mp);
952 #ifndef __FreeBSD_version
953 sbp->f_type = MOUNT_MSDOS;
954 #endif
955 sbp->f_bsize = pmp->pm_bpcluster;
956 sbp->f_iosize = pmp->pm_bpcluster;
957 sbp->f_blocks = pmp->pm_nmbrofclusters;
958 sbp->f_bfree = pmp->pm_freeclustercount;
959 sbp->f_bavail = pmp->pm_freeclustercount;
960 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
961 sbp->f_ffree = 0; /* what to put in here? */
962 if (sbp != &mp->mnt_stat) {
963 #ifdef __FreeBSD_version
964 sbp->f_type = mp->mnt_vfc->vfc_typenum;
965 #endif
966 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
967 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
968 }
969 #ifdef __FreeBSD_version
970 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
971 #endif
972 return (0);
973 }
974
975 static int
976 msdosfs_sync(mp, waitfor, cred, p)
977 struct mount *mp;
978 int waitfor;
979 struct ucred *cred;
980 struct proc *p;
981 {
982 struct vnode *vp, *nvp;
983 struct denode *dep;
984 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
985 int error, allerror = 0;
986
987 /*
988 * If we ever switch to not updating all of the fats all the time,
989 * this would be the place to update them from the first one.
990 */
991 if (pmp->pm_fmod != 0)
992 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
993 panic("msdosfs_sync: rofs mod");
994 else {
995 /* update fats here */
996 }
997 /*
998 * Write back each (modified) denode.
999 */
1000 simple_lock(&mntvnode_slock);
1001 loop:
1002 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
1003 /*
1004 * If the vnode that we are about to sync is no longer
1005 * associated with this mount point, start over.
1006 */
1007 if (vp->v_mount != mp)
1008 goto loop;
1009
1010 simple_lock(&vp->v_interlock);
1011 nvp = vp->v_mntvnodes.le_next;
1012 dep = VTODE(vp);
1013 if (vp->v_type == VNON ||
1014 (dep->de_flag &
1015 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
1016 (vp->v_dirtyblkhd.lh_first == NULL
1017 #ifdef __FreeBSD_version
1018 || waitfor == MNT_LAZY
1019 #endif
1020 )) {
1021
1022 simple_unlock(&vp->v_interlock);
1023 continue;
1024 }
1025 simple_unlock(&mntvnode_slock);
1026 #ifdef __FreeBSD_version
1027 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
1028 #else
1029 error = vget(vp, 1);
1030 #endif
1031 if (error) {
1032 simple_lock(&mntvnode_slock);
1033 if (error == ENOENT)
1034 goto loop;
1035 continue;
1036 }
1037 error = VOP_FSYNC(vp, cred, waitfor, p);
1038 if (error)
1039 allerror = error;
1040 #ifdef __FreeBSD_version
1041 VOP_UNLOCK(vp, 0, p);
1042 #else
1043 VOP_UNLOCK(vp);
1044 #endif
1045 vrele(vp);
1046 simple_lock(&mntvnode_slock);
1047 }
1048 simple_unlock(&mntvnode_slock);
1049
1050 /*
1051 * Flush filesystem control info.
1052 */
1053 #ifdef __FreeBSD_version
1054 if (waitfor != MNT_LAZY) {
1055 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
1056 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
1057 if (error)
1058 allerror = error;
1059 VOP_UNLOCK(pmp->pm_devvp, 0, p);
1060 }
1061 #else
1062 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
1063 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
1064 if (error)
1065 allerror = error;
1066 VOP_UNLOCK(pmp->pm_devvp);
1067 #endif
1068 return (allerror);
1069 }
1070
1071 static int
1072 msdosfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
1073 struct mount *mp;
1074 struct fid *fhp;
1075 #ifdef __FreeBSD_version
1076 struct sockaddr *nam;
1077 #else
1078 struct mbuf *nam;
1079 #endif
1080 struct vnode **vpp;
1081 int *exflagsp;
1082 struct ucred **credanonp;
1083 {
1084 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1085 struct defid *defhp = (struct defid *) fhp;
1086 struct denode *dep;
1087 struct netcred *np;
1088 int error;
1089
1090 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
1091 if (np == NULL)
1092 return (EACCES);
1093 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
1094 if (error) {
1095 *vpp = NULLVP;
1096 return (error);
1097 }
1098 *vpp = DETOV(dep);
1099 *exflagsp = np->netc_exflags;
1100 *credanonp = &np->netc_anon;
1101 return (0);
1102 }
1103
1104 static int
1105 msdosfs_vptofh(vp, fhp)
1106 struct vnode *vp;
1107 struct fid *fhp;
1108 {
1109 struct denode *dep;
1110 struct defid *defhp;
1111
1112 dep = VTODE(vp);
1113 defhp = (struct defid *)fhp;
1114 defhp->defid_len = sizeof(struct defid);
1115 defhp->defid_dirclust = dep->de_dirclust;
1116 defhp->defid_dirofs = dep->de_diroffset;
1117 /* defhp->defid_gen = dep->de_gen; */
1118 return (0);
1119 }
1120
1121 static int
1122 msdosfs_vget(mp, ino, vpp)
1123 struct mount *mp;
1124 ino_t ino;
1125 struct vnode **vpp;
1126 {
1127 return EOPNOTSUPP;
1128 }
1129
1130 static struct vfsops msdosfs_vfsops = {
1131 msdosfs_mount,
1132 msdosfs_start,
1133 msdosfs_unmount,
1134 msdosfs_root,
1135 msdosfs_quotactl,
1136 msdosfs_statfs,
1137 msdosfs_sync,
1138 msdosfs_vget,
1139 msdosfs_fhtovp,
1140 msdosfs_vptofh,
1141 msdosfs_init
1142 };
1143
1144 VFS_SET(msdosfs_vfsops, msdos, MOUNT_MSDOS, 0);
Cache object: a19360f92dcba36053fdd5e6033cd5a3
|