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