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 56aaa38524SConrad Meyer #include <stdbool.h> 5798dc8da5SEd Maste #include <stdio.h> 5898dc8da5SEd Maste #include <string.h> 5998dc8da5SEd Maste 60476b0ab7SEd Maste #include <fs/msdosfs/bpb.h> 6198dc8da5SEd Maste #include "msdos/direntry.h" 62476b0ab7SEd Maste #include <fs/msdosfs/denode.h> 63476b0ab7SEd Maste #include <fs/msdosfs/fat.h> 64840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h> 6598dc8da5SEd Maste 66*d485c77fSKonstantin Belousov #undef clrbuf 67*d485c77fSKonstantin Belousov #include "ffs/buf.h" 6898dc8da5SEd Maste #include "makefs.h" 6998dc8da5SEd Maste #include "msdos.h" 7098dc8da5SEd Maste 7198dc8da5SEd Maste /* 7298dc8da5SEd Maste * dep - directory entry to copy into the directory 7398dc8da5SEd Maste * ddep - directory to add to 7498dc8da5SEd Maste * depp - return the address of the denode for the created directory entry 7598dc8da5SEd Maste * if depp != 0 7698dc8da5SEd Maste * cnp - componentname needed for Win95 long filenames 7798dc8da5SEd Maste */ 7898dc8da5SEd Maste int 7998dc8da5SEd Maste createde(struct denode *dep, struct denode *ddep, struct denode **depp, 8098dc8da5SEd Maste struct componentname *cnp) 8198dc8da5SEd Maste { 8298dc8da5SEd Maste int error; 8398dc8da5SEd Maste u_long dirclust, diroffset; 8498dc8da5SEd Maste struct direntry *ndep; 8598dc8da5SEd Maste struct msdosfsmount *pmp = ddep->de_pmp; 86*d485c77fSKonstantin Belousov struct m_buf *bp; 8798dc8da5SEd Maste daddr_t bn; 8898dc8da5SEd Maste int blsize; 8998dc8da5SEd Maste 9098dc8da5SEd Maste MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 9198dc8da5SEd Maste dep, ddep, depp, cnp)); 9298dc8da5SEd Maste 9398dc8da5SEd Maste /* 9498dc8da5SEd Maste * If no space left in the directory then allocate another cluster 9598dc8da5SEd Maste * and chain it onto the end of the file. There is one exception 9698dc8da5SEd Maste * to this. That is, if the root directory has no more space it 9798dc8da5SEd Maste * can NOT be expanded. extendfile() checks for and fails attempts 9898dc8da5SEd Maste * to extend the root directory. We just return an error in that 9998dc8da5SEd Maste * case. 10098dc8da5SEd Maste */ 10198dc8da5SEd Maste if (ddep->de_fndoffset >= ddep->de_FileSize) { 10298dc8da5SEd Maste diroffset = ddep->de_fndoffset + sizeof(struct direntry) 10398dc8da5SEd Maste - ddep->de_FileSize; 10498dc8da5SEd Maste dirclust = de_clcount(pmp, diroffset); 105*d485c77fSKonstantin Belousov error = m_extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 10698dc8da5SEd Maste if (error) { 107476b0ab7SEd Maste (void)detrunc(ddep, ddep->de_FileSize, 0, NULL); 10898dc8da5SEd Maste return error; 10998dc8da5SEd Maste } 11098dc8da5SEd Maste 11198dc8da5SEd Maste /* 11298dc8da5SEd Maste * Update the size of the directory 11398dc8da5SEd Maste */ 11498dc8da5SEd Maste ddep->de_FileSize += de_cn2off(pmp, dirclust); 11598dc8da5SEd Maste } 11698dc8da5SEd Maste 11798dc8da5SEd Maste /* 11898dc8da5SEd Maste * We just read in the cluster with space. Copy the new directory 11998dc8da5SEd Maste * entry in. Then write it to disk. NOTE: DOS directories 12098dc8da5SEd Maste * do not get smaller as clusters are emptied. 12198dc8da5SEd Maste */ 12298dc8da5SEd Maste error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 12398dc8da5SEd Maste &bn, &dirclust, &blsize); 12498dc8da5SEd Maste if (error) 12598dc8da5SEd Maste return error; 12698dc8da5SEd Maste diroffset = ddep->de_fndoffset; 12798dc8da5SEd Maste if (dirclust != MSDOSFSROOT) 12898dc8da5SEd Maste diroffset &= pmp->pm_crbomask; 129*d485c77fSKonstantin Belousov if ((error = bread((void *)pmp->pm_devvp, bn, blsize, NOCRED, 130*d485c77fSKonstantin Belousov &bp)) != 0) { 13198dc8da5SEd Maste brelse(bp); 13298dc8da5SEd Maste return error; 13398dc8da5SEd Maste } 13498dc8da5SEd Maste ndep = bptoep(pmp, bp, ddep->de_fndoffset); 13598dc8da5SEd Maste 13698dc8da5SEd Maste DE_EXTERNALIZE(ndep, dep); 13798dc8da5SEd Maste 13898dc8da5SEd Maste /* 13998dc8da5SEd Maste * Now write the Win95 long name 14098dc8da5SEd Maste */ 14198dc8da5SEd Maste if (ddep->de_fndcnt > 0) { 14298dc8da5SEd Maste uint8_t chksum = winChksum(ndep->deName); 14398dc8da5SEd Maste const u_char *un = (const u_char *)cnp->cn_nameptr; 14498dc8da5SEd Maste int unlen = cnp->cn_namelen; 14598dc8da5SEd Maste int cnt = 1; 14698dc8da5SEd Maste 14798dc8da5SEd Maste while (--ddep->de_fndcnt >= 0) { 14898dc8da5SEd Maste if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 14998dc8da5SEd Maste if ((error = bwrite(bp)) != 0) 15098dc8da5SEd Maste return error; 15198dc8da5SEd Maste 15298dc8da5SEd Maste ddep->de_fndoffset -= sizeof(struct direntry); 15398dc8da5SEd Maste error = pcbmap(ddep, 15498dc8da5SEd Maste de_cluster(pmp, 15598dc8da5SEd Maste ddep->de_fndoffset), 15698dc8da5SEd Maste &bn, 0, &blsize); 15798dc8da5SEd Maste if (error) 15898dc8da5SEd Maste return error; 15998dc8da5SEd Maste 160*d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, blsize, 16198dc8da5SEd Maste NOCRED, &bp); 16298dc8da5SEd Maste if (error) { 16398dc8da5SEd Maste brelse(bp); 16498dc8da5SEd Maste return error; 16598dc8da5SEd Maste } 16698dc8da5SEd Maste ndep = bptoep(pmp, bp, ddep->de_fndoffset); 16798dc8da5SEd Maste } else { 16898dc8da5SEd Maste ndep--; 16998dc8da5SEd Maste ddep->de_fndoffset -= sizeof(struct direntry); 17098dc8da5SEd Maste } 17198dc8da5SEd Maste if (!unix2winfn(un, unlen, (struct winentry *)ndep, 17298dc8da5SEd Maste cnt++, chksum)) 17398dc8da5SEd Maste break; 17498dc8da5SEd Maste } 17598dc8da5SEd Maste } 17698dc8da5SEd Maste 17798dc8da5SEd Maste if ((error = bwrite(bp)) != 0) 17898dc8da5SEd Maste return error; 17998dc8da5SEd Maste 18098dc8da5SEd Maste /* 18198dc8da5SEd Maste * If they want us to return with the denode gotten. 18298dc8da5SEd Maste */ 18398dc8da5SEd Maste if (depp) { 18498dc8da5SEd Maste if (dep->de_Attributes & ATTR_DIRECTORY) { 18598dc8da5SEd Maste dirclust = dep->de_StartCluster; 18698dc8da5SEd Maste if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 18798dc8da5SEd Maste dirclust = MSDOSFSROOT; 18898dc8da5SEd Maste if (dirclust == MSDOSFSROOT) 18998dc8da5SEd Maste diroffset = MSDOSFSROOT_OFS; 19098dc8da5SEd Maste else 19198dc8da5SEd Maste diroffset = 0; 19298dc8da5SEd Maste } 19398dc8da5SEd Maste return deget(pmp, dirclust, diroffset, depp); 19498dc8da5SEd Maste } 19598dc8da5SEd Maste 19698dc8da5SEd Maste return 0; 19798dc8da5SEd Maste } 19898dc8da5SEd Maste 19998dc8da5SEd Maste /* 20098dc8da5SEd Maste * Read in the disk block containing the directory entry (dirclu, dirofs) 20198dc8da5SEd Maste * and return the address of the buf header, and the address of the 20298dc8da5SEd Maste * directory entry within the block. 20398dc8da5SEd Maste */ 20498dc8da5SEd Maste int 205*d485c77fSKonstantin Belousov m_readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 206*d485c77fSKonstantin Belousov struct m_buf **bpp, struct direntry **epp) 20798dc8da5SEd Maste { 20898dc8da5SEd Maste int error; 20998dc8da5SEd Maste daddr_t bn; 21098dc8da5SEd Maste int blsize; 21198dc8da5SEd Maste 21298dc8da5SEd Maste blsize = pmp->pm_bpcluster; 21398dc8da5SEd Maste if (dirclust == MSDOSFSROOT 21498dc8da5SEd Maste && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 21598dc8da5SEd Maste blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 21698dc8da5SEd Maste bn = detobn(pmp, dirclust, diroffset); 217*d485c77fSKonstantin Belousov if ((error = bread((void *)pmp->pm_devvp, bn, blsize, NOCRED, 218*d485c77fSKonstantin Belousov bpp)) != 0) { 21998dc8da5SEd Maste brelse(*bpp); 22098dc8da5SEd Maste *bpp = NULL; 22198dc8da5SEd Maste return (error); 22298dc8da5SEd Maste } 22398dc8da5SEd Maste if (epp) 22498dc8da5SEd Maste *epp = bptoep(pmp, *bpp, diroffset); 22598dc8da5SEd Maste return (0); 22698dc8da5SEd Maste } 22798dc8da5SEd Maste 22898dc8da5SEd Maste /* 22998dc8da5SEd Maste * Read in the disk block containing the directory entry dep came from and 23098dc8da5SEd Maste * return the address of the buf header, and the address of the directory 23198dc8da5SEd Maste * entry within the block. 23298dc8da5SEd Maste */ 23398dc8da5SEd Maste int 234*d485c77fSKonstantin Belousov m_readde(struct denode *dep, struct m_buf **bpp, struct direntry **epp) 23598dc8da5SEd Maste { 23698dc8da5SEd Maste 237*d485c77fSKonstantin Belousov return (m_readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 23898dc8da5SEd Maste bpp, epp)); 23998dc8da5SEd Maste } 24098dc8da5SEd Maste 24198dc8da5SEd Maste /* 24298dc8da5SEd Maste * Create a unique DOS name in dvp 24398dc8da5SEd Maste */ 24498dc8da5SEd Maste int 24598dc8da5SEd Maste uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 24698dc8da5SEd Maste { 24798dc8da5SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 24898dc8da5SEd Maste struct direntry *dentp; 24998dc8da5SEd Maste int gen; 25098dc8da5SEd Maste int blsize; 25198dc8da5SEd Maste u_long cn; 25298dc8da5SEd Maste daddr_t bn; 253*d485c77fSKonstantin Belousov struct m_buf *bp; 25498dc8da5SEd Maste int error; 25598dc8da5SEd Maste 25698dc8da5SEd Maste if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 25798dc8da5SEd Maste return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 25898dc8da5SEd Maste cnp->cn_namelen, 0) ? 0 : EINVAL); 25998dc8da5SEd Maste 26098dc8da5SEd Maste for (gen = 1;; gen++) { 26198dc8da5SEd Maste /* 26298dc8da5SEd Maste * Generate DOS name with generation number 26398dc8da5SEd Maste */ 26498dc8da5SEd Maste if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 26598dc8da5SEd Maste cnp->cn_namelen, gen)) 26698dc8da5SEd Maste return gen == 1 ? EINVAL : EEXIST; 26798dc8da5SEd Maste 26898dc8da5SEd Maste /* 26998dc8da5SEd Maste * Now look for a dir entry with this exact name 27098dc8da5SEd Maste */ 27198dc8da5SEd Maste for (cn = error = 0; !error; cn++) { 27298dc8da5SEd Maste if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 27398dc8da5SEd Maste if (error == E2BIG) /* EOF reached and not found */ 27498dc8da5SEd Maste return 0; 27598dc8da5SEd Maste return error; 27698dc8da5SEd Maste } 277*d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, blsize, 278*d485c77fSKonstantin Belousov NOCRED, &bp); 27998dc8da5SEd Maste if (error) { 28098dc8da5SEd Maste brelse(bp); 28198dc8da5SEd Maste return error; 28298dc8da5SEd Maste } 28398dc8da5SEd Maste for (dentp = (struct direntry *)bp->b_data; 2842037e988SEd Maste (char *)dentp < bp->b_data + blsize; 28598dc8da5SEd Maste dentp++) { 28698dc8da5SEd Maste if (dentp->deName[0] == SLOT_EMPTY) { 28798dc8da5SEd Maste /* 28898dc8da5SEd Maste * Last used entry and not found 28998dc8da5SEd Maste */ 29098dc8da5SEd Maste brelse(bp); 29198dc8da5SEd Maste return 0; 29298dc8da5SEd Maste } 29398dc8da5SEd Maste /* 29498dc8da5SEd Maste * Ignore volume labels and Win95 entries 29598dc8da5SEd Maste */ 29698dc8da5SEd Maste if (dentp->deAttributes & ATTR_VOLUME) 29798dc8da5SEd Maste continue; 29898dc8da5SEd Maste if (!bcmp(dentp->deName, cp, 11)) { 29998dc8da5SEd Maste error = EEXIST; 30098dc8da5SEd Maste break; 30198dc8da5SEd Maste } 30298dc8da5SEd Maste } 30398dc8da5SEd Maste brelse(bp); 30498dc8da5SEd Maste } 30598dc8da5SEd Maste } 30698dc8da5SEd Maste } 307