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