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 "ffs/buf.h" 60 #include <fs/msdosfs/bpb.h> 61 #include "msdos/direntry.h" 62 #include <fs/msdosfs/denode.h> 63 #include <fs/msdosfs/fat.h> 64 #include <fs/msdosfs/msdosfsmount.h> 65 66 #include "makefs.h" 67 #include "msdos.h" 68 69 /* 70 * dep - directory entry to copy into the directory 71 * ddep - directory to add to 72 * depp - return the address of the denode for the created directory entry 73 * if depp != 0 74 * cnp - componentname needed for Win95 long filenames 75 */ 76 int 77 createde(struct denode *dep, struct denode *ddep, struct denode **depp, 78 struct componentname *cnp) 79 { 80 int error; 81 u_long dirclust, diroffset; 82 struct direntry *ndep; 83 struct msdosfsmount *pmp = ddep->de_pmp; 84 struct buf *bp; 85 daddr_t bn; 86 int blsize; 87 88 MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 89 dep, ddep, depp, cnp)); 90 91 /* 92 * If no space left in the directory then allocate another cluster 93 * and chain it onto the end of the file. There is one exception 94 * to this. That is, if the root directory has no more space it 95 * can NOT be expanded. extendfile() checks for and fails attempts 96 * to extend the root directory. We just return an error in that 97 * case. 98 */ 99 if (ddep->de_fndoffset >= ddep->de_FileSize) { 100 diroffset = ddep->de_fndoffset + sizeof(struct direntry) 101 - ddep->de_FileSize; 102 dirclust = de_clcount(pmp, diroffset); 103 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 104 if (error) { 105 (void)detrunc(ddep, ddep->de_FileSize, 0, NULL); 106 return error; 107 } 108 109 /* 110 * Update the size of the directory 111 */ 112 ddep->de_FileSize += de_cn2off(pmp, dirclust); 113 } 114 115 /* 116 * We just read in the cluster with space. Copy the new directory 117 * entry in. Then write it to disk. NOTE: DOS directories 118 * do not get smaller as clusters are emptied. 119 */ 120 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 121 &bn, &dirclust, &blsize); 122 if (error) 123 return error; 124 diroffset = ddep->de_fndoffset; 125 if (dirclust != MSDOSFSROOT) 126 diroffset &= pmp->pm_crbomask; 127 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) { 128 brelse(bp); 129 return error; 130 } 131 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 132 133 DE_EXTERNALIZE(ndep, dep); 134 135 /* 136 * Now write the Win95 long name 137 */ 138 if (ddep->de_fndcnt > 0) { 139 uint8_t chksum = winChksum(ndep->deName); 140 const u_char *un = (const u_char *)cnp->cn_nameptr; 141 int unlen = cnp->cn_namelen; 142 int cnt = 1; 143 144 while (--ddep->de_fndcnt >= 0) { 145 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 146 if ((error = bwrite(bp)) != 0) 147 return error; 148 149 ddep->de_fndoffset -= sizeof(struct direntry); 150 error = pcbmap(ddep, 151 de_cluster(pmp, 152 ddep->de_fndoffset), 153 &bn, 0, &blsize); 154 if (error) 155 return error; 156 157 error = bread(pmp->pm_devvp, bn, blsize, 158 NOCRED, &bp); 159 if (error) { 160 brelse(bp); 161 return error; 162 } 163 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 164 } else { 165 ndep--; 166 ddep->de_fndoffset -= sizeof(struct direntry); 167 } 168 if (!unix2winfn(un, unlen, (struct winentry *)ndep, 169 cnt++, chksum)) 170 break; 171 } 172 } 173 174 if ((error = bwrite(bp)) != 0) 175 return error; 176 177 /* 178 * If they want us to return with the denode gotten. 179 */ 180 if (depp) { 181 if (dep->de_Attributes & ATTR_DIRECTORY) { 182 dirclust = dep->de_StartCluster; 183 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 184 dirclust = MSDOSFSROOT; 185 if (dirclust == MSDOSFSROOT) 186 diroffset = MSDOSFSROOT_OFS; 187 else 188 diroffset = 0; 189 } 190 return deget(pmp, dirclust, diroffset, depp); 191 } 192 193 return 0; 194 } 195 196 /* 197 * Read in the disk block containing the directory entry (dirclu, dirofs) 198 * and return the address of the buf header, and the address of the 199 * directory entry within the block. 200 */ 201 int 202 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 203 struct buf **bpp, struct direntry **epp) 204 { 205 int error; 206 daddr_t bn; 207 int blsize; 208 209 blsize = pmp->pm_bpcluster; 210 if (dirclust == MSDOSFSROOT 211 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 212 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 213 bn = detobn(pmp, dirclust, diroffset); 214 if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 215 brelse(*bpp); 216 *bpp = NULL; 217 return (error); 218 } 219 if (epp) 220 *epp = bptoep(pmp, *bpp, diroffset); 221 return (0); 222 } 223 224 /* 225 * Read in the disk block containing the directory entry dep came from and 226 * return the address of the buf header, and the address of the directory 227 * entry within the block. 228 */ 229 int 230 readde(struct denode *dep, struct buf **bpp, struct direntry **epp) 231 { 232 233 return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 234 bpp, epp)); 235 } 236 237 /* 238 * Create a unique DOS name in dvp 239 */ 240 int 241 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 242 { 243 struct msdosfsmount *pmp = dep->de_pmp; 244 struct direntry *dentp; 245 int gen; 246 int blsize; 247 u_long cn; 248 daddr_t bn; 249 struct buf *bp; 250 int error; 251 252 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 253 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 254 cnp->cn_namelen, 0) ? 0 : EINVAL); 255 256 for (gen = 1;; gen++) { 257 /* 258 * Generate DOS name with generation number 259 */ 260 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 261 cnp->cn_namelen, gen)) 262 return gen == 1 ? EINVAL : EEXIST; 263 264 /* 265 * Now look for a dir entry with this exact name 266 */ 267 for (cn = error = 0; !error; cn++) { 268 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 269 if (error == E2BIG) /* EOF reached and not found */ 270 return 0; 271 return error; 272 } 273 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 274 if (error) { 275 brelse(bp); 276 return error; 277 } 278 for (dentp = (struct direntry *)bp->b_data; 279 (char *)dentp < bp->b_data + blsize; 280 dentp++) { 281 if (dentp->deName[0] == SLOT_EMPTY) { 282 /* 283 * Last used entry and not found 284 */ 285 brelse(bp); 286 return 0; 287 } 288 /* 289 * Ignore volume labels and Win95 entries 290 */ 291 if (dentp->deAttributes & ATTR_VOLUME) 292 continue; 293 if (!bcmp(dentp->deName, cp, 11)) { 294 error = EEXIST; 295 break; 296 } 297 } 298 brelse(bp); 299 } 300 } 301 } 302