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>
59 #include <fs/msdosfs/bpb.h>
63 #include "msdos/denode.h"
64 #include "msdos/direntry.h"
65 #include "msdos/fat.h"
66 #include "msdos/msdosfsmount.h"
72 * dep - directory entry to copy into the directory
73 * ddep - directory to add to
74 * depp - return the address of the denode for the created directory entry
76 * cnp - componentname needed for Win95 long filenames
79 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
80 struct componentname *cnp)
83 u_long dirclust, diroffset;
84 struct direntry *ndep;
85 struct msdosfsmount *pmp = ddep->de_pmp;
90 MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
91 dep, ddep, depp, cnp));
94 * If no space left in the directory then allocate another cluster
95 * and chain it onto the end of the file. There is one exception
96 * to this. That is, if the root directory has no more space it
97 * can NOT be expanded. extendfile() checks for and fails attempts
98 * to extend the root directory. We just return an error in that
101 if (ddep->de_fndoffset >= ddep->de_FileSize) {
102 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
104 dirclust = de_clcount(pmp, diroffset);
105 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
107 (void)detrunc(ddep, ddep->de_FileSize, 0);
112 * Update the size of the directory
114 ddep->de_FileSize += de_cn2off(pmp, dirclust);
118 * We just read in the cluster with space. Copy the new directory
119 * entry in. Then write it to disk. NOTE: DOS directories
120 * do not get smaller as clusters are emptied.
122 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
123 &bn, &dirclust, &blsize);
126 diroffset = ddep->de_fndoffset;
127 if (dirclust != MSDOSFSROOT)
128 diroffset &= pmp->pm_crbomask;
129 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
133 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
135 DE_EXTERNALIZE(ndep, dep);
138 * Now write the Win95 long name
140 if (ddep->de_fndcnt > 0) {
141 uint8_t chksum = winChksum(ndep->deName);
142 const u_char *un = (const u_char *)cnp->cn_nameptr;
143 int unlen = cnp->cn_namelen;
146 while (--ddep->de_fndcnt >= 0) {
147 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
148 if ((error = bwrite(bp)) != 0)
151 ddep->de_fndoffset -= sizeof(struct direntry);
159 error = bread(pmp->pm_devvp, bn, blsize,
165 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
168 ddep->de_fndoffset -= sizeof(struct direntry);
170 if (!unix2winfn(un, unlen, (struct winentry *)ndep,
176 if ((error = bwrite(bp)) != 0)
180 * If they want us to return with the denode gotten.
183 if (dep->de_Attributes & ATTR_DIRECTORY) {
184 dirclust = dep->de_StartCluster;
185 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
186 dirclust = MSDOSFSROOT;
187 if (dirclust == MSDOSFSROOT)
188 diroffset = MSDOSFSROOT_OFS;
192 return deget(pmp, dirclust, diroffset, depp);
199 * Read in the disk block containing the directory entry (dirclu, dirofs)
200 * and return the address of the buf header, and the address of the
201 * directory entry within the block.
204 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
205 struct buf **bpp, struct direntry **epp)
211 blsize = pmp->pm_bpcluster;
212 if (dirclust == MSDOSFSROOT
213 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
214 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
215 bn = detobn(pmp, dirclust, diroffset);
216 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
222 *epp = bptoep(pmp, *bpp, diroffset);
227 * Read in the disk block containing the directory entry dep came from and
228 * return the address of the buf header, and the address of the directory
229 * entry within the block.
232 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
235 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
240 * Create a unique DOS name in dvp
243 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
245 struct msdosfsmount *pmp = dep->de_pmp;
246 struct direntry *dentp;
254 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
255 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
256 cnp->cn_namelen, 0) ? 0 : EINVAL);
258 for (gen = 1;; gen++) {
260 * Generate DOS name with generation number
262 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
263 cnp->cn_namelen, gen))
264 return gen == 1 ? EINVAL : EEXIST;
267 * Now look for a dir entry with this exact name
269 for (cn = error = 0; !error; cn++) {
270 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
271 if (error == E2BIG) /* EOF reached and not found */
275 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
280 for (dentp = (struct direntry *)bp->b_data;
281 (char *)dentp < bp->b_data + blsize;
283 if (dentp->deName[0] == SLOT_EMPTY) {
285 * Last used entry and not found
291 * Ignore volume labels and Win95 entries
293 if (dentp->deAttributes & ATTR_VOLUME)
295 if (!bcmp(dentp->deName, cp, 11)) {