Kernel - fix access checks
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_vfsops.c
CommitLineData
50c068cf 1/* $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/sys/msdosfs/Attic/msdosfs_vfsops.c,v 1.60.2.8 2004/03/02 09:43:04 tjr Exp $ */
67863d04 2/* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vfsops.c,v 1.52 2008/09/17 21:44:24 dillon Exp $ */
984263bc
MD
3/* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */
4
5/*-
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * All rights reserved.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36/*
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 *
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
41 *
42 * This software is provided "as is".
43 *
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
48 *
49 * October 1992
50 */
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/conf.h>
984263bc 55#include <sys/proc.h>
fad57d0e 56#include <sys/nlookup.h>
984263bc
MD
57#include <sys/kernel.h>
58#include <sys/vnode.h>
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#include <vm/vm_zone.h>
65
1f2de5d4
MD
66#include "bpb.h"
67#include "bootsect.h"
68#include "direntry.h"
69#include "denode.h"
70#include "msdosfsmount.h"
71#include "fat.h"
984263bc 72
66a1ddf5 73extern struct vop_ops msdosfs_vnode_vops;
0961aa92 74
984263bc
MD
75#define MSDOSFS_DFLTBSIZE 4096
76
77#if 1 /*def PC98*/
78/*
79 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
80 * garbage or a random value :-{
81 * If you want to use that broken-signatured media, define the
82 * following symbol even though PC/AT.
83 * (ex. mount PC-98 DOS formatted FD on PC/AT)
84 */
85#define MSDOSFS_NOCHECKSIG
86#endif
87
88MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
89static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
90
a6ee311a
RG
91static int update_mp (struct mount *mp, struct msdosfs_args *argp);
92static int mountmsdosfs (struct vnode *devvp, struct mount *mp,
acde96db 93 struct msdosfs_args *argp);
67863d04
MD
94static int msdosfs_fhtovp (struct mount *, struct vnode *,
95 struct fid *, struct vnode **);
a6ee311a
RG
96static int msdosfs_checkexp (struct mount *, struct sockaddr *,
97 int *, struct ucred **);
98static int msdosfs_mount (struct mount *, char *, caddr_t,
acde96db 99 struct ucred *);
a6ee311a
RG
100static int msdosfs_root (struct mount *, struct vnode **);
101static int msdosfs_statfs (struct mount *, struct statfs *,
acde96db 102 struct ucred *);
87de5057 103static int msdosfs_sync (struct mount *, int);
acde96db 104static int msdosfs_unmount (struct mount *, int);
a6ee311a 105static int msdosfs_vptofh (struct vnode *, struct fid *);
984263bc
MD
106
107static int
4625f023 108update_mp(struct mount *mp, struct msdosfs_args *argp)
984263bc
MD
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_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
117 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
118 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
119 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
120 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
121 }
122 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
123 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
124 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
125 }
126
984263bc
MD
127 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
128 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
129 else if (!(pmp->pm_flags &
130 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
131 struct vnode *rootvp;
132
133 /*
134 * Try to divine whether to support Win'95 long filenames
135 */
136 if (FAT32(pmp))
137 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
138 else {
139 if ((error = msdosfs_root(mp, &rootvp)) != 0)
140 return error;
141 pmp->pm_flags |= findwin95(VTODE(rootvp))
142 ? MSDOSFSMNT_LONGNAME
143 : MSDOSFSMNT_SHORTNAME;
144 vput(rootvp);
145 }
146 }
147 return 0;
148}
149
984263bc
MD
150/*
151 * mp - path - addr in user space of mount point (ie /usr or whatever)
152 * data - addr in user space of mount params including the name of the block
153 * special file to treat as a filesystem.
154 */
155static int
acde96db 156msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
984263bc
MD
157{
158 struct vnode *devvp; /* vnode for blk device to mount */
159 struct msdosfs_args args; /* will hold data from mount request */
160 /* msdosfs specific mount control block */
161 struct msdosfsmount *pmp = NULL;
162 size_t size;
163 int error, flags;
164 mode_t accessmode;
fad57d0e 165 struct nlookupdata nd;
dadab5e9 166
984263bc
MD
167 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
168 if (error)
169 return (error);
170 if (args.magic != MSDOSFS_ARGSMAGIC)
171 args.flags = 0;
172 /*
173 * If updating, check whether changing from read-only to
174 * read/write; if there is no device name, that's all we do.
175 */
176 if (mp->mnt_flag & MNT_UPDATE) {
177 pmp = VFSTOMSDOSFS(mp);
178 error = 0;
179 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
180 flags = WRITECLOSE;
181 if (mp->mnt_flag & MNT_FORCE)
182 flags |= FORCECLOSE;
183 error = vflush(mp, 0, flags);
b8bd7242
MD
184 if (error == 0) {
185 devvp = pmp->pm_devvp;
186 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
187 VOP_CLOSE(devvp, FREAD|FWRITE);
188 pmp->pm_flags |= MSDOSFSMNT_RONLY;
189 }
984263bc
MD
190 }
191 if (!error && (mp->mnt_flag & MNT_RELOAD))
192 /* not yet implemented */
193 error = EOPNOTSUPP;
194 if (error)
195 return (error);
196 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
197 /*
198 * If upgrade to read-write by non-root, then verify
199 * that user has necessary permissions on the device.
200 */
b8bd7242
MD
201 devvp = pmp->pm_devvp;
202 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
acde96db 203 if (cred->cr_uid != 0) {
cb66845a 204 error = VOP_EACCESS(devvp, VREAD | VWRITE, cred);
984263bc 205 if (error) {
a11aaa81 206 vn_unlock(devvp);
984263bc
MD
207 return (error);
208 }
984263bc 209 }
b8bd7242
MD
210 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
211 VOP_CLOSE(devvp, FREAD);
212 vn_unlock(devvp);
984263bc
MD
213 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
214 }
215 if (args.fspec == 0) {
216#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
217 if (args.flags & MSDOSFSMNT_MNTOPT) {
218 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
219 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
220 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
221 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
222 }
223#endif
224 /*
225 * Process export requests.
226 */
227 return (vfs_export(mp, &pmp->pm_export, &args.export));
228 }
229 }
230 /*
231 * Not an update, or updating the name: look up the name
232 * and verify that it refers to a sensible block device.
233 */
fad57d0e
MD
234 devvp = NULL;
235 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
236 if (error == 0)
237 error = nlookup(&nd);
238 if (error == 0)
28623bf9 239 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
fad57d0e 240 nlookup_done(&nd);
984263bc
MD
241 if (error)
242 return (error);
984263bc
MD
243
244 if (!vn_isdisk(devvp, &error)) {
245 vrele(devvp);
246 return (error);
247 }
248 /*
249 * If mount by non-root, then verify that user has necessary
250 * permissions on the device.
251 */
acde96db 252 if (cred->cr_uid != 0) {
984263bc
MD
253 accessmode = VREAD;
254 if ((mp->mnt_flag & MNT_RDONLY) == 0)
255 accessmode |= VWRITE;
ca466bae 256 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
cb66845a 257 error = VOP_EACCESS(devvp, accessmode, cred);
984263bc
MD
258 if (error) {
259 vput(devvp);
260 return (error);
261 }
a11aaa81 262 vn_unlock(devvp);
984263bc
MD
263 }
264 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
acde96db 265 error = mountmsdosfs(devvp, mp, &args);
086c1d7e 266#ifdef MSDOSFS_DEBUG /* only needed for the kprintf below */
984263bc
MD
267 pmp = VFSTOMSDOSFS(mp);
268#endif
269 } else {
270 if (devvp != pmp->pm_devvp)
271 error = EINVAL; /* XXX needs translation */
272 else
273 vrele(devvp);
274 }
275 if (error) {
276 vrele(devvp);
277 return (error);
278 }
279
280 error = update_mp(mp, &args);
281 if (error) {
acde96db 282 msdosfs_unmount(mp, MNT_FORCE);
984263bc
MD
283 return error;
284 }
285
b1ce5639 286 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
984263bc 287 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
acde96db 288 msdosfs_statfs(mp, &mp->mnt_stat, cred);
984263bc 289#ifdef MSDOSFS_DEBUG
086c1d7e 290 kprintf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
984263bc
MD
291#endif
292 return (0);
293}
294
295static int
acde96db 296mountmsdosfs(struct vnode *devvp, struct mount *mp, struct msdosfs_args *argp)
984263bc
MD
297{
298 struct msdosfsmount *pmp;
299 struct buf *bp;
b13267a5 300 cdev_t dev;
984263bc
MD
301 union bootsector *bsp;
302 struct byte_bpb33 *b33;
303 struct byte_bpb50 *b50;
304 struct byte_bpb710 *b710;
305 u_int8_t SecPerClust;
306 u_long clusters;
307 int ronly, error;
308
309 /*
310 * Disallow multiple mounts of the same device.
311 * Disallow mounting of a device that is currently in use
984263bc
MD
312 * Flush out any old buffers remaining from a previous use.
313 */
314 error = vfs_mountedon(devvp);
315 if (error)
316 return (error);
8be7edad 317 if (vcount(devvp) > 0)
984263bc 318 return (EBUSY);
ca466bae 319 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 320 error = vinvalbuf(devvp, V_SAVE, 0, 0);
a11aaa81 321 vn_unlock(devvp);
984263bc
MD
322 if (error)
323 return (error);
324
325 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
ca466bae 326 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
87de5057 327 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
a11aaa81 328 vn_unlock(devvp);
984263bc
MD
329 if (error)
330 return (error);
e4c9c0c8 331 dev = devvp->v_rdev;
984263bc
MD
332 bp = NULL; /* both used in error_exit */
333 pmp = NULL;
334
984263bc
MD
335 /*
336 * Read the boot sector of the filesystem, and then check the
337 * boot signature. If not a dos boot sector then error out.
338 *
339 * NOTE: 2048 is a maximum sector size in current...
340 */
3b568787 341 error = bread(devvp, 0, 2048, &bp);
984263bc
MD
342 if (error)
343 goto error_exit;
344 bp->b_flags |= B_AGE;
345 bsp = (union bootsector *)bp->b_data;
346 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
347 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
348 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
349
984263bc 350#ifndef MSDOSFS_NOCHECKSIG
ae2b058d
HT
351 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
352 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
353 error = EINVAL;
354 goto error_exit;
984263bc
MD
355 }
356#endif
357
e7b4468c 358 pmp = kmalloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
984263bc
MD
359 pmp->pm_mountp = mp;
360
361 /*
362 * Compute several useful quantities from the bpb in the
363 * bootsector. Copy in the dos 5 variant of the bpb then fix up
364 * the fields that are different between dos 5 and dos 3.3.
365 */
366 SecPerClust = b50->bpbSecPerClust;
367 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
368 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
369 pmp->pm_FATs = b50->bpbFATs;
370 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
371 pmp->pm_Sectors = getushort(b50->bpbSectors);
372 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
373 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
374 pmp->pm_Heads = getushort(b50->bpbHeads);
375 pmp->pm_Media = b50->bpbMedia;
376
377 /* calculate the ratio of sector size to DEV_BSIZE */
378 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
379
ae2b058d
HT
380 /*
381 * We don't check pm_Heads nor pm_SecPerTrack, because
382 * these may not be set for EFI file systems. We don't
383 * use these anyway, so we're unaffected if they are
384 * invalid.
385 */
386 if (!pmp->pm_BytesPerSec || !SecPerClust) {
387 error = EINVAL;
388 goto error_exit;
984263bc 389 }
984263bc
MD
390
391 if (pmp->pm_Sectors == 0) {
392 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
393 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
394 } else {
395 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
396 pmp->pm_HugeSectors = pmp->pm_Sectors;
397 }
984263bc
MD
398
399 if (pmp->pm_RootDirEnts == 0) {
a70dba5d 400 if (pmp->pm_Sectors
984263bc
MD
401 || pmp->pm_FATsecs
402 || getushort(b710->bpbFSVers)) {
403 error = EINVAL;
086c1d7e 404 kprintf("mountmsdosfs(): bad FAT32 filesystem\n");
984263bc
MD
405 goto error_exit;
406 }
407 pmp->pm_fatmask = FAT32_MASK;
408 pmp->pm_fatmult = 4;
409 pmp->pm_fatdiv = 1;
410 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
411 if (getushort(b710->bpbExtFlags) & FATMIRROR)
412 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
413 else
414 pmp->pm_flags |= MSDOSFS_FATMIRROR;
415 } else
416 pmp->pm_flags |= MSDOSFS_FATMIRROR;
417
418 /*
419 * Check a few values (could do some more):
420 * - logical sector size: power of 2, >= block size
421 * - sectors per cluster: power of 2, >= 1
422 * - number of sectors: >= 1, <= size of partition
423 */
424 if ( (SecPerClust == 0)
425 || (SecPerClust & (SecPerClust - 1))
426 || (pmp->pm_BytesPerSec < DEV_BSIZE)
427 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
428 || (pmp->pm_HugeSectors == 0)
429 ) {
430 error = EINVAL;
431 goto error_exit;
432 }
433
434 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
435 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
436 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
437 SecPerClust *= pmp->pm_BlkPerSec;
438
439 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
440
441 if (FAT32(pmp)) {
442 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
443 pmp->pm_firstcluster = pmp->pm_fatblk
444 + (pmp->pm_FATs * pmp->pm_FATsecs);
445 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
446 } else {
447 pmp->pm_rootdirblk = pmp->pm_fatblk +
448 (pmp->pm_FATs * pmp->pm_FATsecs);
449 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
450 + DEV_BSIZE - 1)
451 / DEV_BSIZE; /* in blocks */
452 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
453 }
454
455 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
456 SecPerClust + 1;
457 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
458
984263bc
MD
459 if (pmp->pm_fatmask == 0) {
460 if (pmp->pm_maxcluster
461 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
462 /*
463 * This will usually be a floppy disk. This size makes
464 * sure that one fat entry will not be split across
465 * multiple blocks.
466 */
467 pmp->pm_fatmask = FAT12_MASK;
468 pmp->pm_fatmult = 3;
469 pmp->pm_fatdiv = 2;
470 } else {
471 pmp->pm_fatmask = FAT16_MASK;
472 pmp->pm_fatmult = 2;
473 pmp->pm_fatdiv = 1;
474 }
475 }
476
477 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
478 if (pmp->pm_maxcluster >= clusters) {
086c1d7e 479 kprintf("Warning: number of clusters (%ld) exceeds FAT "
984263bc
MD
480 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
481 pmp->pm_maxcluster = clusters - 1;
482 }
483
484
485 if (FAT12(pmp))
486 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
487 else
488 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
489
490 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
54078292 491 pmp->pm_bnshift = DEV_BSHIFT;
984263bc
MD
492
493 /*
494 * Compute mask and shift value for isolating cluster relative byte
495 * offsets and cluster numbers from a file offset.
496 */
497 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
498 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
499 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
500
501 /*
502 * Check for valid cluster size
503 * must be a power of 2
504 */
505 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
506 error = EINVAL;
507 goto error_exit;
508 }
509
510 /*
511 * Release the bootsector buffer.
512 */
513 brelse(bp);
514 bp = NULL;
515
516 /*
517 * Check FSInfo.
518 */
519 if (pmp->pm_fsinfo) {
520 struct fsinfo *fp;
521
54078292 522 if ((error = bread(devvp, de_bntodoff(pmp, pmp->pm_fsinfo), fsi_size(pmp), &bp)) != 0)
984263bc
MD
523 goto error_exit;
524 fp = (struct fsinfo *)bp->b_data;
525 if (!bcmp(fp->fsisig1, "RRaA", 4)
526 && !bcmp(fp->fsisig2, "rrAa", 4)
527 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
50c068cf 528 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
984263bc 529 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
50c068cf
CP
530 if (pmp->pm_nxtfree == 0xffffffff)
531 pmp->pm_nxtfree = CLUST_FIRST;
532 } else
984263bc
MD
533 pmp->pm_fsinfo = 0;
534 brelse(bp);
535 bp = NULL;
536 }
537
538 /*
539 * Check and validate (or perhaps invalidate?) the fsinfo structure?
540 */
541 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
086c1d7e 542 kprintf(
984263bc
MD
543 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
544 pmp->pm_nxtfree, pmp->pm_maxcluster);
545 error = EINVAL;
546 goto error_exit;
547 }
548
549 /*
550 * Allocate memory for the bitmap of allocated clusters, and then
551 * fill it in.
552 */
77652cad 553 pmp->pm_inusemap = kmalloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
984263bc
MD
554 / N_INUSEBITS)
555 * sizeof(*pmp->pm_inusemap),
556 M_MSDOSFSFAT, M_WAITOK);
557
558 /*
559 * fillinusemap() needs pm_devvp.
560 */
561 pmp->pm_dev = dev;
562 pmp->pm_devvp = devvp;
563
564 /*
565 * Have the inuse map filled in.
566 */
567 if ((error = fillinusemap(pmp)) != 0)
568 goto error_exit;
569
570 /*
571 * If they want fat updates to be synchronous then let them suffer
572 * the performance degradation in exchange for the on disk copy of
573 * the fat being correct just about all the time. I suppose this
574 * would be a good thing to turn on if the kernel is still flakey.
575 */
576 if (mp->mnt_flag & MNT_SYNCHRONOUS)
577 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
578
579 /*
580 * Finish up.
581 */
582 if (ronly)
583 pmp->pm_flags |= MSDOSFSMNT_RONLY;
584 else
585 pmp->pm_fmod = 1;
586 mp->mnt_data = (qaddr_t) pmp;
587 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
588 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
589 mp->mnt_flag |= MNT_LOCAL;
66a1ddf5 590 vfs_add_vnodeops(mp, &msdosfs_vnode_vops, &mp->mnt_vn_norm_ops);
e4c9c0c8 591 dev->si_mountpoint = mp;
984263bc
MD
592
593 return 0;
594
595error_exit:
596 if (bp)
597 brelse(bp);
87de5057 598 VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE);
984263bc
MD
599 if (pmp) {
600 if (pmp->pm_inusemap)
efda3bd0
MD
601 kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
602 kfree(pmp, M_MSDOSFSMNT);
984263bc
MD
603 mp->mnt_data = (qaddr_t)0;
604 }
605 return (error);
606}
607
608/*
609 * Unmount the filesystem described by mp.
610 */
611static int
acde96db 612msdosfs_unmount(struct mount *mp, int mntflags)
984263bc
MD
613{
614 struct msdosfsmount *pmp;
615 int error, flags;
616
617 flags = 0;
618 if (mntflags & MNT_FORCE)
619 flags |= FORCECLOSE;
620 error = vflush(mp, 0, flags);
621 if (error)
622 return error;
623 pmp = VFSTOMSDOSFS(mp);
e4c9c0c8 624 pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
984263bc
MD
625#ifdef MSDOSFS_DEBUG
626 {
627 struct vnode *vp = pmp->pm_devvp;
628
086c1d7e 629 kprintf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
1ffae7f1 630 kprintf("flag %08x, sysrefs %d, writecount %d, auxrefs %d\n",
3c37c940
MD
631 vp->v_flag, vp->v_sysref.refcnt,
632 vp->v_writecount, vp->v_auxrefs);
1ffae7f1 633 kprintf("mount %p, op %p\n", vp->v_mount, vp->v_ops);
2ec4b00d 634 kprintf("mount %p\n", vp->v_mount);
1ffae7f1
MD
635 kprintf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
636 RB_ROOT(&vp->v_rbclean_tree),
637 RB_ROOT(&vp->v_rbdirty_tree),
a9a20f98
MD
638 bio_track_active(&vp->v_track_write),
639 vp->v_type);
086c1d7e 640 kprintf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
984263bc
MD
641 vp->v_socket, vp->v_tag,
642 ((u_int *)vp->v_data)[0],
643 ((u_int *)vp->v_data)[1]);
644 }
645#endif
646 error = VOP_CLOSE(pmp->pm_devvp,
87de5057 647 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE);
984263bc 648 vrele(pmp->pm_devvp);
efda3bd0
MD
649 kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
650 kfree(pmp, M_MSDOSFSMNT);
984263bc
MD
651 mp->mnt_data = (qaddr_t)0;
652 mp->mnt_flag &= ~MNT_LOCAL;
653 return (error);
654}
655
656static int
4625f023 657msdosfs_root(struct mount *mp, struct vnode **vpp)
984263bc
MD
658{
659 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
660 struct denode *ndep;
661 int error;
662
663#ifdef MSDOSFS_DEBUG
086c1d7e 664 kprintf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
984263bc
MD
665#endif
666 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
667 if (error)
668 return (error);
669 *vpp = DETOV(ndep);
670 return (0);
671}
672
673static int
acde96db 674msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
984263bc
MD
675{
676 struct msdosfsmount *pmp;
677
678 pmp = VFSTOMSDOSFS(mp);
679 sbp->f_bsize = pmp->pm_bpcluster;
680 sbp->f_iosize = pmp->pm_bpcluster;
681 sbp->f_blocks = pmp->pm_maxcluster + 1;
682 sbp->f_bfree = pmp->pm_freeclustercount;
683 sbp->f_bavail = pmp->pm_freeclustercount;
684 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
685 sbp->f_ffree = 0; /* what to put in here? */
686 if (sbp != &mp->mnt_stat) {
687 sbp->f_type = mp->mnt_vfc->vfc_typenum;
984263bc
MD
688 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
689 }
690 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
691 return (0);
692}
693
41a01a4d
MD
694struct scaninfo {
695 int rescan;
696 int allerror;
697 int waitfor;
41a01a4d
MD
698};
699
5fd012e0 700static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data);
41a01a4d 701
984263bc 702static int
87de5057 703msdosfs_sync(struct mount *mp, int waitfor)
984263bc 704{
984263bc 705 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
41a01a4d
MD
706 struct scaninfo scaninfo;
707 int error;
984263bc
MD
708
709 /*
710 * If we ever switch to not updating all of the fats all the time,
711 * this would be the place to update them from the first one.
712 */
713 if (pmp->pm_fmod != 0) {
714 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
715 panic("msdosfs_sync: rofs mod");
716 else {
717 /* update fats here */
718 }
719 }
720 /*
721 * Write back each (modified) denode.
722 */
41a01a4d
MD
723 scaninfo.allerror = 0;
724 scaninfo.rescan = 1;
41a01a4d
MD
725 while (scaninfo.rescan) {
726 scaninfo.rescan = 0;
5fd012e0 727 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL, msdosfs_sync_scan, &scaninfo);
984263bc 728 }
984263bc
MD
729
730 /*
731 * Flush filesystem control info.
732 */
733 if (waitfor != MNT_LAZY) {
ca466bae 734 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
52174f71 735 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor, 0)) != 0)
41a01a4d 736 scaninfo.allerror = error;
a11aaa81 737 vn_unlock(pmp->pm_devvp);
41a01a4d
MD
738 }
739 return (scaninfo.allerror);
740}
741
4625f023 742static int
5fd012e0 743msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data)
41a01a4d
MD
744{
745 struct scaninfo *info = data;
746 struct denode *dep;
747 int error;
748
749 dep = VTODE(vp);
986e7cda 750 if (vp->v_type == VNON || vp->v_type == VBAD ||
41a01a4d
MD
751 ((dep->de_flag &
752 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
6bae6177 753 (RB_EMPTY(&vp->v_rbdirty_tree) || info->waitfor == MNT_LAZY))) {
41a01a4d 754 return(0);
984263bc 755 }
52174f71 756 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
41a01a4d 757 info->allerror = error;
41a01a4d 758 return(0);
984263bc
MD
759}
760
761static int
67863d04
MD
762msdosfs_fhtovp(struct mount *mp, struct vnode *rootvp,
763 struct fid *fhp, struct vnode **vpp)
984263bc
MD
764{
765 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
766 struct defid *defhp = (struct defid *) fhp;
767 struct denode *dep;
768 int error;
769
770 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
771 if (error) {
772 *vpp = NULLVP;
773 return (error);
774 }
775 *vpp = DETOV(dep);
776 return (0);
777}
778
779static int
4625f023
CP
780msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
781 struct ucred **credanonp)
984263bc
MD
782{
783 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
784 struct netcred *np;
785
786 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
787 if (np == NULL)
788 return (EACCES);
789 *exflagsp = np->netc_exflags;
790 *credanonp = &np->netc_anon;
791 return (0);
792}
793
794static int
4625f023 795msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
984263bc
MD
796{
797 struct denode *dep;
798 struct defid *defhp;
799
800 dep = VTODE(vp);
801 defhp = (struct defid *)fhp;
802 defhp->defid_len = sizeof(struct defid);
803 defhp->defid_dirclust = dep->de_dirclust;
804 defhp->defid_dirofs = dep->de_diroffset;
805 /* defhp->defid_gen = dep->de_gen; */
806 return (0);
807}
808
809static struct vfsops msdosfs_vfsops = {
43c45e8f
HP
810 .vfs_mount = msdosfs_mount,
811 .vfs_unmount = msdosfs_unmount,
812 .vfs_root = msdosfs_root,
813 .vfs_statfs = msdosfs_statfs,
814 .vfs_sync = msdosfs_sync,
815 .vfs_fhtovp = msdosfs_fhtovp,
816 .vfs_checkexp = msdosfs_checkexp,
817 .vfs_vptofh = msdosfs_vptofh,
818 .vfs_init = msdosfs_init,
819 .vfs_uninit = msdosfs_uninit
984263bc
MD
820};
821
822VFS_SET(msdosfs_vfsops, msdos, 0);