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 */ 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> 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 72a30fc63bSPoul-Henning Kamp #define DEHASH(dcl, doff) ((dcl) + (doff) / sizeof(struct direntry)) 7327a0bc89SDoug Rabson 7427a0bc89SDoug Rabson /* 7527a0bc89SDoug Rabson * If deget() succeeds it returns with the gotten denode locked(). 7627a0bc89SDoug Rabson * 7727a0bc89SDoug Rabson * pmp - address of msdosfsmount structure of the filesystem containing 783b97f388SPoul-Henning Kamp * the denode of interest. The address of 7927a0bc89SDoug Rabson * the msdosfsmount structure are used. 8027a0bc89SDoug Rabson * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 8127a0bc89SDoug Rabson * diroffset is relative to the beginning of the root directory, 8227a0bc89SDoug Rabson * otherwise it is cluster relative. 8327a0bc89SDoug Rabson * diroffset - offset past begin of cluster of denode we want 8427a0bc89SDoug Rabson * depp - returns the address of the gotten denode. 8527a0bc89SDoug Rabson */ 8627a0bc89SDoug Rabson int 87952a6212SJordan K. Hubbard deget(pmp, dirclust, diroffset, depp) 8827a0bc89SDoug Rabson struct msdosfsmount *pmp; /* so we know the maj/min number */ 8927a0bc89SDoug Rabson u_long dirclust; /* cluster this dir entry came from */ 9027a0bc89SDoug Rabson u_long diroffset; /* index of entry within the cluster */ 9127a0bc89SDoug Rabson struct denode **depp; /* returns the addr of the gotten denode */ 9227a0bc89SDoug Rabson { 9327a0bc89SDoug Rabson int error; 94a30fc63bSPoul-Henning Kamp u_int hash; 9527a0bc89SDoug Rabson struct mount *mntp = pmp->pm_mountp; 96952a6212SJordan K. Hubbard struct direntry *direntptr; 9727a0bc89SDoug Rabson struct denode *ldep; 98a30fc63bSPoul-Henning Kamp struct vnode *nvp, *xvp; 9927a0bc89SDoug Rabson struct buf *bp; 10027a0bc89SDoug Rabson 101a30fc63bSPoul-Henning Kamp hash = DEHASH(dirclust, diroffset); 102a30fc63bSPoul-Henning Kamp 10327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 104952a6212SJordan K. Hubbard printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 105952a6212SJordan K. Hubbard pmp, dirclust, diroffset, depp); 10627a0bc89SDoug Rabson #endif 10727a0bc89SDoug Rabson 10827a0bc89SDoug Rabson /* 109952a6212SJordan K. Hubbard * On FAT32 filesystems, root is a (more or less) normal 110952a6212SJordan K. Hubbard * directory 11127a0bc89SDoug Rabson */ 112952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == MSDOSFSROOT) 113952a6212SJordan K. Hubbard dirclust = pmp->pm_rootdirblk; 11427a0bc89SDoug Rabson 11527a0bc89SDoug Rabson /* 11627a0bc89SDoug Rabson * See if the denode is in the denode cache. Use the location of 11727a0bc89SDoug Rabson * the directory entry to compute the hash value. For subdir use 118952a6212SJordan K. Hubbard * address of "." entry. For root dir (if not FAT32) use cluster 119952a6212SJordan K. Hubbard * MSDOSFSROOT, offset MSDOSFSROOT_OFS 12027a0bc89SDoug Rabson * 12127a0bc89SDoug Rabson * NOTE: The check for de_refcnt > 0 below insures the denode being 12227a0bc89SDoug Rabson * examined does not represent an unlinked but still open file. 12327a0bc89SDoug Rabson * These files are not to be accessible even when the directory 12427a0bc89SDoug Rabson * entry that represented the file happens to be reused while the 12527a0bc89SDoug Rabson * deleted file is still open. 12627a0bc89SDoug Rabson */ 127a30fc63bSPoul-Henning Kamp error = vfs_hash_get(mntp, hash, LK_EXCLUSIVE, curthread, &nvp); 128a30fc63bSPoul-Henning Kamp if (error) 129a30fc63bSPoul-Henning Kamp return(error); 130a30fc63bSPoul-Henning Kamp if (nvp != NULL) { 131a30fc63bSPoul-Henning Kamp *depp = VTODE(nvp); 132952a6212SJordan K. Hubbard return (0); 13327a0bc89SDoug Rabson } 13427a0bc89SDoug Rabson 1352f9bae59SDavid Greenman /* 1362f9bae59SDavid Greenman * Do the MALLOC before the getnewvnode since doing so afterward 1372f9bae59SDavid Greenman * might cause a bogus v_data pointer to get dereferenced 1382f9bae59SDavid Greenman * elsewhere if MALLOC should block. 1392f9bae59SDavid Greenman */ 140a163d034SWarner Losh MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK); 14127a0bc89SDoug Rabson 14227a0bc89SDoug Rabson /* 14327a0bc89SDoug Rabson * Directory entry was not in cache, have to create a vnode and 14427a0bc89SDoug Rabson * copy it from the passed disk buffer. 14527a0bc89SDoug Rabson */ 14627a0bc89SDoug Rabson /* getnewvnode() does a VREF() on the vnode */ 147aec0fb7bSPoul-Henning Kamp error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp); 148c3c6d51eSPoul-Henning Kamp if (error) { 1492f9bae59SDavid Greenman *depp = NULL; 1502f9bae59SDavid Greenman FREE(ldep, M_MSDOSFSNODE); 15127a0bc89SDoug Rabson return error; 15227a0bc89SDoug Rabson } 15327a0bc89SDoug Rabson bzero((caddr_t)ldep, sizeof *ldep); 15427a0bc89SDoug Rabson nvp->v_data = ldep; 15527a0bc89SDoug Rabson ldep->de_vnode = nvp; 15627a0bc89SDoug Rabson ldep->de_flag = 0; 15727a0bc89SDoug Rabson ldep->de_dirclust = dirclust; 15827a0bc89SDoug Rabson ldep->de_diroffset = diroffset; 15927a0bc89SDoug Rabson fc_purge(ldep, 0); /* init the fat cache for this denode */ 16027a0bc89SDoug Rabson 161a30fc63bSPoul-Henning Kamp error = vfs_hash_insert(nvp, hash, 0, curthread, &xvp); 162a30fc63bSPoul-Henning Kamp if (error) { 163a30fc63bSPoul-Henning Kamp *depp = NULL; 164a30fc63bSPoul-Henning Kamp return (error); 165a30fc63bSPoul-Henning Kamp } 166a30fc63bSPoul-Henning Kamp if (xvp != NULL) { 167a30fc63bSPoul-Henning Kamp /* XXX: Not sure this is right */ 168a30fc63bSPoul-Henning Kamp nvp = xvp; 169a30fc63bSPoul-Henning Kamp ldep->de_vnode = nvp; 170a30fc63bSPoul-Henning Kamp } 17127a0bc89SDoug Rabson 172952a6212SJordan K. Hubbard ldep->de_pmp = pmp; 173952a6212SJordan K. Hubbard ldep->de_refcnt = 1; 17427a0bc89SDoug Rabson /* 17527a0bc89SDoug Rabson * Copy the directory entry into the denode area of the vnode. 17627a0bc89SDoug Rabson */ 177952a6212SJordan K. Hubbard if ((dirclust == MSDOSFSROOT 178952a6212SJordan K. Hubbard || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 179952a6212SJordan K. Hubbard && diroffset == MSDOSFSROOT_OFS) { 18027a0bc89SDoug Rabson /* 18127a0bc89SDoug Rabson * Directory entry for the root directory. There isn't one, 18227a0bc89SDoug Rabson * so we manufacture one. We should probably rummage 18327a0bc89SDoug Rabson * through the root directory and find a label entry (if it 18427a0bc89SDoug Rabson * exists), and then use the time and date from that entry 18527a0bc89SDoug Rabson * as the time and date for the root denode. 18627a0bc89SDoug Rabson */ 187e6e370a7SJeff Roberson nvp->v_vflag |= VV_ROOT; /* should be further down XXX */ 188952a6212SJordan K. Hubbard 18927a0bc89SDoug Rabson ldep->de_Attributes = ATTR_DIRECTORY; 190bad3d41dSDmitrij Tejblum ldep->de_LowerCase = 0; 191952a6212SJordan K. Hubbard if (FAT32(pmp)) 192952a6212SJordan K. Hubbard ldep->de_StartCluster = pmp->pm_rootdirblk; 193952a6212SJordan K. Hubbard /* de_FileSize will be filled in further down */ 194952a6212SJordan K. Hubbard else { 19527a0bc89SDoug Rabson ldep->de_StartCluster = MSDOSFSROOT; 19601f6cfbaSYoshihiro Takahashi ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 197952a6212SJordan K. Hubbard } 19827a0bc89SDoug Rabson /* 19927a0bc89SDoug Rabson * fill in time and date so that dos2unixtime() doesn't 20027a0bc89SDoug Rabson * spit up when called from msdosfs_getattr() with root 20127a0bc89SDoug Rabson * denode 20227a0bc89SDoug Rabson */ 203952a6212SJordan K. Hubbard ldep->de_CHun = 0; 204952a6212SJordan K. Hubbard ldep->de_CTime = 0x0000; /* 00:00:00 */ 205952a6212SJordan K. Hubbard ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 20627a0bc89SDoug Rabson | (1 << DD_DAY_SHIFT); 20727a0bc89SDoug Rabson /* Jan 1, 1980 */ 208952a6212SJordan K. Hubbard ldep->de_ADate = ldep->de_CDate; 209952a6212SJordan K. Hubbard ldep->de_MTime = ldep->de_CTime; 210952a6212SJordan K. Hubbard ldep->de_MDate = ldep->de_CDate; 21127a0bc89SDoug Rabson /* leave the other fields as garbage */ 21227a0bc89SDoug Rabson } else { 213952a6212SJordan K. Hubbard error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 2145097a20dSBruce Evans if (error) { 2155097a20dSBruce Evans /* 2165097a20dSBruce Evans * The denode does not contain anything useful, so 2175097a20dSBruce Evans * it would be wrong to leave it on its hash chain. 2185097a20dSBruce Evans * Arrange for vput() to just forget about it. 2195097a20dSBruce Evans */ 2205097a20dSBruce Evans ldep->de_Name[0] = SLOT_DELETED; 2215097a20dSBruce Evans 2225097a20dSBruce Evans vput(nvp); 2235097a20dSBruce Evans *depp = NULL; 224952a6212SJordan K. Hubbard return (error); 2255097a20dSBruce Evans } 22627a0bc89SDoug Rabson DE_INTERNALIZE(ldep, direntptr); 22727a0bc89SDoug Rabson brelse(bp); 22827a0bc89SDoug Rabson } 22927a0bc89SDoug Rabson 23027a0bc89SDoug Rabson /* 23127a0bc89SDoug Rabson * Fill in a few fields of the vnode and finish filling in the 23227a0bc89SDoug Rabson * denode. Then return the address of the found denode. 23327a0bc89SDoug Rabson */ 23427a0bc89SDoug Rabson if (ldep->de_Attributes & ATTR_DIRECTORY) { 23527a0bc89SDoug Rabson /* 23627a0bc89SDoug Rabson * Since DOS directory entries that describe directories 23727a0bc89SDoug Rabson * have 0 in the filesize field, we take this opportunity 23827a0bc89SDoug Rabson * to find out the length of the directory and plug it into 23927a0bc89SDoug Rabson * the denode structure. 24027a0bc89SDoug Rabson */ 24127a0bc89SDoug Rabson u_long size; 24227a0bc89SDoug Rabson 243b5cdbc6dSTom Rhodes /* 244b5cdbc6dSTom Rhodes * XXX Sometimes, these arrives that . entry have cluster 245b5cdbc6dSTom Rhodes * number 0, when it shouldn't. Use real cluster number 246b5cdbc6dSTom Rhodes * instead of what is written in directory entry. 247b5cdbc6dSTom Rhodes */ 248b5cdbc6dSTom Rhodes if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) { 249b5cdbc6dSTom Rhodes printf("deget(): . entry at clust %ld != %ld\n", 250b5cdbc6dSTom Rhodes dirclust, ldep->de_StartCluster); 251b5cdbc6dSTom Rhodes ldep->de_StartCluster = dirclust; 252b5cdbc6dSTom Rhodes } 253b5cdbc6dSTom Rhodes 25427a0bc89SDoug Rabson nvp->v_type = VDIR; 255952a6212SJordan K. Hubbard if (ldep->de_StartCluster != MSDOSFSROOT) { 256952a6212SJordan K. Hubbard error = pcbmap(ldep, 0xffff, 0, &size, 0); 25727a0bc89SDoug Rabson if (error == E2BIG) { 258952a6212SJordan K. Hubbard ldep->de_FileSize = de_cn2off(pmp, size); 25927a0bc89SDoug Rabson error = 0; 26027a0bc89SDoug Rabson } else 26127a0bc89SDoug Rabson printf("deget(): pcbmap returned %d\n", error); 26227a0bc89SDoug Rabson } 26327a0bc89SDoug Rabson } else 26427a0bc89SDoug Rabson nvp->v_type = VREG; 2651affa3adSPoul-Henning Kamp ldep->de_modrev = init_va_filerev(); 26627a0bc89SDoug Rabson *depp = ldep; 267952a6212SJordan K. Hubbard return (0); 26827a0bc89SDoug Rabson } 26927a0bc89SDoug Rabson 27027a0bc89SDoug Rabson int 271952a6212SJordan K. Hubbard deupdat(dep, waitfor) 27227a0bc89SDoug Rabson struct denode *dep; 27327a0bc89SDoug Rabson int waitfor; 27427a0bc89SDoug Rabson { 27527a0bc89SDoug Rabson int error; 27627a0bc89SDoug Rabson struct buf *bp; 27727a0bc89SDoug Rabson struct direntry *dirp; 278952a6212SJordan K. Hubbard struct timespec ts; 27927a0bc89SDoug Rabson 280952a6212SJordan K. Hubbard if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 281952a6212SJordan K. Hubbard return (0); 282a0502b19SPoul-Henning Kamp getnanotime(&ts); 283952a6212SJordan K. Hubbard DETIMES(dep, &ts, &ts, &ts); 284952a6212SJordan K. Hubbard if ((dep->de_flag & DE_MODIFIED) == 0) 285952a6212SJordan K. Hubbard return (0); 286952a6212SJordan K. Hubbard dep->de_flag &= ~DE_MODIFIED; 287952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 288952a6212SJordan K. Hubbard return (0); 289952a6212SJordan K. Hubbard if (dep->de_refcnt <= 0) 290952a6212SJordan K. Hubbard return (0); 291c3c6d51eSPoul-Henning Kamp error = readde(dep, &bp, &dirp); 292c3c6d51eSPoul-Henning Kamp if (error) 293952a6212SJordan K. Hubbard return (error); 29427a0bc89SDoug Rabson DE_EXTERNALIZE(dirp, dep); 29527a0bc89SDoug Rabson if (waitfor) 296952a6212SJordan K. Hubbard return (bwrite(bp)); 297952a6212SJordan K. Hubbard else { 29827a0bc89SDoug Rabson bdwrite(bp); 299952a6212SJordan K. Hubbard return (0); 300952a6212SJordan K. Hubbard } 30127a0bc89SDoug Rabson } 30227a0bc89SDoug Rabson 30327a0bc89SDoug Rabson /* 30427a0bc89SDoug Rabson * Truncate the file described by dep to the length specified by length. 30527a0bc89SDoug Rabson */ 30627a0bc89SDoug Rabson int 307b40ce416SJulian Elischer detrunc(dep, length, flags, cred, td) 30827a0bc89SDoug Rabson struct denode *dep; 30927a0bc89SDoug Rabson u_long length; 31027a0bc89SDoug Rabson int flags; 31127a0bc89SDoug Rabson struct ucred *cred; 312b40ce416SJulian Elischer struct thread *td; 31327a0bc89SDoug Rabson { 31427a0bc89SDoug Rabson int error; 31527a0bc89SDoug Rabson int allerror; 31627a0bc89SDoug Rabson u_long eofentry; 31727a0bc89SDoug Rabson u_long chaintofree; 31827a0bc89SDoug Rabson daddr_t bn; 31927a0bc89SDoug Rabson int boff; 32027a0bc89SDoug Rabson int isadir = dep->de_Attributes & ATTR_DIRECTORY; 32127a0bc89SDoug Rabson struct buf *bp; 32227a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 32327a0bc89SDoug Rabson 32427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 325952a6212SJordan K. Hubbard printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 32627a0bc89SDoug Rabson #endif 32727a0bc89SDoug Rabson 32827a0bc89SDoug Rabson /* 32927a0bc89SDoug Rabson * Disallow attempts to truncate the root directory since it is of 33027a0bc89SDoug Rabson * fixed size. That's just the way dos filesystems are. We use 33127a0bc89SDoug Rabson * the VROOT bit in the vnode because checking for the directory 33227a0bc89SDoug Rabson * bit and a startcluster of 0 in the denode is not adequate to 33327a0bc89SDoug Rabson * recognize the root directory at this point in a file or 33427a0bc89SDoug Rabson * directory's life. 33527a0bc89SDoug Rabson */ 336e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) { 337952a6212SJordan K. Hubbard printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 33827a0bc89SDoug Rabson dep->de_dirclust, dep->de_diroffset); 339952a6212SJordan K. Hubbard return (EINVAL); 34027a0bc89SDoug Rabson } 34127a0bc89SDoug Rabson 34227a0bc89SDoug Rabson 343bd7e5f99SJohn Dyson if (dep->de_FileSize < length) { 344bd7e5f99SJohn Dyson vnode_pager_setsize(DETOV(dep), length); 34527a0bc89SDoug Rabson return deextend(dep, length, cred); 346bd7e5f99SJohn Dyson } 34727a0bc89SDoug Rabson 34827a0bc89SDoug Rabson /* 34927a0bc89SDoug Rabson * If the desired length is 0 then remember the starting cluster of 35027a0bc89SDoug Rabson * the file and set the StartCluster field in the directory entry 35127a0bc89SDoug Rabson * to 0. If the desired length is not zero, then get the number of 35227a0bc89SDoug Rabson * the last cluster in the shortened file. Then get the number of 35327a0bc89SDoug Rabson * the first cluster in the part of the file that is to be freed. 35427a0bc89SDoug Rabson * Then set the next cluster pointer in the last cluster of the 35527a0bc89SDoug Rabson * file to CLUST_EOFE. 35627a0bc89SDoug Rabson */ 35727a0bc89SDoug Rabson if (length == 0) { 35827a0bc89SDoug Rabson chaintofree = dep->de_StartCluster; 35927a0bc89SDoug Rabson dep->de_StartCluster = 0; 36027a0bc89SDoug Rabson eofentry = ~0; 36127a0bc89SDoug Rabson } else { 362952a6212SJordan K. Hubbard error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 363952a6212SJordan K. Hubbard &eofentry, 0); 364c3c6d51eSPoul-Henning Kamp if (error) { 36527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 36627a0bc89SDoug Rabson printf("detrunc(): pcbmap fails %d\n", error); 36727a0bc89SDoug Rabson #endif 368952a6212SJordan K. Hubbard return (error); 36927a0bc89SDoug Rabson } 37027a0bc89SDoug Rabson } 37127a0bc89SDoug Rabson 372952a6212SJordan K. Hubbard fc_purge(dep, de_clcount(pmp, length)); 37327a0bc89SDoug Rabson 37427a0bc89SDoug Rabson /* 37527a0bc89SDoug Rabson * If the new length is not a multiple of the cluster size then we 37627a0bc89SDoug Rabson * must zero the tail end of the new last cluster in case it 37727a0bc89SDoug Rabson * becomes part of the file again because of a seek. 37827a0bc89SDoug Rabson */ 37927a0bc89SDoug Rabson if ((boff = length & pmp->pm_crbomask) != 0) { 38027a0bc89SDoug Rabson if (isadir) { 38127a0bc89SDoug Rabson bn = cntobn(pmp, eofentry); 38227a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 38327a0bc89SDoug Rabson NOCRED, &bp); 38427a0bc89SDoug Rabson if (error) { 385952a6212SJordan K. Hubbard brelse(bp); 38627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 38727a0bc89SDoug Rabson printf("detrunc(): bread fails %d\n", error); 38827a0bc89SDoug Rabson #endif 389952a6212SJordan K. Hubbard return (error); 39027a0bc89SDoug Rabson } 39127a0bc89SDoug Rabson bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 39227a0bc89SDoug Rabson if (flags & IO_SYNC) 39327a0bc89SDoug Rabson bwrite(bp); 39427a0bc89SDoug Rabson else 39527a0bc89SDoug Rabson bdwrite(bp); 39627a0bc89SDoug Rabson } 397c2f95f66STom Rhodes } 39827a0bc89SDoug Rabson 39927a0bc89SDoug Rabson /* 40027a0bc89SDoug Rabson * Write out the updated directory entry. Even if the update fails 40127a0bc89SDoug Rabson * we free the trailing clusters. 40227a0bc89SDoug Rabson */ 40327a0bc89SDoug Rabson dep->de_FileSize = length; 404952a6212SJordan K. Hubbard if (!isadir) 405952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE|DE_MODIFIED; 406b40ce416SJulian Elischer allerror = vtruncbuf(DETOV(dep), cred, td, length, pmp->pm_bpcluster); 40744fdad99SPeter Wemm #ifdef MSDOSFS_DEBUG 40844fdad99SPeter Wemm if (allerror) 40944fdad99SPeter Wemm printf("detrunc(): vtruncbuf error %d\n", allerror); 41044fdad99SPeter Wemm #endif 41144fdad99SPeter Wemm error = deupdat(dep, 1); 41244fdad99SPeter Wemm if (error && (allerror == 0)) 41344fdad99SPeter Wemm allerror = error; 41427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 415952a6212SJordan K. Hubbard printf("detrunc(): allerror %d, eofentry %lu\n", 41627a0bc89SDoug Rabson allerror, eofentry); 41727a0bc89SDoug Rabson #endif 41827a0bc89SDoug Rabson 41927a0bc89SDoug Rabson /* 42027a0bc89SDoug Rabson * If we need to break the cluster chain for the file then do it 42127a0bc89SDoug Rabson * now. 42227a0bc89SDoug Rabson */ 42327a0bc89SDoug Rabson if (eofentry != ~0) { 42427a0bc89SDoug Rabson error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 42527a0bc89SDoug Rabson &chaintofree, CLUST_EOFE); 42627a0bc89SDoug Rabson if (error) { 42727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 42827a0bc89SDoug Rabson printf("detrunc(): fatentry errors %d\n", error); 42927a0bc89SDoug Rabson #endif 430952a6212SJordan K. Hubbard return (error); 43127a0bc89SDoug Rabson } 432952a6212SJordan K. Hubbard fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 43327a0bc89SDoug Rabson eofentry); 43427a0bc89SDoug Rabson } 43527a0bc89SDoug Rabson 43627a0bc89SDoug Rabson /* 43727a0bc89SDoug Rabson * Now free the clusters removed from the file because of the 43827a0bc89SDoug Rabson * truncation. 43927a0bc89SDoug Rabson */ 440952a6212SJordan K. Hubbard if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 44127a0bc89SDoug Rabson freeclusterchain(pmp, chaintofree); 44227a0bc89SDoug Rabson 443952a6212SJordan K. Hubbard return (allerror); 44427a0bc89SDoug Rabson } 44527a0bc89SDoug Rabson 44627a0bc89SDoug Rabson /* 44727a0bc89SDoug Rabson * Extend the file described by dep to length specified by length. 44827a0bc89SDoug Rabson */ 44927a0bc89SDoug Rabson int 45027a0bc89SDoug Rabson deextend(dep, length, cred) 45127a0bc89SDoug Rabson struct denode *dep; 452952a6212SJordan K. Hubbard u_long length; 45327a0bc89SDoug Rabson struct ucred *cred; 45427a0bc89SDoug Rabson { 45527a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 45627a0bc89SDoug Rabson u_long count; 45727a0bc89SDoug Rabson int error; 45827a0bc89SDoug Rabson 45927a0bc89SDoug Rabson /* 46027a0bc89SDoug Rabson * The root of a DOS filesystem cannot be extended. 46127a0bc89SDoug Rabson */ 462e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 463952a6212SJordan K. Hubbard return (EINVAL); 46427a0bc89SDoug Rabson 46527a0bc89SDoug Rabson /* 466952a6212SJordan K. Hubbard * Directories cannot be extended. 46727a0bc89SDoug Rabson */ 468952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 469952a6212SJordan K. Hubbard return (EISDIR); 47027a0bc89SDoug Rabson 47127a0bc89SDoug Rabson if (length <= dep->de_FileSize) 47227a0bc89SDoug Rabson panic("deextend: file too large"); 47327a0bc89SDoug Rabson 47427a0bc89SDoug Rabson /* 47527a0bc89SDoug Rabson * Compute the number of clusters to allocate. 47627a0bc89SDoug Rabson */ 47727a0bc89SDoug Rabson count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 47827a0bc89SDoug Rabson if (count > 0) { 47927a0bc89SDoug Rabson if (count > pmp->pm_freeclustercount) 480952a6212SJordan K. Hubbard return (ENOSPC); 481c3c6d51eSPoul-Henning Kamp error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 482c3c6d51eSPoul-Henning Kamp if (error) { 48327a0bc89SDoug Rabson /* truncate the added clusters away again */ 48427a0bc89SDoug Rabson (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 485952a6212SJordan K. Hubbard return (error); 48627a0bc89SDoug Rabson } 48727a0bc89SDoug Rabson } 48827a0bc89SDoug Rabson dep->de_FileSize = length; 489952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE|DE_MODIFIED; 490952a6212SJordan K. Hubbard return (deupdat(dep, 1)); 49127a0bc89SDoug Rabson } 49227a0bc89SDoug Rabson 49327a0bc89SDoug Rabson /* 49427a0bc89SDoug Rabson * Move a denode to its correct hash queue after the file it represents has 49527a0bc89SDoug Rabson * been moved to a new directory. 49627a0bc89SDoug Rabson */ 497952a6212SJordan K. Hubbard void 498952a6212SJordan K. Hubbard reinsert(dep) 49927a0bc89SDoug Rabson struct denode *dep; 50027a0bc89SDoug Rabson { 501a30fc63bSPoul-Henning Kamp struct vnode *vp; 502a30fc63bSPoul-Henning Kamp 50327a0bc89SDoug Rabson /* 50427a0bc89SDoug Rabson * Fix up the denode cache. If the denode is for a directory, 50527a0bc89SDoug Rabson * there is nothing to do since the hash is based on the starting 50627a0bc89SDoug Rabson * cluster of the directory file and that hasn't changed. If for a 50727a0bc89SDoug Rabson * file the hash is based on the location of the directory entry, 50827a0bc89SDoug Rabson * so we must remove it from the cache and re-enter it with the 50927a0bc89SDoug Rabson * hash based on the new location of the directory entry. 51027a0bc89SDoug Rabson */ 511952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 512952a6212SJordan K. Hubbard return; 513a30fc63bSPoul-Henning Kamp vp = DETOV(dep); 514a30fc63bSPoul-Henning Kamp vfs_hash_rehash(vp, DEHASH(dep->de_dirclust, dep->de_diroffset)); 51527a0bc89SDoug Rabson } 51627a0bc89SDoug Rabson 51727a0bc89SDoug Rabson int 51827a0bc89SDoug Rabson msdosfs_reclaim(ap) 51927a0bc89SDoug Rabson struct vop_reclaim_args /* { 52027a0bc89SDoug Rabson struct vnode *a_vp; 52127a0bc89SDoug Rabson } */ *ap; 52227a0bc89SDoug Rabson { 52327a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 52427a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 52527a0bc89SDoug Rabson 52627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 527f0707215SPoul-Henning Kamp printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 52827a0bc89SDoug Rabson dep, dep->de_Name, dep->de_refcnt); 52927a0bc89SDoug Rabson #endif 53027a0bc89SDoug Rabson 5314d93c0beSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 53227a0bc89SDoug Rabson vprint("msdosfs_reclaim(): pushing active", vp); 53327a0bc89SDoug Rabson /* 534952a6212SJordan K. Hubbard * Remove the denode from its hash chain. 53527a0bc89SDoug Rabson */ 536a30fc63bSPoul-Henning Kamp vfs_hash_remove(vp); 53727a0bc89SDoug Rabson /* 538952a6212SJordan K. Hubbard * Purge old data structures associated with the denode. 53927a0bc89SDoug Rabson */ 540952a6212SJordan K. Hubbard #if 0 /* XXX */ 54127a0bc89SDoug Rabson dep->de_flag = 0; 542952a6212SJordan K. Hubbard #endif 54327a0bc89SDoug Rabson FREE(dep, M_MSDOSFSNODE); 54427a0bc89SDoug Rabson vp->v_data = NULL; 545a369f34dSPoul-Henning Kamp vnode_destroy_vobject(vp); 54627a0bc89SDoug Rabson 547952a6212SJordan K. Hubbard return (0); 54827a0bc89SDoug Rabson } 54927a0bc89SDoug Rabson 55027a0bc89SDoug Rabson int 55127a0bc89SDoug Rabson msdosfs_inactive(ap) 55227a0bc89SDoug Rabson struct vop_inactive_args /* { 55327a0bc89SDoug Rabson struct vnode *a_vp; 554b40ce416SJulian Elischer struct thread *a_td; 55527a0bc89SDoug Rabson } */ *ap; 55627a0bc89SDoug Rabson { 55727a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 55827a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 559b40ce416SJulian Elischer struct thread *td = ap->a_td; 56027a0bc89SDoug Rabson int error = 0; 56127a0bc89SDoug Rabson 56227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 563f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 56427a0bc89SDoug Rabson #endif 56527a0bc89SDoug Rabson 5664d93c0beSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 56727a0bc89SDoug Rabson vprint("msdosfs_inactive(): pushing active", vp); 56827a0bc89SDoug Rabson 56927a0bc89SDoug Rabson /* 570952a6212SJordan K. Hubbard * Ignore denodes related to stale file handles. 57127a0bc89SDoug Rabson */ 572af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 573af3f60d5SBruce Evans goto out; 57427a0bc89SDoug Rabson 57527a0bc89SDoug Rabson /* 57627a0bc89SDoug Rabson * If the file has been deleted and it is on a read/write 57727a0bc89SDoug Rabson * filesystem, then truncate the file, and mark the directory slot 57827a0bc89SDoug Rabson * as empty. (This may not be necessary for the dos filesystem.) 57927a0bc89SDoug Rabson */ 58027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 581f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", 58227a0bc89SDoug Rabson dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 58327a0bc89SDoug Rabson #endif 58427a0bc89SDoug Rabson if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 585b40ce416SJulian Elischer error = detrunc(dep, (u_long) 0, 0, NOCRED, td); 58627a0bc89SDoug Rabson dep->de_flag |= DE_UPDATE; 58727a0bc89SDoug Rabson dep->de_Name[0] = SLOT_DELETED; 58827a0bc89SDoug Rabson } 589952a6212SJordan K. Hubbard deupdat(dep, 0); 590952a6212SJordan K. Hubbard 591af3f60d5SBruce Evans out: 59227a0bc89SDoug Rabson /* 593952a6212SJordan K. Hubbard * If we are done with the denode, reclaim it 594952a6212SJordan K. Hubbard * so that it can be reused immediately. 59527a0bc89SDoug Rabson */ 59627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 5974d93c0beSJeff Roberson printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 5984d93c0beSJeff Roberson vrefcnt(vp), dep->de_Name[0]); 59927a0bc89SDoug Rabson #endif 600af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 601d4eb29baSPoul-Henning Kamp vrecycle(vp, td); 602952a6212SJordan K. Hubbard return (error); 60327a0bc89SDoug Rabson } 604