1 /* $FreeBSD$ */ 2 /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */ 3 4 /*- 5 * SPDX-License-Identifier: BSD-4-Clause 6 * 7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 9 * All rights reserved. 10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 25 * 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. 36 */ 37 /*- 38 * Written by Paul Popelka (paulp@uts.amdahl.com) 39 * 40 * You can do anything you want with this software, just don't say you wrote 41 * it, and don't remove this notice. 42 * 43 * This software is provided "as is". 44 * 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. 49 * 50 * October 1992 51 */ 52 53 #include <sys/param.h> 54 #include <sys/errno.h> 55 56 #include <stdio.h> 57 #include <string.h> 58 59 #include <fs/msdosfs/bpb.h> 60 61 #include "ffs/buf.h" 62 63 #include "msdos/denode.h" 64 #include "msdos/direntry.h" 65 #include "msdos/fat.h" 66 #include "msdos/msdosfsmount.h" 67 68 #include "makefs.h" 69 #include "msdos.h" 70 71 /* 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 75 * if depp != 0 76 * cnp - componentname needed for Win95 long filenames 77 */ 78 int 79 createde(struct denode *dep, struct denode *ddep, struct denode **depp, 80 struct componentname *cnp) 81 { 82 int error; 83 u_long dirclust, diroffset; 84 struct direntry *ndep; 85 struct msdosfsmount *pmp = ddep->de_pmp; 86 struct buf *bp; 87 daddr_t bn; 88 int blsize; 89 90 MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 91 dep, ddep, depp, cnp)); 92 93 /* 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 99 * case. 100 */ 101 if (ddep->de_fndoffset >= ddep->de_FileSize) { 102 diroffset = ddep->de_fndoffset + sizeof(struct direntry) 103 - ddep->de_FileSize; 104 dirclust = de_clcount(pmp, diroffset); 105 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 106 if (error) { 107 (void)detrunc(ddep, ddep->de_FileSize, 0); 108 return error; 109 } 110 111 /* 112 * Update the size of the directory 113 */ 114 ddep->de_FileSize += de_cn2off(pmp, dirclust); 115 } 116 117 /* 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. 121 */ 122 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 123 &bn, &dirclust, &blsize); 124 if (error) 125 return error; 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) { 130 brelse(bp); 131 return error; 132 } 133 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 134 135 DE_EXTERNALIZE(ndep, dep); 136 137 /* 138 * Now write the Win95 long name 139 */ 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; 144 int cnt = 1; 145 146 while (--ddep->de_fndcnt >= 0) { 147 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 148 if ((error = bwrite(bp)) != 0) 149 return error; 150 151 ddep->de_fndoffset -= sizeof(struct direntry); 152 error = pcbmap(ddep, 153 de_cluster(pmp, 154 ddep->de_fndoffset), 155 &bn, 0, &blsize); 156 if (error) 157 return error; 158 159 error = bread(pmp->pm_devvp, bn, blsize, 160 NOCRED, &bp); 161 if (error) { 162 brelse(bp); 163 return error; 164 } 165 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 166 } else { 167 ndep--; 168 ddep->de_fndoffset -= sizeof(struct direntry); 169 } 170 if (!unix2winfn(un, unlen, (struct winentry *)ndep, 171 cnt++, chksum)) 172 break; 173 } 174 } 175 176 if ((error = bwrite(bp)) != 0) 177 return error; 178 179 /* 180 * If they want us to return with the denode gotten. 181 */ 182 if (depp) { 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; 189 else 190 diroffset = 0; 191 } 192 return deget(pmp, dirclust, diroffset, depp); 193 } 194 195 return 0; 196 } 197 198 /* 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. 202 */ 203 int 204 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 205 struct buf **bpp, struct direntry **epp) 206 { 207 int error; 208 daddr_t bn; 209 int blsize; 210 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) { 217 brelse(*bpp); 218 *bpp = NULL; 219 return (error); 220 } 221 if (epp) 222 *epp = bptoep(pmp, *bpp, diroffset); 223 return (0); 224 } 225 226 /* 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. 230 */ 231 int 232 readde(struct denode *dep, struct buf **bpp, struct direntry **epp) 233 { 234 235 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 236 bpp, epp)); 237 } 238 239 /* 240 * Create a unique DOS name in dvp 241 */ 242 int 243 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 244 { 245 struct msdosfsmount *pmp = dep->de_pmp; 246 struct direntry *dentp; 247 int gen; 248 int blsize; 249 u_long cn; 250 daddr_t bn; 251 struct buf *bp; 252 int error; 253 254 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 255 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 256 cnp->cn_namelen, 0) ? 0 : EINVAL); 257 258 for (gen = 1;; gen++) { 259 /* 260 * Generate DOS name with generation number 261 */ 262 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 263 cnp->cn_namelen, gen)) 264 return gen == 1 ? EINVAL : EEXIST; 265 266 /* 267 * Now look for a dir entry with this exact name 268 */ 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 */ 272 return 0; 273 return error; 274 } 275 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 276 if (error) { 277 brelse(bp); 278 return error; 279 } 280 for (dentp = (struct direntry *)bp->b_data; 281 (char *)dentp < (char *)bp->b_data + blsize; 282 dentp++) { 283 if (dentp->deName[0] == SLOT_EMPTY) { 284 /* 285 * Last used entry and not found 286 */ 287 brelse(bp); 288 return 0; 289 } 290 /* 291 * Ignore volume labels and Win95 entries 292 */ 293 if (dentp->deAttributes & ATTR_VOLUME) 294 continue; 295 if (!bcmp(dentp->deName, cp, 11)) { 296 error = EEXIST; 297 break; 298 } 299 } 300 brelse(bp); 301 } 302 } 303 } 304