144fdad99SPeter Wemm /* $Id: msdosfs_denode.c,v 1.39 1998/08/17 19:09:36 bde Exp $ */ 2952a6212SJordan K. Hubbard /* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */ 327a0bc89SDoug Rabson 427a0bc89SDoug Rabson /*- 5952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 727a0bc89SDoug Rabson * All rights reserved. 827a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 927a0bc89SDoug Rabson * 1027a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1127a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1227a0bc89SDoug Rabson * are met: 1327a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1427a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1527a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1627a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1727a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 1827a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 1927a0bc89SDoug Rabson * must display the following acknowledgement: 2027a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2127a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2227a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2327a0bc89SDoug Rabson * 2427a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2527a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2627a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2727a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2827a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2927a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3027a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3127a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3227a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3327a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3427a0bc89SDoug Rabson */ 3527a0bc89SDoug Rabson /* 3627a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3727a0bc89SDoug Rabson * 3827a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 3927a0bc89SDoug Rabson * it, and don't remove this notice. 4027a0bc89SDoug Rabson * 4127a0bc89SDoug Rabson * This software is provided "as is". 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4427a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4527a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4627a0bc89SDoug Rabson * any damages caused by this software. 4727a0bc89SDoug Rabson * 4827a0bc89SDoug Rabson * October 1992 4927a0bc89SDoug Rabson */ 5027a0bc89SDoug Rabson 5127a0bc89SDoug Rabson #include <sys/param.h> 5227a0bc89SDoug Rabson #include <sys/systm.h> 5327a0bc89SDoug Rabson #include <sys/mount.h> 5427a0bc89SDoug Rabson #include <sys/malloc.h> 5527a0bc89SDoug Rabson #include <sys/proc.h> 5627a0bc89SDoug Rabson #include <sys/buf.h> 5727a0bc89SDoug Rabson #include <sys/vnode.h> 5827a0bc89SDoug Rabson 59c3c6d51eSPoul-Henning Kamp #include <vm/vm.h> 60efeaf95aSDavid Greenman #include <vm/vm_extern.h> 61c3c6d51eSPoul-Henning Kamp 6227a0bc89SDoug Rabson #include <msdosfs/bpb.h> 6327a0bc89SDoug Rabson #include <msdosfs/msdosfsmount.h> 6427a0bc89SDoug Rabson #include <msdosfs/direntry.h> 6527a0bc89SDoug Rabson #include <msdosfs/denode.h> 6627a0bc89SDoug Rabson #include <msdosfs/fat.h> 6727a0bc89SDoug Rabson 68a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part"); 6955166637SPoul-Henning Kamp 70303b270bSEivind Eklund static struct denode **dehashtbl; 71303b270bSEivind Eklund static u_long dehash; /* size of hash table - 1 */ 72952a6212SJordan K. Hubbard #define DEHASH(dev, dcl, doff) (dehashtbl[((dev) + (dcl) + (doff) / \ 73952a6212SJordan K. Hubbard sizeof(struct direntry)) & dehash]) 74af3f60d5SBruce Evans static struct simplelock dehash_slock; 7527a0bc89SDoug Rabson 7694a8606fSDoug Rabson union _qcvt { 7794a8606fSDoug Rabson quad_t qcvt; 7894a8606fSDoug Rabson long val[2]; 7994a8606fSDoug Rabson }; 8094a8606fSDoug Rabson #define SETHIGH(q, h) { \ 8194a8606fSDoug Rabson union _qcvt tmp; \ 8294a8606fSDoug Rabson tmp.qcvt = (q); \ 8394a8606fSDoug Rabson tmp.val[_QUAD_HIGHWORD] = (h); \ 8494a8606fSDoug Rabson (q) = tmp.qcvt; \ 8594a8606fSDoug Rabson } 8694a8606fSDoug Rabson #define SETLOW(q, l) { \ 8794a8606fSDoug Rabson union _qcvt tmp; \ 8894a8606fSDoug Rabson tmp.qcvt = (q); \ 8994a8606fSDoug Rabson tmp.val[_QUAD_LOWWORD] = (l); \ 9094a8606fSDoug Rabson (q) = tmp.qcvt; \ 9194a8606fSDoug Rabson } 9294a8606fSDoug Rabson 9358c27bcfSBruce Evans static struct denode * 9458c27bcfSBruce Evans msdosfs_hashget __P((dev_t dev, u_long dirclust, 9558c27bcfSBruce Evans u_long diroff)); 9658c27bcfSBruce Evans static void msdosfs_hashins __P((struct denode *dep)); 9758c27bcfSBruce Evans static void msdosfs_hashrem __P((struct denode *dep)); 9858c27bcfSBruce Evans 99952a6212SJordan K. Hubbard /*ARGSUSED*/ 100952a6212SJordan K. Hubbard int 101952a6212SJordan K. Hubbard msdosfs_init(vfsp) 1028092a8e1SMike Pritchard struct vfsconf *vfsp; 10327a0bc89SDoug Rabson { 10427a0bc89SDoug Rabson dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, &dehash); 105af3f60d5SBruce Evans simple_lock_init(&dehash_slock); 106952a6212SJordan K. Hubbard return (0); 10727a0bc89SDoug Rabson } 10827a0bc89SDoug Rabson 10927a0bc89SDoug Rabson static struct denode * 11027a0bc89SDoug Rabson msdosfs_hashget(dev, dirclust, diroff) 11127a0bc89SDoug Rabson dev_t dev; 11227a0bc89SDoug Rabson u_long dirclust; 11327a0bc89SDoug Rabson u_long diroff; 11427a0bc89SDoug Rabson { 115af3f60d5SBruce Evans struct proc *p = curproc; /* XXX */ 11627a0bc89SDoug Rabson struct denode *dep; 117af3f60d5SBruce Evans struct vnode *vp; 11827a0bc89SDoug Rabson 119af3f60d5SBruce Evans loop: 120af3f60d5SBruce Evans simple_lock(&dehash_slock); 121952a6212SJordan K. Hubbard for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) { 122af3f60d5SBruce Evans if (dirclust == dep->de_dirclust 123af3f60d5SBruce Evans && diroff == dep->de_diroffset 124af3f60d5SBruce Evans && dev == dep->de_dev 125af3f60d5SBruce Evans && dep->de_refcnt != 0) { 126af3f60d5SBruce Evans vp = DETOV(dep); 127af3f60d5SBruce Evans simple_lock(&vp->v_interlock); 128af3f60d5SBruce Evans simple_unlock(&dehash_slock); 129af3f60d5SBruce Evans if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) 130af3f60d5SBruce Evans goto loop; 131af3f60d5SBruce Evans return (dep); 13227a0bc89SDoug Rabson } 13327a0bc89SDoug Rabson } 134af3f60d5SBruce Evans simple_unlock(&dehash_slock); 135af3f60d5SBruce Evans return (NULL); 13627a0bc89SDoug Rabson } 13727a0bc89SDoug Rabson 13827a0bc89SDoug Rabson static void 13927a0bc89SDoug Rabson msdosfs_hashins(dep) 14027a0bc89SDoug Rabson struct denode *dep; 14127a0bc89SDoug Rabson { 14227a0bc89SDoug Rabson struct denode **depp, *deq; 14327a0bc89SDoug Rabson 144af3f60d5SBruce Evans simple_lock(&dehash_slock); 145952a6212SJordan K. Hubbard depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset); 146c3c6d51eSPoul-Henning Kamp deq = *depp; 147c3c6d51eSPoul-Henning Kamp if (deq) 14827a0bc89SDoug Rabson deq->de_prev = &dep->de_next; 14927a0bc89SDoug Rabson dep->de_next = deq; 15027a0bc89SDoug Rabson dep->de_prev = depp; 15127a0bc89SDoug Rabson *depp = dep; 152af3f60d5SBruce Evans simple_unlock(&dehash_slock); 15327a0bc89SDoug Rabson } 15427a0bc89SDoug Rabson 15527a0bc89SDoug Rabson static void 15627a0bc89SDoug Rabson msdosfs_hashrem(dep) 15727a0bc89SDoug Rabson struct denode *dep; 15827a0bc89SDoug Rabson { 15927a0bc89SDoug Rabson struct denode *deq; 160af3f60d5SBruce Evans 161af3f60d5SBruce Evans simple_lock(&dehash_slock); 162c3c6d51eSPoul-Henning Kamp deq = dep->de_next; 163c3c6d51eSPoul-Henning Kamp if (deq) 16427a0bc89SDoug Rabson deq->de_prev = dep->de_prev; 16527a0bc89SDoug Rabson *dep->de_prev = deq; 16627a0bc89SDoug Rabson #ifdef DIAGNOSTIC 16727a0bc89SDoug Rabson dep->de_next = NULL; 16827a0bc89SDoug Rabson dep->de_prev = NULL; 16927a0bc89SDoug Rabson #endif 170af3f60d5SBruce Evans simple_unlock(&dehash_slock); 17127a0bc89SDoug Rabson } 17227a0bc89SDoug Rabson 17327a0bc89SDoug Rabson /* 17427a0bc89SDoug Rabson * If deget() succeeds it returns with the gotten denode locked(). 17527a0bc89SDoug Rabson * 17627a0bc89SDoug Rabson * pmp - address of msdosfsmount structure of the filesystem containing 17727a0bc89SDoug Rabson * the denode of interest. The pm_dev field and the address of 17827a0bc89SDoug Rabson * the msdosfsmount structure are used. 17927a0bc89SDoug Rabson * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 18027a0bc89SDoug Rabson * diroffset is relative to the beginning of the root directory, 18127a0bc89SDoug Rabson * otherwise it is cluster relative. 18227a0bc89SDoug Rabson * diroffset - offset past begin of cluster of denode we want 18327a0bc89SDoug Rabson * depp - returns the address of the gotten denode. 18427a0bc89SDoug Rabson */ 18527a0bc89SDoug Rabson int 186952a6212SJordan K. Hubbard deget(pmp, dirclust, diroffset, depp) 18727a0bc89SDoug Rabson struct msdosfsmount *pmp; /* so we know the maj/min number */ 18827a0bc89SDoug Rabson u_long dirclust; /* cluster this dir entry came from */ 18927a0bc89SDoug Rabson u_long diroffset; /* index of entry within the cluster */ 19027a0bc89SDoug Rabson struct denode **depp; /* returns the addr of the gotten denode */ 19127a0bc89SDoug Rabson { 19227a0bc89SDoug Rabson int error; 19327a0bc89SDoug Rabson dev_t dev = pmp->pm_dev; 19427a0bc89SDoug Rabson struct mount *mntp = pmp->pm_mountp; 195952a6212SJordan K. Hubbard struct direntry *direntptr; 19627a0bc89SDoug Rabson struct denode *ldep; 19727a0bc89SDoug Rabson struct vnode *nvp; 19827a0bc89SDoug Rabson struct buf *bp; 199af3f60d5SBruce Evans struct proc *p = curproc; /* XXX */ 20000af9731SPoul-Henning Kamp struct timeval tv; 20127a0bc89SDoug Rabson 20227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 203952a6212SJordan K. Hubbard printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 204952a6212SJordan K. Hubbard pmp, dirclust, diroffset, depp); 20527a0bc89SDoug Rabson #endif 20627a0bc89SDoug Rabson 20727a0bc89SDoug Rabson /* 208952a6212SJordan K. Hubbard * On FAT32 filesystems, root is a (more or less) normal 209952a6212SJordan K. Hubbard * directory 21027a0bc89SDoug Rabson */ 211952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == MSDOSFSROOT) 212952a6212SJordan K. Hubbard dirclust = pmp->pm_rootdirblk; 21327a0bc89SDoug Rabson 21427a0bc89SDoug Rabson /* 21527a0bc89SDoug Rabson * See if the denode is in the denode cache. Use the location of 21627a0bc89SDoug Rabson * the directory entry to compute the hash value. For subdir use 217952a6212SJordan K. Hubbard * address of "." entry. For root dir (if not FAT32) use cluster 218952a6212SJordan K. Hubbard * MSDOSFSROOT, offset MSDOSFSROOT_OFS 21927a0bc89SDoug Rabson * 22027a0bc89SDoug Rabson * NOTE: The check for de_refcnt > 0 below insures the denode being 22127a0bc89SDoug Rabson * examined does not represent an unlinked but still open file. 22227a0bc89SDoug Rabson * These files are not to be accessible even when the directory 22327a0bc89SDoug Rabson * entry that represented the file happens to be reused while the 22427a0bc89SDoug Rabson * deleted file is still open. 22527a0bc89SDoug Rabson */ 226c3c6d51eSPoul-Henning Kamp ldep = msdosfs_hashget(dev, dirclust, diroffset); 227c3c6d51eSPoul-Henning Kamp if (ldep) { 22827a0bc89SDoug Rabson *depp = ldep; 229952a6212SJordan K. Hubbard return (0); 23027a0bc89SDoug Rabson } 23127a0bc89SDoug Rabson 2322f9bae59SDavid Greenman /* 2332f9bae59SDavid Greenman * Do the MALLOC before the getnewvnode since doing so afterward 2342f9bae59SDavid Greenman * might cause a bogus v_data pointer to get dereferenced 2352f9bae59SDavid Greenman * elsewhere if MALLOC should block. 2362f9bae59SDavid Greenman */ 2372f9bae59SDavid Greenman MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK); 23827a0bc89SDoug Rabson 23927a0bc89SDoug Rabson /* 24027a0bc89SDoug Rabson * Directory entry was not in cache, have to create a vnode and 24127a0bc89SDoug Rabson * copy it from the passed disk buffer. 24227a0bc89SDoug Rabson */ 24327a0bc89SDoug Rabson /* getnewvnode() does a VREF() on the vnode */ 244c3c6d51eSPoul-Henning Kamp error = getnewvnode(VT_MSDOSFS, mntp, msdosfs_vnodeop_p, &nvp); 245c3c6d51eSPoul-Henning Kamp if (error) { 2462f9bae59SDavid Greenman *depp = NULL; 2472f9bae59SDavid Greenman FREE(ldep, M_MSDOSFSNODE); 24827a0bc89SDoug Rabson return error; 24927a0bc89SDoug Rabson } 25027a0bc89SDoug Rabson bzero((caddr_t)ldep, sizeof *ldep); 251af3f60d5SBruce Evans lockinit(&ldep->de_lock, PINOD, "denode", 0, 0); 25227a0bc89SDoug Rabson nvp->v_data = ldep; 25327a0bc89SDoug Rabson ldep->de_vnode = nvp; 25427a0bc89SDoug Rabson ldep->de_flag = 0; 25527a0bc89SDoug Rabson ldep->de_devvp = 0; 25627a0bc89SDoug Rabson ldep->de_dev = dev; 25727a0bc89SDoug Rabson ldep->de_dirclust = dirclust; 25827a0bc89SDoug Rabson ldep->de_diroffset = diroffset; 25927a0bc89SDoug Rabson fc_purge(ldep, 0); /* init the fat cache for this denode */ 26027a0bc89SDoug Rabson 26127a0bc89SDoug Rabson /* 262af3f60d5SBruce Evans * Lock the denode so that it can't be accessed until we've read 263af3f60d5SBruce Evans * it in and have done what we need to it. Do this here instead 264af3f60d5SBruce Evans * of at the start of msdosfs_hashins() so that reinsert() can 265af3f60d5SBruce Evans * call msdosfs_hashins() with a locked denode. 26627a0bc89SDoug Rabson */ 267af3f60d5SBruce Evans if (lockmgr(&ldep->de_lock, LK_EXCLUSIVE, (struct simplelock *)0, p)) 268af3f60d5SBruce Evans panic("deget: unexpected lock failure"); 269af3f60d5SBruce Evans 270af3f60d5SBruce Evans /* 271af3f60d5SBruce Evans * Insert the denode into the hash queue. 272af3f60d5SBruce Evans */ 27327a0bc89SDoug Rabson msdosfs_hashins(ldep); 27427a0bc89SDoug Rabson 275952a6212SJordan K. Hubbard ldep->de_pmp = pmp; 276952a6212SJordan K. Hubbard ldep->de_refcnt = 1; 27727a0bc89SDoug Rabson /* 27827a0bc89SDoug Rabson * Copy the directory entry into the denode area of the vnode. 27927a0bc89SDoug Rabson */ 280952a6212SJordan K. Hubbard if ((dirclust == MSDOSFSROOT 281952a6212SJordan K. Hubbard || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 282952a6212SJordan K. Hubbard && diroffset == MSDOSFSROOT_OFS) { 28327a0bc89SDoug Rabson /* 28427a0bc89SDoug Rabson * Directory entry for the root directory. There isn't one, 28527a0bc89SDoug Rabson * so we manufacture one. We should probably rummage 28627a0bc89SDoug Rabson * through the root directory and find a label entry (if it 28727a0bc89SDoug Rabson * exists), and then use the time and date from that entry 28827a0bc89SDoug Rabson * as the time and date for the root denode. 28927a0bc89SDoug Rabson */ 290952a6212SJordan K. Hubbard nvp->v_flag |= VROOT; /* should be further down XXX */ 291952a6212SJordan K. Hubbard 29227a0bc89SDoug Rabson ldep->de_Attributes = ATTR_DIRECTORY; 293952a6212SJordan K. Hubbard if (FAT32(pmp)) 294952a6212SJordan K. Hubbard ldep->de_StartCluster = pmp->pm_rootdirblk; 295952a6212SJordan K. Hubbard /* de_FileSize will be filled in further down */ 296952a6212SJordan K. Hubbard else { 29727a0bc89SDoug Rabson ldep->de_StartCluster = MSDOSFSROOT; 29827a0bc89SDoug Rabson ldep->de_FileSize = pmp->pm_rootdirsize * pmp->pm_BytesPerSec; 299952a6212SJordan K. Hubbard } 30027a0bc89SDoug Rabson /* 30127a0bc89SDoug Rabson * fill in time and date so that dos2unixtime() doesn't 30227a0bc89SDoug Rabson * spit up when called from msdosfs_getattr() with root 30327a0bc89SDoug Rabson * denode 30427a0bc89SDoug Rabson */ 305952a6212SJordan K. Hubbard ldep->de_CHun = 0; 306952a6212SJordan K. Hubbard ldep->de_CTime = 0x0000; /* 00:00:00 */ 307952a6212SJordan K. Hubbard ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 30827a0bc89SDoug Rabson | (1 << DD_DAY_SHIFT); 30927a0bc89SDoug Rabson /* Jan 1, 1980 */ 310952a6212SJordan K. Hubbard ldep->de_ADate = ldep->de_CDate; 311952a6212SJordan K. Hubbard ldep->de_MTime = ldep->de_CTime; 312952a6212SJordan K. Hubbard ldep->de_MDate = ldep->de_CDate; 31327a0bc89SDoug Rabson /* leave the other fields as garbage */ 31427a0bc89SDoug Rabson } else { 315952a6212SJordan K. Hubbard error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 3165097a20dSBruce Evans if (error) { 3175097a20dSBruce Evans /* 3185097a20dSBruce Evans * The denode does not contain anything useful, so 3195097a20dSBruce Evans * it would be wrong to leave it on its hash chain. 3205097a20dSBruce Evans * Arrange for vput() to just forget about it. 3215097a20dSBruce Evans */ 3225097a20dSBruce Evans ldep->de_Name[0] = SLOT_DELETED; 3235097a20dSBruce Evans 3245097a20dSBruce Evans vput(nvp); 3255097a20dSBruce Evans *depp = NULL; 326952a6212SJordan K. Hubbard return (error); 3275097a20dSBruce Evans } 32827a0bc89SDoug Rabson DE_INTERNALIZE(ldep, direntptr); 32927a0bc89SDoug Rabson brelse(bp); 33027a0bc89SDoug Rabson } 33127a0bc89SDoug Rabson 33227a0bc89SDoug Rabson /* 33327a0bc89SDoug Rabson * Fill in a few fields of the vnode and finish filling in the 33427a0bc89SDoug Rabson * denode. Then return the address of the found denode. 33527a0bc89SDoug Rabson */ 33627a0bc89SDoug Rabson if (ldep->de_Attributes & ATTR_DIRECTORY) { 33727a0bc89SDoug Rabson /* 33827a0bc89SDoug Rabson * Since DOS directory entries that describe directories 33927a0bc89SDoug Rabson * have 0 in the filesize field, we take this opportunity 34027a0bc89SDoug Rabson * to find out the length of the directory and plug it into 34127a0bc89SDoug Rabson * the denode structure. 34227a0bc89SDoug Rabson */ 34327a0bc89SDoug Rabson u_long size; 34427a0bc89SDoug Rabson 34527a0bc89SDoug Rabson nvp->v_type = VDIR; 346952a6212SJordan K. Hubbard if (ldep->de_StartCluster != MSDOSFSROOT) { 347952a6212SJordan K. Hubbard error = pcbmap(ldep, 0xffff, 0, &size, 0); 34827a0bc89SDoug Rabson if (error == E2BIG) { 349952a6212SJordan K. Hubbard ldep->de_FileSize = de_cn2off(pmp, size); 35027a0bc89SDoug Rabson error = 0; 35127a0bc89SDoug Rabson } else 35227a0bc89SDoug Rabson printf("deget(): pcbmap returned %d\n", error); 35327a0bc89SDoug Rabson } 35427a0bc89SDoug Rabson } else 35527a0bc89SDoug Rabson nvp->v_type = VREG; 356c21410e1SPoul-Henning Kamp getmicrouptime(&tv); 35700af9731SPoul-Henning Kamp SETHIGH(ldep->de_modrev, tv.tv_sec); 35800af9731SPoul-Henning Kamp SETLOW(ldep->de_modrev, tv.tv_usec * 4294); 3595097a20dSBruce Evans ldep->de_devvp = pmp->pm_devvp; 36027a0bc89SDoug Rabson VREF(ldep->de_devvp); 36127a0bc89SDoug Rabson *depp = ldep; 362952a6212SJordan K. Hubbard return (0); 36327a0bc89SDoug Rabson } 36427a0bc89SDoug Rabson 36527a0bc89SDoug Rabson int 366952a6212SJordan K. Hubbard deupdat(dep, waitfor) 36727a0bc89SDoug Rabson struct denode *dep; 36827a0bc89SDoug Rabson int waitfor; 36927a0bc89SDoug Rabson { 37027a0bc89SDoug Rabson int error; 37127a0bc89SDoug Rabson struct buf *bp; 37227a0bc89SDoug Rabson struct direntry *dirp; 373952a6212SJordan K. Hubbard struct timespec ts; 37427a0bc89SDoug Rabson 375952a6212SJordan K. Hubbard if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 376952a6212SJordan K. Hubbard return (0); 377a0502b19SPoul-Henning Kamp getnanotime(&ts); 378952a6212SJordan K. Hubbard DETIMES(dep, &ts, &ts, &ts); 379952a6212SJordan K. Hubbard if ((dep->de_flag & DE_MODIFIED) == 0) 380952a6212SJordan K. Hubbard return (0); 381952a6212SJordan K. Hubbard dep->de_flag &= ~DE_MODIFIED; 382952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 383952a6212SJordan K. Hubbard return (0); 384952a6212SJordan K. Hubbard if (dep->de_refcnt <= 0) 385952a6212SJordan K. Hubbard return (0); 386c3c6d51eSPoul-Henning Kamp error = readde(dep, &bp, &dirp); 387c3c6d51eSPoul-Henning Kamp if (error) 388952a6212SJordan K. Hubbard return (error); 38927a0bc89SDoug Rabson DE_EXTERNALIZE(dirp, dep); 39027a0bc89SDoug Rabson if (waitfor) 391952a6212SJordan K. Hubbard return (bwrite(bp)); 392952a6212SJordan K. Hubbard else { 39327a0bc89SDoug Rabson bdwrite(bp); 394952a6212SJordan K. Hubbard return (0); 395952a6212SJordan K. Hubbard } 39627a0bc89SDoug Rabson } 39727a0bc89SDoug Rabson 39827a0bc89SDoug Rabson /* 39927a0bc89SDoug Rabson * Truncate the file described by dep to the length specified by length. 40027a0bc89SDoug Rabson */ 40127a0bc89SDoug Rabson int 40227a0bc89SDoug Rabson detrunc(dep, length, flags, cred, p) 40327a0bc89SDoug Rabson struct denode *dep; 40427a0bc89SDoug Rabson u_long length; 40527a0bc89SDoug Rabson int flags; 40627a0bc89SDoug Rabson struct ucred *cred; 40727a0bc89SDoug Rabson struct proc *p; 40827a0bc89SDoug Rabson { 40927a0bc89SDoug Rabson int error; 41027a0bc89SDoug Rabson int allerror; 41127a0bc89SDoug Rabson u_long eofentry; 41227a0bc89SDoug Rabson u_long chaintofree; 41327a0bc89SDoug Rabson daddr_t bn; 41427a0bc89SDoug Rabson int boff; 41527a0bc89SDoug Rabson int isadir = dep->de_Attributes & ATTR_DIRECTORY; 41627a0bc89SDoug Rabson struct buf *bp; 41727a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 418c3c6d51eSPoul-Henning Kamp struct timespec ts; 41927a0bc89SDoug Rabson 42027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 421952a6212SJordan K. Hubbard printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 42227a0bc89SDoug Rabson #endif 42327a0bc89SDoug Rabson 42427a0bc89SDoug Rabson /* 42527a0bc89SDoug Rabson * Disallow attempts to truncate the root directory since it is of 42627a0bc89SDoug Rabson * fixed size. That's just the way dos filesystems are. We use 42727a0bc89SDoug Rabson * the VROOT bit in the vnode because checking for the directory 42827a0bc89SDoug Rabson * bit and a startcluster of 0 in the denode is not adequate to 42927a0bc89SDoug Rabson * recognize the root directory at this point in a file or 43027a0bc89SDoug Rabson * directory's life. 43127a0bc89SDoug Rabson */ 432952a6212SJordan K. Hubbard if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) { 433952a6212SJordan K. Hubbard printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 43427a0bc89SDoug Rabson dep->de_dirclust, dep->de_diroffset); 435952a6212SJordan K. Hubbard return (EINVAL); 43627a0bc89SDoug Rabson } 43727a0bc89SDoug Rabson 43827a0bc89SDoug Rabson 439bd7e5f99SJohn Dyson if (dep->de_FileSize < length) { 440bd7e5f99SJohn Dyson vnode_pager_setsize(DETOV(dep), length); 44127a0bc89SDoug Rabson return deextend(dep, length, cred); 442bd7e5f99SJohn Dyson } 44327a0bc89SDoug Rabson 44427a0bc89SDoug Rabson /* 44527a0bc89SDoug Rabson * If the desired length is 0 then remember the starting cluster of 44627a0bc89SDoug Rabson * the file and set the StartCluster field in the directory entry 44727a0bc89SDoug Rabson * to 0. If the desired length is not zero, then get the number of 44827a0bc89SDoug Rabson * the last cluster in the shortened file. Then get the number of 44927a0bc89SDoug Rabson * the first cluster in the part of the file that is to be freed. 45027a0bc89SDoug Rabson * Then set the next cluster pointer in the last cluster of the 45127a0bc89SDoug Rabson * file to CLUST_EOFE. 45227a0bc89SDoug Rabson */ 45327a0bc89SDoug Rabson if (length == 0) { 45427a0bc89SDoug Rabson chaintofree = dep->de_StartCluster; 45527a0bc89SDoug Rabson dep->de_StartCluster = 0; 45627a0bc89SDoug Rabson eofentry = ~0; 45727a0bc89SDoug Rabson } else { 458952a6212SJordan K. Hubbard error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 459952a6212SJordan K. Hubbard &eofentry, 0); 460c3c6d51eSPoul-Henning Kamp if (error) { 46127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 46227a0bc89SDoug Rabson printf("detrunc(): pcbmap fails %d\n", error); 46327a0bc89SDoug Rabson #endif 464952a6212SJordan K. Hubbard return (error); 46527a0bc89SDoug Rabson } 46627a0bc89SDoug Rabson } 46727a0bc89SDoug Rabson 468952a6212SJordan K. Hubbard fc_purge(dep, de_clcount(pmp, length)); 46927a0bc89SDoug Rabson 47027a0bc89SDoug Rabson /* 47127a0bc89SDoug Rabson * If the new length is not a multiple of the cluster size then we 47227a0bc89SDoug Rabson * must zero the tail end of the new last cluster in case it 47327a0bc89SDoug Rabson * becomes part of the file again because of a seek. 47427a0bc89SDoug Rabson */ 47527a0bc89SDoug Rabson if ((boff = length & pmp->pm_crbomask) != 0) { 47627a0bc89SDoug Rabson if (isadir) { 47727a0bc89SDoug Rabson bn = cntobn(pmp, eofentry); 47827a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 47927a0bc89SDoug Rabson NOCRED, &bp); 48027a0bc89SDoug Rabson } else { 48127a0bc89SDoug Rabson bn = de_blk(pmp, length); 48227a0bc89SDoug Rabson error = bread(DETOV(dep), bn, pmp->pm_bpcluster, 48327a0bc89SDoug Rabson NOCRED, &bp); 48427a0bc89SDoug Rabson } 48527a0bc89SDoug Rabson if (error) { 486952a6212SJordan K. Hubbard brelse(bp); 48727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 48827a0bc89SDoug Rabson printf("detrunc(): bread fails %d\n", error); 48927a0bc89SDoug Rabson #endif 490952a6212SJordan K. Hubbard return (error); 49127a0bc89SDoug Rabson } 49227a0bc89SDoug Rabson /* 49327a0bc89SDoug Rabson * is this the right place for it? 49427a0bc89SDoug Rabson */ 49527a0bc89SDoug Rabson bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 49627a0bc89SDoug Rabson if (flags & IO_SYNC) 49727a0bc89SDoug Rabson bwrite(bp); 49827a0bc89SDoug Rabson else 49927a0bc89SDoug Rabson bdwrite(bp); 50027a0bc89SDoug Rabson } 50127a0bc89SDoug Rabson 50227a0bc89SDoug Rabson /* 50327a0bc89SDoug Rabson * Write out the updated directory entry. Even if the update fails 50427a0bc89SDoug Rabson * we free the trailing clusters. 50527a0bc89SDoug Rabson */ 50627a0bc89SDoug Rabson dep->de_FileSize = length; 507952a6212SJordan K. Hubbard if (!isadir) 508952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE|DE_MODIFIED; 50944fdad99SPeter Wemm allerror = vtruncbuf(DETOV(dep), cred, p, length, pmp->pm_bpcluster); 51044fdad99SPeter Wemm #ifdef MSDOSFS_DEBUG 51144fdad99SPeter Wemm if (allerror) 51244fdad99SPeter Wemm printf("detrunc(): vtruncbuf error %d\n", allerror); 51344fdad99SPeter Wemm #endif 51444fdad99SPeter Wemm error = deupdat(dep, 1); 51544fdad99SPeter Wemm if (error && (allerror == 0)) 51644fdad99SPeter Wemm allerror = error; 51727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 518952a6212SJordan K. Hubbard printf("detrunc(): allerror %d, eofentry %lu\n", 51927a0bc89SDoug Rabson allerror, eofentry); 52027a0bc89SDoug Rabson #endif 52127a0bc89SDoug Rabson 52227a0bc89SDoug Rabson /* 52327a0bc89SDoug Rabson * If we need to break the cluster chain for the file then do it 52427a0bc89SDoug Rabson * now. 52527a0bc89SDoug Rabson */ 52627a0bc89SDoug Rabson if (eofentry != ~0) { 52727a0bc89SDoug Rabson error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 52827a0bc89SDoug Rabson &chaintofree, CLUST_EOFE); 52927a0bc89SDoug Rabson if (error) { 53027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 53127a0bc89SDoug Rabson printf("detrunc(): fatentry errors %d\n", error); 53227a0bc89SDoug Rabson #endif 533952a6212SJordan K. Hubbard return (error); 53427a0bc89SDoug Rabson } 535952a6212SJordan K. Hubbard fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 53627a0bc89SDoug Rabson eofentry); 53727a0bc89SDoug Rabson } 53827a0bc89SDoug Rabson 53927a0bc89SDoug Rabson /* 54027a0bc89SDoug Rabson * Now free the clusters removed from the file because of the 54127a0bc89SDoug Rabson * truncation. 54227a0bc89SDoug Rabson */ 543952a6212SJordan K. Hubbard if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 54427a0bc89SDoug Rabson freeclusterchain(pmp, chaintofree); 54527a0bc89SDoug Rabson 546952a6212SJordan K. Hubbard return (allerror); 54727a0bc89SDoug Rabson } 54827a0bc89SDoug Rabson 54927a0bc89SDoug Rabson /* 55027a0bc89SDoug Rabson * Extend the file described by dep to length specified by length. 55127a0bc89SDoug Rabson */ 55227a0bc89SDoug Rabson int 55327a0bc89SDoug Rabson deextend(dep, length, cred) 55427a0bc89SDoug Rabson struct denode *dep; 555952a6212SJordan K. Hubbard u_long length; 55627a0bc89SDoug Rabson struct ucred *cred; 55727a0bc89SDoug Rabson { 55827a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 55927a0bc89SDoug Rabson u_long count; 56027a0bc89SDoug Rabson int error; 561c3c6d51eSPoul-Henning Kamp struct timespec ts; 56227a0bc89SDoug Rabson 56327a0bc89SDoug Rabson /* 56427a0bc89SDoug Rabson * The root of a DOS filesystem cannot be extended. 56527a0bc89SDoug Rabson */ 566952a6212SJordan K. Hubbard if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) 567952a6212SJordan K. Hubbard return (EINVAL); 56827a0bc89SDoug Rabson 56927a0bc89SDoug Rabson /* 570952a6212SJordan K. Hubbard * Directories cannot be extended. 57127a0bc89SDoug Rabson */ 572952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 573952a6212SJordan K. Hubbard return (EISDIR); 57427a0bc89SDoug Rabson 57527a0bc89SDoug Rabson if (length <= dep->de_FileSize) 57627a0bc89SDoug Rabson panic("deextend: file too large"); 57727a0bc89SDoug Rabson 57827a0bc89SDoug Rabson /* 57927a0bc89SDoug Rabson * Compute the number of clusters to allocate. 58027a0bc89SDoug Rabson */ 58127a0bc89SDoug Rabson count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 58227a0bc89SDoug Rabson if (count > 0) { 58327a0bc89SDoug Rabson if (count > pmp->pm_freeclustercount) 584952a6212SJordan K. Hubbard return (ENOSPC); 585c3c6d51eSPoul-Henning Kamp error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 586c3c6d51eSPoul-Henning Kamp if (error) { 58727a0bc89SDoug Rabson /* truncate the added clusters away again */ 58827a0bc89SDoug Rabson (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 589952a6212SJordan K. Hubbard return (error); 59027a0bc89SDoug Rabson } 59127a0bc89SDoug Rabson } 59227a0bc89SDoug Rabson dep->de_FileSize = length; 593952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE|DE_MODIFIED; 594952a6212SJordan K. Hubbard return (deupdat(dep, 1)); 59527a0bc89SDoug Rabson } 59627a0bc89SDoug Rabson 59727a0bc89SDoug Rabson /* 59827a0bc89SDoug Rabson * Move a denode to its correct hash queue after the file it represents has 59927a0bc89SDoug Rabson * been moved to a new directory. 60027a0bc89SDoug Rabson */ 601952a6212SJordan K. Hubbard void 602952a6212SJordan K. Hubbard reinsert(dep) 60327a0bc89SDoug Rabson struct denode *dep; 60427a0bc89SDoug Rabson { 60527a0bc89SDoug Rabson /* 60627a0bc89SDoug Rabson * Fix up the denode cache. If the denode is for a directory, 60727a0bc89SDoug Rabson * there is nothing to do since the hash is based on the starting 60827a0bc89SDoug Rabson * cluster of the directory file and that hasn't changed. If for a 60927a0bc89SDoug Rabson * file the hash is based on the location of the directory entry, 61027a0bc89SDoug Rabson * so we must remove it from the cache and re-enter it with the 61127a0bc89SDoug Rabson * hash based on the new location of the directory entry. 61227a0bc89SDoug Rabson */ 613952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 614952a6212SJordan K. Hubbard return; 61527a0bc89SDoug Rabson msdosfs_hashrem(dep); 61627a0bc89SDoug Rabson msdosfs_hashins(dep); 61727a0bc89SDoug Rabson } 61827a0bc89SDoug Rabson 61927a0bc89SDoug Rabson int 62027a0bc89SDoug Rabson msdosfs_reclaim(ap) 62127a0bc89SDoug Rabson struct vop_reclaim_args /* { 62227a0bc89SDoug Rabson struct vnode *a_vp; 62327a0bc89SDoug Rabson } */ *ap; 62427a0bc89SDoug Rabson { 62527a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 62627a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 62727a0bc89SDoug Rabson 62827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 629f0707215SPoul-Henning Kamp printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 63027a0bc89SDoug Rabson dep, dep->de_Name, dep->de_refcnt); 63127a0bc89SDoug Rabson #endif 63227a0bc89SDoug Rabson 63327a0bc89SDoug Rabson if (prtactive && vp->v_usecount != 0) 63427a0bc89SDoug Rabson vprint("msdosfs_reclaim(): pushing active", vp); 63527a0bc89SDoug Rabson /* 636952a6212SJordan K. Hubbard * Remove the denode from its hash chain. 63727a0bc89SDoug Rabson */ 63827a0bc89SDoug Rabson msdosfs_hashrem(dep); 63927a0bc89SDoug Rabson /* 640952a6212SJordan K. Hubbard * Purge old data structures associated with the denode. 64127a0bc89SDoug Rabson */ 642952a6212SJordan K. Hubbard cache_purge(vp); 64327a0bc89SDoug Rabson if (dep->de_devvp) { 64427a0bc89SDoug Rabson vrele(dep->de_devvp); 64527a0bc89SDoug Rabson dep->de_devvp = 0; 64627a0bc89SDoug Rabson } 647952a6212SJordan K. Hubbard #if 0 /* XXX */ 64827a0bc89SDoug Rabson dep->de_flag = 0; 649952a6212SJordan K. Hubbard #endif 65027a0bc89SDoug Rabson FREE(dep, M_MSDOSFSNODE); 65127a0bc89SDoug Rabson vp->v_data = NULL; 65227a0bc89SDoug Rabson 653952a6212SJordan K. Hubbard return (0); 65427a0bc89SDoug Rabson } 65527a0bc89SDoug Rabson 65627a0bc89SDoug Rabson int 65727a0bc89SDoug Rabson msdosfs_inactive(ap) 65827a0bc89SDoug Rabson struct vop_inactive_args /* { 65927a0bc89SDoug Rabson struct vnode *a_vp; 660af3f60d5SBruce Evans struct proc *a_p; 66127a0bc89SDoug Rabson } */ *ap; 66227a0bc89SDoug Rabson { 66327a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 66427a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 665af3f60d5SBruce Evans struct proc *p = ap->a_p; 66627a0bc89SDoug Rabson int error = 0; 667c3c6d51eSPoul-Henning Kamp struct timespec ts; 66827a0bc89SDoug Rabson 66927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 670f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 67127a0bc89SDoug Rabson #endif 67227a0bc89SDoug Rabson 67327a0bc89SDoug Rabson if (prtactive && vp->v_usecount != 0) 67427a0bc89SDoug Rabson vprint("msdosfs_inactive(): pushing active", vp); 67527a0bc89SDoug Rabson 67627a0bc89SDoug Rabson /* 677952a6212SJordan K. Hubbard * Ignore denodes related to stale file handles. 67827a0bc89SDoug Rabson */ 679af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 680af3f60d5SBruce Evans goto out; 68127a0bc89SDoug Rabson 68227a0bc89SDoug Rabson /* 68327a0bc89SDoug Rabson * If the file has been deleted and it is on a read/write 68427a0bc89SDoug Rabson * filesystem, then truncate the file, and mark the directory slot 68527a0bc89SDoug Rabson * as empty. (This may not be necessary for the dos filesystem.) 68627a0bc89SDoug Rabson */ 68727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 688f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", 68927a0bc89SDoug Rabson dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 69027a0bc89SDoug Rabson #endif 69127a0bc89SDoug Rabson if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 692af3f60d5SBruce Evans error = detrunc(dep, (u_long) 0, 0, NOCRED, p); 69327a0bc89SDoug Rabson dep->de_flag |= DE_UPDATE; 69427a0bc89SDoug Rabson dep->de_Name[0] = SLOT_DELETED; 69527a0bc89SDoug Rabson } 696952a6212SJordan K. Hubbard deupdat(dep, 0); 697952a6212SJordan K. Hubbard 698af3f60d5SBruce Evans out: 699af3f60d5SBruce Evans VOP_UNLOCK(vp, 0, p); 70027a0bc89SDoug Rabson /* 701952a6212SJordan K. Hubbard * If we are done with the denode, reclaim it 702952a6212SJordan K. Hubbard * so that it can be reused immediately. 70327a0bc89SDoug Rabson */ 70427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 70527a0bc89SDoug Rabson printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount, 70627a0bc89SDoug Rabson dep->de_Name[0]); 70727a0bc89SDoug Rabson #endif 708af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 709a5db4bf4SJohn Dyson vrecycle(vp, (struct simplelock *)0, p); 710952a6212SJordan K. Hubbard return (error); 71127a0bc89SDoug Rabson } 712