xref: /netbsd-src/sys/fs/msdosfs/msdosfs_lookup.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: msdosfs_lookup.c,v 1.36 2020/04/04 20:49:30 ad Exp $	*/
2 
3 /*-
4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6  * All rights reserved.
7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Written by Paul Popelka (paulp@uts.amdahl.com)
36  *
37  * You can do anything you want with this software, just don't say you wrote
38  * it, and don't remove this notice.
39  *
40  * This software is provided "as is".
41  *
42  * The author supplies this software to be publicly redistributed on the
43  * understanding that the author is not responsible for the correct
44  * functioning of this software in any circumstances and is not liable for
45  * any damages caused by this software.
46  *
47  * October 1992
48  */
49 
50 #if HAVE_NBTOOL_CONFIG_H
51 #include "nbtool_config.h"
52 #endif
53 
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: msdosfs_lookup.c,v 1.36 2020/04/04 20:49:30 ad Exp $");
56 
57 #include <sys/param.h>
58 
59 #ifdef _KERNEL
60 #include <sys/systm.h>
61 #include <sys/mount.h>
62 #include <sys/kauth.h>
63 #include <sys/namei.h>
64 #include <sys/dirent.h>
65 #include <sys/buf.h>
66 #include <sys/vnode.h>
67 #include <sys/atomic.h>
68 #else
69 #include <ffs/buf.h>
70 #endif /* _KERNEL */
71 
72 #include <fs/msdosfs/bpb.h>
73 #include <fs/msdosfs/direntry.h>
74 #include <fs/msdosfs/denode.h>
75 #include <fs/msdosfs/msdosfsmount.h>
76 #include <fs/msdosfs/fat.h>
77 
78 
79 #ifdef _KERNEL
80 /*
81  * When we search a directory the blocks containing directory entries are
82  * read and examined.  The directory entries contain information that would
83  * normally be in the inode of a unix filesystem.  This means that some of
84  * a directory's contents may also be in memory resident denodes (sort of
85  * an inode).  This can cause problems if we are searching while some other
86  * process is modifying a directory.  To prevent one process from accessing
87  * incompletely modified directory information we depend upon being the
88  * sole owner of a directory block.  bread/brelse provide this service.
89  * This being the case, when a process modifies a directory it must first
90  * acquire the disk block that contains the directory entry to be modified.
91  * Then update the disk block and the denode, and then write the disk block
92  * out to disk.  This way disk blocks containing directory entries and in
93  * memory denode's will be in synch.
94  */
95 int
96 msdosfs_lookup(void *v)
97 {
98 	struct vop_lookup_v2_args /* {
99 		struct vnode *a_dvp;
100 		struct vnode **a_vpp;
101 		struct componentname *a_cnp;
102 	} */ *ap = v;
103 	struct vnode *vdp = ap->a_dvp;
104 	struct vnode **vpp = ap->a_vpp;
105 	struct componentname *cnp = ap->a_cnp;
106 	daddr_t bn;
107 	int error;
108 	int slotcount;
109 	int slotoffset = 0;
110 	int frcn;
111 	u_long cluster;
112 	int blkoff;
113 	int diroff;
114 	int blsize;
115 	int isadir;		/* ~0 if found direntry is a directory	 */
116 	u_long scn;		/* starting cluster number		 */
117 	struct denode *dp;
118 	struct msdosfsmount *pmp;
119 	struct buf *bp = 0;
120 	struct direntry *dep;
121 	u_char dosfilename[12];
122 	int flags;
123 	int nameiop = cnp->cn_nameiop;
124 	int wincnt = 1;
125 	int chksum = -1, chksum_ok;
126 	int olddos = 1;
127 
128 	flags = cnp->cn_flags;
129 
130 #ifdef MSDOSFS_DEBUG
131 	printf("msdosfs_lookup(): looking for %.*s\n",
132 		(int)cnp->cn_namelen, cnp->cn_nameptr);
133 #endif
134 	dp = VTODE(vdp);
135 	pmp = dp->de_pmp;
136 	*vpp = NULL;
137 #ifdef MSDOSFS_DEBUG
138 	printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
139 	    vdp, dp, dp->de_Attributes);
140 #endif
141 
142 	/*
143 	 * Check accessiblity of directory.
144 	 */
145 	if ((error = VOP_ACCESS(vdp, VEXEC, cnp->cn_cred)) != 0)
146 		return (error);
147 
148 	if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
149 	    (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
150 		return (EROFS);
151 
152 	/*
153 	 * We now have a segment name to search for, and a directory to search.
154 	 *
155 	 * Before tediously performing a linear scan of the directory,
156 	 * check the name cache to see if the directory/name pair
157 	 * we are looking for is known already.
158 	 */
159 	if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
160 			 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
161 		return *vpp == NULLVP ? ENOENT: 0;
162 	}
163 
164 	/* May need to restart the lookup with an exclusive lock. */
165 	if (VOP_ISLOCKED(vdp) != LK_EXCLUSIVE)
166 		return ENOLCK;
167 
168 	/*
169 	 * If they are going after the . or .. entry in the root directory,
170 	 * they won't find it.  DOS filesystems don't have them in the root
171 	 * directory.  So, we fake it. deget() is in on this scam too.
172 	 */
173 	if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
174 	    (cnp->cn_namelen == 1 ||
175 		(cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
176 		isadir = ATTR_DIRECTORY;
177 		scn = MSDOSFSROOT;
178 #ifdef MSDOSFS_DEBUG
179 		printf("msdosfs_lookup(): looking for . or .. in root directory\n");
180 #endif
181 		cluster = MSDOSFSROOT;
182 		blkoff = MSDOSFSROOT_OFS;
183 		goto foundroot;
184 	}
185 
186 	switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
187 	    cnp->cn_namelen, 0)) {
188 	case 0:
189 		return (EINVAL);
190 	case 1:
191 		break;
192 	case 2:
193 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
194 		    cnp->cn_namelen, pmp->pm_flags & MSDOSFSMNT_UTF8) + 1;
195 		break;
196 	case 3:
197 		olddos = 0;
198 		wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
199 		    cnp->cn_namelen, pmp->pm_flags & MSDOSFSMNT_UTF8) + 1;
200 		break;
201 	}
202 	if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
203 		wincnt = 1;
204 
205 	/*
206 	 * Suppress search for slots unless creating
207 	 * file and at end of pathname, in which case
208 	 * we watch for a place to put the new file in
209 	 * case it doesn't already exist.
210 	 */
211 	slotcount = wincnt;
212 	if ((nameiop == CREATE || nameiop == RENAME) &&
213 	    (flags & ISLASTCN))
214 		slotcount = 0;
215 
216 #ifdef MSDOSFS_DEBUG
217 	printf("msdosfs_lookup(): dos filename: %s\n", dosfilename);
218 #endif
219 	/*
220 	 * Search the directory pointed at by vdp for the name pointed at
221 	 * by cnp->cn_nameptr.
222 	 */
223 
224 	/*
225 	 * The outer loop ranges over the clusters that make up the
226 	 * directory.  Note that the root directory is different from all
227 	 * other directories.  It has a fixed number of blocks that are not
228 	 * part of the pool of allocatable clusters.  So, we treat it a
229 	 * little differently. The root directory starts at "cluster" 0.
230 	 */
231 	diroff = 0;
232 	for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
233 		if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
234 			if (error == E2BIG)
235 				break;
236 			return (error);
237 		}
238 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
239 		    0, &bp);
240 		if (error) {
241 			return (error);
242 		}
243 		for (blkoff = 0; blkoff < blsize;
244 		     blkoff += sizeof(struct direntry),
245 		     diroff += sizeof(struct direntry)) {
246 			dep = (struct direntry *)((char *)bp->b_data + blkoff);
247 			/*
248 			 * If the slot is empty and we are still looking
249 			 * for an empty then remember this one.  If the
250 			 * slot is not empty then check to see if it
251 			 * matches what we are looking for.  If the slot
252 			 * has never been filled with anything, then the
253 			 * remainder of the directory has never been used,
254 			 * so there is no point in searching it.
255 			 */
256 			if (dep->deName[0] == SLOT_EMPTY ||
257 			    dep->deName[0] == SLOT_DELETED) {
258 				/*
259 				 * Drop memory of previous long matches
260 				 */
261 				chksum = -1;
262 
263 				if (slotcount < wincnt) {
264 					slotcount++;
265 					slotoffset = diroff;
266 				}
267 				if (dep->deName[0] == SLOT_EMPTY) {
268 					brelse(bp, 0);
269 					goto notfound;
270 				}
271 			} else {
272 				/*
273 				 * If there wasn't enough space for our
274 				 * winentries, forget about the empty space
275 				 */
276 				if (slotcount < wincnt)
277 					slotcount = 0;
278 
279 				/*
280 				 * Check for Win95 long filename entry
281 				 */
282 				if (dep->deAttributes == ATTR_WIN95) {
283 					if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
284 						continue;
285 
286 					chksum = winChkName((const u_char *)cnp->cn_nameptr,
287 							    cnp->cn_namelen,
288 							    (struct winentry *)dep,
289 							    chksum,
290 							    pmp->pm_flags & MSDOSFSMNT_UTF8);
291 					continue;
292 				}
293 
294 				/*
295 				 * Ignore volume labels (anywhere, not just
296 				 * the root directory).
297 				 */
298 				if (dep->deAttributes & ATTR_VOLUME) {
299 					chksum = -1;
300 					continue;
301 				}
302 
303 				/*
304 				 * Check for a checksum or name match
305 				 */
306 				chksum_ok = (chksum == winChksum(dep->deName));
307 				if (!chksum_ok && (
308 					!olddos ||
309 					memcmp(&dosfilename[0],dep->deName,8) ||
310 					memcmp(&dosfilename[8],dep->deExtension,3))) {
311 					chksum = -1;
312 					continue;
313 				}
314 #ifdef MSDOSFS_DEBUG
315 				printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
316 				    blkoff, diroff);
317 #endif
318 				/*
319 				 * Remember where this directory
320 				 * entry came from for whoever did
321 				 * this lookup.
322 				 */
323 				dp->de_fndoffset = diroff;
324 				if (chksum_ok && nameiop == RENAME) {
325 					/*
326 					 * Target had correct long name
327 					 * directory entries, reuse them
328 					 * as needed.
329 					 */
330 					dp->de_fndcnt = wincnt - 1;
331 				} else {
332 					/*
333 					 * Long name directory entries
334 					 * not present or corrupt, can only
335 					 * reuse dos directory entry.
336 					 */
337 					dp->de_fndcnt = 0;
338 				}
339 
340 				goto found;
341 			}
342 		}	/* for (blkoff = 0; .... */
343 		/*
344 		 * Release the buffer holding the directory cluster just
345 		 * searched.
346 		 */
347 		brelse(bp, 0);
348 	}	/* for (frcn = 0; ; frcn++) */
349 
350 notfound:
351 	/*
352 	 * We hold no disk buffers at this point.
353 	 */
354 
355 	/*
356 	 * If we get here we didn't find the entry we were looking for. But
357 	 * that's ok if we are creating or renaming and are at the end of
358 	 * the pathname and the directory hasn't been removed.
359 	 */
360 #ifdef MSDOSFS_DEBUG
361 	printf("msdosfs_lookup(): op %d, refcnt %ld, slotcount %d, slotoffset %d\n",
362 	    nameiop, dp->de_refcnt, slotcount, slotoffset);
363 #endif
364 	if ((nameiop == CREATE || nameiop == RENAME) &&
365 	    (flags & ISLASTCN) && dp->de_refcnt != 0) {
366 		/*
367 		 * Access for write is interpreted as allowing
368 		 * creation of files in the directory.
369 		 */
370 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
371 		if (error)
372 			return (error);
373 
374 		/*
375 		 * Fixup the slot description to point to the place where
376 		 * we might put the new DOS direntry (putting the Win95
377 		 * long name entries before that)
378 		 */
379 		if (!slotcount) {
380 			slotcount = 1;
381 			slotoffset = diroff;
382 		}
383 		if (wincnt > slotcount) {
384 			slotoffset +=
385 				sizeof(struct direntry) * (wincnt - slotcount);
386 		}
387 
388 		/*
389 		 * Return an indication of where the new directory
390 		 * entry should be put.
391 		 */
392 		dp->de_fndoffset = slotoffset;
393 		dp->de_fndcnt = wincnt - 1;
394 
395 		/*
396 		 * We return with the directory locked, so that
397 		 * the parameters we set up above will still be
398 		 * valid if we actually decide to do a direnter().
399 		 * We return ni_vp == NULL to indicate that the entry
400 		 * does not currently exist; we leave a pointer to
401 		 * the (locked) directory inode in ndp->ni_dvp.
402 		 *
403 		 * NB - if the directory is unlocked, then this
404 		 * information cannot be used.
405 		 */
406 		return (EJUSTRETURN);
407 	}
408 
409 #if 0
410 	/*
411 	 * Insert name into cache (as non-existent) if appropriate.
412 	 *
413 	 * XXX Negative caching is broken for msdosfs because the name
414 	 * cache doesn't understand peculiarities such as case insensitivity
415 	 * and 8.3 filenames.  Hence, it may not invalidate all negative
416 	 * entries if a file with this name is later created.
417 	 * e.g. creating a file 'foo' won't invalidate a negative entry
418 	 * for 'FOO'.
419 	 */
420 	if (nameiop != CREATE)
421 		cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
422 			    cnp->cn_flags);
423 #endif
424 
425 	return (ENOENT);
426 
427 found:
428 	/*
429 	 * NOTE:  We still have the buffer with matched directory entry at
430 	 * this point.
431 	 */
432 	isadir = dep->deAttributes & ATTR_DIRECTORY;
433 	scn = getushort(dep->deStartCluster);
434 	if (FAT32(pmp)) {
435 		scn |= getushort(dep->deHighClust) << 16;
436 		if (scn == pmp->pm_rootdirblk) {
437 			/*
438 			 * There should actually be 0 here.
439 			 * Just ignore the error.
440 			 */
441 			scn = MSDOSFSROOT;
442 		}
443 	}
444 
445 	if (isadir) {
446 		cluster = scn;
447 		if (cluster == MSDOSFSROOT)
448 			blkoff = MSDOSFSROOT_OFS;
449 		else
450 			blkoff = 0;
451 	} else if (cluster == MSDOSFSROOT)
452 		blkoff = diroff;
453 
454 	/*
455 	 * Now release buf to allow deget to read the entry again.
456 	 * Reserving it here and giving it to deget could result
457 	 * in a deadlock.
458 	 */
459 	brelse(bp, 0);
460 
461 foundroot:
462 	/*
463 	 * If we entered at foundroot, then we are looking for the . or ..
464 	 * entry of the filesystems root directory.  isadir and scn were
465 	 * setup before jumping here.  And, bp is already null.
466 	 */
467 	if (FAT32(pmp) && scn == MSDOSFSROOT)
468 		scn = pmp->pm_rootdirblk;
469 
470 	/*
471 	 * If deleting, and at end of pathname, return
472 	 * parameters which can be used to remove file.
473 	 * Lock the inode, being careful with ".".
474 	 */
475 	if (nameiop == DELETE && (flags & ISLASTCN)) {
476 		/*
477 		 * Don't allow deleting the root.
478 		 */
479 		if (blkoff == MSDOSFSROOT_OFS)
480 			return EINVAL;
481 
482 		/*
483 		 * Write access to directory required to delete files.
484 		 */
485 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
486 		if (error)
487 			return (error);
488 
489 		/*
490 		 * Return pointer to current entry in dp->i_offset.
491 		 * Save directory inode pointer in ndp->ni_dvp for dirremove().
492 		 */
493 		if (dp->de_StartCluster == scn && isadir) {	/* "." */
494 			vref(vdp);
495 			*vpp = vdp;
496 			return (0);
497 		}
498 		error = deget(pmp, cluster, blkoff, vpp);
499 		return error;
500 	}
501 
502 	/*
503 	 * If rewriting (RENAME), return the inode and the
504 	 * information required to rewrite the present directory
505 	 * Must get inode of directory entry to verify it's a
506 	 * regular file, or empty directory.
507 	 */
508 	if (nameiop == RENAME && (flags & ISLASTCN)) {
509 
510 		if (vdp->v_mount->mnt_flag & MNT_RDONLY)
511 			return (EROFS);
512 
513 		if (blkoff == MSDOSFSROOT_OFS)
514 			return EINVAL;
515 
516 		error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred);
517 		if (error)
518 			return (error);
519 
520 		/*
521 		 * Careful about locking second inode.
522 		 * This can only occur if the target is ".".
523 		 */
524 		if (dp->de_StartCluster == scn && isadir)
525 			return (EISDIR);
526 
527 		error = deget(pmp, cluster, blkoff, vpp);
528 		return error;
529 	}
530 
531 	if (dp->de_StartCluster == scn && isadir) {
532 		vref(vdp);	/* we want ourself, ie "." */
533 		*vpp = vdp;
534 	} else if ((error = deget(pmp, cluster, blkoff, vpp)) != 0) {
535 		return error;
536 	}
537 
538 	/*
539 	 * Insert name into cache if appropriate.
540 	 */
541 	cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
542 
543 	return 0;
544 }
545 #endif /* _KERNEL */
546 
547 /*
548  * dep  - directory entry to copy into the directory
549  * ddep - directory to add to
550  * depp - return the address of the denode for the created directory entry
551  *	  if depp != 0
552  * cnp  - componentname needed for Win95 long filenames
553  */
554 int
555 createde(struct denode *dep, struct denode *ddep, struct denode **depp, struct componentname *cnp)
556 {
557 	int error, rberror;
558 	u_long dirclust, clusoffset;
559 	u_long fndoffset, havecnt = 0, wcnt = 1, i;
560 	struct direntry *ndep;
561 	struct msdosfsmount *pmp = ddep->de_pmp;
562 	struct buf *bp;
563 	daddr_t bn;
564 	int blsize;
565 #ifdef _KERNEL
566 	int async = ddep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
567 #else
568 #define async 0
569 #endif
570 
571 #ifdef MSDOSFS_DEBUG
572 	printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
573 	    dep, ddep, depp, cnp);
574 #endif
575 
576 	/*
577 	 * If no space left in the directory then allocate another cluster
578 	 * and chain it onto the end of the file.  There is one exception
579 	 * to this.  That is, if the root directory has no more space it
580 	 * can NOT be expanded.  extendfile() checks for and fails attempts
581 	 * to extend the root directory.  We just return an error in that
582 	 * case.
583 	 */
584 	if (ddep->de_fndoffset >= ddep->de_FileSize) {
585 		u_long needlen = ddep->de_fndoffset + sizeof(struct direntry)
586 		    - ddep->de_FileSize;
587 		dirclust = de_clcount(pmp, needlen);
588 		if ((error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR)) != 0) {
589 			(void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
590 			goto err_norollback;
591 		}
592 
593 		/*
594 		 * Update the size of the directory
595 		 */
596 		ddep->de_FileSize += de_cn2off(pmp, dirclust);
597 	}
598 
599 	/*
600 	 * We just read in the cluster with space.  Copy the new directory
601 	 * entry in.  Then write it to disk. NOTE:  DOS directories
602 	 * do not get smaller as clusters are emptied.
603 	 */
604 	error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
605 		       &bn, &dirclust, &blsize);
606 	if (error)
607 		goto err_norollback;
608 	clusoffset = ddep->de_fndoffset;
609 	if (dirclust != MSDOSFSROOT)
610 		clusoffset &= pmp->pm_crbomask;
611 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
612 	    B_MODIFY, &bp)) != 0) {
613 		goto err_norollback;
614 	}
615 	ndep = bptoep(pmp, bp, clusoffset);
616 
617 	DE_EXTERNALIZE(ndep, dep);
618 
619 	/*
620 	 * Now write the Win95 long name
621 	 */
622 	if (ddep->de_fndcnt > 0) {
623 		u_int8_t chksum = winChksum(ndep->deName);
624 		const u_char *un = (const u_char *)cnp->cn_nameptr;
625 		int unlen = cnp->cn_namelen;
626 		u_long xhavecnt;
627 
628 		fndoffset = ddep->de_fndoffset;
629 		xhavecnt = ddep->de_fndcnt + 1;
630 
631 		for(; wcnt < xhavecnt; wcnt++) {
632 			if ((fndoffset & pmp->pm_crbomask) == 0) {
633 				/* we should never get here if ddep is root
634 				 * directory */
635 
636 				if (async)
637 					(void) bdwrite(bp);
638 				else if ((error = bwrite(bp)) != 0)
639 					goto rollback;
640 
641 				fndoffset -= sizeof(struct direntry);
642 				error = pcbmap(ddep,
643 					       de_cluster(pmp, fndoffset),
644 					       &bn, 0, &blsize);
645 				if (error)
646 					goto rollback;
647 
648 				error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
649 				    blsize, B_MODIFY, &bp);
650 				if (error) {
651 					goto rollback;
652 				}
653 				ndep = bptoep(pmp, bp,
654 						fndoffset & pmp->pm_crbomask);
655 			} else {
656 				ndep--;
657 				fndoffset -= sizeof(struct direntry);
658 			}
659 			if (!unix2winfn(un, unlen, (struct winentry *)ndep,
660 					wcnt, chksum,
661 					ddep->de_pmp->pm_flags & MSDOSFSMNT_UTF8))
662 				break;
663 		}
664 	}
665 
666 	if (async)
667 		bdwrite(bp);
668 	else if ((error = bwrite(bp)) != 0)
669 		goto rollback;
670 
671 	/*
672 	 * If they want us to return with the denode gotten.
673 	 */
674 	if (depp) {
675 		u_long diroffset = clusoffset;
676 
677 		if (dep->de_Attributes & ATTR_DIRECTORY) {
678 			dirclust = dep->de_StartCluster;
679 			if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
680 				dirclust = MSDOSFSROOT;
681 			if (dirclust == MSDOSFSROOT)
682 				diroffset = MSDOSFSROOT_OFS;
683 			else
684 				diroffset = 0;
685 		}
686 #ifdef MAKEFS
687 		error = deget(pmp, dirclust, diroffset, depp);
688 #else
689 		struct vnode *vp;
690 
691 		error = deget(pmp, dirclust, diroffset, &vp);
692 		if (error == 0)
693 			*depp = VTODE(vp);
694 		else
695 			*depp = NULL;
696 #endif
697 		return error;
698 	}
699 
700 	return 0;
701 
702     rollback:
703 	/*
704 	 * Mark all slots modified so far as deleted. Note that we
705 	 * can't just call removede(), since directory is not in
706 	 * consistent state.
707 	 */
708 	fndoffset = ddep->de_fndoffset;
709 	rberror = pcbmap(ddep, de_cluster(pmp, fndoffset),
710 	       &bn, NULL, &blsize);
711 	if (rberror)
712 		goto err_norollback;
713 	if ((rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
714 	    B_MODIFY, &bp)) != 0) {
715 		goto err_norollback;
716 	}
717 	ndep = bptoep(pmp, bp, clusoffset);
718 
719 	havecnt = ddep->de_fndcnt + 1;
720 	for(i = wcnt; i <= havecnt; i++) {
721 		/* mark entry as deleted */
722 		ndep->deName[0] = SLOT_DELETED;
723 
724 		if ((fndoffset & pmp->pm_crbomask) == 0) {
725 			/* we should never get here if ddep is root
726 			 * directory */
727 
728 			if (async)
729 				bdwrite(bp);
730 			else if ((rberror = bwrite(bp)) != 0)
731 				goto err_norollback;
732 
733 			fndoffset -= sizeof(struct direntry);
734 			rberror = pcbmap(ddep,
735 				       de_cluster(pmp, fndoffset),
736 				       &bn, 0, &blsize);
737 			if (rberror)
738 				goto err_norollback;
739 
740 			rberror = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
741 			    blsize, B_MODIFY, &bp);
742 			if (rberror) {
743 				goto err_norollback;
744 			}
745 			ndep = bptoep(pmp, bp, fndoffset);
746 		} else {
747 			ndep--;
748 			fndoffset -= sizeof(struct direntry);
749 		}
750 	}
751 
752 	/* ignore any further error */
753 	if (async)
754 		(void) bdwrite(bp);
755 	else
756 		(void) bwrite(bp);
757 
758     err_norollback:
759 	return error;
760 }
761 
762 /*
763  * Be sure a directory is empty except for "." and "..". Return 1 if empty,
764  * return 0 if not empty or error.
765  */
766 int
767 dosdirempty(struct denode *dep)
768 {
769 	int blsize;
770 	int error;
771 	u_long cn;
772 	daddr_t bn;
773 	struct buf *bp;
774 	struct msdosfsmount *pmp = dep->de_pmp;
775 	struct direntry *dentp;
776 
777 	/*
778 	 * Since the filesize field in directory entries for a directory is
779 	 * zero, we just have to feel our way through the directory until
780 	 * we hit end of file.
781 	 */
782 	for (cn = 0;; cn++) {
783 		if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
784 			if (error == E2BIG)
785 				return (1);	/* it's empty */
786 			return (0);
787 		}
788 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
789 		    0, &bp);
790 		if (error) {
791 			return (0);
792 		}
793 		for (dentp = (struct direntry *)bp->b_data;
794 		     (char *)dentp < (char *)bp->b_data + blsize;
795 		     dentp++) {
796 			if (dentp->deName[0] != SLOT_DELETED &&
797 			    (dentp->deAttributes & ATTR_VOLUME) == 0) {
798 				/*
799 				 * In dos directories an entry whose name
800 				 * starts with SLOT_EMPTY (0) starts the
801 				 * beginning of the unused part of the
802 				 * directory, so we can just return that it
803 				 * is empty.
804 				 */
805 				if (dentp->deName[0] == SLOT_EMPTY) {
806 					brelse(bp, 0);
807 					return (1);
808 				}
809 				/*
810 				 * Any names other than "." and ".." in a
811 				 * directory mean it is not empty.
812 				 */
813 				if (memcmp(dentp->deName, ".          ", 11) &&
814 				    memcmp(dentp->deName, "..         ", 11)) {
815 					brelse(bp, 0);
816 #ifdef MSDOSFS_DEBUG
817 					printf("dosdirempty(): found %.11s, %d, %d\n",
818 					    dentp->deName, dentp->deName[0],
819 						dentp->deName[1]);
820 #endif
821 					return (0);	/* not empty */
822 				}
823 			}
824 		}
825 		brelse(bp, 0);
826 	}
827 	/* NOTREACHED */
828 }
829 
830 /*
831  * Check to see if the directory described by target is in some
832  * subdirectory of source.  This prevents something like the following from
833  * succeeding and leaving a bunch or files and directories orphaned. mv
834  * /a/b/c /a/b/c/d/e/f Where c and f are directories.
835  *
836  * source - the inode for /a/b/c
837  * target - the inode for /a/b/c/d/e/f
838  *
839  * Returns 0 if target is NOT a subdirectory of source.
840  * Otherwise returns a non-zero error number.
841  * The target inode is always unlocked on return.
842  */
843 int
844 doscheckpath(struct denode *source, struct denode *target)
845 {
846 	u_long scn;
847 	struct msdosfsmount *pmp;
848 	struct direntry *ep;
849 	struct denode *dep;
850 	struct buf *bp = NULL;
851 	int error = 0;
852 
853 	dep = target;
854 	if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
855 	    (source->de_Attributes & ATTR_DIRECTORY) == 0) {
856 		error = ENOTDIR;
857 		goto out;
858 	}
859 	if (dep->de_StartCluster == source->de_StartCluster) {
860 		error = EEXIST;
861 		goto out;
862 	}
863 	if (dep->de_StartCluster == MSDOSFSROOT)
864 		goto out;
865 	pmp = dep->de_pmp;
866 #ifdef	DIAGNOSTIC
867 	if (pmp != source->de_pmp)
868 		panic("doscheckpath: source and target on different filesystems");
869 #endif
870 	if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)
871 		goto out;
872 
873 	for (;;) {
874 		if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
875 			error = ENOTDIR;
876 			break;
877 		}
878 		scn = dep->de_StartCluster;
879 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, cntobn(pmp, scn)),
880 			      pmp->pm_bpcluster, 0, &bp);
881 		if (error)
882 			break;
883 
884 		ep = (struct direntry *) bp->b_data + 1;
885 		if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
886 		    memcmp(ep->deName, "..         ", 11) != 0) {
887 			error = ENOTDIR;
888 			break;
889 		}
890 		scn = getushort(ep->deStartCluster);
891 		if (FAT32(pmp))
892 			scn |= getushort(ep->deHighClust) << 16;
893 
894 		if (scn == source->de_StartCluster) {
895 			error = EINVAL;
896 			break;
897 		}
898 		if (scn == MSDOSFSROOT)
899 			break;
900 		if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
901 			/*
902 			 * scn should be 0 in this case,
903 			 * but we silently ignore the error.
904 			 */
905 			break;
906 		}
907 
908 		vput(DETOV(dep));
909 		brelse(bp, 0);
910 		bp = NULL;
911 #ifdef MAKEFS
912 		/* NOTE: deget() clears dep on error */
913 		if ((error = deget(pmp, scn, 0, &dep)) != 0)
914 			break;
915 #else
916 		struct vnode *vp;
917 
918 		dep = NULL;
919 		error = deget(pmp, scn, 0, &vp);
920 		if (error)
921 			break;
922 		error = vn_lock(vp, LK_EXCLUSIVE);
923 		if (error) {
924 			vrele(vp);
925 			break;
926 		}
927 		dep = VTODE(vp);
928 #endif
929 	}
930 out:
931 	if (bp)
932 		brelse(bp, 0);
933 	if (error == ENOTDIR)
934 		printf("doscheckpath(): .. not a directory?\n");
935 	if (dep != NULL)
936 		vput(DETOV(dep));
937 	return (error);
938 }
939 
940 /*
941  * Read in the disk block containing the directory entry (dirclu, dirofs)
942  * and return the address of the buf header, and the address of the
943  * directory entry within the block.
944  */
945 int
946 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, struct buf **bpp, struct direntry **epp)
947 {
948 	int error;
949 	daddr_t bn;
950 	int blsize;
951 
952 	blsize = pmp->pm_bpcluster;
953 	if (dirclust == MSDOSFSROOT
954 	    && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
955 		blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
956 	bn = detobn(pmp, dirclust, diroffset);
957 	if ((error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
958 	    0, bpp)) != 0) {
959 		*bpp = NULL;
960 		return (error);
961 	}
962 	if (epp)
963 		*epp = bptoep(pmp, *bpp, diroffset);
964 	return (0);
965 }
966 
967 /*
968  * Read in the disk block containing the directory entry dep came from and
969  * return the address of the buf header, and the address of the directory
970  * entry within the block.
971  */
972 int
973 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
974 {
975 	return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
976 			bpp, epp));
977 }
978 
979 /*
980  * Remove a directory entry. At this point the file represented by the
981  * directory entry to be removed is still full length until noone has it
982  * open.  When the file no longer being used msdosfs_inactive() is called
983  * and will truncate the file to 0 length.  When the vnode containing the
984  * denode is needed for some other purpose by VFS it will call
985  * msdosfs_reclaim() which will remove the denode from the denode cache.
986  */
987 int
988 removede(struct denode *pdep, struct denode *dep)
989 	/* pdep:	 directory where the entry is removed */
990 	/* dep:	 file to be removed */
991 {
992 	int error;
993 	struct direntry *ep;
994 	struct buf *bp;
995 	daddr_t bn;
996 	int blsize;
997 	struct msdosfsmount *pmp = pdep->de_pmp;
998 	u_long offset = pdep->de_fndoffset;
999 #ifdef _KERNEL
1000 	int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
1001 #else
1002 #define async 0
1003 #endif
1004 
1005 #ifdef MSDOSFS_DEBUG
1006 	printf("removede(): filename %s, dep %p, offset %08lx\n",
1007 	    dep->de_Name, dep, offset);
1008 #endif
1009 
1010 	if (--dep->de_refcnt == 0) {
1011 #ifndef MAKEFS
1012 		struct denode_key old_key = dep->de_key;
1013 		struct denode_key new_key = dep->de_key;
1014 
1015 		KASSERT(new_key.dk_dirgen == NULL);
1016 		new_key.dk_dirgen = dep;
1017 		vcache_rekey_enter(pmp->pm_mountp, DETOV(dep), &old_key,
1018 		    sizeof(old_key), &new_key, sizeof(new_key));
1019 		dep->de_key = new_key;
1020 		vcache_rekey_exit(pmp->pm_mountp, DETOV(dep), &old_key,
1021 		    sizeof(old_key), &dep->de_key, sizeof(dep->de_key));
1022 #endif
1023 	}
1024 	offset += sizeof(struct direntry);
1025 	do {
1026 		offset -= sizeof(struct direntry);
1027 		error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
1028 		if (error)
1029 			return error;
1030 		error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1031 		    B_MODIFY, &bp);
1032 		if (error) {
1033 			return error;
1034 		}
1035 		ep = bptoep(pmp, bp, offset);
1036 		/*
1037 		 * Check whether, if we came here the second time, i.e.
1038 		 * when underflowing into the previous block, the last
1039 		 * entry in this block is a longfilename entry, too.
1040 		 */
1041 		if (ep->deAttributes != ATTR_WIN95
1042 		    && offset != pdep->de_fndoffset) {
1043 			brelse(bp, 0);
1044 			break;
1045 		}
1046 		offset += sizeof(struct direntry);
1047 		while (1) {
1048 			/*
1049 			 * We are a bit agressive here in that we delete any Win95
1050 			 * entries preceding this entry, not just the ones we "own".
1051 			 * Since these presumably aren't valid anyway,
1052 			 * there should be no harm.
1053 			 */
1054 			offset -= sizeof(struct direntry);
1055 			ep--->deName[0] = SLOT_DELETED;
1056 			if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1057 			    || !(offset & pmp->pm_crbomask)
1058 			    || ep->deAttributes != ATTR_WIN95)
1059 				break;
1060 		}
1061 		if (async)
1062 			bdwrite(bp);
1063 		else if ((error = bwrite(bp)) != 0)
1064 			return error;
1065 	} while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
1066 	    && !(offset & pmp->pm_crbomask)
1067 	    && offset);
1068 	return 0;
1069 }
1070 
1071 /*
1072  * Create a unique DOS name in dvp
1073  */
1074 int
1075 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
1076 {
1077 	struct msdosfsmount *pmp = dep->de_pmp;
1078 	struct direntry *dentp;
1079 	int gen;
1080 	int blsize;
1081 	u_long cn;
1082 	daddr_t bn;
1083 	struct buf *bp;
1084 	int error;
1085 
1086 	for (gen = 1;; gen++) {
1087 		/*
1088 		 * Generate DOS name with generation number
1089 		 */
1090 		if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
1091 		    cnp->cn_namelen, gen))
1092 			return gen == 1 ? EINVAL : EEXIST;
1093 
1094 		/*
1095 		 * Now look for a dir entry with this exact name
1096 		 */
1097 		for (cn = error = 0; !error; cn++) {
1098 			if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
1099 				if (error == E2BIG)	/* EOF reached and not found */
1100 					return 0;
1101 				return error;
1102 			}
1103 			error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1104 			    0, &bp);
1105 			if (error) {
1106 				return error;
1107 			}
1108 			for (dentp = (struct direntry *)bp->b_data;
1109 			     (char *)dentp < (char *)bp->b_data + blsize;
1110 			     dentp++) {
1111 				if (dentp->deName[0] == SLOT_EMPTY) {
1112 					/*
1113 					 * Last used entry and not found
1114 					 */
1115 					brelse(bp, 0);
1116 					return 0;
1117 				}
1118 				/*
1119 				 * Ignore volume labels and Win95 entries
1120 				 */
1121 				if (dentp->deAttributes & ATTR_VOLUME)
1122 					continue;
1123 				if (!memcmp(dentp->deName, cp, 11)) {
1124 					error = EEXIST;
1125 					break;
1126 				}
1127 			}
1128 			brelse(bp, 0);
1129 		}
1130 	}
1131 }
1132 
1133 /*
1134  * Find any Win'95 long filename entry in directory dep
1135  */
1136 int
1137 findwin95(struct denode *dep)
1138 {
1139 	struct msdosfsmount *pmp = dep->de_pmp;
1140 	struct direntry *dentp;
1141 	int blsize, win95;
1142 	u_long cn;
1143 	daddr_t bn;
1144 	struct buf *bp;
1145 
1146 	win95 = 1;
1147 	/*
1148 	 * Read through the directory looking for Win'95 entries
1149 	 * XXX Note: Error currently handled just as EOF
1150 	 */
1151 	for (cn = 0;; cn++) {
1152 		if (pcbmap(dep, cn, &bn, 0, &blsize))
1153 			return win95;
1154 		if (bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1155 		    0, &bp)) {
1156 			return win95;
1157 		}
1158 		for (dentp = (struct direntry *)bp->b_data;
1159 		     (char *)dentp < (char *)bp->b_data + blsize;
1160 		     dentp++) {
1161 			if (dentp->deName[0] == SLOT_EMPTY) {
1162 				/*
1163 				 * Last used entry and not found
1164 				 */
1165 				brelse(bp, 0);
1166 				return win95;
1167 			}
1168 			if (dentp->deName[0] == SLOT_DELETED) {
1169 				/*
1170 				 * Ignore deleted files
1171 				 * Note: might be an indication of Win'95
1172 				 * anyway	XXX
1173 				 */
1174 				continue;
1175 			}
1176 			if (dentp->deAttributes == ATTR_WIN95) {
1177 				brelse(bp, 0);
1178 				return 1;
1179 			}
1180 			win95 = 0;
1181 		}
1182 		brelse(bp, 0);
1183 	}
1184 }
1185