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 <stdbool.h> 57 #include <stdio.h> 58 #include <string.h> 59 60 #include <fs/msdosfs/bpb.h> 61 #include "msdos/denode.h" 62 #include <fs/msdosfs/fat.h> 63 #include <fs/msdosfs/msdosfsmount.h> 64 65 #include "makefs.h" 66 #include "msdos.h" 67 68 /* 69 * dep - directory entry to copy into the directory 70 * ddep - directory to add to 71 * depp - return the address of the denode for the created directory entry 72 * if depp != 0 73 * cnp - componentname needed for Win95 long filenames 74 */ 75 int 76 createde(struct denode *dep, struct denode *ddep, struct denode **depp, 77 struct componentname *cnp) 78 { 79 int error; 80 u_long dirclust, diroffset; 81 struct direntry *ndep; 82 struct msdosfsmount *pmp = ddep->de_pmp; 83 struct m_buf *bp; 84 daddr_t bn; 85 int blsize; 86 87 MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 88 dep, ddep, depp, cnp)); 89 90 /* 91 * If no space left in the directory then allocate another cluster 92 * and chain it onto the end of the file. There is one exception 93 * to this. That is, if the root directory has no more space it 94 * can NOT be expanded. extendfile() checks for and fails attempts 95 * to extend the root directory. We just return an error in that 96 * case. 97 */ 98 if (ddep->de_fndoffset >= ddep->de_FileSize) { 99 diroffset = ddep->de_fndoffset + sizeof(struct direntry) 100 - ddep->de_FileSize; 101 dirclust = de_clcount(pmp, diroffset); 102 error = m_extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 103 if (error) { 104 (void)detrunc(ddep, ddep->de_FileSize, 0, NULL); 105 return error; 106 } 107 108 /* 109 * Update the size of the directory 110 */ 111 ddep->de_FileSize += de_cn2off(pmp, dirclust); 112 } 113 114 /* 115 * We just read in the cluster with space. Copy the new directory 116 * entry in. Then write it to disk. NOTE: DOS directories 117 * do not get smaller as clusters are emptied. 118 */ 119 error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 120 &bn, &dirclust, &blsize); 121 if (error) 122 return error; 123 diroffset = ddep->de_fndoffset; 124 if (dirclust != MSDOSFSROOT) 125 diroffset &= pmp->pm_crbomask; 126 if ((error = bread((void *)pmp->pm_devvp, bn, blsize, NOCRED, 127 &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((void *)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 m_readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 203 struct m_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((void *)pmp->pm_devvp, bn, blsize, NOCRED, 215 bpp)) != 0) { 216 brelse(*bpp); 217 *bpp = NULL; 218 return (error); 219 } 220 if (epp) 221 *epp = bptoep(pmp, *bpp, diroffset); 222 return (0); 223 } 224 225 /* 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. 229 */ 230 int 231 m_readde(struct denode *dep, struct m_buf **bpp, struct direntry **epp) 232 { 233 234 return (m_readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 235 bpp, epp)); 236 } 237 238 /* 239 * Create a unique DOS name in dvp 240 */ 241 int 242 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 243 { 244 struct msdosfsmount *pmp = dep->de_pmp; 245 struct direntry *dentp; 246 int gen; 247 int blsize; 248 u_long cn; 249 daddr_t bn; 250 struct m_buf *bp; 251 int error; 252 253 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 254 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 255 cnp->cn_namelen, 0) ? 0 : EINVAL); 256 257 for (gen = 1;; gen++) { 258 /* 259 * Generate DOS name with generation number 260 */ 261 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 262 cnp->cn_namelen, gen)) 263 return gen == 1 ? EINVAL : EEXIST; 264 265 /* 266 * Now look for a dir entry with this exact name 267 */ 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 */ 271 return 0; 272 return error; 273 } 274 error = bread((void *)pmp->pm_devvp, bn, blsize, 275 NOCRED, &bp); 276 if (error) { 277 brelse(bp); 278 return error; 279 } 280 for (dentp = (struct direntry *)bp->b_data; 281 (char *)dentp < 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