2 /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by TooLs GmbH.
23 * 4. The name of TooLs GmbH may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
43 * This software is provided "as is".
45 * The author supplies this software to be publicly redistributed on the
46 * understanding that the author is not responsible for the correct
47 * functioning of this software in any circumstances and is not liable for
48 * any damages caused by this software.
53 #include <sys/param.h>
54 #include <sys/errno.h>
61 #include <fs/msdosfs/bpb.h>
62 #include "msdos/direntry.h"
63 #include <fs/msdosfs/denode.h>
64 #include <fs/msdosfs/fat.h>
65 #include <fs/msdosfs/msdosfsmount.h>
71 * dep - directory entry to copy into the directory
72 * ddep - directory to add to
73 * depp - return the address of the denode for the created directory entry
75 * cnp - componentname needed for Win95 long filenames
78 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
79 struct componentname *cnp)
82 u_long dirclust, diroffset;
83 struct direntry *ndep;
84 struct msdosfsmount *pmp = ddep->de_pmp;
89 MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
90 dep, ddep, depp, cnp));
93 * If no space left in the directory then allocate another cluster
94 * and chain it onto the end of the file. There is one exception
95 * to this. That is, if the root directory has no more space it
96 * can NOT be expanded. extendfile() checks for and fails attempts
97 * to extend the root directory. We just return an error in that
100 if (ddep->de_fndoffset >= ddep->de_FileSize) {
101 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
103 dirclust = de_clcount(pmp, diroffset);
104 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
106 (void)detrunc(ddep, ddep->de_FileSize, 0, NULL);
111 * Update the size of the directory
113 ddep->de_FileSize += de_cn2off(pmp, dirclust);
117 * We just read in the cluster with space. Copy the new directory
118 * entry in. Then write it to disk. NOTE: DOS directories
119 * do not get smaller as clusters are emptied.
121 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
122 &bn, &dirclust, &blsize);
125 diroffset = ddep->de_fndoffset;
126 if (dirclust != MSDOSFSROOT)
127 diroffset &= pmp->pm_crbomask;
128 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
132 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
134 DE_EXTERNALIZE(ndep, dep);
137 * Now write the Win95 long name
139 if (ddep->de_fndcnt > 0) {
140 uint8_t chksum = winChksum(ndep->deName);
141 const u_char *un = (const u_char *)cnp->cn_nameptr;
142 int unlen = cnp->cn_namelen;
145 while (--ddep->de_fndcnt >= 0) {
146 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
147 if ((error = bwrite(bp)) != 0)
150 ddep->de_fndoffset -= sizeof(struct direntry);
158 error = bread(pmp->pm_devvp, bn, blsize,
164 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
167 ddep->de_fndoffset -= sizeof(struct direntry);
169 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
175 if ((error = bwrite(bp)) != 0)
179 * If they want us to return with the denode gotten.
182 if (dep->de_Attributes & ATTR_DIRECTORY) {
183 dirclust = dep->de_StartCluster;
184 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
185 dirclust = MSDOSFSROOT;
186 if (dirclust == MSDOSFSROOT)
187 diroffset = MSDOSFSROOT_OFS;
191 return deget(pmp, dirclust, diroffset, depp);
198 * Read in the disk block containing the directory entry (dirclu, dirofs)
199 * and return the address of the buf header, and the address of the
200 * directory entry within the block.
203 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
204 struct buf **bpp, struct direntry **epp)
210 blsize = pmp->pm_bpcluster;
211 if (dirclust == MSDOSFSROOT
212 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
213 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
214 bn = detobn(pmp, dirclust, diroffset);
215 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
221 *epp = bptoep(pmp, *bpp, diroffset);
226 * Read in the disk block containing the directory entry dep came from and
227 * return the address of the buf header, and the address of the directory
228 * entry within the block.
231 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
234 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
239 * Create a unique DOS name in dvp
242 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
244 struct msdosfsmount *pmp = dep->de_pmp;
245 struct direntry *dentp;
253 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
254 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
255 cnp->cn_namelen, 0) ? 0 : EINVAL);
257 for (gen = 1;; gen++) {
259 * Generate DOS name with generation number
261 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
262 cnp->cn_namelen, gen))
263 return gen == 1 ? EINVAL : EEXIST;
266 * Now look for a dir entry with this exact name
268 for (cn = error = 0; !error; cn++) {
269 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
270 if (error == E2BIG) /* EOF reached and not found */
274 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
279 for (dentp = (struct direntry *)bp->b_data;
280 (char *)dentp < bp->b_data + blsize;
282 if (dentp->deName[0] == SLOT_EMPTY) {
284 * Last used entry and not found
290 * Ignore volume labels and Win95 entries
292 if (dentp->deAttributes & ATTR_VOLUME)
294 if (!bcmp(dentp->deName, cp, 11)) {