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 <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */
59 #include <sys/mount.h>
60 #include <sys/buf.h>
61 #include <sys/fcntl.h>
62 #include <sys/malloc.h>
63 #include <sys/stat.h> /* defines ALLPERMS */
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 #if 1 /*def PC98*/
73 /*
74 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
75 * garbage or a random value :-{
76 * If you want to use that broken-signatured media, define the
77 * following symbol even though PC/AT.
78 * (ex. mount PC-98 DOS formatted FD on PC/AT)
79 */
80 #define MSDOSFS_NOCHECKSIG
81 #endif
82
83 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
84 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
85
86 static int update_mp __P((struct mount *mp, struct msdosfs_args *argp));
87 static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
88 struct proc *p, struct msdosfs_args *argp));
89 static int msdosfs_fhtovp __P((struct mount *, struct fid *,
90 struct sockaddr *, struct vnode **, int *,
91 struct ucred **));
92 static int msdosfs_mount __P((struct mount *, char *, caddr_t,
93 struct nameidata *, struct proc *));
94 static int msdosfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
95 struct proc *));
96 static int msdosfs_root __P((struct mount *, struct vnode **));
97 static int msdosfs_start __P((struct mount *, int, struct proc *));
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_vget __P((struct mount *mp, ino_t ino,
104 struct vnode **vpp));
105 static int msdosfs_vptofh __P((struct vnode *, struct fid *));
106
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 mp->mnt_op = &msdosfs_vfsops;
182 mp->mnt_flag = 0;
183 LIST_INIT(&mp->mnt_vnodelist);
184
185 args.flags = 0;
186 args.uid = 0;
187 args.gid = 0;
188 args.mask = 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 CIRCLEQ_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, NULLVP, 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
308 if (devvp->v_type != VBLK) {
309 vrele(devvp);
310 return (ENOTBLK);
311 }
312 if (major(devvp->v_rdev) >= nblkdev ||
313 bdevsw[major(devvp->v_rdev)] == NULL) {
314 vrele(devvp);
315 return (ENXIO);
316 }
317 /*
318 * If mount by non-root, then verify that user has necessary
319 * permissions on the device.
320 */
321 if (p->p_ucred->cr_uid != 0) {
322 accessmode = VREAD;
323 if ((mp->mnt_flag & MNT_RDONLY) == 0)
324 accessmode |= VWRITE;
325 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
326 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
327 if (error) {
328 vput(devvp);
329 return (error);
330 }
331 VOP_UNLOCK(devvp, 0, p);
332 }
333 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
334 error = mountmsdosfs(devvp, mp, p, &args);
335 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
336 pmp = VFSTOMSDOSFS(mp);
337 #endif
338 } else {
339 if (devvp != pmp->pm_devvp)
340 error = EINVAL; /* XXX needs translation */
341 else
342 vrele(devvp);
343 }
344 if (error) {
345 vrele(devvp);
346 return (error);
347 }
348
349 error = update_mp(mp, &args);
350 if (error) {
351 msdosfs_unmount(mp, MNT_FORCE, p);
352 return error;
353 }
354
355 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
356 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
357 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
358 &size);
359 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
360 (void) msdosfs_statfs(mp, &mp->mnt_stat, p);
361 #ifdef MSDOSFS_DEBUG
362 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
363 #endif
364 return (0);
365 }
366
367 static int
368 mountmsdosfs(devvp, mp, p, argp)
369 struct vnode *devvp;
370 struct mount *mp;
371 struct proc *p;
372 struct msdosfs_args *argp;
373 {
374 struct msdosfsmount *pmp;
375 struct buf *bp;
376 dev_t dev = devvp->v_rdev;
377 #ifndef __FreeBSD__
378 struct partinfo dpart;
379 int bsize = 0, dtype = 0, tmp;
380 #endif
381 union bootsector *bsp;
382 struct byte_bpb33 *b33;
383 struct byte_bpb50 *b50;
384 struct byte_bpb710 *b710;
385 u_int8_t SecPerClust;
386 int ronly, error;
387
388 /*
389 * Disallow multiple mounts of the same device.
390 * Disallow mounting of a device that is currently in use
391 * (except for root, which might share swap device for miniroot).
392 * Flush out any old buffers remaining from a previous use.
393 */
394 error = vfs_mountedon(devvp);
395 if (error)
396 return (error);
397 if (vcount(devvp) > 1 && devvp != rootvp)
398 return (EBUSY);
399 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
400 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
401 VOP_UNLOCK(devvp, 0, p);
402 if (error)
403 return (error);
404
405 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
406 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
407 if (error)
408 return (error);
409
410 bp = NULL; /* both used in error_exit */
411 pmp = NULL;
412
413 #ifndef __FreeBSD__
414 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
415 /*
416 * We need the disklabel to calculate the size of a FAT entry
417 * later on. Also make sure the partition contains a filesystem
418 * of type FS_MSDOS. This doesn't work for floppies, so we have
419 * to check for them too.
420 *
421 * At least some parts of the msdos fs driver seem to assume
422 * that the size of a disk block will always be 512 bytes.
423 * Let's check it...
424 */
425 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
426 FREAD, NOCRED, p);
427 if (error)
428 goto error_exit;
429 tmp = dpart.part->p_fstype;
430 dtype = dpart.disklab->d_type;
431 bsize = dpart.disklab->d_secsize;
432 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
433 error = EINVAL;
434 goto error_exit;
435 }
436 }
437 #endif
438
439 /*
440 * Read the boot sector of the filesystem, and then check the
441 * boot signature. If not a dos boot sector then error out.
442 *
443 * NOTE: 2048 is a maximum sector size in current...
444 */
445 error = bread(devvp, 0, 2048, NOCRED, &bp);
446 if (error)
447 goto error_exit;
448 bp->b_flags |= B_AGE;
449 bsp = (union bootsector *)bp->b_data;
450 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
451 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
452 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
453
454 #ifndef __FreeBSD__
455 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
456 #endif
457 #ifndef MSDOSFS_NOCHECKSIG
458 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
459 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
460 error = EINVAL;
461 goto error_exit;
462 }
463 #endif
464 #ifndef __FreeBSD__
465 }
466 #endif
467
468 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
469 bzero((caddr_t)pmp, sizeof *pmp);
470 pmp->pm_mountp = mp;
471
472 /*
473 * Compute several useful quantities from the bpb in the
474 * bootsector. Copy in the dos 5 variant of the bpb then fix up
475 * the fields that are different between dos 5 and dos 3.3.
476 */
477 SecPerClust = b50->bpbSecPerClust;
478 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
479 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
480 pmp->pm_FATs = b50->bpbFATs;
481 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
482 pmp->pm_Sectors = getushort(b50->bpbSectors);
483 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
484 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
485 pmp->pm_Heads = getushort(b50->bpbHeads);
486 pmp->pm_Media = b50->bpbMedia;
487
488 /* calculate the ratio of sector size to DEV_BSIZE */
489 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
490
491 #ifndef __FreeBSD__
492 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
493 #endif
494 /* XXX - We should probably check more values here */
495 if (!pmp->pm_BytesPerSec || !SecPerClust
496 || !pmp->pm_Heads || pmp->pm_Heads > 255
497 #ifdef PC98
498 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
499 #else
500 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
501 #endif
502 error = EINVAL;
503 goto error_exit;
504 }
505 #ifndef __FreeBSD__
506 }
507 #endif
508
509 if (pmp->pm_Sectors == 0) {
510 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
511 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
512 } else {
513 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
514 pmp->pm_HugeSectors = pmp->pm_Sectors;
515 }
516 if (pmp->pm_HugeSectors > 0xffffffff /
517 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
518 /*
519 * We cannot deal currently with this size of disk
520 * due to fileid limitations (see msdosfs_getattr and
521 * msdosfs_readdir)
522 */
523 error = EINVAL;
524 printf("mountmsdosfs(): disk too big, sorry\n");
525 goto error_exit;
526 }
527
528 if (pmp->pm_RootDirEnts == 0) {
529 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
530 || bsp->bs710.bsBootSectSig3 != BOOTSIG3
531 || pmp->pm_Sectors
532 || pmp->pm_FATsecs
533 || getushort(b710->bpbFSVers)) {
534 error = EINVAL;
535 printf("mountmsdosfs(): bad FAT32 filesystem\n");
536 goto error_exit;
537 }
538 pmp->pm_fatmask = FAT32_MASK;
539 pmp->pm_fatmult = 4;
540 pmp->pm_fatdiv = 1;
541 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
542 if (getushort(b710->bpbExtFlags) & FATMIRROR)
543 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
544 else
545 pmp->pm_flags |= MSDOSFS_FATMIRROR;
546 } else
547 pmp->pm_flags |= MSDOSFS_FATMIRROR;
548
549 /*
550 * Check a few values (could do some more):
551 * - logical sector size: power of 2, >= block size
552 * - sectors per cluster: power of 2, >= 1
553 * - number of sectors: >= 1, <= size of partition
554 */
555 if ( (SecPerClust == 0)
556 || (SecPerClust & (SecPerClust - 1))
557 || (pmp->pm_BytesPerSec < DEV_BSIZE)
558 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
559 || (pmp->pm_HugeSectors == 0)
560 ) {
561 error = EINVAL;
562 goto error_exit;
563 }
564
565 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
566 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
567 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
568 SecPerClust *= pmp->pm_BlkPerSec;
569
570 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
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_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
586 SecPerClust;
587 pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
588 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
589
590 #ifndef __FreeBSD__
591 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
592 if ((pmp->pm_nmbrofclusters <= (0xff0 - 2))
593 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
594 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
595 ) {
596 pmp->pm_fatmask = FAT12_MASK;
597 pmp->pm_fatmult = 3;
598 pmp->pm_fatdiv = 2;
599 } else {
600 pmp->pm_fatmask = FAT16_MASK;
601 pmp->pm_fatmult = 2;
602 pmp->pm_fatdiv = 1;
603 }
604 } else
605 #endif
606 if (pmp->pm_fatmask == 0) {
607 if (pmp->pm_maxcluster
608 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
609 /*
610 * This will usually be a floppy disk. This size makes
611 * sure that one fat entry will not be split across
612 * multiple blocks.
613 */
614 pmp->pm_fatmask = FAT12_MASK;
615 pmp->pm_fatmult = 3;
616 pmp->pm_fatdiv = 2;
617 } else {
618 pmp->pm_fatmask = FAT16_MASK;
619 pmp->pm_fatmult = 2;
620 pmp->pm_fatdiv = 1;
621 }
622 }
623 if (FAT12(pmp))
624 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
625 else
626 pmp->pm_fatblocksize = DFLTBSIZE;
627
628 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
629 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
630
631 /*
632 * Compute mask and shift value for isolating cluster relative byte
633 * offsets and cluster numbers from a file offset.
634 */
635 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
636 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
637 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
638
639 /*
640 * Check for valid cluster size
641 * must be a power of 2
642 */
643 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
644 error = EINVAL;
645 goto error_exit;
646 }
647
648 /*
649 * Release the bootsector buffer.
650 */
651 brelse(bp);
652 bp = NULL;
653
654 /*
655 * Check FSInfo.
656 */
657 if (pmp->pm_fsinfo) {
658 struct fsinfo *fp;
659
660 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
661 NOCRED, &bp)) != 0)
662 goto error_exit;
663 fp = (struct fsinfo *)bp->b_data;
664 if (!bcmp(fp->fsisig1, "RRaA", 4)
665 && !bcmp(fp->fsisig2, "rrAa", 4)
666 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
667 && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
668 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
669 else
670 pmp->pm_fsinfo = 0;
671 brelse(bp);
672 bp = NULL;
673 }
674
675 /*
676 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
677 */
678
679 /*
680 * Allocate memory for the bitmap of allocated clusters, and then
681 * fill it in.
682 */
683 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
684 / N_INUSEBITS)
685 * sizeof(*pmp->pm_inusemap),
686 M_MSDOSFSFAT, M_WAITOK);
687
688 /*
689 * fillinusemap() needs pm_devvp.
690 */
691 pmp->pm_dev = dev;
692 pmp->pm_devvp = devvp;
693
694 /*
695 * Have the inuse map filled in.
696 */
697 if ((error = fillinusemap(pmp)) != 0)
698 goto error_exit;
699
700 /*
701 * If they want fat updates to be synchronous then let them suffer
702 * the performance degradation in exchange for the on disk copy of
703 * the fat being correct just about all the time. I suppose this
704 * would be a good thing to turn on if the kernel is still flakey.
705 */
706 if (mp->mnt_flag & MNT_SYNCHRONOUS)
707 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
708
709 /*
710 * Finish up.
711 */
712 if (ronly)
713 pmp->pm_flags |= MSDOSFSMNT_RONLY;
714 else
715 pmp->pm_fmod = 1;
716 mp->mnt_data = (qaddr_t) pmp;
717 mp->mnt_stat.f_fsid.val[0] = (long)dev;
718 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
719 mp->mnt_flag |= MNT_LOCAL;
720 devvp->v_specmountpoint = mp;
721
722 return 0;
723
724 error_exit:
725 if (bp)
726 brelse(bp);
727 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
728 if (pmp) {
729 if (pmp->pm_inusemap)
730 free(pmp->pm_inusemap, M_MSDOSFSFAT);
731 free(pmp, M_MSDOSFSMNT);
732 mp->mnt_data = (qaddr_t)0;
733 }
734 return (error);
735 }
736
737 static int
738 msdosfs_start(mp, flags, p)
739 struct mount *mp;
740 int flags;
741 struct proc *p;
742 {
743
744 return (0);
745 }
746
747 /*
748 * Unmount the filesystem described by mp.
749 */
750 static int
751 msdosfs_unmount(mp, mntflags, p)
752 struct mount *mp;
753 int mntflags;
754 struct proc *p;
755 {
756 struct msdosfsmount *pmp;
757 int error, flags;
758
759 flags = 0;
760 if (mntflags & MNT_FORCE)
761 flags |= FORCECLOSE;
762 error = vflush(mp, NULLVP, flags);
763 if (error)
764 return error;
765 pmp = VFSTOMSDOSFS(mp);
766 pmp->pm_devvp->v_specmountpoint = NULL;
767 #ifdef MSDOSFS_DEBUG
768 {
769 struct vnode *vp = pmp->pm_devvp;
770
771 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
772 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
773 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
774 printf("lastr %d, id %lu, mount %p, op %p\n",
775 vp->v_lastr, vp->v_id, vp->v_mount, vp->v_op);
776 printf("freef %p, freeb %p, mount %p\n",
777 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
778 vp->v_mount);
779 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
780 TAILQ_FIRST(&vp->v_cleanblkhd),
781 TAILQ_FIRST(&vp->v_dirtyblkhd),
782 vp->v_numoutput, vp->v_type);
783 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
784 vp->v_socket, vp->v_tag,
785 ((u_int *)vp->v_data)[0],
786 ((u_int *)vp->v_data)[1]);
787 }
788 #endif
789 error = VOP_CLOSE(pmp->pm_devvp,
790 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
791 NOCRED, p);
792 vrele(pmp->pm_devvp);
793 free(pmp->pm_inusemap, M_MSDOSFSFAT);
794 free(pmp, M_MSDOSFSMNT);
795 mp->mnt_data = (qaddr_t)0;
796 mp->mnt_flag &= ~MNT_LOCAL;
797 return (error);
798 }
799
800 static int
801 msdosfs_root(mp, vpp)
802 struct mount *mp;
803 struct vnode **vpp;
804 {
805 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
806 struct denode *ndep;
807 int error;
808
809 #ifdef MSDOSFS_DEBUG
810 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
811 #endif
812 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
813 if (error)
814 return (error);
815 *vpp = DETOV(ndep);
816 return (0);
817 }
818
819 static int
820 msdosfs_quotactl(mp, cmds, uid, arg, p)
821 struct mount *mp;
822 int cmds;
823 uid_t uid;
824 caddr_t arg;
825 struct proc *p;
826 {
827 return EOPNOTSUPP;
828 }
829
830 static int
831 msdosfs_statfs(mp, sbp, p)
832 struct mount *mp;
833 struct statfs *sbp;
834 struct proc *p;
835 {
836 struct msdosfsmount *pmp;
837
838 pmp = VFSTOMSDOSFS(mp);
839 sbp->f_bsize = pmp->pm_bpcluster;
840 sbp->f_iosize = pmp->pm_bpcluster;
841 sbp->f_blocks = pmp->pm_nmbrofclusters;
842 sbp->f_bfree = pmp->pm_freeclustercount;
843 sbp->f_bavail = pmp->pm_freeclustercount;
844 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
845 sbp->f_ffree = 0; /* what to put in here? */
846 if (sbp != &mp->mnt_stat) {
847 sbp->f_type = mp->mnt_vfc->vfc_typenum;
848 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
849 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
850 }
851 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
852 return (0);
853 }
854
855 static int
856 msdosfs_sync(mp, waitfor, cred, p)
857 struct mount *mp;
858 int waitfor;
859 struct ucred *cred;
860 struct proc *p;
861 {
862 struct vnode *vp, *nvp;
863 struct denode *dep;
864 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
865 int error, allerror = 0;
866
867 /*
868 * If we ever switch to not updating all of the fats all the time,
869 * this would be the place to update them from the first one.
870 */
871 if (pmp->pm_fmod != 0)
872 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
873 panic("msdosfs_sync: rofs mod");
874 else {
875 /* update fats here */
876 }
877 /*
878 * Write back each (modified) denode.
879 */
880 simple_lock(&mntvnode_slock);
881 loop:
882 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
883 /*
884 * If the vnode that we are about to sync is no longer
885 * associated with this mount point, start over.
886 */
887 if (vp->v_mount != mp)
888 goto loop;
889
890 simple_lock(&vp->v_interlock);
891 nvp = vp->v_mntvnodes.le_next;
892 dep = VTODE(vp);
893 if (vp->v_type == VNON ||
894 (dep->de_flag &
895 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
896 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY)) {
897 simple_unlock(&vp->v_interlock);
898 continue;
899 }
900 simple_unlock(&mntvnode_slock);
901 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
902 if (error) {
903 simple_lock(&mntvnode_slock);
904 if (error == ENOENT)
905 goto loop;
906 continue;
907 }
908 error = VOP_FSYNC(vp, cred, waitfor, p);
909 if (error)
910 allerror = error;
911 VOP_UNLOCK(vp, 0, p);
912 vrele(vp);
913 simple_lock(&mntvnode_slock);
914 }
915 simple_unlock(&mntvnode_slock);
916
917 /*
918 * Flush filesystem control info.
919 */
920 if (waitfor != MNT_LAZY) {
921 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
922 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
923 if (error)
924 allerror = error;
925 VOP_UNLOCK(pmp->pm_devvp, 0, p);
926 }
927 return (allerror);
928 }
929
930 static int
931 msdosfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
932 struct mount *mp;
933 struct fid *fhp;
934 struct sockaddr *nam;
935 struct vnode **vpp;
936 int *exflagsp;
937 struct ucred **credanonp;
938 {
939 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
940 struct defid *defhp = (struct defid *) fhp;
941 struct denode *dep;
942 struct netcred *np;
943 int error;
944
945 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
946 if (np == NULL)
947 return (EACCES);
948 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
949 if (error) {
950 *vpp = NULLVP;
951 return (error);
952 }
953 *vpp = DETOV(dep);
954 *exflagsp = np->netc_exflags;
955 *credanonp = &np->netc_anon;
956 return (0);
957 }
958
959 static int
960 msdosfs_vptofh(vp, fhp)
961 struct vnode *vp;
962 struct fid *fhp;
963 {
964 struct denode *dep;
965 struct defid *defhp;
966
967 dep = VTODE(vp);
968 defhp = (struct defid *)fhp;
969 defhp->defid_len = sizeof(struct defid);
970 defhp->defid_dirclust = dep->de_dirclust;
971 defhp->defid_dirofs = dep->de_diroffset;
972 /* defhp->defid_gen = dep->de_gen; */
973 return (0);
974 }
975
976 static int
977 msdosfs_vget(mp, ino, vpp)
978 struct mount *mp;
979 ino_t ino;
980 struct vnode **vpp;
981 {
982 return EOPNOTSUPP;
983 }
984
985 static struct vfsops msdosfs_vfsops = {
986 msdosfs_mount,
987 msdosfs_start,
988 msdosfs_unmount,
989 msdosfs_root,
990 msdosfs_quotactl,
991 msdosfs_statfs,
992 msdosfs_sync,
993 msdosfs_vget,
994 msdosfs_fhtovp,
995 msdosfs_vptofh,
996 msdosfs_init
997 };
998
999 VFS_SET(msdosfs_vfsops, msdos, 0);
Cache object: 2a9395a60adcdeff1164b50ca2b26e5d
|