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/namei.h> 549626b608SPoul-Henning Kamp #include <sys/bio.h> 5527a0bc89SDoug Rabson #include <sys/buf.h> 5627a0bc89SDoug Rabson #include <sys/vnode.h> 5727a0bc89SDoug Rabson #include <sys/mount.h> 5827a0bc89SDoug Rabson 591166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h> 60c4f02a89SMax Khon #include <fs/msdosfs/msdosfsmount.h> 611166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h> 621166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h> 631166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h> 6427a0bc89SDoug Rabson 6527a0bc89SDoug Rabson /* 6627a0bc89SDoug Rabson * When we search a directory the blocks containing directory entries are 6727a0bc89SDoug Rabson * read and examined. The directory entries contain information that would 6827a0bc89SDoug Rabson * normally be in the inode of a unix filesystem. This means that some of 6927a0bc89SDoug Rabson * a directory's contents may also be in memory resident denodes (sort of 7027a0bc89SDoug Rabson * an inode). This can cause problems if we are searching while some other 7127a0bc89SDoug Rabson * process is modifying a directory. To prevent one process from accessing 7227a0bc89SDoug Rabson * incompletely modified directory information we depend upon being the 73b84d2921SPoul-Henning Kamp * sole owner of a directory block. bread/brelse provide this service. 7427a0bc89SDoug Rabson * This being the case, when a process modifies a directory it must first 7527a0bc89SDoug Rabson * acquire the disk block that contains the directory entry to be modified. 7627a0bc89SDoug Rabson * Then update the disk block and the denode, and then write the disk block 7727a0bc89SDoug Rabson * out to disk. This way disk blocks containing directory entries and in 7827a0bc89SDoug Rabson * memory denode's will be in synch. 7927a0bc89SDoug Rabson */ 8027a0bc89SDoug Rabson int 8127a0bc89SDoug Rabson msdosfs_lookup(ap) 820fa2443fSPoul-Henning Kamp struct vop_cachedlookup_args /* { 8327a0bc89SDoug Rabson struct vnode *a_dvp; 8427a0bc89SDoug Rabson struct vnode **a_vpp; 8527a0bc89SDoug Rabson struct componentname *a_cnp; 8627a0bc89SDoug Rabson } */ *ap; 8727a0bc89SDoug Rabson { 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; 189c4f02a89SMax Khon mbnambuf_init(); 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; 229c4f02a89SMax Khon mbnambuf_init(); 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 254c4f02a89SMax Khon chksum = win2unixfn((struct winentry *)dep, 255c4f02a89SMax Khon chksum, 256c4f02a89SMax Khon pmp); 257c4f02a89SMax Khon continue; 258c4f02a89SMax Khon } 259c4f02a89SMax Khon 260952a6212SJordan K. Hubbard chksum = winChkName((const u_char *)cnp->cn_nameptr, 26169a36f24SMike Smith unlen, 2624d63452fSAndrey A. Chernov chksum, 263c4f02a89SMax Khon pmp); 264c4f02a89SMax Khon if (chksum == -2) { 265c4f02a89SMax Khon chksum = -1; 266952a6212SJordan K. Hubbard continue; 267952a6212SJordan K. Hubbard } 268952a6212SJordan K. Hubbard 269952a6212SJordan K. Hubbard /* 27027a0bc89SDoug Rabson * Ignore volume labels (anywhere, not just 27127a0bc89SDoug Rabson * the root directory). 27227a0bc89SDoug Rabson */ 273952a6212SJordan K. Hubbard if (dep->deAttributes & ATTR_VOLUME) { 274952a6212SJordan K. Hubbard chksum = -1; 275952a6212SJordan K. Hubbard continue; 276952a6212SJordan K. Hubbard } 277952a6212SJordan K. Hubbard 278952a6212SJordan K. Hubbard /* 279952a6212SJordan K. Hubbard * Check for a checksum or name match 280952a6212SJordan K. Hubbard */ 2816a5bf04aSTim J. Robbins chksum_ok = (chksum == winChksum(dep->deName)); 2826a5bf04aSTim J. Robbins if (!chksum_ok 283952a6212SJordan K. Hubbard && (!olddos || bcmp(dosfilename, dep->deName, 11))) { 284952a6212SJordan K. Hubbard chksum = -1; 285952a6212SJordan K. Hubbard continue; 286952a6212SJordan K. Hubbard } 28727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 288952a6212SJordan K. Hubbard printf("msdosfs_lookup(): match blkoff %d, diroff %d\n", 289952a6212SJordan K. Hubbard blkoff, diroff); 29027a0bc89SDoug Rabson #endif 29127a0bc89SDoug Rabson /* 29227a0bc89SDoug Rabson * Remember where this directory 29327a0bc89SDoug Rabson * entry came from for whoever did 294952a6212SJordan K. Hubbard * this lookup. 29527a0bc89SDoug Rabson */ 29627a0bc89SDoug Rabson dp->de_fndoffset = diroff; 2976a5bf04aSTim J. Robbins if (chksum_ok && nameiop == RENAME) { 2986a5bf04aSTim J. Robbins /* 2996a5bf04aSTim J. Robbins * Target had correct long name 3006a5bf04aSTim J. Robbins * directory entries, reuse them 3016a5bf04aSTim J. Robbins * as needed. 3026a5bf04aSTim J. Robbins */ 303191e6fd0SDmitrij Tejblum dp->de_fndcnt = wincnt - 1; 3046a5bf04aSTim J. Robbins } else { 3056a5bf04aSTim J. Robbins /* 3066a5bf04aSTim J. Robbins * Long name directory entries 3076a5bf04aSTim J. Robbins * not present or corrupt, can only 3086a5bf04aSTim J. Robbins * reuse dos directory entry. 3096a5bf04aSTim J. Robbins */ 3106a5bf04aSTim J. Robbins dp->de_fndcnt = 0; 3116a5bf04aSTim J. Robbins } 312952a6212SJordan K. Hubbard 31327a0bc89SDoug Rabson goto found; 31427a0bc89SDoug Rabson } 315952a6212SJordan K. Hubbard } /* for (blkoff = 0; .... */ 31627a0bc89SDoug Rabson /* 31727a0bc89SDoug Rabson * Release the buffer holding the directory cluster just 31827a0bc89SDoug Rabson * searched. 31927a0bc89SDoug Rabson */ 32027a0bc89SDoug Rabson brelse(bp); 32127a0bc89SDoug Rabson } /* for (frcn = 0; ; frcn++) */ 322952a6212SJordan K. Hubbard 323952a6212SJordan K. Hubbard notfound: 32427a0bc89SDoug Rabson /* 32527a0bc89SDoug Rabson * We hold no disk buffers at this point. 32627a0bc89SDoug Rabson */ 32727a0bc89SDoug Rabson 32827a0bc89SDoug Rabson /* 329952a6212SJordan K. Hubbard * Fixup the slot description to point to the place where 330952a6212SJordan K. Hubbard * we might put the new DOS direntry (putting the Win95 331952a6212SJordan K. Hubbard * long name entries before that) 332952a6212SJordan K. Hubbard */ 333952a6212SJordan K. Hubbard if (!slotcount) { 334952a6212SJordan K. Hubbard slotcount = 1; 335952a6212SJordan K. Hubbard slotoffset = diroff; 336952a6212SJordan K. Hubbard } 337952a6212SJordan K. Hubbard if (wincnt > slotcount) 338952a6212SJordan K. Hubbard slotoffset += sizeof(struct direntry) * (wincnt - slotcount); 339952a6212SJordan K. Hubbard 340952a6212SJordan K. Hubbard /* 34127a0bc89SDoug Rabson * If we get here we didn't find the entry we were looking for. But 34227a0bc89SDoug Rabson * that's ok if we are creating or renaming and are at the end of 34327a0bc89SDoug Rabson * the pathname and the directory hasn't been removed. 34427a0bc89SDoug Rabson */ 34527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 346952a6212SJordan K. Hubbard printf("msdosfs_lookup(): op %d, refcnt %ld\n", 347952a6212SJordan K. Hubbard nameiop, dp->de_refcnt); 348952a6212SJordan K. Hubbard printf(" slotcount %d, slotoffset %d\n", 349952a6212SJordan K. Hubbard slotcount, slotoffset); 35027a0bc89SDoug Rabson #endif 35127a0bc89SDoug Rabson if ((nameiop == CREATE || nameiop == RENAME) && 35227a0bc89SDoug Rabson (flags & ISLASTCN) && dp->de_refcnt != 0) { 353952a6212SJordan K. Hubbard /* 354952a6212SJordan K. Hubbard * Access for write is interpreted as allowing 355952a6212SJordan K. Hubbard * creation of files in the directory. 356952a6212SJordan K. Hubbard */ 357b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 358d8762fa6SBruce Evans if (error) 359952a6212SJordan K. Hubbard return (error); 360952a6212SJordan K. Hubbard /* 361952a6212SJordan K. Hubbard * Return an indication of where the new directory 362952a6212SJordan K. Hubbard * entry should be put. 363952a6212SJordan K. Hubbard */ 36427a0bc89SDoug Rabson dp->de_fndoffset = slotoffset; 365952a6212SJordan K. Hubbard dp->de_fndcnt = wincnt - 1; 366952a6212SJordan K. Hubbard 367952a6212SJordan K. Hubbard /* 368952a6212SJordan K. Hubbard * We return with the directory locked, so that 369952a6212SJordan K. Hubbard * the parameters we set up above will still be 370952a6212SJordan K. Hubbard * valid if we actually decide to do a direnter(). 371952a6212SJordan K. Hubbard * We return ni_vp == NULL to indicate that the entry 372952a6212SJordan K. Hubbard * does not currently exist; we leave a pointer to 373952a6212SJordan K. Hubbard * the (locked) directory inode in ndp->ni_dvp. 374952a6212SJordan K. Hubbard * The pathname buffer is saved so that the name 375952a6212SJordan K. Hubbard * can be obtained later. 376952a6212SJordan K. Hubbard * 377952a6212SJordan K. Hubbard * NB - if the directory is unlocked, then this 378952a6212SJordan K. Hubbard * information cannot be used. 379952a6212SJordan K. Hubbard */ 38027a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 381952a6212SJordan K. Hubbard return (EJUSTRETURN); 38227a0bc89SDoug Rabson } 38323e8fcafSDavid Schultz #if 0 38427a0bc89SDoug Rabson /* 385952a6212SJordan K. Hubbard * Insert name into cache (as non-existent) if appropriate. 38623e8fcafSDavid Schultz * 38723e8fcafSDavid Schultz * XXX Negative caching is broken for msdosfs because the name 38823e8fcafSDavid Schultz * cache doesn't understand peculiarities such as case insensitivity 38923e8fcafSDavid Schultz * and 8.3 filenames. Hence, it may not invalidate all negative 39023e8fcafSDavid Schultz * entries if a file with this name is later created. 39127a0bc89SDoug Rabson */ 39227a0bc89SDoug Rabson if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) 39327a0bc89SDoug Rabson cache_enter(vdp, *vpp, cnp); 39423e8fcafSDavid Schultz #endif 395952a6212SJordan K. Hubbard return (ENOENT); 39627a0bc89SDoug Rabson 397952a6212SJordan K. Hubbard found: 39827a0bc89SDoug Rabson /* 39927a0bc89SDoug Rabson * NOTE: We still have the buffer with matched directory entry at 40027a0bc89SDoug Rabson * this point. 40127a0bc89SDoug Rabson */ 40227a0bc89SDoug Rabson isadir = dep->deAttributes & ATTR_DIRECTORY; 40327a0bc89SDoug Rabson scn = getushort(dep->deStartCluster); 404952a6212SJordan K. Hubbard if (FAT32(pmp)) { 405952a6212SJordan K. Hubbard scn |= getushort(dep->deHighClust) << 16; 406952a6212SJordan K. Hubbard if (scn == pmp->pm_rootdirblk) { 407952a6212SJordan K. Hubbard /* 408952a6212SJordan K. Hubbard * There should actually be 0 here. 409952a6212SJordan K. Hubbard * Just ignore the error. 410952a6212SJordan K. Hubbard */ 411952a6212SJordan K. Hubbard scn = MSDOSFSROOT; 412952a6212SJordan K. Hubbard } 413952a6212SJordan K. Hubbard } 41427a0bc89SDoug Rabson 415952a6212SJordan K. Hubbard if (isadir) { 416952a6212SJordan K. Hubbard cluster = scn; 417952a6212SJordan K. Hubbard if (cluster == MSDOSFSROOT) 418952a6212SJordan K. Hubbard blkoff = MSDOSFSROOT_OFS; 419952a6212SJordan K. Hubbard else 420952a6212SJordan K. Hubbard blkoff = 0; 421952a6212SJordan K. Hubbard } else if (cluster == MSDOSFSROOT) 422952a6212SJordan K. Hubbard blkoff = diroff; 423952a6212SJordan K. Hubbard 424952a6212SJordan K. Hubbard /* 425952a6212SJordan K. Hubbard * Now release buf to allow deget to read the entry again. 426952a6212SJordan K. Hubbard * Reserving it here and giving it to deget could result 427952a6212SJordan K. Hubbard * in a deadlock. 428952a6212SJordan K. Hubbard */ 429952a6212SJordan K. Hubbard brelse(bp); 430952a6212SJordan K. Hubbard bp = 0; 431952a6212SJordan K. Hubbard 432952a6212SJordan K. Hubbard foundroot: 43327a0bc89SDoug Rabson /* 43427a0bc89SDoug Rabson * If we entered at foundroot, then we are looking for the . or .. 43527a0bc89SDoug Rabson * entry of the filesystems root directory. isadir and scn were 436952a6212SJordan K. Hubbard * setup before jumping here. And, bp is already null. 43727a0bc89SDoug Rabson */ 438952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == MSDOSFSROOT) 439952a6212SJordan K. Hubbard scn = pmp->pm_rootdirblk; 44027a0bc89SDoug Rabson 44127a0bc89SDoug Rabson /* 442952a6212SJordan K. Hubbard * If deleting, and at end of pathname, return 443952a6212SJordan K. Hubbard * parameters which can be used to remove file. 44427a0bc89SDoug Rabson */ 44527a0bc89SDoug Rabson if (nameiop == DELETE && (flags & ISLASTCN)) { 446952a6212SJordan K. Hubbard /* 447952a6212SJordan K. Hubbard * Don't allow deleting the root. 448952a6212SJordan K. Hubbard */ 449952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 450952a6212SJordan K. Hubbard return EROFS; /* really? XXX */ 451952a6212SJordan K. Hubbard 452952a6212SJordan K. Hubbard /* 453952a6212SJordan K. Hubbard * Write access to directory required to delete files. 454952a6212SJordan K. Hubbard */ 455b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 456952a6212SJordan K. Hubbard if (error) 457952a6212SJordan K. Hubbard return (error); 458952a6212SJordan K. Hubbard 459952a6212SJordan K. Hubbard /* 460952a6212SJordan K. Hubbard * Return pointer to current entry in dp->i_offset. 461952a6212SJordan K. Hubbard * Save directory inode pointer in ndp->ni_dvp for dirremove(). 462952a6212SJordan K. Hubbard */ 46327a0bc89SDoug Rabson if (dp->de_StartCluster == scn && isadir) { /* "." */ 46427a0bc89SDoug Rabson VREF(vdp); 46527a0bc89SDoug Rabson *vpp = vdp; 466952a6212SJordan K. Hubbard return (0); 46727a0bc89SDoug Rabson } 468952a6212SJordan K. Hubbard error = deget(pmp, cluster, blkoff, &tdp); 469952a6212SJordan K. Hubbard if (error) 470952a6212SJordan K. Hubbard return (error); 47127a0bc89SDoug Rabson *vpp = DETOV(tdp); 472952a6212SJordan K. Hubbard return (0); 47327a0bc89SDoug Rabson } 47427a0bc89SDoug Rabson 47527a0bc89SDoug Rabson /* 476952a6212SJordan K. Hubbard * If rewriting (RENAME), return the inode and the 477952a6212SJordan K. Hubbard * information required to rewrite the present directory 478952a6212SJordan K. Hubbard * Must get inode of directory entry to verify it's a 479952a6212SJordan K. Hubbard * regular file, or empty directory. 48027a0bc89SDoug Rabson */ 481fcc9c112SJeff Roberson if (nameiop == RENAME && (flags & ISLASTCN)) { 482952a6212SJordan K. Hubbard if (blkoff == MSDOSFSROOT_OFS) 483952a6212SJordan K. Hubbard return EROFS; /* really? XXX */ 484952a6212SJordan K. Hubbard 485b40ce416SJulian Elischer error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread); 486952a6212SJordan K. Hubbard if (error) 487952a6212SJordan K. Hubbard return (error); 488952a6212SJordan K. Hubbard 489952a6212SJordan K. Hubbard /* 490952a6212SJordan K. Hubbard * Careful about locking second inode. 491952a6212SJordan K. Hubbard * This can only occur if the target is ".". 492952a6212SJordan K. Hubbard */ 493952a6212SJordan K. Hubbard if (dp->de_StartCluster == scn && isadir) 494952a6212SJordan K. Hubbard return (EISDIR); 495952a6212SJordan K. Hubbard 496952a6212SJordan K. Hubbard if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 497952a6212SJordan K. Hubbard return (error); 49827a0bc89SDoug Rabson *vpp = DETOV(tdp); 49927a0bc89SDoug Rabson cnp->cn_flags |= SAVENAME; 500952a6212SJordan K. Hubbard return (0); 50127a0bc89SDoug Rabson } 50227a0bc89SDoug Rabson 50327a0bc89SDoug Rabson /* 504952a6212SJordan K. Hubbard * Step through the translation in the name. We do not `vput' the 505952a6212SJordan K. Hubbard * directory because we may need it again if a symbolic link 506952a6212SJordan K. Hubbard * is relative to the current directory. Instead we save it 507952a6212SJordan K. Hubbard * unlocked as "pdp". We must get the target inode before unlocking 508952a6212SJordan K. Hubbard * the directory to insure that the inode will not be removed 509952a6212SJordan K. Hubbard * before we get it. We prevent deadlock by always fetching 510952a6212SJordan K. Hubbard * inodes from the root, moving down the directory tree. Thus 511952a6212SJordan K. Hubbard * when following backward pointers ".." we must unlock the 512952a6212SJordan K. Hubbard * parent directory before getting the requested directory. 513952a6212SJordan K. Hubbard * There is a potential race condition here if both the current 514952a6212SJordan K. Hubbard * and parent directories are removed before the VFS_VGET for the 515952a6212SJordan K. Hubbard * inode associated with ".." returns. We hope that this occurs 516952a6212SJordan K. Hubbard * infrequently since we cannot avoid this race condition without 517952a6212SJordan K. Hubbard * implementing a sophisticated deadlock detection algorithm. 518952a6212SJordan K. Hubbard * Note also that this simple deadlock detection scheme will not 519952a6212SJordan K. Hubbard * work if the filesystem has any hard links other than ".." 520952a6212SJordan K. Hubbard * that point backwards in the directory structure. 52127a0bc89SDoug Rabson */ 52227a0bc89SDoug Rabson pdp = vdp; 52327a0bc89SDoug Rabson if (flags & ISDOTDOT) { 524b40ce416SJulian Elischer VOP_UNLOCK(pdp, 0, td); 525952a6212SJordan K. Hubbard error = deget(pmp, cluster, blkoff, &tdp); 526b40ce416SJulian Elischer vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td); 5274585e3acSJeff Roberson if (error) 528952a6212SJordan K. Hubbard return (error); 52927a0bc89SDoug Rabson *vpp = DETOV(tdp); 530952a6212SJordan K. Hubbard } else if (dp->de_StartCluster == scn && isadir) { 531952a6212SJordan K. Hubbard VREF(vdp); /* we want ourself, ie "." */ 53227a0bc89SDoug Rabson *vpp = vdp; 53327a0bc89SDoug Rabson } else { 534952a6212SJordan K. Hubbard if ((error = deget(pmp, cluster, blkoff, &tdp)) != 0) 535952a6212SJordan K. Hubbard return (error); 53627a0bc89SDoug Rabson *vpp = DETOV(tdp); 53727a0bc89SDoug Rabson } 53827a0bc89SDoug Rabson 53927a0bc89SDoug Rabson /* 540952a6212SJordan K. Hubbard * Insert name into cache if appropriate. 54127a0bc89SDoug Rabson */ 54227a0bc89SDoug Rabson if (cnp->cn_flags & MAKEENTRY) 54327a0bc89SDoug Rabson cache_enter(vdp, *vpp, cnp); 544952a6212SJordan K. Hubbard return (0); 54527a0bc89SDoug Rabson } 54627a0bc89SDoug Rabson 54727a0bc89SDoug Rabson /* 54827a0bc89SDoug Rabson * dep - directory entry to copy into the directory 54927a0bc89SDoug Rabson * ddep - directory to add to 55027a0bc89SDoug Rabson * depp - return the address of the denode for the created directory entry 55127a0bc89SDoug Rabson * if depp != 0 552952a6212SJordan K. Hubbard * cnp - componentname needed for Win95 long filenames 55327a0bc89SDoug Rabson */ 55427a0bc89SDoug Rabson int 555952a6212SJordan K. Hubbard createde(dep, ddep, depp, cnp) 55627a0bc89SDoug Rabson struct denode *dep; 55727a0bc89SDoug Rabson struct denode *ddep; 55827a0bc89SDoug Rabson struct denode **depp; 559952a6212SJordan K. Hubbard struct componentname *cnp; 56027a0bc89SDoug Rabson { 56127a0bc89SDoug Rabson int error; 56227a0bc89SDoug Rabson u_long dirclust, diroffset; 56327a0bc89SDoug Rabson struct direntry *ndep; 56427a0bc89SDoug Rabson struct msdosfsmount *pmp = ddep->de_pmp; 56527a0bc89SDoug Rabson struct buf *bp; 566952a6212SJordan K. Hubbard daddr_t bn; 567952a6212SJordan K. Hubbard int blsize; 56827a0bc89SDoug Rabson 56927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 570952a6212SJordan K. Hubbard printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n", 571952a6212SJordan K. Hubbard dep, ddep, depp, cnp); 57227a0bc89SDoug Rabson #endif 57327a0bc89SDoug Rabson 57427a0bc89SDoug Rabson /* 57527a0bc89SDoug Rabson * If no space left in the directory then allocate another cluster 57627a0bc89SDoug Rabson * and chain it onto the end of the file. There is one exception 57727a0bc89SDoug Rabson * to this. That is, if the root directory has no more space it 57827a0bc89SDoug Rabson * can NOT be expanded. extendfile() checks for and fails attempts 57927a0bc89SDoug Rabson * to extend the root directory. We just return an error in that 58027a0bc89SDoug Rabson * case. 58127a0bc89SDoug Rabson */ 582952a6212SJordan K. Hubbard if (ddep->de_fndoffset >= ddep->de_FileSize) { 583952a6212SJordan K. Hubbard diroffset = ddep->de_fndoffset + sizeof(struct direntry) 584952a6212SJordan K. Hubbard - ddep->de_FileSize; 585952a6212SJordan K. Hubbard dirclust = de_clcount(pmp, diroffset); 586952a6212SJordan K. Hubbard error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR); 587952a6212SJordan K. Hubbard if (error) { 588952a6212SJordan K. Hubbard (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED, NULL); 58927a0bc89SDoug Rabson return error; 590952a6212SJordan K. Hubbard } 591952a6212SJordan K. Hubbard 59227a0bc89SDoug Rabson /* 59327a0bc89SDoug Rabson * Update the size of the directory 59427a0bc89SDoug Rabson */ 595952a6212SJordan K. Hubbard ddep->de_FileSize += de_cn2off(pmp, dirclust); 596952a6212SJordan K. Hubbard } 597952a6212SJordan K. Hubbard 59827a0bc89SDoug Rabson /* 599952a6212SJordan K. Hubbard * We just read in the cluster with space. Copy the new directory 60027a0bc89SDoug Rabson * entry in. Then write it to disk. NOTE: DOS directories 60127a0bc89SDoug Rabson * do not get smaller as clusters are emptied. 60227a0bc89SDoug Rabson */ 603952a6212SJordan K. Hubbard error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset), 604952a6212SJordan K. Hubbard &bn, &dirclust, &blsize); 60527a0bc89SDoug Rabson if (error) 60627a0bc89SDoug Rabson return error; 607952a6212SJordan K. Hubbard diroffset = ddep->de_fndoffset; 608952a6212SJordan K. Hubbard if (dirclust != MSDOSFSROOT) 609952a6212SJordan K. Hubbard diroffset &= pmp->pm_crbomask; 610952a6212SJordan K. Hubbard if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) { 611952a6212SJordan K. Hubbard brelse(bp); 612952a6212SJordan K. Hubbard return error; 61327a0bc89SDoug Rabson } 614952a6212SJordan K. Hubbard ndep = bptoep(pmp, bp, ddep->de_fndoffset); 615952a6212SJordan K. Hubbard 61627a0bc89SDoug Rabson DE_EXTERNALIZE(ndep, dep); 61727a0bc89SDoug Rabson 61827a0bc89SDoug Rabson /* 619952a6212SJordan K. Hubbard * Now write the Win95 long name 620952a6212SJordan K. Hubbard */ 621952a6212SJordan K. Hubbard if (ddep->de_fndcnt > 0) { 622952a6212SJordan K. Hubbard u_int8_t chksum = winChksum(ndep->deName); 623952a6212SJordan K. Hubbard const u_char *un = (const u_char *)cnp->cn_nameptr; 624952a6212SJordan K. Hubbard int unlen = cnp->cn_namelen; 625952a6212SJordan K. Hubbard int cnt = 1; 626952a6212SJordan K. Hubbard 627952a6212SJordan K. Hubbard while (--ddep->de_fndcnt >= 0) { 628952a6212SJordan K. Hubbard if (!(ddep->de_fndoffset & pmp->pm_crbomask)) { 629952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 630952a6212SJordan K. Hubbard return error; 631952a6212SJordan K. Hubbard 632952a6212SJordan K. Hubbard ddep->de_fndoffset -= sizeof(struct direntry); 633952a6212SJordan K. Hubbard error = pcbmap(ddep, 634952a6212SJordan K. Hubbard de_cluster(pmp, 635952a6212SJordan K. Hubbard ddep->de_fndoffset), 636952a6212SJordan K. Hubbard &bn, 0, &blsize); 637952a6212SJordan K. Hubbard if (error) 638952a6212SJordan K. Hubbard return error; 639952a6212SJordan K. Hubbard 640952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, 641952a6212SJordan K. Hubbard NOCRED, &bp); 642952a6212SJordan K. Hubbard if (error) { 643952a6212SJordan K. Hubbard brelse(bp); 644952a6212SJordan K. Hubbard return error; 645952a6212SJordan K. Hubbard } 646952a6212SJordan K. Hubbard ndep = bptoep(pmp, bp, ddep->de_fndoffset); 647952a6212SJordan K. Hubbard } else { 648952a6212SJordan K. Hubbard ndep--; 649952a6212SJordan K. Hubbard ddep->de_fndoffset -= sizeof(struct direntry); 650952a6212SJordan K. Hubbard } 65113df76f2SAndrey A. Chernov if (!unix2winfn(un, unlen, (struct winentry *)ndep, 652c4f02a89SMax Khon cnt++, chksum, pmp)) 653952a6212SJordan K. Hubbard break; 654952a6212SJordan K. Hubbard } 655952a6212SJordan K. Hubbard } 656952a6212SJordan K. Hubbard 657952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 658952a6212SJordan K. Hubbard return error; 659952a6212SJordan K. Hubbard 660952a6212SJordan K. Hubbard /* 66127a0bc89SDoug Rabson * If they want us to return with the denode gotten. 66227a0bc89SDoug Rabson */ 66327a0bc89SDoug Rabson if (depp) { 664952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) { 665952a6212SJordan K. Hubbard dirclust = dep->de_StartCluster; 666952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk) 667952a6212SJordan K. Hubbard dirclust = MSDOSFSROOT; 668952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT) 669952a6212SJordan K. Hubbard diroffset = MSDOSFSROOT_OFS; 670952a6212SJordan K. Hubbard else 671952a6212SJordan K. Hubbard diroffset = 0; 67227a0bc89SDoug Rabson } 673952a6212SJordan K. Hubbard return deget(pmp, dirclust, diroffset, depp); 67427a0bc89SDoug Rabson } 675952a6212SJordan K. Hubbard 67627a0bc89SDoug Rabson return 0; 67727a0bc89SDoug Rabson } 67827a0bc89SDoug Rabson 67927a0bc89SDoug Rabson /* 68027a0bc89SDoug Rabson * Be sure a directory is empty except for "." and "..". Return 1 if empty, 68127a0bc89SDoug Rabson * return 0 if not empty or error. 68227a0bc89SDoug Rabson */ 68327a0bc89SDoug Rabson int 68427a0bc89SDoug Rabson dosdirempty(dep) 68527a0bc89SDoug Rabson struct denode *dep; 68627a0bc89SDoug Rabson { 687952a6212SJordan K. Hubbard int blsize; 68827a0bc89SDoug Rabson int error; 68927a0bc89SDoug Rabson u_long cn; 69027a0bc89SDoug Rabson daddr_t bn; 69127a0bc89SDoug Rabson struct buf *bp; 69227a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 69327a0bc89SDoug Rabson struct direntry *dentp; 69427a0bc89SDoug Rabson 69527a0bc89SDoug Rabson /* 69627a0bc89SDoug Rabson * Since the filesize field in directory entries for a directory is 69727a0bc89SDoug Rabson * zero, we just have to feel our way through the directory until 69827a0bc89SDoug Rabson * we hit end of file. 69927a0bc89SDoug Rabson */ 70027a0bc89SDoug Rabson for (cn = 0;; cn++) { 701952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 70227a0bc89SDoug Rabson if (error == E2BIG) 703952a6212SJordan K. Hubbard return (1); /* it's empty */ 704952a6212SJordan K. Hubbard return (0); 705952a6212SJordan K. Hubbard } 706952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 707952a6212SJordan K. Hubbard if (error) { 708952a6212SJordan K. Hubbard brelse(bp); 709952a6212SJordan K. Hubbard return (0); 710952a6212SJordan K. Hubbard } 711952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 712952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 713952a6212SJordan K. Hubbard dentp++) { 714952a6212SJordan K. Hubbard if (dentp->deName[0] != SLOT_DELETED && 715952a6212SJordan K. Hubbard (dentp->deAttributes & ATTR_VOLUME) == 0) { 71627a0bc89SDoug Rabson /* 71727a0bc89SDoug Rabson * In dos directories an entry whose name 71827a0bc89SDoug Rabson * starts with SLOT_EMPTY (0) starts the 71927a0bc89SDoug Rabson * beginning of the unused part of the 72027a0bc89SDoug Rabson * directory, so we can just return that it 72127a0bc89SDoug Rabson * is empty. 72227a0bc89SDoug Rabson */ 72327a0bc89SDoug Rabson if (dentp->deName[0] == SLOT_EMPTY) { 72427a0bc89SDoug Rabson brelse(bp); 725952a6212SJordan K. Hubbard return (1); 72627a0bc89SDoug Rabson } 72727a0bc89SDoug Rabson /* 72827a0bc89SDoug Rabson * Any names other than "." and ".." in a 72927a0bc89SDoug Rabson * directory mean it is not empty. 73027a0bc89SDoug Rabson */ 73127a0bc89SDoug Rabson if (bcmp(dentp->deName, ". ", 11) && 73227a0bc89SDoug Rabson bcmp(dentp->deName, ".. ", 11)) { 73327a0bc89SDoug Rabson brelse(bp); 73427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 735952a6212SJordan K. Hubbard printf("dosdirempty(): entry found %02x, %02x\n", 736952a6212SJordan K. Hubbard dentp->deName[0], dentp->deName[1]); 73727a0bc89SDoug Rabson #endif 738952a6212SJordan K. Hubbard return (0); /* not empty */ 73927a0bc89SDoug Rabson } 74027a0bc89SDoug Rabson } 74127a0bc89SDoug Rabson } 74227a0bc89SDoug Rabson brelse(bp); 74327a0bc89SDoug Rabson } 74427a0bc89SDoug Rabson /* NOTREACHED */ 74527a0bc89SDoug Rabson } 74627a0bc89SDoug Rabson 74727a0bc89SDoug Rabson /* 74827a0bc89SDoug Rabson * Check to see if the directory described by target is in some 74927a0bc89SDoug Rabson * subdirectory of source. This prevents something like the following from 75027a0bc89SDoug Rabson * succeeding and leaving a bunch or files and directories orphaned. mv 75127a0bc89SDoug Rabson * /a/b/c /a/b/c/d/e/f Where c and f are directories. 75227a0bc89SDoug Rabson * 75327a0bc89SDoug Rabson * source - the inode for /a/b/c 75427a0bc89SDoug Rabson * target - the inode for /a/b/c/d/e/f 75527a0bc89SDoug Rabson * 75627a0bc89SDoug Rabson * Returns 0 if target is NOT a subdirectory of source. 75727a0bc89SDoug Rabson * Otherwise returns a non-zero error number. 75827a0bc89SDoug Rabson * The target inode is always unlocked on return. 75927a0bc89SDoug Rabson */ 76027a0bc89SDoug Rabson int 76127a0bc89SDoug Rabson doscheckpath(source, target) 76227a0bc89SDoug Rabson struct denode *source; 76327a0bc89SDoug Rabson struct denode *target; 76427a0bc89SDoug Rabson { 76527a0bc89SDoug Rabson daddr_t scn; 76627a0bc89SDoug Rabson struct msdosfsmount *pmp; 76727a0bc89SDoug Rabson struct direntry *ep; 76827a0bc89SDoug Rabson struct denode *dep; 76927a0bc89SDoug Rabson struct buf *bp = NULL; 77027a0bc89SDoug Rabson int error = 0; 77127a0bc89SDoug Rabson 77227a0bc89SDoug Rabson dep = target; 77327a0bc89SDoug Rabson if ((target->de_Attributes & ATTR_DIRECTORY) == 0 || 77427a0bc89SDoug Rabson (source->de_Attributes & ATTR_DIRECTORY) == 0) { 77527a0bc89SDoug Rabson error = ENOTDIR; 77627a0bc89SDoug Rabson goto out; 77727a0bc89SDoug Rabson } 77827a0bc89SDoug Rabson if (dep->de_StartCluster == source->de_StartCluster) { 77927a0bc89SDoug Rabson error = EEXIST; 78027a0bc89SDoug Rabson goto out; 78127a0bc89SDoug Rabson } 78227a0bc89SDoug Rabson if (dep->de_StartCluster == MSDOSFSROOT) 78327a0bc89SDoug Rabson goto out; 784952a6212SJordan K. Hubbard pmp = dep->de_pmp; 785952a6212SJordan K. Hubbard #ifdef DIAGNOSTIC 786952a6212SJordan K. Hubbard if (pmp != source->de_pmp) 787952a6212SJordan K. Hubbard panic("doscheckpath: source and target on different filesystems"); 788952a6212SJordan K. Hubbard #endif 789952a6212SJordan K. Hubbard if (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk) 790952a6212SJordan K. Hubbard goto out; 791952a6212SJordan K. Hubbard 79227a0bc89SDoug Rabson for (;;) { 79327a0bc89SDoug Rabson if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) { 79427a0bc89SDoug Rabson error = ENOTDIR; 795952a6212SJordan K. Hubbard break; 79627a0bc89SDoug Rabson } 79727a0bc89SDoug Rabson scn = dep->de_StartCluster; 79827a0bc89SDoug Rabson error = bread(pmp->pm_devvp, cntobn(pmp, scn), 79927a0bc89SDoug Rabson pmp->pm_bpcluster, NOCRED, &bp); 800952a6212SJordan K. Hubbard if (error) 80127a0bc89SDoug Rabson break; 802952a6212SJordan K. Hubbard 80327a0bc89SDoug Rabson ep = (struct direntry *) bp->b_data + 1; 80427a0bc89SDoug Rabson if ((ep->deAttributes & ATTR_DIRECTORY) == 0 || 80527a0bc89SDoug Rabson bcmp(ep->deName, ".. ", 11) != 0) { 80627a0bc89SDoug Rabson error = ENOTDIR; 80727a0bc89SDoug Rabson break; 80827a0bc89SDoug Rabson } 80927a0bc89SDoug Rabson scn = getushort(ep->deStartCluster); 810952a6212SJordan K. Hubbard if (FAT32(pmp)) 811952a6212SJordan K. Hubbard scn |= getushort(ep->deHighClust) << 16; 812952a6212SJordan K. Hubbard 81327a0bc89SDoug Rabson if (scn == source->de_StartCluster) { 81427a0bc89SDoug Rabson error = EINVAL; 81527a0bc89SDoug Rabson break; 81627a0bc89SDoug Rabson } 81727a0bc89SDoug Rabson if (scn == MSDOSFSROOT) 81827a0bc89SDoug Rabson break; 819952a6212SJordan K. Hubbard if (FAT32(pmp) && scn == pmp->pm_rootdirblk) { 820952a6212SJordan K. Hubbard /* 821952a6212SJordan K. Hubbard * scn should be 0 in this case, 822952a6212SJordan K. Hubbard * but we silently ignore the error. 823952a6212SJordan K. Hubbard */ 824952a6212SJordan K. Hubbard break; 825952a6212SJordan K. Hubbard } 826952a6212SJordan K. Hubbard 82727a0bc89SDoug Rabson vput(DETOV(dep)); 82827a0bc89SDoug Rabson brelse(bp); 82927a0bc89SDoug Rabson bp = NULL; 830952a6212SJordan K. Hubbard /* NOTE: deget() clears dep on error */ 831952a6212SJordan K. Hubbard if ((error = deget(pmp, scn, 0, &dep)) != 0) 83227a0bc89SDoug Rabson break; 83327a0bc89SDoug Rabson } 83427a0bc89SDoug Rabson out:; 83527a0bc89SDoug Rabson if (bp) 83627a0bc89SDoug Rabson brelse(bp); 83727a0bc89SDoug Rabson if (error == ENOTDIR) 83827a0bc89SDoug Rabson printf("doscheckpath(): .. not a directory?\n"); 83927a0bc89SDoug Rabson if (dep != NULL) 84027a0bc89SDoug Rabson vput(DETOV(dep)); 841952a6212SJordan K. Hubbard return (error); 84227a0bc89SDoug Rabson } 84327a0bc89SDoug Rabson 84427a0bc89SDoug Rabson /* 84527a0bc89SDoug Rabson * Read in the disk block containing the directory entry (dirclu, dirofs) 84627a0bc89SDoug Rabson * and return the address of the buf header, and the address of the 84727a0bc89SDoug Rabson * directory entry within the block. 84827a0bc89SDoug Rabson */ 84927a0bc89SDoug Rabson int 850952a6212SJordan K. Hubbard readep(pmp, dirclust, diroffset, bpp, epp) 85127a0bc89SDoug Rabson struct msdosfsmount *pmp; 852952a6212SJordan K. Hubbard u_long dirclust, diroffset; 85327a0bc89SDoug Rabson struct buf **bpp; 85427a0bc89SDoug Rabson struct direntry **epp; 85527a0bc89SDoug Rabson { 85627a0bc89SDoug Rabson int error; 85727a0bc89SDoug Rabson daddr_t bn; 858952a6212SJordan K. Hubbard int blsize; 85927a0bc89SDoug Rabson 860952a6212SJordan K. Hubbard blsize = pmp->pm_bpcluster; 861952a6212SJordan K. Hubbard if (dirclust == MSDOSFSROOT 862952a6212SJordan K. Hubbard && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize) 863952a6212SJordan K. Hubbard blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask; 864952a6212SJordan K. Hubbard bn = detobn(pmp, dirclust, diroffset); 865952a6212SJordan K. Hubbard if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) { 866952a6212SJordan K. Hubbard brelse(*bpp); 86727a0bc89SDoug Rabson *bpp = NULL; 868952a6212SJordan K. Hubbard return (error); 86927a0bc89SDoug Rabson } 87027a0bc89SDoug Rabson if (epp) 871952a6212SJordan K. Hubbard *epp = bptoep(pmp, *bpp, diroffset); 872952a6212SJordan K. Hubbard return (0); 87327a0bc89SDoug Rabson } 87427a0bc89SDoug Rabson 87527a0bc89SDoug Rabson /* 87627a0bc89SDoug Rabson * Read in the disk block containing the directory entry dep came from and 87727a0bc89SDoug Rabson * return the address of the buf header, and the address of the directory 87827a0bc89SDoug Rabson * entry within the block. 87927a0bc89SDoug Rabson */ 88027a0bc89SDoug Rabson int 88127a0bc89SDoug Rabson readde(dep, bpp, epp) 88227a0bc89SDoug Rabson struct denode *dep; 88327a0bc89SDoug Rabson struct buf **bpp; 88427a0bc89SDoug Rabson struct direntry **epp; 88527a0bc89SDoug Rabson { 886952a6212SJordan K. Hubbard 887952a6212SJordan K. Hubbard return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset, 888952a6212SJordan K. Hubbard bpp, epp)); 889952a6212SJordan K. Hubbard } 890952a6212SJordan K. Hubbard 891952a6212SJordan K. Hubbard /* 892952a6212SJordan K. Hubbard * Remove a directory entry. At this point the file represented by the 893952a6212SJordan K. Hubbard * directory entry to be removed is still full length until noone has it 894952a6212SJordan K. Hubbard * open. When the file no longer being used msdosfs_inactive() is called 895952a6212SJordan K. Hubbard * and will truncate the file to 0 length. When the vnode containing the 896952a6212SJordan K. Hubbard * denode is needed for some other purpose by VFS it will call 897952a6212SJordan K. Hubbard * msdosfs_reclaim() which will remove the denode from the denode cache. 898952a6212SJordan K. Hubbard */ 899952a6212SJordan K. Hubbard int 900952a6212SJordan K. Hubbard removede(pdep, dep) 901952a6212SJordan K. Hubbard struct denode *pdep; /* directory where the entry is removed */ 902952a6212SJordan K. Hubbard struct denode *dep; /* file to be removed */ 903952a6212SJordan K. Hubbard { 904952a6212SJordan K. Hubbard int error; 905952a6212SJordan K. Hubbard struct direntry *ep; 906952a6212SJordan K. Hubbard struct buf *bp; 907952a6212SJordan K. Hubbard daddr_t bn; 908952a6212SJordan K. Hubbard int blsize; 909952a6212SJordan K. Hubbard struct msdosfsmount *pmp = pdep->de_pmp; 910952a6212SJordan K. Hubbard u_long offset = pdep->de_fndoffset; 911952a6212SJordan K. Hubbard 912952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG 913952a6212SJordan K. Hubbard printf("removede(): filename %s, dep %p, offset %08lx\n", 914952a6212SJordan K. Hubbard dep->de_Name, dep, offset); 915952a6212SJordan K. Hubbard #endif 916952a6212SJordan K. Hubbard 917952a6212SJordan K. Hubbard dep->de_refcnt--; 918952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 919952a6212SJordan K. Hubbard do { 920952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 921952a6212SJordan K. Hubbard error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize); 922952a6212SJordan K. Hubbard if (error) 923952a6212SJordan K. Hubbard return error; 924952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 925952a6212SJordan K. Hubbard if (error) { 926952a6212SJordan K. Hubbard brelse(bp); 927952a6212SJordan K. Hubbard return error; 928952a6212SJordan K. Hubbard } 929952a6212SJordan K. Hubbard ep = bptoep(pmp, bp, offset); 930952a6212SJordan K. Hubbard /* 931952a6212SJordan K. Hubbard * Check whether, if we came here the second time, i.e. 932952a6212SJordan K. Hubbard * when underflowing into the previous block, the last 933952a6212SJordan K. Hubbard * entry in this block is a longfilename entry, too. 934952a6212SJordan K. Hubbard */ 935952a6212SJordan K. Hubbard if (ep->deAttributes != ATTR_WIN95 936952a6212SJordan K. Hubbard && offset != pdep->de_fndoffset) { 937952a6212SJordan K. Hubbard brelse(bp); 938952a6212SJordan K. Hubbard break; 939952a6212SJordan K. Hubbard } 940952a6212SJordan K. Hubbard offset += sizeof(struct direntry); 941952a6212SJordan K. Hubbard while (1) { 942952a6212SJordan K. Hubbard /* 943952a6212SJordan K. Hubbard * We are a bit agressive here in that we delete any Win95 944952a6212SJordan K. Hubbard * entries preceding this entry, not just the ones we "own". 945952a6212SJordan K. Hubbard * Since these presumably aren't valid anyway, 946952a6212SJordan K. Hubbard * there should be no harm. 947952a6212SJordan K. Hubbard */ 948952a6212SJordan K. Hubbard offset -= sizeof(struct direntry); 949952a6212SJordan K. Hubbard ep--->deName[0] = SLOT_DELETED; 950952a6212SJordan K. Hubbard if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) 951952a6212SJordan K. Hubbard || !(offset & pmp->pm_crbomask) 952952a6212SJordan K. Hubbard || ep->deAttributes != ATTR_WIN95) 953952a6212SJordan K. Hubbard break; 954952a6212SJordan K. Hubbard } 955952a6212SJordan K. Hubbard if ((error = bwrite(bp)) != 0) 956952a6212SJordan K. Hubbard return error; 957952a6212SJordan K. Hubbard } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95) 958952a6212SJordan K. Hubbard && !(offset & pmp->pm_crbomask) 959952a6212SJordan K. Hubbard && offset); 960952a6212SJordan K. Hubbard return 0; 961952a6212SJordan K. Hubbard } 962952a6212SJordan K. Hubbard 963952a6212SJordan K. Hubbard /* 964952a6212SJordan K. Hubbard * Create a unique DOS name in dvp 965952a6212SJordan K. Hubbard */ 966952a6212SJordan K. Hubbard int 967952a6212SJordan K. Hubbard uniqdosname(dep, cnp, cp) 968952a6212SJordan K. Hubbard struct denode *dep; 969952a6212SJordan K. Hubbard struct componentname *cnp; 970952a6212SJordan K. Hubbard u_char *cp; 971952a6212SJordan K. Hubbard { 972952a6212SJordan K. Hubbard struct msdosfsmount *pmp = dep->de_pmp; 973952a6212SJordan K. Hubbard struct direntry *dentp; 974952a6212SJordan K. Hubbard int gen; 975952a6212SJordan K. Hubbard int blsize; 976952a6212SJordan K. Hubbard u_long cn; 977952a6212SJordan K. Hubbard daddr_t bn; 978952a6212SJordan K. Hubbard struct buf *bp; 979952a6212SJordan K. Hubbard int error; 980952a6212SJordan K. Hubbard 981af9f1d50SDmitrij Tejblum if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) 982011cdb57SDmitrij Tejblum return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 983c4f02a89SMax Khon cnp->cn_namelen, 0, pmp) ? 0 : EINVAL); 984011cdb57SDmitrij Tejblum 985952a6212SJordan K. Hubbard for (gen = 1;; gen++) { 986952a6212SJordan K. Hubbard /* 987952a6212SJordan K. Hubbard * Generate DOS name with generation number 988952a6212SJordan K. Hubbard */ 989952a6212SJordan K. Hubbard if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp, 990c4f02a89SMax Khon cnp->cn_namelen, gen, pmp)) 991952a6212SJordan K. Hubbard return gen == 1 ? EINVAL : EEXIST; 992952a6212SJordan K. Hubbard 993952a6212SJordan K. Hubbard /* 994952a6212SJordan K. Hubbard * Now look for a dir entry with this exact name 995952a6212SJordan K. Hubbard */ 996952a6212SJordan K. Hubbard for (cn = error = 0; !error; cn++) { 997952a6212SJordan K. Hubbard if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) { 998952a6212SJordan K. Hubbard if (error == E2BIG) /* EOF reached and not found */ 999952a6212SJordan K. Hubbard return 0; 1000952a6212SJordan K. Hubbard return error; 1001952a6212SJordan K. Hubbard } 1002952a6212SJordan K. Hubbard error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp); 1003952a6212SJordan K. Hubbard if (error) { 1004952a6212SJordan K. Hubbard brelse(bp); 1005952a6212SJordan K. Hubbard return error; 1006952a6212SJordan K. Hubbard } 1007952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 1008952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 1009952a6212SJordan K. Hubbard dentp++) { 1010952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_EMPTY) { 1011952a6212SJordan K. Hubbard /* 1012952a6212SJordan K. Hubbard * Last used entry and not found 1013952a6212SJordan K. Hubbard */ 1014952a6212SJordan K. Hubbard brelse(bp); 1015952a6212SJordan K. Hubbard return 0; 1016952a6212SJordan K. Hubbard } 1017952a6212SJordan K. Hubbard /* 1018952a6212SJordan K. Hubbard * Ignore volume labels and Win95 entries 1019952a6212SJordan K. Hubbard */ 1020952a6212SJordan K. Hubbard if (dentp->deAttributes & ATTR_VOLUME) 1021952a6212SJordan K. Hubbard continue; 1022952a6212SJordan K. Hubbard if (!bcmp(dentp->deName, cp, 11)) { 1023952a6212SJordan K. Hubbard error = EEXIST; 1024952a6212SJordan K. Hubbard break; 1025952a6212SJordan K. Hubbard } 1026952a6212SJordan K. Hubbard } 1027952a6212SJordan K. Hubbard brelse(bp); 1028952a6212SJordan K. Hubbard } 1029952a6212SJordan K. Hubbard } 1030952a6212SJordan K. Hubbard } 1031952a6212SJordan K. Hubbard 1032952a6212SJordan K. Hubbard /* 1033952a6212SJordan K. Hubbard * Find any Win'95 long filename entry in directory dep 1034952a6212SJordan K. Hubbard */ 1035952a6212SJordan K. Hubbard int 1036952a6212SJordan K. Hubbard findwin95(dep) 1037952a6212SJordan K. Hubbard struct denode *dep; 1038952a6212SJordan K. Hubbard { 1039952a6212SJordan K. Hubbard struct msdosfsmount *pmp = dep->de_pmp; 1040952a6212SJordan K. Hubbard struct direntry *dentp; 104157081f7bSDmitrij Tejblum int blsize, win95; 1042952a6212SJordan K. Hubbard u_long cn; 1043952a6212SJordan K. Hubbard daddr_t bn; 1044952a6212SJordan K. Hubbard struct buf *bp; 1045952a6212SJordan K. Hubbard 104657081f7bSDmitrij Tejblum win95 = 1; 1047952a6212SJordan K. Hubbard /* 1048952a6212SJordan K. Hubbard * Read through the directory looking for Win'95 entries 1049952a6212SJordan K. Hubbard * Note: Error currently handled just as EOF XXX 1050952a6212SJordan K. Hubbard */ 1051952a6212SJordan K. Hubbard for (cn = 0;; cn++) { 1052952a6212SJordan K. Hubbard if (pcbmap(dep, cn, &bn, 0, &blsize)) 105357081f7bSDmitrij Tejblum return (win95); 1054952a6212SJordan K. Hubbard if (bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) { 1055952a6212SJordan K. Hubbard brelse(bp); 105657081f7bSDmitrij Tejblum return (win95); 1057952a6212SJordan K. Hubbard } 1058952a6212SJordan K. Hubbard for (dentp = (struct direntry *)bp->b_data; 1059952a6212SJordan K. Hubbard (char *)dentp < bp->b_data + blsize; 1060952a6212SJordan K. Hubbard dentp++) { 1061952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_EMPTY) { 1062952a6212SJordan K. Hubbard /* 1063952a6212SJordan K. Hubbard * Last used entry and not found 1064952a6212SJordan K. Hubbard */ 1065952a6212SJordan K. Hubbard brelse(bp); 106657081f7bSDmitrij Tejblum return (win95); 1067952a6212SJordan K. Hubbard } 1068952a6212SJordan K. Hubbard if (dentp->deName[0] == SLOT_DELETED) { 1069952a6212SJordan K. Hubbard /* 1070952a6212SJordan K. Hubbard * Ignore deleted files 1071952a6212SJordan K. Hubbard * Note: might be an indication of Win'95 anyway XXX 1072952a6212SJordan K. Hubbard */ 1073952a6212SJordan K. Hubbard continue; 1074952a6212SJordan K. Hubbard } 1075952a6212SJordan K. Hubbard if (dentp->deAttributes == ATTR_WIN95) { 1076952a6212SJordan K. Hubbard brelse(bp); 1077952a6212SJordan K. Hubbard return 1; 1078952a6212SJordan K. Hubbard } 107957081f7bSDmitrij Tejblum win95 = 0; 1080952a6212SJordan K. Hubbard } 1081952a6212SJordan K. Hubbard brelse(bp); 1082952a6212SJordan K. Hubbard } 108327a0bc89SDoug Rabson } 1084