1c3aac50fSPeter Wemm /* $FreeBSD$ */ 2952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */ 327a0bc89SDoug Rabson 427a0bc89SDoug Rabson /*- 5952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 727a0bc89SDoug Rabson * All rights reserved. 827a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 927a0bc89SDoug Rabson * 1027a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1127a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1227a0bc89SDoug Rabson * are met: 1327a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1427a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1527a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1627a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1727a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 1827a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 1927a0bc89SDoug Rabson * must display the following acknowledgement: 2027a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2127a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2227a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2327a0bc89SDoug Rabson * 2427a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2527a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2627a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2727a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2827a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2927a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3027a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3127a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3227a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3327a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3427a0bc89SDoug Rabson */ 35d167cf6fSWarner Losh /*- 3627a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3727a0bc89SDoug Rabson * 3827a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 3927a0bc89SDoug Rabson * it, and don't remove this notice. 4027a0bc89SDoug Rabson * 4127a0bc89SDoug Rabson * This software is provided "as is". 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4427a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4527a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4627a0bc89SDoug Rabson * any damages caused by this software. 4727a0bc89SDoug Rabson * 4827a0bc89SDoug Rabson * October 1992 4927a0bc89SDoug Rabson */ 5027a0bc89SDoug Rabson 5127a0bc89SDoug Rabson #include <sys/param.h> 5227a0bc89SDoug Rabson #include <sys/systm.h> 536becd1c8SBruce Evans #include <sys/buf.h> 5465baf8f0SBruce Evans #include <sys/conf.h> 556becd1c8SBruce Evans #include <sys/iconv.h> 566becd1c8SBruce Evans #include <sys/kernel.h> 571103771dSBruce Evans #include <sys/lock.h> 586becd1c8SBruce Evans #include <sys/malloc.h> 596becd1c8SBruce Evans #include <sys/mount.h> 601103771dSBruce Evans #include <sys/mutex.h> 6127a0bc89SDoug Rabson #include <sys/namei.h> 62acd3428bSRobert Watson #include <sys/priv.h> 6327a0bc89SDoug Rabson #include <sys/proc.h> 646becd1c8SBruce Evans #include <sys/stat.h> 6527a0bc89SDoug Rabson #include <sys/vnode.h> 6627a0bc89SDoug Rabson 679a135592SPoul-Henning Kamp #include <geom/geom.h> 689a135592SPoul-Henning Kamp #include <geom/geom_vfs.h> 699a135592SPoul-Henning Kamp 706becd1c8SBruce Evans #include <fs/msdosfs/bootsect.h> 716becd1c8SBruce Evans #include <fs/msdosfs/bpb.h> 726becd1c8SBruce Evans #include <fs/msdosfs/direntry.h> 736becd1c8SBruce Evans #include <fs/msdosfs/denode.h> 746becd1c8SBruce Evans #include <fs/msdosfs/fat.h> 756becd1c8SBruce Evans #include <fs/msdosfs/msdosfsmount.h> 766becd1c8SBruce Evans 777c3fc9deSBruce Evans /* Mount options that we support. */ 786a4b48f4SPoul-Henning Kamp static const char *msdosfs_opts[] = { 79cb65c1eeSBruce Evans "async", "noatime", "noclusterr", "noclusterw", 807c3fc9deSBruce Evans "export", "force", "from", "sync", 817c3fc9deSBruce Evans "cs_dos", "cs_local", "cs_win", "dirmask", 827c3fc9deSBruce Evans "gid", "kiconv", "large", "longname", 837c3fc9deSBruce Evans "longnames", "mask", "shortname", "shortnames", 847c3fc9deSBruce Evans "uid", "win95", "nowin95", 856a4b48f4SPoul-Henning Kamp NULL 866a4b48f4SPoul-Henning Kamp }; 876a4b48f4SPoul-Henning Kamp 8801f6cfbaSYoshihiro Takahashi #if 1 /*def PC98*/ 8901f6cfbaSYoshihiro Takahashi /* 9001f6cfbaSYoshihiro Takahashi * XXX - The boot signature formatted by NEC PC-98 DOS looks like a 9101f6cfbaSYoshihiro Takahashi * garbage or a random value :-{ 9201f6cfbaSYoshihiro Takahashi * If you want to use that broken-signatured media, define the 9301f6cfbaSYoshihiro Takahashi * following symbol even though PC/AT. 9401f6cfbaSYoshihiro Takahashi * (ex. mount PC-98 DOS formatted FD on PC/AT) 9501f6cfbaSYoshihiro Takahashi */ 9601f6cfbaSYoshihiro Takahashi #define MSDOSFS_NOCHECKSIG 9701f6cfbaSYoshihiro Takahashi #endif 9801f6cfbaSYoshihiro Takahashi 995bb84bc8SRobert Watson MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure"); 1005bb84bc8SRobert Watson static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table"); 10155166637SPoul-Henning Kamp 1022d7c6b27SBruce Evans struct iconv_functions *msdosfs_iconv; 103c4f02a89SMax Khon 1046a4b48f4SPoul-Henning Kamp static int update_mp(struct mount *mp, struct thread *td); 10511caded3SAlfred Perlstein static int mountmsdosfs(struct vnode *devvp, struct mount *mp, 1066a4b48f4SPoul-Henning Kamp struct thread *td); 1071be5bc74STom Rhodes static vfs_fhtovp_t msdosfs_fhtovp; 1086a4b48f4SPoul-Henning Kamp static vfs_mount_t msdosfs_mount; 1099bf1a756SPoul-Henning Kamp static vfs_root_t msdosfs_root; 1109bf1a756SPoul-Henning Kamp static vfs_statfs_t msdosfs_statfs; 1119bf1a756SPoul-Henning Kamp static vfs_sync_t msdosfs_sync; 1129bf1a756SPoul-Henning Kamp static vfs_unmount_t msdosfs_unmount; 113af482601SBruce Evans 1141a9415afSTim J. Robbins /* Maximum length of a character set name (arbitrary). */ 1151a9415afSTim J. Robbins #define MAXCSLEN 64 1161a9415afSTim J. Robbins 117952a6212SJordan K. Hubbard static int 118dc9a617aSCraig Rodrigues update_mp(struct mount *mp, struct thread *td) 119952a6212SJordan K. Hubbard { 120952a6212SJordan K. Hubbard struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 1216a4b48f4SPoul-Henning Kamp void *dos, *win, *local; 1226a4b48f4SPoul-Henning Kamp int error, v; 123952a6212SJordan K. Hubbard 1246a4b48f4SPoul-Henning Kamp if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) { 1256a4b48f4SPoul-Henning Kamp if (msdosfs_iconv != NULL) { 1266a4b48f4SPoul-Henning Kamp error = vfs_getopt(mp->mnt_optnew, 1276a4b48f4SPoul-Henning Kamp "cs_win", &win, NULL); 1286a4b48f4SPoul-Henning Kamp if (!error) 1296a4b48f4SPoul-Henning Kamp error = vfs_getopt(mp->mnt_optnew, 1306a4b48f4SPoul-Henning Kamp "cs_local", &local, NULL); 1316a4b48f4SPoul-Henning Kamp if (!error) 1326a4b48f4SPoul-Henning Kamp error = vfs_getopt(mp->mnt_optnew, 1336a4b48f4SPoul-Henning Kamp "cs_dos", &dos, NULL); 1346a4b48f4SPoul-Henning Kamp if (!error) { 1351a9415afSTim J. Robbins msdosfs_iconv->open(win, local, &pmp->pm_u2w); 1361a9415afSTim J. Robbins msdosfs_iconv->open(local, win, &pmp->pm_w2u); 1371a9415afSTim J. Robbins msdosfs_iconv->open(dos, local, &pmp->pm_u2d); 1381a9415afSTim J. Robbins msdosfs_iconv->open(local, dos, &pmp->pm_d2u); 1396a4b48f4SPoul-Henning Kamp } 1401a9415afSTim J. Robbins if (error != 0) 1411a9415afSTim J. Robbins return (error); 142c4f02a89SMax Khon } else { 143c4f02a89SMax Khon pmp->pm_w2u = NULL; 144c4f02a89SMax Khon pmp->pm_u2w = NULL; 145c4f02a89SMax Khon pmp->pm_d2u = NULL; 146c4f02a89SMax Khon pmp->pm_u2d = NULL; 1477391f611SAndrey A. Chernov } 1481a9415afSTim J. Robbins } 1491a9415afSTim J. Robbins 1506a4b48f4SPoul-Henning Kamp if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) 1516a4b48f4SPoul-Henning Kamp pmp->pm_gid = v; 1526a4b48f4SPoul-Henning Kamp if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) 1536a4b48f4SPoul-Henning Kamp pmp->pm_uid = v; 1546a4b48f4SPoul-Henning Kamp if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v)) 1556a4b48f4SPoul-Henning Kamp pmp->pm_mask = v & ALLPERMS; 1566a4b48f4SPoul-Henning Kamp if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v)) 1576a4b48f4SPoul-Henning Kamp pmp->pm_dirmask = v & ALLPERMS; 1586a4b48f4SPoul-Henning Kamp vfs_flagopt(mp->mnt_optnew, "shortname", 1596a4b48f4SPoul-Henning Kamp &pmp->pm_flags, MSDOSFSMNT_SHORTNAME); 1604ab12573SCraig Rodrigues vfs_flagopt(mp->mnt_optnew, "shortnames", 1614ab12573SCraig Rodrigues &pmp->pm_flags, MSDOSFSMNT_SHORTNAME); 1626a4b48f4SPoul-Henning Kamp vfs_flagopt(mp->mnt_optnew, "longname", 1636a4b48f4SPoul-Henning Kamp &pmp->pm_flags, MSDOSFSMNT_LONGNAME); 1644ab12573SCraig Rodrigues vfs_flagopt(mp->mnt_optnew, "longnames", 1654ab12573SCraig Rodrigues &pmp->pm_flags, MSDOSFSMNT_LONGNAME); 1666a4b48f4SPoul-Henning Kamp vfs_flagopt(mp->mnt_optnew, "kiconv", 1676a4b48f4SPoul-Henning Kamp &pmp->pm_flags, MSDOSFSMNT_KICONV); 1686a4b48f4SPoul-Henning Kamp 169d75b2048SCraig Rodrigues if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0) 1706a4b48f4SPoul-Henning Kamp pmp->pm_flags |= MSDOSFSMNT_NOWIN95; 171d75b2048SCraig Rodrigues else 172d75b2048SCraig Rodrigues pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95; 173952a6212SJordan K. Hubbard 174952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_NOWIN95) 175952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_SHORTNAME; 176952a6212SJordan K. Hubbard else if (!(pmp->pm_flags & 177952a6212SJordan K. Hubbard (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) { 178952a6212SJordan K. Hubbard struct vnode *rootvp; 179952a6212SJordan K. Hubbard 180952a6212SJordan K. Hubbard /* 181952a6212SJordan K. Hubbard * Try to divine whether to support Win'95 long filenames 182952a6212SJordan K. Hubbard */ 183952a6212SJordan K. Hubbard if (FAT32(pmp)) 184952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_LONGNAME; 185952a6212SJordan K. Hubbard else { 186d9b2d9f7SJeff Roberson if ((error = 187d9b2d9f7SJeff Roberson msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0) 188952a6212SJordan K. Hubbard return error; 1892d7c6b27SBruce Evans pmp->pm_flags |= findwin95(VTODE(rootvp)) ? 1902d7c6b27SBruce Evans MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME; 191952a6212SJordan K. Hubbard vput(rootvp); 192952a6212SJordan K. Hubbard } 193952a6212SJordan K. Hubbard } 194952a6212SJordan K. Hubbard return 0; 195952a6212SJordan K. Hubbard } 196952a6212SJordan K. Hubbard 1976a4b48f4SPoul-Henning Kamp static int 1986a4b48f4SPoul-Henning Kamp msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td) 1996a4b48f4SPoul-Henning Kamp { 2006a4b48f4SPoul-Henning Kamp struct msdosfs_args args; 2016a4b48f4SPoul-Henning Kamp int error; 2026a4b48f4SPoul-Henning Kamp 2036a4b48f4SPoul-Henning Kamp if (data == NULL) 2046a4b48f4SPoul-Henning Kamp return (EINVAL); 2056a4b48f4SPoul-Henning Kamp error = copyin(data, &args, sizeof args); 2066a4b48f4SPoul-Henning Kamp if (error) 2076a4b48f4SPoul-Henning Kamp return (error); 2086a4b48f4SPoul-Henning Kamp 2096a4b48f4SPoul-Henning Kamp ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN); 2106a4b48f4SPoul-Henning Kamp ma = mount_arg(ma, "export", &args.export, sizeof args.export); 2116a4b48f4SPoul-Henning Kamp ma = mount_argf(ma, "uid", "%d", args.uid); 2126a4b48f4SPoul-Henning Kamp ma = mount_argf(ma, "gid", "%d", args.gid); 2136a4b48f4SPoul-Henning Kamp ma = mount_argf(ma, "mask", "%d", args.mask); 2146a4b48f4SPoul-Henning Kamp ma = mount_argf(ma, "dirmask", "%d", args.dirmask); 2156a4b48f4SPoul-Henning Kamp 2166a4b48f4SPoul-Henning Kamp ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname"); 2176a4b48f4SPoul-Henning Kamp ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname"); 2186a4b48f4SPoul-Henning Kamp ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95"); 2196a4b48f4SPoul-Henning Kamp ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv"); 2206a4b48f4SPoul-Henning Kamp 2216a4b48f4SPoul-Henning Kamp ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN); 2226a4b48f4SPoul-Henning Kamp ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN); 2236a4b48f4SPoul-Henning Kamp ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN); 2246a4b48f4SPoul-Henning Kamp 2256a4b48f4SPoul-Henning Kamp error = kernel_mount(ma, flags); 2266a4b48f4SPoul-Henning Kamp 2276a4b48f4SPoul-Henning Kamp return (error); 2286a4b48f4SPoul-Henning Kamp } 2296a4b48f4SPoul-Henning Kamp 23027a0bc89SDoug Rabson /* 23127a0bc89SDoug Rabson * mp - path - addr in user space of mount point (ie /usr or whatever) 23227a0bc89SDoug Rabson * data - addr in user space of mount params including the name of the block 23327a0bc89SDoug Rabson * special file to treat as a filesystem. 23427a0bc89SDoug Rabson */ 2357fefffeeSPoul-Henning Kamp static int 2366a4b48f4SPoul-Henning Kamp msdosfs_mount(struct mount *mp, struct thread *td) 23727a0bc89SDoug Rabson { 23827a0bc89SDoug Rabson struct vnode *devvp; /* vnode for blk device to mount */ 239952a6212SJordan K. Hubbard /* msdosfs specific mount control block */ 240952a6212SJordan K. Hubbard struct msdosfsmount *pmp = NULL; 2415e8c582aSPoul-Henning Kamp struct nameidata ndp; 242269c902fSPoul-Henning Kamp int error, flags; 243952a6212SJordan K. Hubbard mode_t accessmode; 2446a4b48f4SPoul-Henning Kamp char *from; 24527a0bc89SDoug Rabson 2466a4b48f4SPoul-Henning Kamp if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts)) 2476a4b48f4SPoul-Henning Kamp return (EINVAL); 2486a4b48f4SPoul-Henning Kamp 24927a0bc89SDoug Rabson /* 250952a6212SJordan K. Hubbard * If updating, check whether changing from read-only to 251952a6212SJordan K. Hubbard * read/write; if there is no device name, that's all we do. 25227a0bc89SDoug Rabson */ 25327a0bc89SDoug Rabson if (mp->mnt_flag & MNT_UPDATE) { 25482c59ec6SCraig Rodrigues int ro_to_rw = 0; 255269c902fSPoul-Henning Kamp 2562d7c6b27SBruce Evans pmp = VFSTOMSDOSFS(mp); 2575eb304a9SCraig Rodrigues if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) { 258e9022ef8SCraig Rodrigues /* 259e9022ef8SCraig Rodrigues * Forbid export requests if filesystem has 260e9022ef8SCraig Rodrigues * MSDOSFS_LARGEFS flag set. 261e9022ef8SCraig Rodrigues */ 262e9022ef8SCraig Rodrigues if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) { 263e9022ef8SCraig Rodrigues vfs_mount_error(mp, 264e9022ef8SCraig Rodrigues "MSDOSFS_LARGEFS flag set, cannot export"); 265269c902fSPoul-Henning Kamp return (EOPNOTSUPP); 266e9022ef8SCraig Rodrigues } 267269c902fSPoul-Henning Kamp } 2689a135592SPoul-Henning Kamp if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && 2696a4b48f4SPoul-Henning Kamp vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 2708df6bac4SPoul-Henning Kamp error = VFS_SYNC(mp, MNT_WAIT, td); 271e83f1423SPoul-Henning Kamp if (error) 272e83f1423SPoul-Henning Kamp return (error); 27327a0bc89SDoug Rabson flags = WRITECLOSE; 27427a0bc89SDoug Rabson if (mp->mnt_flag & MNT_FORCE) 27527a0bc89SDoug Rabson flags |= FORCECLOSE; 276f257b7a5SAlfred Perlstein error = vflush(mp, 0, flags, td); 2776a4b48f4SPoul-Henning Kamp if (error) 2786a4b48f4SPoul-Henning Kamp return (error); 2799a135592SPoul-Henning Kamp DROP_GIANT(); 2809a135592SPoul-Henning Kamp g_topology_lock(); 2814eb3abf0SBruce Evans error = g_access(pmp->pm_cp, 0, -1, 0); 2829a135592SPoul-Henning Kamp g_topology_unlock(); 2839a135592SPoul-Henning Kamp PICKUP_GIANT(); 2844eb3abf0SBruce Evans if (error) 2854eb3abf0SBruce Evans return (error); 2864eb3abf0SBruce Evans 287bcc4260fSXin LI /* Now the volume is clean. Mark it. */ 288bcc4260fSXin LI error = markvoldirty(pmp, 0); 289bcc4260fSXin LI if (error && (flags & FORCECLOSE) == 0) 290bcc4260fSXin LI return (error); 2916a4b48f4SPoul-Henning Kamp } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && 2926a4b48f4SPoul-Henning Kamp !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) { 293952a6212SJordan K. Hubbard /* 294952a6212SJordan K. Hubbard * If upgrade to read-write by non-root, then verify 295952a6212SJordan K. Hubbard * that user has necessary permissions on the device. 296952a6212SJordan K. Hubbard */ 297952a6212SJordan K. Hubbard devvp = pmp->pm_devvp; 298b40ce416SJulian Elischer vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td); 299952a6212SJordan K. Hubbard error = VOP_ACCESS(devvp, VREAD | VWRITE, 300a854ed98SJohn Baldwin td->td_ucred, td); 301acd3428bSRobert Watson if (error) 302acd3428bSRobert Watson error = priv_check(td, PRIV_VFS_MOUNT_PERM); 303952a6212SJordan K. Hubbard if (error) { 304b40ce416SJulian Elischer VOP_UNLOCK(devvp, 0, td); 305952a6212SJordan K. Hubbard return (error); 306952a6212SJordan K. Hubbard } 307b40ce416SJulian Elischer VOP_UNLOCK(devvp, 0, td); 3089a135592SPoul-Henning Kamp DROP_GIANT(); 3099a135592SPoul-Henning Kamp g_topology_lock(); 3109a135592SPoul-Henning Kamp error = g_access(pmp->pm_cp, 0, 1, 0); 3119a135592SPoul-Henning Kamp g_topology_unlock(); 3129a135592SPoul-Henning Kamp PICKUP_GIANT(); 3139a135592SPoul-Henning Kamp if (error) 3149a135592SPoul-Henning Kamp return (error); 315cede1f56STom Rhodes 31682c59ec6SCraig Rodrigues ro_to_rw = 1; 317952a6212SJordan K. Hubbard } 3186a4b48f4SPoul-Henning Kamp vfs_flagopt(mp->mnt_optnew, "ro", 3196a4b48f4SPoul-Henning Kamp &pmp->pm_flags, MSDOSFSMNT_RONLY); 3206a4b48f4SPoul-Henning Kamp vfs_flagopt(mp->mnt_optnew, "ro", 3216a4b48f4SPoul-Henning Kamp &mp->mnt_flag, MNT_RDONLY); 32282c59ec6SCraig Rodrigues 32382c59ec6SCraig Rodrigues if (ro_to_rw) { 32482c59ec6SCraig Rodrigues /* Now that the volume is modifiable, mark it dirty. */ 32582c59ec6SCraig Rodrigues error = markvoldirty(pmp, 1); 32682c59ec6SCraig Rodrigues if (error) 32782c59ec6SCraig Rodrigues return (error); 32882c59ec6SCraig Rodrigues } 329952a6212SJordan K. Hubbard } 33027a0bc89SDoug Rabson /* 331952a6212SJordan K. Hubbard * Not an update, or updating the name: look up the name 332e9827c6dSBruce Evans * and verify that it refers to a sensible disk device. 33327a0bc89SDoug Rabson */ 3346a4b48f4SPoul-Henning Kamp if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL)) 3356a4b48f4SPoul-Henning Kamp return (EINVAL); 33675d7ba93SSuleiman Souhlal NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td); 3375e8c582aSPoul-Henning Kamp error = namei(&ndp); 338952a6212SJordan K. Hubbard if (error) 339952a6212SJordan K. Hubbard return (error); 3405e8c582aSPoul-Henning Kamp devvp = ndp.ni_vp; 3415e8c582aSPoul-Henning Kamp NDFREE(&ndp, NDF_ONLY_PNBUF); 342952a6212SJordan K. Hubbard 343ba4ad1fcSPoul-Henning Kamp if (!vn_isdisk(devvp, &error)) { 34475d7ba93SSuleiman Souhlal vput(devvp); 345ba4ad1fcSPoul-Henning Kamp return (error); 34627a0bc89SDoug Rabson } 34727a0bc89SDoug Rabson /* 348952a6212SJordan K. Hubbard * If mount by non-root, then verify that user has necessary 349952a6212SJordan K. Hubbard * permissions on the device. 35027a0bc89SDoug Rabson */ 351952a6212SJordan K. Hubbard accessmode = VREAD; 352952a6212SJordan K. Hubbard if ((mp->mnt_flag & MNT_RDONLY) == 0) 353952a6212SJordan K. Hubbard accessmode |= VWRITE; 354a854ed98SJohn Baldwin error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td); 355acd3428bSRobert Watson if (error) 356acd3428bSRobert Watson error = priv_check(td, PRIV_VFS_MOUNT_PERM); 357952a6212SJordan K. Hubbard if (error) { 358952a6212SJordan K. Hubbard vput(devvp); 359952a6212SJordan K. Hubbard return (error); 360952a6212SJordan K. Hubbard } 361952a6212SJordan K. Hubbard if ((mp->mnt_flag & MNT_UPDATE) == 0) { 3626a4b48f4SPoul-Henning Kamp error = mountmsdosfs(devvp, mp, td); 363952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG /* only needed for the printf below */ 364952a6212SJordan K. Hubbard pmp = VFSTOMSDOSFS(mp); 365952a6212SJordan K. Hubbard #endif 366952a6212SJordan K. Hubbard } else { 36727a0bc89SDoug Rabson if (devvp != pmp->pm_devvp) 368952a6212SJordan K. Hubbard error = EINVAL; /* XXX needs translation */ 36927a0bc89SDoug Rabson else 37075d7ba93SSuleiman Souhlal vput(devvp); 37127a0bc89SDoug Rabson } 37227a0bc89SDoug Rabson if (error) { 37327a0bc89SDoug Rabson vrele(devvp); 374952a6212SJordan K. Hubbard return (error); 375952a6212SJordan K. Hubbard } 376952a6212SJordan K. Hubbard 3776a4b48f4SPoul-Henning Kamp error = update_mp(mp, td); 378952a6212SJordan K. Hubbard if (error) { 3791a9415afSTim J. Robbins if ((mp->mnt_flag & MNT_UPDATE) == 0) 380b40ce416SJulian Elischer msdosfs_unmount(mp, MNT_FORCE, td); 38127a0bc89SDoug Rabson return error; 38227a0bc89SDoug Rabson } 3836a4b48f4SPoul-Henning Kamp 3846a4b48f4SPoul-Henning Kamp vfs_mountedfrom(mp, from); 38527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 3866a4b48f4SPoul-Henning Kamp printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap); 38727a0bc89SDoug Rabson #endif 388952a6212SJordan K. Hubbard return (0); 38927a0bc89SDoug Rabson } 39027a0bc89SDoug Rabson 3917fefffeeSPoul-Henning Kamp static int 392dc9a617aSCraig Rodrigues mountmsdosfs(struct vnode *devvp, struct mount *mp, struct thread *td) 39327a0bc89SDoug Rabson { 394952a6212SJordan K. Hubbard struct msdosfsmount *pmp; 395952a6212SJordan K. Hubbard struct buf *bp; 39689c9c53dSPoul-Henning Kamp struct cdev *dev = devvp->v_rdev; 39727a0bc89SDoug Rabson union bootsector *bsp; 39827a0bc89SDoug Rabson struct byte_bpb33 *b33; 39927a0bc89SDoug Rabson struct byte_bpb50 *b50; 400952a6212SJordan K. Hubbard struct byte_bpb710 *b710; 401952a6212SJordan K. Hubbard u_int8_t SecPerClust; 402499d3ffaSBoris Popov u_long clusters; 403952a6212SJordan K. Hubbard int ronly, error; 4049a135592SPoul-Henning Kamp struct g_consumer *cp; 4059a135592SPoul-Henning Kamp struct bufobj *bo; 40627a0bc89SDoug Rabson 4074eb3abf0SBruce Evans ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 4089a135592SPoul-Henning Kamp /* XXX: use VOP_ACCESS to check FS perms */ 4099a135592SPoul-Henning Kamp DROP_GIANT(); 4109a135592SPoul-Henning Kamp g_topology_lock(); 4114eb3abf0SBruce Evans error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1); 4129a135592SPoul-Henning Kamp g_topology_unlock(); 4139a135592SPoul-Henning Kamp PICKUP_GIANT(); 414b40ce416SJulian Elischer VOP_UNLOCK(devvp, 0, td); 415c3c6d51eSPoul-Henning Kamp if (error) 416952a6212SJordan K. Hubbard return (error); 417952a6212SJordan K. Hubbard 4189a135592SPoul-Henning Kamp bo = &devvp->v_bufobj; 4192d7c6b27SBruce Evans bp = NULL; /* This and pmp both used in error_exit. */ 420952a6212SJordan K. Hubbard pmp = NULL; 421952a6212SJordan K. Hubbard 42227a0bc89SDoug Rabson /* 423952a6212SJordan K. Hubbard * Read the boot sector of the filesystem, and then check the 424952a6212SJordan K. Hubbard * boot signature. If not a dos boot sector then error out. 42501f6cfbaSYoshihiro Takahashi * 42693fe42b6SBruce Evans * NOTE: 8192 is a magic size that works for ffs. 42727a0bc89SDoug Rabson */ 42893fe42b6SBruce Evans error = bread(devvp, 0, 8192, NOCRED, &bp); 429c3c6d51eSPoul-Henning Kamp if (error) 43027a0bc89SDoug Rabson goto error_exit; 431952a6212SJordan K. Hubbard bp->b_flags |= B_AGE; 432952a6212SJordan K. Hubbard bsp = (union bootsector *)bp->b_data; 43327a0bc89SDoug Rabson b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB; 43427a0bc89SDoug Rabson b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB; 43501a4d019SJohn Baldwin b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB; 436952a6212SJordan K. Hubbard 43701f6cfbaSYoshihiro Takahashi #ifndef MSDOSFS_NOCHECKSIG 438952a6212SJordan K. Hubbard if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 439952a6212SJordan K. Hubbard || bsp->bs50.bsBootSectSig1 != BOOTSIG1) { 44027a0bc89SDoug Rabson error = EINVAL; 44127a0bc89SDoug Rabson goto error_exit; 44227a0bc89SDoug Rabson } 44301f6cfbaSYoshihiro Takahashi #endif 44427a0bc89SDoug Rabson 445a163d034SWarner Losh pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO); 44627a0bc89SDoug Rabson pmp->pm_mountp = mp; 4479a135592SPoul-Henning Kamp pmp->pm_cp = cp; 4489a135592SPoul-Henning Kamp pmp->pm_bo = bo; 44927a0bc89SDoug Rabson 45027a0bc89SDoug Rabson /* 4514eb3abf0SBruce Evans * Initialize ownerships and permissions, since nothing else will 4524eb3abf0SBruce Evans * initialize them iff we are mounting root. 4534eb3abf0SBruce Evans */ 4544eb3abf0SBruce Evans pmp->pm_uid = UID_ROOT; 4554eb3abf0SBruce Evans pmp->pm_gid = GID_WHEEL; 4564eb3abf0SBruce Evans pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH | 4574eb3abf0SBruce Evans S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR; 4584eb3abf0SBruce Evans 4594eb3abf0SBruce Evans /* 460f458f2a5SCraig Rodrigues * Experimental support for large MS-DOS filesystems. 461f458f2a5SCraig Rodrigues * WARNING: This uses at least 32 bytes of kernel memory (which is not 462f458f2a5SCraig Rodrigues * reclaimed until the FS is unmounted) for each file on disk to map 463f458f2a5SCraig Rodrigues * between the 32-bit inode numbers used by VFS and the 64-bit 464f458f2a5SCraig Rodrigues * pseudo-inode numbers used internally by msdosfs. This is only 465f458f2a5SCraig Rodrigues * safe to use in certain controlled situations (e.g. read-only FS 466f458f2a5SCraig Rodrigues * with less than 1 million files). 467f458f2a5SCraig Rodrigues * Since the mappings do not persist across unmounts (or reboots), these 468f458f2a5SCraig Rodrigues * filesystems are not suitable for exporting through NFS, or any other 469f458f2a5SCraig Rodrigues * application that requires fixed inode numbers. 470f458f2a5SCraig Rodrigues */ 4712d7c6b27SBruce Evans vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS); 472f458f2a5SCraig Rodrigues 473f458f2a5SCraig Rodrigues /* 47427a0bc89SDoug Rabson * Compute several useful quantities from the bpb in the 47527a0bc89SDoug Rabson * bootsector. Copy in the dos 5 variant of the bpb then fix up 47627a0bc89SDoug Rabson * the fields that are different between dos 5 and dos 3.3. 47727a0bc89SDoug Rabson */ 478952a6212SJordan K. Hubbard SecPerClust = b50->bpbSecPerClust; 47927a0bc89SDoug Rabson pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec); 480696f22f0SJohn Baldwin if (pmp->pm_BytesPerSec < DEV_BSIZE) { 481696f22f0SJohn Baldwin error = EINVAL; 482696f22f0SJohn Baldwin goto error_exit; 483696f22f0SJohn Baldwin } 48427a0bc89SDoug Rabson pmp->pm_ResSectors = getushort(b50->bpbResSectors); 48527a0bc89SDoug Rabson pmp->pm_FATs = b50->bpbFATs; 48627a0bc89SDoug Rabson pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts); 48727a0bc89SDoug Rabson pmp->pm_Sectors = getushort(b50->bpbSectors); 48827a0bc89SDoug Rabson pmp->pm_FATsecs = getushort(b50->bpbFATsecs); 48927a0bc89SDoug Rabson pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack); 49027a0bc89SDoug Rabson pmp->pm_Heads = getushort(b50->bpbHeads); 491952a6212SJordan K. Hubbard pmp->pm_Media = b50->bpbMedia; 49227a0bc89SDoug Rabson 49301f6cfbaSYoshihiro Takahashi /* calculate the ratio of sector size to DEV_BSIZE */ 49401f6cfbaSYoshihiro Takahashi pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE; 49501f6cfbaSYoshihiro Takahashi 49627a0bc89SDoug Rabson /* XXX - We should probably check more values here */ 497952a6212SJordan K. Hubbard if (!pmp->pm_BytesPerSec || !SecPerClust 4980ff34b5eSRobert Watson || !pmp->pm_Heads 499ad63a118SSatoshi Asami #ifdef PC98 500952a6212SJordan K. Hubbard || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) { 501ad63a118SSatoshi Asami #else 502952a6212SJordan K. Hubbard || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) { 503ad63a118SSatoshi Asami #endif 50427a0bc89SDoug Rabson error = EINVAL; 50527a0bc89SDoug Rabson goto error_exit; 50627a0bc89SDoug Rabson } 50727a0bc89SDoug Rabson 50827a0bc89SDoug Rabson if (pmp->pm_Sectors == 0) { 50927a0bc89SDoug Rabson pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs); 51027a0bc89SDoug Rabson pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors); 51127a0bc89SDoug Rabson } else { 51227a0bc89SDoug Rabson pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs); 51327a0bc89SDoug Rabson pmp->pm_HugeSectors = pmp->pm_Sectors; 51427a0bc89SDoug Rabson } 515f458f2a5SCraig Rodrigues if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) { 516c681be37SDmitrij Tejblum if (pmp->pm_HugeSectors > 0xffffffff / 517c681be37SDmitrij Tejblum (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) { 518952a6212SJordan K. Hubbard /* 519952a6212SJordan K. Hubbard * We cannot deal currently with this size of disk 520952a6212SJordan K. Hubbard * due to fileid limitations (see msdosfs_getattr and 521952a6212SJordan K. Hubbard * msdosfs_readdir) 522952a6212SJordan K. Hubbard */ 523952a6212SJordan K. Hubbard error = EINVAL; 524f458f2a5SCraig Rodrigues vfs_mount_error(mp, 525f458f2a5SCraig Rodrigues "Disk too big, try '-o large' mount option"); 526952a6212SJordan K. Hubbard goto error_exit; 527952a6212SJordan K. Hubbard } 528f458f2a5SCraig Rodrigues } 529952a6212SJordan K. Hubbard 530952a6212SJordan K. Hubbard if (pmp->pm_RootDirEnts == 0) { 53120c5ba36SPeter Edwards if (pmp->pm_Sectors 532952a6212SJordan K. Hubbard || pmp->pm_FATsecs 533952a6212SJordan K. Hubbard || getushort(b710->bpbFSVers)) { 534952a6212SJordan K. Hubbard error = EINVAL; 535aaf0bb19SAndrey A. Chernov printf("mountmsdosfs(): bad FAT32 filesystem\n"); 536952a6212SJordan K. Hubbard goto error_exit; 537952a6212SJordan K. Hubbard } 538952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT32_MASK; 539952a6212SJordan K. Hubbard pmp->pm_fatmult = 4; 540952a6212SJordan K. Hubbard pmp->pm_fatdiv = 1; 541952a6212SJordan K. Hubbard pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs); 542952a6212SJordan K. Hubbard if (getushort(b710->bpbExtFlags) & FATMIRROR) 543952a6212SJordan K. Hubbard pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM; 544952a6212SJordan K. Hubbard else 545952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFS_FATMIRROR; 546952a6212SJordan K. Hubbard } else 547952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFS_FATMIRROR; 548952a6212SJordan K. Hubbard 549952a6212SJordan K. Hubbard /* 550952a6212SJordan K. Hubbard * Check a few values (could do some more): 551952a6212SJordan K. Hubbard * - logical sector size: power of 2, >= block size 552952a6212SJordan K. Hubbard * - sectors per cluster: power of 2, >= 1 553952a6212SJordan K. Hubbard * - number of sectors: >= 1, <= size of partition 554696f22f0SJohn Baldwin * - number of FAT sectors: >= 1 555952a6212SJordan K. Hubbard */ 556952a6212SJordan K. Hubbard if ( (SecPerClust == 0) 557952a6212SJordan K. Hubbard || (SecPerClust & (SecPerClust - 1)) 55801f6cfbaSYoshihiro Takahashi || (pmp->pm_BytesPerSec < DEV_BSIZE) 559952a6212SJordan K. Hubbard || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) 560952a6212SJordan K. Hubbard || (pmp->pm_HugeSectors == 0) 561696f22f0SJohn Baldwin || (pmp->pm_FATsecs == 0) 562952a6212SJordan K. Hubbard ) { 563952a6212SJordan K. Hubbard error = EINVAL; 564952a6212SJordan K. Hubbard goto error_exit; 565952a6212SJordan K. Hubbard } 56601f6cfbaSYoshihiro Takahashi 56701f6cfbaSYoshihiro Takahashi pmp->pm_HugeSectors *= pmp->pm_BlkPerSec; 56801f6cfbaSYoshihiro Takahashi pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */ 56901f6cfbaSYoshihiro Takahashi pmp->pm_FATsecs *= pmp->pm_BlkPerSec; 57001f6cfbaSYoshihiro Takahashi SecPerClust *= pmp->pm_BlkPerSec; 57101f6cfbaSYoshihiro Takahashi 57201f6cfbaSYoshihiro Takahashi pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec; 57301f6cfbaSYoshihiro Takahashi 574952a6212SJordan K. Hubbard if (FAT32(pmp)) { 575952a6212SJordan K. Hubbard pmp->pm_rootdirblk = getulong(b710->bpbRootClust); 576952a6212SJordan K. Hubbard pmp->pm_firstcluster = pmp->pm_fatblk 577952a6212SJordan K. Hubbard + (pmp->pm_FATs * pmp->pm_FATsecs); 57801f6cfbaSYoshihiro Takahashi pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec; 579952a6212SJordan K. Hubbard } else { 58027a0bc89SDoug Rabson pmp->pm_rootdirblk = pmp->pm_fatblk + 58127a0bc89SDoug Rabson (pmp->pm_FATs * pmp->pm_FATsecs); 582952a6212SJordan K. Hubbard pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry) 58301f6cfbaSYoshihiro Takahashi + DEV_BSIZE - 1) 58401f6cfbaSYoshihiro Takahashi / DEV_BSIZE; /* in blocks */ 58527a0bc89SDoug Rabson pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize; 586952a6212SJordan K. Hubbard } 587952a6212SJordan K. Hubbard 588499d3ffaSBoris Popov pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) / 589499d3ffaSBoris Popov SecPerClust + 1; 59001f6cfbaSYoshihiro Takahashi pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */ 591952a6212SJordan K. Hubbard 592952a6212SJordan K. Hubbard if (pmp->pm_fatmask == 0) { 593952a6212SJordan K. Hubbard if (pmp->pm_maxcluster 594952a6212SJordan K. Hubbard <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) { 59527a0bc89SDoug Rabson /* 596952a6212SJordan K. Hubbard * This will usually be a floppy disk. This size makes 597952a6212SJordan K. Hubbard * sure that one fat entry will not be split across 598952a6212SJordan K. Hubbard * multiple blocks. 59927a0bc89SDoug Rabson */ 600952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT12_MASK; 601952a6212SJordan K. Hubbard pmp->pm_fatmult = 3; 602952a6212SJordan K. Hubbard pmp->pm_fatdiv = 2; 603952a6212SJordan K. Hubbard } else { 604952a6212SJordan K. Hubbard pmp->pm_fatmask = FAT16_MASK; 605952a6212SJordan K. Hubbard pmp->pm_fatmult = 2; 606952a6212SJordan K. Hubbard pmp->pm_fatdiv = 1; 607952a6212SJordan K. Hubbard } 608952a6212SJordan K. Hubbard } 609499d3ffaSBoris Popov 610499d3ffaSBoris Popov clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv; 611499d3ffaSBoris Popov if (pmp->pm_maxcluster >= clusters) { 612499d3ffaSBoris Popov printf("Warning: number of clusters (%ld) exceeds FAT " 613499d3ffaSBoris Popov "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters); 614499d3ffaSBoris Popov pmp->pm_maxcluster = clusters - 1; 615499d3ffaSBoris Popov } 616499d3ffaSBoris Popov 617952a6212SJordan K. Hubbard if (FAT12(pmp)) 61893fe42b6SBruce Evans pmp->pm_fatblocksize = 3 * 512; 61927a0bc89SDoug Rabson else 62093fe42b6SBruce Evans pmp->pm_fatblocksize = PAGE_SIZE; 62193fe42b6SBruce Evans pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize, 62293fe42b6SBruce Evans pmp->pm_BytesPerSec); 62301f6cfbaSYoshihiro Takahashi pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE; 62401f6cfbaSYoshihiro Takahashi pmp->pm_bnshift = ffs(DEV_BSIZE) - 1; 62527a0bc89SDoug Rabson 62627a0bc89SDoug Rabson /* 62727a0bc89SDoug Rabson * Compute mask and shift value for isolating cluster relative byte 62827a0bc89SDoug Rabson * offsets and cluster numbers from a file offset. 62927a0bc89SDoug Rabson */ 63001f6cfbaSYoshihiro Takahashi pmp->pm_bpcluster = SecPerClust * DEV_BSIZE; 631952a6212SJordan K. Hubbard pmp->pm_crbomask = pmp->pm_bpcluster - 1; 632952a6212SJordan K. Hubbard pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1; 63327a0bc89SDoug Rabson 634952a6212SJordan K. Hubbard /* 635952a6212SJordan K. Hubbard * Check for valid cluster size 636952a6212SJordan K. Hubbard * must be a power of 2 637952a6212SJordan K. Hubbard */ 638952a6212SJordan K. Hubbard if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) { 639952a6212SJordan K. Hubbard error = EINVAL; 640952a6212SJordan K. Hubbard goto error_exit; 641ad63a118SSatoshi Asami } 64227a0bc89SDoug Rabson 64327a0bc89SDoug Rabson /* 64427a0bc89SDoug Rabson * Release the bootsector buffer. 64527a0bc89SDoug Rabson */ 646952a6212SJordan K. Hubbard brelse(bp); 647952a6212SJordan K. Hubbard bp = NULL; 648952a6212SJordan K. Hubbard 649952a6212SJordan K. Hubbard /* 6508d61a735SBruce Evans * Check the fsinfo sector if we have one. Silently fix up our 6518d61a735SBruce Evans * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff) 6528d61a735SBruce Evans * or too large. Ignore fp->fsinfree for now, since we need to 6538d61a735SBruce Evans * read the entire FAT anyway to fill the inuse map. 654952a6212SJordan K. Hubbard */ 655952a6212SJordan K. Hubbard if (pmp->pm_fsinfo) { 656952a6212SJordan K. Hubbard struct fsinfo *fp; 657952a6212SJordan K. Hubbard 65837269429SBruce Evans if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec, 65901f6cfbaSYoshihiro Takahashi NOCRED, &bp)) != 0) 660952a6212SJordan K. Hubbard goto error_exit; 661952a6212SJordan K. Hubbard fp = (struct fsinfo *)bp->b_data; 662952a6212SJordan K. Hubbard if (!bcmp(fp->fsisig1, "RRaA", 4) 663952a6212SJordan K. Hubbard && !bcmp(fp->fsisig2, "rrAa", 4) 664fd7c4230SBruce Evans && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) { 665952a6212SJordan K. Hubbard pmp->pm_nxtfree = getulong(fp->fsinxtfree); 6668d61a735SBruce Evans if (pmp->pm_nxtfree > pmp->pm_maxcluster) 6678bb386f2STim J. Robbins pmp->pm_nxtfree = CLUST_FIRST; 6688bb386f2STim J. Robbins } else 669952a6212SJordan K. Hubbard pmp->pm_fsinfo = 0; 670952a6212SJordan K. Hubbard brelse(bp); 671952a6212SJordan K. Hubbard bp = NULL; 672952a6212SJordan K. Hubbard } 673952a6212SJordan K. Hubbard 674952a6212SJordan K. Hubbard /* 6758d61a735SBruce Evans * Finish initializing pmp->pm_nxtfree (just in case the first few 6768d61a735SBruce Evans * sectors aren't properly reserved in the FAT). This completes 6778d61a735SBruce Evans * the fixup for fp->fsinxtfree, and fixes up the zero-initialized 6788d61a735SBruce Evans * value if there is no fsinfo. We will use pmp->pm_nxtfree 6798d61a735SBruce Evans * internally even if there is no fsinfo. 680952a6212SJordan K. Hubbard */ 6818d61a735SBruce Evans if (pmp->pm_nxtfree < CLUST_FIRST) 6828d61a735SBruce Evans pmp->pm_nxtfree = CLUST_FIRST; 68327a0bc89SDoug Rabson 68427a0bc89SDoug Rabson /* 68527a0bc89SDoug Rabson * Allocate memory for the bitmap of allocated clusters, and then 68627a0bc89SDoug Rabson * fill it in. 68727a0bc89SDoug Rabson */ 6880ef0dd6fSBruce Evans pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS) 68927a0bc89SDoug Rabson * sizeof(*pmp->pm_inusemap), 690a163d034SWarner Losh M_MSDOSFSFAT, M_WAITOK); 69127a0bc89SDoug Rabson 69227a0bc89SDoug Rabson /* 69327a0bc89SDoug Rabson * fillinusemap() needs pm_devvp. 69427a0bc89SDoug Rabson */ 69527a0bc89SDoug Rabson pmp->pm_devvp = devvp; 69627a0bc89SDoug Rabson 69727a0bc89SDoug Rabson /* 69827a0bc89SDoug Rabson * Have the inuse map filled in. 69927a0bc89SDoug Rabson */ 700952a6212SJordan K. Hubbard if ((error = fillinusemap(pmp)) != 0) 70127a0bc89SDoug Rabson goto error_exit; 70227a0bc89SDoug Rabson 70327a0bc89SDoug Rabson /* 70427a0bc89SDoug Rabson * If they want fat updates to be synchronous then let them suffer 70527a0bc89SDoug Rabson * the performance degradation in exchange for the on disk copy of 70627a0bc89SDoug Rabson * the fat being correct just about all the time. I suppose this 70727a0bc89SDoug Rabson * would be a good thing to turn on if the kernel is still flakey. 70827a0bc89SDoug Rabson */ 709952a6212SJordan K. Hubbard if (mp->mnt_flag & MNT_SYNCHRONOUS) 710952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_WAITONFAT; 71127a0bc89SDoug Rabson 71227a0bc89SDoug Rabson /* 71327a0bc89SDoug Rabson * Finish up. 71427a0bc89SDoug Rabson */ 715952a6212SJordan K. Hubbard if (ronly) 716952a6212SJordan K. Hubbard pmp->pm_flags |= MSDOSFSMNT_RONLY; 717cede1f56STom Rhodes else { 718a78c9287SBruce Evans /* Mark the volume dirty while it is mounted read/write. */ 719cede1f56STom Rhodes if ((error = markvoldirty(pmp, 1)) != 0) 720cede1f56STom Rhodes goto error_exit; 72127a0bc89SDoug Rabson pmp->pm_fmod = 1; 722cede1f56STom Rhodes } 72377465d93SAlfred Perlstein mp->mnt_data = pmp; 724939cb752SBruce Evans mp->mnt_stat.f_fsid.val[0] = dev2udev(dev); 725af3f60d5SBruce Evans mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum; 7265da56ddbSTor Egge MNT_ILOCK(mp); 727cc9d8990SPeter Wemm mp->mnt_flag |= MNT_LOCAL; 7285da56ddbSTor Egge MNT_IUNLOCK(mp); 72927a0bc89SDoug Rabson 730f458f2a5SCraig Rodrigues if (pmp->pm_flags & MSDOSFS_LARGEFS) 7313bc482ecSTim J. Robbins msdosfs_fileno_init(mp); 7323bc482ecSTim J. Robbins 73327a0bc89SDoug Rabson return 0; 73427a0bc89SDoug Rabson 735952a6212SJordan K. Hubbard error_exit: 736952a6212SJordan K. Hubbard if (bp) 737952a6212SJordan K. Hubbard brelse(bp); 7389a135592SPoul-Henning Kamp if (cp != NULL) { 7399a135592SPoul-Henning Kamp DROP_GIANT(); 7409a135592SPoul-Henning Kamp g_topology_lock(); 74184a69752SPoul-Henning Kamp g_vfs_close(cp, td); 7429a135592SPoul-Henning Kamp g_topology_unlock(); 7439a135592SPoul-Henning Kamp PICKUP_GIANT(); 7449a135592SPoul-Henning Kamp } 74527a0bc89SDoug Rabson if (pmp) { 74627a0bc89SDoug Rabson if (pmp->pm_inusemap) 747952a6212SJordan K. Hubbard free(pmp->pm_inusemap, M_MSDOSFSFAT); 748952a6212SJordan K. Hubbard free(pmp, M_MSDOSFSMNT); 74977465d93SAlfred Perlstein mp->mnt_data = NULL; 75027a0bc89SDoug Rabson } 751952a6212SJordan K. Hubbard return (error); 75227a0bc89SDoug Rabson } 75327a0bc89SDoug Rabson 75427a0bc89SDoug Rabson /* 75527a0bc89SDoug Rabson * Unmount the filesystem described by mp. 75627a0bc89SDoug Rabson */ 7577fefffeeSPoul-Henning Kamp static int 758dc9a617aSCraig Rodrigues msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td) 75927a0bc89SDoug Rabson { 760952a6212SJordan K. Hubbard struct msdosfsmount *pmp; 761952a6212SJordan K. Hubbard int error, flags; 76227a0bc89SDoug Rabson 763952a6212SJordan K. Hubbard flags = 0; 764952a6212SJordan K. Hubbard if (mntflags & MNT_FORCE) 76527a0bc89SDoug Rabson flags |= FORCECLOSE; 766f257b7a5SAlfred Perlstein error = vflush(mp, 0, flags, td); 767c3c6d51eSPoul-Henning Kamp if (error) 76827a0bc89SDoug Rabson return error; 769952a6212SJordan K. Hubbard pmp = VFSTOMSDOSFS(mp); 770c4f02a89SMax Khon if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) { 771c4f02a89SMax Khon if (pmp->pm_w2u) 772c4f02a89SMax Khon msdosfs_iconv->close(pmp->pm_w2u); 773c4f02a89SMax Khon if (pmp->pm_u2w) 774c4f02a89SMax Khon msdosfs_iconv->close(pmp->pm_u2w); 775c4f02a89SMax Khon if (pmp->pm_d2u) 776c4f02a89SMax Khon msdosfs_iconv->close(pmp->pm_d2u); 777c4f02a89SMax Khon if (pmp->pm_u2d) 778c4f02a89SMax Khon msdosfs_iconv->close(pmp->pm_u2d); 779c4f02a89SMax Khon } 780cede1f56STom Rhodes 781a78c9287SBruce Evans /* If the volume was mounted read/write, mark it clean now. */ 782cede1f56STom Rhodes if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) { 783cede1f56STom Rhodes error = markvoldirty(pmp, 0); 784a78c9287SBruce Evans if (error && (flags & FORCECLOSE) == 0) 785cede1f56STom Rhodes return (error); 786cede1f56STom Rhodes } 787952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 788952a6212SJordan K. Hubbard { 789952a6212SJordan K. Hubbard struct vnode *vp = pmp->pm_devvp; 790952a6212SJordan K. Hubbard 7914d93c0beSJeff Roberson VI_LOCK(vp); 792f69d42a1SPoul-Henning Kamp vn_printf(vp, 793f69d42a1SPoul-Henning Kamp "msdosfs_umount(): just before calling VOP_CLOSE()\n"); 794952a6212SJordan K. Hubbard printf("freef %p, freeb %p, mount %p\n", 795fc2ffbe6SPoul-Henning Kamp TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev, 796952a6212SJordan K. Hubbard vp->v_mount); 797952a6212SJordan K. Hubbard printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n", 798156cb265SPoul-Henning Kamp TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd), 799156cb265SPoul-Henning Kamp TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd), 800156cb265SPoul-Henning Kamp vp->v_bufobj.bo_numoutput, vp->v_type); 8014d93c0beSJeff Roberson VI_UNLOCK(vp); 802952a6212SJordan K. Hubbard } 803952a6212SJordan K. Hubbard #endif 8049a135592SPoul-Henning Kamp DROP_GIANT(); 8059a135592SPoul-Henning Kamp g_topology_lock(); 80684a69752SPoul-Henning Kamp g_vfs_close(pmp->pm_cp, td); 8079a135592SPoul-Henning Kamp g_topology_unlock(); 8089a135592SPoul-Henning Kamp PICKUP_GIANT(); 80927a0bc89SDoug Rabson vrele(pmp->pm_devvp); 810952a6212SJordan K. Hubbard free(pmp->pm_inusemap, M_MSDOSFSFAT); 811f458f2a5SCraig Rodrigues if (pmp->pm_flags & MSDOSFS_LARGEFS) { 8123bc482ecSTim J. Robbins msdosfs_fileno_free(mp); 813f458f2a5SCraig Rodrigues } 814952a6212SJordan K. Hubbard free(pmp, M_MSDOSFSMNT); 81577465d93SAlfred Perlstein mp->mnt_data = NULL; 8165da56ddbSTor Egge MNT_ILOCK(mp); 817cc9d8990SPeter Wemm mp->mnt_flag &= ~MNT_LOCAL; 8185da56ddbSTor Egge MNT_IUNLOCK(mp); 819952a6212SJordan K. Hubbard return (error); 82027a0bc89SDoug Rabson } 82127a0bc89SDoug Rabson 8227fefffeeSPoul-Henning Kamp static int 823dc9a617aSCraig Rodrigues msdosfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 82427a0bc89SDoug Rabson { 825952a6212SJordan K. Hubbard struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 82627a0bc89SDoug Rabson struct denode *ndep; 82727a0bc89SDoug Rabson int error; 82827a0bc89SDoug Rabson 82927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 830952a6212SJordan K. Hubbard printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp); 83127a0bc89SDoug Rabson #endif 832952a6212SJordan K. Hubbard error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep); 833952a6212SJordan K. Hubbard if (error) 834952a6212SJordan K. Hubbard return (error); 83527a0bc89SDoug Rabson *vpp = DETOV(ndep); 836952a6212SJordan K. Hubbard return (0); 83727a0bc89SDoug Rabson } 83827a0bc89SDoug Rabson 8397fefffeeSPoul-Henning Kamp static int 840dc9a617aSCraig Rodrigues msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 84127a0bc89SDoug Rabson { 842952a6212SJordan K. Hubbard struct msdosfsmount *pmp; 84327a0bc89SDoug Rabson 844952a6212SJordan K. Hubbard pmp = VFSTOMSDOSFS(mp); 84527a0bc89SDoug Rabson sbp->f_bsize = pmp->pm_bpcluster; 84627a0bc89SDoug Rabson sbp->f_iosize = pmp->pm_bpcluster; 847499d3ffaSBoris Popov sbp->f_blocks = pmp->pm_maxcluster + 1; 84827a0bc89SDoug Rabson sbp->f_bfree = pmp->pm_freeclustercount; 84927a0bc89SDoug Rabson sbp->f_bavail = pmp->pm_freeclustercount; 85027a0bc89SDoug Rabson sbp->f_files = pmp->pm_RootDirEnts; /* XXX */ 85127a0bc89SDoug Rabson sbp->f_ffree = 0; /* what to put in here? */ 852952a6212SJordan K. Hubbard return (0); 85327a0bc89SDoug Rabson } 85427a0bc89SDoug Rabson 8557fefffeeSPoul-Henning Kamp static int 856dc9a617aSCraig Rodrigues msdosfs_sync(struct mount *mp, int waitfor, struct thread *td) 85727a0bc89SDoug Rabson { 858952a6212SJordan K. Hubbard struct vnode *vp, *nvp; 85927a0bc89SDoug Rabson struct denode *dep; 860952a6212SJordan K. Hubbard struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 861952a6212SJordan K. Hubbard int error, allerror = 0; 86227a0bc89SDoug Rabson 86327a0bc89SDoug Rabson /* 86427a0bc89SDoug Rabson * If we ever switch to not updating all of the fats all the time, 86527a0bc89SDoug Rabson * this would be the place to update them from the first one. 86627a0bc89SDoug Rabson */ 867dfd5dee1SPeter Wemm if (pmp->pm_fmod != 0) { 868952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_RONLY) 86927a0bc89SDoug Rabson panic("msdosfs_sync: rofs mod"); 87027a0bc89SDoug Rabson else { 87127a0bc89SDoug Rabson /* update fats here */ 87227a0bc89SDoug Rabson } 873dfd5dee1SPeter Wemm } 87427a0bc89SDoug Rabson /* 875952a6212SJordan K. Hubbard * Write back each (modified) denode. 87627a0bc89SDoug Rabson */ 877ca430f2eSAlexander Kabaev MNT_ILOCK(mp); 87827a0bc89SDoug Rabson loop: 879e3c5a7a4SPoul-Henning Kamp MNT_VNODE_FOREACH(vp, mp, nvp) { 8804d93c0beSJeff Roberson VI_LOCK(vp); 8818da00465SJeff Roberson if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) { 8824ab2c8bdSJeff Roberson VI_UNLOCK(vp); 8834ab2c8bdSJeff Roberson continue; 8844ab2c8bdSJeff Roberson } 885ca430f2eSAlexander Kabaev MNT_IUNLOCK(mp); 88627a0bc89SDoug Rabson dep = VTODE(vp); 887f00f5d71SPoul-Henning Kamp if ((dep->de_flag & 888c681be37SDmitrij Tejblum (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 && 889156cb265SPoul-Henning Kamp (vp->v_bufobj.bo_dirty.bv_cnt == 0 || 890f00f5d71SPoul-Henning Kamp waitfor == MNT_LAZY)) { 8914d93c0beSJeff Roberson VI_UNLOCK(vp); 892ca430f2eSAlexander Kabaev MNT_ILOCK(mp); 89327a0bc89SDoug Rabson continue; 894af3f60d5SBruce Evans } 895b40ce416SJulian Elischer error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td); 896af3f60d5SBruce Evans if (error) { 897ca430f2eSAlexander Kabaev MNT_ILOCK(mp); 898af3f60d5SBruce Evans if (error == ENOENT) 89927a0bc89SDoug Rabson goto loop; 900af3f60d5SBruce Evans continue; 901af3f60d5SBruce Evans } 9028df6bac4SPoul-Henning Kamp error = VOP_FSYNC(vp, waitfor, td); 903c3c6d51eSPoul-Henning Kamp if (error) 90427a0bc89SDoug Rabson allerror = error; 905cb9ddc80SAlexander Kabaev VOP_UNLOCK(vp, 0, td); 906cb9ddc80SAlexander Kabaev vrele(vp); 907ca430f2eSAlexander Kabaev MNT_ILOCK(mp); 90827a0bc89SDoug Rabson } 909ca430f2eSAlexander Kabaev MNT_IUNLOCK(mp); 91027a0bc89SDoug Rabson 91127a0bc89SDoug Rabson /* 91227a0bc89SDoug Rabson * Flush filesystem control info. 91327a0bc89SDoug Rabson */ 914c681be37SDmitrij Tejblum if (waitfor != MNT_LAZY) { 915b40ce416SJulian Elischer vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td); 9168df6bac4SPoul-Henning Kamp error = VOP_FSYNC(pmp->pm_devvp, waitfor, td); 917c3c6d51eSPoul-Henning Kamp if (error) 91827a0bc89SDoug Rabson allerror = error; 919b40ce416SJulian Elischer VOP_UNLOCK(pmp->pm_devvp, 0, td); 920c681be37SDmitrij Tejblum } 921952a6212SJordan K. Hubbard return (allerror); 92227a0bc89SDoug Rabson } 92327a0bc89SDoug Rabson 9241be5bc74STom Rhodes static int 9251be5bc74STom Rhodes msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 9261be5bc74STom Rhodes { 9271be5bc74STom Rhodes struct msdosfsmount *pmp = VFSTOMSDOSFS(mp); 9281be5bc74STom Rhodes struct defid *defhp = (struct defid *) fhp; 9291be5bc74STom Rhodes struct denode *dep; 9301be5bc74STom Rhodes int error; 9311be5bc74STom Rhodes 9321be5bc74STom Rhodes error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep); 9331be5bc74STom Rhodes if (error) { 9341be5bc74STom Rhodes *vpp = NULLVP; 9351be5bc74STom Rhodes return (error); 9361be5bc74STom Rhodes } 9371be5bc74STom Rhodes *vpp = DETOV(dep); 9381be5bc74STom Rhodes vnode_create_vobject(*vpp, dep->de_FileSize, curthread); 9391be5bc74STom Rhodes return (0); 9401be5bc74STom Rhodes } 9411be5bc74STom Rhodes 94230ffadf3SPoul-Henning Kamp static struct vfsops msdosfs_vfsops = { 9431be5bc74STom Rhodes .vfs_fhtovp = msdosfs_fhtovp, 9446a4b48f4SPoul-Henning Kamp .vfs_mount = msdosfs_mount, 9456a4b48f4SPoul-Henning Kamp .vfs_cmount = msdosfs_cmount, 9467652131bSPoul-Henning Kamp .vfs_root = msdosfs_root, 9477652131bSPoul-Henning Kamp .vfs_statfs = msdosfs_statfs, 9487652131bSPoul-Henning Kamp .vfs_sync = msdosfs_sync, 9497652131bSPoul-Henning Kamp .vfs_unmount = msdosfs_unmount, 95027a0bc89SDoug Rabson }; 951c901836cSGarrett Wollman 9524ccd7546SRuslan Ermilov VFS_SET(msdosfs_vfsops, msdosfs, 0); 953c4f02a89SMax Khon MODULE_VERSION(msdosfs, 1); 954