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