1c3aac50fSPeter Wemm /* $FreeBSD$ */ 2952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */ 327a0bc89SDoug Rabson 427a0bc89SDoug Rabson /*- 5d63027b6SPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 6d63027b6SPedro F. Giffuni * 7952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 8952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 927a0bc89SDoug Rabson * All rights reserved. 1027a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 1127a0bc89SDoug Rabson * 1227a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1327a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1427a0bc89SDoug Rabson * are met: 1527a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1627a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1727a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1827a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1927a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 2027a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 2127a0bc89SDoug Rabson * must display the following acknowledgement: 2227a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2327a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2427a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2527a0bc89SDoug Rabson * 2627a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2727a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2827a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2927a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3027a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 3127a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3227a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3327a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3427a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3527a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3627a0bc89SDoug Rabson */ 37d167cf6fSWarner Losh /*- 3827a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3927a0bc89SDoug Rabson * 4027a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 4127a0bc89SDoug Rabson * it, and don't remove this notice. 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * This software is provided "as is". 4427a0bc89SDoug Rabson * 4527a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4627a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4727a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4827a0bc89SDoug Rabson * any damages caused by this software. 4927a0bc89SDoug Rabson * 5027a0bc89SDoug Rabson * October 1992 5127a0bc89SDoug Rabson */ 5227a0bc89SDoug Rabson 5327a0bc89SDoug Rabson #include <sys/param.h> 54919d1ea2SPeter Wemm #include <sys/systm.h> 5527a0bc89SDoug Rabson #include <sys/buf.h> 5627a0bc89SDoug Rabson #include <sys/mount.h> 57a878a31cSBruce Evans #include <sys/namei.h> 58a878a31cSBruce Evans #include <sys/vnode.h> 5927a0bc89SDoug Rabson 601166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h> 611166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h> 621166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h> 631166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h> 64a878a31cSBruce Evans #include <fs/msdosfs/msdosfsmount.h> 6527a0bc89SDoug Rabson 66db811dd7SKonstantin Belousov int 67db811dd7SKonstantin Belousov msdosfs_lookup(struct vop_cachedlookup_args *ap) 68db811dd7SKonstantin Belousov { 69db811dd7SKonstantin Belousov 7092d4e088SKonstantin Belousov return (msdosfs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL)); 71db811dd7SKonstantin Belousov } 72db811dd7SKonstantin Belousov 73a6945216SKonstantin Belousov struct deget_dotdot { 74a6945216SKonstantin Belousov u_long cluster; 75a6945216SKonstantin Belousov int blkoff; 76a6945216SKonstantin Belousov }; 77a6945216SKonstantin Belousov 78a6945216SKonstantin Belousov static int 79a6945216SKonstantin Belousov msdosfs_deget_dotdot(struct mount *mp, void *arg, int lkflags, 80a6945216SKonstantin Belousov struct vnode **rvp) 81a6945216SKonstantin Belousov { 82a6945216SKonstantin Belousov struct deget_dotdot *dd_arg; 83a6945216SKonstantin Belousov struct denode *rdp; 84a6945216SKonstantin Belousov struct msdosfsmount *pmp; 85a6945216SKonstantin Belousov int error; 86a6945216SKonstantin Belousov 87a6945216SKonstantin Belousov pmp = VFSTOMSDOSFS(mp); 88a6945216SKonstantin Belousov dd_arg = arg; 89*ae7e8a02SKonstantin Belousov error = deget(pmp, dd_arg->cluster, dd_arg->blkoff, 90*ae7e8a02SKonstantin Belousov LK_EXCLUSIVE, &rdp); 91a6945216SKonstantin Belousov if (error == 0) 92a6945216SKonstantin Belousov *rvp = DETOV(rdp); 93a6945216SKonstantin Belousov return (error); 94a6945216SKonstantin Belousov } 95a6945216SKonstantin Belousov 9627a0bc89SDoug Rabson /* 9727a0bc89SDoug Rabson * When we search a directory the blocks containing directory entries are 9827a0bc89SDoug Rabson * read and examined. The directory entries contain information that would 9927a0bc89SDoug Rabson * normally be in the inode of a unix filesystem. This means that some of 10027a0bc89SDoug Rabson * a directory's contents may also be in memory resident denodes (sort of 10127a0bc89SDoug Rabson * an inode). This can cause problems if we are searching while some other 10227a0bc89SDoug Rabson * process is modifying a directory. To prevent one process from accessing 10327a0bc89SDoug Rabson * incompletely modified directory information we depend upon being the 104b84d2921SPoul-Henning Kamp * sole owner of a directory block. bread/brelse provide this service. 10527a0bc89SDoug Rabson * This being the case, when a process modifies a directory it must first 10627a0bc89SDoug Rabson * acquire the disk block that contains the directory entry to be modified. 10727a0bc89SDoug Rabson * Then update the disk block and the denode, and then write the disk block 10827a0bc89SDoug Rabson * out to disk. This way disk blocks containing directory entries and in 10927a0bc89SDoug Rabson * memory denode's will be in synch. 11027a0bc89SDoug Rabson */ 11192d4e088SKonstantin Belousov int 11292d4e088SKonstantin Belousov msdosfs_lookup_ino(struct vnode *vdp, struct vnode **vpp, 11323c53312SEd Maste struct componentname *cnp, uint64_t *dd_inum) 11427a0bc89SDoug Rabson { 115c2819440SBruce Evans struct mbnambuf nb; 11627a0bc89SDoug Rabson daddr_t bn; 11727a0bc89SDoug Rabson int error; 118952a6212SJordan K. Hubbard int slotcount; 119952a6212SJordan K. Hubbard int slotoffset = 0; 12027a0bc89SDoug Rabson int frcn; 12127a0bc89SDoug Rabson u_long cluster; 122952a6212SJordan K. Hubbard int blkoff; 12327a0bc89SDoug Rabson int diroff; 124952a6212SJordan K. Hubbard int blsize; 12527a0bc89SDoug Rabson int isadir; /* ~0 if found direntry is a directory */ 12627a0bc89SDoug Rabson u_long scn; /* starting cluster number */ 12727a0bc89SDoug Rabson struct vnode *pdp; 12827a0bc89SDoug Rabson struct denode *dp; 12927a0bc89SDoug Rabson struct denode *tdp; 13027a0bc89SDoug Rabson struct msdosfsmount *pmp; 131f7a3729cSKevin Lo struct buf *bp = NULL; 13227a0bc89SDoug Rabson struct direntry *dep = NULL; 133a6945216SKonstantin Belousov struct deget_dotdot dd_arg; 13427a0bc89SDoug Rabson u_char dosfilename[12]; 13527a0bc89SDoug Rabson int flags = cnp->cn_flags; 13627a0bc89SDoug Rabson int nameiop = cnp->cn_nameiop; 13769a36f24SMike Smith int unlen; 13823c53312SEd Maste uint64_t inode1; 13927a0bc89SDoug Rabson 140952a6212SJordan K. Hubbard int wincnt = 1; 1416a5bf04aSTim J. Robbins int chksum = -1, chksum_ok; 142952a6212SJordan K. Hubbard int olddos = 1; 143952a6212SJordan K. Hubbard 14427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 14527a0bc89SDoug Rabson printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr); 14627a0bc89SDoug Rabson #endif 14727a0bc89SDoug Rabson dp = VTODE(vdp); 14827a0bc89SDoug Rabson pmp = dp->de_pmp; 14927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 150952a6212SJordan K. Hubbard printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n", 15127a0bc89SDoug Rabson vdp, dp, dp->de_Attributes); 15227a0bc89SDoug Rabson #endif 15327a0bc89SDoug Rabson 154db811dd7SKonstantin Belousov restart: 15584caee6bSKonstantin Belousov if (vpp != NULL) 15684caee6bSKonstantin Belousov *vpp = NULL; 15727a0bc89SDoug Rabson /* 15827a0bc89SDoug Rabson * If they are going after the . or .. entry in the root directory, 15927a0bc89SDoug Rabson * they won't find it. DOS filesystems don't have them in the root 16027a0bc89SDoug Rabson * directory. So, we fake it. deget() is in on this scam too. 16127a0bc89SDoug Rabson */ 162e6e370a7SJeff Roberson if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' && 16327a0bc89SDoug Rabson (cnp->cn_namelen == 1 || 16427a0bc89SDoug Rabson (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) { 16527a0bc89SDoug Rabson isadir = ATTR_DIRECTORY; 16627a0bc89SDoug Rabson scn = MSDOSFSROOT; 16727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 16827a0bc89SDoug Rabson printf("msdosfs_lookup(): looking for . or .. in root directory\n"); 16927a0bc89SDoug Rabson #endif 17027a0bc89SDoug Rabson cluster = MSDOSFSROOT; 171952a6212SJordan K. Hubbard blkoff = MSDOSFSROOT_OFS; 17227a0bc89SDoug Rabson goto foundroot; 17327a0bc89SDoug Rabson } 17427a0bc89SDoug Rabson 175952a6212SJordan K. Hubbard switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename, 176c4f02a89SMax Khon cnp->cn_namelen, 0, pmp)) { 177952a6212SJordan K. Hubbard case 0: 178952a6212SJordan K. Hubbard return (EINVAL); 179952a6212SJordan K. Hubbard case 1: 180952a6212SJordan K. Hubbard break; 181952a6212SJordan K. Hubbard case 2: 182952a6212SJordan K. Hubbard wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 183c4f02a89SMax Khon cnp->cn_namelen, pmp) + 1; 184952a6212SJordan K. Hubbard break; 185952a6212SJordan K. Hubbard case 3: 186952a6212SJordan K. Hubbard olddos = 0; 187952a6212SJordan K. Hubbard wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 188c4f02a89SMax Khon cnp->cn_namelen, pmp) + 1; 189952a6212SJordan K. Hubbard break; 19027a0bc89SDoug Rabson } 191011cdb57SDmitrij Tejblum if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) { 192952a6212SJordan K. Hubbard wincnt = 1; 193011cdb57SDmitrij Tejblum olddos = 1; 194011cdb57SDmitrij Tejblum } 19569a36f24SMike Smith unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen); 19627a0bc89SDoug Rabson 197952a6212SJordan K. Hubbard /* 198952a6212SJordan K. Hubbard * Suppress search for slots unless creating 199952a6212SJordan K. Hubbard * file and at end of pathname, in which case 200952a6212SJordan K. Hubbard * we watch for a place to put the new file in 201952a6212SJordan K. Hubbard * case it doesn't already exist. 202952a6212SJordan K. Hubbard */ 203952a6212SJordan K. Hubbard slotcount = wincnt; 204952a6212SJordan K. Hubbard if ((nameiop == CREATE || nameiop == RENAME) && 205952a6212SJordan K. Hubbard (flags & ISLASTCN)) 206952a6212SJordan K. Hubbard slotcount = 0; 207952a6212SJordan K. Hubbard 20827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 209952a6212SJordan K. Hubbard printf("msdosfs_lookup(): dos version of filename %s, length %ld\n", 21027a0bc89SDoug Rabson dosfilename, cnp->cn_namelen); 21127a0bc89SDoug Rabson #endif 21227a0bc89SDoug Rabson /* 21327a0bc89SDoug Rabson * Search the directory pointed at by vdp for the name pointed at 21427a0bc89SDoug Rabson * by cnp->cn_nameptr. 21527a0bc89SDoug Rabson */ 21627a0bc89SDoug Rabson tdp = NULL; 217c2819440SBruce Evans mbnambuf_init(&nb); 21827a0bc89SDoug Rabson /* 21927a0bc89SDoug Rabson * The outer loop ranges over the clusters that make up the 22027a0bc89SDoug Rabson * directory. Note that the root directory is different from all 22127a0bc89SDoug Rabson * other directories. It has a fixed number of blocks that are not 22227a0bc89SDoug Rabson * part of the pool of allocatable clusters. So, we treat it a 22327a0bc89SDoug Rabson * little differently. The root directory starts at "cluster" 0. 22427a0bc89SDoug Rabson */ 225952a6212SJordan K. Hubbard diroff = 0; 22627a0bc89SDoug Rabson for (frcn = 0;; frcn++) { 227952a6212SJordan K. Hubbard error = pcbmap(dp, frcn, &bn, &cluster, &blsize); 228c3c6d51eSPoul-Henning Kamp if (error) { 22927a0bc89SDoug Rabson if (error == E2BIG) 23027a0bc89SDoug Rabson break; 231952a6212SJordan K. Hubbard return (error); 23227a0bc89SDoug Rabson } 233952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 234952a6212SJordan K. Hubbard if (error) { 235952a6212SJordan K. Hubbard return (error); 236952a6212SJordan K. Hubbard } 237952a6212SJordan K. Hubbard for (blkoff = 0; blkoff < blsize; 238952a6212SJordan K. Hubbard blkoff += sizeof(struct direntry), 239952a6212SJordan K. Hubbard diroff += sizeof(struct direntry)) { 240952a6212SJordan K. Hubbard dep = (struct direntry *)(bp->b_data + blkoff); 24127a0bc89SDoug Rabson /* 24227a0bc89SDoug Rabson * If the slot is empty and we are still looking 24327a0bc89SDoug Rabson * for an empty then remember this one. If the 24427a0bc89SDoug Rabson * slot is not empty then check to see if it 24527a0bc89SDoug Rabson * matches what we are looking for. If the slot 24627a0bc89SDoug Rabson * has never been filled with anything, then the 24727a0bc89SDoug Rabson * remainder of the directory has never been used, 24827a0bc89SDoug Rabson * so there is no point in searching it. 24927a0bc89SDoug Rabson */ 25027a0bc89SDoug Rabson if (dep->deName[0] == SLOT_EMPTY || 25127a0bc89SDoug Rabson dep->deName[0] == SLOT_DELETED) { 252952a6212SJordan K. Hubbard /* 253952a6212SJordan K. Hubbard * Drop memory of previous long matches 254952a6212SJordan K. Hubbard */ 255952a6212SJordan K. Hubbard chksum = -1; 256c2819440SBruce Evans mbnambuf_init(&nb); 257952a6212SJordan K. Hubbard 258952a6212SJordan K. Hubbard if (slotcount < wincnt) { 259952a6212SJordan K. Hubbard slotcount++; 26027a0bc89SDoug Rabson slotoffset = diroff; 26127a0bc89SDoug Rabson } 26227a0bc89SDoug Rabson if (dep->deName[0] == SLOT_EMPTY) { 26327a0bc89SDoug Rabson brelse(bp); 26427a0bc89SDoug Rabson goto notfound; 26527a0bc89SDoug Rabson } 26627a0bc89SDoug Rabson } else { 26727a0bc89SDoug Rabson /* 268952a6212SJordan K. Hubbard * If there wasn't enough space for our winentries, 269952a6212SJordan K. Hubbard * forget about the empty space 270952a6212SJordan K. Hubbard */ 271952a6212SJordan K. Hubbard if (slotcount < wincnt) 272952a6212SJordan K. Hubbard slotcount = 0; 273952a6212SJordan K. Hubbard 274952a6212SJordan K. Hubbard /* 275952a6212SJordan K. Hubbard * Check for Win95 long filename entry 276952a6212SJordan K. Hubbard */ 277952a6212SJordan K. Hubbard if (dep->deAttributes == ATTR_WIN95) { 278952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 279952a6212SJordan K. Hubbard continue; 280952a6212SJordan K. Hubbard 281c2819440SBruce Evans chksum = win2unixfn(&nb, 282c2819440SBruce Evans (struct winentry *)dep, chksum, 283c4f02a89SMax Khon pmp); 284c4f02a89SMax Khon continue; 285c4f02a89SMax Khon } 286c4f02a89SMax Khon 287c2819440SBruce Evans chksum = winChkName(&nb, 288c2819440SBruce Evans (const u_char *)cnp->cn_nameptr, unlen, 289c2819440SBruce Evans chksum, pmp); 290c4f02a89SMax Khon if (chksum == -2) { 291c4f02a89SMax Khon chksum = -1; 292952a6212SJordan K. Hubbard continue; 293952a6212SJordan K. Hubbard } 294952a6212SJordan K. Hubbard 295952a6212SJordan K. Hubbard /* 29627a0bc89SDoug Rabson * Ignore volume labels (anywhere, not just 29727a0bc89SDoug Rabson * the root directory). 29827a0bc89SDoug Rabson */ 299952a6212SJordan K. Hubbard if (dep->deAttributes & ATTR_VOLUME) { 300952a6212SJordan K. Hubbard chksum = -1; 301952a6212SJordan K. Hubbard continue; 302952a6212SJordan K. Hubbard } 303952a6212SJordan K. Hubbard 304952a6212SJordan K. Hubbard /* 305952a6212SJordan K. Hubbard * Check for a checksum or name match 306952a6212SJordan K. Hubbard */ 30748d1bcf8SKonstantin Belousov chksum_ok = (chksum == winChksum(dep->deName)); 3086a5bf04aSTim J. Robbins if (!chksum_ok 309952a6212SJordan K. Hubbard && (!olddos || bcmp(dosfilename, dep->deName, 11))) { 310952a6212SJordan K. Hubbard chksum = -1; 311952a6212SJordan K. Hubbard continue; 312952a6212SJordan K. Hubbard } 31327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 314952a6212SJordan K. Hubbard printf("msdosfs_lookup(): match blkoff %d, diroff %d\n", 315952a6212SJordan K. Hubbard blkoff, diroff); 31627a0bc89SDoug Rabson #endif 31727a0bc89SDoug Rabson /* 31827a0bc89SDoug Rabson * Remember where this directory 31927a0bc89SDoug Rabson * entry came from for whoever did 320952a6212SJordan K. Hubbard * this lookup. 32127a0bc89SDoug Rabson */ 32227a0bc89SDoug Rabson dp->de_fndoffset = diroff; 3236a5bf04aSTim J. Robbins if (chksum_ok && nameiop == RENAME) { 3246a5bf04aSTim J. Robbins /* 3256a5bf04aSTim J. Robbins * Target had correct long name 3266a5bf04aSTim J. Robbins * directory entries, reuse them 3276a5bf04aSTim J. Robbins * as needed. 3286a5bf04aSTim J. Robbins */ 329191e6fd0SDmitrij Tejblum dp->de_fndcnt = wincnt - 1; 3306a5bf04aSTim J. Robbins } else { 3316a5bf04aSTim J. Robbins /* 3326a5bf04aSTim J. Robbins * Long name directory entries 3336a5bf04aSTim J. Robbins * not present or corrupt, can only 3346a5bf04aSTim J. Robbins * reuse dos directory entry. 3356a5bf04aSTim J. Robbins */ 3366a5bf04aSTim J. Robbins dp->de_fndcnt = 0; 3376a5bf04aSTim J. Robbins } 338952a6212SJordan K. Hubbard 33927a0bc89SDoug Rabson goto found; 34027a0bc89SDoug Rabson } 341952a6212SJordan K. Hubbard } /* for (blkoff = 0; .... */ 34227a0bc89SDoug Rabson /* 34327a0bc89SDoug Rabson * Release the buffer holding the directory cluster just 34427a0bc89SDoug Rabson * searched. 34527a0bc89SDoug Rabson */ 34627a0bc89SDoug Rabson brelse(bp); 34727a0bc89SDoug Rabson } /* for (frcn = 0; ; frcn++) */ 348952a6212SJordan K. Hubbard 349952a6212SJordan K. Hubbard notfound: 35027a0bc89SDoug Rabson /* 35127a0bc89SDoug Rabson * We hold no disk buffers at this point. 35227a0bc89SDoug Rabson */ 35327a0bc89SDoug Rabson 35427a0bc89SDoug Rabson /* 355952a6212SJordan K. Hubbard * Fixup the slot description to point to the place where 356952a6212SJordan K. Hubbard * we might put the new DOS direntry (putting the Win95 357952a6212SJordan K. Hubbard * long name entries before that) 358952a6212SJordan K. Hubbard */ 359952a6212SJordan K. Hubbard if (!slotcount) { 360952a6212SJordan K. Hubbard slotcount = 1; 361952a6212SJordan K. Hubbard slotoffset = diroff; 362952a6212SJordan K. Hubbard } 363952a6212SJordan K. Hubbard if (wincnt > slotcount) 364952a6212SJordan K. Hubbard slotoffset += sizeof(struct direntry) * (wincnt - slotcount); 365952a6212SJordan K. Hubbard 366952a6212SJordan K. Hubbard /* 36727a0bc89SDoug Rabson * If we get here we didn't find the entry we were looking for. But 36827a0bc89SDoug Rabson * that's ok if we are creating or renaming and are at the end of 36927a0bc89SDoug Rabson * the pathname and the directory hasn't been removed. 37027a0bc89SDoug Rabson */ 37127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 372952a6212SJordan K. Hubbard printf("msdosfs_lookup(): op %d, refcnt %ld\n", 373952a6212SJordan K. Hubbard nameiop, dp->de_refcnt); 374952a6212SJordan K. Hubbard printf(" slotcount %d, slotoffset %d\n", 375952a6212SJordan K. Hubbard slotcount, slotoffset); 37627a0bc89SDoug Rabson #endif 37727a0bc89SDoug Rabson if ((nameiop == CREATE || nameiop == RENAME) && 37827a0bc89SDoug Rabson (flags & ISLASTCN) && dp->de_refcnt != 0) { 379952a6212SJordan K. Hubbard /* 380952a6212SJordan K. Hubbard * Access for write is interpreted as allowing 381952a6212SJordan K. Hubbard * creation of files in the directory. 382952a6212SJordan K. Hubbard */ 383b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 384d8762fa6SBruce Evans if (error) 385952a6212SJordan K. Hubbard return (error); 386952a6212SJordan K. Hubbard /* 387952a6212SJordan K. Hubbard * Return an indication of where the new directory 388952a6212SJordan K. Hubbard * entry should be put. 389952a6212SJordan K. Hubbard */ 39027a0bc89SDoug Rabson dp->de_fndoffset = slotoffset; 391952a6212SJordan K. Hubbard dp->de_fndcnt = wincnt - 1; 392952a6212SJordan K. Hubbard 393952a6212SJordan K. Hubbard /* 394952a6212SJordan K. Hubbard * We return with the directory locked, so that 395952a6212SJordan K. Hubbard * the parameters we set up above will still be 396952a6212SJordan K. Hubbard * valid if we actually decide to do a direnter(). 397952a6212SJordan K. Hubbard * We return ni_vp == NULL to indicate that the entry 398952a6212SJordan K. Hubbard * does not currently exist; we leave a pointer to 399952a6212SJordan K. Hubbard * the (locked) directory inode in ndp->ni_dvp. 400952a6212SJordan K. Hubbard * The pathname buffer is saved so that the name 401952a6212SJordan K. Hubbard * can be obtained later. 402952a6212SJordan K. Hubbard * 403952a6212SJordan K. Hubbard * NB - if the directory is unlocked, then this 404952a6212SJordan K. Hubbard * information cannot be used. 405952a6212SJordan K. Hubbard */ 40627a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 407952a6212SJordan K. Hubbard return (EJUSTRETURN); 40827a0bc89SDoug Rabson } 40923e8fcafSDavid Schultz #if 0 41027a0bc89SDoug Rabson /* 411952a6212SJordan K. Hubbard * Insert name into cache (as non-existent) if appropriate. 41223e8fcafSDavid Schultz * 41323e8fcafSDavid Schultz * XXX Negative caching is broken for msdosfs because the name 41423e8fcafSDavid Schultz * cache doesn't understand peculiarities such as case insensitivity 41523e8fcafSDavid Schultz * and 8.3 filenames. Hence, it may not invalidate all negative 41623e8fcafSDavid Schultz * entries if a file with this name is later created. 41727a0bc89SDoug Rabson */ 4186c21f6edSKonstantin Belousov if ((cnp->cn_flags & MAKEENTRY) != 0) 41927a0bc89SDoug Rabson cache_enter(vdp, *vpp, cnp); 42023e8fcafSDavid Schultz #endif 421952a6212SJordan K. Hubbard return (ENOENT); 42227a0bc89SDoug Rabson 423952a6212SJordan K. Hubbard found: 42427a0bc89SDoug Rabson /* 42527a0bc89SDoug Rabson * NOTE: We still have the buffer with matched directory entry at 42627a0bc89SDoug Rabson * this point. 42727a0bc89SDoug Rabson */ 42827a0bc89SDoug Rabson isadir = dep->deAttributes & ATTR_DIRECTORY; 42927a0bc89SDoug Rabson scn = getushort(dep->deStartCluster); 430952a6212SJordan K. Hubbard if (FAT32(pmp)) { 431952a6212SJordan K. Hubbard scn |= getushort(dep->deHighClust) << 16; 432952a6212SJordan K. Hubbard if (scn == pmp->pm_rootdirblk) { 433952a6212SJordan K. Hubbard /* 434952a6212SJordan K. Hubbard * There should actually be 0 here. 435952a6212SJordan K. Hubbard * Just ignore the error. 436952a6212SJordan K. Hubbard */ 437952a6212SJordan K. Hubbard scn = MSDOSFSROOT; 438952a6212SJordan K. Hubbard } 439952a6212SJordan K. Hubbard } 44027a0bc89SDoug Rabson 441952a6212SJordan K. Hubbard if (isadir) { 442952a6212SJordan K. Hubbard cluster = scn; 443952a6212SJordan K. Hubbard if (cluster == MSDOSFSROOT) 444952a6212SJordan K. Hubbard blkoff = MSDOSFSROOT_OFS; 445952a6212SJordan K. Hubbard else 446952a6212SJordan K. Hubbard blkoff = 0; 447952a6212SJordan K. Hubbard } else if (cluster == MSDOSFSROOT) 448952a6212SJordan K. Hubbard blkoff = diroff; 449952a6212SJordan K. Hubbard 450952a6212SJordan K. Hubbard /* 451952a6212SJordan K. Hubbard * Now release buf to allow deget to read the entry again. 452952a6212SJordan K. Hubbard * Reserving it here and giving it to deget could result 453952a6212SJordan K. Hubbard * in a deadlock. 454952a6212SJordan K. Hubbard */ 455952a6212SJordan K. Hubbard brelse(bp); 4560d3e502fSPedro F. Giffuni bp = NULL; 457952a6212SJordan K. Hubbard 458952a6212SJordan K. Hubbard foundroot: 45927a0bc89SDoug Rabson /* 46027a0bc89SDoug Rabson * If we entered at foundroot, then we are looking for the . or .. 46127a0bc89SDoug Rabson * entry of the filesystems root directory. isadir and scn were 462952a6212SJordan K. Hubbard * setup before jumping here. And, bp is already null. 46327a0bc89SDoug Rabson */ 464952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == MSDOSFSROOT) 465952a6212SJordan K. Hubbard scn = pmp->pm_rootdirblk; 46627a0bc89SDoug Rabson 467db811dd7SKonstantin Belousov if (dd_inum != NULL) { 468db811dd7SKonstantin Belousov *dd_inum = (uint64_t)pmp->pm_bpcluster * scn + blkoff; 469db811dd7SKonstantin Belousov return (0); 470db811dd7SKonstantin Belousov } 471db811dd7SKonstantin Belousov 47227a0bc89SDoug Rabson /* 473952a6212SJordan K. Hubbard * If deleting, and at end of pathname, return 474952a6212SJordan K. Hubbard * parameters which can be used to remove file. 47527a0bc89SDoug Rabson */ 47627a0bc89SDoug Rabson if (nameiop == DELETE && (flags & ISLASTCN)) { 477952a6212SJordan K. Hubbard /* 478952a6212SJordan K. Hubbard * Don't allow deleting the root. 479952a6212SJordan K. Hubbard */ 480952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 4819ba671deSKonstantin Belousov return (EBUSY); 482952a6212SJordan K. Hubbard 483952a6212SJordan K. Hubbard /* 484952a6212SJordan K. Hubbard * Write access to directory required to delete files. 485952a6212SJordan K. Hubbard */ 486b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 487952a6212SJordan K. Hubbard if (error) 488952a6212SJordan K. Hubbard return (error); 489952a6212SJordan K. Hubbard 490952a6212SJordan K. Hubbard /* 491952a6212SJordan K. Hubbard * Return pointer to current entry in dp->i_offset. 492952a6212SJordan K. Hubbard * Save directory inode pointer in ndp->ni_dvp for dirremove(). 493952a6212SJordan K. Hubbard */ 49427a0bc89SDoug Rabson if (dp->de_StartCluster == scn && isadir) { /* "." */ 49527a0bc89SDoug Rabson VREF(vdp); 49627a0bc89SDoug Rabson *vpp = vdp; 497952a6212SJordan K. Hubbard return (0); 49827a0bc89SDoug Rabson } 499*ae7e8a02SKonstantin Belousov error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE, &tdp); 500952a6212SJordan K. Hubbard if (error) 501952a6212SJordan K. Hubbard return (error); 50227a0bc89SDoug Rabson *vpp = DETOV(tdp); 503952a6212SJordan K. Hubbard return (0); 50427a0bc89SDoug Rabson } 50527a0bc89SDoug Rabson 50627a0bc89SDoug Rabson /* 507952a6212SJordan K. Hubbard * If rewriting (RENAME), return the inode and the 508952a6212SJordan K. Hubbard * information required to rewrite the present directory 509952a6212SJordan K. Hubbard * Must get inode of directory entry to verify it's a 510952a6212SJordan K. Hubbard * regular file, or empty directory. 51127a0bc89SDoug Rabson */ 512fcc9c112SJeff Roberson if (nameiop == RENAME && (flags & ISLASTCN)) { 513952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 5149ba671deSKonstantin Belousov return (EBUSY); 515952a6212SJordan K. Hubbard 516b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 517952a6212SJordan K. Hubbard if (error) 518952a6212SJordan K. Hubbard return (error); 519952a6212SJordan K. Hubbard 520952a6212SJordan K. Hubbard /* 521952a6212SJordan K. Hubbard * Careful about locking second inode. 522952a6212SJordan K. Hubbard * This can only occur if the target is ".". 523952a6212SJordan K. Hubbard */ 524952a6212SJordan K. Hubbard if (dp->de_StartCluster == scn && isadir) 525952a6212SJordan K. Hubbard return (EISDIR); 526952a6212SJordan K. Hubbard 527*ae7e8a02SKonstantin Belousov if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE, 528*ae7e8a02SKonstantin Belousov &tdp)) != 0) 529952a6212SJordan K. Hubbard return (error); 53027a0bc89SDoug Rabson *vpp = DETOV(tdp); 53127a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 532952a6212SJordan K. Hubbard return (0); 53327a0bc89SDoug Rabson } 53427a0bc89SDoug Rabson 53527a0bc89SDoug Rabson /* 536952a6212SJordan K. Hubbard * Step through the translation in the name. We do not `vput' the 537952a6212SJordan K. Hubbard * directory because we may need it again if a symbolic link 538952a6212SJordan K. Hubbard * is relative to the current directory. Instead we save it 539952a6212SJordan K. Hubbard * unlocked as "pdp". We must get the target inode before unlocking 540952a6212SJordan K. Hubbard * the directory to insure that the inode will not be removed 541952a6212SJordan K. Hubbard * before we get it. We prevent deadlock by always fetching 542952a6212SJordan K. Hubbard * inodes from the root, moving down the directory tree. Thus 543952a6212SJordan K. Hubbard * when following backward pointers ".." we must unlock the 544952a6212SJordan K. Hubbard * parent directory before getting the requested directory. 54527a0bc89SDoug Rabson */ 54627a0bc89SDoug Rabson pdp = vdp; 54727a0bc89SDoug Rabson if (flags & ISDOTDOT) { 548a6945216SKonstantin Belousov dd_arg.cluster = cluster; 549a6945216SKonstantin Belousov dd_arg.blkoff = blkoff; 550a6945216SKonstantin Belousov error = vn_vget_ino_gen(vdp, msdosfs_deget_dotdot, 551a6945216SKonstantin Belousov &dd_arg, cnp->cn_lkflags, vpp); 552a6945216SKonstantin Belousov if (error != 0) { 55384caee6bSKonstantin Belousov *vpp = NULL; 554952a6212SJordan K. Hubbard return (error); 55584caee6bSKonstantin Belousov } 556db811dd7SKonstantin Belousov /* 557db811dd7SKonstantin Belousov * Recheck that ".." still points to the inode we 558db811dd7SKonstantin Belousov * looked up before pdp lock was dropped. 559db811dd7SKonstantin Belousov */ 56092d4e088SKonstantin Belousov error = msdosfs_lookup_ino(pdp, NULL, cnp, &inode1); 561db811dd7SKonstantin Belousov if (error) { 562db811dd7SKonstantin Belousov vput(*vpp); 56384caee6bSKonstantin Belousov *vpp = NULL; 564db811dd7SKonstantin Belousov return (error); 565db811dd7SKonstantin Belousov } 566db811dd7SKonstantin Belousov if (VTODE(*vpp)->de_inode != inode1) { 567db811dd7SKonstantin Belousov vput(*vpp); 568db811dd7SKonstantin Belousov goto restart; 569db811dd7SKonstantin Belousov } 570952a6212SJordan K. Hubbard } else if (dp->de_StartCluster == scn && isadir) { 571952a6212SJordan K. Hubbard VREF(vdp); /* we want ourself, ie "." */ 57227a0bc89SDoug Rabson *vpp = vdp; 57327a0bc89SDoug Rabson } else { 574*ae7e8a02SKonstantin Belousov if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE, 575*ae7e8a02SKonstantin Belousov &tdp)) != 0) 576952a6212SJordan K. Hubbard return (error); 57727a0bc89SDoug Rabson *vpp = DETOV(tdp); 57827a0bc89SDoug Rabson } 57927a0bc89SDoug Rabson 58027a0bc89SDoug Rabson /* 581952a6212SJordan K. Hubbard * Insert name into cache if appropriate. 58227a0bc89SDoug Rabson */ 58327a0bc89SDoug Rabson if (cnp->cn_flags & MAKEENTRY) 58427a0bc89SDoug Rabson cache_enter(vdp, *vpp, cnp); 585952a6212SJordan K. Hubbard return (0); 58627a0bc89SDoug Rabson } 58727a0bc89SDoug Rabson 58827a0bc89SDoug Rabson /* 58927a0bc89SDoug Rabson * dep - directory entry to copy into the directory 59027a0bc89SDoug Rabson * ddep - directory to add to 59127a0bc89SDoug Rabson * depp - return the address of the denode for the created directory entry 59227a0bc89SDoug Rabson * if depp != 0 593952a6212SJordan K. Hubbard * cnp - componentname needed for Win95 long filenames 59427a0bc89SDoug Rabson */ 59527a0bc89SDoug Rabson int 59610c9700fSEd Maste createde(struct denode *dep, struct denode *ddep, struct denode **depp, 59710c9700fSEd Maste struct componentname *cnp) 59827a0bc89SDoug Rabson { 59927a0bc89SDoug Rabson int error; 60027a0bc89SDoug Rabson u_long dirclust, diroffset; 60127a0bc89SDoug Rabson struct direntry *ndep; 60227a0bc89SDoug Rabson struct msdosfsmount *pmp = ddep->de_pmp; 60327a0bc89SDoug Rabson struct buf *bp; 604952a6212SJordan K. Hubbard daddr_t bn; 605952a6212SJordan K. Hubbard int blsize; 60627a0bc89SDoug Rabson 60727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 608952a6212SJordan K. Hubbard printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 609952a6212SJordan K. Hubbard dep, ddep, depp, cnp); 61027a0bc89SDoug Rabson #endif 61127a0bc89SDoug Rabson 61227a0bc89SDoug Rabson /* 61327a0bc89SDoug Rabson * If no space left in the directory then allocate another cluster 61427a0bc89SDoug Rabson * and chain it onto the end of the file. There is one exception 61527a0bc89SDoug Rabson * to this. That is, if the root directory has no more space it 61627a0bc89SDoug Rabson * can NOT be expanded. extendfile() checks for and fails attempts 61727a0bc89SDoug Rabson * to extend the root directory. We just return an error in that 61827a0bc89SDoug Rabson * case. 61927a0bc89SDoug Rabson */ 620952a6212SJordan K. Hubbard if (ddep->de_fndoffset >= ddep->de_FileSize) { 621952a6212SJordan K. Hubbard diroffset = ddep->de_fndoffset + sizeof(struct direntry) 622952a6212SJordan K. Hubbard - ddep->de_FileSize; 623952a6212SJordan K. Hubbard dirclust = de_clcount(pmp, diroffset); 624952a6212SJordan K. Hubbard error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 625952a6212SJordan K. Hubbard if (error) { 626c52fd858SEdward Tomasz Napierala (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED); 62727a0bc89SDoug Rabson return error; 628952a6212SJordan K. Hubbard } 629952a6212SJordan K. Hubbard 63027a0bc89SDoug Rabson /* 63127a0bc89SDoug Rabson * Update the size of the directory 63227a0bc89SDoug Rabson */ 633952a6212SJordan K. Hubbard ddep->de_FileSize += de_cn2off(pmp, dirclust); 634952a6212SJordan K. Hubbard } 635952a6212SJordan K. Hubbard 63627a0bc89SDoug Rabson /* 637952a6212SJordan K. Hubbard * We just read in the cluster with space. Copy the new directory 63827a0bc89SDoug Rabson * entry in. Then write it to disk. NOTE: DOS directories 63927a0bc89SDoug Rabson * do not get smaller as clusters are emptied. 64027a0bc89SDoug Rabson */ 641952a6212SJordan K. Hubbard error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 642952a6212SJordan K. Hubbard &bn, &dirclust, &blsize); 64327a0bc89SDoug Rabson if (error) 64427a0bc89SDoug Rabson return error; 645952a6212SJordan K. Hubbard diroffset = ddep->de_fndoffset; 646952a6212SJordan K. Hubbard if (dirclust != MSDOSFSROOT) 647952a6212SJordan K. Hubbard diroffset &= pmp->pm_crbomask; 648952a6212SJordan K. Hubbard if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) { 649952a6212SJordan K. Hubbard brelse(bp); 650952a6212SJordan K. Hubbard return error; 65127a0bc89SDoug Rabson } 652952a6212SJordan K. Hubbard ndep = bptoep(pmp, bp, ddep->de_fndoffset); 653952a6212SJordan K. Hubbard 65427a0bc89SDoug Rabson DE_EXTERNALIZE(ndep, dep); 65527a0bc89SDoug Rabson 65627a0bc89SDoug Rabson /* 657952a6212SJordan K. Hubbard * Now write the Win95 long name 658952a6212SJordan K. Hubbard */ 659952a6212SJordan K. Hubbard if (ddep->de_fndcnt > 0) { 66023c53312SEd Maste uint8_t chksum = winChksum(ndep->deName); 661952a6212SJordan K. Hubbard const u_char *un = (const u_char *)cnp->cn_nameptr; 662952a6212SJordan K. Hubbard int unlen = cnp->cn_namelen; 663952a6212SJordan K. Hubbard int cnt = 1; 664952a6212SJordan K. Hubbard 665952a6212SJordan K. Hubbard while (--ddep->de_fndcnt >= 0) { 666952a6212SJordan K. Hubbard if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 6672aacee77SKonstantin Belousov if (DOINGASYNC(DETOV(ddep))) 668cb65c1eeSBruce Evans bdwrite(bp); 669cb65c1eeSBruce Evans else if ((error = bwrite(bp)) != 0) 670952a6212SJordan K. Hubbard return error; 671952a6212SJordan K. Hubbard 672952a6212SJordan K. Hubbard ddep->de_fndoffset -= sizeof(struct direntry); 673952a6212SJordan K. Hubbard error = pcbmap(ddep, 674952a6212SJordan K. Hubbard de_cluster(pmp, 675952a6212SJordan K. Hubbard ddep->de_fndoffset), 676952a6212SJordan K. Hubbard &bn, 0, &blsize); 677952a6212SJordan K. Hubbard if (error) 678952a6212SJordan K. Hubbard return error; 679952a6212SJordan K. Hubbard 680952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, 681952a6212SJordan K. Hubbard NOCRED, &bp); 682952a6212SJordan K. Hubbard if (error) { 683952a6212SJordan K. Hubbard return error; 684952a6212SJordan K. Hubbard } 685952a6212SJordan K. Hubbard ndep = bptoep(pmp, bp, ddep->de_fndoffset); 686952a6212SJordan K. Hubbard } else { 687952a6212SJordan K. Hubbard ndep--; 688952a6212SJordan K. Hubbard ddep->de_fndoffset -= sizeof(struct direntry); 689952a6212SJordan K. Hubbard } 69013df76f2SAndrey A. Chernov if (!unix2winfn(un, unlen, (struct winentry *)ndep, 691c4f02a89SMax Khon cnt++, chksum, pmp)) 692952a6212SJordan K. Hubbard break; 693952a6212SJordan K. Hubbard } 694952a6212SJordan K. Hubbard } 695952a6212SJordan K. Hubbard 6962aacee77SKonstantin Belousov if (DOINGASYNC(DETOV(ddep))) 697cb65c1eeSBruce Evans bdwrite(bp); 698cb65c1eeSBruce Evans else if ((error = bwrite(bp)) != 0) 699952a6212SJordan K. Hubbard return error; 700952a6212SJordan K. Hubbard 701952a6212SJordan K. Hubbard /* 70227a0bc89SDoug Rabson * If they want us to return with the denode gotten. 70327a0bc89SDoug Rabson */ 70427a0bc89SDoug Rabson if (depp) { 705952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) { 706952a6212SJordan K. Hubbard dirclust = dep->de_StartCluster; 707952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 708952a6212SJordan K. Hubbard dirclust = MSDOSFSROOT; 709952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT) 710952a6212SJordan K. Hubbard diroffset = MSDOSFSROOT_OFS; 711952a6212SJordan K. Hubbard else 712952a6212SJordan K. Hubbard diroffset = 0; 71327a0bc89SDoug Rabson } 714*ae7e8a02SKonstantin Belousov return (deget(pmp, dirclust, diroffset, LK_EXCLUSIVE, depp)); 71527a0bc89SDoug Rabson } 716952a6212SJordan K. Hubbard 71727a0bc89SDoug Rabson return 0; 71827a0bc89SDoug Rabson } 71927a0bc89SDoug Rabson 72027a0bc89SDoug Rabson /* 72127a0bc89SDoug Rabson * Be sure a directory is empty except for "." and "..". Return 1 if empty, 72227a0bc89SDoug Rabson * return 0 if not empty or error. 72327a0bc89SDoug Rabson */ 72427a0bc89SDoug Rabson int 72510c9700fSEd Maste dosdirempty(struct denode *dep) 72627a0bc89SDoug Rabson { 727952a6212SJordan K. Hubbard int blsize; 72827a0bc89SDoug Rabson int error; 72927a0bc89SDoug Rabson u_long cn; 73027a0bc89SDoug Rabson daddr_t bn; 73127a0bc89SDoug Rabson struct buf *bp; 73227a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 73327a0bc89SDoug Rabson struct direntry *dentp; 73427a0bc89SDoug Rabson 73527a0bc89SDoug Rabson /* 73627a0bc89SDoug Rabson * Since the filesize field in directory entries for a directory is 73727a0bc89SDoug Rabson * zero, we just have to feel our way through the directory until 73827a0bc89SDoug Rabson * we hit end of file. 73927a0bc89SDoug Rabson */ 74027a0bc89SDoug Rabson for (cn = 0;; cn++) { 741952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 74227a0bc89SDoug Rabson if (error == E2BIG) 743952a6212SJordan K. Hubbard return (1); /* it's empty */ 744952a6212SJordan K. Hubbard return (0); 745952a6212SJordan K. Hubbard } 746952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 747952a6212SJordan K. Hubbard if (error) { 748952a6212SJordan K. Hubbard return (0); 749952a6212SJordan K. Hubbard } 750952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 751952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 752952a6212SJordan K. Hubbard dentp++) { 753952a6212SJordan K. Hubbard if (dentp->deName[0] != SLOT_DELETED && 754952a6212SJordan K. Hubbard (dentp->deAttributes & ATTR_VOLUME) == 0) { 75527a0bc89SDoug Rabson /* 75627a0bc89SDoug Rabson * In dos directories an entry whose name 75727a0bc89SDoug Rabson * starts with SLOT_EMPTY (0) starts the 75827a0bc89SDoug Rabson * beginning of the unused part of the 75927a0bc89SDoug Rabson * directory, so we can just return that it 76027a0bc89SDoug Rabson * is empty. 76127a0bc89SDoug Rabson */ 76227a0bc89SDoug Rabson if (dentp->deName[0] == SLOT_EMPTY) { 76327a0bc89SDoug Rabson brelse(bp); 764952a6212SJordan K. Hubbard return (1); 76527a0bc89SDoug Rabson } 76627a0bc89SDoug Rabson /* 76727a0bc89SDoug Rabson * Any names other than "." and ".." in a 76827a0bc89SDoug Rabson * directory mean it is not empty. 76927a0bc89SDoug Rabson */ 77027a0bc89SDoug Rabson if (bcmp(dentp->deName, ". ", 11) && 77127a0bc89SDoug Rabson bcmp(dentp->deName, ".. ", 11)) { 77227a0bc89SDoug Rabson brelse(bp); 77327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 774952a6212SJordan K. Hubbard printf("dosdirempty(): entry found %02x, %02x\n", 775952a6212SJordan K. Hubbard dentp->deName[0], dentp->deName[1]); 77627a0bc89SDoug Rabson #endif 777952a6212SJordan K. Hubbard return (0); /* not empty */ 77827a0bc89SDoug Rabson } 77927a0bc89SDoug Rabson } 78027a0bc89SDoug Rabson } 78127a0bc89SDoug Rabson brelse(bp); 78227a0bc89SDoug Rabson } 78327a0bc89SDoug Rabson /* NOTREACHED */ 78427a0bc89SDoug Rabson } 78527a0bc89SDoug Rabson 78627a0bc89SDoug Rabson /* 78727a0bc89SDoug Rabson * Check to see if the directory described by target is in some 78827a0bc89SDoug Rabson * subdirectory of source. This prevents something like the following from 78927a0bc89SDoug Rabson * succeeding and leaving a bunch or files and directories orphaned. mv 79027a0bc89SDoug Rabson * /a/b/c /a/b/c/d/e/f Where c and f are directories. 79127a0bc89SDoug Rabson * 79227a0bc89SDoug Rabson * source - the inode for /a/b/c 79327a0bc89SDoug Rabson * target - the inode for /a/b/c/d/e/f 79427a0bc89SDoug Rabson * 79527a0bc89SDoug Rabson * Returns 0 if target is NOT a subdirectory of source. 79627a0bc89SDoug Rabson * Otherwise returns a non-zero error number. 79727a0bc89SDoug Rabson * The target inode is always unlocked on return. 79827a0bc89SDoug Rabson */ 79927a0bc89SDoug Rabson int 80010c9700fSEd Maste doscheckpath(struct denode *source, struct denode *target) 80127a0bc89SDoug Rabson { 80227a0bc89SDoug Rabson daddr_t scn; 80327a0bc89SDoug Rabson struct msdosfsmount *pmp; 80427a0bc89SDoug Rabson struct direntry *ep; 80527a0bc89SDoug Rabson struct denode *dep; 80627a0bc89SDoug Rabson struct buf *bp = NULL; 80727a0bc89SDoug Rabson int error = 0; 80827a0bc89SDoug Rabson 80927a0bc89SDoug Rabson dep = target; 81027a0bc89SDoug Rabson if ((target->de_Attributes & ATTR_DIRECTORY) == 0 || 81127a0bc89SDoug Rabson (source->de_Attributes & ATTR_DIRECTORY) == 0) { 81227a0bc89SDoug Rabson error = ENOTDIR; 81327a0bc89SDoug Rabson goto out; 81427a0bc89SDoug Rabson } 81527a0bc89SDoug Rabson if (dep->de_StartCluster == source->de_StartCluster) { 81627a0bc89SDoug Rabson error = EEXIST; 81727a0bc89SDoug Rabson goto out; 81827a0bc89SDoug Rabson } 81927a0bc89SDoug Rabson if (dep->de_StartCluster == MSDOSFSROOT) 82027a0bc89SDoug Rabson goto out; 821952a6212SJordan K. Hubbard pmp = dep->de_pmp; 822952a6212SJordan K. Hubbard #ifdef DIAGNOSTIC 823952a6212SJordan K. Hubbard if (pmp != source->de_pmp) 824952a6212SJordan K. Hubbard panic("doscheckpath: source and target on different filesystems"); 825952a6212SJordan K. Hubbard #endif 826952a6212SJordan K. Hubbard if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk) 827952a6212SJordan K. Hubbard goto out; 828952a6212SJordan K. Hubbard 82927a0bc89SDoug Rabson for (;;) { 83027a0bc89SDoug Rabson if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) { 83127a0bc89SDoug Rabson error = ENOTDIR; 832952a6212SJordan K. Hubbard break; 83327a0bc89SDoug Rabson } 83427a0bc89SDoug Rabson scn = dep->de_StartCluster; 83527a0bc89SDoug Rabson error = bread(pmp->pm_devvp, cntobn(pmp, scn), 83627a0bc89SDoug Rabson pmp->pm_bpcluster, NOCRED, &bp); 837952a6212SJordan K. Hubbard if (error) 83827a0bc89SDoug Rabson break; 839952a6212SJordan K. Hubbard 84027a0bc89SDoug Rabson ep = (struct direntry *) bp->b_data + 1; 84127a0bc89SDoug Rabson if ((ep->deAttributes & ATTR_DIRECTORY) == 0 || 84227a0bc89SDoug Rabson bcmp(ep->deName, ".. ", 11) != 0) { 84327a0bc89SDoug Rabson error = ENOTDIR; 84427a0bc89SDoug Rabson break; 84527a0bc89SDoug Rabson } 84627a0bc89SDoug Rabson scn = getushort(ep->deStartCluster); 847952a6212SJordan K. Hubbard if (FAT32(pmp)) 848952a6212SJordan K. Hubbard scn |= getushort(ep->deHighClust) << 16; 849952a6212SJordan K. Hubbard 85027a0bc89SDoug Rabson if (scn == source->de_StartCluster) { 85127a0bc89SDoug Rabson error = EINVAL; 85227a0bc89SDoug Rabson break; 85327a0bc89SDoug Rabson } 85427a0bc89SDoug Rabson if (scn == MSDOSFSROOT) 85527a0bc89SDoug Rabson break; 856952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == pmp->pm_rootdirblk) { 857952a6212SJordan K. Hubbard /* 858952a6212SJordan K. Hubbard * scn should be 0 in this case, 859952a6212SJordan K. Hubbard * but we silently ignore the error. 860952a6212SJordan K. Hubbard */ 861952a6212SJordan K. Hubbard break; 862952a6212SJordan K. Hubbard } 863952a6212SJordan K. Hubbard 86427a0bc89SDoug Rabson vput(DETOV(dep)); 86527a0bc89SDoug Rabson brelse(bp); 86627a0bc89SDoug Rabson bp = NULL; 867952a6212SJordan K. Hubbard /* NOTE: deget() clears dep on error */ 868*ae7e8a02SKonstantin Belousov if ((error = deget(pmp, scn, 0, LK_EXCLUSIVE, &dep)) != 0) 86927a0bc89SDoug Rabson break; 87027a0bc89SDoug Rabson } 87127a0bc89SDoug Rabson out:; 87227a0bc89SDoug Rabson if (bp) 87327a0bc89SDoug Rabson brelse(bp); 87454cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 87527a0bc89SDoug Rabson if (error == ENOTDIR) 87627a0bc89SDoug Rabson printf("doscheckpath(): .. not a directory?\n"); 87754cf9198SKonstantin Belousov #endif 87827a0bc89SDoug Rabson if (dep != NULL) 87927a0bc89SDoug Rabson vput(DETOV(dep)); 880952a6212SJordan K. Hubbard return (error); 88127a0bc89SDoug Rabson } 88227a0bc89SDoug Rabson 88327a0bc89SDoug Rabson /* 88427a0bc89SDoug Rabson * Read in the disk block containing the directory entry (dirclu, dirofs) 88527a0bc89SDoug Rabson * and return the address of the buf header, and the address of the 88627a0bc89SDoug Rabson * directory entry within the block. 88727a0bc89SDoug Rabson */ 88827a0bc89SDoug Rabson int 88910c9700fSEd Maste readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 89010c9700fSEd Maste struct buf **bpp, struct direntry **epp) 89127a0bc89SDoug Rabson { 89227a0bc89SDoug Rabson int error; 89327a0bc89SDoug Rabson daddr_t bn; 894952a6212SJordan K. Hubbard int blsize; 89527a0bc89SDoug Rabson 896952a6212SJordan K. Hubbard blsize = pmp->pm_bpcluster; 897952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT 898952a6212SJordan K. Hubbard && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 899952a6212SJordan K. Hubbard blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 900952a6212SJordan K. Hubbard bn = detobn(pmp, dirclust, diroffset); 901952a6212SJordan K. Hubbard if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 902952a6212SJordan K. Hubbard brelse(*bpp); 90327a0bc89SDoug Rabson *bpp = NULL; 904952a6212SJordan K. Hubbard return (error); 90527a0bc89SDoug Rabson } 90627a0bc89SDoug Rabson if (epp) 907952a6212SJordan K. Hubbard *epp = bptoep(pmp, *bpp, diroffset); 908952a6212SJordan K. Hubbard return (0); 90927a0bc89SDoug Rabson } 91027a0bc89SDoug Rabson 91127a0bc89SDoug Rabson /* 91227a0bc89SDoug Rabson * Read in the disk block containing the directory entry dep came from and 91327a0bc89SDoug Rabson * return the address of the buf header, and the address of the directory 91427a0bc89SDoug Rabson * entry within the block. 91527a0bc89SDoug Rabson */ 91627a0bc89SDoug Rabson int 91710c9700fSEd Maste readde(struct denode *dep, struct buf **bpp, struct direntry **epp) 91827a0bc89SDoug Rabson { 919952a6212SJordan K. Hubbard 920952a6212SJordan K. Hubbard return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 921952a6212SJordan K. Hubbard bpp, epp)); 922952a6212SJordan K. Hubbard } 923952a6212SJordan K. Hubbard 924952a6212SJordan K. Hubbard /* 925952a6212SJordan K. Hubbard * Remove a directory entry. At this point the file represented by the 926952a6212SJordan K. Hubbard * directory entry to be removed is still full length until no one has it 927952a6212SJordan K. Hubbard * open. When the file no longer being used msdosfs_inactive() is called 928952a6212SJordan K. Hubbard * and will truncate the file to 0 length. When the vnode containing the 929952a6212SJordan K. Hubbard * denode is needed for some other purpose by VFS it will call 930952a6212SJordan K. Hubbard * msdosfs_reclaim() which will remove the denode from the denode cache. 9314882501bSEd Maste * 9324882501bSEd Maste * pdep directory where the entry is removed 9334882501bSEd Maste * dep file to be removed 934952a6212SJordan K. Hubbard */ 935952a6212SJordan K. Hubbard int 93610c9700fSEd Maste removede(struct denode *pdep, struct denode *dep) 937952a6212SJordan K. Hubbard { 938952a6212SJordan K. Hubbard int error; 939952a6212SJordan K. Hubbard struct direntry *ep; 940952a6212SJordan K. Hubbard struct buf *bp; 941952a6212SJordan K. Hubbard daddr_t bn; 942952a6212SJordan K. Hubbard int blsize; 943952a6212SJordan K. Hubbard struct msdosfsmount *pmp = pdep->de_pmp; 944952a6212SJordan K. Hubbard u_long offset = pdep->de_fndoffset; 945952a6212SJordan K. Hubbard 946952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 947952a6212SJordan K. Hubbard printf("removede(): filename %s, dep %p, offset %08lx\n", 948952a6212SJordan K. Hubbard dep->de_Name, dep, offset); 949952a6212SJordan K. Hubbard #endif 950952a6212SJordan K. Hubbard 951952a6212SJordan K. Hubbard dep->de_refcnt--; 952952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 953952a6212SJordan K. Hubbard do { 954952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 955952a6212SJordan K. Hubbard error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize); 956952a6212SJordan K. Hubbard if (error) 957952a6212SJordan K. Hubbard return error; 958952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 959952a6212SJordan K. Hubbard if (error) { 960952a6212SJordan K. Hubbard return error; 961952a6212SJordan K. Hubbard } 962952a6212SJordan K. Hubbard ep = bptoep(pmp, bp, offset); 963952a6212SJordan K. Hubbard /* 964952a6212SJordan K. Hubbard * Check whether, if we came here the second time, i.e. 965952a6212SJordan K. Hubbard * when underflowing into the previous block, the last 966952a6212SJordan K. Hubbard * entry in this block is a longfilename entry, too. 967952a6212SJordan K. Hubbard */ 968952a6212SJordan K. Hubbard if (ep->deAttributes != ATTR_WIN95 969952a6212SJordan K. Hubbard && offset != pdep->de_fndoffset) { 970952a6212SJordan K. Hubbard brelse(bp); 971952a6212SJordan K. Hubbard break; 972952a6212SJordan K. Hubbard } 973952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 974952a6212SJordan K. Hubbard while (1) { 975952a6212SJordan K. Hubbard /* 976b3a15dddSPedro F. Giffuni * We are a bit aggressive here in that we delete any Win95 977952a6212SJordan K. Hubbard * entries preceding this entry, not just the ones we "own". 978952a6212SJordan K. Hubbard * Since these presumably aren't valid anyway, 979952a6212SJordan K. Hubbard * there should be no harm. 980952a6212SJordan K. Hubbard */ 981952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 982952a6212SJordan K. Hubbard ep--->deName[0] = SLOT_DELETED; 983952a6212SJordan K. Hubbard if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) 984952a6212SJordan K. Hubbard || !(offset & pmp->pm_crbomask) 985952a6212SJordan K. Hubbard || ep->deAttributes != ATTR_WIN95) 986952a6212SJordan K. Hubbard break; 987952a6212SJordan K. Hubbard } 9882aacee77SKonstantin Belousov if (DOINGASYNC(DETOV(pdep))) 989cb65c1eeSBruce Evans bdwrite(bp); 990cb65c1eeSBruce Evans else if ((error = bwrite(bp)) != 0) 991952a6212SJordan K. Hubbard return error; 992952a6212SJordan K. Hubbard } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95) 993952a6212SJordan K. Hubbard && !(offset & pmp->pm_crbomask) 994952a6212SJordan K. Hubbard && offset); 995952a6212SJordan K. Hubbard return 0; 996952a6212SJordan K. Hubbard } 997952a6212SJordan K. Hubbard 998952a6212SJordan K. Hubbard /* 999952a6212SJordan K. Hubbard * Create a unique DOS name in dvp 1000952a6212SJordan K. Hubbard */ 1001952a6212SJordan K. Hubbard int 100210c9700fSEd Maste uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp) 1003952a6212SJordan K. Hubbard { 1004952a6212SJordan K. Hubbard struct msdosfsmount *pmp = dep->de_pmp; 1005952a6212SJordan K. Hubbard struct direntry *dentp; 1006952a6212SJordan K. Hubbard int gen; 1007952a6212SJordan K. Hubbard int blsize; 1008952a6212SJordan K. Hubbard u_long cn; 1009952a6212SJordan K. Hubbard daddr_t bn; 1010952a6212SJordan K. Hubbard struct buf *bp; 1011952a6212SJordan K. Hubbard int error; 1012952a6212SJordan K. Hubbard 1013af9f1d50SDmitrij Tejblum if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 1014011cdb57SDmitrij Tejblum return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 1015c4f02a89SMax Khon cnp->cn_namelen, 0, pmp) ? 0 : EINVAL); 1016011cdb57SDmitrij Tejblum 1017952a6212SJordan K. Hubbard for (gen = 1;; gen++) { 1018952a6212SJordan K. Hubbard /* 1019952a6212SJordan K. Hubbard * Generate DOS name with generation number 1020952a6212SJordan K. Hubbard */ 1021952a6212SJordan K. Hubbard if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 1022c4f02a89SMax Khon cnp->cn_namelen, gen, pmp)) 1023952a6212SJordan K. Hubbard return gen == 1 ? EINVAL : EEXIST; 1024952a6212SJordan K. Hubbard 1025952a6212SJordan K. Hubbard /* 1026952a6212SJordan K. Hubbard * Now look for a dir entry with this exact name 1027952a6212SJordan K. Hubbard */ 1028952a6212SJordan K. Hubbard for (cn = error = 0; !error; cn++) { 1029952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 1030952a6212SJordan K. Hubbard if (error == E2BIG) /* EOF reached and not found */ 1031952a6212SJordan K. Hubbard return 0; 1032952a6212SJordan K. Hubbard return error; 1033952a6212SJordan K. Hubbard } 1034952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 1035952a6212SJordan K. Hubbard if (error) { 1036952a6212SJordan K. Hubbard return error; 1037952a6212SJordan K. Hubbard } 1038952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 1039952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 1040952a6212SJordan K. Hubbard dentp++) { 1041952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_EMPTY) { 1042952a6212SJordan K. Hubbard /* 1043952a6212SJordan K. Hubbard * Last used entry and not found 1044952a6212SJordan K. Hubbard */ 1045952a6212SJordan K. Hubbard brelse(bp); 1046952a6212SJordan K. Hubbard return 0; 1047952a6212SJordan K. Hubbard } 1048952a6212SJordan K. Hubbard /* 1049952a6212SJordan K. Hubbard * Ignore volume labels and Win95 entries 1050952a6212SJordan K. Hubbard */ 1051952a6212SJordan K. Hubbard if (dentp->deAttributes & ATTR_VOLUME) 1052952a6212SJordan K. Hubbard continue; 1053952a6212SJordan K. Hubbard if (!bcmp(dentp->deName, cp, 11)) { 1054952a6212SJordan K. Hubbard error = EEXIST; 1055952a6212SJordan K. Hubbard break; 1056952a6212SJordan K. Hubbard } 1057952a6212SJordan K. Hubbard } 1058952a6212SJordan K. Hubbard brelse(bp); 1059952a6212SJordan K. Hubbard } 1060952a6212SJordan K. Hubbard } 1061952a6212SJordan K. Hubbard } 1062