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