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 /*- 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 */ 35d167cf6fSWarner Losh /*- 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> 52919d1ea2SPeter Wemm #include <sys/systm.h> 5327a0bc89SDoug Rabson #include <sys/buf.h> 5427a0bc89SDoug Rabson #include <sys/mount.h> 55a878a31cSBruce Evans #include <sys/namei.h> 56a878a31cSBruce Evans #include <sys/vnode.h> 5727a0bc89SDoug Rabson 581166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h> 591166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h> 601166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h> 611166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h> 62a878a31cSBruce Evans #include <fs/msdosfs/msdosfsmount.h> 6327a0bc89SDoug Rabson 6427a0bc89SDoug Rabson /* 6527a0bc89SDoug Rabson * When we search a directory the blocks containing directory entries are 6627a0bc89SDoug Rabson * read and examined. The directory entries contain information that would 6727a0bc89SDoug Rabson * normally be in the inode of a unix filesystem. This means that some of 6827a0bc89SDoug Rabson * a directory's contents may also be in memory resident denodes (sort of 6927a0bc89SDoug Rabson * an inode). This can cause problems if we are searching while some other 7027a0bc89SDoug Rabson * process is modifying a directory. To prevent one process from accessing 7127a0bc89SDoug Rabson * incompletely modified directory information we depend upon being the 72b84d2921SPoul-Henning Kamp * sole owner of a directory block. bread/brelse provide this service. 7327a0bc89SDoug Rabson * This being the case, when a process modifies a directory it must first 7427a0bc89SDoug Rabson * acquire the disk block that contains the directory entry to be modified. 7527a0bc89SDoug Rabson * Then update the disk block and the denode, and then write the disk block 7627a0bc89SDoug Rabson * out to disk. This way disk blocks containing directory entries and in 7727a0bc89SDoug Rabson * memory denode's will be in synch. 7827a0bc89SDoug Rabson */ 7927a0bc89SDoug Rabson int 8027a0bc89SDoug Rabson msdosfs_lookup(ap) 810fa2443fSPoul-Henning Kamp struct vop_cachedlookup_args /* { 8227a0bc89SDoug Rabson struct vnode *a_dvp; 8327a0bc89SDoug Rabson struct vnode **a_vpp; 8427a0bc89SDoug Rabson struct componentname *a_cnp; 8527a0bc89SDoug Rabson } */ *ap; 8627a0bc89SDoug Rabson { 87c2819440SBruce Evans struct mbnambuf nb; 8827a0bc89SDoug Rabson struct vnode *vdp = ap->a_dvp; 8927a0bc89SDoug Rabson struct vnode **vpp = ap->a_vpp; 9027a0bc89SDoug Rabson struct componentname *cnp = ap->a_cnp; 9127a0bc89SDoug Rabson daddr_t bn; 9227a0bc89SDoug Rabson int error; 93952a6212SJordan K. Hubbard int slotcount; 94952a6212SJordan K. Hubbard int slotoffset = 0; 9527a0bc89SDoug Rabson int frcn; 9627a0bc89SDoug Rabson u_long cluster; 97952a6212SJordan K. Hubbard int blkoff; 9827a0bc89SDoug Rabson int diroff; 99952a6212SJordan K. Hubbard int blsize; 10027a0bc89SDoug Rabson int isadir; /* ~0 if found direntry is a directory */ 10127a0bc89SDoug Rabson u_long scn; /* starting cluster number */ 10227a0bc89SDoug Rabson struct vnode *pdp; 10327a0bc89SDoug Rabson struct denode *dp; 10427a0bc89SDoug Rabson struct denode *tdp; 10527a0bc89SDoug Rabson struct msdosfsmount *pmp; 10627a0bc89SDoug Rabson struct buf *bp = 0; 10727a0bc89SDoug Rabson struct direntry *dep = NULL; 10827a0bc89SDoug Rabson u_char dosfilename[12]; 10927a0bc89SDoug Rabson int flags = cnp->cn_flags; 11027a0bc89SDoug Rabson int nameiop = cnp->cn_nameiop; 111b40ce416SJulian Elischer struct thread *td = cnp->cn_thread; 11269a36f24SMike Smith int unlen; 11327a0bc89SDoug Rabson 114952a6212SJordan K. Hubbard int wincnt = 1; 1156a5bf04aSTim J. Robbins int chksum = -1, chksum_ok; 116952a6212SJordan K. Hubbard int olddos = 1; 117952a6212SJordan K. Hubbard 11827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 11927a0bc89SDoug Rabson printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr); 12027a0bc89SDoug Rabson #endif 12127a0bc89SDoug Rabson dp = VTODE(vdp); 12227a0bc89SDoug Rabson pmp = dp->de_pmp; 12327a0bc89SDoug Rabson *vpp = NULL; 12427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 125952a6212SJordan K. Hubbard printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n", 12627a0bc89SDoug Rabson vdp, dp, dp->de_Attributes); 12727a0bc89SDoug Rabson #endif 12827a0bc89SDoug Rabson 12927a0bc89SDoug Rabson /* 13027a0bc89SDoug Rabson * If they are going after the . or .. entry in the root directory, 13127a0bc89SDoug Rabson * they won't find it. DOS filesystems don't have them in the root 13227a0bc89SDoug Rabson * directory. So, we fake it. deget() is in on this scam too. 13327a0bc89SDoug Rabson */ 134e6e370a7SJeff Roberson if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' && 13527a0bc89SDoug Rabson (cnp->cn_namelen == 1 || 13627a0bc89SDoug Rabson (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) { 13727a0bc89SDoug Rabson isadir = ATTR_DIRECTORY; 13827a0bc89SDoug Rabson scn = MSDOSFSROOT; 13927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 14027a0bc89SDoug Rabson printf("msdosfs_lookup(): looking for . or .. in root directory\n"); 14127a0bc89SDoug Rabson #endif 14227a0bc89SDoug Rabson cluster = MSDOSFSROOT; 143952a6212SJordan K. Hubbard blkoff = MSDOSFSROOT_OFS; 14427a0bc89SDoug Rabson goto foundroot; 14527a0bc89SDoug Rabson } 14627a0bc89SDoug Rabson 147952a6212SJordan K. Hubbard switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename, 148c4f02a89SMax Khon cnp->cn_namelen, 0, pmp)) { 149952a6212SJordan K. Hubbard case 0: 150952a6212SJordan K. Hubbard return (EINVAL); 151952a6212SJordan K. Hubbard case 1: 152952a6212SJordan K. Hubbard break; 153952a6212SJordan K. Hubbard case 2: 154952a6212SJordan K. Hubbard wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 155c4f02a89SMax Khon cnp->cn_namelen, pmp) + 1; 156952a6212SJordan K. Hubbard break; 157952a6212SJordan K. Hubbard case 3: 158952a6212SJordan K. Hubbard olddos = 0; 159952a6212SJordan K. Hubbard wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr, 160c4f02a89SMax Khon cnp->cn_namelen, pmp) + 1; 161952a6212SJordan K. Hubbard break; 16227a0bc89SDoug Rabson } 163011cdb57SDmitrij Tejblum if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) { 164952a6212SJordan K. Hubbard wincnt = 1; 165011cdb57SDmitrij Tejblum olddos = 1; 166011cdb57SDmitrij Tejblum } 16769a36f24SMike Smith unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen); 16827a0bc89SDoug Rabson 169952a6212SJordan K. Hubbard /* 170952a6212SJordan K. Hubbard * Suppress search for slots unless creating 171952a6212SJordan K. Hubbard * file and at end of pathname, in which case 172952a6212SJordan K. Hubbard * we watch for a place to put the new file in 173952a6212SJordan K. Hubbard * case it doesn't already exist. 174952a6212SJordan K. Hubbard */ 175952a6212SJordan K. Hubbard slotcount = wincnt; 176952a6212SJordan K. Hubbard if ((nameiop == CREATE || nameiop == RENAME) && 177952a6212SJordan K. Hubbard (flags & ISLASTCN)) 178952a6212SJordan K. Hubbard slotcount = 0; 179952a6212SJordan K. Hubbard 18027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 181952a6212SJordan K. Hubbard printf("msdosfs_lookup(): dos version of filename %s, length %ld\n", 18227a0bc89SDoug Rabson dosfilename, cnp->cn_namelen); 18327a0bc89SDoug Rabson #endif 18427a0bc89SDoug Rabson /* 18527a0bc89SDoug Rabson * Search the directory pointed at by vdp for the name pointed at 18627a0bc89SDoug Rabson * by cnp->cn_nameptr. 18727a0bc89SDoug Rabson */ 18827a0bc89SDoug Rabson tdp = NULL; 189c2819440SBruce Evans mbnambuf_init(&nb); 19027a0bc89SDoug Rabson /* 19127a0bc89SDoug Rabson * The outer loop ranges over the clusters that make up the 19227a0bc89SDoug Rabson * directory. Note that the root directory is different from all 19327a0bc89SDoug Rabson * other directories. It has a fixed number of blocks that are not 19427a0bc89SDoug Rabson * part of the pool of allocatable clusters. So, we treat it a 19527a0bc89SDoug Rabson * little differently. The root directory starts at "cluster" 0. 19627a0bc89SDoug Rabson */ 197952a6212SJordan K. Hubbard diroff = 0; 19827a0bc89SDoug Rabson for (frcn = 0;; frcn++) { 199952a6212SJordan K. Hubbard error = pcbmap(dp, frcn, &bn, &cluster, &blsize); 200c3c6d51eSPoul-Henning Kamp if (error) { 20127a0bc89SDoug Rabson if (error == E2BIG) 20227a0bc89SDoug Rabson break; 203952a6212SJordan K. Hubbard return (error); 20427a0bc89SDoug Rabson } 205952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 206952a6212SJordan K. Hubbard if (error) { 207952a6212SJordan K. Hubbard brelse(bp); 208952a6212SJordan K. Hubbard return (error); 209952a6212SJordan K. Hubbard } 210952a6212SJordan K. Hubbard for (blkoff = 0; blkoff < blsize; 211952a6212SJordan K. Hubbard blkoff += sizeof(struct direntry), 212952a6212SJordan K. Hubbard diroff += sizeof(struct direntry)) { 213952a6212SJordan K. Hubbard dep = (struct direntry *)(bp->b_data + blkoff); 21427a0bc89SDoug Rabson /* 21527a0bc89SDoug Rabson * If the slot is empty and we are still looking 21627a0bc89SDoug Rabson * for an empty then remember this one. If the 21727a0bc89SDoug Rabson * slot is not empty then check to see if it 21827a0bc89SDoug Rabson * matches what we are looking for. If the slot 21927a0bc89SDoug Rabson * has never been filled with anything, then the 22027a0bc89SDoug Rabson * remainder of the directory has never been used, 22127a0bc89SDoug Rabson * so there is no point in searching it. 22227a0bc89SDoug Rabson */ 22327a0bc89SDoug Rabson if (dep->deName[0] == SLOT_EMPTY || 22427a0bc89SDoug Rabson dep->deName[0] == SLOT_DELETED) { 225952a6212SJordan K. Hubbard /* 226952a6212SJordan K. Hubbard * Drop memory of previous long matches 227952a6212SJordan K. Hubbard */ 228952a6212SJordan K. Hubbard chksum = -1; 229c2819440SBruce Evans mbnambuf_init(&nb); 230952a6212SJordan K. Hubbard 231952a6212SJordan K. Hubbard if (slotcount < wincnt) { 232952a6212SJordan K. Hubbard slotcount++; 23327a0bc89SDoug Rabson slotoffset = diroff; 23427a0bc89SDoug Rabson } 23527a0bc89SDoug Rabson if (dep->deName[0] == SLOT_EMPTY) { 23627a0bc89SDoug Rabson brelse(bp); 23727a0bc89SDoug Rabson goto notfound; 23827a0bc89SDoug Rabson } 23927a0bc89SDoug Rabson } else { 24027a0bc89SDoug Rabson /* 241952a6212SJordan K. Hubbard * If there wasn't enough space for our winentries, 242952a6212SJordan K. Hubbard * forget about the empty space 243952a6212SJordan K. Hubbard */ 244952a6212SJordan K. Hubbard if (slotcount < wincnt) 245952a6212SJordan K. Hubbard slotcount = 0; 246952a6212SJordan K. Hubbard 247952a6212SJordan K. Hubbard /* 248952a6212SJordan K. Hubbard * Check for Win95 long filename entry 249952a6212SJordan K. Hubbard */ 250952a6212SJordan K. Hubbard if (dep->deAttributes == ATTR_WIN95) { 251952a6212SJordan K. Hubbard if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 252952a6212SJordan K. Hubbard continue; 253952a6212SJordan K. Hubbard 254c2819440SBruce Evans chksum = win2unixfn(&nb, 255c2819440SBruce Evans (struct winentry *)dep, chksum, 256c4f02a89SMax Khon pmp); 257c4f02a89SMax Khon continue; 258c4f02a89SMax Khon } 259c4f02a89SMax Khon 260c2819440SBruce Evans chksum = winChkName(&nb, 261c2819440SBruce Evans (const u_char *)cnp->cn_nameptr, unlen, 262c2819440SBruce Evans chksum, pmp); 263c4f02a89SMax Khon if (chksum == -2) { 264c4f02a89SMax Khon chksum = -1; 265952a6212SJordan K. Hubbard continue; 266952a6212SJordan K. Hubbard } 267952a6212SJordan K. Hubbard 268952a6212SJordan K. Hubbard /* 26927a0bc89SDoug Rabson * Ignore volume labels (anywhere, not just 27027a0bc89SDoug Rabson * the root directory). 27127a0bc89SDoug Rabson */ 272952a6212SJordan K. Hubbard if (dep->deAttributes & ATTR_VOLUME) { 273952a6212SJordan K. Hubbard chksum = -1; 274952a6212SJordan K. Hubbard continue; 275952a6212SJordan K. Hubbard } 276952a6212SJordan K. Hubbard 277952a6212SJordan K. Hubbard /* 278952a6212SJordan K. Hubbard * Check for a checksum or name match 279952a6212SJordan K. Hubbard */ 280710a9accSMax Khon chksum_ok = (chksum == winChksum(dep)); 2816a5bf04aSTim J. Robbins if (!chksum_ok 282952a6212SJordan K. Hubbard && (!olddos || bcmp(dosfilename, dep->deName, 11))) { 283952a6212SJordan K. Hubbard chksum = -1; 284952a6212SJordan K. Hubbard continue; 285952a6212SJordan K. Hubbard } 28627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 287952a6212SJordan K. Hubbard printf("msdosfs_lookup(): match blkoff %d, diroff %d\n", 288952a6212SJordan K. Hubbard blkoff, diroff); 28927a0bc89SDoug Rabson #endif 29027a0bc89SDoug Rabson /* 29127a0bc89SDoug Rabson * Remember where this directory 29227a0bc89SDoug Rabson * entry came from for whoever did 293952a6212SJordan K. Hubbard * this lookup. 29427a0bc89SDoug Rabson */ 29527a0bc89SDoug Rabson dp->de_fndoffset = diroff; 2966a5bf04aSTim J. Robbins if (chksum_ok && nameiop == RENAME) { 2976a5bf04aSTim J. Robbins /* 2986a5bf04aSTim J. Robbins * Target had correct long name 2996a5bf04aSTim J. Robbins * directory entries, reuse them 3006a5bf04aSTim J. Robbins * as needed. 3016a5bf04aSTim J. Robbins */ 302191e6fd0SDmitrij Tejblum dp->de_fndcnt = wincnt - 1; 3036a5bf04aSTim J. Robbins } else { 3046a5bf04aSTim J. Robbins /* 3056a5bf04aSTim J. Robbins * Long name directory entries 3066a5bf04aSTim J. Robbins * not present or corrupt, can only 3076a5bf04aSTim J. Robbins * reuse dos directory entry. 3086a5bf04aSTim J. Robbins */ 3096a5bf04aSTim J. Robbins dp->de_fndcnt = 0; 3106a5bf04aSTim J. Robbins } 311952a6212SJordan K. Hubbard 31227a0bc89SDoug Rabson goto found; 31327a0bc89SDoug Rabson } 314952a6212SJordan K. Hubbard } /* for (blkoff = 0; .... */ 31527a0bc89SDoug Rabson /* 31627a0bc89SDoug Rabson * Release the buffer holding the directory cluster just 31727a0bc89SDoug Rabson * searched. 31827a0bc89SDoug Rabson */ 31927a0bc89SDoug Rabson brelse(bp); 32027a0bc89SDoug Rabson } /* for (frcn = 0; ; frcn++) */ 321952a6212SJordan K. Hubbard 322952a6212SJordan K. Hubbard notfound: 32327a0bc89SDoug Rabson /* 32427a0bc89SDoug Rabson * We hold no disk buffers at this point. 32527a0bc89SDoug Rabson */ 32627a0bc89SDoug Rabson 32727a0bc89SDoug Rabson /* 328952a6212SJordan K. Hubbard * Fixup the slot description to point to the place where 329952a6212SJordan K. Hubbard * we might put the new DOS direntry (putting the Win95 330952a6212SJordan K. Hubbard * long name entries before that) 331952a6212SJordan K. Hubbard */ 332952a6212SJordan K. Hubbard if (!slotcount) { 333952a6212SJordan K. Hubbard slotcount = 1; 334952a6212SJordan K. Hubbard slotoffset = diroff; 335952a6212SJordan K. Hubbard } 336952a6212SJordan K. Hubbard if (wincnt > slotcount) 337952a6212SJordan K. Hubbard slotoffset += sizeof(struct direntry) * (wincnt - slotcount); 338952a6212SJordan K. Hubbard 339952a6212SJordan K. Hubbard /* 34027a0bc89SDoug Rabson * If we get here we didn't find the entry we were looking for. But 34127a0bc89SDoug Rabson * that's ok if we are creating or renaming and are at the end of 34227a0bc89SDoug Rabson * the pathname and the directory hasn't been removed. 34327a0bc89SDoug Rabson */ 34427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 345952a6212SJordan K. Hubbard printf("msdosfs_lookup(): op %d, refcnt %ld\n", 346952a6212SJordan K. Hubbard nameiop, dp->de_refcnt); 347952a6212SJordan K. Hubbard printf(" slotcount %d, slotoffset %d\n", 348952a6212SJordan K. Hubbard slotcount, slotoffset); 34927a0bc89SDoug Rabson #endif 35027a0bc89SDoug Rabson if ((nameiop == CREATE || nameiop == RENAME) && 35127a0bc89SDoug Rabson (flags & ISLASTCN) && dp->de_refcnt != 0) { 352952a6212SJordan K. Hubbard /* 353952a6212SJordan K. Hubbard * Access for write is interpreted as allowing 354952a6212SJordan K. Hubbard * creation of files in the directory. 355952a6212SJordan K. Hubbard */ 356b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 357d8762fa6SBruce Evans if (error) 358952a6212SJordan K. Hubbard return (error); 359952a6212SJordan K. Hubbard /* 360952a6212SJordan K. Hubbard * Return an indication of where the new directory 361952a6212SJordan K. Hubbard * entry should be put. 362952a6212SJordan K. Hubbard */ 36327a0bc89SDoug Rabson dp->de_fndoffset = slotoffset; 364952a6212SJordan K. Hubbard dp->de_fndcnt = wincnt - 1; 365952a6212SJordan K. Hubbard 366952a6212SJordan K. Hubbard /* 367952a6212SJordan K. Hubbard * We return with the directory locked, so that 368952a6212SJordan K. Hubbard * the parameters we set up above will still be 369952a6212SJordan K. Hubbard * valid if we actually decide to do a direnter(). 370952a6212SJordan K. Hubbard * We return ni_vp == NULL to indicate that the entry 371952a6212SJordan K. Hubbard * does not currently exist; we leave a pointer to 372952a6212SJordan K. Hubbard * the (locked) directory inode in ndp->ni_dvp. 373952a6212SJordan K. Hubbard * The pathname buffer is saved so that the name 374952a6212SJordan K. Hubbard * can be obtained later. 375952a6212SJordan K. Hubbard * 376952a6212SJordan K. Hubbard * NB - if the directory is unlocked, then this 377952a6212SJordan K. Hubbard * information cannot be used. 378952a6212SJordan K. Hubbard */ 37927a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 380952a6212SJordan K. Hubbard return (EJUSTRETURN); 38127a0bc89SDoug Rabson } 38223e8fcafSDavid Schultz #if 0 38327a0bc89SDoug Rabson /* 384952a6212SJordan K. Hubbard * Insert name into cache (as non-existent) if appropriate. 38523e8fcafSDavid Schultz * 38623e8fcafSDavid Schultz * XXX Negative caching is broken for msdosfs because the name 38723e8fcafSDavid Schultz * cache doesn't understand peculiarities such as case insensitivity 38823e8fcafSDavid Schultz * and 8.3 filenames. Hence, it may not invalidate all negative 38923e8fcafSDavid Schultz * entries if a file with this name is later created. 39027a0bc89SDoug Rabson */ 39127a0bc89SDoug Rabson if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 39227a0bc89SDoug Rabson cache_enter(vdp, *vpp, cnp); 39323e8fcafSDavid Schultz #endif 394952a6212SJordan K. Hubbard return (ENOENT); 39527a0bc89SDoug Rabson 396952a6212SJordan K. Hubbard found: 39727a0bc89SDoug Rabson /* 39827a0bc89SDoug Rabson * NOTE: We still have the buffer with matched directory entry at 39927a0bc89SDoug Rabson * this point. 40027a0bc89SDoug Rabson */ 40127a0bc89SDoug Rabson isadir = dep->deAttributes & ATTR_DIRECTORY; 40227a0bc89SDoug Rabson scn = getushort(dep->deStartCluster); 403952a6212SJordan K. Hubbard if (FAT32(pmp)) { 404952a6212SJordan K. Hubbard scn |= getushort(dep->deHighClust) << 16; 405952a6212SJordan K. Hubbard if (scn == pmp->pm_rootdirblk) { 406952a6212SJordan K. Hubbard /* 407952a6212SJordan K. Hubbard * There should actually be 0 here. 408952a6212SJordan K. Hubbard * Just ignore the error. 409952a6212SJordan K. Hubbard */ 410952a6212SJordan K. Hubbard scn = MSDOSFSROOT; 411952a6212SJordan K. Hubbard } 412952a6212SJordan K. Hubbard } 41327a0bc89SDoug Rabson 414952a6212SJordan K. Hubbard if (isadir) { 415952a6212SJordan K. Hubbard cluster = scn; 416952a6212SJordan K. Hubbard if (cluster == MSDOSFSROOT) 417952a6212SJordan K. Hubbard blkoff = MSDOSFSROOT_OFS; 418952a6212SJordan K. Hubbard else 419952a6212SJordan K. Hubbard blkoff = 0; 420952a6212SJordan K. Hubbard } else if (cluster == MSDOSFSROOT) 421952a6212SJordan K. Hubbard blkoff = diroff; 422952a6212SJordan K. Hubbard 423952a6212SJordan K. Hubbard /* 424952a6212SJordan K. Hubbard * Now release buf to allow deget to read the entry again. 425952a6212SJordan K. Hubbard * Reserving it here and giving it to deget could result 426952a6212SJordan K. Hubbard * in a deadlock. 427952a6212SJordan K. Hubbard */ 428952a6212SJordan K. Hubbard brelse(bp); 429952a6212SJordan K. Hubbard bp = 0; 430952a6212SJordan K. Hubbard 431952a6212SJordan K. Hubbard foundroot: 43227a0bc89SDoug Rabson /* 43327a0bc89SDoug Rabson * If we entered at foundroot, then we are looking for the . or .. 43427a0bc89SDoug Rabson * entry of the filesystems root directory. isadir and scn were 435952a6212SJordan K. Hubbard * setup before jumping here. And, bp is already null. 43627a0bc89SDoug Rabson */ 437952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == MSDOSFSROOT) 438952a6212SJordan K. Hubbard scn = pmp->pm_rootdirblk; 43927a0bc89SDoug Rabson 44027a0bc89SDoug Rabson /* 441952a6212SJordan K. Hubbard * If deleting, and at end of pathname, return 442952a6212SJordan K. Hubbard * parameters which can be used to remove file. 44327a0bc89SDoug Rabson */ 44427a0bc89SDoug Rabson if (nameiop == DELETE && (flags & ISLASTCN)) { 445952a6212SJordan K. Hubbard /* 446952a6212SJordan K. Hubbard * Don't allow deleting the root. 447952a6212SJordan K. Hubbard */ 448952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 449952a6212SJordan K. Hubbard return EROFS; /* really? XXX */ 450952a6212SJordan K. Hubbard 451952a6212SJordan K. Hubbard /* 452952a6212SJordan K. Hubbard * Write access to directory required to delete files. 453952a6212SJordan K. Hubbard */ 454b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 455952a6212SJordan K. Hubbard if (error) 456952a6212SJordan K. Hubbard return (error); 457952a6212SJordan K. Hubbard 458952a6212SJordan K. Hubbard /* 459952a6212SJordan K. Hubbard * Return pointer to current entry in dp->i_offset. 460952a6212SJordan K. Hubbard * Save directory inode pointer in ndp->ni_dvp for dirremove(). 461952a6212SJordan K. Hubbard */ 46227a0bc89SDoug Rabson if (dp->de_StartCluster == scn && isadir) { /* "." */ 46327a0bc89SDoug Rabson VREF(vdp); 46427a0bc89SDoug Rabson *vpp = vdp; 465952a6212SJordan K. Hubbard return (0); 46627a0bc89SDoug Rabson } 467952a6212SJordan K. Hubbard error = deget(pmp, cluster, blkoff, &tdp); 468952a6212SJordan K. Hubbard if (error) 469952a6212SJordan K. Hubbard return (error); 47027a0bc89SDoug Rabson *vpp = DETOV(tdp); 471952a6212SJordan K. Hubbard return (0); 47227a0bc89SDoug Rabson } 47327a0bc89SDoug Rabson 47427a0bc89SDoug Rabson /* 475952a6212SJordan K. Hubbard * If rewriting (RENAME), return the inode and the 476952a6212SJordan K. Hubbard * information required to rewrite the present directory 477952a6212SJordan K. Hubbard * Must get inode of directory entry to verify it's a 478952a6212SJordan K. Hubbard * regular file, or empty directory. 47927a0bc89SDoug Rabson */ 480fcc9c112SJeff Roberson if (nameiop == RENAME && (flags & ISLASTCN)) { 481952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 482952a6212SJordan K. Hubbard return EROFS; /* really? XXX */ 483952a6212SJordan K. Hubbard 484b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 485952a6212SJordan K. Hubbard if (error) 486952a6212SJordan K. Hubbard return (error); 487952a6212SJordan K. Hubbard 488952a6212SJordan K. Hubbard /* 489952a6212SJordan K. Hubbard * Careful about locking second inode. 490952a6212SJordan K. Hubbard * This can only occur if the target is ".". 491952a6212SJordan K. Hubbard */ 492952a6212SJordan K. Hubbard if (dp->de_StartCluster == scn && isadir) 493952a6212SJordan K. Hubbard return (EISDIR); 494952a6212SJordan K. Hubbard 495952a6212SJordan K. Hubbard if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 496952a6212SJordan K. Hubbard return (error); 49727a0bc89SDoug Rabson *vpp = DETOV(tdp); 49827a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 499952a6212SJordan K. Hubbard return (0); 50027a0bc89SDoug Rabson } 50127a0bc89SDoug Rabson 50227a0bc89SDoug Rabson /* 503952a6212SJordan K. Hubbard * Step through the translation in the name. We do not `vput' the 504952a6212SJordan K. Hubbard * directory because we may need it again if a symbolic link 505952a6212SJordan K. Hubbard * is relative to the current directory. Instead we save it 506952a6212SJordan K. Hubbard * unlocked as "pdp". We must get the target inode before unlocking 507952a6212SJordan K. Hubbard * the directory to insure that the inode will not be removed 508952a6212SJordan K. Hubbard * before we get it. We prevent deadlock by always fetching 509952a6212SJordan K. Hubbard * inodes from the root, moving down the directory tree. Thus 510952a6212SJordan K. Hubbard * when following backward pointers ".." we must unlock the 511952a6212SJordan K. Hubbard * parent directory before getting the requested directory. 512952a6212SJordan K. Hubbard * There is a potential race condition here if both the current 513952a6212SJordan K. Hubbard * and parent directories are removed before the VFS_VGET for the 514952a6212SJordan K. Hubbard * inode associated with ".." returns. We hope that this occurs 515952a6212SJordan K. Hubbard * infrequently since we cannot avoid this race condition without 516952a6212SJordan K. Hubbard * implementing a sophisticated deadlock detection algorithm. 517952a6212SJordan K. Hubbard * Note also that this simple deadlock detection scheme will not 518952a6212SJordan K. Hubbard * work if the filesystem has any hard links other than ".." 519952a6212SJordan K. Hubbard * that point backwards in the directory structure. 52027a0bc89SDoug Rabson */ 52127a0bc89SDoug Rabson pdp = vdp; 52227a0bc89SDoug Rabson if (flags & ISDOTDOT) { 523b40ce416SJulian Elischer VOP_UNLOCK(pdp, 0, td); 524952a6212SJordan K. Hubbard error = deget(pmp, cluster, blkoff, &tdp); 525b40ce416SJulian Elischer vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td); 5264585e3acSJeff Roberson if (error) 527952a6212SJordan K. Hubbard return (error); 52827a0bc89SDoug Rabson *vpp = DETOV(tdp); 529952a6212SJordan K. Hubbard } else if (dp->de_StartCluster == scn && isadir) { 530952a6212SJordan K. Hubbard VREF(vdp); /* we want ourself, ie "." */ 53127a0bc89SDoug Rabson *vpp = vdp; 53227a0bc89SDoug Rabson } else { 533952a6212SJordan K. Hubbard if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 534952a6212SJordan K. Hubbard return (error); 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) { 621710a9accSMax Khon u_int8_t chksum = winChksum(ndep); 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, 651c4f02a89SMax Khon cnt++, chksum, pmp)) 652952a6212SJordan K. Hubbard break; 653952a6212SJordan K. Hubbard } 654952a6212SJordan K. Hubbard } 655952a6212SJordan K. Hubbard 656952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 657952a6212SJordan K. Hubbard return error; 658952a6212SJordan K. Hubbard 659952a6212SJordan K. Hubbard /* 66027a0bc89SDoug Rabson * If they want us to return with the denode gotten. 66127a0bc89SDoug Rabson */ 66227a0bc89SDoug Rabson if (depp) { 663952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) { 664952a6212SJordan K. Hubbard dirclust = dep->de_StartCluster; 665952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 666952a6212SJordan K. Hubbard dirclust = MSDOSFSROOT; 667952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT) 668952a6212SJordan K. Hubbard diroffset = MSDOSFSROOT_OFS; 669952a6212SJordan K. Hubbard else 670952a6212SJordan K. Hubbard diroffset = 0; 67127a0bc89SDoug Rabson } 672952a6212SJordan K. Hubbard return deget(pmp, dirclust, diroffset, depp); 67327a0bc89SDoug Rabson } 674952a6212SJordan K. Hubbard 67527a0bc89SDoug Rabson return 0; 67627a0bc89SDoug Rabson } 67727a0bc89SDoug Rabson 67827a0bc89SDoug Rabson /* 67927a0bc89SDoug Rabson * Be sure a directory is empty except for "." and "..". Return 1 if empty, 68027a0bc89SDoug Rabson * return 0 if not empty or error. 68127a0bc89SDoug Rabson */ 68227a0bc89SDoug Rabson int 68327a0bc89SDoug Rabson dosdirempty(dep) 68427a0bc89SDoug Rabson struct denode *dep; 68527a0bc89SDoug Rabson { 686952a6212SJordan K. Hubbard int blsize; 68727a0bc89SDoug Rabson int error; 68827a0bc89SDoug Rabson u_long cn; 68927a0bc89SDoug Rabson daddr_t bn; 69027a0bc89SDoug Rabson struct buf *bp; 69127a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 69227a0bc89SDoug Rabson struct direntry *dentp; 69327a0bc89SDoug Rabson 69427a0bc89SDoug Rabson /* 69527a0bc89SDoug Rabson * Since the filesize field in directory entries for a directory is 69627a0bc89SDoug Rabson * zero, we just have to feel our way through the directory until 69727a0bc89SDoug Rabson * we hit end of file. 69827a0bc89SDoug Rabson */ 69927a0bc89SDoug Rabson for (cn = 0;; cn++) { 700952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 70127a0bc89SDoug Rabson if (error == E2BIG) 702952a6212SJordan K. Hubbard return (1); /* it's empty */ 703952a6212SJordan K. Hubbard return (0); 704952a6212SJordan K. Hubbard } 705952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 706952a6212SJordan K. Hubbard if (error) { 707952a6212SJordan K. Hubbard brelse(bp); 708952a6212SJordan K. Hubbard return (0); 709952a6212SJordan K. Hubbard } 710952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 711952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 712952a6212SJordan K. Hubbard dentp++) { 713952a6212SJordan K. Hubbard if (dentp->deName[0] != SLOT_DELETED && 714952a6212SJordan K. Hubbard (dentp->deAttributes & ATTR_VOLUME) == 0) { 71527a0bc89SDoug Rabson /* 71627a0bc89SDoug Rabson * In dos directories an entry whose name 71727a0bc89SDoug Rabson * starts with SLOT_EMPTY (0) starts the 71827a0bc89SDoug Rabson * beginning of the unused part of the 71927a0bc89SDoug Rabson * directory, so we can just return that it 72027a0bc89SDoug Rabson * is empty. 72127a0bc89SDoug Rabson */ 72227a0bc89SDoug Rabson if (dentp->deName[0] == SLOT_EMPTY) { 72327a0bc89SDoug Rabson brelse(bp); 724952a6212SJordan K. Hubbard return (1); 72527a0bc89SDoug Rabson } 72627a0bc89SDoug Rabson /* 72727a0bc89SDoug Rabson * Any names other than "." and ".." in a 72827a0bc89SDoug Rabson * directory mean it is not empty. 72927a0bc89SDoug Rabson */ 73027a0bc89SDoug Rabson if (bcmp(dentp->deName, ". ", 11) && 73127a0bc89SDoug Rabson bcmp(dentp->deName, ".. ", 11)) { 73227a0bc89SDoug Rabson brelse(bp); 73327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 734952a6212SJordan K. Hubbard printf("dosdirempty(): entry found %02x, %02x\n", 735952a6212SJordan K. Hubbard dentp->deName[0], dentp->deName[1]); 73627a0bc89SDoug Rabson #endif 737952a6212SJordan K. Hubbard return (0); /* not empty */ 73827a0bc89SDoug Rabson } 73927a0bc89SDoug Rabson } 74027a0bc89SDoug Rabson } 74127a0bc89SDoug Rabson brelse(bp); 74227a0bc89SDoug Rabson } 74327a0bc89SDoug Rabson /* NOTREACHED */ 74427a0bc89SDoug Rabson } 74527a0bc89SDoug Rabson 74627a0bc89SDoug Rabson /* 74727a0bc89SDoug Rabson * Check to see if the directory described by target is in some 74827a0bc89SDoug Rabson * subdirectory of source. This prevents something like the following from 74927a0bc89SDoug Rabson * succeeding and leaving a bunch or files and directories orphaned. mv 75027a0bc89SDoug Rabson * /a/b/c /a/b/c/d/e/f Where c and f are directories. 75127a0bc89SDoug Rabson * 75227a0bc89SDoug Rabson * source - the inode for /a/b/c 75327a0bc89SDoug Rabson * target - the inode for /a/b/c/d/e/f 75427a0bc89SDoug Rabson * 75527a0bc89SDoug Rabson * Returns 0 if target is NOT a subdirectory of source. 75627a0bc89SDoug Rabson * Otherwise returns a non-zero error number. 75727a0bc89SDoug Rabson * The target inode is always unlocked on return. 75827a0bc89SDoug Rabson */ 75927a0bc89SDoug Rabson int 76027a0bc89SDoug Rabson doscheckpath(source, target) 76127a0bc89SDoug Rabson struct denode *source; 76227a0bc89SDoug Rabson struct denode *target; 76327a0bc89SDoug Rabson { 76427a0bc89SDoug Rabson daddr_t scn; 76527a0bc89SDoug Rabson struct msdosfsmount *pmp; 76627a0bc89SDoug Rabson struct direntry *ep; 76727a0bc89SDoug Rabson struct denode *dep; 76827a0bc89SDoug Rabson struct buf *bp = NULL; 76927a0bc89SDoug Rabson int error = 0; 77027a0bc89SDoug Rabson 77127a0bc89SDoug Rabson dep = target; 77227a0bc89SDoug Rabson if ((target->de_Attributes & ATTR_DIRECTORY) == 0 || 77327a0bc89SDoug Rabson (source->de_Attributes & ATTR_DIRECTORY) == 0) { 77427a0bc89SDoug Rabson error = ENOTDIR; 77527a0bc89SDoug Rabson goto out; 77627a0bc89SDoug Rabson } 77727a0bc89SDoug Rabson if (dep->de_StartCluster == source->de_StartCluster) { 77827a0bc89SDoug Rabson error = EEXIST; 77927a0bc89SDoug Rabson goto out; 78027a0bc89SDoug Rabson } 78127a0bc89SDoug Rabson if (dep->de_StartCluster == MSDOSFSROOT) 78227a0bc89SDoug Rabson goto out; 783952a6212SJordan K. Hubbard pmp = dep->de_pmp; 784952a6212SJordan K. Hubbard #ifdef DIAGNOSTIC 785952a6212SJordan K. Hubbard if (pmp != source->de_pmp) 786952a6212SJordan K. Hubbard panic("doscheckpath: source and target on different filesystems"); 787952a6212SJordan K. Hubbard #endif 788952a6212SJordan K. Hubbard if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk) 789952a6212SJordan K. Hubbard goto out; 790952a6212SJordan K. Hubbard 79127a0bc89SDoug Rabson for (;;) { 79227a0bc89SDoug Rabson if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) { 79327a0bc89SDoug Rabson error = ENOTDIR; 794952a6212SJordan K. Hubbard break; 79527a0bc89SDoug Rabson } 79627a0bc89SDoug Rabson scn = dep->de_StartCluster; 79727a0bc89SDoug Rabson error = bread(pmp->pm_devvp, cntobn(pmp, scn), 79827a0bc89SDoug Rabson pmp->pm_bpcluster, NOCRED, &bp); 799952a6212SJordan K. Hubbard if (error) 80027a0bc89SDoug Rabson break; 801952a6212SJordan K. Hubbard 80227a0bc89SDoug Rabson ep = (struct direntry *) bp->b_data + 1; 80327a0bc89SDoug Rabson if ((ep->deAttributes & ATTR_DIRECTORY) == 0 || 80427a0bc89SDoug Rabson bcmp(ep->deName, ".. ", 11) != 0) { 80527a0bc89SDoug Rabson error = ENOTDIR; 80627a0bc89SDoug Rabson break; 80727a0bc89SDoug Rabson } 80827a0bc89SDoug Rabson scn = getushort(ep->deStartCluster); 809952a6212SJordan K. Hubbard if (FAT32(pmp)) 810952a6212SJordan K. Hubbard scn |= getushort(ep->deHighClust) << 16; 811952a6212SJordan K. Hubbard 81227a0bc89SDoug Rabson if (scn == source->de_StartCluster) { 81327a0bc89SDoug Rabson error = EINVAL; 81427a0bc89SDoug Rabson break; 81527a0bc89SDoug Rabson } 81627a0bc89SDoug Rabson if (scn == MSDOSFSROOT) 81727a0bc89SDoug Rabson break; 818952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == pmp->pm_rootdirblk) { 819952a6212SJordan K. Hubbard /* 820952a6212SJordan K. Hubbard * scn should be 0 in this case, 821952a6212SJordan K. Hubbard * but we silently ignore the error. 822952a6212SJordan K. Hubbard */ 823952a6212SJordan K. Hubbard break; 824952a6212SJordan K. Hubbard } 825952a6212SJordan K. Hubbard 82627a0bc89SDoug Rabson vput(DETOV(dep)); 82727a0bc89SDoug Rabson brelse(bp); 82827a0bc89SDoug Rabson bp = NULL; 829952a6212SJordan K. Hubbard /* NOTE: deget() clears dep on error */ 830952a6212SJordan K. Hubbard if ((error = deget(pmp, scn, 0, &dep)) != 0) 83127a0bc89SDoug Rabson break; 83227a0bc89SDoug Rabson } 83327a0bc89SDoug Rabson out:; 83427a0bc89SDoug Rabson if (bp) 83527a0bc89SDoug Rabson brelse(bp); 83627a0bc89SDoug Rabson if (error == ENOTDIR) 83727a0bc89SDoug Rabson printf("doscheckpath(): .. not a directory?\n"); 83827a0bc89SDoug Rabson if (dep != NULL) 83927a0bc89SDoug Rabson vput(DETOV(dep)); 840952a6212SJordan K. Hubbard return (error); 84127a0bc89SDoug Rabson } 84227a0bc89SDoug Rabson 84327a0bc89SDoug Rabson /* 84427a0bc89SDoug Rabson * Read in the disk block containing the directory entry (dirclu, dirofs) 84527a0bc89SDoug Rabson * and return the address of the buf header, and the address of the 84627a0bc89SDoug Rabson * directory entry within the block. 84727a0bc89SDoug Rabson */ 84827a0bc89SDoug Rabson int 849952a6212SJordan K. Hubbard readep(pmp, dirclust, diroffset, bpp, epp) 85027a0bc89SDoug Rabson struct msdosfsmount *pmp; 851952a6212SJordan K. Hubbard u_long dirclust, diroffset; 85227a0bc89SDoug Rabson struct buf **bpp; 85327a0bc89SDoug Rabson struct direntry **epp; 85427a0bc89SDoug Rabson { 85527a0bc89SDoug Rabson int error; 85627a0bc89SDoug Rabson daddr_t bn; 857952a6212SJordan K. Hubbard int blsize; 85827a0bc89SDoug Rabson 859952a6212SJordan K. Hubbard blsize = pmp->pm_bpcluster; 860952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT 861952a6212SJordan K. Hubbard && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 862952a6212SJordan K. Hubbard blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 863952a6212SJordan K. Hubbard bn = detobn(pmp, dirclust, diroffset); 864952a6212SJordan K. Hubbard if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 865952a6212SJordan K. Hubbard brelse(*bpp); 86627a0bc89SDoug Rabson *bpp = NULL; 867952a6212SJordan K. Hubbard return (error); 86827a0bc89SDoug Rabson } 86927a0bc89SDoug Rabson if (epp) 870952a6212SJordan K. Hubbard *epp = bptoep(pmp, *bpp, diroffset); 871952a6212SJordan K. Hubbard return (0); 87227a0bc89SDoug Rabson } 87327a0bc89SDoug Rabson 87427a0bc89SDoug Rabson /* 87527a0bc89SDoug Rabson * Read in the disk block containing the directory entry dep came from and 87627a0bc89SDoug Rabson * return the address of the buf header, and the address of the directory 87727a0bc89SDoug Rabson * entry within the block. 87827a0bc89SDoug Rabson */ 87927a0bc89SDoug Rabson int 88027a0bc89SDoug Rabson readde(dep, bpp, epp) 88127a0bc89SDoug Rabson struct denode *dep; 88227a0bc89SDoug Rabson struct buf **bpp; 88327a0bc89SDoug Rabson struct direntry **epp; 88427a0bc89SDoug Rabson { 885952a6212SJordan K. Hubbard 886952a6212SJordan K. Hubbard return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 887952a6212SJordan K. Hubbard bpp, epp)); 888952a6212SJordan K. Hubbard } 889952a6212SJordan K. Hubbard 890952a6212SJordan K. Hubbard /* 891952a6212SJordan K. Hubbard * Remove a directory entry. At this point the file represented by the 892952a6212SJordan K. Hubbard * directory entry to be removed is still full length until noone has it 893952a6212SJordan K. Hubbard * open. When the file no longer being used msdosfs_inactive() is called 894952a6212SJordan K. Hubbard * and will truncate the file to 0 length. When the vnode containing the 895952a6212SJordan K. Hubbard * denode is needed for some other purpose by VFS it will call 896952a6212SJordan K. Hubbard * msdosfs_reclaim() which will remove the denode from the denode cache. 897952a6212SJordan K. Hubbard */ 898952a6212SJordan K. Hubbard int 899952a6212SJordan K. Hubbard removede(pdep, dep) 900952a6212SJordan K. Hubbard struct denode *pdep; /* directory where the entry is removed */ 901952a6212SJordan K. Hubbard struct denode *dep; /* file to be removed */ 902952a6212SJordan K. Hubbard { 903952a6212SJordan K. Hubbard int error; 904952a6212SJordan K. Hubbard struct direntry *ep; 905952a6212SJordan K. Hubbard struct buf *bp; 906952a6212SJordan K. Hubbard daddr_t bn; 907952a6212SJordan K. Hubbard int blsize; 908952a6212SJordan K. Hubbard struct msdosfsmount *pmp = pdep->de_pmp; 909952a6212SJordan K. Hubbard u_long offset = pdep->de_fndoffset; 910952a6212SJordan K. Hubbard 911952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 912952a6212SJordan K. Hubbard printf("removede(): filename %s, dep %p, offset %08lx\n", 913952a6212SJordan K. Hubbard dep->de_Name, dep, offset); 914952a6212SJordan K. Hubbard #endif 915952a6212SJordan K. Hubbard 916952a6212SJordan K. Hubbard dep->de_refcnt--; 917952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 918952a6212SJordan K. Hubbard do { 919952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 920952a6212SJordan K. Hubbard error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize); 921952a6212SJordan K. Hubbard if (error) 922952a6212SJordan K. Hubbard return error; 923952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 924952a6212SJordan K. Hubbard if (error) { 925952a6212SJordan K. Hubbard brelse(bp); 926952a6212SJordan K. Hubbard return error; 927952a6212SJordan K. Hubbard } 928952a6212SJordan K. Hubbard ep = bptoep(pmp, bp, offset); 929952a6212SJordan K. Hubbard /* 930952a6212SJordan K. Hubbard * Check whether, if we came here the second time, i.e. 931952a6212SJordan K. Hubbard * when underflowing into the previous block, the last 932952a6212SJordan K. Hubbard * entry in this block is a longfilename entry, too. 933952a6212SJordan K. Hubbard */ 934952a6212SJordan K. Hubbard if (ep->deAttributes != ATTR_WIN95 935952a6212SJordan K. Hubbard && offset != pdep->de_fndoffset) { 936952a6212SJordan K. Hubbard brelse(bp); 937952a6212SJordan K. Hubbard break; 938952a6212SJordan K. Hubbard } 939952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 940952a6212SJordan K. Hubbard while (1) { 941952a6212SJordan K. Hubbard /* 942952a6212SJordan K. Hubbard * We are a bit agressive here in that we delete any Win95 943952a6212SJordan K. Hubbard * entries preceding this entry, not just the ones we "own". 944952a6212SJordan K. Hubbard * Since these presumably aren't valid anyway, 945952a6212SJordan K. Hubbard * there should be no harm. 946952a6212SJordan K. Hubbard */ 947952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 948952a6212SJordan K. Hubbard ep--->deName[0] = SLOT_DELETED; 949952a6212SJordan K. Hubbard if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) 950952a6212SJordan K. Hubbard || !(offset & pmp->pm_crbomask) 951952a6212SJordan K. Hubbard || ep->deAttributes != ATTR_WIN95) 952952a6212SJordan K. Hubbard break; 953952a6212SJordan K. Hubbard } 954952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 955952a6212SJordan K. Hubbard return error; 956952a6212SJordan K. Hubbard } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95) 957952a6212SJordan K. Hubbard && !(offset & pmp->pm_crbomask) 958952a6212SJordan K. Hubbard && offset); 959952a6212SJordan K. Hubbard return 0; 960952a6212SJordan K. Hubbard } 961952a6212SJordan K. Hubbard 962952a6212SJordan K. Hubbard /* 963952a6212SJordan K. Hubbard * Create a unique DOS name in dvp 964952a6212SJordan K. Hubbard */ 965952a6212SJordan K. Hubbard int 966952a6212SJordan K. Hubbard uniqdosname(dep, cnp, cp) 967952a6212SJordan K. Hubbard struct denode *dep; 968952a6212SJordan K. Hubbard struct componentname *cnp; 969952a6212SJordan K. Hubbard u_char *cp; 970952a6212SJordan K. Hubbard { 971952a6212SJordan K. Hubbard struct msdosfsmount *pmp = dep->de_pmp; 972952a6212SJordan K. Hubbard struct direntry *dentp; 973952a6212SJordan K. Hubbard int gen; 974952a6212SJordan K. Hubbard int blsize; 975952a6212SJordan K. Hubbard u_long cn; 976952a6212SJordan K. Hubbard daddr_t bn; 977952a6212SJordan K. Hubbard struct buf *bp; 978952a6212SJordan K. Hubbard int error; 979952a6212SJordan K. Hubbard 980af9f1d50SDmitrij Tejblum if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 981011cdb57SDmitrij Tejblum return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 982c4f02a89SMax Khon cnp->cn_namelen, 0, pmp) ? 0 : EINVAL); 983011cdb57SDmitrij Tejblum 984952a6212SJordan K. Hubbard for (gen = 1;; gen++) { 985952a6212SJordan K. Hubbard /* 986952a6212SJordan K. Hubbard * Generate DOS name with generation number 987952a6212SJordan K. Hubbard */ 988952a6212SJordan K. Hubbard if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 989c4f02a89SMax Khon cnp->cn_namelen, gen, pmp)) 990952a6212SJordan K. Hubbard return gen == 1 ? EINVAL : EEXIST; 991952a6212SJordan K. Hubbard 992952a6212SJordan K. Hubbard /* 993952a6212SJordan K. Hubbard * Now look for a dir entry with this exact name 994952a6212SJordan K. Hubbard */ 995952a6212SJordan K. Hubbard for (cn = error = 0; !error; cn++) { 996952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 997952a6212SJordan K. Hubbard if (error == E2BIG) /* EOF reached and not found */ 998952a6212SJordan K. Hubbard return 0; 999952a6212SJordan K. Hubbard return error; 1000952a6212SJordan K. Hubbard } 1001952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 1002952a6212SJordan K. Hubbard if (error) { 1003952a6212SJordan K. Hubbard brelse(bp); 1004952a6212SJordan K. Hubbard return error; 1005952a6212SJordan K. Hubbard } 1006952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 1007952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 1008952a6212SJordan K. Hubbard dentp++) { 1009952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_EMPTY) { 1010952a6212SJordan K. Hubbard /* 1011952a6212SJordan K. Hubbard * Last used entry and not found 1012952a6212SJordan K. Hubbard */ 1013952a6212SJordan K. Hubbard brelse(bp); 1014952a6212SJordan K. Hubbard return 0; 1015952a6212SJordan K. Hubbard } 1016952a6212SJordan K. Hubbard /* 1017952a6212SJordan K. Hubbard * Ignore volume labels and Win95 entries 1018952a6212SJordan K. Hubbard */ 1019952a6212SJordan K. Hubbard if (dentp->deAttributes & ATTR_VOLUME) 1020952a6212SJordan K. Hubbard continue; 1021952a6212SJordan K. Hubbard if (!bcmp(dentp->deName, cp, 11)) { 1022952a6212SJordan K. Hubbard error = EEXIST; 1023952a6212SJordan K. Hubbard break; 1024952a6212SJordan K. Hubbard } 1025952a6212SJordan K. Hubbard } 1026952a6212SJordan K. Hubbard brelse(bp); 1027952a6212SJordan K. Hubbard } 1028952a6212SJordan K. Hubbard } 1029952a6212SJordan K. Hubbard } 1030952a6212SJordan K. Hubbard 1031952a6212SJordan K. Hubbard /* 1032952a6212SJordan K. Hubbard * Find any Win'95 long filename entry in directory dep 1033952a6212SJordan K. Hubbard */ 1034952a6212SJordan K. Hubbard int 1035952a6212SJordan K. Hubbard findwin95(dep) 1036952a6212SJordan K. Hubbard struct denode *dep; 1037952a6212SJordan K. Hubbard { 1038952a6212SJordan K. Hubbard struct msdosfsmount *pmp = dep->de_pmp; 1039952a6212SJordan K. Hubbard struct direntry *dentp; 104057081f7bSDmitrij Tejblum int blsize, win95; 1041952a6212SJordan K. Hubbard u_long cn; 1042952a6212SJordan K. Hubbard daddr_t bn; 1043952a6212SJordan K. Hubbard struct buf *bp; 1044952a6212SJordan K. Hubbard 104557081f7bSDmitrij Tejblum win95 = 1; 1046952a6212SJordan K. Hubbard /* 1047952a6212SJordan K. Hubbard * Read through the directory looking for Win'95 entries 1048952a6212SJordan K. Hubbard * Note: Error currently handled just as EOF XXX 1049952a6212SJordan K. Hubbard */ 1050952a6212SJordan K. Hubbard for (cn = 0;; cn++) { 1051952a6212SJordan K. Hubbard if (pcbmap(dep, cn, &bn, 0, &blsize)) 105257081f7bSDmitrij Tejblum return (win95); 1053952a6212SJordan K. Hubbard if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) { 1054952a6212SJordan K. Hubbard brelse(bp); 105557081f7bSDmitrij Tejblum return (win95); 1056952a6212SJordan K. Hubbard } 1057952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 1058952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 1059952a6212SJordan K. Hubbard dentp++) { 1060952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_EMPTY) { 1061952a6212SJordan K. Hubbard /* 1062952a6212SJordan K. Hubbard * Last used entry and not found 1063952a6212SJordan K. Hubbard */ 1064952a6212SJordan K. Hubbard brelse(bp); 106557081f7bSDmitrij Tejblum return (win95); 1066952a6212SJordan K. Hubbard } 1067952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_DELETED) { 1068952a6212SJordan K. Hubbard /* 1069952a6212SJordan K. Hubbard * Ignore deleted files 1070952a6212SJordan K. Hubbard * Note: might be an indication of Win'95 anyway XXX 1071952a6212SJordan K. Hubbard */ 1072952a6212SJordan K. Hubbard continue; 1073952a6212SJordan K. Hubbard } 1074952a6212SJordan K. Hubbard if (dentp->deAttributes == ATTR_WIN95) { 1075952a6212SJordan K. Hubbard brelse(bp); 1076952a6212SJordan K. Hubbard return 1; 1077952a6212SJordan K. Hubbard } 107857081f7bSDmitrij Tejblum win95 = 0; 1079952a6212SJordan K. Hubbard } 1080952a6212SJordan K. Hubbard brelse(bp); 1081952a6212SJordan K. Hubbard } 108227a0bc89SDoug Rabson } 1083