1/* $FreeBSD$ */
2/*	$NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg 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/bio.h>
54#include <sys/buf.h>
55#include <sys/clock.h>
56#include <sys/dirent.h>
57#include <sys/lock.h>
58#include <sys/lockf.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/stat.h>
65#include <sys/unistd.h>
66#include <sys/vnode.h>
67
68#include <vm/vm.h>
69#include <vm/vm_extern.h>
70
71#include <fs/msdosfs/bpb.h>
72#include <fs/msdosfs/direntry.h>
73#include <fs/msdosfs/denode.h>
74#include <fs/msdosfs/fat.h>
75#include <fs/msdosfs/msdosfsmount.h>
76
77#define	DOS_FILESIZE_MAX	0xffffffff
78
79/*
80 * Prototypes for MSDOSFS vnode operations
81 */
82static vop_create_t	msdosfs_create;
83static vop_mknod_t	msdosfs_mknod;
84static vop_open_t	msdosfs_open;
85static vop_close_t	msdosfs_close;
86static vop_access_t	msdosfs_access;
87static vop_getattr_t	msdosfs_getattr;
88static vop_setattr_t	msdosfs_setattr;
89static vop_read_t	msdosfs_read;
90static vop_write_t	msdosfs_write;
91static vop_fsync_t	msdosfs_fsync;
92static vop_remove_t	msdosfs_remove;
93static vop_link_t	msdosfs_link;
94static vop_rename_t	msdosfs_rename;
95static vop_mkdir_t	msdosfs_mkdir;
96static vop_rmdir_t	msdosfs_rmdir;
97static vop_symlink_t	msdosfs_symlink;
98static vop_readdir_t	msdosfs_readdir;
99static vop_bmap_t	msdosfs_bmap;
100static vop_strategy_t	msdosfs_strategy;
101static vop_print_t	msdosfs_print;
102static vop_pathconf_t	msdosfs_pathconf;
103static vop_vptofh_t	msdosfs_vptofh;
104
105/*
106 * Some general notes:
107 *
108 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
109 * read/written using the vnode for the filesystem. Blocks that represent
110 * the contents of a file are read/written using the vnode for the file
111 * (including directories when they are read/written as files). This
112 * presents problems for the dos filesystem because data that should be in
113 * an inode (if dos had them) resides in the directory itself.  Since we
114 * must update directory entries without the benefit of having the vnode
115 * for the directory we must use the vnode for the filesystem.  This means
116 * that when a directory is actually read/written (via read, write, or
117 * readdir, or seek) we must use the vnode for the filesystem instead of
118 * the vnode for the directory as would happen in ufs. This is to insure we
119 * retreive the correct block from the buffer cache since the hash value is
120 * based upon the vnode address and the desired block number.
121 */
122
123/*
124 * Create a regular file. On entry the directory to contain the file being
125 * created is locked.  We must release before we return. We must also free
126 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
127 * only if the SAVESTART bit in cn_flags is clear on success.
128 */
129static int
130msdosfs_create(ap)
131	struct vop_create_args /* {
132		struct vnode *a_dvp;
133		struct vnode **a_vpp;
134		struct componentname *a_cnp;
135		struct vattr *a_vap;
136	} */ *ap;
137{
138	struct componentname *cnp = ap->a_cnp;
139	struct denode ndirent;
140	struct denode *dep;
141	struct denode *pdep = VTODE(ap->a_dvp);
142	struct timespec ts;
143	int error;
144
145#ifdef MSDOSFS_DEBUG
146	printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
147#endif
148
149	/*
150	 * If this is the root directory and there is no space left we
151	 * can't do anything.  This is because the root directory can not
152	 * change size.
153	 */
154	if (pdep->de_StartCluster == MSDOSFSROOT
155	    && pdep->de_fndoffset >= pdep->de_FileSize) {
156		error = ENOSPC;
157		goto bad;
158	}
159
160	/*
161	 * Create a directory entry for the file, then call createde() to
162	 * have it installed. NOTE: DOS files are always executable.  We
163	 * use the absence of the owner write bit to make the file
164	 * readonly.
165	 */
166#ifdef DIAGNOSTIC
167	if ((cnp->cn_flags & HASBUF) == 0)
168		panic("msdosfs_create: no name");
169#endif
170	bzero(&ndirent, sizeof(ndirent));
171	error = uniqdosname(pdep, cnp, ndirent.de_Name);
172	if (error)
173		goto bad;
174
175	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
176				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
177	ndirent.de_LowerCase = 0;
178	ndirent.de_StartCluster = 0;
179	ndirent.de_FileSize = 0;
180	ndirent.de_pmp = pdep->de_pmp;
181	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
182	getnanotime(&ts);
183	DETIMES(&ndirent, &ts, &ts, &ts);
184	error = createde(&ndirent, pdep, &dep, cnp);
185	if (error)
186		goto bad;
187	*ap->a_vpp = DETOV(dep);
188	return (0);
189
190bad:
191	return (error);
192}
193
194static int
195msdosfs_mknod(ap)
196	struct vop_mknod_args /* {
197		struct vnode *a_dvp;
198		struct vnode **a_vpp;
199		struct componentname *a_cnp;
200		struct vattr *a_vap;
201	} */ *ap;
202{
203
204    return (EINVAL);
205}
206
207static int
208msdosfs_open(ap)
209	struct vop_open_args /* {
210		struct vnode *a_vp;
211		int a_mode;
212		struct ucred *a_cred;
213		struct thread *a_td;
214		struct file *a_fp;
215	} */ *ap;
216{
217	struct denode *dep = VTODE(ap->a_vp);
218	vnode_create_vobject(ap->a_vp, dep->de_FileSize, ap->a_td);
219	return 0;
220}
221
222static int
223msdosfs_close(ap)
224	struct vop_close_args /* {
225		struct vnode *a_vp;
226		int a_fflag;
227		struct ucred *a_cred;
228		struct thread *a_td;
229	} */ *ap;
230{
231	struct vnode *vp = ap->a_vp;
232	struct denode *dep = VTODE(vp);
233	struct timespec ts;
234
235	VI_LOCK(vp);
236	if (vp->v_usecount > 1) {
237		getnanotime(&ts);
238		DETIMES(dep, &ts, &ts, &ts);
239	}
240	VI_UNLOCK(vp);
241	return 0;
242}
243
244static int
245msdosfs_access(ap)
246	struct vop_access_args /* {
247		struct vnode *a_vp;
248		accmode_t a_accmode;
249		struct ucred *a_cred;
250		struct thread *a_td;
251	} */ *ap;
252{
253	struct vnode *vp = ap->a_vp;
254	struct denode *dep = VTODE(ap->a_vp);
255	struct msdosfsmount *pmp = dep->de_pmp;
256	mode_t file_mode;
257	accmode_t accmode = ap->a_accmode;
258
259	file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
260	    ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
261	file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
262
263	/*
264	 * Disallow writing to directories and regular files if the
265	 * filesystem is read-only.
266	 */
267	if (accmode & VWRITE) {
268		switch (vp->v_type) {
269		case VDIR:
270		case VREG:
271			if (vp->v_mount->mnt_flag & MNT_RDONLY)
272				return (EROFS);
273			break;
274		default:
275			break;
276		}
277	}
278
279	return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
280	    ap->a_accmode, ap->a_cred, NULL));
281}
282
283static int
284msdosfs_getattr(ap)
285	struct vop_getattr_args /* {
286		struct vnode *a_vp;
287		struct vattr *a_vap;
288		struct ucred *a_cred;
289	} */ *ap;
290{
291	struct denode *dep = VTODE(ap->a_vp);
292	struct msdosfsmount *pmp = dep->de_pmp;
293	struct vattr *vap = ap->a_vap;
294	mode_t mode;
295	struct timespec ts;
296	u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
297	uint64_t fileid;
298
299	getnanotime(&ts);
300	DETIMES(dep, &ts, &ts, &ts);
301	vap->va_fsid = dev2udev(pmp->pm_dev);
302	/*
303	 * The following computation of the fileid must be the same as that
304	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
305	 * doesn't work.
306	 */
307	if (dep->de_Attributes & ATTR_DIRECTORY) {
308		fileid = (uint64_t)cntobn(pmp, dep->de_StartCluster) *
309		    dirsperblk;
310		if (dep->de_StartCluster == MSDOSFSROOT)
311			fileid = 1;
312	} else {
313		fileid = (uint64_t)cntobn(pmp, dep->de_dirclust) *
314		    dirsperblk;
315		if (dep->de_dirclust == MSDOSFSROOT)
316			fileid = (uint64_t)roottobn(pmp, 0) * dirsperblk;
317		fileid += (uoff_t)dep->de_diroffset / sizeof(struct direntry);
318	}
319
320	if (pmp->pm_flags & MSDOSFS_LARGEFS)
321		vap->va_fileid = msdosfs_fileno_map(pmp->pm_mountp, fileid);
322	else
323		vap->va_fileid = (long)fileid;
324
325	if ((dep->de_Attributes & ATTR_READONLY) == 0)
326		mode = S_IRWXU|S_IRWXG|S_IRWXO;
327	else
328		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
329	vap->va_mode = mode &
330	    (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
331	vap->va_uid = pmp->pm_uid;
332	vap->va_gid = pmp->pm_gid;
333	vap->va_nlink = 1;
334	vap->va_rdev = NODEV;
335	vap->va_size = dep->de_FileSize;
336	fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
337	vap->va_ctime = vap->va_mtime;
338	if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
339		fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
340		fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
341		    0, &vap->va_birthtime);
342	} else {
343		vap->va_atime = vap->va_mtime;
344		vap->va_birthtime.tv_sec = -1;
345		vap->va_birthtime.tv_nsec = 0;
346	}
347	vap->va_flags = 0;
348	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
349		vap->va_flags |= SF_ARCHIVED;
350	vap->va_gen = 0;
351	vap->va_blocksize = pmp->pm_bpcluster;
352	vap->va_bytes =
353	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
354	vap->va_type = ap->a_vp->v_type;
355	vap->va_filerev = dep->de_modrev;
356	return (0);
357}
358
359static int
360msdosfs_setattr(ap)
361	struct vop_setattr_args /* {
362		struct vnode *a_vp;
363		struct vattr *a_vap;
364		struct ucred *a_cred;
365	} */ *ap;
366{
367	struct vnode *vp = ap->a_vp;
368	struct denode *dep = VTODE(ap->a_vp);
369	struct msdosfsmount *pmp = dep->de_pmp;
370	struct vattr *vap = ap->a_vap;
371	struct ucred *cred = ap->a_cred;
372	struct thread *td = curthread;
373	int error = 0;
374
375#ifdef MSDOSFS_DEBUG
376	printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
377	    ap->a_vp, vap, cred);
378#endif
379
380	/*
381	 * Check for unsettable attributes.
382	 */
383	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
384	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
385	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
386	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
387#ifdef MSDOSFS_DEBUG
388		printf("msdosfs_setattr(): returning EINVAL\n");
389		printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
390		    vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
391		printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
392		    vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
393		printf("    va_uid %x, va_gid %x\n",
394		    vap->va_uid, vap->va_gid);
395#endif
396		return (EINVAL);
397	}
398	if (vap->va_flags != VNOVAL) {
399		if (vp->v_mount->mnt_flag & MNT_RDONLY)
400			return (EROFS);
401		if (cred->cr_uid != pmp->pm_uid) {
402			error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
403			if (error)
404				return (error);
405		}
406		/*
407		 * We are very inconsistent about handling unsupported
408		 * attributes.  We ignored the access time and the
409		 * read and execute bits.  We were strict for the other
410		 * attributes.
411		 *
412		 * Here we are strict, stricter than ufs in not allowing
413		 * users to attempt to set SF_SETTABLE bits or anyone to
414		 * set unsupported bits.  However, we ignore attempts to
415		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
416		 * sensible filesystem attempts it a lot.
417		 */
418		if (vap->va_flags & SF_SETTABLE) {
419			error = priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0);
420			if (error)
421				return (error);
422		}
423		if (vap->va_flags & ~SF_ARCHIVED)
424			return EOPNOTSUPP;
425		if (vap->va_flags & SF_ARCHIVED)
426			dep->de_Attributes &= ~ATTR_ARCHIVE;
427		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
428			dep->de_Attributes |= ATTR_ARCHIVE;
429		dep->de_flag |= DE_MODIFIED;
430	}
431
432	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
433		uid_t uid;
434		gid_t gid;
435
436		if (vp->v_mount->mnt_flag & MNT_RDONLY)
437			return (EROFS);
438		uid = vap->va_uid;
439		if (uid == (uid_t)VNOVAL)
440			uid = pmp->pm_uid;
441		gid = vap->va_gid;
442		if (gid == (gid_t)VNOVAL)
443			gid = pmp->pm_gid;
444		if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
445		    (gid != pmp->pm_gid && !groupmember(gid, cred))) {
446			error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
447			if (error)
448				return (error);
449		}
450		if (uid != pmp->pm_uid || gid != pmp->pm_gid)
451			return EINVAL;
452	}
453
454	if (vap->va_size != VNOVAL) {
455		switch (vp->v_type) {
456		case VDIR:
457			return (EISDIR);
458		case VREG:
459			/*
460			 * Truncation is only supported for regular files,
461			 * Disallow it if the filesystem is read-only.
462			 */
463			if (vp->v_mount->mnt_flag & MNT_RDONLY)
464				return (EROFS);
465			break;
466		default:
467			/*
468			 * According to POSIX, the result is unspecified
469			 * for file types other than regular files,
470			 * directories and shared memory objects.  We
471			 * don't support any file types except regular
472			 * files and directories in this file system, so
473			 * this (default) case is unreachable and can do
474			 * anything.  Keep falling through to detrunc()
475			 * for now.
476			 */
477			break;
478		}
479		error = detrunc(dep, vap->va_size, 0, cred, td);
480		if (error)
481			return error;
482	}
483	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
484		if (vp->v_mount->mnt_flag & MNT_RDONLY)
485			return (EROFS);
486		if (vap->va_vaflags & VA_UTIMES_NULL) {
487			error = VOP_ACCESS(vp, VADMIN, cred, td);
488			if (error)
489				error = VOP_ACCESS(vp, VWRITE, cred, td);
490		} else
491			error = VOP_ACCESS(vp, VADMIN, cred, td);
492		if (vp->v_type != VDIR) {
493			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
494			    vap->va_atime.tv_sec != VNOVAL) {
495				dep->de_flag &= ~DE_ACCESS;
496				timespec2fattime(&vap->va_atime, 0,
497				    &dep->de_ADate, NULL, NULL);
498			}
499			if (vap->va_mtime.tv_sec != VNOVAL) {
500				dep->de_flag &= ~DE_UPDATE;
501				timespec2fattime(&vap->va_mtime, 0,
502				    &dep->de_MDate, &dep->de_MTime, NULL);
503			}
504			dep->de_Attributes |= ATTR_ARCHIVE;
505			dep->de_flag |= DE_MODIFIED;
506		}
507	}
508	/*
509	 * DOS files only have the ability to have their writability
510	 * attribute set, so we use the owner write bit to set the readonly
511	 * attribute.
512	 */
513	if (vap->va_mode != (mode_t)VNOVAL) {
514		if (vp->v_mount->mnt_flag & MNT_RDONLY)
515			return (EROFS);
516		if (cred->cr_uid != pmp->pm_uid) {
517			error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
518			if (error)
519				return (error);
520		}
521		if (vp->v_type != VDIR) {
522			/* We ignore the read and execute bits. */
523			if (vap->va_mode & VWRITE)
524				dep->de_Attributes &= ~ATTR_READONLY;
525			else
526				dep->de_Attributes |= ATTR_READONLY;
527			dep->de_Attributes |= ATTR_ARCHIVE;
528			dep->de_flag |= DE_MODIFIED;
529		}
530	}
531	return (deupdat(dep, 0));
532}
533
534static int
535msdosfs_read(ap)
536	struct vop_read_args /* {
537		struct vnode *a_vp;
538		struct uio *a_uio;
539		int a_ioflag;
540		struct ucred *a_cred;
541	} */ *ap;
542{
543	int error = 0;
544	int blsize;
545	int isadir;
546	ssize_t orig_resid;
547	u_int n;
548	u_long diff;
549	u_long on;
550	daddr_t lbn;
551	daddr_t rablock;
552	int rasize;
553	int seqcount;
554	struct buf *bp;
555	struct vnode *vp = ap->a_vp;
556	struct denode *dep = VTODE(vp);
557	struct msdosfsmount *pmp = dep->de_pmp;
558	struct uio *uio = ap->a_uio;
559
560	/*
561	 * If they didn't ask for any data, then we are done.
562	 */
563	orig_resid = uio->uio_resid;
564	if (orig_resid == 0)
565		return (0);
566
567	/*
568	 * The caller is supposed to ensure that
569	 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
570	 * We don't need to check for large offsets as in ffs because
571	 * dep->de_FileSize <= DOS_FILESIZE_MAX < OFF_MAX, so large
572	 * offsets cannot cause overflow even in theory.
573	 */
574
575	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
576
577	isadir = dep->de_Attributes & ATTR_DIRECTORY;
578	do {
579		if (uio->uio_offset >= dep->de_FileSize)
580			break;
581		lbn = de_cluster(pmp, uio->uio_offset);
582		rablock = lbn + 1;
583		blsize = pmp->pm_bpcluster;
584		on = uio->uio_offset & pmp->pm_crbomask;
585		/*
586		 * If we are operating on a directory file then be sure to
587		 * do i/o with the vnode for the filesystem instead of the
588		 * vnode for the directory.
589		 */
590		if (isadir) {
591			/* convert cluster # to block # */
592			error = pcbmap(dep, lbn, &lbn, 0, &blsize);
593			if (error == E2BIG) {
594				error = EINVAL;
595				break;
596			} else if (error)
597				break;
598			error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
599		} else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
600			error = bread(vp, lbn, blsize, NOCRED, &bp);
601		} else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
602			error = cluster_read(vp, dep->de_FileSize, lbn, blsize,
603			    NOCRED, on + uio->uio_resid, seqcount, &bp);
604		} else if (seqcount > 1) {
605			rasize = blsize;
606			error = breadn(vp, lbn,
607			    blsize, &rablock, &rasize, 1, NOCRED, &bp);
608		} else {
609			error = bread(vp, lbn, blsize, NOCRED, &bp);
610		}
611		if (error) {
612			brelse(bp);
613			break;
614		}
615		diff = pmp->pm_bpcluster - on;
616		n = diff > uio->uio_resid ? uio->uio_resid : diff;
617		diff = dep->de_FileSize - uio->uio_offset;
618		if (diff < n)
619			n = diff;
620		diff = blsize - bp->b_resid;
621		if (diff < n)
622			n = diff;
623		error = uiomove(bp->b_data + on, (int) n, uio);
624		brelse(bp);
625	} while (error == 0 && uio->uio_resid > 0 && n != 0);
626	if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
627	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
628		dep->de_flag |= DE_ACCESS;
629	return (error);
630}
631
632/*
633 * Write data to a file or directory.
634 */
635static int
636msdosfs_write(ap)
637	struct vop_write_args /* {
638		struct vnode *a_vp;
639		struct uio *a_uio;
640		int a_ioflag;
641		struct ucred *a_cred;
642	} */ *ap;
643{
644	int n;
645	int croffset;
646	ssize_t resid;
647	u_long osize;
648	int error = 0;
649	u_long count;
650	int seqcount;
651	daddr_t bn, lastcn;
652	struct buf *bp;
653	int ioflag = ap->a_ioflag;
654	struct uio *uio = ap->a_uio;
655	struct vnode *vp = ap->a_vp;
656	struct vnode *thisvp;
657	struct denode *dep = VTODE(vp);
658	struct msdosfsmount *pmp = dep->de_pmp;
659	struct ucred *cred = ap->a_cred;
660
661#ifdef MSDOSFS_DEBUG
662	printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
663	    vp, uio, ioflag, cred);
664	printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
665	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
666#endif
667
668	switch (vp->v_type) {
669	case VREG:
670		if (ioflag & IO_APPEND)
671			uio->uio_offset = dep->de_FileSize;
672		thisvp = vp;
673		break;
674	case VDIR:
675		return EISDIR;
676	default:
677		panic("msdosfs_write(): bad file type");
678	}
679
680	/*
681	 * This is needed (unlike in ffs_write()) because we extend the
682	 * file outside of the loop but we don't want to extend the file
683	 * for writes of 0 bytes.
684	 */
685	if (uio->uio_resid == 0)
686		return (0);
687
688	/*
689	 * The caller is supposed to ensure that
690	 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
691	 */
692	if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
693		return (EFBIG);
694
695	/*
696	 * If they've exceeded their filesize limit, tell them about it.
697	 */
698	if (vn_rlimit_fsize(vp, uio, uio->uio_td))
699		return (EFBIG);
700
701	/*
702	 * If the offset we are starting the write at is beyond the end of
703	 * the file, then they've done a seek.  Unix filesystems allow
704	 * files with holes in them, DOS doesn't so we must fill the hole
705	 * with zeroed blocks.
706	 */
707	if (uio->uio_offset > dep->de_FileSize) {
708		error = deextend(dep, uio->uio_offset, cred);
709		if (error)
710			return (error);
711	}
712
713	/*
714	 * Remember some values in case the write fails.
715	 */
716	resid = uio->uio_resid;
717	osize = dep->de_FileSize;
718
719	/*
720	 * If we write beyond the end of the file, extend it to its ultimate
721	 * size ahead of the time to hopefully get a contiguous area.
722	 */
723	if (uio->uio_offset + resid > osize) {
724		count = de_clcount(pmp, uio->uio_offset + resid) -
725			de_clcount(pmp, osize);
726		error = extendfile(dep, count, NULL, NULL, 0);
727		if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
728			goto errexit;
729		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
730	} else
731		lastcn = de_clcount(pmp, osize) - 1;
732
733	seqcount = ioflag >> IO_SEQSHIFT;
734	do {
735		if (de_cluster(pmp, uio->uio_offset) > lastcn) {
736			error = ENOSPC;
737			break;
738		}
739
740		croffset = uio->uio_offset & pmp->pm_crbomask;
741		n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
742		if (uio->uio_offset + n > dep->de_FileSize) {
743			dep->de_FileSize = uio->uio_offset + n;
744			/* The object size needs to be set before buffer is allocated */
745			vnode_pager_setsize(vp, dep->de_FileSize);
746		}
747
748		bn = de_cluster(pmp, uio->uio_offset);
749		if ((uio->uio_offset & pmp->pm_crbomask) == 0
750		    && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
751			> de_cluster(pmp, uio->uio_offset)
752			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
753			/*
754			 * If either the whole cluster gets written,
755			 * or we write the cluster from its start beyond EOF,
756			 * then no need to read data from disk.
757			 */
758			bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0, 0);
759			vfs_bio_clrbuf(bp);
760			/*
761			 * Do the bmap now, since pcbmap needs buffers
762			 * for the fat table. (see msdosfs_strategy)
763			 */
764			if (bp->b_blkno == bp->b_lblkno) {
765				error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
766				if (error)
767					bp->b_blkno = -1;
768				else
769					bp->b_blkno = bn;
770			}
771			if (bp->b_blkno == -1) {
772				brelse(bp);
773				if (!error)
774					error = EIO;		/* XXX */
775				break;
776			}
777		} else {
778			/*
779			 * The block we need to write into exists, so read it in.
780			 */
781			error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
782			if (error) {
783				brelse(bp);
784				break;
785			}
786		}
787
788		/*
789		 * Should these vnode_pager_* functions be done on dir
790		 * files?
791		 */
792
793		/*
794		 * Copy the data from user space into the buf header.
795		 */
796		error = uiomove(bp->b_data + croffset, n, uio);
797		if (error) {
798			brelse(bp);
799			break;
800		}
801
802		/* Prepare for clustered writes in some else clauses. */
803		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
804			bp->b_flags |= B_CLUSTEROK;
805
806		/*
807		 * If IO_SYNC, then each buffer is written synchronously.
808		 * Otherwise, if we have a severe page deficiency then
809		 * write the buffer asynchronously.  Otherwise, if on a
810		 * cluster boundary then write the buffer asynchronously,
811		 * combining it with contiguous clusters if permitted and
812		 * possible, since we don't expect more writes into this
813		 * buffer soon.  Otherwise, do a delayed write because we
814		 * expect more writes into this buffer soon.
815		 */
816		if (ioflag & IO_SYNC)
817			(void)bwrite(bp);
818		else if (vm_page_count_severe() || buf_dirty_count_severe())
819			bawrite(bp);
820		else if (n + croffset == pmp->pm_bpcluster) {
821			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
822				cluster_write(vp, bp, dep->de_FileSize,
823				    seqcount);
824			else
825				bawrite(bp);
826		} else
827			bdwrite(bp);
828		dep->de_flag |= DE_UPDATE;
829	} while (error == 0 && uio->uio_resid > 0);
830
831	/*
832	 * If the write failed and they want us to, truncate the file back
833	 * to the size it was before the write was attempted.
834	 */
835errexit:
836	if (error) {
837		if (ioflag & IO_UNIT) {
838			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
839			uio->uio_offset -= resid - uio->uio_resid;
840			uio->uio_resid = resid;
841		} else {
842			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
843			if (uio->uio_resid != resid)
844				error = 0;
845		}
846	} else if (ioflag & IO_SYNC)
847		error = deupdat(dep, 1);
848	return (error);
849}
850
851/*
852 * Flush the blocks of a file to disk.
853 */
854static int
855msdosfs_fsync(ap)
856	struct vop_fsync_args /* {
857		struct vnode *a_vp;
858		struct ucred *a_cred;
859		int a_waitfor;
860		struct thread *a_td;
861	} */ *ap;
862{
863	struct vnode *devvp;
864	int allerror, error;
865
866	vop_stdfsync(ap);
867
868	/*
869	* If the syncing request comes from fsync(2), sync the entire
870	* FAT and any other metadata that happens to be on devvp.  We
871	* need this mainly for the FAT.  We write the FAT sloppily, and
872	* syncing it all now is the best we can easily do to get all
873	* directory entries associated with the file (not just the file)
874	* fully synced.  The other metadata includes critical metadata
875	* for all directory entries, but only in the MNT_ASYNC case.  We
876	* will soon sync all metadata in the file's directory entry.
877	* Non-critical metadata for associated directory entries only
878	* gets synced accidentally, as in most file systems.
879	*/
880	if (ap->a_waitfor == MNT_WAIT) {
881		devvp = VTODE(ap->a_vp)->de_pmp->pm_devvp;
882		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
883		allerror = VOP_FSYNC(devvp, MNT_WAIT, ap->a_td);
884		VOP_UNLOCK(devvp, 0);
885	} else
886		allerror = 0;
887
888	error = deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT);
889	if (allerror == 0)
890		allerror = error;
891	return (allerror);
892}
893
894static int
895msdosfs_remove(ap)
896	struct vop_remove_args /* {
897		struct vnode *a_dvp;
898		struct vnode *a_vp;
899		struct componentname *a_cnp;
900	} */ *ap;
901{
902	struct denode *dep = VTODE(ap->a_vp);
903	struct denode *ddep = VTODE(ap->a_dvp);
904	int error;
905
906	if (ap->a_vp->v_type == VDIR)
907		error = EPERM;
908	else
909		error = removede(ddep, dep);
910#ifdef MSDOSFS_DEBUG
911	printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
912#endif
913	return (error);
914}
915
916/*
917 * DOS filesystems don't know what links are.
918 */
919static int
920msdosfs_link(ap)
921	struct vop_link_args /* {
922		struct vnode *a_tdvp;
923		struct vnode *a_vp;
924		struct componentname *a_cnp;
925	} */ *ap;
926{
927	return (EOPNOTSUPP);
928}
929
930/*
931 * Renames on files require moving the denode to a new hash queue since the
932 * denode's location is used to compute which hash queue to put the file
933 * in. Unless it is a rename in place.  For example "mv a b".
934 *
935 * What follows is the basic algorithm:
936 *
937 * if (file move) {
938 *	if (dest file exists) {
939 *		remove dest file
940 *	}
941 *	if (dest and src in same directory) {
942 *		rewrite name in existing directory slot
943 *	} else {
944 *		write new entry in dest directory
945 *		update offset and dirclust in denode
946 *		move denode to new hash chain
947 *		clear old directory entry
948 *	}
949 * } else {
950 *	directory move
951 *	if (dest directory exists) {
952 *		if (dest is not empty) {
953 *			return ENOTEMPTY
954 *		}
955 *		remove dest directory
956 *	}
957 *	if (dest and src in same directory) {
958 *		rewrite name in existing entry
959 *	} else {
960 *		be sure dest is not a child of src directory
961 *		write entry in dest directory
962 *		update "." and ".." in moved directory
963 *		clear old directory entry for moved directory
964 *	}
965 * }
966 *
967 * On entry:
968 *	source's parent directory is unlocked
969 *	source file or directory is unlocked
970 *	destination's parent directory is locked
971 *	destination file or directory is locked if it exists
972 *
973 * On exit:
974 *	all denodes should be released
975 */
976static int
977msdosfs_rename(ap)
978	struct vop_rename_args /* {
979		struct vnode *a_fdvp;
980		struct vnode *a_fvp;
981		struct componentname *a_fcnp;
982		struct vnode *a_tdvp;
983		struct vnode *a_tvp;
984		struct componentname *a_tcnp;
985	} */ *ap;
986{
987	struct vnode *tdvp = ap->a_tdvp;
988	struct vnode *fvp = ap->a_fvp;
989	struct vnode *fdvp = ap->a_fdvp;
990	struct vnode *tvp = ap->a_tvp;
991	struct componentname *tcnp = ap->a_tcnp;
992	struct componentname *fcnp = ap->a_fcnp;
993	struct denode *ip, *xp, *dp, *zp;
994	u_char toname[12], oldname[11];
995	u_long from_diroffset, to_diroffset;
996	u_char to_count;
997	int doingdirectory = 0, newparent = 0;
998	int error;
999	u_long cn, pcl;
1000	daddr_t bn;
1001	struct denode *fddep;	/* from file's parent directory	 */
1002	struct msdosfsmount *pmp;
1003	struct direntry *dotdotp;
1004	struct buf *bp;
1005
1006	fddep = VTODE(ap->a_fdvp);
1007	pmp = fddep->de_pmp;
1008
1009	pmp = VFSTOMSDOSFS(fdvp->v_mount);
1010
1011#ifdef DIAGNOSTIC
1012	if ((tcnp->cn_flags & HASBUF) == 0 ||
1013	    (fcnp->cn_flags & HASBUF) == 0)
1014		panic("msdosfs_rename: no name");
1015#endif
1016	/*
1017	 * Check for cross-device rename.
1018	 */
1019	if (fvp->v_mount != tdvp->v_mount ||
1020	    (tvp && fvp->v_mount != tvp->v_mount)) {
1021		error = EXDEV;
1022abortit:
1023		if (tdvp == tvp)
1024			vrele(tdvp);
1025		else
1026			vput(tdvp);
1027		if (tvp)
1028			vput(tvp);
1029		vrele(fdvp);
1030		vrele(fvp);
1031		return (error);
1032	}
1033
1034	/*
1035	 * If source and dest are the same, do nothing.
1036	 */
1037	if (tvp == fvp) {
1038		error = 0;
1039		goto abortit;
1040	}
1041
1042	error = vn_lock(fvp, LK_EXCLUSIVE);
1043	if (error)
1044		goto abortit;
1045	dp = VTODE(fdvp);
1046	ip = VTODE(fvp);
1047
1048	/*
1049	 * Be sure we are not renaming ".", "..", or an alias of ".". This
1050	 * leads to a crippled directory tree.  It's pretty tough to do a
1051	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1052	 * doesn't work if the ".." entry is missing.
1053	 */
1054	if (ip->de_Attributes & ATTR_DIRECTORY) {
1055		/*
1056		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1057		 */
1058		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1059		    dp == ip ||
1060		    (fcnp->cn_flags & ISDOTDOT) ||
1061		    (tcnp->cn_flags & ISDOTDOT) ||
1062		    (ip->de_flag & DE_RENAME)) {
1063			VOP_UNLOCK(fvp, 0);
1064			error = EINVAL;
1065			goto abortit;
1066		}
1067		ip->de_flag |= DE_RENAME;
1068		doingdirectory++;
1069	}
1070
1071	/*
1072	 * When the target exists, both the directory
1073	 * and target vnodes are returned locked.
1074	 */
1075	dp = VTODE(tdvp);
1076	xp = tvp ? VTODE(tvp) : NULL;
1077	/*
1078	 * Remember direntry place to use for destination
1079	 */
1080	to_diroffset = dp->de_fndoffset;
1081	to_count = dp->de_fndcnt;
1082
1083	/*
1084	 * If ".." must be changed (ie the directory gets a new
1085	 * parent) then the source directory must not be in the
1086	 * directory hierarchy above the target, as this would
1087	 * orphan everything below the source directory. Also
1088	 * the user must have write permission in the source so
1089	 * as to be able to change "..". We must repeat the call
1090	 * to namei, as the parent directory is unlocked by the
1091	 * call to doscheckpath().
1092	 */
1093	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1094	VOP_UNLOCK(fvp, 0);
1095	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1096		newparent = 1;
1097	if (doingdirectory && newparent) {
1098		if (error)	/* write access check above */
1099			goto bad;
1100		if (xp != NULL)
1101			vput(tvp);
1102		/*
1103		 * doscheckpath() vput()'s dp,
1104		 * so we have to do a relookup afterwards
1105		 */
1106		error = doscheckpath(ip, dp);
1107		if (error)
1108			goto out;
1109		if ((tcnp->cn_flags & SAVESTART) == 0)
1110			panic("msdosfs_rename: lost to startdir");
1111		error = relookup(tdvp, &tvp, tcnp);
1112		if (error)
1113			goto out;
1114		dp = VTODE(tdvp);
1115		xp = tvp ? VTODE(tvp) : NULL;
1116	}
1117
1118	if (xp != NULL) {
1119		/*
1120		 * Target must be empty if a directory and have no links
1121		 * to it. Also, ensure source and target are compatible
1122		 * (both directories, or both not directories).
1123		 */
1124		if (xp->de_Attributes & ATTR_DIRECTORY) {
1125			if (!dosdirempty(xp)) {
1126				error = ENOTEMPTY;
1127				goto bad;
1128			}
1129			if (!doingdirectory) {
1130				error = ENOTDIR;
1131				goto bad;
1132			}
1133			cache_purge(tdvp);
1134		} else if (doingdirectory) {
1135			error = EISDIR;
1136			goto bad;
1137		}
1138		error = removede(dp, xp);
1139		if (error)
1140			goto bad;
1141		vput(tvp);
1142		xp = NULL;
1143	}
1144
1145	/*
1146	 * Convert the filename in tcnp into a dos filename. We copy this
1147	 * into the denode and directory entry for the destination
1148	 * file/directory.
1149	 */
1150	error = uniqdosname(VTODE(tdvp), tcnp, toname);
1151	if (error)
1152		goto abortit;
1153
1154	/*
1155	 * Since from wasn't locked at various places above,
1156	 * have to do a relookup here.
1157	 */
1158	fcnp->cn_flags &= ~MODMASK;
1159	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1160	if ((fcnp->cn_flags & SAVESTART) == 0)
1161		panic("msdosfs_rename: lost from startdir");
1162	if (!newparent)
1163		VOP_UNLOCK(tdvp, 0);
1164	if (relookup(fdvp, &fvp, fcnp) == 0)
1165		vrele(fdvp);
1166	if (fvp == NULL) {
1167		/*
1168		 * From name has disappeared.
1169		 */
1170		if (doingdirectory)
1171			panic("rename: lost dir entry");
1172		if (newparent)
1173			VOP_UNLOCK(tdvp, 0);
1174		vrele(tdvp);
1175		vrele(ap->a_fvp);
1176		return 0;
1177	}
1178	xp = VTODE(fvp);
1179	zp = VTODE(fdvp);
1180	from_diroffset = zp->de_fndoffset;
1181
1182	/*
1183	 * Ensure that the directory entry still exists and has not
1184	 * changed till now. If the source is a file the entry may
1185	 * have been unlinked or renamed. In either case there is
1186	 * no further work to be done. If the source is a directory
1187	 * then it cannot have been rmdir'ed or renamed; this is
1188	 * prohibited by the DE_RENAME flag.
1189	 */
1190	if (xp != ip) {
1191		if (doingdirectory)
1192			panic("rename: lost dir entry");
1193		VOP_UNLOCK(fvp, 0);
1194		if (newparent)
1195			VOP_UNLOCK(fdvp, 0);
1196		vrele(ap->a_fvp);
1197		xp = NULL;
1198	} else {
1199		vrele(fvp);
1200		xp = NULL;
1201
1202		/*
1203		 * First write a new entry in the destination
1204		 * directory and mark the entry in the source directory
1205		 * as deleted.  Then move the denode to the correct hash
1206		 * chain for its new location in the filesystem.  And, if
1207		 * we moved a directory, then update its .. entry to point
1208		 * to the new parent directory.
1209		 */
1210		bcopy(ip->de_Name, oldname, 11);
1211		bcopy(toname, ip->de_Name, 11);	/* update denode */
1212		dp->de_fndoffset = to_diroffset;
1213		dp->de_fndcnt = to_count;
1214		error = createde(ip, dp, (struct denode **)0, tcnp);
1215		if (error) {
1216			bcopy(oldname, ip->de_Name, 11);
1217			if (newparent)
1218				VOP_UNLOCK(fdvp, 0);
1219			VOP_UNLOCK(fvp, 0);
1220			goto bad;
1221		}
1222		/*
1223		 * If ip is for a directory, then its name should always
1224		 * be "." since it is for the directory entry in the
1225		 * directory itself (msdosfs_lookup() always translates
1226		 * to the "." entry so as to get a unique denode, except
1227		 * for the root directory there are different
1228		 * complications).  However, we just corrupted its name
1229		 * to pass the correct name to createde().  Undo this.
1230		 */
1231		if ((ip->de_Attributes & ATTR_DIRECTORY) != 0)
1232			bcopy(oldname, ip->de_Name, 11);
1233		ip->de_refcnt++;
1234		zp->de_fndoffset = from_diroffset;
1235		error = removede(zp, ip);
1236		if (error) {
1237			/* XXX should downgrade to ro here, fs is corrupt */
1238			if (newparent)
1239				VOP_UNLOCK(fdvp, 0);
1240			VOP_UNLOCK(fvp, 0);
1241			goto bad;
1242		}
1243		if (!doingdirectory) {
1244			error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1245				       &ip->de_dirclust, 0);
1246			if (error) {
1247				/* XXX should downgrade to ro here, fs is corrupt */
1248				if (newparent)
1249					VOP_UNLOCK(fdvp, 0);
1250				VOP_UNLOCK(fvp, 0);
1251				goto bad;
1252			}
1253			if (ip->de_dirclust == MSDOSFSROOT)
1254				ip->de_diroffset = to_diroffset;
1255			else
1256				ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1257		}
1258		reinsert(ip);
1259		if (newparent)
1260			VOP_UNLOCK(fdvp, 0);
1261	}
1262
1263	/*
1264	 * If we moved a directory to a new parent directory, then we must
1265	 * fixup the ".." entry in the moved directory.
1266	 */
1267	if (doingdirectory && newparent) {
1268		cn = ip->de_StartCluster;
1269		if (cn == MSDOSFSROOT) {
1270			/* this should never happen */
1271			panic("msdosfs_rename(): updating .. in root directory?");
1272		} else
1273			bn = cntobn(pmp, cn);
1274		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1275			      NOCRED, &bp);
1276		if (error) {
1277			/* XXX should downgrade to ro here, fs is corrupt */
1278			brelse(bp);
1279			VOP_UNLOCK(fvp, 0);
1280			goto bad;
1281		}
1282		dotdotp = (struct direntry *)bp->b_data + 1;
1283		pcl = dp->de_StartCluster;
1284		if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1285			pcl = MSDOSFSROOT;
1286		putushort(dotdotp->deStartCluster, pcl);
1287		if (FAT32(pmp))
1288			putushort(dotdotp->deHighClust, pcl >> 16);
1289		if (DOINGASYNC(fvp))
1290			bdwrite(bp);
1291		else if ((error = bwrite(bp)) != 0) {
1292			/* XXX should downgrade to ro here, fs is corrupt */
1293			VOP_UNLOCK(fvp, 0);
1294			goto bad;
1295		}
1296	}
1297
1298	/*
1299	 * The msdosfs lookup is case insensitive. Several aliases may
1300	 * be inserted for a single directory entry. As a consequnce,
1301	 * name cache purge done by lookup for fvp when DELETE op for
1302	 * namei is specified, might be not enough to expunge all
1303	 * namecache entries that were installed for this direntry.
1304	 */
1305	cache_purge(fvp);
1306	VOP_UNLOCK(fvp, 0);
1307bad:
1308	if (xp)
1309		vput(tvp);
1310	vput(tdvp);
1311out:
1312	ip->de_flag &= ~DE_RENAME;
1313	vrele(fdvp);
1314	vrele(fvp);
1315	return (error);
1316
1317}
1318
1319static struct {
1320	struct direntry dot;
1321	struct direntry dotdot;
1322} dosdirtemplate = {
1323	{	".          ",				/* the . entry */
1324		ATTR_DIRECTORY,				/* file attribute */
1325		0,					/* reserved */
1326		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1327		{ 0, 0 },				/* access date */
1328		{ 0, 0 },				/* high bits of start cluster */
1329		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1330		{ 0, 0 },				/* startcluster */
1331		{ 0, 0, 0, 0 }				/* filesize */
1332	},
1333	{	"..         ",				/* the .. entry */
1334		ATTR_DIRECTORY,				/* file attribute */
1335		0,					/* reserved */
1336		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1337		{ 0, 0 },				/* access date */
1338		{ 0, 0 },				/* high bits of start cluster */
1339		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1340		{ 0, 0 },				/* startcluster */
1341		{ 0, 0, 0, 0 }				/* filesize */
1342	}
1343};
1344
1345static int
1346msdosfs_mkdir(ap)
1347	struct vop_mkdir_args /* {
1348		struct vnode *a_dvp;
1349		struct vnode **a_vpp;
1350		struvt componentname *a_cnp;
1351		struct vattr *a_vap;
1352	} */ *ap;
1353{
1354	struct componentname *cnp = ap->a_cnp;
1355	struct denode *dep;
1356	struct denode *pdep = VTODE(ap->a_dvp);
1357	struct direntry *denp;
1358	struct msdosfsmount *pmp = pdep->de_pmp;
1359	struct buf *bp;
1360	u_long newcluster, pcl;
1361	int bn;
1362	int error;
1363	struct denode ndirent;
1364	struct timespec ts;
1365
1366	/*
1367	 * If this is the root directory and there is no space left we
1368	 * can't do anything.  This is because the root directory can not
1369	 * change size.
1370	 */
1371	if (pdep->de_StartCluster == MSDOSFSROOT
1372	    && pdep->de_fndoffset >= pdep->de_FileSize) {
1373		error = ENOSPC;
1374		goto bad2;
1375	}
1376
1377	/*
1378	 * Allocate a cluster to hold the about to be created directory.
1379	 */
1380	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1381	if (error)
1382		goto bad2;
1383
1384	bzero(&ndirent, sizeof(ndirent));
1385	ndirent.de_pmp = pmp;
1386	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1387	getnanotime(&ts);
1388	DETIMES(&ndirent, &ts, &ts, &ts);
1389
1390	/*
1391	 * Now fill the cluster with the "." and ".." entries. And write
1392	 * the cluster to disk.  This way it is there for the parent
1393	 * directory to be pointing at if there were a crash.
1394	 */
1395	bn = cntobn(pmp, newcluster);
1396	/* always succeeds */
1397	bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
1398	bzero(bp->b_data, pmp->pm_bpcluster);
1399	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1400	denp = (struct direntry *)bp->b_data;
1401	putushort(denp[0].deStartCluster, newcluster);
1402	putushort(denp[0].deCDate, ndirent.de_CDate);
1403	putushort(denp[0].deCTime, ndirent.de_CTime);
1404	denp[0].deCHundredth = ndirent.de_CHun;
1405	putushort(denp[0].deADate, ndirent.de_ADate);
1406	putushort(denp[0].deMDate, ndirent.de_MDate);
1407	putushort(denp[0].deMTime, ndirent.de_MTime);
1408	pcl = pdep->de_StartCluster;
1409	/*
1410	 * Although the root directory has a non-magic starting cluster
1411	 * number for FAT32, chkdsk and fsck_msdosfs still require
1412	 * references to it in dotdot entries to be magic.
1413	 */
1414	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1415		pcl = MSDOSFSROOT;
1416	putushort(denp[1].deStartCluster, pcl);
1417	putushort(denp[1].deCDate, ndirent.de_CDate);
1418	putushort(denp[1].deCTime, ndirent.de_CTime);
1419	denp[1].deCHundredth = ndirent.de_CHun;
1420	putushort(denp[1].deADate, ndirent.de_ADate);
1421	putushort(denp[1].deMDate, ndirent.de_MDate);
1422	putushort(denp[1].deMTime, ndirent.de_MTime);
1423	if (FAT32(pmp)) {
1424		putushort(denp[0].deHighClust, newcluster >> 16);
1425		putushort(denp[1].deHighClust, pcl >> 16);
1426	}
1427
1428	if (DOINGASYNC(ap->a_dvp))
1429		bdwrite(bp);
1430	else if ((error = bwrite(bp)) != 0)
1431		goto bad;
1432
1433	/*
1434	 * Now build up a directory entry pointing to the newly allocated
1435	 * cluster.  This will be written to an empty slot in the parent
1436	 * directory.
1437	 */
1438#ifdef DIAGNOSTIC
1439	if ((cnp->cn_flags & HASBUF) == 0)
1440		panic("msdosfs_mkdir: no name");
1441#endif
1442	error = uniqdosname(pdep, cnp, ndirent.de_Name);
1443	if (error)
1444		goto bad;
1445
1446	ndirent.de_Attributes = ATTR_DIRECTORY;
1447	ndirent.de_LowerCase = 0;
1448	ndirent.de_StartCluster = newcluster;
1449	ndirent.de_FileSize = 0;
1450	error = createde(&ndirent, pdep, &dep, cnp);
1451	if (error)
1452		goto bad;
1453	*ap->a_vpp = DETOV(dep);
1454	return (0);
1455
1456bad:
1457	clusterfree(pmp, newcluster, NULL);
1458bad2:
1459	return (error);
1460}
1461
1462static int
1463msdosfs_rmdir(ap)
1464	struct vop_rmdir_args /* {
1465		struct vnode *a_dvp;
1466		struct vnode *a_vp;
1467		struct componentname *a_cnp;
1468	} */ *ap;
1469{
1470	struct vnode *vp = ap->a_vp;
1471	struct vnode *dvp = ap->a_dvp;
1472	struct componentname *cnp = ap->a_cnp;
1473	struct denode *ip, *dp;
1474	struct thread *td = cnp->cn_thread;
1475	int error;
1476
1477	ip = VTODE(vp);
1478	dp = VTODE(dvp);
1479
1480	/*
1481	 * Verify the directory is empty (and valid).
1482	 * (Rmdir ".." won't be valid since
1483	 *  ".." will contain a reference to
1484	 *  the current directory and thus be
1485	 *  non-empty.)
1486	 */
1487	error = 0;
1488	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1489		error = ENOTEMPTY;
1490		goto out;
1491	}
1492	/*
1493	 * Delete the entry from the directory.  For dos filesystems this
1494	 * gets rid of the directory entry on disk, the in memory copy
1495	 * still exists but the de_refcnt is <= 0.  This prevents it from
1496	 * being found by deget().  When the vput() on dep is done we give
1497	 * up access and eventually msdosfs_reclaim() will be called which
1498	 * will remove it from the denode cache.
1499	 */
1500	error = removede(dp, ip);
1501	if (error)
1502		goto out;
1503	/*
1504	 * This is where we decrement the link count in the parent
1505	 * directory.  Since dos filesystems don't do this we just purge
1506	 * the name cache.
1507	 */
1508	cache_purge(dvp);
1509	/*
1510	 * Truncate the directory that is being deleted.
1511	 */
1512	error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, td);
1513	cache_purge(vp);
1514
1515out:
1516	return (error);
1517}
1518
1519/*
1520 * DOS filesystems don't know what symlinks are.
1521 */
1522static int
1523msdosfs_symlink(ap)
1524	struct vop_symlink_args /* {
1525		struct vnode *a_dvp;
1526		struct vnode **a_vpp;
1527		struct componentname *a_cnp;
1528		struct vattr *a_vap;
1529		char *a_target;
1530	} */ *ap;
1531{
1532	return (EOPNOTSUPP);
1533}
1534
1535static int
1536msdosfs_readdir(ap)
1537	struct vop_readdir_args /* {
1538		struct vnode *a_vp;
1539		struct uio *a_uio;
1540		struct ucred *a_cred;
1541		int *a_eofflag;
1542		int *a_ncookies;
1543		u_long **a_cookies;
1544	} */ *ap;
1545{
1546	struct mbnambuf nb;
1547	int error = 0;
1548	int diff;
1549	long n;
1550	int blsize;
1551	long on;
1552	u_long cn;
1553	uint64_t fileno;
1554	u_long dirsperblk;
1555	long bias = 0;
1556	daddr_t bn, lbn;
1557	struct buf *bp;
1558	struct denode *dep = VTODE(ap->a_vp);
1559	struct msdosfsmount *pmp = dep->de_pmp;
1560	struct direntry *dentp;
1561	struct dirent dirbuf;
1562	struct uio *uio = ap->a_uio;
1563	u_long *cookies = NULL;
1564	int ncookies = 0;
1565	off_t offset, off;
1566	int chksum = -1;
1567
1568#ifdef MSDOSFS_DEBUG
1569	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1570	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1571#endif
1572
1573	/*
1574	 * msdosfs_readdir() won't operate properly on regular files since
1575	 * it does i/o only with the filesystem vnode, and hence can
1576	 * retrieve the wrong block from the buffer cache for a plain file.
1577	 * So, fail attempts to readdir() on a plain file.
1578	 */
1579	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1580		return (ENOTDIR);
1581
1582	/*
1583	 * To be safe, initialize dirbuf
1584	 */
1585	bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1586
1587	/*
1588	 * If the user buffer is smaller than the size of one dos directory
1589	 * entry or the file offset is not a multiple of the size of a
1590	 * directory entry, then we fail the read.
1591	 */
1592	off = offset = uio->uio_offset;
1593	if (uio->uio_resid < sizeof(struct direntry) ||
1594	    (offset & (sizeof(struct direntry) - 1)))
1595		return (EINVAL);
1596
1597	if (ap->a_ncookies) {
1598		ncookies = uio->uio_resid / 16;
1599		cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
1600		       M_WAITOK);
1601		*ap->a_cookies = cookies;
1602		*ap->a_ncookies = ncookies;
1603	}
1604
1605	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1606
1607	/*
1608	 * If they are reading from the root directory then, we simulate
1609	 * the . and .. entries since these don't exist in the root
1610	 * directory.  We also set the offset bias to make up for having to
1611	 * simulate these entries. By this I mean that at file offset 64 we
1612	 * read the first entry in the root directory that lives on disk.
1613	 */
1614	if (dep->de_StartCluster == MSDOSFSROOT
1615	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1616#if 0
1617		printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1618		    offset);
1619#endif
1620		bias = 2 * sizeof(struct direntry);
1621		if (offset < bias) {
1622			for (n = (int)offset / sizeof(struct direntry);
1623			     n < 2; n++) {
1624				if (FAT32(pmp))
1625					fileno = (uint64_t)cntobn(pmp,
1626								 pmp->pm_rootdirblk)
1627							  * dirsperblk;
1628				else
1629					fileno = 1;
1630				if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1631					dirbuf.d_fileno =
1632					    msdosfs_fileno_map(pmp->pm_mountp,
1633					    fileno);
1634				} else {
1635
1636					dirbuf.d_fileno = (uint32_t)fileno;
1637				}
1638				dirbuf.d_type = DT_DIR;
1639				switch (n) {
1640				case 0:
1641					dirbuf.d_namlen = 1;
1642					strcpy(dirbuf.d_name, ".");
1643					break;
1644				case 1:
1645					dirbuf.d_namlen = 2;
1646					strcpy(dirbuf.d_name, "..");
1647					break;
1648				}
1649				dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1650				if (uio->uio_resid < dirbuf.d_reclen)
1651					goto out;
1652				error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1653				if (error)
1654					goto out;
1655				offset += sizeof(struct direntry);
1656				off = offset;
1657				if (cookies) {
1658					*cookies++ = offset;
1659					if (--ncookies <= 0)
1660						goto out;
1661				}
1662			}
1663		}
1664	}
1665
1666	mbnambuf_init(&nb);
1667	off = offset;
1668	while (uio->uio_resid > 0) {
1669		lbn = de_cluster(pmp, offset - bias);
1670		on = (offset - bias) & pmp->pm_crbomask;
1671		n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1672		diff = dep->de_FileSize - (offset - bias);
1673		if (diff <= 0)
1674			break;
1675		n = min(n, diff);
1676		error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1677		if (error)
1678			break;
1679		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1680		if (error) {
1681			brelse(bp);
1682			return (error);
1683		}
1684		n = min(n, blsize - bp->b_resid);
1685		if (n == 0) {
1686			brelse(bp);
1687			return (EIO);
1688		}
1689
1690		/*
1691		 * Convert from dos directory entries to fs-independent
1692		 * directory entries.
1693		 */
1694		for (dentp = (struct direntry *)(bp->b_data + on);
1695		     (char *)dentp < bp->b_data + on + n;
1696		     dentp++, offset += sizeof(struct direntry)) {
1697#if 0
1698			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1699			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1700#endif
1701			/*
1702			 * If this is an unused entry, we can stop.
1703			 */
1704			if (dentp->deName[0] == SLOT_EMPTY) {
1705				brelse(bp);
1706				goto out;
1707			}
1708			/*
1709			 * Skip deleted entries.
1710			 */
1711			if (dentp->deName[0] == SLOT_DELETED) {
1712				chksum = -1;
1713				mbnambuf_init(&nb);
1714				continue;
1715			}
1716
1717			/*
1718			 * Handle Win95 long directory entries
1719			 */
1720			if (dentp->deAttributes == ATTR_WIN95) {
1721				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1722					continue;
1723				chksum = win2unixfn(&nb,
1724				    (struct winentry *)dentp, chksum, pmp);
1725				continue;
1726			}
1727
1728			/*
1729			 * Skip volume labels
1730			 */
1731			if (dentp->deAttributes & ATTR_VOLUME) {
1732				chksum = -1;
1733				mbnambuf_init(&nb);
1734				continue;
1735			}
1736			/*
1737			 * This computation of d_fileno must match
1738			 * the computation of va_fileid in
1739			 * msdosfs_getattr.
1740			 */
1741			if (dentp->deAttributes & ATTR_DIRECTORY) {
1742				fileno = getushort(dentp->deStartCluster);
1743				if (FAT32(pmp))
1744					fileno |= getushort(dentp->deHighClust) << 16;
1745				/* if this is the root directory */
1746				if (fileno == MSDOSFSROOT)
1747					if (FAT32(pmp))
1748						fileno = (uint64_t)cntobn(pmp,
1749								pmp->pm_rootdirblk)
1750							 * dirsperblk;
1751					else
1752						fileno = 1;
1753				else
1754					fileno = (uint64_t)cntobn(pmp, fileno) *
1755					    dirsperblk;
1756				dirbuf.d_type = DT_DIR;
1757			} else {
1758				fileno = (uoff_t)offset /
1759				    sizeof(struct direntry);
1760				dirbuf.d_type = DT_REG;
1761			}
1762			if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1763				dirbuf.d_fileno =
1764				    msdosfs_fileno_map(pmp->pm_mountp, fileno);
1765			} else
1766				dirbuf.d_fileno = (uint32_t)fileno;
1767
1768			if (chksum != winChksum(dentp->deName)) {
1769				dirbuf.d_namlen = dos2unixfn(dentp->deName,
1770				    (u_char *)dirbuf.d_name,
1771				    dentp->deLowerCase |
1772					((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1773					(LCASE_BASE | LCASE_EXT) : 0),
1774				    pmp);
1775				mbnambuf_init(&nb);
1776			} else
1777				mbnambuf_flush(&nb, &dirbuf);
1778			chksum = -1;
1779			dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1780			if (uio->uio_resid < dirbuf.d_reclen) {
1781				brelse(bp);
1782				goto out;
1783			}
1784			error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1785			if (error) {
1786				brelse(bp);
1787				goto out;
1788			}
1789			if (cookies) {
1790				*cookies++ = offset + sizeof(struct direntry);
1791				if (--ncookies <= 0) {
1792					brelse(bp);
1793					goto out;
1794				}
1795			}
1796			off = offset + sizeof(struct direntry);
1797		}
1798		brelse(bp);
1799	}
1800out:
1801	/* Subtract unused cookies */
1802	if (ap->a_ncookies)
1803		*ap->a_ncookies -= ncookies;
1804
1805	uio->uio_offset = off;
1806
1807	/*
1808	 * Set the eofflag (NFS uses it)
1809	 */
1810	if (ap->a_eofflag) {
1811		if (dep->de_FileSize - (offset - bias) <= 0)
1812			*ap->a_eofflag = 1;
1813		else
1814			*ap->a_eofflag = 0;
1815	}
1816	return (error);
1817}
1818
1819/*-
1820 * a_vp   - pointer to the file's vnode
1821 * a_bn   - logical block number within the file (cluster number for us)
1822 * a_bop  - where to return the bufobj of the special file containing the fs
1823 * a_bnp  - where to return the "physical" block number corresponding to a_bn
1824 *          (relative to the special file; units are blocks of size DEV_BSIZE)
1825 * a_runp - where to return the "run past" a_bn.  This is the count of logical
1826 *          blocks whose physical blocks (together with a_bn's physical block)
1827 *          are contiguous.
1828 * a_runb - where to return the "run before" a_bn.
1829 */
1830static int
1831msdosfs_bmap(ap)
1832	struct vop_bmap_args /* {
1833		struct vnode *a_vp;
1834		daddr_t a_bn;
1835		struct bufobj **a_bop;
1836		daddr_t *a_bnp;
1837		int *a_runp;
1838		int *a_runb;
1839	} */ *ap;
1840{
1841	struct denode *dep;
1842	struct mount *mp;
1843	struct msdosfsmount *pmp;
1844	struct vnode *vp;
1845	daddr_t runbn;
1846	u_long cn;
1847	int bnpercn, error, maxio, maxrun, run;
1848
1849	vp = ap->a_vp;
1850	dep = VTODE(vp);
1851	pmp = dep->de_pmp;
1852	if (ap->a_bop != NULL)
1853		*ap->a_bop = &pmp->pm_devvp->v_bufobj;
1854	if (ap->a_bnp == NULL)
1855		return (0);
1856	if (ap->a_runp != NULL)
1857		*ap->a_runp = 0;
1858	if (ap->a_runb != NULL)
1859		*ap->a_runb = 0;
1860	cn = ap->a_bn;
1861	if (cn != ap->a_bn)
1862		return (EFBIG);
1863	error = pcbmap(dep, cn, ap->a_bnp, NULL, NULL);
1864	if (error != 0 || (ap->a_runp == NULL && ap->a_runb == NULL))
1865		return (error);
1866
1867	mp = vp->v_mount;
1868	maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1869	bnpercn = de_cn2bn(pmp, 1);
1870	if (ap->a_runp != NULL) {
1871		maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1872		for (run = 1; run <= maxrun; run++) {
1873			if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1874			    runbn != *ap->a_bnp + run * bnpercn)
1875				break;
1876		}
1877		*ap->a_runp = run - 1;
1878	}
1879	if (ap->a_runb != NULL) {
1880		maxrun = ulmin(maxio - 1, cn);
1881		for (run = 1; run < maxrun; run++) {
1882			if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1883			    runbn != *ap->a_bnp - run * bnpercn)
1884				break;
1885		}
1886		*ap->a_runb = run - 1;
1887	}
1888	return (0);
1889}
1890
1891static int
1892msdosfs_strategy(ap)
1893	struct vop_strategy_args /* {
1894		struct vnode *a_vp;
1895		struct buf *a_bp;
1896	} */ *ap;
1897{
1898	struct buf *bp = ap->a_bp;
1899	struct denode *dep = VTODE(ap->a_vp);
1900	struct bufobj *bo;
1901	int error = 0;
1902	daddr_t blkno;
1903
1904	/*
1905	 * If we don't already know the filesystem relative block number
1906	 * then get it using pcbmap().  If pcbmap() returns the block
1907	 * number as -1 then we've got a hole in the file.  DOS filesystems
1908	 * don't allow files with holes, so we shouldn't ever see this.
1909	 */
1910	if (bp->b_blkno == bp->b_lblkno) {
1911		error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1912		bp->b_blkno = blkno;
1913		if (error) {
1914			bp->b_error = error;
1915			bp->b_ioflags |= BIO_ERROR;
1916			bufdone(bp);
1917			return (0);
1918		}
1919		if ((long)bp->b_blkno == -1)
1920			vfs_bio_clrbuf(bp);
1921	}
1922	if (bp->b_blkno == -1) {
1923		bufdone(bp);
1924		return (0);
1925	}
1926	/*
1927	 * Read/write the block from/to the disk that contains the desired
1928	 * file block.
1929	 */
1930	bp->b_iooffset = dbtob(bp->b_blkno);
1931	bo = dep->de_pmp->pm_bo;
1932	BO_STRATEGY(bo, bp);
1933	return (0);
1934}
1935
1936static int
1937msdosfs_print(ap)
1938	struct vop_print_args /* {
1939		struct vnode *vp;
1940	} */ *ap;
1941{
1942	struct denode *dep = VTODE(ap->a_vp);
1943
1944	printf("\tstartcluster %lu, dircluster %lu, diroffset %lu, ",
1945	       dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1946	printf("on dev %s\n", devtoname(dep->de_pmp->pm_dev));
1947	return (0);
1948}
1949
1950static int
1951msdosfs_pathconf(ap)
1952	struct vop_pathconf_args /* {
1953		struct vnode *a_vp;
1954		int a_name;
1955		int *a_retval;
1956	} */ *ap;
1957{
1958	struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1959
1960	switch (ap->a_name) {
1961	case _PC_LINK_MAX:
1962		*ap->a_retval = 1;
1963		return (0);
1964	case _PC_NAME_MAX:
1965		*ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1966		return (0);
1967	case _PC_PATH_MAX:
1968		*ap->a_retval = PATH_MAX;
1969		return (0);
1970	case _PC_CHOWN_RESTRICTED:
1971		*ap->a_retval = 1;
1972		return (0);
1973	case _PC_NO_TRUNC:
1974		*ap->a_retval = 0;
1975		return (0);
1976	default:
1977		return (EINVAL);
1978	}
1979	/* NOTREACHED */
1980}
1981
1982static int
1983msdosfs_vptofh(ap)
1984	struct vop_vptofh_args /* {
1985		struct vnode *a_vp;
1986		struct fid *a_fhp;
1987	} */ *ap;
1988{
1989	struct denode *dep;
1990	struct defid *defhp;
1991
1992	dep = VTODE(ap->a_vp);
1993	defhp = (struct defid *)ap->a_fhp;
1994	defhp->defid_len = sizeof(struct defid);
1995	defhp->defid_dirclust = dep->de_dirclust;
1996	defhp->defid_dirofs = dep->de_diroffset;
1997	/* defhp->defid_gen = dep->de_gen; */
1998	return (0);
1999}
2000
2001/* Global vfs data structures for msdosfs */
2002struct vop_vector msdosfs_vnodeops = {
2003	.vop_default =		&default_vnodeops,
2004
2005	.vop_access =		msdosfs_access,
2006	.vop_bmap =		msdosfs_bmap,
2007	.vop_cachedlookup =	msdosfs_lookup,
2008	.vop_open =		msdosfs_open,
2009	.vop_close =		msdosfs_close,
2010	.vop_create =		msdosfs_create,
2011	.vop_fsync =		msdosfs_fsync,
2012	.vop_getattr =		msdosfs_getattr,
2013	.vop_inactive =		msdosfs_inactive,
2014	.vop_link =		msdosfs_link,
2015	.vop_lookup =		vfs_cache_lookup,
2016	.vop_mkdir =		msdosfs_mkdir,
2017	.vop_mknod =		msdosfs_mknod,
2018	.vop_pathconf =		msdosfs_pathconf,
2019	.vop_print =		msdosfs_print,
2020	.vop_read =		msdosfs_read,
2021	.vop_readdir =		msdosfs_readdir,
2022	.vop_reclaim =		msdosfs_reclaim,
2023	.vop_remove =		msdosfs_remove,
2024	.vop_rename =		msdosfs_rename,
2025	.vop_rmdir =		msdosfs_rmdir,
2026	.vop_setattr =		msdosfs_setattr,
2027	.vop_strategy =		msdosfs_strategy,
2028	.vop_symlink =		msdosfs_symlink,
2029	.vop_write =		msdosfs_write,
2030	.vop_vptofh =		msdosfs_vptofh,
2031};
2032