msdosfs_vnops.c revision 175294
1281SN/A/* $FreeBSD: head/sys/fs/msdosfs/msdosfs_vnops.c 175294 2008-01-13 14:44:15Z attilio $ */
21129Sjjg/*	$NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $	*/
3281SN/A
4281SN/A/*-
5281SN/A * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6281SN/A * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7281SN/A * All rights reserved.
8281SN/A * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9281SN/A *
10281SN/A * Redistribution and use in source and binary forms, with or without
11281SN/A * modification, are permitted provided that the following conditions
12281SN/A * are met:
13281SN/A * 1. Redistributions of source code must retain the above copyright
14281SN/A *    notice, this list of conditions and the following disclaimer.
15281SN/A * 2. Redistributions in binary form must reproduce the above copyright
16281SN/A *    notice, this list of conditions and the following disclaimer in the
17281SN/A *    documentation and/or other materials provided with the distribution.
18281SN/A * 3. All advertising materials mentioning features or use of this software
19281SN/A *    must display the following acknowledgement:
20281SN/A *	This product includes software developed by TooLs GmbH.
21281SN/A * 4. The name of TooLs GmbH may not be used to endorse or promote products
22281SN/A *    derived from this software without specific prior written permission.
23281SN/A *
24281SN/A * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25281SN/A * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26281SN/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27281SN/A * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
281038Sdfuchs * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29410SN/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30281SN/A * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31281SN/A * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32281SN/A * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33281SN/A * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34281SN/A */
35281SN/A/*-
36281SN/A * Written by Paul Popelka (paulp@uts.amdahl.com)
37281SN/A *
38281SN/A * You can do anything you want with this software, just don't say you wrote
391129Sjjg * it, and don't remove this notice.
401129Sjjg *
41281SN/A * This software is provided "as is".
42281SN/A *
43281SN/A * The author supplies this software to be publicly redistributed on the
44281SN/A * understanding that the author is not responsible for the correct
45281SN/A * functioning of this software in any circumstances and is not liable for
46281SN/A * any damages caused by this software.
47281SN/A *
48281SN/A * October 1992
491129Sjjg */
501129Sjjg
51281SN/A#include <sys/param.h>
52281SN/A#include <sys/systm.h>
53281SN/A#include <sys/bio.h>
54281SN/A#include <sys/buf.h>
55281SN/A#include <sys/clock.h>
56281SN/A#include <sys/dirent.h>
57281SN/A#include <sys/lock.h>
58281SN/A#include <sys/lockf.h>
59281SN/A#include <sys/malloc.h>
60281SN/A#include <sys/mount.h>
61281SN/A#include <sys/mutex.h>
62281SN/A#include <sys/namei.h>
63281SN/A#include <sys/priv.h>
64281SN/A#include <sys/proc.h>
65705Sjoehw#include <sys/resourcevar.h>
66281SN/A#include <sys/signalvar.h>
67281SN/A#include <sys/stat.h>
68281SN/A#include <sys/unistd.h>
69281SN/A#include <sys/vnode.h>
70281SN/A
71281SN/A#include <vm/vm.h>
72281SN/A#include <vm/vm_extern.h>
73281SN/A
74281SN/A#include <fs/msdosfs/bpb.h>
75281SN/A#include <fs/msdosfs/direntry.h>
76281SN/A#include <fs/msdosfs/denode.h>
77281SN/A#include <fs/msdosfs/fat.h>
78281SN/A#include <fs/msdosfs/msdosfsmount.h>
79281SN/A
80281SN/A#define	DOS_FILESIZE_MAX	0xffffffff
81281SN/A
82281SN/A/*
83281SN/A * Prototypes for MSDOSFS vnode operations
84281SN/A */
85281SN/Astatic vop_advlock_t	msdosfs_advlock;
86281SN/Astatic vop_create_t	msdosfs_create;
87281SN/Astatic vop_mknod_t	msdosfs_mknod;
88281SN/Astatic vop_open_t	msdosfs_open;
89281SN/Astatic vop_close_t	msdosfs_close;
90281SN/Astatic vop_access_t	msdosfs_access;
91281SN/Astatic vop_getattr_t	msdosfs_getattr;
92281SN/Astatic vop_setattr_t	msdosfs_setattr;
93281SN/Astatic vop_read_t	msdosfs_read;
94281SN/Astatic vop_write_t	msdosfs_write;
95281SN/Astatic vop_fsync_t	msdosfs_fsync;
96281SN/Astatic vop_remove_t	msdosfs_remove;
97281SN/Astatic vop_link_t	msdosfs_link;
98281SN/Astatic vop_rename_t	msdosfs_rename;
99281SN/Astatic vop_mkdir_t	msdosfs_mkdir;
100281SN/Astatic vop_rmdir_t	msdosfs_rmdir;
101281SN/Astatic vop_symlink_t	msdosfs_symlink;
102281SN/Astatic vop_readdir_t	msdosfs_readdir;
103281SN/Astatic vop_bmap_t	msdosfs_bmap;
104281SN/Astatic vop_strategy_t	msdosfs_strategy;
105281SN/Astatic vop_print_t	msdosfs_print;
106281SN/Astatic vop_pathconf_t	msdosfs_pathconf;
107281SN/Astatic vop_vptofh_t	msdosfs_vptofh;
108281SN/A
109281SN/A/*
110281SN/A * Some general notes:
111281SN/A *
112281SN/A * In the ufs filesystem the inodes, superblocks, and indirect blocks are
113281SN/A * read/written using the vnode for the filesystem. Blocks that represent
114281SN/A * the contents of a file are read/written using the vnode for the file
115281SN/A * (including directories when they are read/written as files). This
116281SN/A * presents problems for the dos filesystem because data that should be in
117281SN/A * an inode (if dos had them) resides in the directory itself.  Since we
118281SN/A * must update directory entries without the benefit of having the vnode
119281SN/A * for the directory we must use the vnode for the filesystem.  This means
120281SN/A * that when a directory is actually read/written (via read, write, or
121281SN/A * readdir, or seek) we must use the vnode for the filesystem instead of
122281SN/A * the vnode for the directory as would happen in ufs. This is to insure we
123281SN/A * retreive the correct block from the buffer cache since the hash value is
124281SN/A * based upon the vnode address and the desired block number.
125281SN/A */
126281SN/A
127281SN/A/*
128281SN/A * Create a regular file. On entry the directory to contain the file being
129281SN/A * created is locked.  We must release before we return. We must also free
130281SN/A * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
131281SN/A * only if the SAVESTART bit in cn_flags is clear on success.
132281SN/A */
133281SN/Astatic int
134281SN/Amsdosfs_create(ap)
135281SN/A	struct vop_create_args /* {
136281SN/A		struct vnode *a_dvp;
137281SN/A		struct vnode **a_vpp;
138281SN/A		struct componentname *a_cnp;
139281SN/A		struct vattr *a_vap;
140281SN/A	} */ *ap;
1411038Sdfuchs{
1421038Sdfuchs	struct componentname *cnp = ap->a_cnp;
1431038Sdfuchs	struct denode ndirent;
1441038Sdfuchs	struct denode *dep;
1451038Sdfuchs	struct denode *pdep = VTODE(ap->a_dvp);
1461038Sdfuchs	struct timespec ts;
1471038Sdfuchs	int error;
1481038Sdfuchs
1491038Sdfuchs#ifdef MSDOSFS_DEBUG
1501038Sdfuchs	printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
1511038Sdfuchs#endif
1521038Sdfuchs
1531038Sdfuchs	/*
154410SN/A	 * If this is the root directory and there is no space left we
155410SN/A	 * can't do anything.  This is because the root directory can not
156281SN/A	 * change size.
157281SN/A	 */
158281SN/A	if (pdep->de_StartCluster == MSDOSFSROOT
159281SN/A	    && pdep->de_fndoffset >= pdep->de_FileSize) {
160281SN/A		error = ENOSPC;
161410SN/A		goto bad;
162281SN/A	}
163281SN/A
164281SN/A	/*
165281SN/A	 * Create a directory entry for the file, then call createde() to
166410SN/A	 * have it installed. NOTE: DOS files are always executable.  We
167281SN/A	 * use the absence of the owner write bit to make the file
168281SN/A	 * readonly.
169281SN/A	 */
170726Savstepan#ifdef DIAGNOSTIC
171410SN/A	if ((cnp->cn_flags & HASBUF) == 0)
172410SN/A		panic("msdosfs_create: no name");
173705Sjoehw#endif
174410SN/A	bzero(&ndirent, sizeof(ndirent));
175410SN/A	error = uniqdosname(pdep, cnp, ndirent.de_Name);
176705Sjoehw	if (error)
177705Sjoehw		goto bad;
178705Sjoehw
179705Sjoehw	ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
180705Sjoehw				ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
181705Sjoehw	ndirent.de_LowerCase = 0;
182705Sjoehw	ndirent.de_StartCluster = 0;
183705Sjoehw	ndirent.de_FileSize = 0;
184705Sjoehw	ndirent.de_dev = pdep->de_dev;
185705Sjoehw	ndirent.de_pmp = pdep->de_pmp;
186705Sjoehw	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
187705Sjoehw	getnanotime(&ts);
188705Sjoehw	DETIMES(&ndirent, &ts, &ts, &ts);
189705Sjoehw	error = createde(&ndirent, pdep, &dep, cnp);
190705Sjoehw	if (error)
191705Sjoehw		goto bad;
192410SN/A	*ap->a_vpp = DETOV(dep);
193410SN/A	return (0);
194705Sjoehw
195410SN/Abad:
196489SN/A	return (error);
197489SN/A}
198489SN/A
199489SN/Astatic int
200489SN/Amsdosfs_mknod(ap)
201489SN/A	struct vop_mknod_args /* {
202489SN/A		struct vnode *a_dvp;
203410SN/A		struct vnode **a_vpp;
204410SN/A		struct componentname *a_cnp;
2051038Sdfuchs		struct vattr *a_vap;
2061038Sdfuchs	} */ *ap;
207410SN/A{
208410SN/A
209410SN/A    return (EINVAL);
210281SN/A}
211281SN/A
212410SN/Astatic int
213281SN/Amsdosfs_open(ap)
214281SN/A	struct vop_open_args /* {
215281SN/A		struct vnode *a_vp;
216726Savstepan		int a_mode;
217410SN/A		struct ucred *a_cred;
218410SN/A		struct thread *a_td;
219410SN/A		int a_fdidx;
220281SN/A	} */ *ap;
2211096Sjoehw{
222281SN/A	struct denode *dep = VTODE(ap->a_vp);
223281SN/A	vnode_create_vobject(ap->a_vp, dep->de_FileSize, ap->a_td);
224281SN/A	return 0;
225410SN/A}
226281SN/A
227281SN/Astatic int
228281SN/Amsdosfs_close(ap)
229281SN/A	struct vop_close_args /* {
230281SN/A		struct vnode *a_vp;
231281SN/A		int a_fflag;
232281SN/A		struct ucred *a_cred;
233281SN/A		struct thread *a_td;
234281SN/A	} */ *ap;
235281SN/A{
236281SN/A	struct vnode *vp = ap->a_vp;
237281SN/A	struct denode *dep = VTODE(vp);
238281SN/A	struct timespec ts;
239281SN/A
240281SN/A	VI_LOCK(vp);
241281SN/A	if (vp->v_usecount > 1) {
242281SN/A		getnanotime(&ts);
2431096Sjoehw		DETIMES(dep, &ts, &ts, &ts);
244281SN/A	}
245281SN/A	VI_UNLOCK(vp);
246281SN/A	return 0;
247410SN/A}
248410SN/A
249281SN/Astatic int
250281SN/Amsdosfs_access(ap)
251281SN/A	struct vop_access_args /* {
252281SN/A		struct vnode *a_vp;
253281SN/A		int a_mode;
254410SN/A		struct ucred *a_cred;
255410SN/A		struct thread *a_td;
256410SN/A	} */ *ap;
257410SN/A{
258410SN/A	struct vnode *vp = ap->a_vp;
259705Sjoehw	struct denode *dep = VTODE(ap->a_vp);
260410SN/A	struct msdosfsmount *pmp = dep->de_pmp;
261410SN/A	mode_t file_mode, mode = ap->a_mode;
262410SN/A
263705Sjoehw	file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
264705Sjoehw	    ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
265705Sjoehw	file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
266705Sjoehw
267705Sjoehw	/*
268705Sjoehw	 * Disallow writing to directories and regular files if the
269705Sjoehw	 * filesystem is read-only.
270705Sjoehw	 */
271705Sjoehw	if (mode & VWRITE) {
272705Sjoehw		switch (vp->v_type) {
273705Sjoehw		case VDIR:
274705Sjoehw		case VREG:
275705Sjoehw			if (vp->v_mount->mnt_flag & MNT_RDONLY)
276705Sjoehw				return (EROFS);
277705Sjoehw			break;
278705Sjoehw		default:
279705Sjoehw			break;
280705Sjoehw		}
281410SN/A	}
282410SN/A
283705Sjoehw	return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
284410SN/A	    ap->a_mode, ap->a_cred, NULL));
285705Sjoehw}
286557SN/A
287557SN/Astatic int
288557SN/Amsdosfs_getattr(ap)
289541SN/A	struct vop_getattr_args /* {
290541SN/A		struct vnode *a_vp;
291541SN/A		struct vattr *a_vap;
292489SN/A		struct ucred *a_cred;
293489SN/A		struct thread *a_td;
294489SN/A	} */ *ap;
295489SN/A{
296410SN/A	struct denode *dep = VTODE(ap->a_vp);
297410SN/A	struct msdosfsmount *pmp = dep->de_pmp;
298705Sjoehw	struct vattr *vap = ap->a_vap;
299410SN/A	mode_t mode;
300410SN/A	struct timespec ts;
301410SN/A	u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
302281SN/A	uint64_t fileid;
303410SN/A
304281SN/A	getnanotime(&ts);
305410SN/A	DETIMES(dep, &ts, &ts, &ts);
306410SN/A	vap->va_fsid = dev2udev(dep->de_dev);
307281SN/A	/*
308281SN/A	 * The following computation of the fileid must be the same as that
309705Sjoehw	 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
310281SN/A	 * doesn't work.
311541SN/A	 */
312541SN/A	if (dep->de_Attributes & ATTR_DIRECTORY) {
313541SN/A		fileid = (uint64_t)cntobn(pmp, dep->de_StartCluster) *
314541SN/A		    dirsperblk;
315281SN/A		if (dep->de_StartCluster == MSDOSFSROOT)
316281SN/A			fileid = 1;
317281SN/A	} else {
318281SN/A		fileid = (uint64_t)cntobn(pmp, dep->de_dirclust) *
319410SN/A		    dirsperblk;
320410SN/A		if (dep->de_dirclust == MSDOSFSROOT)
321410SN/A			fileid = (uint64_t)roottobn(pmp, 0) * dirsperblk;
322281SN/A		fileid += (uoff_t)dep->de_diroffset / sizeof(struct direntry);
323281SN/A	}
324281SN/A
325281SN/A	if (pmp->pm_flags & MSDOSFS_LARGEFS)
326281SN/A		vap->va_fileid = msdosfs_fileno_map(pmp->pm_mountp, fileid);
327410SN/A	else
328410SN/A		vap->va_fileid = (long)fileid;
329281SN/A
330281SN/A	if ((dep->de_Attributes & ATTR_READONLY) == 0)
331281SN/A		mode = S_IRWXU|S_IRWXG|S_IRWXO;
332281SN/A	else
333281SN/A		mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
334281SN/A	vap->va_mode = mode &
335281SN/A	    (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
336281SN/A	vap->va_uid = pmp->pm_uid;
337281SN/A	vap->va_gid = pmp->pm_gid;
338281SN/A	vap->va_nlink = 1;
339281SN/A	vap->va_rdev = 0;
340281SN/A	vap->va_size = dep->de_FileSize;
341281SN/A	fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
342281SN/A	vap->va_ctime = vap->va_mtime;
343281SN/A	if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
344281SN/A		fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
345281SN/A		fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
346281SN/A		    0, &vap->va_birthtime);
347281SN/A	} else {
348281SN/A		vap->va_atime = vap->va_mtime;
349281SN/A		vap->va_birthtime.tv_sec = -1;
350281SN/A		vap->va_birthtime.tv_nsec = 0;
351281SN/A	}
352281SN/A	vap->va_flags = 0;
353281SN/A	if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
354281SN/A		vap->va_flags |= SF_ARCHIVED;
355281SN/A	vap->va_gen = 0;
356281SN/A	vap->va_blocksize = pmp->pm_bpcluster;
357281SN/A	vap->va_bytes =
358281SN/A	    (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
359281SN/A	vap->va_type = ap->a_vp->v_type;
360281SN/A	vap->va_filerev = dep->de_modrev;
361281SN/A	return (0);
362281SN/A}
363281SN/A
364281SN/Astatic int
365281SN/Amsdosfs_setattr(ap)
366281SN/A	struct vop_setattr_args /* {
367281SN/A		struct vnode *a_vp;
368281SN/A		struct vattr *a_vap;
369281SN/A		struct ucred *a_cred;
370281SN/A		struct thread *a_td;
371281SN/A	} */ *ap;
372281SN/A{
373281SN/A	struct vnode *vp = ap->a_vp;
374281SN/A	struct denode *dep = VTODE(ap->a_vp);
375281SN/A	struct msdosfsmount *pmp = dep->de_pmp;
376281SN/A	struct vattr *vap = ap->a_vap;
377281SN/A	struct ucred *cred = ap->a_cred;
378281SN/A	int error = 0;
379281SN/A
380281SN/A#ifdef MSDOSFS_DEBUG
381281SN/A	printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
382281SN/A	    ap->a_vp, vap, cred, ap->a_td);
383281SN/A#endif
384281SN/A
385281SN/A	/*
386281SN/A	 * Check for unsettable attributes.
387281SN/A	 */
388281SN/A	if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
389281SN/A	    (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
390281SN/A	    (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
391281SN/A	    (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
392281SN/A#ifdef MSDOSFS_DEBUG
393281SN/A		printf("msdosfs_setattr(): returning EINVAL\n");
394281SN/A		printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
395281SN/A		    vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
396281SN/A		printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
397281SN/A		    vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
398281SN/A		printf("    va_uid %x, va_gid %x\n",
399281SN/A		    vap->va_uid, vap->va_gid);
400281SN/A#endif
401281SN/A		return (EINVAL);
402281SN/A	}
403281SN/A	if (vap->va_flags != VNOVAL) {
404281SN/A		if (vp->v_mount->mnt_flag & MNT_RDONLY)
405281SN/A			return (EROFS);
406281SN/A		if (cred->cr_uid != pmp->pm_uid) {
407281SN/A			error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
408281SN/A			if (error)
409281SN/A				return (error);
410281SN/A		}
411281SN/A		/*
412281SN/A		 * We are very inconsistent about handling unsupported
413281SN/A		 * attributes.  We ignored the access time and the
414281SN/A		 * read and execute bits.  We were strict for the other
415281SN/A		 * attributes.
416281SN/A		 *
417281SN/A		 * Here we are strict, stricter than ufs in not allowing
418281SN/A		 * users to attempt to set SF_SETTABLE bits or anyone to
419281SN/A		 * set unsupported bits.  However, we ignore attempts to
420281SN/A		 * set ATTR_ARCHIVE for directories `cp -pr' from a more
421281SN/A		 * sensible filesystem attempts it a lot.
422281SN/A		 */
423281SN/A		if (vap->va_flags & SF_SETTABLE) {
424281SN/A			error = priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0);
425281SN/A			if (error)
426281SN/A				return (error);
427281SN/A		}
428281SN/A		if (vap->va_flags & ~SF_ARCHIVED)
429281SN/A			return EOPNOTSUPP;
430281SN/A		if (vap->va_flags & SF_ARCHIVED)
431281SN/A			dep->de_Attributes &= ~ATTR_ARCHIVE;
432281SN/A		else if (!(dep->de_Attributes & ATTR_DIRECTORY))
433281SN/A			dep->de_Attributes |= ATTR_ARCHIVE;
434281SN/A		dep->de_flag |= DE_MODIFIED;
435281SN/A	}
436281SN/A
437281SN/A	if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
438281SN/A		uid_t uid;
439281SN/A		gid_t gid;
440281SN/A
441281SN/A		if (vp->v_mount->mnt_flag & MNT_RDONLY)
442281SN/A			return (EROFS);
443281SN/A		uid = vap->va_uid;
444281SN/A		if (uid == (uid_t)VNOVAL)
445281SN/A			uid = pmp->pm_uid;
446281SN/A		gid = vap->va_gid;
447281SN/A		if (gid == (gid_t)VNOVAL)
448281SN/A			gid = pmp->pm_gid;
449281SN/A		if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
450281SN/A		    (gid != pmp->pm_gid && !groupmember(gid, cred))) {
451281SN/A			error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
452281SN/A			if (error)
453281SN/A				return (error);
454281SN/A		}
455281SN/A		if (uid != pmp->pm_uid || gid != pmp->pm_gid)
456281SN/A			return EINVAL;
457281SN/A	}
458281SN/A
459281SN/A	if (vap->va_size != VNOVAL) {
460281SN/A		switch (vp->v_type) {
461281SN/A		case VDIR:
462281SN/A			return (EISDIR);
463281SN/A		case VREG:
464281SN/A			/*
465281SN/A			 * Truncation is only supported for regular files,
466281SN/A			 * Disallow it if the filesystem is read-only.
467281SN/A			 */
468281SN/A			if (vp->v_mount->mnt_flag & MNT_RDONLY)
469281SN/A				return (EROFS);
470281SN/A			break;
471281SN/A		default:
472281SN/A			/*
473281SN/A			 * According to POSIX, the result is unspecified
474281SN/A			 * for file types other than regular files,
475281SN/A			 * directories and shared memory objects.  We
476281SN/A			 * don't support any file types except regular
477281SN/A			 * files and directories in this file system, so
478281SN/A			 * this (default) case is unreachable and can do
479281SN/A			 * anything.  Keep falling through to detrunc()
480281SN/A			 * for now.
481281SN/A			 */
482281SN/A			break;
483281SN/A		}
484281SN/A		error = detrunc(dep, vap->va_size, 0, cred, ap->a_td);
485281SN/A		if (error)
486281SN/A			return error;
487281SN/A	}
488281SN/A	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
489281SN/A		if (vp->v_mount->mnt_flag & MNT_RDONLY)
490281SN/A			return (EROFS);
491418SN/A		if (vap->va_vaflags & VA_UTIMES_NULL) {
492418SN/A			error = VOP_ACCESS(vp, VADMIN, cred, ap->a_td);
493418SN/A			if (error)
494418SN/A				error = VOP_ACCESS(vp, VWRITE, cred,
495418SN/A				    ap->a_td);
496418SN/A		} else
497418SN/A			error = VOP_ACCESS(vp, VADMIN, cred, ap->a_td);
498418SN/A		if (vp->v_type != VDIR) {
499418SN/A			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
500418SN/A			    vap->va_atime.tv_sec != VNOVAL) {
501418SN/A				dep->de_flag &= ~DE_ACCESS;
502418SN/A				timespec2fattime(&vap->va_atime, 0,
503418SN/A				    &dep->de_ADate, NULL, NULL);
504418SN/A			}
505421SN/A			if (vap->va_mtime.tv_sec != VNOVAL) {
506421SN/A				dep->de_flag &= ~DE_UPDATE;
507726Savstepan				timespec2fattime(&vap->va_mtime, 0,
508418SN/A				    &dep->de_MDate, &dep->de_MTime, NULL);
509418SN/A			}
510281SN/A			dep->de_Attributes |= ATTR_ARCHIVE;
511281SN/A			dep->de_flag |= DE_MODIFIED;
512281SN/A		}
513281SN/A	}
514281SN/A	/*
515281SN/A	 * DOS files only have the ability to have their writability
516281SN/A	 * attribute set, so we use the owner write bit to set the readonly
517281SN/A	 * attribute.
518281SN/A	 */
519281SN/A	if (vap->va_mode != (mode_t)VNOVAL) {
520281SN/A		if (vp->v_mount->mnt_flag & MNT_RDONLY)
521281SN/A			return (EROFS);
522281SN/A		if (cred->cr_uid != pmp->pm_uid) {
523281SN/A			error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
524281SN/A			if (error)
525281SN/A				return (error);
526281SN/A		}
527281SN/A		if (vp->v_type != VDIR) {
528281SN/A			/* We ignore the read and execute bits. */
529281SN/A			if (vap->va_mode & VWRITE)
530281SN/A				dep->de_Attributes &= ~ATTR_READONLY;
531281SN/A			else
532281SN/A				dep->de_Attributes |= ATTR_READONLY;
533281SN/A			dep->de_Attributes |= ATTR_ARCHIVE;
534281SN/A			dep->de_flag |= DE_MODIFIED;
535281SN/A		}
536281SN/A	}
537281SN/A	return (deupdat(dep, 0));
538281SN/A}
539281SN/A
540281SN/Astatic int
541281SN/Amsdosfs_read(ap)
542281SN/A	struct vop_read_args /* {
543281SN/A		struct vnode *a_vp;
544281SN/A		struct uio *a_uio;
545281SN/A		int a_ioflag;
546281SN/A		struct ucred *a_cred;
547	} */ *ap;
548{
549	int error = 0;
550	int blsize;
551	int isadir;
552	int orig_resid;
553	u_int n;
554	u_long diff;
555	u_long on;
556	daddr_t lbn;
557	daddr_t rablock;
558	int rasize;
559	int seqcount;
560	struct buf *bp;
561	struct vnode *vp = ap->a_vp;
562	struct denode *dep = VTODE(vp);
563	struct msdosfsmount *pmp = dep->de_pmp;
564	struct uio *uio = ap->a_uio;
565
566	/*
567	 * If they didn't ask for any data, then we are done.
568	 */
569	orig_resid = uio->uio_resid;
570	if (orig_resid == 0)
571		return (0);
572
573	/*
574	 * The caller is supposed to ensure that
575	 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
576	 * We don't need to check for large offsets as in ffs because
577	 * dep->de_FileSize <= DOS_FILESIZE_MAX < OFF_MAX, so large
578	 * offsets cannot cause overflow even in theory.
579	 */
580
581	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
582
583	isadir = dep->de_Attributes & ATTR_DIRECTORY;
584	do {
585		if (uio->uio_offset >= dep->de_FileSize)
586			break;
587		lbn = de_cluster(pmp, uio->uio_offset);
588		rablock = lbn + 1;
589		blsize = pmp->pm_bpcluster;
590		on = uio->uio_offset & pmp->pm_crbomask;
591		/*
592		 * If we are operating on a directory file then be sure to
593		 * do i/o with the vnode for the filesystem instead of the
594		 * vnode for the directory.
595		 */
596		if (isadir) {
597			/* convert cluster # to block # */
598			error = pcbmap(dep, lbn, &lbn, 0, &blsize);
599			if (error == E2BIG) {
600				error = EINVAL;
601				break;
602			} else if (error)
603				break;
604			error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
605		} else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
606			error = bread(vp, lbn, blsize, NOCRED, &bp);
607		} else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
608			error = cluster_read(vp, dep->de_FileSize, lbn, blsize,
609			    NOCRED, on + uio->uio_resid, seqcount, &bp);
610		} else if (seqcount > 1) {
611			rasize = blsize;
612			error = breadn(vp, lbn,
613			    blsize, &rablock, &rasize, 1, NOCRED, &bp);
614		} else {
615			error = bread(vp, lbn, blsize, NOCRED, &bp);
616		}
617		if (error) {
618			brelse(bp);
619			break;
620		}
621		diff = pmp->pm_bpcluster - on;
622		n = diff > uio->uio_resid ? uio->uio_resid : diff;
623		diff = dep->de_FileSize - uio->uio_offset;
624		if (diff < n)
625			n = diff;
626		diff = blsize - bp->b_resid;
627		if (diff < n)
628			n = diff;
629		error = uiomove(bp->b_data + on, (int) n, uio);
630		brelse(bp);
631	} while (error == 0 && uio->uio_resid > 0 && n != 0);
632	if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
633	    (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
634		dep->de_flag |= DE_ACCESS;
635	return (error);
636}
637
638/*
639 * Write data to a file or directory.
640 */
641static int
642msdosfs_write(ap)
643	struct vop_write_args /* {
644		struct vnode *a_vp;
645		struct uio *a_uio;
646		int a_ioflag;
647		struct ucred *a_cred;
648	} */ *ap;
649{
650	int n;
651	int croffset;
652	int resid;
653	u_long osize;
654	int error = 0;
655	u_long count;
656	int seqcount;
657	daddr_t bn, lastcn;
658	struct buf *bp;
659	int ioflag = ap->a_ioflag;
660	struct uio *uio = ap->a_uio;
661	struct thread *td = uio->uio_td;
662	struct vnode *vp = ap->a_vp;
663	struct vnode *thisvp;
664	struct denode *dep = VTODE(vp);
665	struct msdosfsmount *pmp = dep->de_pmp;
666	struct ucred *cred = ap->a_cred;
667
668#ifdef MSDOSFS_DEBUG
669	printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
670	    vp, uio, ioflag, cred);
671	printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
672	    dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
673#endif
674
675	switch (vp->v_type) {
676	case VREG:
677		if (ioflag & IO_APPEND)
678			uio->uio_offset = dep->de_FileSize;
679		thisvp = vp;
680		break;
681	case VDIR:
682		return EISDIR;
683	default:
684		panic("msdosfs_write(): bad file type");
685	}
686
687	/*
688	 * This is needed (unlike in ffs_write()) because we extend the
689	 * file outside of the loop but we don't want to extend the file
690	 * for writes of 0 bytes.
691	 */
692	if (uio->uio_resid == 0)
693		return (0);
694
695	/*
696	 * The caller is supposed to ensure that
697	 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
698	 */
699	if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
700		return (EFBIG);
701
702	/*
703	 * If they've exceeded their filesize limit, tell them about it.
704	 */
705	if (td != NULL) {
706		PROC_LOCK(td->td_proc);
707		if ((uoff_t)uio->uio_offset + uio->uio_resid >
708		    lim_cur(td->td_proc, RLIMIT_FSIZE)) {
709			psignal(td->td_proc, SIGXFSZ);
710			PROC_UNLOCK(td->td_proc);
711			return (EFBIG);
712		}
713		PROC_UNLOCK(td->td_proc);
714	}
715
716	/*
717	 * If the offset we are starting the write at is beyond the end of
718	 * the file, then they've done a seek.  Unix filesystems allow
719	 * files with holes in them, DOS doesn't so we must fill the hole
720	 * with zeroed blocks.
721	 */
722	if (uio->uio_offset > dep->de_FileSize) {
723		error = deextend(dep, uio->uio_offset, cred);
724		if (error)
725			return (error);
726	}
727
728	/*
729	 * Remember some values in case the write fails.
730	 */
731	resid = uio->uio_resid;
732	osize = dep->de_FileSize;
733
734	/*
735	 * If we write beyond the end of the file, extend it to its ultimate
736	 * size ahead of the time to hopefully get a contiguous area.
737	 */
738	if (uio->uio_offset + resid > osize) {
739		count = de_clcount(pmp, uio->uio_offset + resid) -
740			de_clcount(pmp, osize);
741		error = extendfile(dep, count, NULL, NULL, 0);
742		if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
743			goto errexit;
744		lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
745	} else
746		lastcn = de_clcount(pmp, osize) - 1;
747
748	seqcount = ioflag >> IO_SEQSHIFT;
749	do {
750		if (de_cluster(pmp, uio->uio_offset) > lastcn) {
751			error = ENOSPC;
752			break;
753		}
754
755		croffset = uio->uio_offset & pmp->pm_crbomask;
756		n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
757		if (uio->uio_offset + n > dep->de_FileSize) {
758			dep->de_FileSize = uio->uio_offset + n;
759			/* The object size needs to be set before buffer is allocated */
760			vnode_pager_setsize(vp, dep->de_FileSize);
761		}
762
763		bn = de_cluster(pmp, uio->uio_offset);
764		if ((uio->uio_offset & pmp->pm_crbomask) == 0
765		    && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
766			> de_cluster(pmp, uio->uio_offset)
767			|| uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
768			/*
769			 * If either the whole cluster gets written,
770			 * or we write the cluster from its start beyond EOF,
771			 * then no need to read data from disk.
772			 */
773			bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0, 0);
774			vfs_bio_clrbuf(bp);
775			/*
776			 * Do the bmap now, since pcbmap needs buffers
777			 * for the fat table. (see msdosfs_strategy)
778			 */
779			if (bp->b_blkno == bp->b_lblkno) {
780				error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
781				if (error)
782					bp->b_blkno = -1;
783				else
784					bp->b_blkno = bn;
785			}
786			if (bp->b_blkno == -1) {
787				brelse(bp);
788				if (!error)
789					error = EIO;		/* XXX */
790				break;
791			}
792		} else {
793			/*
794			 * The block we need to write into exists, so read it in.
795			 */
796			error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
797			if (error) {
798				brelse(bp);
799				break;
800			}
801		}
802
803		/*
804		 * Should these vnode_pager_* functions be done on dir
805		 * files?
806		 */
807
808		/*
809		 * Copy the data from user space into the buf header.
810		 */
811		error = uiomove(bp->b_data + croffset, n, uio);
812		if (error) {
813			brelse(bp);
814			break;
815		}
816
817		/* Prepare for clustered writes in some else clauses. */
818		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
819			bp->b_flags |= B_CLUSTEROK;
820
821		/*
822		 * If IO_SYNC, then each buffer is written synchronously.
823		 * Otherwise, if we have a severe page deficiency then
824		 * write the buffer asynchronously.  Otherwise, if on a
825		 * cluster boundary then write the buffer asynchronously,
826		 * combining it with contiguous clusters if permitted and
827		 * possible, since we don't expect more writes into this
828		 * buffer soon.  Otherwise, do a delayed write because we
829		 * expect more writes into this buffer soon.
830		 */
831		if (ioflag & IO_SYNC)
832			(void)bwrite(bp);
833		else if (vm_page_count_severe() || buf_dirty_count_severe())
834			bawrite(bp);
835		else if (n + croffset == pmp->pm_bpcluster) {
836			if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
837				cluster_write(vp, bp, dep->de_FileSize,
838				    seqcount);
839			else
840				bawrite(bp);
841		} else
842			bdwrite(bp);
843		dep->de_flag |= DE_UPDATE;
844	} while (error == 0 && uio->uio_resid > 0);
845
846	/*
847	 * If the write failed and they want us to, truncate the file back
848	 * to the size it was before the write was attempted.
849	 */
850errexit:
851	if (error) {
852		if (ioflag & IO_UNIT) {
853			detrunc(dep, osize, ioflag & IO_SYNC, NOCRED, NULL);
854			uio->uio_offset -= resid - uio->uio_resid;
855			uio->uio_resid = resid;
856		} else {
857			detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
858			if (uio->uio_resid != resid)
859				error = 0;
860		}
861	} else if (ioflag & IO_SYNC)
862		error = deupdat(dep, 1);
863	return (error);
864}
865
866/*
867 * Flush the blocks of a file to disk.
868 *
869 * This function is worthless for vnodes that represent directories. Maybe we
870 * could just do a sync if they try an fsync on a directory file.
871 */
872static int
873msdosfs_fsync(ap)
874	struct vop_fsync_args /* {
875		struct vnode *a_vp;
876		struct ucred *a_cred;
877		int a_waitfor;
878		struct thread *a_td;
879	} */ *ap;
880{
881
882	vop_stdfsync(ap);
883	return (deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT));
884}
885
886static int
887msdosfs_remove(ap)
888	struct vop_remove_args /* {
889		struct vnode *a_dvp;
890		struct vnode *a_vp;
891		struct componentname *a_cnp;
892	} */ *ap;
893{
894	struct denode *dep = VTODE(ap->a_vp);
895	struct denode *ddep = VTODE(ap->a_dvp);
896	int error;
897
898	if (ap->a_vp->v_type == VDIR)
899		error = EPERM;
900	else
901		error = removede(ddep, dep);
902#ifdef MSDOSFS_DEBUG
903	printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
904#endif
905	return (error);
906}
907
908/*
909 * DOS filesystems don't know what links are.
910 */
911static int
912msdosfs_link(ap)
913	struct vop_link_args /* {
914		struct vnode *a_tdvp;
915		struct vnode *a_vp;
916		struct componentname *a_cnp;
917	} */ *ap;
918{
919	return (EOPNOTSUPP);
920}
921
922/*
923 * Renames on files require moving the denode to a new hash queue since the
924 * denode's location is used to compute which hash queue to put the file
925 * in. Unless it is a rename in place.  For example "mv a b".
926 *
927 * What follows is the basic algorithm:
928 *
929 * if (file move) {
930 *	if (dest file exists) {
931 *		remove dest file
932 *	}
933 *	if (dest and src in same directory) {
934 *		rewrite name in existing directory slot
935 *	} else {
936 *		write new entry in dest directory
937 *		update offset and dirclust in denode
938 *		move denode to new hash chain
939 *		clear old directory entry
940 *	}
941 * } else {
942 *	directory move
943 *	if (dest directory exists) {
944 *		if (dest is not empty) {
945 *			return ENOTEMPTY
946 *		}
947 *		remove dest directory
948 *	}
949 *	if (dest and src in same directory) {
950 *		rewrite name in existing entry
951 *	} else {
952 *		be sure dest is not a child of src directory
953 *		write entry in dest directory
954 *		update "." and ".." in moved directory
955 *		clear old directory entry for moved directory
956 *	}
957 * }
958 *
959 * On entry:
960 *	source's parent directory is unlocked
961 *	source file or directory is unlocked
962 *	destination's parent directory is locked
963 *	destination file or directory is locked if it exists
964 *
965 * On exit:
966 *	all denodes should be released
967 */
968static int
969msdosfs_rename(ap)
970	struct vop_rename_args /* {
971		struct vnode *a_fdvp;
972		struct vnode *a_fvp;
973		struct componentname *a_fcnp;
974		struct vnode *a_tdvp;
975		struct vnode *a_tvp;
976		struct componentname *a_tcnp;
977	} */ *ap;
978{
979	struct vnode *tdvp = ap->a_tdvp;
980	struct vnode *fvp = ap->a_fvp;
981	struct vnode *fdvp = ap->a_fdvp;
982	struct vnode *tvp = ap->a_tvp;
983	struct componentname *tcnp = ap->a_tcnp;
984	struct componentname *fcnp = ap->a_fcnp;
985	struct denode *ip, *xp, *dp, *zp;
986	u_char toname[11], oldname[11];
987	u_long from_diroffset, to_diroffset;
988	u_char to_count;
989	int doingdirectory = 0, newparent = 0;
990	int error;
991	u_long cn;
992	daddr_t bn;
993	struct denode *fddep;	/* from file's parent directory	 */
994	struct msdosfsmount *pmp;
995	struct direntry *dotdotp;
996	struct buf *bp;
997
998	fddep = VTODE(ap->a_fdvp);
999	pmp = fddep->de_pmp;
1000
1001	pmp = VFSTOMSDOSFS(fdvp->v_mount);
1002
1003#ifdef DIAGNOSTIC
1004	if ((tcnp->cn_flags & HASBUF) == 0 ||
1005	    (fcnp->cn_flags & HASBUF) == 0)
1006		panic("msdosfs_rename: no name");
1007#endif
1008	/*
1009	 * Check for cross-device rename.
1010	 */
1011	if (fvp->v_mount != tdvp->v_mount ||
1012	    (tvp && fvp->v_mount != tvp->v_mount)) {
1013		error = EXDEV;
1014abortit:
1015		if (tdvp == tvp)
1016			vrele(tdvp);
1017		else
1018			vput(tdvp);
1019		if (tvp)
1020			vput(tvp);
1021		vrele(fdvp);
1022		vrele(fvp);
1023		return (error);
1024	}
1025
1026	/*
1027	 * If source and dest are the same, do nothing.
1028	 */
1029	if (tvp == fvp) {
1030		error = 0;
1031		goto abortit;
1032	}
1033
1034	error = vn_lock(fvp, LK_EXCLUSIVE);
1035	if (error)
1036		goto abortit;
1037	dp = VTODE(fdvp);
1038	ip = VTODE(fvp);
1039
1040	/*
1041	 * Be sure we are not renaming ".", "..", or an alias of ".". This
1042	 * leads to a crippled directory tree.  It's pretty tough to do a
1043	 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1044	 * doesn't work if the ".." entry is missing.
1045	 */
1046	if (ip->de_Attributes & ATTR_DIRECTORY) {
1047		/*
1048		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1049		 */
1050		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1051		    dp == ip ||
1052		    (fcnp->cn_flags & ISDOTDOT) ||
1053		    (tcnp->cn_flags & ISDOTDOT) ||
1054		    (ip->de_flag & DE_RENAME)) {
1055			VOP_UNLOCK(fvp, 0);
1056			error = EINVAL;
1057			goto abortit;
1058		}
1059		ip->de_flag |= DE_RENAME;
1060		doingdirectory++;
1061	}
1062
1063	/*
1064	 * When the target exists, both the directory
1065	 * and target vnodes are returned locked.
1066	 */
1067	dp = VTODE(tdvp);
1068	xp = tvp ? VTODE(tvp) : NULL;
1069	/*
1070	 * Remember direntry place to use for destination
1071	 */
1072	to_diroffset = dp->de_fndoffset;
1073	to_count = dp->de_fndcnt;
1074
1075	/*
1076	 * If ".." must be changed (ie the directory gets a new
1077	 * parent) then the source directory must not be in the
1078	 * directory heirarchy above the target, as this would
1079	 * orphan everything below the source directory. Also
1080	 * the user must have write permission in the source so
1081	 * as to be able to change "..". We must repeat the call
1082	 * to namei, as the parent directory is unlocked by the
1083	 * call to doscheckpath().
1084	 */
1085	error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1086	VOP_UNLOCK(fvp, 0);
1087	if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1088		newparent = 1;
1089	if (doingdirectory && newparent) {
1090		if (error)	/* write access check above */
1091			goto bad;
1092		if (xp != NULL)
1093			vput(tvp);
1094		/*
1095		 * doscheckpath() vput()'s dp,
1096		 * so we have to do a relookup afterwards
1097		 */
1098		error = doscheckpath(ip, dp);
1099		if (error)
1100			goto out;
1101		if ((tcnp->cn_flags & SAVESTART) == 0)
1102			panic("msdosfs_rename: lost to startdir");
1103		error = relookup(tdvp, &tvp, tcnp);
1104		if (error)
1105			goto out;
1106		dp = VTODE(tdvp);
1107		xp = tvp ? VTODE(tvp) : NULL;
1108	}
1109
1110	if (xp != NULL) {
1111		/*
1112		 * Target must be empty if a directory and have no links
1113		 * to it. Also, ensure source and target are compatible
1114		 * (both directories, or both not directories).
1115		 */
1116		if (xp->de_Attributes & ATTR_DIRECTORY) {
1117			if (!dosdirempty(xp)) {
1118				error = ENOTEMPTY;
1119				goto bad;
1120			}
1121			if (!doingdirectory) {
1122				error = ENOTDIR;
1123				goto bad;
1124			}
1125			cache_purge(tdvp);
1126		} else if (doingdirectory) {
1127			error = EISDIR;
1128			goto bad;
1129		}
1130		error = removede(dp, xp);
1131		if (error)
1132			goto bad;
1133		vput(tvp);
1134		xp = NULL;
1135	}
1136
1137	/*
1138	 * Convert the filename in tcnp into a dos filename. We copy this
1139	 * into the denode and directory entry for the destination
1140	 * file/directory.
1141	 */
1142	error = uniqdosname(VTODE(tdvp), tcnp, toname);
1143	if (error)
1144		goto abortit;
1145
1146	/*
1147	 * Since from wasn't locked at various places above,
1148	 * have to do a relookup here.
1149	 */
1150	fcnp->cn_flags &= ~MODMASK;
1151	fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1152	if ((fcnp->cn_flags & SAVESTART) == 0)
1153		panic("msdosfs_rename: lost from startdir");
1154	if (!newparent)
1155		VOP_UNLOCK(tdvp, 0);
1156	if (relookup(fdvp, &fvp, fcnp) == 0)
1157		vrele(fdvp);
1158	if (fvp == NULL) {
1159		/*
1160		 * From name has disappeared.
1161		 */
1162		if (doingdirectory)
1163			panic("rename: lost dir entry");
1164		if (newparent)
1165			VOP_UNLOCK(tdvp, 0);
1166		vrele(tdvp);
1167		vrele(ap->a_fvp);
1168		return 0;
1169	}
1170	xp = VTODE(fvp);
1171	zp = VTODE(fdvp);
1172	from_diroffset = zp->de_fndoffset;
1173
1174	/*
1175	 * Ensure that the directory entry still exists and has not
1176	 * changed till now. If the source is a file the entry may
1177	 * have been unlinked or renamed. In either case there is
1178	 * no further work to be done. If the source is a directory
1179	 * then it cannot have been rmdir'ed or renamed; this is
1180	 * prohibited by the DE_RENAME flag.
1181	 */
1182	if (xp != ip) {
1183		if (doingdirectory)
1184			panic("rename: lost dir entry");
1185		VOP_UNLOCK(fvp, 0);
1186		if (newparent)
1187			VOP_UNLOCK(fdvp, 0);
1188		vrele(ap->a_fvp);
1189		xp = NULL;
1190	} else {
1191		vrele(fvp);
1192		xp = NULL;
1193
1194		/*
1195		 * First write a new entry in the destination
1196		 * directory and mark the entry in the source directory
1197		 * as deleted.  Then move the denode to the correct hash
1198		 * chain for its new location in the filesystem.  And, if
1199		 * we moved a directory, then update its .. entry to point
1200		 * to the new parent directory.
1201		 */
1202		bcopy(ip->de_Name, oldname, 11);
1203		bcopy(toname, ip->de_Name, 11);	/* update denode */
1204		dp->de_fndoffset = to_diroffset;
1205		dp->de_fndcnt = to_count;
1206		error = createde(ip, dp, (struct denode **)0, tcnp);
1207		if (error) {
1208			bcopy(oldname, ip->de_Name, 11);
1209			if (newparent)
1210				VOP_UNLOCK(fdvp, 0);
1211			VOP_UNLOCK(fvp, 0);
1212			goto bad;
1213		}
1214		ip->de_refcnt++;
1215		zp->de_fndoffset = from_diroffset;
1216		error = removede(zp, ip);
1217		if (error) {
1218			/* XXX should downgrade to ro here, fs is corrupt */
1219			if (newparent)
1220				VOP_UNLOCK(fdvp, 0);
1221			VOP_UNLOCK(fvp, 0);
1222			goto bad;
1223		}
1224		if (!doingdirectory) {
1225			error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1226				       &ip->de_dirclust, 0);
1227			if (error) {
1228				/* XXX should downgrade to ro here, fs is corrupt */
1229				if (newparent)
1230					VOP_UNLOCK(fdvp, 0);
1231				VOP_UNLOCK(fvp, 0);
1232				goto bad;
1233			}
1234			if (ip->de_dirclust == MSDOSFSROOT)
1235				ip->de_diroffset = to_diroffset;
1236			else
1237				ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1238		}
1239		reinsert(ip);
1240		if (newparent)
1241			VOP_UNLOCK(fdvp, 0);
1242	}
1243
1244	/*
1245	 * If we moved a directory to a new parent directory, then we must
1246	 * fixup the ".." entry in the moved directory.
1247	 */
1248	if (doingdirectory && newparent) {
1249		cn = ip->de_StartCluster;
1250		if (cn == MSDOSFSROOT) {
1251			/* this should never happen */
1252			panic("msdosfs_rename(): updating .. in root directory?");
1253		} else
1254			bn = cntobn(pmp, cn);
1255		error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1256			      NOCRED, &bp);
1257		if (error) {
1258			/* XXX should downgrade to ro here, fs is corrupt */
1259			brelse(bp);
1260			VOP_UNLOCK(fvp, 0);
1261			goto bad;
1262		}
1263		dotdotp = (struct direntry *)bp->b_data + 1;
1264		putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1265		if (FAT32(pmp))
1266			putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1267		if (fvp->v_mount->mnt_flag & MNT_ASYNC)
1268			bdwrite(bp);
1269		else if ((error = bwrite(bp)) != 0) {
1270			/* XXX should downgrade to ro here, fs is corrupt */
1271			VOP_UNLOCK(fvp, 0);
1272			goto bad;
1273		}
1274	}
1275
1276	VOP_UNLOCK(fvp, 0);
1277bad:
1278	if (xp)
1279		vput(tvp);
1280	vput(tdvp);
1281out:
1282	ip->de_flag &= ~DE_RENAME;
1283	vrele(fdvp);
1284	vrele(fvp);
1285	return (error);
1286
1287}
1288
1289static struct {
1290	struct direntry dot;
1291	struct direntry dotdot;
1292} dosdirtemplate = {
1293	{	".       ", "   ",			/* the . entry */
1294		ATTR_DIRECTORY,				/* file attribute */
1295		0,					/* reserved */
1296		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1297		{ 0, 0 },				/* access date */
1298		{ 0, 0 },				/* high bits of start cluster */
1299		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1300		{ 0, 0 },				/* startcluster */
1301		{ 0, 0, 0, 0 }				/* filesize */
1302	},
1303	{	"..      ", "   ",			/* the .. entry */
1304		ATTR_DIRECTORY,				/* file attribute */
1305		0,					/* reserved */
1306		0, { 0, 0 }, { 0, 0 },			/* create time & date */
1307		{ 0, 0 },				/* access date */
1308		{ 0, 0 },				/* high bits of start cluster */
1309		{ 210, 4 }, { 210, 4 },			/* modify time & date */
1310		{ 0, 0 },				/* startcluster */
1311		{ 0, 0, 0, 0 }				/* filesize */
1312	}
1313};
1314
1315static int
1316msdosfs_mkdir(ap)
1317	struct vop_mkdir_args /* {
1318		struct vnode *a_dvp;
1319		struct vnode **a_vpp;
1320		struvt componentname *a_cnp;
1321		struct vattr *a_vap;
1322	} */ *ap;
1323{
1324	struct componentname *cnp = ap->a_cnp;
1325	struct denode *dep;
1326	struct denode *pdep = VTODE(ap->a_dvp);
1327	struct direntry *denp;
1328	struct msdosfsmount *pmp = pdep->de_pmp;
1329	struct buf *bp;
1330	u_long newcluster, pcl;
1331	int bn;
1332	int error;
1333	struct denode ndirent;
1334	struct timespec ts;
1335
1336	/*
1337	 * If this is the root directory and there is no space left we
1338	 * can't do anything.  This is because the root directory can not
1339	 * change size.
1340	 */
1341	if (pdep->de_StartCluster == MSDOSFSROOT
1342	    && pdep->de_fndoffset >= pdep->de_FileSize) {
1343		error = ENOSPC;
1344		goto bad2;
1345	}
1346
1347	/*
1348	 * Allocate a cluster to hold the about to be created directory.
1349	 */
1350	error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1351	if (error)
1352		goto bad2;
1353
1354	bzero(&ndirent, sizeof(ndirent));
1355	ndirent.de_pmp = pmp;
1356	ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1357	getnanotime(&ts);
1358	DETIMES(&ndirent, &ts, &ts, &ts);
1359
1360	/*
1361	 * Now fill the cluster with the "." and ".." entries. And write
1362	 * the cluster to disk.  This way it is there for the parent
1363	 * directory to be pointing at if there were a crash.
1364	 */
1365	bn = cntobn(pmp, newcluster);
1366	/* always succeeds */
1367	bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
1368	bzero(bp->b_data, pmp->pm_bpcluster);
1369	bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1370	denp = (struct direntry *)bp->b_data;
1371	putushort(denp[0].deStartCluster, newcluster);
1372	putushort(denp[0].deCDate, ndirent.de_CDate);
1373	putushort(denp[0].deCTime, ndirent.de_CTime);
1374	denp[0].deCHundredth = ndirent.de_CHun;
1375	putushort(denp[0].deADate, ndirent.de_ADate);
1376	putushort(denp[0].deMDate, ndirent.de_MDate);
1377	putushort(denp[0].deMTime, ndirent.de_MTime);
1378	pcl = pdep->de_StartCluster;
1379	if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1380		pcl = 0;
1381	putushort(denp[1].deStartCluster, pcl);
1382	putushort(denp[1].deCDate, ndirent.de_CDate);
1383	putushort(denp[1].deCTime, ndirent.de_CTime);
1384	denp[1].deCHundredth = ndirent.de_CHun;
1385	putushort(denp[1].deADate, ndirent.de_ADate);
1386	putushort(denp[1].deMDate, ndirent.de_MDate);
1387	putushort(denp[1].deMTime, ndirent.de_MTime);
1388	if (FAT32(pmp)) {
1389		putushort(denp[0].deHighClust, newcluster >> 16);
1390		putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1391	}
1392
1393	if (ap->a_dvp->v_mount->mnt_flag & MNT_ASYNC)
1394		bdwrite(bp);
1395	else if ((error = bwrite(bp)) != 0)
1396		goto bad;
1397
1398	/*
1399	 * Now build up a directory entry pointing to the newly allocated
1400	 * cluster.  This will be written to an empty slot in the parent
1401	 * directory.
1402	 */
1403#ifdef DIAGNOSTIC
1404	if ((cnp->cn_flags & HASBUF) == 0)
1405		panic("msdosfs_mkdir: no name");
1406#endif
1407	error = uniqdosname(pdep, cnp, ndirent.de_Name);
1408	if (error)
1409		goto bad;
1410
1411	ndirent.de_Attributes = ATTR_DIRECTORY;
1412	ndirent.de_LowerCase = 0;
1413	ndirent.de_StartCluster = newcluster;
1414	ndirent.de_FileSize = 0;
1415	ndirent.de_dev = pdep->de_dev;
1416	error = createde(&ndirent, pdep, &dep, cnp);
1417	if (error)
1418		goto bad;
1419	*ap->a_vpp = DETOV(dep);
1420	return (0);
1421
1422bad:
1423	clusterfree(pmp, newcluster, NULL);
1424bad2:
1425	return (error);
1426}
1427
1428static int
1429msdosfs_rmdir(ap)
1430	struct vop_rmdir_args /* {
1431		struct vnode *a_dvp;
1432		struct vnode *a_vp;
1433		struct componentname *a_cnp;
1434	} */ *ap;
1435{
1436	struct vnode *vp = ap->a_vp;
1437	struct vnode *dvp = ap->a_dvp;
1438	struct componentname *cnp = ap->a_cnp;
1439	struct denode *ip, *dp;
1440	struct thread *td = cnp->cn_thread;
1441	int error;
1442
1443	ip = VTODE(vp);
1444	dp = VTODE(dvp);
1445
1446	/*
1447	 * Verify the directory is empty (and valid).
1448	 * (Rmdir ".." won't be valid since
1449	 *  ".." will contain a reference to
1450	 *  the current directory and thus be
1451	 *  non-empty.)
1452	 */
1453	error = 0;
1454	if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1455		error = ENOTEMPTY;
1456		goto out;
1457	}
1458	/*
1459	 * Delete the entry from the directory.  For dos filesystems this
1460	 * gets rid of the directory entry on disk, the in memory copy
1461	 * still exists but the de_refcnt is <= 0.  This prevents it from
1462	 * being found by deget().  When the vput() on dep is done we give
1463	 * up access and eventually msdosfs_reclaim() will be called which
1464	 * will remove it from the denode cache.
1465	 */
1466	error = removede(dp, ip);
1467	if (error)
1468		goto out;
1469	/*
1470	 * This is where we decrement the link count in the parent
1471	 * directory.  Since dos filesystems don't do this we just purge
1472	 * the name cache.
1473	 */
1474	cache_purge(dvp);
1475	VOP_UNLOCK(dvp, 0);
1476	/*
1477	 * Truncate the directory that is being deleted.
1478	 */
1479	error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, td);
1480	cache_purge(vp);
1481
1482	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1483out:
1484	return (error);
1485}
1486
1487/*
1488 * DOS filesystems don't know what symlinks are.
1489 */
1490static int
1491msdosfs_symlink(ap)
1492	struct vop_symlink_args /* {
1493		struct vnode *a_dvp;
1494		struct vnode **a_vpp;
1495		struct componentname *a_cnp;
1496		struct vattr *a_vap;
1497		char *a_target;
1498	} */ *ap;
1499{
1500	return (EOPNOTSUPP);
1501}
1502
1503static int
1504msdosfs_readdir(ap)
1505	struct vop_readdir_args /* {
1506		struct vnode *a_vp;
1507		struct uio *a_uio;
1508		struct ucred *a_cred;
1509		int *a_eofflag;
1510		int *a_ncookies;
1511		u_long **a_cookies;
1512	} */ *ap;
1513{
1514	struct mbnambuf nb;
1515	int error = 0;
1516	int diff;
1517	long n;
1518	int blsize;
1519	long on;
1520	u_long cn;
1521	uint64_t fileno;
1522	u_long dirsperblk;
1523	long bias = 0;
1524	daddr_t bn, lbn;
1525	struct buf *bp;
1526	struct denode *dep = VTODE(ap->a_vp);
1527	struct msdosfsmount *pmp = dep->de_pmp;
1528	struct direntry *dentp;
1529	struct dirent dirbuf;
1530	struct uio *uio = ap->a_uio;
1531	u_long *cookies = NULL;
1532	int ncookies = 0;
1533	off_t offset, off;
1534	int chksum = -1;
1535
1536#ifdef MSDOSFS_DEBUG
1537	printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1538	    ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1539#endif
1540
1541	/*
1542	 * msdosfs_readdir() won't operate properly on regular files since
1543	 * it does i/o only with the the filesystem vnode, and hence can
1544	 * retrieve the wrong block from the buffer cache for a plain file.
1545	 * So, fail attempts to readdir() on a plain file.
1546	 */
1547	if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1548		return (ENOTDIR);
1549
1550	/*
1551	 * To be safe, initialize dirbuf
1552	 */
1553	bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1554
1555	/*
1556	 * If the user buffer is smaller than the size of one dos directory
1557	 * entry or the file offset is not a multiple of the size of a
1558	 * directory entry, then we fail the read.
1559	 */
1560	off = offset = uio->uio_offset;
1561	if (uio->uio_resid < sizeof(struct direntry) ||
1562	    (offset & (sizeof(struct direntry) - 1)))
1563		return (EINVAL);
1564
1565	if (ap->a_ncookies) {
1566		ncookies = uio->uio_resid / 16;
1567		MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1568		       M_WAITOK);
1569		*ap->a_cookies = cookies;
1570		*ap->a_ncookies = ncookies;
1571	}
1572
1573	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1574
1575	/*
1576	 * If they are reading from the root directory then, we simulate
1577	 * the . and .. entries since these don't exist in the root
1578	 * directory.  We also set the offset bias to make up for having to
1579	 * simulate these entries. By this I mean that at file offset 64 we
1580	 * read the first entry in the root directory that lives on disk.
1581	 */
1582	if (dep->de_StartCluster == MSDOSFSROOT
1583	    || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1584#if 0
1585		printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1586		    offset);
1587#endif
1588		bias = 2 * sizeof(struct direntry);
1589		if (offset < bias) {
1590			for (n = (int)offset / sizeof(struct direntry);
1591			     n < 2; n++) {
1592				if (FAT32(pmp))
1593					fileno = (uint64_t)cntobn(pmp,
1594								 pmp->pm_rootdirblk)
1595							  * dirsperblk;
1596				else
1597					fileno = 1;
1598				if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1599					dirbuf.d_fileno =
1600					    msdosfs_fileno_map(pmp->pm_mountp,
1601					    fileno);
1602				} else {
1603
1604					dirbuf.d_fileno = (uint32_t)fileno;
1605				}
1606				dirbuf.d_type = DT_DIR;
1607				switch (n) {
1608				case 0:
1609					dirbuf.d_namlen = 1;
1610					strcpy(dirbuf.d_name, ".");
1611					break;
1612				case 1:
1613					dirbuf.d_namlen = 2;
1614					strcpy(dirbuf.d_name, "..");
1615					break;
1616				}
1617				dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1618				if (uio->uio_resid < dirbuf.d_reclen)
1619					goto out;
1620				error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1621				if (error)
1622					goto out;
1623				offset += sizeof(struct direntry);
1624				off = offset;
1625				if (cookies) {
1626					*cookies++ = offset;
1627					if (--ncookies <= 0)
1628						goto out;
1629				}
1630			}
1631		}
1632	}
1633
1634	mbnambuf_init(&nb);
1635	off = offset;
1636	while (uio->uio_resid > 0) {
1637		lbn = de_cluster(pmp, offset - bias);
1638		on = (offset - bias) & pmp->pm_crbomask;
1639		n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1640		diff = dep->de_FileSize - (offset - bias);
1641		if (diff <= 0)
1642			break;
1643		n = min(n, diff);
1644		error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1645		if (error)
1646			break;
1647		error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1648		if (error) {
1649			brelse(bp);
1650			return (error);
1651		}
1652		n = min(n, blsize - bp->b_resid);
1653		if (n == 0) {
1654			brelse(bp);
1655			return (EIO);
1656		}
1657
1658		/*
1659		 * Convert from dos directory entries to fs-independent
1660		 * directory entries.
1661		 */
1662		for (dentp = (struct direntry *)(bp->b_data + on);
1663		     (char *)dentp < bp->b_data + on + n;
1664		     dentp++, offset += sizeof(struct direntry)) {
1665#if 0
1666			printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1667			    dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1668#endif
1669			/*
1670			 * If this is an unused entry, we can stop.
1671			 */
1672			if (dentp->deName[0] == SLOT_EMPTY) {
1673				brelse(bp);
1674				goto out;
1675			}
1676			/*
1677			 * Skip deleted entries.
1678			 */
1679			if (dentp->deName[0] == SLOT_DELETED) {
1680				chksum = -1;
1681				mbnambuf_init(&nb);
1682				continue;
1683			}
1684
1685			/*
1686			 * Handle Win95 long directory entries
1687			 */
1688			if (dentp->deAttributes == ATTR_WIN95) {
1689				if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1690					continue;
1691				chksum = win2unixfn(&nb,
1692				    (struct winentry *)dentp, chksum, pmp);
1693				continue;
1694			}
1695
1696			/*
1697			 * Skip volume labels
1698			 */
1699			if (dentp->deAttributes & ATTR_VOLUME) {
1700				chksum = -1;
1701				mbnambuf_init(&nb);
1702				continue;
1703			}
1704			/*
1705			 * This computation of d_fileno must match
1706			 * the computation of va_fileid in
1707			 * msdosfs_getattr.
1708			 */
1709			if (dentp->deAttributes & ATTR_DIRECTORY) {
1710				fileno = getushort(dentp->deStartCluster);
1711				if (FAT32(pmp))
1712					fileno |= getushort(dentp->deHighClust) << 16;
1713				/* if this is the root directory */
1714				if (fileno == MSDOSFSROOT)
1715					if (FAT32(pmp))
1716						fileno = (uint64_t)cntobn(pmp,
1717								pmp->pm_rootdirblk)
1718							 * dirsperblk;
1719					else
1720						fileno = 1;
1721				else
1722					fileno = (uint64_t)cntobn(pmp, fileno) *
1723					    dirsperblk;
1724				dirbuf.d_type = DT_DIR;
1725			} else {
1726				fileno = (uoff_t)offset /
1727				    sizeof(struct direntry);
1728				dirbuf.d_type = DT_REG;
1729			}
1730			if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1731				dirbuf.d_fileno =
1732				    msdosfs_fileno_map(pmp->pm_mountp, fileno);
1733			} else
1734				dirbuf.d_fileno = (uint32_t)fileno;
1735
1736			if (chksum != winChksum(dentp)) {
1737				dirbuf.d_namlen = dos2unixfn(dentp->deName,
1738				    (u_char *)dirbuf.d_name,
1739				    dentp->deLowerCase |
1740					((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1741					(LCASE_BASE | LCASE_EXT) : 0),
1742				    pmp);
1743				mbnambuf_init(&nb);
1744			} else
1745				mbnambuf_flush(&nb, &dirbuf);
1746			chksum = -1;
1747			dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1748			if (uio->uio_resid < dirbuf.d_reclen) {
1749				brelse(bp);
1750				goto out;
1751			}
1752			error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1753			if (error) {
1754				brelse(bp);
1755				goto out;
1756			}
1757			if (cookies) {
1758				*cookies++ = offset + sizeof(struct direntry);
1759				if (--ncookies <= 0) {
1760					brelse(bp);
1761					goto out;
1762				}
1763			}
1764			off = offset + sizeof(struct direntry);
1765		}
1766		brelse(bp);
1767	}
1768out:
1769	/* Subtract unused cookies */
1770	if (ap->a_ncookies)
1771		*ap->a_ncookies -= ncookies;
1772
1773	uio->uio_offset = off;
1774
1775	/*
1776	 * Set the eofflag (NFS uses it)
1777	 */
1778	if (ap->a_eofflag) {
1779		if (dep->de_FileSize - (offset - bias) <= 0)
1780			*ap->a_eofflag = 1;
1781		else
1782			*ap->a_eofflag = 0;
1783	}
1784	return (error);
1785}
1786
1787/*-
1788 * a_vp   - pointer to the file's vnode
1789 * a_bn   - logical block number within the file (cluster number for us)
1790 * a_bop  - where to return the bufobj of the special file containing the fs
1791 * a_bnp  - where to return the "physical" block number corresponding to a_bn
1792 *          (relative to the special file; units are blocks of size DEV_BSIZE)
1793 * a_runp - where to return the "run past" a_bn.  This is the count of logical
1794 *          blocks whose physical blocks (together with a_bn's physical block)
1795 *          are contiguous.
1796 * a_runb - where to return the "run before" a_bn.
1797 */
1798static int
1799msdosfs_bmap(ap)
1800	struct vop_bmap_args /* {
1801		struct vnode *a_vp;
1802		daddr_t a_bn;
1803		struct bufobj **a_bop;
1804		daddr_t *a_bnp;
1805		int *a_runp;
1806		int *a_runb;
1807	} */ *ap;
1808{
1809	struct denode *dep;
1810	struct mount *mp;
1811	struct msdosfsmount *pmp;
1812	struct vnode *vp;
1813	daddr_t runbn;
1814	u_long cn;
1815	int bnpercn, error, maxio, maxrun, run;
1816
1817	vp = ap->a_vp;
1818	dep = VTODE(vp);
1819	pmp = dep->de_pmp;
1820	if (ap->a_bop != NULL)
1821		*ap->a_bop = &pmp->pm_devvp->v_bufobj;
1822	if (ap->a_bnp == NULL)
1823		return (0);
1824	if (ap->a_runp != NULL)
1825		*ap->a_runp = 0;
1826	if (ap->a_runb != NULL)
1827		*ap->a_runb = 0;
1828	cn = ap->a_bn;
1829	if (cn != ap->a_bn)
1830		return (EFBIG);
1831	error = pcbmap(dep, cn, ap->a_bnp, NULL, NULL);
1832	if (error != 0 || (ap->a_runp == NULL && ap->a_runb == NULL))
1833		return (error);
1834
1835	mp = vp->v_mount;
1836	maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1837	bnpercn = de_cn2bn(pmp, 1);
1838	if (ap->a_runp != NULL) {
1839		maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1840		for (run = 1; run <= maxrun; run++) {
1841			if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1842			    runbn != *ap->a_bnp + run * bnpercn)
1843				break;
1844		}
1845		*ap->a_runp = run - 1;
1846	}
1847	if (ap->a_runb != NULL) {
1848		maxrun = ulmin(maxio - 1, cn);
1849		for (run = 1; run < maxrun; run++) {
1850			if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1851			    runbn != *ap->a_bnp - run * bnpercn)
1852				break;
1853		}
1854		*ap->a_runb = run - 1;
1855	}
1856	return (0);
1857}
1858
1859static int
1860msdosfs_strategy(ap)
1861	struct vop_strategy_args /* {
1862		struct vnode *a_vp;
1863		struct buf *a_bp;
1864	} */ *ap;
1865{
1866	struct buf *bp = ap->a_bp;
1867	struct denode *dep = VTODE(ap->a_vp);
1868	struct bufobj *bo;
1869	int error = 0;
1870	daddr_t blkno;
1871
1872	/*
1873	 * If we don't already know the filesystem relative block number
1874	 * then get it using pcbmap().  If pcbmap() returns the block
1875	 * number as -1 then we've got a hole in the file.  DOS filesystems
1876	 * don't allow files with holes, so we shouldn't ever see this.
1877	 */
1878	if (bp->b_blkno == bp->b_lblkno) {
1879		error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1880		bp->b_blkno = blkno;
1881		if (error) {
1882			bp->b_error = error;
1883			bp->b_ioflags |= BIO_ERROR;
1884			bufdone(bp);
1885			return (error);
1886		}
1887		if ((long)bp->b_blkno == -1)
1888			vfs_bio_clrbuf(bp);
1889	}
1890	if (bp->b_blkno == -1) {
1891		bufdone(bp);
1892		return (0);
1893	}
1894	/*
1895	 * Read/write the block from/to the disk that contains the desired
1896	 * file block.
1897	 */
1898	bp->b_iooffset = dbtob(bp->b_blkno);
1899	bo = dep->de_pmp->pm_bo;
1900	BO_STRATEGY(bo, bp);
1901	return (0);
1902}
1903
1904static int
1905msdosfs_print(ap)
1906	struct vop_print_args /* {
1907		struct vnode *vp;
1908	} */ *ap;
1909{
1910	struct denode *dep = VTODE(ap->a_vp);
1911
1912	printf("\tstartcluster %lu, dircluster %lu, diroffset %lu, ",
1913	       dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1914	printf("on dev %s\n", devtoname(dep->de_dev));
1915	return (0);
1916}
1917
1918static int
1919msdosfs_pathconf(ap)
1920	struct vop_pathconf_args /* {
1921		struct vnode *a_vp;
1922		int a_name;
1923		int *a_retval;
1924	} */ *ap;
1925{
1926	struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1927
1928	switch (ap->a_name) {
1929	case _PC_LINK_MAX:
1930		*ap->a_retval = 1;
1931		return (0);
1932	case _PC_NAME_MAX:
1933		*ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1934		return (0);
1935	case _PC_PATH_MAX:
1936		*ap->a_retval = PATH_MAX;
1937		return (0);
1938	case _PC_CHOWN_RESTRICTED:
1939		*ap->a_retval = 1;
1940		return (0);
1941	case _PC_NO_TRUNC:
1942		*ap->a_retval = 0;
1943		return (0);
1944	default:
1945		return (EINVAL);
1946	}
1947	/* NOTREACHED */
1948}
1949
1950static int
1951msdosfs_advlock(ap)
1952	struct vop_advlock_args /* {
1953		struct vnode *a_vp;
1954		u_char a_id;
1955		int a_op;
1956		struct flock *a_fl;
1957		int a_flags;
1958	} */ *ap;
1959{
1960	struct denode *dep = VTODE(ap->a_vp);
1961
1962	return (lf_advlock(ap, &dep->de_lockf, dep->de_FileSize));
1963}
1964
1965static int
1966msdosfs_vptofh(ap)
1967	struct vop_vptofh_args /* {
1968		struct vnode *a_vp;
1969		struct fid *a_fhp;
1970	} */ *ap;
1971{
1972	struct denode *dep;
1973	struct defid *defhp;
1974
1975	dep = VTODE(ap->a_vp);
1976	defhp = (struct defid *)ap->a_fhp;
1977	defhp->defid_len = sizeof(struct defid);
1978	defhp->defid_dirclust = dep->de_dirclust;
1979	defhp->defid_dirofs = dep->de_diroffset;
1980	/* defhp->defid_gen = dep->de_gen; */
1981	return (0);
1982}
1983
1984/* Global vfs data structures for msdosfs */
1985struct vop_vector msdosfs_vnodeops = {
1986	.vop_default =		&default_vnodeops,
1987
1988	.vop_access =		msdosfs_access,
1989	.vop_advlock =		msdosfs_advlock,
1990	.vop_bmap =		msdosfs_bmap,
1991	.vop_cachedlookup =	msdosfs_lookup,
1992	.vop_open =		msdosfs_open,
1993	.vop_close =		msdosfs_close,
1994	.vop_create =		msdosfs_create,
1995	.vop_fsync =		msdosfs_fsync,
1996	.vop_getattr =		msdosfs_getattr,
1997	.vop_inactive =		msdosfs_inactive,
1998	.vop_link =		msdosfs_link,
1999	.vop_lookup =		vfs_cache_lookup,
2000	.vop_mkdir =		msdosfs_mkdir,
2001	.vop_mknod =		msdosfs_mknod,
2002	.vop_pathconf =		msdosfs_pathconf,
2003	.vop_print =		msdosfs_print,
2004	.vop_read =		msdosfs_read,
2005	.vop_readdir =		msdosfs_readdir,
2006	.vop_reclaim =		msdosfs_reclaim,
2007	.vop_remove =		msdosfs_remove,
2008	.vop_rename =		msdosfs_rename,
2009	.vop_rmdir =		msdosfs_rmdir,
2010	.vop_setattr =		msdosfs_setattr,
2011	.vop_strategy =		msdosfs_strategy,
2012	.vop_symlink =		msdosfs_symlink,
2013	.vop_write =		msdosfs_write,
2014	.vop_vptofh =		msdosfs_vptofh,
2015};
2016