198dc8da5SEd Maste /* $FreeBSD$ */ 298dc8da5SEd Maste /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */ 398dc8da5SEd Maste 498dc8da5SEd Maste /*- 598dc8da5SEd Maste * SPDX-License-Identifier: BSD-4-Clause 698dc8da5SEd Maste * 798dc8da5SEd Maste * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 898dc8da5SEd Maste * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 998dc8da5SEd Maste * All rights reserved. 1098dc8da5SEd Maste * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 1198dc8da5SEd Maste * 1298dc8da5SEd Maste * Redistribution and use in source and binary forms, with or without 1398dc8da5SEd Maste * modification, are permitted provided that the following conditions 1498dc8da5SEd Maste * are met: 1598dc8da5SEd Maste * 1. Redistributions of source code must retain the above copyright 1698dc8da5SEd Maste * notice, this list of conditions and the following disclaimer. 1798dc8da5SEd Maste * 2. Redistributions in binary form must reproduce the above copyright 1898dc8da5SEd Maste * notice, this list of conditions and the following disclaimer in the 1998dc8da5SEd Maste * documentation and/or other materials provided with the distribution. 2098dc8da5SEd Maste * 3. All advertising materials mentioning features or use of this software 2198dc8da5SEd Maste * must display the following acknowledgement: 2298dc8da5SEd Maste * This product includes software developed by TooLs GmbH. 2398dc8da5SEd Maste * 4. The name of TooLs GmbH may not be used to endorse or promote products 2498dc8da5SEd Maste * derived from this software without specific prior written permission. 2598dc8da5SEd Maste * 2698dc8da5SEd Maste * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2798dc8da5SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2898dc8da5SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2998dc8da5SEd Maste * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3098dc8da5SEd Maste * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 3198dc8da5SEd Maste * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3298dc8da5SEd Maste * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3398dc8da5SEd Maste * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3498dc8da5SEd Maste * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3598dc8da5SEd Maste * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3698dc8da5SEd Maste */ 3798dc8da5SEd Maste /*- 3898dc8da5SEd Maste * Written by Paul Popelka (paulp@uts.amdahl.com) 3998dc8da5SEd Maste * 4098dc8da5SEd Maste * You can do anything you want with this software, just don't say you wrote 4198dc8da5SEd Maste * it, and don't remove this notice. 4298dc8da5SEd Maste * 4398dc8da5SEd Maste * This software is provided "as is". 4498dc8da5SEd Maste * 4598dc8da5SEd Maste * The author supplies this software to be publicly redistributed on the 4698dc8da5SEd Maste * understanding that the author is not responsible for the correct 4798dc8da5SEd Maste * functioning of this software in any circumstances and is not liable for 4898dc8da5SEd Maste * any damages caused by this software. 4998dc8da5SEd Maste * 5098dc8da5SEd Maste * October 1992 5198dc8da5SEd Maste */ 5298dc8da5SEd Maste 5398dc8da5SEd Maste #include <sys/param.h> 5498dc8da5SEd Maste #include <sys/errno.h> 5598dc8da5SEd Maste 56*aaa38524SConrad Meyer #include <stdbool.h> 5798dc8da5SEd Maste #include <stdio.h> 5898dc8da5SEd Maste #include <string.h> 5998dc8da5SEd Maste 6098dc8da5SEd Maste #include "ffs/buf.h" 61476b0ab7SEd Maste #include <fs/msdosfs/bpb.h> 6298dc8da5SEd Maste #include "msdos/direntry.h" 63476b0ab7SEd Maste #include <fs/msdosfs/denode.h> 64476b0ab7SEd Maste #include <fs/msdosfs/fat.h> 65840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h> 6698dc8da5SEd Maste 6798dc8da5SEd Maste #include "makefs.h" 6898dc8da5SEd Maste #include "msdos.h" 6998dc8da5SEd Maste 7098dc8da5SEd Maste /* 7198dc8da5SEd Maste * dep - directory entry to copy into the directory 7298dc8da5SEd Maste * ddep - directory to add to 7398dc8da5SEd Maste * depp - return the address of the denode for the created directory entry 7498dc8da5SEd Maste * if depp != 0 7598dc8da5SEd Maste * cnp - componentname needed for Win95 long filenames 7698dc8da5SEd Maste */ 7798dc8da5SEd Maste int 7898dc8da5SEd Maste createde(struct denode *dep, struct denode *ddep, struct denode **depp, 7998dc8da5SEd Maste struct componentname *cnp) 8098dc8da5SEd Maste { 8198dc8da5SEd Maste int error; 8298dc8da5SEd Maste u_long dirclust, diroffset; 8398dc8da5SEd Maste struct direntry *ndep; 8498dc8da5SEd Maste struct msdosfsmount *pmp = ddep->de_pmp; 8598dc8da5SEd Maste struct buf *bp; 8698dc8da5SEd Maste daddr_t bn; 8798dc8da5SEd Maste int blsize; 8898dc8da5SEd Maste 8998dc8da5SEd Maste MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 9098dc8da5SEd Maste dep, ddep, depp, cnp)); 9198dc8da5SEd Maste 9298dc8da5SEd Maste /* 9398dc8da5SEd Maste * If no space left in the directory then allocate another cluster 9498dc8da5SEd Maste * and chain it onto the end of the file. There is one exception 9598dc8da5SEd Maste * to this. That is, if the root directory has no more space it 9698dc8da5SEd Maste * can NOT be expanded. extendfile() checks for and fails attempts 9798dc8da5SEd Maste * to extend the root directory. We just return an error in that 9898dc8da5SEd Maste * case. 9998dc8da5SEd Maste */ 10098dc8da5SEd Maste if (ddep->de_fndoffset >= ddep->de_FileSize) { 10198dc8da5SEd Maste diroffset = ddep->de_fndoffset + sizeof(struct direntry) 10298dc8da5SEd Maste - ddep->de_FileSize; 10398dc8da5SEd Maste dirclust = de_clcount(pmp, diroffset); 10498dc8da5SEd Maste error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 10598dc8da5SEd Maste if (error) { 106476b0ab7SEd Maste (void)detrunc(ddep, ddep->de_FileSize, 0, NULL); 10798dc8da5SEd Maste return error; 10898dc8da5SEd Maste } 10998dc8da5SEd Maste 11098dc8da5SEd Maste /* 11198dc8da5SEd Maste * Update the size of the directory 11298dc8da5SEd Maste */ 11398dc8da5SEd Maste ddep->de_FileSize += de_cn2off(pmp, dirclust); 11498dc8da5SEd Maste } 11598dc8da5SEd Maste 11698dc8da5SEd Maste /* 11798dc8da5SEd Maste * We just read in the cluster with space. Copy the new directory 11898dc8da5SEd Maste * entry in. Then write it to disk. NOTE: DOS directories 11998dc8da5SEd Maste * do not get smaller as clusters are emptied. 12098dc8da5SEd Maste */ 12198dc8da5SEd Maste error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 12298dc8da5SEd Maste &bn, &dirclust, &blsize); 12398dc8da5SEd Maste if (error) 12498dc8da5SEd Maste return error; 12598dc8da5SEd Maste diroffset = ddep->de_fndoffset; 12698dc8da5SEd Maste if (dirclust != MSDOSFSROOT) 12798dc8da5SEd Maste diroffset &= pmp->pm_crbomask; 12898dc8da5SEd Maste if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) { 12998dc8da5SEd Maste brelse(bp); 13098dc8da5SEd Maste return error; 13198dc8da5SEd Maste } 13298dc8da5SEd Maste ndep = bptoep(pmp, bp, ddep->de_fndoffset); 13398dc8da5SEd Maste 13498dc8da5SEd Maste DE_EXTERNALIZE(ndep, dep); 13598dc8da5SEd Maste 13698dc8da5SEd Maste /* 13798dc8da5SEd Maste * Now write the Win95 long name 13898dc8da5SEd Maste */ 13998dc8da5SEd Maste if (ddep->de_fndcnt > 0) { 14098dc8da5SEd Maste uint8_t chksum = winChksum(ndep->deName); 14198dc8da5SEd Maste const u_char *un = (const u_char *)cnp->cn_nameptr; 14298dc8da5SEd Maste int unlen = cnp->cn_namelen; 14398dc8da5SEd Maste int cnt = 1; 14498dc8da5SEd Maste 14598dc8da5SEd Maste while (--ddep->de_fndcnt >= 0) { 14698dc8da5SEd Maste if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 14798dc8da5SEd Maste if ((error = bwrite(bp)) != 0) 14898dc8da5SEd Maste return error; 14998dc8da5SEd Maste 15098dc8da5SEd Maste ddep->de_fndoffset -= sizeof(struct direntry); 15198dc8da5SEd Maste error = pcbmap(ddep, 15298dc8da5SEd Maste de_cluster(pmp, 15398dc8da5SEd Maste ddep->de_fndoffset), 15498dc8da5SEd Maste &bn, 0, &blsize); 15598dc8da5SEd Maste if (error) 15698dc8da5SEd Maste return error; 15798dc8da5SEd Maste 15898dc8da5SEd Maste error = bread(pmp->pm_devvp, bn, blsize, 15998dc8da5SEd Maste NOCRED, &bp); 16098dc8da5SEd Maste if (error) { 16198dc8da5SEd Maste brelse(bp); 16298dc8da5SEd Maste return error; 16398dc8da5SEd Maste } 16498dc8da5SEd Maste ndep = bptoep(pmp, bp, ddep->de_fndoffset); 16598dc8da5SEd Maste } else { 16698dc8da5SEd Maste ndep--; 16798dc8da5SEd Maste ddep->de_fndoffset -= sizeof(struct direntry); 16898dc8da5SEd Maste } 16998dc8da5SEd Maste if (!unix2winfn(un, unlen, (struct winentry *)ndep, 17098dc8da5SEd Maste cnt++, chksum)) 17198dc8da5SEd Maste break; 17298dc8da5SEd Maste } 17398dc8da5SEd Maste } 17498dc8da5SEd Maste 17598dc8da5SEd Maste if ((error = bwrite(bp)) != 0) 17698dc8da5SEd Maste return error; 17798dc8da5SEd Maste 17898dc8da5SEd Maste /* 17998dc8da5SEd Maste * If they want us to return with the denode gotten. 18098dc8da5SEd Maste */ 18198dc8da5SEd Maste if (depp) { 18298dc8da5SEd Maste if (dep->de_Attributes & ATTR_DIRECTORY) { 18398dc8da5SEd Maste dirclust = dep->de_StartCluster; 18498dc8da5SEd Maste if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 18598dc8da5SEd Maste dirclust = MSDOSFSROOT; 18698dc8da5SEd Maste if (dirclust == MSDOSFSROOT) 18798dc8da5SEd Maste diroffset = MSDOSFSROOT_OFS; 18898dc8da5SEd Maste else 18998dc8da5SEd Maste diroffset = 0; 19098dc8da5SEd Maste } 19198dc8da5SEd Maste return deget(pmp, dirclust, diroffset, depp); 19298dc8da5SEd Maste } 19398dc8da5SEd Maste 19498dc8da5SEd Maste return 0; 19598dc8da5SEd Maste } 19698dc8da5SEd Maste 19798dc8da5SEd Maste /* 19898dc8da5SEd Maste * Read in the disk block containing the directory entry (dirclu, dirofs) 19998dc8da5SEd Maste * and return the address of the buf header, and the address of the 20098dc8da5SEd Maste * directory entry within the block. 20198dc8da5SEd Maste */ 20298dc8da5SEd Maste int 20398dc8da5SEd Maste readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 20498dc8da5SEd Maste struct buf **bpp, struct direntry **epp) 20598dc8da5SEd Maste { 20698dc8da5SEd Maste int error; 20798dc8da5SEd Maste daddr_t bn; 20898dc8da5SEd Maste int blsize; 20998dc8da5SEd Maste 21098dc8da5SEd Maste blsize = pmp->pm_bpcluster; 21198dc8da5SEd Maste if (dirclust == MSDOSFSROOT 21298dc8da5SEd Maste && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 21398dc8da5SEd Maste blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 21498dc8da5SEd Maste bn = detobn(pmp, dirclust, diroffset); 21598dc8da5SEd Maste if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 21698dc8da5SEd Maste brelse(*bpp); 21798dc8da5SEd Maste *bpp = NULL; 21898dc8da5SEd Maste return (error); 21998dc8da5SEd Maste } 22098dc8da5SEd Maste if (epp) 22198dc8da5SEd Maste *epp = bptoep(pmp, *bpp, diroffset); 22298dc8da5SEd Maste return (0); 22398dc8da5SEd Maste } 22498dc8da5SEd Maste 22598dc8da5SEd Maste /* 22698dc8da5SEd Maste * Read in the disk block containing the directory entry dep came from and 22798dc8da5SEd Maste * return the address of the buf header, and the address of the directory 22898dc8da5SEd Maste * entry within the block. 22998dc8da5SEd Maste */ 23098dc8da5SEd Maste int 23198dc8da5SEd Maste readde(struct denode *dep, struct buf **bpp, struct direntry **epp) 23298dc8da5SEd Maste { 23398dc8da5SEd Maste 23498dc8da5SEd Maste return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 23598dc8da5SEd Maste bpp, epp)); 23698dc8da5SEd Maste } 23798dc8da5SEd Maste 23898dc8da5SEd Maste /* 23998dc8da5SEd Maste * Create a unique DOS name in dvp 24098dc8da5SEd Maste */ 24198dc8da5SEd Maste int 24298dc8da5SEd Maste uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 24398dc8da5SEd Maste { 24498dc8da5SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 24598dc8da5SEd Maste struct direntry *dentp; 24698dc8da5SEd Maste int gen; 24798dc8da5SEd Maste int blsize; 24898dc8da5SEd Maste u_long cn; 24998dc8da5SEd Maste daddr_t bn; 25098dc8da5SEd Maste struct buf *bp; 25198dc8da5SEd Maste int error; 25298dc8da5SEd Maste 25398dc8da5SEd Maste if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 25498dc8da5SEd Maste return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 25598dc8da5SEd Maste cnp->cn_namelen, 0) ? 0 : EINVAL); 25698dc8da5SEd Maste 25798dc8da5SEd Maste for (gen = 1;; gen++) { 25898dc8da5SEd Maste /* 25998dc8da5SEd Maste * Generate DOS name with generation number 26098dc8da5SEd Maste */ 26198dc8da5SEd Maste if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 26298dc8da5SEd Maste cnp->cn_namelen, gen)) 26398dc8da5SEd Maste return gen == 1 ? EINVAL : EEXIST; 26498dc8da5SEd Maste 26598dc8da5SEd Maste /* 26698dc8da5SEd Maste * Now look for a dir entry with this exact name 26798dc8da5SEd Maste */ 26898dc8da5SEd Maste for (cn = error = 0; !error; cn++) { 26998dc8da5SEd Maste if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 27098dc8da5SEd Maste if (error == E2BIG) /* EOF reached and not found */ 27198dc8da5SEd Maste return 0; 27298dc8da5SEd Maste return error; 27398dc8da5SEd Maste } 27498dc8da5SEd Maste error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 27598dc8da5SEd Maste if (error) { 27698dc8da5SEd Maste brelse(bp); 27798dc8da5SEd Maste return error; 27898dc8da5SEd Maste } 27998dc8da5SEd Maste for (dentp = (struct direntry *)bp->b_data; 2802037e988SEd Maste (char *)dentp < bp->b_data + blsize; 28198dc8da5SEd Maste dentp++) { 28298dc8da5SEd Maste if (dentp->deName[0] == SLOT_EMPTY) { 28398dc8da5SEd Maste /* 28498dc8da5SEd Maste * Last used entry and not found 28598dc8da5SEd Maste */ 28698dc8da5SEd Maste brelse(bp); 28798dc8da5SEd Maste return 0; 28898dc8da5SEd Maste } 28998dc8da5SEd Maste /* 29098dc8da5SEd Maste * Ignore volume labels and Win95 entries 29198dc8da5SEd Maste */ 29298dc8da5SEd Maste if (dentp->deAttributes & ATTR_VOLUME) 29398dc8da5SEd Maste continue; 29498dc8da5SEd Maste if (!bcmp(dentp->deName, cp, 11)) { 29598dc8da5SEd Maste error = EEXIST; 29698dc8da5SEd Maste break; 29798dc8da5SEd Maste } 29898dc8da5SEd Maste } 29998dc8da5SEd Maste brelse(bp); 30098dc8da5SEd Maste } 30198dc8da5SEd Maste } 30298dc8da5SEd Maste } 303