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 return error; 129 } 130 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 131 132 DE_EXTERNALIZE(ndep, dep); 133 134 /* 135 * Now write the Win95 long name 136 */ 137 if (ddep->de_fndcnt > 0) { 138 uint8_t chksum = winChksum(ndep->deName); 139 const u_char *un = (const u_char *)cnp->cn_nameptr; 140 int unlen = cnp->cn_namelen; 141 int cnt = 1; 142 143 while (--ddep->de_fndcnt >= 0) { 144 if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 145 if ((error = bwrite(bp)) != 0) 146 return error; 147 148 ddep->de_fndoffset -= sizeof(struct direntry); 149 error = pcbmap(ddep, 150 de_cluster(pmp, 151 ddep->de_fndoffset), 152 &bn, 0, &blsize); 153 if (error) 154 return error; 155 156 error = bread((void *)pmp->pm_devvp, bn, blsize, 157 NOCRED, &bp); 158 if (error) { 159 return error; 160 } 161 ndep = bptoep(pmp, bp, ddep->de_fndoffset); 162 } else { 163 ndep--; 164 ddep->de_fndoffset -= sizeof(struct direntry); 165 } 166 if (!unix2winfn(un, unlen, (struct winentry *)ndep, 167 cnt++, chksum)) 168 break; 169 } 170 } 171 172 if ((error = bwrite(bp)) != 0) 173 return error; 174 175 /* 176 * If they want us to return with the denode gotten. 177 */ 178 if (depp) { 179 if (dep->de_Attributes & ATTR_DIRECTORY) { 180 dirclust = dep->de_StartCluster; 181 if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 182 dirclust = MSDOSFSROOT; 183 if (dirclust == MSDOSFSROOT) 184 diroffset = MSDOSFSROOT_OFS; 185 else 186 diroffset = 0; 187 } 188 return deget(pmp, dirclust, diroffset, 0, depp); 189 } 190 191 return 0; 192 } 193 194 /* 195 * Read in the disk block containing the directory entry (dirclu, dirofs) 196 * and return the address of the buf header, and the address of the 197 * directory entry within the block. 198 */ 199 int 200 m_readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 201 struct m_buf **bpp, struct direntry **epp) 202 { 203 int error; 204 daddr_t bn; 205 int blsize; 206 207 blsize = pmp->pm_bpcluster; 208 if (dirclust == MSDOSFSROOT 209 && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 210 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 211 bn = detobn(pmp, dirclust, diroffset); 212 if ((error = bread((void *)pmp->pm_devvp, bn, blsize, NOCRED, 213 bpp)) != 0) { 214 *bpp = NULL; 215 return (error); 216 } 217 if (epp) 218 *epp = bptoep(pmp, *bpp, diroffset); 219 return (0); 220 } 221 222 /* 223 * Read in the disk block containing the directory entry dep came from and 224 * return the address of the buf header, and the address of the directory 225 * entry within the block. 226 */ 227 int 228 m_readde(struct denode *dep, struct m_buf **bpp, struct direntry **epp) 229 { 230 231 return (m_readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 232 bpp, epp)); 233 } 234 235 /* 236 * Create a unique DOS name in dvp 237 */ 238 int 239 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 240 { 241 struct msdosfsmount *pmp = dep->de_pmp; 242 struct direntry *dentp; 243 int gen; 244 int blsize; 245 u_long cn; 246 daddr_t bn; 247 struct m_buf *bp; 248 int error; 249 250 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 251 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 252 cnp->cn_namelen, 0) ? 0 : EINVAL); 253 254 for (gen = 1;; gen++) { 255 /* 256 * Generate DOS name with generation number 257 */ 258 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 259 cnp->cn_namelen, gen)) 260 return gen == 1 ? EINVAL : EEXIST; 261 262 /* 263 * Now look for a dir entry with this exact name 264 */ 265 for (cn = error = 0; !error; cn++) { 266 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 267 if (error == E2BIG) /* EOF reached and not found */ 268 return 0; 269 return error; 270 } 271 error = bread((void *)pmp->pm_devvp, bn, blsize, 272 NOCRED, &bp); 273 if (error) { 274 return error; 275 } 276 for (dentp = (struct direntry *)bp->b_data; 277 (char *)dentp < bp->b_data + blsize; 278 dentp++) { 279 if (dentp->deName[0] == SLOT_EMPTY) { 280 /* 281 * Last used entry and not found 282 */ 283 brelse(bp); 284 return 0; 285 } 286 /* 287 * Ignore volume labels and Win95 entries 288 */ 289 if (dentp->deAttributes & ATTR_VOLUME) 290 continue; 291 if (!bcmp(dentp->deName, cp, 11)) { 292 error = EEXIST; 293 break; 294 } 295 } 296 brelse(bp); 297 } 298 } 299 } 300