1 /* $FreeBSD: releng/6.4/sys/fs/msdosfs/msdosfs_vfsops.c 181729 2008-08-14 12:33:40Z kib $ */
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/bio.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 <sys/iconv.h>
65 #include <sys/mutex.h>
66
67 #include <fs/msdosfs/bpb.h>
68 #include <fs/msdosfs/bootsect.h>
69 #include <fs/msdosfs/msdosfsmount.h>
70 #include <fs/msdosfs/direntry.h>
71 #include <fs/msdosfs/denode.h>
72 #include <fs/msdosfs/fat.h>
73
74 #include <geom/geom.h>
75 #include <geom/geom_vfs.h>
76
77 /* List of mount options we support */
78 static const char *msdosfs_opts[] = {
79 "from",
80 "atime", "export", "force", "sync",
81 "uid", "gid", "mask", "dirmask",
82 "shortname", "shortnames", "longname", "longnames", "nowin95", "win95",
83 "kiconv", "cs_win", "cs_dos", "cs_local", "large",
84 NULL
85 };
86
87 #define MSDOSFS_DFLTBSIZE 4096
88
89 #if 1 /*def PC98*/
90 /*
91 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
92 * garbage or a random value :-{
93 * If you want to use that broken-signatured media, define the
94 * following symbol even though PC/AT.
95 * (ex. mount PC-98 DOS formatted FD on PC/AT)
96 */
97 #define MSDOSFS_NOCHECKSIG
98 #endif
99
100 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
101 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
102
103 struct iconv_functions *msdosfs_iconv = NULL;
104
105 static int update_mp(struct mount *mp, struct thread *td);
106 static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
107 struct thread *td);
108 static vfs_fhtovp_t msdosfs_fhtovp;
109 static vfs_mount_t msdosfs_mount;
110 static vfs_root_t msdosfs_root;
111 static vfs_statfs_t msdosfs_statfs;
112 static vfs_sync_t msdosfs_sync;
113 static vfs_unmount_t msdosfs_unmount;
114 static vfs_vptofh_t msdosfs_vptofh;
115
116 /* Maximum length of a character set name (arbitrary). */
117 #define MAXCSLEN 64
118
119 static int
120 update_mp(mp, td)
121 struct mount *mp;
122 struct thread *td;
123 {
124 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
125 void *dos, *win, *local;
126 int error, v;
127
128 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
129 if (msdosfs_iconv != NULL) {
130 error = vfs_getopt(mp->mnt_optnew,
131 "cs_win", &win, NULL);
132 if (!error)
133 error = vfs_getopt(mp->mnt_optnew,
134 "cs_local", &local, NULL);
135 if (!error)
136 error = vfs_getopt(mp->mnt_optnew,
137 "cs_dos", &dos, NULL);
138 if (!error) {
139 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
140 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
141 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
142 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
143 }
144 if (error != 0)
145 return (error);
146 } else {
147 pmp->pm_w2u = NULL;
148 pmp->pm_u2w = NULL;
149 pmp->pm_d2u = NULL;
150 pmp->pm_u2d = NULL;
151 }
152 }
153
154 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
155 pmp->pm_gid = v;
156 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
157 pmp->pm_uid = v;
158 if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v))
159 pmp->pm_mask = v & ALLPERMS;
160 if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v))
161 pmp->pm_dirmask = v & ALLPERMS;
162 vfs_flagopt(mp->mnt_optnew, "shortname",
163 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
164 vfs_flagopt(mp->mnt_optnew, "shortnames",
165 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
166 vfs_flagopt(mp->mnt_optnew, "longname",
167 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
168 vfs_flagopt(mp->mnt_optnew, "longnames",
169 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
170 vfs_flagopt(mp->mnt_optnew, "kiconv",
171 &pmp->pm_flags, MSDOSFSMNT_KICONV);
172
173 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
174 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
175 else
176 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
177
178 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
179 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
180 else if (!(pmp->pm_flags &
181 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
182 struct vnode *rootvp;
183
184 /*
185 * Try to divine whether to support Win'95 long filenames
186 */
187 if (FAT32(pmp))
188 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
189 else {
190 if ((error =
191 msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
192 return error;
193 pmp->pm_flags |= findwin95(VTODE(rootvp))
194 ? MSDOSFSMNT_LONGNAME
195 : MSDOSFSMNT_SHORTNAME;
196 vput(rootvp);
197 }
198 }
199 return 0;
200 }
201
202 static int
203 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
204 {
205 struct msdosfs_args args;
206 int error;
207
208 if (data == NULL)
209 return (EINVAL);
210 error = copyin(data, &args, sizeof args);
211 if (error)
212 return (error);
213
214 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
215 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
216 ma = mount_argf(ma, "uid", "%d", args.uid);
217 ma = mount_argf(ma, "gid", "%d", args.gid);
218 ma = mount_argf(ma, "mask", "%d", args.mask);
219 ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
220
221 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
222 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
223 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
224 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
225
226 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
227 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
228 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
229
230 error = kernel_mount(ma, flags);
231
232 return (error);
233 }
234
235 /*
236 * mp - path - addr in user space of mount point (ie /usr or whatever)
237 * data - addr in user space of mount params including the name of the block
238 * special file to treat as a filesystem.
239 */
240 static int
241 msdosfs_mount(struct mount *mp, struct thread *td)
242 {
243 struct vnode *devvp; /* vnode for blk device to mount */
244 /* msdosfs specific mount control block */
245 struct msdosfsmount *pmp = NULL;
246 struct nameidata ndp;
247 int error, flags;
248 mode_t accessmode;
249 char *from;
250
251 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
252 return (EINVAL);
253
254 /*
255 * If updating, check whether changing from read-only to
256 * read/write; if there is no device name, that's all we do.
257 */
258 if (mp->mnt_flag & MNT_UPDATE) {
259 int ro_to_rw = 0;
260 pmp = VFSTOMSDOSFS(mp);
261
262 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
263 /*
264 * Forbid export requests if filesystem has
265 * MSDOSFS_LARGEFS flag set.
266 */
267 if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0)
268 return (EOPNOTSUPP);
269 }
270 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
271 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
272 error = VFS_SYNC(mp, MNT_WAIT, td);
273 if (error)
274 return (error);
275 flags = WRITECLOSE;
276 if (mp->mnt_flag & MNT_FORCE)
277 flags |= FORCECLOSE;
278 error = vflush(mp, 0, flags, td);
279 if (error)
280 return (error);
281 DROP_GIANT();
282 g_topology_lock();
283 g_access(pmp->pm_cp, 0, -1, 0);
284 g_topology_unlock();
285 PICKUP_GIANT();
286 /* Now the volume is clean. Mark it. */
287 error = markvoldirty(pmp, 0);
288 if (error && (flags & FORCECLOSE) == 0)
289 return (error);
290 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
291 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
292 /*
293 * If upgrade to read-write by non-root, then verify
294 * that user has necessary permissions on the device.
295 */
296 if (suser(td)) {
297 devvp = pmp->pm_devvp;
298 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
299 error = VOP_ACCESS(devvp, VREAD | VWRITE,
300 td->td_ucred, td);
301 if (error) {
302 VOP_UNLOCK(devvp, 0, td);
303 return (error);
304 }
305 VOP_UNLOCK(devvp, 0, td);
306 }
307 DROP_GIANT();
308 g_topology_lock();
309 error = g_access(pmp->pm_cp, 0, 1, 0);
310 g_topology_unlock();
311 PICKUP_GIANT();
312 if (error)
313 return (error);
314
315 ro_to_rw = 1;
316 }
317 vfs_flagopt(mp->mnt_optnew, "ro",
318 &pmp->pm_flags, MSDOSFSMNT_RONLY);
319 vfs_flagopt(mp->mnt_optnew, "ro",
320 &mp->mnt_flag, MNT_RDONLY);
321
322 if (ro_to_rw) {
323 /* Now that the volume is modifiable, mark it dirty. */
324 error = markvoldirty(pmp, 1);
325 if (error)
326 return (error);
327 }
328
329 if (vfs_getopt(mp->mnt_optnew, "from", NULL, NULL)) {
330 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
331 if (args.flags & MSDOSFSMNT_MNTOPT) {
332 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
333 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
334 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
335 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
336 }
337 #endif
338 }
339 }
340 /*
341 * Not an update, or updating the name: look up the name
342 * and verify that it refers to a sensible disk device.
343 */
344 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
345 return (EINVAL);
346 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
347 error = namei(&ndp);
348 if (error)
349 return (error);
350 devvp = ndp.ni_vp;
351 NDFREE(&ndp, NDF_ONLY_PNBUF);
352
353 if (!vn_isdisk(devvp, &error)) {
354 vput(devvp);
355 return (error);
356 }
357 /*
358 * If mount by non-root, then verify that user has necessary
359 * permissions on the device.
360 */
361 if (suser(td)) {
362 accessmode = VREAD;
363 if ((mp->mnt_flag & MNT_RDONLY) == 0)
364 accessmode |= VWRITE;
365 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
366 if (error) {
367 vput(devvp);
368 return (error);
369 }
370 }
371 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
372 error = mountmsdosfs(devvp, mp, td);
373 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
374 pmp = VFSTOMSDOSFS(mp);
375 #endif
376 } else {
377 if (devvp != pmp->pm_devvp)
378 error = EINVAL; /* XXX needs translation */
379 else
380 vput(devvp);
381 }
382 if (error) {
383 vrele(devvp);
384 return (error);
385 }
386
387 error = update_mp(mp, td);
388 if (error) {
389 if ((mp->mnt_flag & MNT_UPDATE) == 0)
390 msdosfs_unmount(mp, MNT_FORCE, td);
391 return error;
392 }
393
394 vfs_mountedfrom(mp, from);
395 #ifdef MSDOSFS_DEBUG
396 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
397 #endif
398 return (0);
399 }
400
401 static int
402 mountmsdosfs(devvp, mp, td)
403 struct vnode *devvp;
404 struct mount *mp;
405 struct thread *td;
406 {
407 struct msdosfsmount *pmp;
408 struct buf *bp;
409 struct cdev *dev = devvp->v_rdev;
410 union bootsector *bsp;
411 struct byte_bpb33 *b33;
412 struct byte_bpb50 *b50;
413 struct byte_bpb710 *b710;
414 u_int8_t SecPerClust;
415 u_long clusters;
416 int ronly, error;
417 struct g_consumer *cp;
418 struct bufobj *bo;
419
420 ronly = !vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL);
421 /* XXX: use VOP_ACCESS to check FS perms */
422 DROP_GIANT();
423 g_topology_lock();
424 error = g_vfs_open(devvp, &cp, "msdos", ronly ? 0 : 1);
425 g_topology_unlock();
426 PICKUP_GIANT();
427 VOP_UNLOCK(devvp, 0, td);
428 if (error)
429 return (error);
430
431 bo = &devvp->v_bufobj;
432 bp = NULL; /* both used in error_exit */
433 pmp = NULL;
434
435 /*
436 * Read the boot sector of the filesystem, and then check the
437 * boot signature. If not a dos boot sector then error out.
438 *
439 * NOTE: 2048 is a maximum sector size in current...
440 */
441 error = bread(devvp, 0, 2048, NOCRED, &bp);
442 if (error)
443 goto error_exit;
444 bp->b_flags |= B_AGE;
445 bsp = (union bootsector *)bp->b_data;
446 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
447 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
448 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
449
450 #ifndef MSDOSFS_NOCHECKSIG
451 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
452 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
453 error = EINVAL;
454 goto error_exit;
455 }
456 #endif
457
458 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
459 pmp->pm_mountp = mp;
460 pmp->pm_cp = cp;
461 pmp->pm_bo = bo;
462
463 /*
464 * Experimental support for large MS-DOS filesystems.
465 * WARNING: This uses at least 32 bytes of kernel memory (which is not
466 * reclaimed until the FS is unmounted) for each file on disk to map
467 * between the 32-bit inode numbers used by VFS and the 64-bit
468 * pseudo-inode numbers used internally by msdosfs. This is only
469 * safe to use in certain controlled situations (e.g. read-only FS
470 * with less than 1 million files).
471 * Since the mappings do not persist across unmounts (or reboots), these
472 * filesystems are not suitable for exporting through NFS, or any other
473 * application that requires fixed inode numbers.
474 */
475 vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags,
476 MSDOSFS_LARGEFS);
477
478 /*
479 * Compute several useful quantities from the bpb in the
480 * bootsector. Copy in the dos 5 variant of the bpb then fix up
481 * the fields that are different between dos 5 and dos 3.3.
482 */
483 SecPerClust = b50->bpbSecPerClust;
484 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
485 if (pmp->pm_BytesPerSec < DEV_BSIZE) {
486 error = EINVAL;
487 goto error_exit;
488 }
489 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
490 pmp->pm_FATs = b50->bpbFATs;
491 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
492 pmp->pm_Sectors = getushort(b50->bpbSectors);
493 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
494 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
495 pmp->pm_Heads = getushort(b50->bpbHeads);
496 pmp->pm_Media = b50->bpbMedia;
497
498 /* calculate the ratio of sector size to DEV_BSIZE */
499 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
500
501 /*
502 * We don't check pm_Heads nor pm_SecPerTrack, because
503 * these may not be set for EFI file systems. We don't
504 * use these anyway, so we're unaffected if they are
505 * invalid.
506 */
507 if (!pmp->pm_BytesPerSec || !SecPerClust) {
508 error = EINVAL;
509 goto error_exit;
510 }
511
512 if (pmp->pm_Sectors == 0) {
513 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
514 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
515 } else {
516 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
517 pmp->pm_HugeSectors = pmp->pm_Sectors;
518 }
519 if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) {
520 if (pmp->pm_HugeSectors > 0xffffffff /
521 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
522 /*
523 * We cannot deal currently with this size of disk
524 * due to fileid limitations (see msdosfs_getattr and
525 * msdosfs_readdir)
526 */
527 error = EINVAL;
528 vfs_mount_error(mp,
529 "Disk too big, try '-o large' mount option");
530 printf("Disk too big, try '-o large' mount option\n");
531 goto error_exit;
532 }
533 }
534
535 if (pmp->pm_RootDirEnts == 0) {
536 if (pmp->pm_Sectors
537 || pmp->pm_FATsecs
538 || getushort(b710->bpbFSVers)) {
539 error = EINVAL;
540 printf("mountmsdosfs(): bad FAT32 filesystem\n");
541 goto error_exit;
542 }
543 pmp->pm_fatmask = FAT32_MASK;
544 pmp->pm_fatmult = 4;
545 pmp->pm_fatdiv = 1;
546 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
547 if (getushort(b710->bpbExtFlags) & FATMIRROR)
548 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
549 else
550 pmp->pm_flags |= MSDOSFS_FATMIRROR;
551 } else
552 pmp->pm_flags |= MSDOSFS_FATMIRROR;
553
554 /*
555 * Check a few values (could do some more):
556 * - logical sector size: power of 2, >= block size
557 * - sectors per cluster: power of 2, >= 1
558 * - number of sectors: >= 1, <= size of partition
559 * - number of FAT sectors: >= 1
560 */
561 if ( (SecPerClust == 0)
562 || (SecPerClust & (SecPerClust - 1))
563 || (pmp->pm_BytesPerSec < DEV_BSIZE)
564 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
565 || (pmp->pm_HugeSectors == 0)
566 || (pmp->pm_FATsecs == 0)
567 ) {
568 error = EINVAL;
569 goto error_exit;
570 }
571
572 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
573 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
574 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
575 SecPerClust *= pmp->pm_BlkPerSec;
576
577 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
578
579 if (FAT32(pmp)) {
580 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
581 pmp->pm_firstcluster = pmp->pm_fatblk
582 + (pmp->pm_FATs * pmp->pm_FATsecs);
583 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
584 } else {
585 pmp->pm_rootdirblk = pmp->pm_fatblk +
586 (pmp->pm_FATs * pmp->pm_FATsecs);
587 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
588 + DEV_BSIZE - 1)
589 / DEV_BSIZE; /* in blocks */
590 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
591 }
592
593 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
594 SecPerClust + 1;
595 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
596
597 if (pmp->pm_fatmask == 0) {
598 if (pmp->pm_maxcluster
599 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
600 /*
601 * This will usually be a floppy disk. This size makes
602 * sure that one fat entry will not be split across
603 * multiple blocks.
604 */
605 pmp->pm_fatmask = FAT12_MASK;
606 pmp->pm_fatmult = 3;
607 pmp->pm_fatdiv = 2;
608 } else {
609 pmp->pm_fatmask = FAT16_MASK;
610 pmp->pm_fatmult = 2;
611 pmp->pm_fatdiv = 1;
612 }
613 }
614
615 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
616 if (pmp->pm_maxcluster >= clusters) {
617 printf("Warning: number of clusters (%ld) exceeds FAT "
618 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
619 pmp->pm_maxcluster = clusters - 1;
620 }
621
622
623 if (FAT12(pmp))
624 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
625 else
626 pmp->pm_fatblocksize = MSDOSFS_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 if (pmp->pm_nxtfree == 0xffffffff)
670 pmp->pm_nxtfree = CLUST_FIRST;
671 } else
672 pmp->pm_fsinfo = 0;
673 brelse(bp);
674 bp = NULL;
675 }
676
677 /*
678 * Check and validate (or perhaps invalidate?) the fsinfo structure?
679 */
680 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
681 printf(
682 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
683 pmp->pm_nxtfree, pmp->pm_maxcluster);
684 error = EINVAL;
685 goto error_exit;
686 }
687
688 /*
689 * Allocate memory for the bitmap of allocated clusters, and then
690 * fill it in.
691 */
692 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
693 * sizeof(*pmp->pm_inusemap),
694 M_MSDOSFSFAT, M_WAITOK);
695
696 /*
697 * fillinusemap() needs pm_devvp.
698 */
699 pmp->pm_devvp = devvp;
700
701 /*
702 * Have the inuse map filled in.
703 */
704 if ((error = fillinusemap(pmp)) != 0)
705 goto error_exit;
706
707 /*
708 * If they want fat updates to be synchronous then let them suffer
709 * the performance degradation in exchange for the on disk copy of
710 * the fat being correct just about all the time. I suppose this
711 * would be a good thing to turn on if the kernel is still flakey.
712 */
713 if (mp->mnt_flag & MNT_SYNCHRONOUS)
714 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
715
716 /*
717 * Finish up.
718 */
719 if (ronly)
720 pmp->pm_flags |= MSDOSFSMNT_RONLY;
721 else {
722 /* Mark the volume dirty while it is mounted read/write. */
723 if ((error = markvoldirty(pmp, 1)) != 0)
724 goto error_exit;
725 pmp->pm_fmod = 1;
726 }
727 mp->mnt_data = (qaddr_t) pmp;
728 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
729 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
730 MNT_ILOCK(mp);
731 mp->mnt_flag |= MNT_LOCAL;
732 MNT_IUNLOCK(mp);
733
734 if (pmp->pm_flags & MSDOSFS_LARGEFS)
735 msdosfs_fileno_init(mp);
736
737 return 0;
738
739 error_exit:
740 if (bp)
741 brelse(bp);
742 if (cp != NULL) {
743 DROP_GIANT();
744 g_topology_lock();
745 g_vfs_close(cp, td);
746 g_topology_unlock();
747 PICKUP_GIANT();
748 }
749 if (pmp) {
750 if (pmp->pm_inusemap)
751 free(pmp->pm_inusemap, M_MSDOSFSFAT);
752 free(pmp, M_MSDOSFSMNT);
753 mp->mnt_data = (qaddr_t)0;
754 }
755 return (error);
756 }
757
758 /*
759 * Unmount the filesystem described by mp.
760 */
761 static int
762 msdosfs_unmount(mp, mntflags, td)
763 struct mount *mp;
764 int mntflags;
765 struct thread *td;
766 {
767 struct msdosfsmount *pmp;
768 int error, flags;
769
770 flags = 0;
771 if (mntflags & MNT_FORCE)
772 flags |= FORCECLOSE;
773 error = vflush(mp, 0, flags, td);
774 if (error)
775 return error;
776 pmp = VFSTOMSDOSFS(mp);
777 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
778 if (pmp->pm_w2u)
779 msdosfs_iconv->close(pmp->pm_w2u);
780 if (pmp->pm_u2w)
781 msdosfs_iconv->close(pmp->pm_u2w);
782 if (pmp->pm_d2u)
783 msdosfs_iconv->close(pmp->pm_d2u);
784 if (pmp->pm_u2d)
785 msdosfs_iconv->close(pmp->pm_u2d);
786 }
787
788 /* If the volume was mounted read/write, mark it clean now. */
789 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
790 error = markvoldirty(pmp, 0);
791 if (error && (flags & FORCECLOSE) == 0)
792 return (error);
793 }
794 #ifdef MSDOSFS_DEBUG
795 {
796 struct vnode *vp = pmp->pm_devvp;
797
798 VI_LOCK(vp);
799 vn_printf(vp,
800 "msdosfs_umount(): just before calling VOP_CLOSE()\n");
801 printf("freef %p, freeb %p, mount %p\n",
802 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
803 vp->v_mount);
804 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
805 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
806 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
807 vp->v_bufobj.bo_numoutput, vp->v_type);
808 VI_UNLOCK(vp);
809 }
810 #endif
811 DROP_GIANT();
812 g_topology_lock();
813 g_vfs_close(pmp->pm_cp, td);
814 g_topology_unlock();
815 PICKUP_GIANT();
816 vrele(pmp->pm_devvp);
817 free(pmp->pm_inusemap, M_MSDOSFSFAT);
818 if (pmp->pm_flags & MSDOSFS_LARGEFS) {
819 msdosfs_fileno_free(mp);
820 }
821 free(pmp, M_MSDOSFSMNT);
822 mp->mnt_data = (qaddr_t)0;
823 MNT_ILOCK(mp);
824 mp->mnt_flag &= ~MNT_LOCAL;
825 MNT_IUNLOCK(mp);
826 return (error);
827 }
828
829 static int
830 msdosfs_root(mp, flags, vpp, td)
831 struct mount *mp;
832 int flags;
833 struct vnode **vpp;
834 struct thread *td;
835 {
836 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
837 struct denode *ndep;
838 int error;
839
840 #ifdef MSDOSFS_DEBUG
841 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
842 #endif
843 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
844 if (error)
845 return (error);
846 *vpp = DETOV(ndep);
847 return (0);
848 }
849
850 static int
851 msdosfs_statfs(mp, sbp, td)
852 struct mount *mp;
853 struct statfs *sbp;
854 struct thread *td;
855 {
856 struct msdosfsmount *pmp;
857
858 pmp = VFSTOMSDOSFS(mp);
859 sbp->f_bsize = pmp->pm_bpcluster;
860 sbp->f_iosize = pmp->pm_bpcluster;
861 sbp->f_blocks = pmp->pm_maxcluster + 1;
862 sbp->f_bfree = pmp->pm_freeclustercount;
863 sbp->f_bavail = pmp->pm_freeclustercount;
864 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
865 sbp->f_ffree = 0; /* what to put in here? */
866 return (0);
867 }
868
869 static int
870 msdosfs_sync(mp, waitfor, td)
871 struct mount *mp;
872 int waitfor;
873 struct thread *td;
874 {
875 struct vnode *vp, *nvp;
876 struct denode *dep;
877 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
878 int error, allerror = 0;
879
880 /*
881 * If we ever switch to not updating all of the fats all the time,
882 * this would be the place to update them from the first one.
883 */
884 if (pmp->pm_fmod != 0) {
885 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
886 panic("msdosfs_sync: rofs mod");
887 else {
888 /* update fats here */
889 }
890 }
891 /*
892 * Write back each (modified) denode.
893 */
894 MNT_ILOCK(mp);
895 loop:
896 MNT_VNODE_FOREACH(vp, mp, nvp) {
897 VI_LOCK(vp);
898 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
899 VI_UNLOCK(vp);
900 continue;
901 }
902 MNT_IUNLOCK(mp);
903 dep = VTODE(vp);
904 if ((dep->de_flag &
905 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
906 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
907 waitfor == MNT_LAZY)) {
908 VI_UNLOCK(vp);
909 MNT_ILOCK(mp);
910 continue;
911 }
912 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
913 if (error) {
914 MNT_ILOCK(mp);
915 if (error == ENOENT)
916 goto loop;
917 continue;
918 }
919 error = VOP_FSYNC(vp, waitfor, td);
920 if (error)
921 allerror = error;
922 VOP_UNLOCK(vp, 0, td);
923 vrele(vp);
924 MNT_ILOCK(mp);
925 }
926 MNT_IUNLOCK(mp);
927
928 /*
929 * Flush filesystem control info.
930 */
931 if (waitfor != MNT_LAZY) {
932 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
933 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
934 if (error)
935 allerror = error;
936 VOP_UNLOCK(pmp->pm_devvp, 0, td);
937 }
938 return (allerror);
939 }
940
941 static int
942 msdosfs_fhtovp(mp, fhp, vpp)
943 struct mount *mp;
944 struct fid *fhp;
945 struct vnode **vpp;
946 {
947 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
948 struct defid *defhp = (struct defid *) fhp;
949 struct denode *dep;
950 int error;
951
952 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
953 if (error) {
954 *vpp = NULLVP;
955 return (error);
956 }
957 *vpp = DETOV(dep);
958 vnode_create_vobject_off(*vpp, dep->de_FileSize, curthread);
959 return (0);
960 }
961
962 static int
963 msdosfs_vptofh(vp, fhp)
964 struct vnode *vp;
965 struct fid *fhp;
966 {
967 struct denode *dep;
968 struct defid *defhp;
969
970 dep = VTODE(vp);
971 defhp = (struct defid *)fhp;
972 defhp->defid_len = sizeof(struct defid);
973 defhp->defid_dirclust = dep->de_dirclust;
974 defhp->defid_dirofs = dep->de_diroffset;
975 /* defhp->defid_gen = dep->de_gen; */
976 return (0);
977 }
978
979 static struct vfsops msdosfs_vfsops = {
980 .vfs_fhtovp = msdosfs_fhtovp,
981 .vfs_mount = msdosfs_mount,
982 .vfs_cmount = msdosfs_cmount,
983 .vfs_root = msdosfs_root,
984 .vfs_statfs = msdosfs_statfs,
985 .vfs_sync = msdosfs_sync,
986 .vfs_unmount = msdosfs_unmount,
987 .vfs_vptofh = msdosfs_vptofh,
988 };
989
990 VFS_SET(msdosfs_vfsops, msdosfs, 0);
991 MODULE_VERSION(msdosfs, 1);
Cache object: 2fbd4f8f1e568417c23ea37ffdf19813
|