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