1c3aac50fSPeter Wemm /* $FreeBSD$ */ 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> 531c5bb3eaSPeter Wemm #include <sys/kernel.h> 5427a0bc89SDoug Rabson #include <sys/mount.h> 5527a0bc89SDoug Rabson #include <sys/malloc.h> 569626b608SPoul-Henning Kamp #include <sys/bio.h> 5727a0bc89SDoug Rabson #include <sys/buf.h> 5827a0bc89SDoug Rabson #include <sys/vnode.h> 591b367556SJason Evans #include <sys/mutex.h> 6027a0bc89SDoug Rabson 61c3c6d51eSPoul-Henning Kamp #include <vm/vm.h> 62efeaf95aSDavid Greenman #include <vm/vm_extern.h> 63c3c6d51eSPoul-Henning Kamp 641166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h> 651166fb51SRuslan Ermilov #include <fs/msdosfs/msdosfsmount.h> 661166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h> 671166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h> 681166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h> 6927a0bc89SDoug Rabson 70a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part"); 7155166637SPoul-Henning Kamp 72303b270bSEivind Eklund static struct denode **dehashtbl; 73303b270bSEivind Eklund static u_long dehash; /* size of hash table - 1 */ 74bfbb9ce6SPoul-Henning Kamp #define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \ 75952a6212SJordan K. Hubbard sizeof(struct direntry)) & dehash]) 761b367556SJason Evans static struct mtx dehash_mtx; 7767af3913SWarner Losh static int dehash_init; 7827a0bc89SDoug Rabson 7958c27bcfSBruce Evans static struct denode * 8089c9c53dSPoul-Henning Kamp msdosfs_hashget(struct cdev *dev, u_long dirclust, u_long diroff); 8111caded3SAlfred Perlstein static void msdosfs_hashins(struct denode *dep); 8211caded3SAlfred Perlstein static void msdosfs_hashrem(struct denode *dep); 8358c27bcfSBruce Evans 84952a6212SJordan K. Hubbard /*ARGSUSED*/ 85952a6212SJordan K. Hubbard int 86952a6212SJordan K. Hubbard msdosfs_init(vfsp) 878092a8e1SMike Pritchard struct vfsconf *vfsp; 8827a0bc89SDoug Rabson { 8967af3913SWarner Losh /* 9067af3913SWarner Losh * The following lines prevent us from initializing the mutex 9167af3913SWarner Losh * init multiple times. I'm not sure why we get called multiple 9267af3913SWarner Losh * times, but the following prevents the panic when we initalize 9367af3913SWarner Losh * the mutext the second time. XXX BAD XXX 9467af3913SWarner Losh */ 9567af3913SWarner Losh if (dehash_init) { 9667af3913SWarner Losh printf("Warning: msdosfs_init called more than once!\n"); 9767af3913SWarner Losh return (0); 9867af3913SWarner Losh } 9967af3913SWarner Losh dehash_init++; 10027a0bc89SDoug Rabson dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, &dehash); 1016008862bSJohn Baldwin mtx_init(&dehash_mtx, "msdosfs dehash", NULL, MTX_DEF); 102952a6212SJordan K. Hubbard return (0); 10327a0bc89SDoug Rabson } 10427a0bc89SDoug Rabson 105432a8400SBoris Popov int 106432a8400SBoris Popov msdosfs_uninit(vfsp) 107432a8400SBoris Popov struct vfsconf *vfsp; 108432a8400SBoris Popov { 109432a8400SBoris Popov 110432a8400SBoris Popov if (dehashtbl) 111432a8400SBoris Popov free(dehashtbl, M_MSDOSFSMNT); 1121b367556SJason Evans mtx_destroy(&dehash_mtx); 11367af3913SWarner Losh dehash_init--; 114432a8400SBoris Popov return (0); 115432a8400SBoris Popov } 116432a8400SBoris Popov 11727a0bc89SDoug Rabson static struct denode * 11827a0bc89SDoug Rabson msdosfs_hashget(dev, dirclust, diroff) 11989c9c53dSPoul-Henning Kamp struct cdev *dev; 12027a0bc89SDoug Rabson u_long dirclust; 12127a0bc89SDoug Rabson u_long diroff; 12227a0bc89SDoug Rabson { 123b40ce416SJulian Elischer struct thread *td = curthread; /* XXX */ 12427a0bc89SDoug Rabson struct denode *dep; 125af3f60d5SBruce Evans struct vnode *vp; 12627a0bc89SDoug Rabson 127af3f60d5SBruce Evans loop: 1289ed346baSBosko Milekic mtx_lock(&dehash_mtx); 129952a6212SJordan K. Hubbard for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) { 130af3f60d5SBruce Evans if (dirclust == dep->de_dirclust 131af3f60d5SBruce Evans && diroff == dep->de_diroffset 132af3f60d5SBruce Evans && dev == dep->de_dev 133af3f60d5SBruce Evans && dep->de_refcnt != 0) { 134af3f60d5SBruce Evans vp = DETOV(dep); 1359ed346baSBosko Milekic mtx_lock(&vp->v_interlock); 1369ed346baSBosko Milekic mtx_unlock(&dehash_mtx); 137b40ce416SJulian Elischer if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) 138af3f60d5SBruce Evans goto loop; 139af3f60d5SBruce Evans return (dep); 14027a0bc89SDoug Rabson } 14127a0bc89SDoug Rabson } 1429ed346baSBosko Milekic mtx_unlock(&dehash_mtx); 143af3f60d5SBruce Evans return (NULL); 14427a0bc89SDoug Rabson } 14527a0bc89SDoug Rabson 14627a0bc89SDoug Rabson static void 14727a0bc89SDoug Rabson msdosfs_hashins(dep) 14827a0bc89SDoug Rabson struct denode *dep; 14927a0bc89SDoug Rabson { 15027a0bc89SDoug Rabson struct denode **depp, *deq; 15127a0bc89SDoug Rabson 1529ed346baSBosko Milekic mtx_lock(&dehash_mtx); 153952a6212SJordan K. Hubbard depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset); 154c3c6d51eSPoul-Henning Kamp deq = *depp; 155c3c6d51eSPoul-Henning Kamp if (deq) 15627a0bc89SDoug Rabson deq->de_prev = &dep->de_next; 15727a0bc89SDoug Rabson dep->de_next = deq; 15827a0bc89SDoug Rabson dep->de_prev = depp; 15927a0bc89SDoug Rabson *depp = dep; 1609ed346baSBosko Milekic mtx_unlock(&dehash_mtx); 16127a0bc89SDoug Rabson } 16227a0bc89SDoug Rabson 16327a0bc89SDoug Rabson static void 16427a0bc89SDoug Rabson msdosfs_hashrem(dep) 16527a0bc89SDoug Rabson struct denode *dep; 16627a0bc89SDoug Rabson { 16727a0bc89SDoug Rabson struct denode *deq; 168af3f60d5SBruce Evans 1699ed346baSBosko Milekic mtx_lock(&dehash_mtx); 170c3c6d51eSPoul-Henning Kamp deq = dep->de_next; 171c3c6d51eSPoul-Henning Kamp if (deq) 17227a0bc89SDoug Rabson deq->de_prev = dep->de_prev; 17327a0bc89SDoug Rabson *dep->de_prev = deq; 17427a0bc89SDoug Rabson #ifdef DIAGNOSTIC 17527a0bc89SDoug Rabson dep->de_next = NULL; 17627a0bc89SDoug Rabson dep->de_prev = NULL; 17727a0bc89SDoug Rabson #endif 1789ed346baSBosko Milekic mtx_unlock(&dehash_mtx); 17927a0bc89SDoug Rabson } 18027a0bc89SDoug Rabson 18127a0bc89SDoug Rabson /* 18227a0bc89SDoug Rabson * If deget() succeeds it returns with the gotten denode locked(). 18327a0bc89SDoug Rabson * 18427a0bc89SDoug Rabson * pmp - address of msdosfsmount structure of the filesystem containing 18527a0bc89SDoug Rabson * the denode of interest. The pm_dev field and the address of 18627a0bc89SDoug Rabson * the msdosfsmount structure are used. 18727a0bc89SDoug Rabson * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 18827a0bc89SDoug Rabson * diroffset is relative to the beginning of the root directory, 18927a0bc89SDoug Rabson * otherwise it is cluster relative. 19027a0bc89SDoug Rabson * diroffset - offset past begin of cluster of denode we want 19127a0bc89SDoug Rabson * depp - returns the address of the gotten denode. 19227a0bc89SDoug Rabson */ 19327a0bc89SDoug Rabson int 194952a6212SJordan K. Hubbard deget(pmp, dirclust, diroffset, depp) 19527a0bc89SDoug Rabson struct msdosfsmount *pmp; /* so we know the maj/min number */ 19627a0bc89SDoug Rabson u_long dirclust; /* cluster this dir entry came from */ 19727a0bc89SDoug Rabson u_long diroffset; /* index of entry within the cluster */ 19827a0bc89SDoug Rabson struct denode **depp; /* returns the addr of the gotten denode */ 19927a0bc89SDoug Rabson { 20027a0bc89SDoug Rabson int error; 20189c9c53dSPoul-Henning Kamp struct cdev *dev = pmp->pm_dev; 20227a0bc89SDoug Rabson struct mount *mntp = pmp->pm_mountp; 203952a6212SJordan K. Hubbard struct direntry *direntptr; 20427a0bc89SDoug Rabson struct denode *ldep; 20527a0bc89SDoug Rabson struct vnode *nvp; 20627a0bc89SDoug Rabson struct buf *bp; 207b40ce416SJulian Elischer struct thread *td = curthread; /* XXX */ 20827a0bc89SDoug Rabson 20927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 210952a6212SJordan K. Hubbard printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 211952a6212SJordan K. Hubbard pmp, dirclust, diroffset, depp); 21227a0bc89SDoug Rabson #endif 21327a0bc89SDoug Rabson 21427a0bc89SDoug Rabson /* 215952a6212SJordan K. Hubbard * On FAT32 filesystems, root is a (more or less) normal 216952a6212SJordan K. Hubbard * directory 21727a0bc89SDoug Rabson */ 218952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == MSDOSFSROOT) 219952a6212SJordan K. Hubbard dirclust = pmp->pm_rootdirblk; 22027a0bc89SDoug Rabson 22127a0bc89SDoug Rabson /* 22227a0bc89SDoug Rabson * See if the denode is in the denode cache. Use the location of 22327a0bc89SDoug Rabson * the directory entry to compute the hash value. For subdir use 224952a6212SJordan K. Hubbard * address of "." entry. For root dir (if not FAT32) use cluster 225952a6212SJordan K. Hubbard * MSDOSFSROOT, offset MSDOSFSROOT_OFS 22627a0bc89SDoug Rabson * 22727a0bc89SDoug Rabson * NOTE: The check for de_refcnt > 0 below insures the denode being 22827a0bc89SDoug Rabson * examined does not represent an unlinked but still open file. 22927a0bc89SDoug Rabson * These files are not to be accessible even when the directory 23027a0bc89SDoug Rabson * entry that represented the file happens to be reused while the 23127a0bc89SDoug Rabson * deleted file is still open. 23227a0bc89SDoug Rabson */ 233c3c6d51eSPoul-Henning Kamp ldep = msdosfs_hashget(dev, dirclust, diroffset); 234c3c6d51eSPoul-Henning Kamp if (ldep) { 23527a0bc89SDoug Rabson *depp = ldep; 236952a6212SJordan K. Hubbard return (0); 23727a0bc89SDoug Rabson } 23827a0bc89SDoug Rabson 2392f9bae59SDavid Greenman /* 2402f9bae59SDavid Greenman * Do the MALLOC before the getnewvnode since doing so afterward 2412f9bae59SDavid Greenman * might cause a bogus v_data pointer to get dereferenced 2422f9bae59SDavid Greenman * elsewhere if MALLOC should block. 2432f9bae59SDavid Greenman */ 244a163d034SWarner Losh MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK); 24527a0bc89SDoug Rabson 24627a0bc89SDoug Rabson /* 24727a0bc89SDoug Rabson * Directory entry was not in cache, have to create a vnode and 24827a0bc89SDoug Rabson * copy it from the passed disk buffer. 24927a0bc89SDoug Rabson */ 25027a0bc89SDoug Rabson /* getnewvnode() does a VREF() on the vnode */ 25106be2aaaSNate Lawson error = getnewvnode("msdosfs", mntp, msdosfs_vnodeop_p, &nvp); 252c3c6d51eSPoul-Henning Kamp if (error) { 2532f9bae59SDavid Greenman *depp = NULL; 2542f9bae59SDavid Greenman FREE(ldep, M_MSDOSFSNODE); 25527a0bc89SDoug Rabson return error; 25627a0bc89SDoug Rabson } 25727a0bc89SDoug Rabson bzero((caddr_t)ldep, sizeof *ldep); 25827a0bc89SDoug Rabson nvp->v_data = ldep; 25927a0bc89SDoug Rabson ldep->de_vnode = nvp; 26027a0bc89SDoug Rabson ldep->de_flag = 0; 26127a0bc89SDoug Rabson ldep->de_devvp = 0; 26227a0bc89SDoug Rabson ldep->de_dev = dev; 26327a0bc89SDoug Rabson ldep->de_dirclust = dirclust; 26427a0bc89SDoug Rabson ldep->de_diroffset = diroffset; 26527a0bc89SDoug Rabson fc_purge(ldep, 0); /* init the fat cache for this denode */ 26627a0bc89SDoug Rabson 26727a0bc89SDoug Rabson /* 268af3f60d5SBruce Evans * Lock the denode so that it can't be accessed until we've read 269af3f60d5SBruce Evans * it in and have done what we need to it. Do this here instead 270af3f60d5SBruce Evans * of at the start of msdosfs_hashins() so that reinsert() can 271af3f60d5SBruce Evans * call msdosfs_hashins() with a locked denode. 27227a0bc89SDoug Rabson */ 273b40ce416SJulian Elischer if (VOP_LOCK(nvp, LK_EXCLUSIVE, td) != 0) 274af3f60d5SBruce Evans panic("deget: unexpected lock failure"); 275af3f60d5SBruce Evans 276af3f60d5SBruce Evans /* 277af3f60d5SBruce Evans * Insert the denode into the hash queue. 278af3f60d5SBruce Evans */ 27927a0bc89SDoug Rabson msdosfs_hashins(ldep); 28027a0bc89SDoug Rabson 281952a6212SJordan K. Hubbard ldep->de_pmp = pmp; 282952a6212SJordan K. Hubbard ldep->de_refcnt = 1; 28327a0bc89SDoug Rabson /* 28427a0bc89SDoug Rabson * Copy the directory entry into the denode area of the vnode. 28527a0bc89SDoug Rabson */ 286952a6212SJordan K. Hubbard if ((dirclust == MSDOSFSROOT 287952a6212SJordan K. Hubbard || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 288952a6212SJordan K. Hubbard && diroffset == MSDOSFSROOT_OFS) { 28927a0bc89SDoug Rabson /* 29027a0bc89SDoug Rabson * Directory entry for the root directory. There isn't one, 29127a0bc89SDoug Rabson * so we manufacture one. We should probably rummage 29227a0bc89SDoug Rabson * through the root directory and find a label entry (if it 29327a0bc89SDoug Rabson * exists), and then use the time and date from that entry 29427a0bc89SDoug Rabson * as the time and date for the root denode. 29527a0bc89SDoug Rabson */ 296e6e370a7SJeff Roberson nvp->v_vflag |= VV_ROOT; /* should be further down XXX */ 297952a6212SJordan K. Hubbard 29827a0bc89SDoug Rabson ldep->de_Attributes = ATTR_DIRECTORY; 299bad3d41dSDmitrij Tejblum ldep->de_LowerCase = 0; 300952a6212SJordan K. Hubbard if (FAT32(pmp)) 301952a6212SJordan K. Hubbard ldep->de_StartCluster = pmp->pm_rootdirblk; 302952a6212SJordan K. Hubbard /* de_FileSize will be filled in further down */ 303952a6212SJordan K. Hubbard else { 30427a0bc89SDoug Rabson ldep->de_StartCluster = MSDOSFSROOT; 30501f6cfbaSYoshihiro Takahashi ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 306952a6212SJordan K. Hubbard } 30727a0bc89SDoug Rabson /* 30827a0bc89SDoug Rabson * fill in time and date so that dos2unixtime() doesn't 30927a0bc89SDoug Rabson * spit up when called from msdosfs_getattr() with root 31027a0bc89SDoug Rabson * denode 31127a0bc89SDoug Rabson */ 312952a6212SJordan K. Hubbard ldep->de_CHun = 0; 313952a6212SJordan K. Hubbard ldep->de_CTime = 0x0000; /* 00:00:00 */ 314952a6212SJordan K. Hubbard ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 31527a0bc89SDoug Rabson | (1 << DD_DAY_SHIFT); 31627a0bc89SDoug Rabson /* Jan 1, 1980 */ 317952a6212SJordan K. Hubbard ldep->de_ADate = ldep->de_CDate; 318952a6212SJordan K. Hubbard ldep->de_MTime = ldep->de_CTime; 319952a6212SJordan K. Hubbard ldep->de_MDate = ldep->de_CDate; 32027a0bc89SDoug Rabson /* leave the other fields as garbage */ 32127a0bc89SDoug Rabson } else { 322952a6212SJordan K. Hubbard error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 3235097a20dSBruce Evans if (error) { 3245097a20dSBruce Evans /* 3255097a20dSBruce Evans * The denode does not contain anything useful, so 3265097a20dSBruce Evans * it would be wrong to leave it on its hash chain. 3275097a20dSBruce Evans * Arrange for vput() to just forget about it. 3285097a20dSBruce Evans */ 3295097a20dSBruce Evans ldep->de_Name[0] = SLOT_DELETED; 3305097a20dSBruce Evans 3315097a20dSBruce Evans vput(nvp); 3325097a20dSBruce Evans *depp = NULL; 333952a6212SJordan K. Hubbard return (error); 3345097a20dSBruce Evans } 33527a0bc89SDoug Rabson DE_INTERNALIZE(ldep, direntptr); 33627a0bc89SDoug Rabson brelse(bp); 33727a0bc89SDoug Rabson } 33827a0bc89SDoug Rabson 33927a0bc89SDoug Rabson /* 34027a0bc89SDoug Rabson * Fill in a few fields of the vnode and finish filling in the 34127a0bc89SDoug Rabson * denode. Then return the address of the found denode. 34227a0bc89SDoug Rabson */ 34327a0bc89SDoug Rabson if (ldep->de_Attributes & ATTR_DIRECTORY) { 34427a0bc89SDoug Rabson /* 34527a0bc89SDoug Rabson * Since DOS directory entries that describe directories 34627a0bc89SDoug Rabson * have 0 in the filesize field, we take this opportunity 34727a0bc89SDoug Rabson * to find out the length of the directory and plug it into 34827a0bc89SDoug Rabson * the denode structure. 34927a0bc89SDoug Rabson */ 35027a0bc89SDoug Rabson u_long size; 35127a0bc89SDoug Rabson 352b5cdbc6dSTom Rhodes /* 353b5cdbc6dSTom Rhodes * XXX Sometimes, these arrives that . entry have cluster 354b5cdbc6dSTom Rhodes * number 0, when it shouldn't. Use real cluster number 355b5cdbc6dSTom Rhodes * instead of what is written in directory entry. 356b5cdbc6dSTom Rhodes */ 357b5cdbc6dSTom Rhodes if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) { 358b5cdbc6dSTom Rhodes printf("deget(): . entry at clust %ld != %ld\n", 359b5cdbc6dSTom Rhodes dirclust, ldep->de_StartCluster); 360b5cdbc6dSTom Rhodes ldep->de_StartCluster = dirclust; 361b5cdbc6dSTom Rhodes } 362b5cdbc6dSTom Rhodes 36327a0bc89SDoug Rabson nvp->v_type = VDIR; 364952a6212SJordan K. Hubbard if (ldep->de_StartCluster != MSDOSFSROOT) { 365952a6212SJordan K. Hubbard error = pcbmap(ldep, 0xffff, 0, &size, 0); 36627a0bc89SDoug Rabson if (error == E2BIG) { 367952a6212SJordan K. Hubbard ldep->de_FileSize = de_cn2off(pmp, size); 36827a0bc89SDoug Rabson error = 0; 36927a0bc89SDoug Rabson } else 37027a0bc89SDoug Rabson printf("deget(): pcbmap returned %d\n", error); 37127a0bc89SDoug Rabson } 37227a0bc89SDoug Rabson } else 37327a0bc89SDoug Rabson nvp->v_type = VREG; 3741affa3adSPoul-Henning Kamp 3751affa3adSPoul-Henning Kamp ldep->de_modrev = init_va_filerev(); 3765097a20dSBruce Evans ldep->de_devvp = pmp->pm_devvp; 37727a0bc89SDoug Rabson VREF(ldep->de_devvp); 37827a0bc89SDoug Rabson *depp = ldep; 379952a6212SJordan K. Hubbard return (0); 38027a0bc89SDoug Rabson } 38127a0bc89SDoug Rabson 38227a0bc89SDoug Rabson int 383952a6212SJordan K. Hubbard deupdat(dep, waitfor) 38427a0bc89SDoug Rabson struct denode *dep; 38527a0bc89SDoug Rabson int waitfor; 38627a0bc89SDoug Rabson { 38727a0bc89SDoug Rabson int error; 38827a0bc89SDoug Rabson struct buf *bp; 38927a0bc89SDoug Rabson struct direntry *dirp; 390952a6212SJordan K. Hubbard struct timespec ts; 39127a0bc89SDoug Rabson 392952a6212SJordan K. Hubbard if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 393952a6212SJordan K. Hubbard return (0); 394a0502b19SPoul-Henning Kamp getnanotime(&ts); 395952a6212SJordan K. Hubbard DETIMES(dep, &ts, &ts, &ts); 396952a6212SJordan K. Hubbard if ((dep->de_flag & DE_MODIFIED) == 0) 397952a6212SJordan K. Hubbard return (0); 398952a6212SJordan K. Hubbard dep->de_flag &= ~DE_MODIFIED; 399952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 400952a6212SJordan K. Hubbard return (0); 401952a6212SJordan K. Hubbard if (dep->de_refcnt <= 0) 402952a6212SJordan K. Hubbard return (0); 403c3c6d51eSPoul-Henning Kamp error = readde(dep, &bp, &dirp); 404c3c6d51eSPoul-Henning Kamp if (error) 405952a6212SJordan K. Hubbard return (error); 40627a0bc89SDoug Rabson DE_EXTERNALIZE(dirp, dep); 40727a0bc89SDoug Rabson if (waitfor) 408952a6212SJordan K. Hubbard return (bwrite(bp)); 409952a6212SJordan K. Hubbard else { 41027a0bc89SDoug Rabson bdwrite(bp); 411952a6212SJordan K. Hubbard return (0); 412952a6212SJordan K. Hubbard } 41327a0bc89SDoug Rabson } 41427a0bc89SDoug Rabson 41527a0bc89SDoug Rabson /* 41627a0bc89SDoug Rabson * Truncate the file described by dep to the length specified by length. 41727a0bc89SDoug Rabson */ 41827a0bc89SDoug Rabson int 419b40ce416SJulian Elischer detrunc(dep, length, flags, cred, td) 42027a0bc89SDoug Rabson struct denode *dep; 42127a0bc89SDoug Rabson u_long length; 42227a0bc89SDoug Rabson int flags; 42327a0bc89SDoug Rabson struct ucred *cred; 424b40ce416SJulian Elischer struct thread *td; 42527a0bc89SDoug Rabson { 42627a0bc89SDoug Rabson int error; 42727a0bc89SDoug Rabson int allerror; 42827a0bc89SDoug Rabson u_long eofentry; 42927a0bc89SDoug Rabson u_long chaintofree; 43027a0bc89SDoug Rabson daddr_t bn; 43127a0bc89SDoug Rabson int boff; 43227a0bc89SDoug Rabson int isadir = dep->de_Attributes & ATTR_DIRECTORY; 43327a0bc89SDoug Rabson struct buf *bp; 43427a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 43527a0bc89SDoug Rabson 43627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 437952a6212SJordan K. Hubbard printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 43827a0bc89SDoug Rabson #endif 43927a0bc89SDoug Rabson 44027a0bc89SDoug Rabson /* 44127a0bc89SDoug Rabson * Disallow attempts to truncate the root directory since it is of 44227a0bc89SDoug Rabson * fixed size. That's just the way dos filesystems are. We use 44327a0bc89SDoug Rabson * the VROOT bit in the vnode because checking for the directory 44427a0bc89SDoug Rabson * bit and a startcluster of 0 in the denode is not adequate to 44527a0bc89SDoug Rabson * recognize the root directory at this point in a file or 44627a0bc89SDoug Rabson * directory's life. 44727a0bc89SDoug Rabson */ 448e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) { 449952a6212SJordan K. Hubbard printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 45027a0bc89SDoug Rabson dep->de_dirclust, dep->de_diroffset); 451952a6212SJordan K. Hubbard return (EINVAL); 45227a0bc89SDoug Rabson } 45327a0bc89SDoug Rabson 45427a0bc89SDoug Rabson 455bd7e5f99SJohn Dyson if (dep->de_FileSize < length) { 456bd7e5f99SJohn Dyson vnode_pager_setsize(DETOV(dep), length); 45727a0bc89SDoug Rabson return deextend(dep, length, cred); 458bd7e5f99SJohn Dyson } 45927a0bc89SDoug Rabson 46027a0bc89SDoug Rabson /* 46127a0bc89SDoug Rabson * If the desired length is 0 then remember the starting cluster of 46227a0bc89SDoug Rabson * the file and set the StartCluster field in the directory entry 46327a0bc89SDoug Rabson * to 0. If the desired length is not zero, then get the number of 46427a0bc89SDoug Rabson * the last cluster in the shortened file. Then get the number of 46527a0bc89SDoug Rabson * the first cluster in the part of the file that is to be freed. 46627a0bc89SDoug Rabson * Then set the next cluster pointer in the last cluster of the 46727a0bc89SDoug Rabson * file to CLUST_EOFE. 46827a0bc89SDoug Rabson */ 46927a0bc89SDoug Rabson if (length == 0) { 47027a0bc89SDoug Rabson chaintofree = dep->de_StartCluster; 47127a0bc89SDoug Rabson dep->de_StartCluster = 0; 47227a0bc89SDoug Rabson eofentry = ~0; 47327a0bc89SDoug Rabson } else { 474952a6212SJordan K. Hubbard error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 475952a6212SJordan K. Hubbard &eofentry, 0); 476c3c6d51eSPoul-Henning Kamp if (error) { 47727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 47827a0bc89SDoug Rabson printf("detrunc(): pcbmap fails %d\n", error); 47927a0bc89SDoug Rabson #endif 480952a6212SJordan K. Hubbard return (error); 48127a0bc89SDoug Rabson } 48227a0bc89SDoug Rabson } 48327a0bc89SDoug Rabson 484952a6212SJordan K. Hubbard fc_purge(dep, de_clcount(pmp, length)); 48527a0bc89SDoug Rabson 48627a0bc89SDoug Rabson /* 48727a0bc89SDoug Rabson * If the new length is not a multiple of the cluster size then we 48827a0bc89SDoug Rabson * must zero the tail end of the new last cluster in case it 48927a0bc89SDoug Rabson * becomes part of the file again because of a seek. 49027a0bc89SDoug Rabson */ 49127a0bc89SDoug Rabson if ((boff = length & pmp->pm_crbomask) != 0) { 49227a0bc89SDoug Rabson if (isadir) { 49327a0bc89SDoug Rabson bn = cntobn(pmp, eofentry); 49427a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 49527a0bc89SDoug Rabson NOCRED, &bp); 49627a0bc89SDoug Rabson if (error) { 497952a6212SJordan K. Hubbard brelse(bp); 49827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 49927a0bc89SDoug Rabson printf("detrunc(): bread fails %d\n", error); 50027a0bc89SDoug Rabson #endif 501952a6212SJordan K. Hubbard return (error); 50227a0bc89SDoug Rabson } 50327a0bc89SDoug Rabson bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 50427a0bc89SDoug Rabson if (flags & IO_SYNC) 50527a0bc89SDoug Rabson bwrite(bp); 50627a0bc89SDoug Rabson else 50727a0bc89SDoug Rabson bdwrite(bp); 50827a0bc89SDoug Rabson } 509c2f95f66STom Rhodes } 51027a0bc89SDoug Rabson 51127a0bc89SDoug Rabson /* 51227a0bc89SDoug Rabson * Write out the updated directory entry. Even if the update fails 51327a0bc89SDoug Rabson * we free the trailing clusters. 51427a0bc89SDoug Rabson */ 51527a0bc89SDoug Rabson dep->de_FileSize = length; 516952a6212SJordan K. Hubbard if (!isadir) 517952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE|DE_MODIFIED; 518b40ce416SJulian Elischer allerror = vtruncbuf(DETOV(dep), cred, td, length, pmp->pm_bpcluster); 51944fdad99SPeter Wemm #ifdef MSDOSFS_DEBUG 52044fdad99SPeter Wemm if (allerror) 52144fdad99SPeter Wemm printf("detrunc(): vtruncbuf error %d\n", allerror); 52244fdad99SPeter Wemm #endif 52344fdad99SPeter Wemm error = deupdat(dep, 1); 52444fdad99SPeter Wemm if (error && (allerror == 0)) 52544fdad99SPeter Wemm allerror = error; 52627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 527952a6212SJordan K. Hubbard printf("detrunc(): allerror %d, eofentry %lu\n", 52827a0bc89SDoug Rabson allerror, eofentry); 52927a0bc89SDoug Rabson #endif 53027a0bc89SDoug Rabson 53127a0bc89SDoug Rabson /* 53227a0bc89SDoug Rabson * If we need to break the cluster chain for the file then do it 53327a0bc89SDoug Rabson * now. 53427a0bc89SDoug Rabson */ 53527a0bc89SDoug Rabson if (eofentry != ~0) { 53627a0bc89SDoug Rabson error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 53727a0bc89SDoug Rabson &chaintofree, CLUST_EOFE); 53827a0bc89SDoug Rabson if (error) { 53927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 54027a0bc89SDoug Rabson printf("detrunc(): fatentry errors %d\n", error); 54127a0bc89SDoug Rabson #endif 542952a6212SJordan K. Hubbard return (error); 54327a0bc89SDoug Rabson } 544952a6212SJordan K. Hubbard fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 54527a0bc89SDoug Rabson eofentry); 54627a0bc89SDoug Rabson } 54727a0bc89SDoug Rabson 54827a0bc89SDoug Rabson /* 54927a0bc89SDoug Rabson * Now free the clusters removed from the file because of the 55027a0bc89SDoug Rabson * truncation. 55127a0bc89SDoug Rabson */ 552952a6212SJordan K. Hubbard if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 55327a0bc89SDoug Rabson freeclusterchain(pmp, chaintofree); 55427a0bc89SDoug Rabson 555952a6212SJordan K. Hubbard return (allerror); 55627a0bc89SDoug Rabson } 55727a0bc89SDoug Rabson 55827a0bc89SDoug Rabson /* 55927a0bc89SDoug Rabson * Extend the file described by dep to length specified by length. 56027a0bc89SDoug Rabson */ 56127a0bc89SDoug Rabson int 56227a0bc89SDoug Rabson deextend(dep, length, cred) 56327a0bc89SDoug Rabson struct denode *dep; 564952a6212SJordan K. Hubbard u_long length; 56527a0bc89SDoug Rabson struct ucred *cred; 56627a0bc89SDoug Rabson { 56727a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 56827a0bc89SDoug Rabson u_long count; 56927a0bc89SDoug Rabson int error; 57027a0bc89SDoug Rabson 57127a0bc89SDoug Rabson /* 57227a0bc89SDoug Rabson * The root of a DOS filesystem cannot be extended. 57327a0bc89SDoug Rabson */ 574e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 575952a6212SJordan K. Hubbard return (EINVAL); 57627a0bc89SDoug Rabson 57727a0bc89SDoug Rabson /* 578952a6212SJordan K. Hubbard * Directories cannot be extended. 57927a0bc89SDoug Rabson */ 580952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 581952a6212SJordan K. Hubbard return (EISDIR); 58227a0bc89SDoug Rabson 58327a0bc89SDoug Rabson if (length <= dep->de_FileSize) 58427a0bc89SDoug Rabson panic("deextend: file too large"); 58527a0bc89SDoug Rabson 58627a0bc89SDoug Rabson /* 58727a0bc89SDoug Rabson * Compute the number of clusters to allocate. 58827a0bc89SDoug Rabson */ 58927a0bc89SDoug Rabson count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 59027a0bc89SDoug Rabson if (count > 0) { 59127a0bc89SDoug Rabson if (count > pmp->pm_freeclustercount) 592952a6212SJordan K. Hubbard return (ENOSPC); 593c3c6d51eSPoul-Henning Kamp error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 594c3c6d51eSPoul-Henning Kamp if (error) { 59527a0bc89SDoug Rabson /* truncate the added clusters away again */ 59627a0bc89SDoug Rabson (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 597952a6212SJordan K. Hubbard return (error); 59827a0bc89SDoug Rabson } 59927a0bc89SDoug Rabson } 60027a0bc89SDoug Rabson dep->de_FileSize = length; 601952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE|DE_MODIFIED; 602952a6212SJordan K. Hubbard return (deupdat(dep, 1)); 60327a0bc89SDoug Rabson } 60427a0bc89SDoug Rabson 60527a0bc89SDoug Rabson /* 60627a0bc89SDoug Rabson * Move a denode to its correct hash queue after the file it represents has 60727a0bc89SDoug Rabson * been moved to a new directory. 60827a0bc89SDoug Rabson */ 609952a6212SJordan K. Hubbard void 610952a6212SJordan K. Hubbard reinsert(dep) 61127a0bc89SDoug Rabson struct denode *dep; 61227a0bc89SDoug Rabson { 61327a0bc89SDoug Rabson /* 61427a0bc89SDoug Rabson * Fix up the denode cache. If the denode is for a directory, 61527a0bc89SDoug Rabson * there is nothing to do since the hash is based on the starting 61627a0bc89SDoug Rabson * cluster of the directory file and that hasn't changed. If for a 61727a0bc89SDoug Rabson * file the hash is based on the location of the directory entry, 61827a0bc89SDoug Rabson * so we must remove it from the cache and re-enter it with the 61927a0bc89SDoug Rabson * hash based on the new location of the directory entry. 62027a0bc89SDoug Rabson */ 621952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 622952a6212SJordan K. Hubbard return; 62327a0bc89SDoug Rabson msdosfs_hashrem(dep); 62427a0bc89SDoug Rabson msdosfs_hashins(dep); 62527a0bc89SDoug Rabson } 62627a0bc89SDoug Rabson 62727a0bc89SDoug Rabson int 62827a0bc89SDoug Rabson msdosfs_reclaim(ap) 62927a0bc89SDoug Rabson struct vop_reclaim_args /* { 63027a0bc89SDoug Rabson struct vnode *a_vp; 63127a0bc89SDoug Rabson } */ *ap; 63227a0bc89SDoug Rabson { 63327a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 63427a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 63527a0bc89SDoug Rabson 63627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 637f0707215SPoul-Henning Kamp printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 63827a0bc89SDoug Rabson dep, dep->de_Name, dep->de_refcnt); 63927a0bc89SDoug Rabson #endif 64027a0bc89SDoug Rabson 6414d93c0beSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 64227a0bc89SDoug Rabson vprint("msdosfs_reclaim(): pushing active", vp); 64327a0bc89SDoug Rabson /* 644952a6212SJordan K. Hubbard * Remove the denode from its hash chain. 64527a0bc89SDoug Rabson */ 64627a0bc89SDoug Rabson msdosfs_hashrem(dep); 64727a0bc89SDoug Rabson /* 648952a6212SJordan K. Hubbard * Purge old data structures associated with the denode. 64927a0bc89SDoug Rabson */ 65027a0bc89SDoug Rabson if (dep->de_devvp) { 65127a0bc89SDoug Rabson vrele(dep->de_devvp); 65227a0bc89SDoug Rabson dep->de_devvp = 0; 65327a0bc89SDoug Rabson } 654952a6212SJordan K. Hubbard #if 0 /* XXX */ 65527a0bc89SDoug Rabson dep->de_flag = 0; 656952a6212SJordan K. Hubbard #endif 65727a0bc89SDoug Rabson FREE(dep, M_MSDOSFSNODE); 65827a0bc89SDoug Rabson vp->v_data = NULL; 65927a0bc89SDoug Rabson 660952a6212SJordan K. Hubbard return (0); 66127a0bc89SDoug Rabson } 66227a0bc89SDoug Rabson 66327a0bc89SDoug Rabson int 66427a0bc89SDoug Rabson msdosfs_inactive(ap) 66527a0bc89SDoug Rabson struct vop_inactive_args /* { 66627a0bc89SDoug Rabson struct vnode *a_vp; 667b40ce416SJulian Elischer struct thread *a_td; 66827a0bc89SDoug Rabson } */ *ap; 66927a0bc89SDoug Rabson { 67027a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 67127a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 672b40ce416SJulian Elischer struct thread *td = ap->a_td; 67327a0bc89SDoug Rabson int error = 0; 67427a0bc89SDoug Rabson 67527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 676f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 67727a0bc89SDoug Rabson #endif 67827a0bc89SDoug Rabson 6794d93c0beSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 68027a0bc89SDoug Rabson vprint("msdosfs_inactive(): pushing active", vp); 68127a0bc89SDoug Rabson 68227a0bc89SDoug Rabson /* 683952a6212SJordan K. Hubbard * Ignore denodes related to stale file handles. 68427a0bc89SDoug Rabson */ 685af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 686af3f60d5SBruce Evans goto out; 68727a0bc89SDoug Rabson 68827a0bc89SDoug Rabson /* 68927a0bc89SDoug Rabson * If the file has been deleted and it is on a read/write 69027a0bc89SDoug Rabson * filesystem, then truncate the file, and mark the directory slot 69127a0bc89SDoug Rabson * as empty. (This may not be necessary for the dos filesystem.) 69227a0bc89SDoug Rabson */ 69327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 694f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", 69527a0bc89SDoug Rabson dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 69627a0bc89SDoug Rabson #endif 69727a0bc89SDoug Rabson if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 698b40ce416SJulian Elischer error = detrunc(dep, (u_long) 0, 0, NOCRED, td); 69927a0bc89SDoug Rabson dep->de_flag |= DE_UPDATE; 70027a0bc89SDoug Rabson dep->de_Name[0] = SLOT_DELETED; 70127a0bc89SDoug Rabson } 702952a6212SJordan K. Hubbard deupdat(dep, 0); 703952a6212SJordan K. Hubbard 704af3f60d5SBruce Evans out: 705b40ce416SJulian Elischer VOP_UNLOCK(vp, 0, td); 70627a0bc89SDoug Rabson /* 707952a6212SJordan K. Hubbard * If we are done with the denode, reclaim it 708952a6212SJordan K. Hubbard * so that it can be reused immediately. 70927a0bc89SDoug Rabson */ 71027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 7114d93c0beSJeff Roberson printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 7124d93c0beSJeff Roberson vrefcnt(vp), dep->de_Name[0]); 71327a0bc89SDoug Rabson #endif 714af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 715b40ce416SJulian Elischer vrecycle(vp, NULL, td); 716952a6212SJordan K. Hubbard return (error); 71727a0bc89SDoug Rabson } 718