1af9f1d50SDmitrij Tejblum /* $Id: msdosfs_lookup.c,v 1.24 1998/05/17 21:18:08 dt Exp $ */ 2952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */ 327a0bc89SDoug Rabson 427a0bc89SDoug Rabson /*- 5952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 727a0bc89SDoug Rabson * All rights reserved. 827a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 927a0bc89SDoug Rabson * 1027a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1127a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1227a0bc89SDoug Rabson * are met: 1327a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1427a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1527a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1627a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1727a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 1827a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 1927a0bc89SDoug Rabson * must display the following acknowledgement: 2027a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2127a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2227a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2327a0bc89SDoug Rabson * 2427a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2527a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2627a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2727a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2827a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2927a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3027a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3127a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3227a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3327a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3427a0bc89SDoug Rabson */ 3527a0bc89SDoug Rabson /* 3627a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3727a0bc89SDoug Rabson * 3827a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 3927a0bc89SDoug Rabson * it, and don't remove this notice. 4027a0bc89SDoug Rabson * 4127a0bc89SDoug Rabson * This software is provided "as is". 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4427a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4527a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4627a0bc89SDoug Rabson * any damages caused by this software. 4727a0bc89SDoug Rabson * 4827a0bc89SDoug Rabson * October 1992 4927a0bc89SDoug Rabson */ 5027a0bc89SDoug Rabson 5127a0bc89SDoug Rabson #include <sys/param.h> 5227a0bc89SDoug Rabson #include <sys/namei.h> 5327a0bc89SDoug Rabson #include <sys/buf.h> 5427a0bc89SDoug Rabson #include <sys/vnode.h> 5527a0bc89SDoug Rabson #include <sys/mount.h> 56c3c6d51eSPoul-Henning Kamp #include <sys/systm.h> 5727a0bc89SDoug Rabson 5827a0bc89SDoug Rabson #include <msdosfs/bpb.h> 5927a0bc89SDoug Rabson #include <msdosfs/direntry.h> 6027a0bc89SDoug Rabson #include <msdosfs/denode.h> 6127a0bc89SDoug Rabson #include <msdosfs/msdosfsmount.h> 6227a0bc89SDoug Rabson #include <msdosfs/fat.h> 6327a0bc89SDoug Rabson 6458c27bcfSBruce Evans static int markdeleted __P((struct msdosfsmount *pmp, u_long dirclust, 6558c27bcfSBruce Evans u_long diroffset)); 6658c27bcfSBruce Evans 6727a0bc89SDoug Rabson /* 6827a0bc89SDoug Rabson * When we search a directory the blocks containing directory entries are 6927a0bc89SDoug Rabson * read and examined. The directory entries contain information that would 7027a0bc89SDoug Rabson * normally be in the inode of a unix filesystem. This means that some of 7127a0bc89SDoug Rabson * a directory's contents may also be in memory resident denodes (sort of 7227a0bc89SDoug Rabson * an inode). This can cause problems if we are searching while some other 7327a0bc89SDoug Rabson * process is modifying a directory. To prevent one process from accessing 7427a0bc89SDoug Rabson * incompletely modified directory information we depend upon being the 75b84d2921SPoul-Henning Kamp * sole owner of a directory block. bread/brelse provide this service. 7627a0bc89SDoug Rabson * This being the case, when a process modifies a directory it must first 7727a0bc89SDoug Rabson * acquire the disk block that contains the directory entry to be modified. 7827a0bc89SDoug Rabson * Then update the disk block and the denode, and then write the disk block 7927a0bc89SDoug Rabson * out to disk. This way disk blocks containing directory entries and in 8027a0bc89SDoug Rabson * memory denode's will be in synch. 8127a0bc89SDoug Rabson */ 8227a0bc89SDoug Rabson int 8327a0bc89SDoug Rabson msdosfs_lookup(ap) 840fa2443fSPoul-Henning Kamp struct vop_cachedlookup_args /* { 8527a0bc89SDoug Rabson struct vnode *a_dvp; 8627a0bc89SDoug Rabson struct vnode **a_vpp; 8727a0bc89SDoug Rabson struct componentname *a_cnp; 8827a0bc89SDoug Rabson } */ *ap; 8927a0bc89SDoug Rabson { 9027a0bc89SDoug Rabson struct vnode *vdp = ap->a_dvp; 9127a0bc89SDoug Rabson struct vnode **vpp = ap->a_vpp; 9227a0bc89SDoug Rabson struct componentname *cnp = ap->a_cnp; 9327a0bc89SDoug Rabson daddr_t bn; 9427a0bc89SDoug Rabson int error; 9527a0bc89SDoug Rabson int lockparent; 9627a0bc89SDoug Rabson int wantparent; 97952a6212SJordan K. Hubbard int slotcount; 98952a6212SJordan K. Hubbard int slotoffset = 0; 9927a0bc89SDoug Rabson int frcn; 10027a0bc89SDoug Rabson u_long cluster; 101952a6212SJordan K. Hubbard int blkoff; 10227a0bc89SDoug Rabson int diroff; 103952a6212SJordan K. Hubbard int blsize; 10427a0bc89SDoug Rabson int isadir; /* ~0 if found direntry is a directory */ 10527a0bc89SDoug Rabson u_long scn; /* starting cluster number */ 10627a0bc89SDoug Rabson struct vnode *pdp; 10727a0bc89SDoug Rabson struct denode *dp; 10827a0bc89SDoug Rabson struct denode *tdp; 10927a0bc89SDoug Rabson struct msdosfsmount *pmp; 11027a0bc89SDoug Rabson struct buf *bp = 0; 11127a0bc89SDoug Rabson struct direntry *dep = NULL; 112d8762fa6SBruce Evans struct ucred *cred = cnp->cn_cred; 11327a0bc89SDoug Rabson u_char dosfilename[12]; 11427a0bc89SDoug Rabson int flags = cnp->cn_flags; 11527a0bc89SDoug Rabson int nameiop = cnp->cn_nameiop; 116af3f60d5SBruce Evans struct proc *p = cnp->cn_proc; 11769a36f24SMike Smith int unlen; 11827a0bc89SDoug Rabson 119952a6212SJordan K. Hubbard int wincnt = 1; 120952a6212SJordan K. Hubbard int chksum = -1; 121952a6212SJordan K. Hubbard int olddos = 1; 122952a6212SJordan K. Hubbard 12327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 12427a0bc89SDoug Rabson printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr); 12527a0bc89SDoug Rabson #endif 12627a0bc89SDoug Rabson dp = VTODE(vdp); 12727a0bc89SDoug Rabson pmp = dp->de_pmp; 12827a0bc89SDoug Rabson *vpp = NULL; 12927a0bc89SDoug Rabson lockparent = flags & LOCKPARENT; 13027a0bc89SDoug Rabson wantparent = flags & (LOCKPARENT | WANTPARENT); 13127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 132952a6212SJordan K. Hubbard printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n", 13327a0bc89SDoug Rabson vdp, dp, dp->de_Attributes); 13427a0bc89SDoug Rabson #endif 13527a0bc89SDoug Rabson 13627a0bc89SDoug Rabson /* 13727a0bc89SDoug Rabson * If they are going after the . or .. entry in the root directory, 13827a0bc89SDoug Rabson * they won't find it. DOS filesystems don't have them in the root 13927a0bc89SDoug Rabson * directory. So, we fake it. deget() is in on this scam too. 14027a0bc89SDoug Rabson */ 14127a0bc89SDoug Rabson if ((vdp->v_flag & VROOT) && cnp->cn_nameptr[0] == '.' && 14227a0bc89SDoug Rabson (cnp->cn_namelen == 1 || 14327a0bc89SDoug Rabson (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) { 14427a0bc89SDoug Rabson isadir = ATTR_DIRECTORY; 14527a0bc89SDoug Rabson scn = MSDOSFSROOT; 14627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 14727a0bc89SDoug Rabson printf("msdosfs_lookup(): looking for . or .. in root directory\n"); 14827a0bc89SDoug Rabson #endif 14927a0bc89SDoug Rabson cluster = MSDOSFSROOT; 150952a6212SJordan K. Hubbard blkoff = MSDOSFSROOT_OFS; 15127a0bc89SDoug Rabson goto foundroot; 15227a0bc89SDoug Rabson } 15327a0bc89SDoug Rabson 154952a6212SJordan K. Hubbard switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename, 1557391f611SAndrey A. Chernov cnp->cn_namelen, 0, 1567391f611SAndrey A. Chernov pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d, 1577391f611SAndrey A. Chernov pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu)) { 158952a6212SJordan K. Hubbard case 0: 159952a6212SJordan K. Hubbard return (EINVAL); 160952a6212SJordan K. Hubbard case 1: 161952a6212SJordan K. Hubbard break; 162952a6212SJordan K. Hubbard case 2: 163952a6212SJordan K. Hubbard wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 164952a6212SJordan K. Hubbard cnp->cn_namelen) + 1; 165952a6212SJordan K. Hubbard break; 166952a6212SJordan K. Hubbard case 3: 167952a6212SJordan K. Hubbard olddos = 0; 168952a6212SJordan K. Hubbard wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 169952a6212SJordan K. Hubbard cnp->cn_namelen) + 1; 170952a6212SJordan K. Hubbard break; 17127a0bc89SDoug Rabson } 172011cdb57SDmitrij Tejblum if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) { 173952a6212SJordan K. Hubbard wincnt = 1; 174011cdb57SDmitrij Tejblum olddos = 1; 175011cdb57SDmitrij Tejblum } 17669a36f24SMike Smith unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen); 17727a0bc89SDoug Rabson 178952a6212SJordan K. Hubbard /* 179952a6212SJordan K. Hubbard * Suppress search for slots unless creating 180952a6212SJordan K. Hubbard * file and at end of pathname, in which case 181952a6212SJordan K. Hubbard * we watch for a place to put the new file in 182952a6212SJordan K. Hubbard * case it doesn't already exist. 183952a6212SJordan K. Hubbard */ 184952a6212SJordan K. Hubbard slotcount = wincnt; 185952a6212SJordan K. Hubbard if ((nameiop == CREATE || nameiop == RENAME) && 186952a6212SJordan K. Hubbard (flags & ISLASTCN)) 187952a6212SJordan K. Hubbard slotcount = 0; 188952a6212SJordan K. Hubbard 18927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 190952a6212SJordan K. Hubbard printf("msdosfs_lookup(): dos version of filename %s, length %ld\n", 19127a0bc89SDoug Rabson dosfilename, cnp->cn_namelen); 19227a0bc89SDoug Rabson #endif 19327a0bc89SDoug Rabson /* 19427a0bc89SDoug Rabson * Search the directory pointed at by vdp for the name pointed at 19527a0bc89SDoug Rabson * by cnp->cn_nameptr. 19627a0bc89SDoug Rabson */ 19727a0bc89SDoug Rabson tdp = NULL; 19827a0bc89SDoug Rabson /* 19927a0bc89SDoug Rabson * The outer loop ranges over the clusters that make up the 20027a0bc89SDoug Rabson * directory. Note that the root directory is different from all 20127a0bc89SDoug Rabson * other directories. It has a fixed number of blocks that are not 20227a0bc89SDoug Rabson * part of the pool of allocatable clusters. So, we treat it a 20327a0bc89SDoug Rabson * little differently. The root directory starts at "cluster" 0. 20427a0bc89SDoug Rabson */ 205952a6212SJordan K. Hubbard diroff = 0; 20627a0bc89SDoug Rabson for (frcn = 0;; frcn++) { 207952a6212SJordan K. Hubbard error = pcbmap(dp, frcn, &bn, &cluster, &blsize); 208c3c6d51eSPoul-Henning Kamp if (error) { 20927a0bc89SDoug Rabson if (error == E2BIG) 21027a0bc89SDoug Rabson break; 211952a6212SJordan K. Hubbard return (error); 21227a0bc89SDoug Rabson } 213952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 214952a6212SJordan K. Hubbard if (error) { 215952a6212SJordan K. Hubbard brelse(bp); 216952a6212SJordan K. Hubbard return (error); 217952a6212SJordan K. Hubbard } 218952a6212SJordan K. Hubbard for (blkoff = 0; blkoff < blsize; 219952a6212SJordan K. Hubbard blkoff += sizeof(struct direntry), 220952a6212SJordan K. Hubbard diroff += sizeof(struct direntry)) { 221952a6212SJordan K. Hubbard dep = (struct direntry *)(bp->b_data + blkoff); 22227a0bc89SDoug Rabson /* 22327a0bc89SDoug Rabson * If the slot is empty and we are still looking 22427a0bc89SDoug Rabson * for an empty then remember this one. If the 22527a0bc89SDoug Rabson * slot is not empty then check to see if it 22627a0bc89SDoug Rabson * matches what we are looking for. If the slot 22727a0bc89SDoug Rabson * has never been filled with anything, then the 22827a0bc89SDoug Rabson * remainder of the directory has never been used, 22927a0bc89SDoug Rabson * so there is no point in searching it. 23027a0bc89SDoug Rabson */ 23127a0bc89SDoug Rabson if (dep->deName[0] == SLOT_EMPTY || 23227a0bc89SDoug Rabson dep->deName[0] == SLOT_DELETED) { 233952a6212SJordan K. Hubbard /* 234952a6212SJordan K. Hubbard * Drop memory of previous long matches 235952a6212SJordan K. Hubbard */ 236952a6212SJordan K. Hubbard chksum = -1; 237952a6212SJordan K. Hubbard 238952a6212SJordan K. Hubbard if (slotcount < wincnt) { 239952a6212SJordan K. Hubbard slotcount++; 24027a0bc89SDoug Rabson slotoffset = diroff; 24127a0bc89SDoug Rabson } 24227a0bc89SDoug Rabson if (dep->deName[0] == SLOT_EMPTY) { 24327a0bc89SDoug Rabson brelse(bp); 24427a0bc89SDoug Rabson goto notfound; 24527a0bc89SDoug Rabson } 24627a0bc89SDoug Rabson } else { 24727a0bc89SDoug Rabson /* 248952a6212SJordan K. Hubbard * If there wasn't enough space for our winentries, 249952a6212SJordan K. Hubbard * forget about the empty space 250952a6212SJordan K. Hubbard */ 251952a6212SJordan K. Hubbard if (slotcount < wincnt) 252952a6212SJordan K. Hubbard slotcount = 0; 253952a6212SJordan K. Hubbard 254952a6212SJordan K. Hubbard /* 255952a6212SJordan K. Hubbard * Check for Win95 long filename entry 256952a6212SJordan K. Hubbard */ 257952a6212SJordan K. Hubbard if (dep->deAttributes == ATTR_WIN95) { 258952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 259952a6212SJordan K. Hubbard continue; 260952a6212SJordan K. Hubbard 261952a6212SJordan K. Hubbard chksum = winChkName((const u_char *)cnp->cn_nameptr, 26269a36f24SMike Smith unlen, 263952a6212SJordan K. Hubbard (struct winentry *)dep, 2644d63452fSAndrey A. Chernov chksum, 2657391f611SAndrey A. Chernov pmp->pm_flags & MSDOSFSMNT_U2WTABLE, 2667391f611SAndrey A. Chernov pmp->pm_u2w, 2677391f611SAndrey A. Chernov pmp->pm_flags & MSDOSFSMNT_ULTABLE, 2685114f1d7SAndrey A. Chernov pmp->pm_ul); 269952a6212SJordan K. Hubbard continue; 270952a6212SJordan K. Hubbard } 271952a6212SJordan K. Hubbard 272952a6212SJordan K. Hubbard /* 27327a0bc89SDoug Rabson * Ignore volume labels (anywhere, not just 27427a0bc89SDoug Rabson * the root directory). 27527a0bc89SDoug Rabson */ 276952a6212SJordan K. Hubbard if (dep->deAttributes & ATTR_VOLUME) { 277952a6212SJordan K. Hubbard chksum = -1; 278952a6212SJordan K. Hubbard continue; 279952a6212SJordan K. Hubbard } 280952a6212SJordan K. Hubbard 281952a6212SJordan K. Hubbard /* 282952a6212SJordan K. Hubbard * Check for a checksum or name match 283952a6212SJordan K. Hubbard */ 284952a6212SJordan K. Hubbard if (chksum != winChksum(dep->deName) 285952a6212SJordan K. Hubbard && (!olddos || bcmp(dosfilename, dep->deName, 11))) { 286952a6212SJordan K. Hubbard chksum = -1; 287952a6212SJordan K. Hubbard continue; 288952a6212SJordan K. Hubbard } 28927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 290952a6212SJordan K. Hubbard printf("msdosfs_lookup(): match blkoff %d, diroff %d\n", 291952a6212SJordan K. Hubbard blkoff, diroff); 29227a0bc89SDoug Rabson #endif 29327a0bc89SDoug Rabson /* 29427a0bc89SDoug Rabson * Remember where this directory 29527a0bc89SDoug Rabson * entry came from for whoever did 296952a6212SJordan K. Hubbard * this lookup. 29727a0bc89SDoug Rabson */ 29827a0bc89SDoug Rabson dp->de_fndoffset = diroff; 299191e6fd0SDmitrij Tejblum dp->de_fndcnt = wincnt - 1; 300952a6212SJordan K. Hubbard 30127a0bc89SDoug Rabson goto found; 30227a0bc89SDoug Rabson } 303952a6212SJordan K. Hubbard } /* for (blkoff = 0; .... */ 30427a0bc89SDoug Rabson /* 30527a0bc89SDoug Rabson * Release the buffer holding the directory cluster just 30627a0bc89SDoug Rabson * searched. 30727a0bc89SDoug Rabson */ 30827a0bc89SDoug Rabson brelse(bp); 30927a0bc89SDoug Rabson } /* for (frcn = 0; ; frcn++) */ 310952a6212SJordan K. Hubbard 311952a6212SJordan K. Hubbard notfound: 31227a0bc89SDoug Rabson /* 31327a0bc89SDoug Rabson * We hold no disk buffers at this point. 31427a0bc89SDoug Rabson */ 31527a0bc89SDoug Rabson 31627a0bc89SDoug Rabson /* 317952a6212SJordan K. Hubbard * Fixup the slot description to point to the place where 318952a6212SJordan K. Hubbard * we might put the new DOS direntry (putting the Win95 319952a6212SJordan K. Hubbard * long name entries before that) 320952a6212SJordan K. Hubbard */ 321952a6212SJordan K. Hubbard if (!slotcount) { 322952a6212SJordan K. Hubbard slotcount = 1; 323952a6212SJordan K. Hubbard slotoffset = diroff; 324952a6212SJordan K. Hubbard } 325952a6212SJordan K. Hubbard if (wincnt > slotcount) 326952a6212SJordan K. Hubbard slotoffset += sizeof(struct direntry) * (wincnt - slotcount); 327952a6212SJordan K. Hubbard 328952a6212SJordan K. Hubbard /* 32927a0bc89SDoug Rabson * If we get here we didn't find the entry we were looking for. But 33027a0bc89SDoug Rabson * that's ok if we are creating or renaming and are at the end of 33127a0bc89SDoug Rabson * the pathname and the directory hasn't been removed. 33227a0bc89SDoug Rabson */ 33327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 334952a6212SJordan K. Hubbard printf("msdosfs_lookup(): op %d, refcnt %ld\n", 335952a6212SJordan K. Hubbard nameiop, dp->de_refcnt); 336952a6212SJordan K. Hubbard printf(" slotcount %d, slotoffset %d\n", 337952a6212SJordan K. Hubbard slotcount, slotoffset); 33827a0bc89SDoug Rabson #endif 33927a0bc89SDoug Rabson if ((nameiop == CREATE || nameiop == RENAME) && 34027a0bc89SDoug Rabson (flags & ISLASTCN) && dp->de_refcnt != 0) { 341952a6212SJordan K. Hubbard /* 342952a6212SJordan K. Hubbard * Access for write is interpreted as allowing 343952a6212SJordan K. Hubbard * creation of files in the directory. 344952a6212SJordan K. Hubbard */ 345952a6212SJordan K. Hubbard error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc); 346d8762fa6SBruce Evans if (error) 347952a6212SJordan K. Hubbard return (error); 348952a6212SJordan K. Hubbard /* 349952a6212SJordan K. Hubbard * Return an indication of where the new directory 350952a6212SJordan K. Hubbard * entry should be put. 351952a6212SJordan K. Hubbard */ 35227a0bc89SDoug Rabson dp->de_fndoffset = slotoffset; 353952a6212SJordan K. Hubbard dp->de_fndcnt = wincnt - 1; 354952a6212SJordan K. Hubbard 355952a6212SJordan K. Hubbard /* 356952a6212SJordan K. Hubbard * We return with the directory locked, so that 357952a6212SJordan K. Hubbard * the parameters we set up above will still be 358952a6212SJordan K. Hubbard * valid if we actually decide to do a direnter(). 359952a6212SJordan K. Hubbard * We return ni_vp == NULL to indicate that the entry 360952a6212SJordan K. Hubbard * does not currently exist; we leave a pointer to 361952a6212SJordan K. Hubbard * the (locked) directory inode in ndp->ni_dvp. 362952a6212SJordan K. Hubbard * The pathname buffer is saved so that the name 363952a6212SJordan K. Hubbard * can be obtained later. 364952a6212SJordan K. Hubbard * 365952a6212SJordan K. Hubbard * NB - if the directory is unlocked, then this 366952a6212SJordan K. Hubbard * information cannot be used. 367952a6212SJordan K. Hubbard */ 36827a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 369952a6212SJordan K. Hubbard if (!lockparent) 370af3f60d5SBruce Evans VOP_UNLOCK(vdp, 0, p); 371952a6212SJordan K. Hubbard return (EJUSTRETURN); 37227a0bc89SDoug Rabson } 37327a0bc89SDoug Rabson /* 374952a6212SJordan K. Hubbard * Insert name into cache (as non-existent) if appropriate. 37527a0bc89SDoug Rabson */ 37627a0bc89SDoug Rabson if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 37727a0bc89SDoug Rabson cache_enter(vdp, *vpp, cnp); 378952a6212SJordan K. Hubbard return (ENOENT); 37927a0bc89SDoug Rabson 380952a6212SJordan K. Hubbard found: 38127a0bc89SDoug Rabson /* 38227a0bc89SDoug Rabson * NOTE: We still have the buffer with matched directory entry at 38327a0bc89SDoug Rabson * this point. 38427a0bc89SDoug Rabson */ 38527a0bc89SDoug Rabson isadir = dep->deAttributes & ATTR_DIRECTORY; 38627a0bc89SDoug Rabson scn = getushort(dep->deStartCluster); 387952a6212SJordan K. Hubbard if (FAT32(pmp)) { 388952a6212SJordan K. Hubbard scn |= getushort(dep->deHighClust) << 16; 389952a6212SJordan K. Hubbard if (scn == pmp->pm_rootdirblk) { 390952a6212SJordan K. Hubbard /* 391952a6212SJordan K. Hubbard * There should actually be 0 here. 392952a6212SJordan K. Hubbard * Just ignore the error. 393952a6212SJordan K. Hubbard */ 394952a6212SJordan K. Hubbard scn = MSDOSFSROOT; 395952a6212SJordan K. Hubbard } 396952a6212SJordan K. Hubbard } 39727a0bc89SDoug Rabson 398952a6212SJordan K. Hubbard if (isadir) { 399952a6212SJordan K. Hubbard cluster = scn; 400952a6212SJordan K. Hubbard if (cluster == MSDOSFSROOT) 401952a6212SJordan K. Hubbard blkoff = MSDOSFSROOT_OFS; 402952a6212SJordan K. Hubbard else 403952a6212SJordan K. Hubbard blkoff = 0; 404952a6212SJordan K. Hubbard } else if (cluster == MSDOSFSROOT) 405952a6212SJordan K. Hubbard blkoff = diroff; 406952a6212SJordan K. Hubbard 407952a6212SJordan K. Hubbard /* 408952a6212SJordan K. Hubbard * Now release buf to allow deget to read the entry again. 409952a6212SJordan K. Hubbard * Reserving it here and giving it to deget could result 410952a6212SJordan K. Hubbard * in a deadlock. 411952a6212SJordan K. Hubbard */ 412952a6212SJordan K. Hubbard brelse(bp); 413952a6212SJordan K. Hubbard bp = 0; 414952a6212SJordan K. Hubbard 415952a6212SJordan K. Hubbard foundroot: 41627a0bc89SDoug Rabson /* 41727a0bc89SDoug Rabson * If we entered at foundroot, then we are looking for the . or .. 41827a0bc89SDoug Rabson * entry of the filesystems root directory. isadir and scn were 419952a6212SJordan K. Hubbard * setup before jumping here. And, bp is already null. 42027a0bc89SDoug Rabson */ 421952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == MSDOSFSROOT) 422952a6212SJordan K. Hubbard scn = pmp->pm_rootdirblk; 42327a0bc89SDoug Rabson 42427a0bc89SDoug Rabson /* 425952a6212SJordan K. Hubbard * If deleting, and at end of pathname, return 426952a6212SJordan K. Hubbard * parameters which can be used to remove file. 427952a6212SJordan K. Hubbard * If the wantparent flag isn't set, we return only 428952a6212SJordan K. Hubbard * the directory (in ndp->ni_dvp), otherwise we go 429952a6212SJordan K. Hubbard * on and lock the inode, being careful with ".". 43027a0bc89SDoug Rabson */ 43127a0bc89SDoug Rabson if (nameiop == DELETE && (flags & ISLASTCN)) { 432952a6212SJordan K. Hubbard /* 433952a6212SJordan K. Hubbard * Don't allow deleting the root. 434952a6212SJordan K. Hubbard */ 435952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 436952a6212SJordan K. Hubbard return EROFS; /* really? XXX */ 437952a6212SJordan K. Hubbard 438952a6212SJordan K. Hubbard /* 439952a6212SJordan K. Hubbard * Write access to directory required to delete files. 440952a6212SJordan K. Hubbard */ 441952a6212SJordan K. Hubbard error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc); 442952a6212SJordan K. Hubbard if (error) 443952a6212SJordan K. Hubbard return (error); 444952a6212SJordan K. Hubbard 445952a6212SJordan K. Hubbard /* 446952a6212SJordan K. Hubbard * Return pointer to current entry in dp->i_offset. 447952a6212SJordan K. Hubbard * Save directory inode pointer in ndp->ni_dvp for dirremove(). 448952a6212SJordan K. Hubbard */ 44927a0bc89SDoug Rabson if (dp->de_StartCluster == scn && isadir) { /* "." */ 45027a0bc89SDoug Rabson VREF(vdp); 45127a0bc89SDoug Rabson *vpp = vdp; 452952a6212SJordan K. Hubbard return (0); 45327a0bc89SDoug Rabson } 454952a6212SJordan K. Hubbard error = deget(pmp, cluster, blkoff, &tdp); 455952a6212SJordan K. Hubbard if (error) 456952a6212SJordan K. Hubbard return (error); 45727a0bc89SDoug Rabson *vpp = DETOV(tdp); 45827a0bc89SDoug Rabson if (!lockparent) 459af3f60d5SBruce Evans VOP_UNLOCK(vdp, 0, p); 460952a6212SJordan K. Hubbard return (0); 46127a0bc89SDoug Rabson } 46227a0bc89SDoug Rabson 46327a0bc89SDoug Rabson /* 464952a6212SJordan K. Hubbard * If rewriting (RENAME), return the inode and the 465952a6212SJordan K. Hubbard * information required to rewrite the present directory 466952a6212SJordan K. Hubbard * Must get inode of directory entry to verify it's a 467952a6212SJordan K. Hubbard * regular file, or empty directory. 46827a0bc89SDoug Rabson */ 469952a6212SJordan K. Hubbard if (nameiop == RENAME && wantparent && 470952a6212SJordan K. Hubbard (flags & ISLASTCN)) { 471952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 472952a6212SJordan K. Hubbard return EROFS; /* really? XXX */ 473952a6212SJordan K. Hubbard 474952a6212SJordan K. Hubbard error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_proc); 475952a6212SJordan K. Hubbard if (error) 476952a6212SJordan K. Hubbard return (error); 477952a6212SJordan K. Hubbard 478952a6212SJordan K. Hubbard /* 479952a6212SJordan K. Hubbard * Careful about locking second inode. 480952a6212SJordan K. Hubbard * This can only occur if the target is ".". 481952a6212SJordan K. Hubbard */ 482952a6212SJordan K. Hubbard if (dp->de_StartCluster == scn && isadir) 483952a6212SJordan K. Hubbard return (EISDIR); 484952a6212SJordan K. Hubbard 485952a6212SJordan K. Hubbard if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 486952a6212SJordan K. Hubbard return (error); 48727a0bc89SDoug Rabson *vpp = DETOV(tdp); 48827a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 48927a0bc89SDoug Rabson if (!lockparent) 490af3f60d5SBruce Evans VOP_UNLOCK(vdp, 0, p); 491952a6212SJordan K. Hubbard return (0); 49227a0bc89SDoug Rabson } 49327a0bc89SDoug Rabson 49427a0bc89SDoug Rabson /* 495952a6212SJordan K. Hubbard * Step through the translation in the name. We do not `vput' the 496952a6212SJordan K. Hubbard * directory because we may need it again if a symbolic link 497952a6212SJordan K. Hubbard * is relative to the current directory. Instead we save it 498952a6212SJordan K. Hubbard * unlocked as "pdp". We must get the target inode before unlocking 499952a6212SJordan K. Hubbard * the directory to insure that the inode will not be removed 500952a6212SJordan K. Hubbard * before we get it. We prevent deadlock by always fetching 501952a6212SJordan K. Hubbard * inodes from the root, moving down the directory tree. Thus 502952a6212SJordan K. Hubbard * when following backward pointers ".." we must unlock the 503952a6212SJordan K. Hubbard * parent directory before getting the requested directory. 504952a6212SJordan K. Hubbard * There is a potential race condition here if both the current 505952a6212SJordan K. Hubbard * and parent directories are removed before the VFS_VGET for the 506952a6212SJordan K. Hubbard * inode associated with ".." returns. We hope that this occurs 507952a6212SJordan K. Hubbard * infrequently since we cannot avoid this race condition without 508952a6212SJordan K. Hubbard * implementing a sophisticated deadlock detection algorithm. 509952a6212SJordan K. Hubbard * Note also that this simple deadlock detection scheme will not 510952a6212SJordan K. Hubbard * work if the file system has any hard links other than ".." 511952a6212SJordan K. Hubbard * that point backwards in the directory structure. 51227a0bc89SDoug Rabson */ 51327a0bc89SDoug Rabson pdp = vdp; 51427a0bc89SDoug Rabson if (flags & ISDOTDOT) { 515af3f60d5SBruce Evans VOP_UNLOCK(pdp, 0, p); 516952a6212SJordan K. Hubbard error = deget(pmp, cluster, blkoff, &tdp); 51727a0bc89SDoug Rabson if (error) { 518af3f60d5SBruce Evans vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p); 519952a6212SJordan K. Hubbard return (error); 52027a0bc89SDoug Rabson } 521952a6212SJordan K. Hubbard if (lockparent && (flags & ISLASTCN) && 522952a6212SJordan K. Hubbard (error = vn_lock(pdp, LK_EXCLUSIVE, p))) { 52327a0bc89SDoug Rabson vput(DETOV(tdp)); 524952a6212SJordan K. Hubbard return (error); 52527a0bc89SDoug Rabson } 52627a0bc89SDoug Rabson *vpp = DETOV(tdp); 527952a6212SJordan K. Hubbard } else if (dp->de_StartCluster == scn && isadir) { 528952a6212SJordan K. Hubbard VREF(vdp); /* we want ourself, ie "." */ 52927a0bc89SDoug Rabson *vpp = vdp; 53027a0bc89SDoug Rabson } else { 531952a6212SJordan K. Hubbard if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 532952a6212SJordan K. Hubbard return (error); 53327a0bc89SDoug Rabson if (!lockparent || !(flags & ISLASTCN)) 534af3f60d5SBruce Evans VOP_UNLOCK(pdp, 0, p); 53527a0bc89SDoug Rabson *vpp = DETOV(tdp); 53627a0bc89SDoug Rabson } 53727a0bc89SDoug Rabson 53827a0bc89SDoug Rabson /* 539952a6212SJordan K. Hubbard * Insert name into cache if appropriate. 54027a0bc89SDoug Rabson */ 54127a0bc89SDoug Rabson if (cnp->cn_flags & MAKEENTRY) 54227a0bc89SDoug Rabson cache_enter(vdp, *vpp, cnp); 543952a6212SJordan K. Hubbard return (0); 54427a0bc89SDoug Rabson } 54527a0bc89SDoug Rabson 54627a0bc89SDoug Rabson /* 54727a0bc89SDoug Rabson * dep - directory entry to copy into the directory 54827a0bc89SDoug Rabson * ddep - directory to add to 54927a0bc89SDoug Rabson * depp - return the address of the denode for the created directory entry 55027a0bc89SDoug Rabson * if depp != 0 551952a6212SJordan K. Hubbard * cnp - componentname needed for Win95 long filenames 55227a0bc89SDoug Rabson */ 55327a0bc89SDoug Rabson int 554952a6212SJordan K. Hubbard createde(dep, ddep, depp, cnp) 55527a0bc89SDoug Rabson struct denode *dep; 55627a0bc89SDoug Rabson struct denode *ddep; 55727a0bc89SDoug Rabson struct denode **depp; 558952a6212SJordan K. Hubbard struct componentname *cnp; 55927a0bc89SDoug Rabson { 56027a0bc89SDoug Rabson int error; 56127a0bc89SDoug Rabson u_long dirclust, diroffset; 56227a0bc89SDoug Rabson struct direntry *ndep; 56327a0bc89SDoug Rabson struct msdosfsmount *pmp = ddep->de_pmp; 56427a0bc89SDoug Rabson struct buf *bp; 565952a6212SJordan K. Hubbard daddr_t bn; 566952a6212SJordan K. Hubbard int blsize; 56727a0bc89SDoug Rabson 56827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 569952a6212SJordan K. Hubbard printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 570952a6212SJordan K. Hubbard dep, ddep, depp, cnp); 57127a0bc89SDoug Rabson #endif 57227a0bc89SDoug Rabson 57327a0bc89SDoug Rabson /* 57427a0bc89SDoug Rabson * If no space left in the directory then allocate another cluster 57527a0bc89SDoug Rabson * and chain it onto the end of the file. There is one exception 57627a0bc89SDoug Rabson * to this. That is, if the root directory has no more space it 57727a0bc89SDoug Rabson * can NOT be expanded. extendfile() checks for and fails attempts 57827a0bc89SDoug Rabson * to extend the root directory. We just return an error in that 57927a0bc89SDoug Rabson * case. 58027a0bc89SDoug Rabson */ 581952a6212SJordan K. Hubbard if (ddep->de_fndoffset >= ddep->de_FileSize) { 582952a6212SJordan K. Hubbard diroffset = ddep->de_fndoffset + sizeof(struct direntry) 583952a6212SJordan K. Hubbard - ddep->de_FileSize; 584952a6212SJordan K. Hubbard dirclust = de_clcount(pmp, diroffset); 585952a6212SJordan K. Hubbard error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 586952a6212SJordan K. Hubbard if (error) { 587952a6212SJordan K. Hubbard (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED, NULL); 58827a0bc89SDoug Rabson return error; 589952a6212SJordan K. Hubbard } 590952a6212SJordan K. Hubbard 59127a0bc89SDoug Rabson /* 59227a0bc89SDoug Rabson * Update the size of the directory 59327a0bc89SDoug Rabson */ 594952a6212SJordan K. Hubbard ddep->de_FileSize += de_cn2off(pmp, dirclust); 595952a6212SJordan K. Hubbard } 596952a6212SJordan K. Hubbard 59727a0bc89SDoug Rabson /* 598952a6212SJordan K. Hubbard * We just read in the cluster with space. Copy the new directory 59927a0bc89SDoug Rabson * entry in. Then write it to disk. NOTE: DOS directories 60027a0bc89SDoug Rabson * do not get smaller as clusters are emptied. 60127a0bc89SDoug Rabson */ 602952a6212SJordan K. Hubbard error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 603952a6212SJordan K. Hubbard &bn, &dirclust, &blsize); 60427a0bc89SDoug Rabson if (error) 60527a0bc89SDoug Rabson return error; 606952a6212SJordan K. Hubbard diroffset = ddep->de_fndoffset; 607952a6212SJordan K. Hubbard if (dirclust != MSDOSFSROOT) 608952a6212SJordan K. Hubbard diroffset &= pmp->pm_crbomask; 609952a6212SJordan K. Hubbard if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) { 610952a6212SJordan K. Hubbard brelse(bp); 611952a6212SJordan K. Hubbard return error; 61227a0bc89SDoug Rabson } 613952a6212SJordan K. Hubbard ndep = bptoep(pmp, bp, ddep->de_fndoffset); 614952a6212SJordan K. Hubbard 61527a0bc89SDoug Rabson DE_EXTERNALIZE(ndep, dep); 61627a0bc89SDoug Rabson 61727a0bc89SDoug Rabson /* 618952a6212SJordan K. Hubbard * Now write the Win95 long name 619952a6212SJordan K. Hubbard */ 620952a6212SJordan K. Hubbard if (ddep->de_fndcnt > 0) { 621952a6212SJordan K. Hubbard u_int8_t chksum = winChksum(ndep->deName); 622952a6212SJordan K. Hubbard const u_char *un = (const u_char *)cnp->cn_nameptr; 623952a6212SJordan K. Hubbard int unlen = cnp->cn_namelen; 624952a6212SJordan K. Hubbard int cnt = 1; 625952a6212SJordan K. Hubbard 626952a6212SJordan K. Hubbard while (--ddep->de_fndcnt >= 0) { 627952a6212SJordan K. Hubbard if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 628952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 629952a6212SJordan K. Hubbard return error; 630952a6212SJordan K. Hubbard 631952a6212SJordan K. Hubbard ddep->de_fndoffset -= sizeof(struct direntry); 632952a6212SJordan K. Hubbard error = pcbmap(ddep, 633952a6212SJordan K. Hubbard de_cluster(pmp, 634952a6212SJordan K. Hubbard ddep->de_fndoffset), 635952a6212SJordan K. Hubbard &bn, 0, &blsize); 636952a6212SJordan K. Hubbard if (error) 637952a6212SJordan K. Hubbard return error; 638952a6212SJordan K. Hubbard 639952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, 640952a6212SJordan K. Hubbard NOCRED, &bp); 641952a6212SJordan K. Hubbard if (error) { 642952a6212SJordan K. Hubbard brelse(bp); 643952a6212SJordan K. Hubbard return error; 644952a6212SJordan K. Hubbard } 645952a6212SJordan K. Hubbard ndep = bptoep(pmp, bp, ddep->de_fndoffset); 646952a6212SJordan K. Hubbard } else { 647952a6212SJordan K. Hubbard ndep--; 648952a6212SJordan K. Hubbard ddep->de_fndoffset -= sizeof(struct direntry); 649952a6212SJordan K. Hubbard } 65013df76f2SAndrey A. Chernov if (!unix2winfn(un, unlen, (struct winentry *)ndep, 65113df76f2SAndrey A. Chernov cnt++, chksum, 6527391f611SAndrey A. Chernov pmp->pm_flags & MSDOSFSMNT_U2WTABLE, 6537391f611SAndrey A. Chernov pmp->pm_u2w)) 654952a6212SJordan K. Hubbard break; 655952a6212SJordan K. Hubbard } 656952a6212SJordan K. Hubbard } 657952a6212SJordan K. Hubbard 658952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 659952a6212SJordan K. Hubbard return error; 660952a6212SJordan K. Hubbard 661952a6212SJordan K. Hubbard /* 66227a0bc89SDoug Rabson * If they want us to return with the denode gotten. 66327a0bc89SDoug Rabson */ 66427a0bc89SDoug Rabson if (depp) { 665952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) { 666952a6212SJordan K. Hubbard dirclust = dep->de_StartCluster; 667952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 668952a6212SJordan K. Hubbard dirclust = MSDOSFSROOT; 669952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT) 670952a6212SJordan K. Hubbard diroffset = MSDOSFSROOT_OFS; 671952a6212SJordan K. Hubbard else 672952a6212SJordan K. Hubbard diroffset = 0; 67327a0bc89SDoug Rabson } 674952a6212SJordan K. Hubbard return deget(pmp, dirclust, diroffset, depp); 67527a0bc89SDoug Rabson } 676952a6212SJordan K. Hubbard 67727a0bc89SDoug Rabson return 0; 67827a0bc89SDoug Rabson } 67927a0bc89SDoug Rabson 68027a0bc89SDoug Rabson /* 68127a0bc89SDoug Rabson * Be sure a directory is empty except for "." and "..". Return 1 if empty, 68227a0bc89SDoug Rabson * return 0 if not empty or error. 68327a0bc89SDoug Rabson */ 68427a0bc89SDoug Rabson int 68527a0bc89SDoug Rabson dosdirempty(dep) 68627a0bc89SDoug Rabson struct denode *dep; 68727a0bc89SDoug Rabson { 688952a6212SJordan K. Hubbard int blsize; 68927a0bc89SDoug Rabson int error; 69027a0bc89SDoug Rabson u_long cn; 69127a0bc89SDoug Rabson daddr_t bn; 69227a0bc89SDoug Rabson struct buf *bp; 69327a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 69427a0bc89SDoug Rabson struct direntry *dentp; 69527a0bc89SDoug Rabson 69627a0bc89SDoug Rabson /* 69727a0bc89SDoug Rabson * Since the filesize field in directory entries for a directory is 69827a0bc89SDoug Rabson * zero, we just have to feel our way through the directory until 69927a0bc89SDoug Rabson * we hit end of file. 70027a0bc89SDoug Rabson */ 70127a0bc89SDoug Rabson for (cn = 0;; cn++) { 702952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 70327a0bc89SDoug Rabson if (error == E2BIG) 704952a6212SJordan K. Hubbard return (1); /* it's empty */ 705952a6212SJordan K. Hubbard return (0); 706952a6212SJordan K. Hubbard } 707952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 708952a6212SJordan K. Hubbard if (error) { 709952a6212SJordan K. Hubbard brelse(bp); 710952a6212SJordan K. Hubbard return (0); 711952a6212SJordan K. Hubbard } 712952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 713952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 714952a6212SJordan K. Hubbard dentp++) { 715952a6212SJordan K. Hubbard if (dentp->deName[0] != SLOT_DELETED && 716952a6212SJordan K. Hubbard (dentp->deAttributes & ATTR_VOLUME) == 0) { 71727a0bc89SDoug Rabson /* 71827a0bc89SDoug Rabson * In dos directories an entry whose name 71927a0bc89SDoug Rabson * starts with SLOT_EMPTY (0) starts the 72027a0bc89SDoug Rabson * beginning of the unused part of the 72127a0bc89SDoug Rabson * directory, so we can just return that it 72227a0bc89SDoug Rabson * is empty. 72327a0bc89SDoug Rabson */ 72427a0bc89SDoug Rabson if (dentp->deName[0] == SLOT_EMPTY) { 72527a0bc89SDoug Rabson brelse(bp); 726952a6212SJordan K. Hubbard return (1); 72727a0bc89SDoug Rabson } 72827a0bc89SDoug Rabson /* 72927a0bc89SDoug Rabson * Any names other than "." and ".." in a 73027a0bc89SDoug Rabson * directory mean it is not empty. 73127a0bc89SDoug Rabson */ 73227a0bc89SDoug Rabson if (bcmp(dentp->deName, ". ", 11) && 73327a0bc89SDoug Rabson bcmp(dentp->deName, ".. ", 11)) { 73427a0bc89SDoug Rabson brelse(bp); 73527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 736952a6212SJordan K. Hubbard printf("dosdirempty(): entry found %02x, %02x\n", 737952a6212SJordan K. Hubbard dentp->deName[0], dentp->deName[1]); 73827a0bc89SDoug Rabson #endif 739952a6212SJordan K. Hubbard return (0); /* not empty */ 74027a0bc89SDoug Rabson } 74127a0bc89SDoug Rabson } 74227a0bc89SDoug Rabson } 74327a0bc89SDoug Rabson brelse(bp); 74427a0bc89SDoug Rabson } 74527a0bc89SDoug Rabson /* NOTREACHED */ 74627a0bc89SDoug Rabson } 74727a0bc89SDoug Rabson 74827a0bc89SDoug Rabson /* 74927a0bc89SDoug Rabson * Check to see if the directory described by target is in some 75027a0bc89SDoug Rabson * subdirectory of source. This prevents something like the following from 75127a0bc89SDoug Rabson * succeeding and leaving a bunch or files and directories orphaned. mv 75227a0bc89SDoug Rabson * /a/b/c /a/b/c/d/e/f Where c and f are directories. 75327a0bc89SDoug Rabson * 75427a0bc89SDoug Rabson * source - the inode for /a/b/c 75527a0bc89SDoug Rabson * target - the inode for /a/b/c/d/e/f 75627a0bc89SDoug Rabson * 75727a0bc89SDoug Rabson * Returns 0 if target is NOT a subdirectory of source. 75827a0bc89SDoug Rabson * Otherwise returns a non-zero error number. 75927a0bc89SDoug Rabson * The target inode is always unlocked on return. 76027a0bc89SDoug Rabson */ 76127a0bc89SDoug Rabson int 76227a0bc89SDoug Rabson doscheckpath(source, target) 76327a0bc89SDoug Rabson struct denode *source; 76427a0bc89SDoug Rabson struct denode *target; 76527a0bc89SDoug Rabson { 76627a0bc89SDoug Rabson daddr_t scn; 76727a0bc89SDoug Rabson struct msdosfsmount *pmp; 76827a0bc89SDoug Rabson struct direntry *ep; 76927a0bc89SDoug Rabson struct denode *dep; 77027a0bc89SDoug Rabson struct buf *bp = NULL; 77127a0bc89SDoug Rabson int error = 0; 77227a0bc89SDoug Rabson 77327a0bc89SDoug Rabson dep = target; 77427a0bc89SDoug Rabson if ((target->de_Attributes & ATTR_DIRECTORY) == 0 || 77527a0bc89SDoug Rabson (source->de_Attributes & ATTR_DIRECTORY) == 0) { 77627a0bc89SDoug Rabson error = ENOTDIR; 77727a0bc89SDoug Rabson goto out; 77827a0bc89SDoug Rabson } 77927a0bc89SDoug Rabson if (dep->de_StartCluster == source->de_StartCluster) { 78027a0bc89SDoug Rabson error = EEXIST; 78127a0bc89SDoug Rabson goto out; 78227a0bc89SDoug Rabson } 78327a0bc89SDoug Rabson if (dep->de_StartCluster == MSDOSFSROOT) 78427a0bc89SDoug Rabson goto out; 785952a6212SJordan K. Hubbard pmp = dep->de_pmp; 786952a6212SJordan K. Hubbard #ifdef DIAGNOSTIC 787952a6212SJordan K. Hubbard if (pmp != source->de_pmp) 788952a6212SJordan K. Hubbard panic("doscheckpath: source and target on different filesystems"); 789952a6212SJordan K. Hubbard #endif 790952a6212SJordan K. Hubbard if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk) 791952a6212SJordan K. Hubbard goto out; 792952a6212SJordan K. Hubbard 79327a0bc89SDoug Rabson for (;;) { 79427a0bc89SDoug Rabson if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) { 79527a0bc89SDoug Rabson error = ENOTDIR; 796952a6212SJordan K. Hubbard break; 79727a0bc89SDoug Rabson } 79827a0bc89SDoug Rabson scn = dep->de_StartCluster; 79927a0bc89SDoug Rabson error = bread(pmp->pm_devvp, cntobn(pmp, scn), 80027a0bc89SDoug Rabson pmp->pm_bpcluster, NOCRED, &bp); 801952a6212SJordan K. Hubbard if (error) 80227a0bc89SDoug Rabson break; 803952a6212SJordan K. Hubbard 80427a0bc89SDoug Rabson ep = (struct direntry *) bp->b_data + 1; 80527a0bc89SDoug Rabson if ((ep->deAttributes & ATTR_DIRECTORY) == 0 || 80627a0bc89SDoug Rabson bcmp(ep->deName, ".. ", 11) != 0) { 80727a0bc89SDoug Rabson error = ENOTDIR; 80827a0bc89SDoug Rabson break; 80927a0bc89SDoug Rabson } 81027a0bc89SDoug Rabson scn = getushort(ep->deStartCluster); 811952a6212SJordan K. Hubbard if (FAT32(pmp)) 812952a6212SJordan K. Hubbard scn |= getushort(ep->deHighClust) << 16; 813952a6212SJordan K. Hubbard 81427a0bc89SDoug Rabson if (scn == source->de_StartCluster) { 81527a0bc89SDoug Rabson error = EINVAL; 81627a0bc89SDoug Rabson break; 81727a0bc89SDoug Rabson } 81827a0bc89SDoug Rabson if (scn == MSDOSFSROOT) 81927a0bc89SDoug Rabson break; 820952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == pmp->pm_rootdirblk) { 821952a6212SJordan K. Hubbard /* 822952a6212SJordan K. Hubbard * scn should be 0 in this case, 823952a6212SJordan K. Hubbard * but we silently ignore the error. 824952a6212SJordan K. Hubbard */ 825952a6212SJordan K. Hubbard break; 826952a6212SJordan K. Hubbard } 827952a6212SJordan K. Hubbard 82827a0bc89SDoug Rabson vput(DETOV(dep)); 82927a0bc89SDoug Rabson brelse(bp); 83027a0bc89SDoug Rabson bp = NULL; 831952a6212SJordan K. Hubbard /* NOTE: deget() clears dep on error */ 832952a6212SJordan K. Hubbard if ((error = deget(pmp, scn, 0, &dep)) != 0) 83327a0bc89SDoug Rabson break; 83427a0bc89SDoug Rabson } 83527a0bc89SDoug Rabson out:; 83627a0bc89SDoug Rabson if (bp) 83727a0bc89SDoug Rabson brelse(bp); 83827a0bc89SDoug Rabson if (error == ENOTDIR) 83927a0bc89SDoug Rabson printf("doscheckpath(): .. not a directory?\n"); 84027a0bc89SDoug Rabson if (dep != NULL) 84127a0bc89SDoug Rabson vput(DETOV(dep)); 842952a6212SJordan K. Hubbard return (error); 84327a0bc89SDoug Rabson } 84427a0bc89SDoug Rabson 84527a0bc89SDoug Rabson /* 84627a0bc89SDoug Rabson * Read in the disk block containing the directory entry (dirclu, dirofs) 84727a0bc89SDoug Rabson * and return the address of the buf header, and the address of the 84827a0bc89SDoug Rabson * directory entry within the block. 84927a0bc89SDoug Rabson */ 85027a0bc89SDoug Rabson int 851952a6212SJordan K. Hubbard readep(pmp, dirclust, diroffset, bpp, epp) 85227a0bc89SDoug Rabson struct msdosfsmount *pmp; 853952a6212SJordan K. Hubbard u_long dirclust, diroffset; 85427a0bc89SDoug Rabson struct buf **bpp; 85527a0bc89SDoug Rabson struct direntry **epp; 85627a0bc89SDoug Rabson { 85727a0bc89SDoug Rabson int error; 85827a0bc89SDoug Rabson daddr_t bn; 859952a6212SJordan K. Hubbard int blsize; 860952a6212SJordan K. Hubbard u_long boff; 86127a0bc89SDoug Rabson 862952a6212SJordan K. Hubbard boff = diroffset & ~pmp->pm_crbomask; 863952a6212SJordan K. Hubbard blsize = pmp->pm_bpcluster; 864952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT 865952a6212SJordan K. Hubbard && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 866952a6212SJordan K. Hubbard blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 867952a6212SJordan K. Hubbard bn = detobn(pmp, dirclust, diroffset); 868952a6212SJordan K. Hubbard if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 869952a6212SJordan K. Hubbard brelse(*bpp); 87027a0bc89SDoug Rabson *bpp = NULL; 871952a6212SJordan K. Hubbard return (error); 87227a0bc89SDoug Rabson } 87327a0bc89SDoug Rabson if (epp) 874952a6212SJordan K. Hubbard *epp = bptoep(pmp, *bpp, diroffset); 875952a6212SJordan K. Hubbard return (0); 87627a0bc89SDoug Rabson } 87727a0bc89SDoug Rabson 87827a0bc89SDoug Rabson /* 87927a0bc89SDoug Rabson * Read in the disk block containing the directory entry dep came from and 88027a0bc89SDoug Rabson * return the address of the buf header, and the address of the directory 88127a0bc89SDoug Rabson * entry within the block. 88227a0bc89SDoug Rabson */ 88327a0bc89SDoug Rabson int 88427a0bc89SDoug Rabson readde(dep, bpp, epp) 88527a0bc89SDoug Rabson struct denode *dep; 88627a0bc89SDoug Rabson struct buf **bpp; 88727a0bc89SDoug Rabson struct direntry **epp; 88827a0bc89SDoug Rabson { 889952a6212SJordan K. Hubbard 890952a6212SJordan K. Hubbard return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 891952a6212SJordan K. Hubbard bpp, epp)); 892952a6212SJordan K. Hubbard } 893952a6212SJordan K. Hubbard 894952a6212SJordan K. Hubbard /* 895952a6212SJordan K. Hubbard * Remove a directory entry. At this point the file represented by the 896952a6212SJordan K. Hubbard * directory entry to be removed is still full length until noone has it 897952a6212SJordan K. Hubbard * open. When the file no longer being used msdosfs_inactive() is called 898952a6212SJordan K. Hubbard * and will truncate the file to 0 length. When the vnode containing the 899952a6212SJordan K. Hubbard * denode is needed for some other purpose by VFS it will call 900952a6212SJordan K. Hubbard * msdosfs_reclaim() which will remove the denode from the denode cache. 901952a6212SJordan K. Hubbard */ 902952a6212SJordan K. Hubbard int 903952a6212SJordan K. Hubbard removede(pdep, dep) 904952a6212SJordan K. Hubbard struct denode *pdep; /* directory where the entry is removed */ 905952a6212SJordan K. Hubbard struct denode *dep; /* file to be removed */ 906952a6212SJordan K. Hubbard { 907952a6212SJordan K. Hubbard int error; 908952a6212SJordan K. Hubbard struct direntry *ep; 909952a6212SJordan K. Hubbard struct buf *bp; 910952a6212SJordan K. Hubbard daddr_t bn; 911952a6212SJordan K. Hubbard int blsize; 912952a6212SJordan K. Hubbard struct msdosfsmount *pmp = pdep->de_pmp; 913952a6212SJordan K. Hubbard u_long offset = pdep->de_fndoffset; 914952a6212SJordan K. Hubbard 915952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 916952a6212SJordan K. Hubbard printf("removede(): filename %s, dep %p, offset %08lx\n", 917952a6212SJordan K. Hubbard dep->de_Name, dep, offset); 918952a6212SJordan K. Hubbard #endif 919952a6212SJordan K. Hubbard 920952a6212SJordan K. Hubbard dep->de_refcnt--; 921952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 922952a6212SJordan K. Hubbard do { 923952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 924952a6212SJordan K. Hubbard error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize); 925952a6212SJordan K. Hubbard if (error) 926952a6212SJordan K. Hubbard return error; 927952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 928952a6212SJordan K. Hubbard if (error) { 929952a6212SJordan K. Hubbard brelse(bp); 930952a6212SJordan K. Hubbard return error; 931952a6212SJordan K. Hubbard } 932952a6212SJordan K. Hubbard ep = bptoep(pmp, bp, offset); 933952a6212SJordan K. Hubbard /* 934952a6212SJordan K. Hubbard * Check whether, if we came here the second time, i.e. 935952a6212SJordan K. Hubbard * when underflowing into the previous block, the last 936952a6212SJordan K. Hubbard * entry in this block is a longfilename entry, too. 937952a6212SJordan K. Hubbard */ 938952a6212SJordan K. Hubbard if (ep->deAttributes != ATTR_WIN95 939952a6212SJordan K. Hubbard && offset != pdep->de_fndoffset) { 940952a6212SJordan K. Hubbard brelse(bp); 941952a6212SJordan K. Hubbard break; 942952a6212SJordan K. Hubbard } 943952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 944952a6212SJordan K. Hubbard while (1) { 945952a6212SJordan K. Hubbard /* 946952a6212SJordan K. Hubbard * We are a bit agressive here in that we delete any Win95 947952a6212SJordan K. Hubbard * entries preceding this entry, not just the ones we "own". 948952a6212SJordan K. Hubbard * Since these presumably aren't valid anyway, 949952a6212SJordan K. Hubbard * there should be no harm. 950952a6212SJordan K. Hubbard */ 951952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 952952a6212SJordan K. Hubbard ep--->deName[0] = SLOT_DELETED; 953952a6212SJordan K. Hubbard if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) 954952a6212SJordan K. Hubbard || !(offset & pmp->pm_crbomask) 955952a6212SJordan K. Hubbard || ep->deAttributes != ATTR_WIN95) 956952a6212SJordan K. Hubbard break; 957952a6212SJordan K. Hubbard } 958952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 959952a6212SJordan K. Hubbard return error; 960952a6212SJordan K. Hubbard } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95) 961952a6212SJordan K. Hubbard && !(offset & pmp->pm_crbomask) 962952a6212SJordan K. Hubbard && offset); 963952a6212SJordan K. Hubbard return 0; 964952a6212SJordan K. Hubbard } 965952a6212SJordan K. Hubbard 966952a6212SJordan K. Hubbard /* 967952a6212SJordan K. Hubbard * Create a unique DOS name in dvp 968952a6212SJordan K. Hubbard */ 969952a6212SJordan K. Hubbard int 970952a6212SJordan K. Hubbard uniqdosname(dep, cnp, cp) 971952a6212SJordan K. Hubbard struct denode *dep; 972952a6212SJordan K. Hubbard struct componentname *cnp; 973952a6212SJordan K. Hubbard u_char *cp; 974952a6212SJordan K. Hubbard { 975952a6212SJordan K. Hubbard struct msdosfsmount *pmp = dep->de_pmp; 976952a6212SJordan K. Hubbard struct direntry *dentp; 977952a6212SJordan K. Hubbard int gen; 978952a6212SJordan K. Hubbard int blsize; 979952a6212SJordan K. Hubbard u_long cn; 980952a6212SJordan K. Hubbard daddr_t bn; 981952a6212SJordan K. Hubbard struct buf *bp; 982952a6212SJordan K. Hubbard int error; 983952a6212SJordan K. Hubbard 984af9f1d50SDmitrij Tejblum if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 985011cdb57SDmitrij Tejblum return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 986011cdb57SDmitrij Tejblum cnp->cn_namelen, 0, 987011cdb57SDmitrij Tejblum pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d, 988011cdb57SDmitrij Tejblum pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu) ? 989011cdb57SDmitrij Tejblum 0 : EINVAL); 990011cdb57SDmitrij Tejblum 991952a6212SJordan K. Hubbard for (gen = 1;; gen++) { 992952a6212SJordan K. Hubbard /* 993952a6212SJordan K. Hubbard * Generate DOS name with generation number 994952a6212SJordan K. Hubbard */ 995952a6212SJordan K. Hubbard if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 9967391f611SAndrey A. Chernov cnp->cn_namelen, gen, 9977391f611SAndrey A. Chernov pmp->pm_flags & MSDOSFSMNT_U2WTABLE, pmp->pm_u2d, 9987391f611SAndrey A. Chernov pmp->pm_flags & MSDOSFSMNT_ULTABLE, pmp->pm_lu)) 999952a6212SJordan K. Hubbard return gen == 1 ? EINVAL : EEXIST; 1000952a6212SJordan K. Hubbard 1001952a6212SJordan K. Hubbard /* 1002952a6212SJordan K. Hubbard * Now look for a dir entry with this exact name 1003952a6212SJordan K. Hubbard */ 1004952a6212SJordan K. Hubbard for (cn = error = 0; !error; cn++) { 1005952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 1006952a6212SJordan K. Hubbard if (error == E2BIG) /* EOF reached and not found */ 1007952a6212SJordan K. Hubbard return 0; 1008952a6212SJordan K. Hubbard return error; 1009952a6212SJordan K. Hubbard } 1010952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 1011952a6212SJordan K. Hubbard if (error) { 1012952a6212SJordan K. Hubbard brelse(bp); 1013952a6212SJordan K. Hubbard return error; 1014952a6212SJordan K. Hubbard } 1015952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 1016952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 1017952a6212SJordan K. Hubbard dentp++) { 1018952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_EMPTY) { 1019952a6212SJordan K. Hubbard /* 1020952a6212SJordan K. Hubbard * Last used entry and not found 1021952a6212SJordan K. Hubbard */ 1022952a6212SJordan K. Hubbard brelse(bp); 1023952a6212SJordan K. Hubbard return 0; 1024952a6212SJordan K. Hubbard } 1025952a6212SJordan K. Hubbard /* 1026952a6212SJordan K. Hubbard * Ignore volume labels and Win95 entries 1027952a6212SJordan K. Hubbard */ 1028952a6212SJordan K. Hubbard if (dentp->deAttributes & ATTR_VOLUME) 1029952a6212SJordan K. Hubbard continue; 1030952a6212SJordan K. Hubbard if (!bcmp(dentp->deName, cp, 11)) { 1031952a6212SJordan K. Hubbard error = EEXIST; 1032952a6212SJordan K. Hubbard break; 1033952a6212SJordan K. Hubbard } 1034952a6212SJordan K. Hubbard } 1035952a6212SJordan K. Hubbard brelse(bp); 1036952a6212SJordan K. Hubbard } 1037952a6212SJordan K. Hubbard } 1038952a6212SJordan K. Hubbard } 1039952a6212SJordan K. Hubbard 1040952a6212SJordan K. Hubbard /* 1041952a6212SJordan K. Hubbard * Find any Win'95 long filename entry in directory dep 1042952a6212SJordan K. Hubbard */ 1043952a6212SJordan K. Hubbard int 1044952a6212SJordan K. Hubbard findwin95(dep) 1045952a6212SJordan K. Hubbard struct denode *dep; 1046952a6212SJordan K. Hubbard { 1047952a6212SJordan K. Hubbard struct msdosfsmount *pmp = dep->de_pmp; 1048952a6212SJordan K. Hubbard struct direntry *dentp; 1049952a6212SJordan K. Hubbard int blsize; 1050952a6212SJordan K. Hubbard u_long cn; 1051952a6212SJordan K. Hubbard daddr_t bn; 1052952a6212SJordan K. Hubbard struct buf *bp; 1053952a6212SJordan K. Hubbard 1054952a6212SJordan K. Hubbard /* 1055952a6212SJordan K. Hubbard * Read through the directory looking for Win'95 entries 1056952a6212SJordan K. Hubbard * Note: Error currently handled just as EOF XXX 1057952a6212SJordan K. Hubbard */ 1058952a6212SJordan K. Hubbard for (cn = 0;; cn++) { 1059952a6212SJordan K. Hubbard if (pcbmap(dep, cn, &bn, 0, &blsize)) 1060952a6212SJordan K. Hubbard return 0; 1061952a6212SJordan K. Hubbard if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) { 1062952a6212SJordan K. Hubbard brelse(bp); 1063952a6212SJordan K. Hubbard return 0; 1064952a6212SJordan K. Hubbard } 1065952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 1066952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 1067952a6212SJordan K. Hubbard dentp++) { 1068952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_EMPTY) { 1069952a6212SJordan K. Hubbard /* 1070952a6212SJordan K. Hubbard * Last used entry and not found 1071952a6212SJordan K. Hubbard */ 1072952a6212SJordan K. Hubbard brelse(bp); 1073952a6212SJordan K. Hubbard return 0; 1074952a6212SJordan K. Hubbard } 1075952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_DELETED) { 1076952a6212SJordan K. Hubbard /* 1077952a6212SJordan K. Hubbard * Ignore deleted files 1078952a6212SJordan K. Hubbard * Note: might be an indication of Win'95 anyway XXX 1079952a6212SJordan K. Hubbard */ 1080952a6212SJordan K. Hubbard continue; 1081952a6212SJordan K. Hubbard } 1082952a6212SJordan K. Hubbard if (dentp->deAttributes == ATTR_WIN95) { 1083952a6212SJordan K. Hubbard brelse(bp); 1084952a6212SJordan K. Hubbard return 1; 1085952a6212SJordan K. Hubbard } 1086952a6212SJordan K. Hubbard } 1087952a6212SJordan K. Hubbard brelse(bp); 1088952a6212SJordan K. Hubbard } 108927a0bc89SDoug Rabson } 1090