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