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> 5327a0bc89SDoug Rabson #include <sys/buf.h> 543c960d93SPoul-Henning Kamp #include <sys/clock.h> 55a878a31cSBruce Evans #include <sys/kernel.h> 56a878a31cSBruce Evans #include <sys/malloc.h> 57a878a31cSBruce Evans #include <sys/mount.h> 5827a0bc89SDoug Rabson #include <sys/vnode.h> 5927a0bc89SDoug Rabson 60c3c6d51eSPoul-Henning Kamp #include <vm/vm.h> 61efeaf95aSDavid Greenman #include <vm/vm_extern.h> 62c3c6d51eSPoul-Henning Kamp 631166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h> 641166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h> 651166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h> 661166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h> 67a878a31cSBruce Evans #include <fs/msdosfs/msdosfsmount.h> 6827a0bc89SDoug Rabson 695bb84bc8SRobert Watson static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part"); 7055166637SPoul-Henning Kamp 71f4b423aeSPoul-Henning Kamp static int 72f4b423aeSPoul-Henning Kamp de_vncmpf(struct vnode *vp, void *arg) 73f4b423aeSPoul-Henning Kamp { 74f4b423aeSPoul-Henning Kamp struct denode *de; 75f4b423aeSPoul-Henning Kamp uint64_t *a; 76f4b423aeSPoul-Henning Kamp 77f4b423aeSPoul-Henning Kamp a = arg; 78f4b423aeSPoul-Henning Kamp de = VTODE(vp); 79f4b423aeSPoul-Henning Kamp return (de->de_inode != *a); 80f4b423aeSPoul-Henning Kamp } 8127a0bc89SDoug Rabson 8227a0bc89SDoug Rabson /* 8327a0bc89SDoug Rabson * If deget() succeeds it returns with the gotten denode locked(). 8427a0bc89SDoug Rabson * 8527a0bc89SDoug Rabson * pmp - address of msdosfsmount structure of the filesystem containing 863b97f388SPoul-Henning Kamp * the denode of interest. The address of 8727a0bc89SDoug Rabson * the msdosfsmount structure are used. 8827a0bc89SDoug Rabson * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 8927a0bc89SDoug Rabson * diroffset is relative to the beginning of the root directory, 9027a0bc89SDoug Rabson * otherwise it is cluster relative. 9127a0bc89SDoug Rabson * diroffset - offset past begin of cluster of denode we want 9227a0bc89SDoug Rabson * depp - returns the address of the gotten denode. 9327a0bc89SDoug Rabson */ 9427a0bc89SDoug Rabson int 95952a6212SJordan K. Hubbard deget(pmp, dirclust, diroffset, depp) 9627a0bc89SDoug Rabson struct msdosfsmount *pmp; /* so we know the maj/min number */ 9727a0bc89SDoug Rabson u_long dirclust; /* cluster this dir entry came from */ 9827a0bc89SDoug Rabson u_long diroffset; /* index of entry within the cluster */ 9927a0bc89SDoug Rabson struct denode **depp; /* returns the addr of the gotten denode */ 10027a0bc89SDoug Rabson { 10127a0bc89SDoug Rabson int error; 102f4b423aeSPoul-Henning Kamp uint64_t inode; 10327a0bc89SDoug Rabson struct mount *mntp = pmp->pm_mountp; 104952a6212SJordan K. Hubbard struct direntry *direntptr; 10527a0bc89SDoug Rabson struct denode *ldep; 106a30fc63bSPoul-Henning Kamp struct vnode *nvp, *xvp; 10727a0bc89SDoug Rabson struct buf *bp; 10827a0bc89SDoug Rabson 10927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 110952a6212SJordan K. Hubbard printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 111952a6212SJordan K. Hubbard pmp, dirclust, diroffset, depp); 11227a0bc89SDoug Rabson #endif 11327a0bc89SDoug Rabson 11427a0bc89SDoug Rabson /* 115952a6212SJordan K. Hubbard * On FAT32 filesystems, root is a (more or less) normal 116952a6212SJordan K. Hubbard * directory 11727a0bc89SDoug Rabson */ 118952a6212SJordan K. Hubbard if (FAT32(pmp) && dirclust == MSDOSFSROOT) 119952a6212SJordan K. Hubbard dirclust = pmp->pm_rootdirblk; 12027a0bc89SDoug Rabson 12127a0bc89SDoug Rabson /* 12227a0bc89SDoug Rabson * See if the denode is in the denode cache. Use the location of 12327a0bc89SDoug Rabson * the directory entry to compute the hash value. For subdir use 124952a6212SJordan K. Hubbard * address of "." entry. For root dir (if not FAT32) use cluster 125952a6212SJordan K. Hubbard * MSDOSFSROOT, offset MSDOSFSROOT_OFS 12627a0bc89SDoug Rabson * 12727a0bc89SDoug Rabson * NOTE: The check for de_refcnt > 0 below insures the denode being 12827a0bc89SDoug Rabson * examined does not represent an unlinked but still open file. 12927a0bc89SDoug Rabson * These files are not to be accessible even when the directory 13027a0bc89SDoug Rabson * entry that represented the file happens to be reused while the 13127a0bc89SDoug Rabson * deleted file is still open. 13227a0bc89SDoug Rabson */ 1335ddf2985SDavid E. O'Brien inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 134f4b423aeSPoul-Henning Kamp 135f4b423aeSPoul-Henning Kamp error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp, 136f4b423aeSPoul-Henning Kamp de_vncmpf, &inode); 137a30fc63bSPoul-Henning Kamp if (error) 138a30fc63bSPoul-Henning Kamp return (error); 139a30fc63bSPoul-Henning Kamp if (nvp != NULL) { 140a30fc63bSPoul-Henning Kamp *depp = VTODE(nvp); 141f4b423aeSPoul-Henning Kamp KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust")); 142f4b423aeSPoul-Henning Kamp KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset")); 143952a6212SJordan K. Hubbard return (0); 14427a0bc89SDoug Rabson } 14527a0bc89SDoug Rabson 1462f9bae59SDavid Greenman /* 1478b36e813SKonstantin Belousov * Do the malloc before the getnewvnode since doing so afterward 1482f9bae59SDavid Greenman * might cause a bogus v_data pointer to get dereferenced 1492f9bae59SDavid Greenman * elsewhere if MALLOC should block. 1502f9bae59SDavid Greenman */ 1518b36e813SKonstantin Belousov ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO); 15227a0bc89SDoug Rabson 15327a0bc89SDoug Rabson /* 15427a0bc89SDoug Rabson * Directory entry was not in cache, have to create a vnode and 15527a0bc89SDoug Rabson * copy it from the passed disk buffer. 15627a0bc89SDoug Rabson */ 15727a0bc89SDoug Rabson /* getnewvnode() does a VREF() on the vnode */ 158aec0fb7bSPoul-Henning Kamp error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp); 159c3c6d51eSPoul-Henning Kamp if (error) { 1602f9bae59SDavid Greenman *depp = NULL; 1611ede983cSDag-Erling Smørgrav free(ldep, M_MSDOSFSNODE); 16227a0bc89SDoug Rabson return error; 16327a0bc89SDoug Rabson } 16427a0bc89SDoug Rabson nvp->v_data = ldep; 16527a0bc89SDoug Rabson ldep->de_vnode = nvp; 16627a0bc89SDoug Rabson ldep->de_flag = 0; 16727a0bc89SDoug Rabson ldep->de_dirclust = dirclust; 16827a0bc89SDoug Rabson ldep->de_diroffset = diroffset; 169f4b423aeSPoul-Henning Kamp ldep->de_inode = inode; 17027a0bc89SDoug Rabson fc_purge(ldep, 0); /* init the fat cache for this denode */ 17127a0bc89SDoug Rabson 1720e9eb108SAttilio Rao lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL); 17361b9d89fSTor Egge error = insmntque(nvp, mntp); 17461b9d89fSTor Egge if (error != 0) { 1751ede983cSDag-Erling Smørgrav free(ldep, M_MSDOSFSNODE); 17661b9d89fSTor Egge *depp = NULL; 17761b9d89fSTor Egge return (error); 17861b9d89fSTor Egge } 1790e9eb108SAttilio Rao error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp, 180f4b423aeSPoul-Henning Kamp de_vncmpf, &inode); 181a30fc63bSPoul-Henning Kamp if (error) { 182a30fc63bSPoul-Henning Kamp *depp = NULL; 183a30fc63bSPoul-Henning Kamp return (error); 184a30fc63bSPoul-Henning Kamp } 185a30fc63bSPoul-Henning Kamp if (xvp != NULL) { 186a30fc63bSPoul-Henning Kamp /* XXX: Not sure this is right */ 187a30fc63bSPoul-Henning Kamp nvp = xvp; 188a30fc63bSPoul-Henning Kamp ldep->de_vnode = nvp; 189a30fc63bSPoul-Henning Kamp } 19027a0bc89SDoug Rabson 191952a6212SJordan K. Hubbard ldep->de_pmp = pmp; 192952a6212SJordan K. Hubbard ldep->de_refcnt = 1; 19327a0bc89SDoug Rabson /* 19427a0bc89SDoug Rabson * Copy the directory entry into the denode area of the vnode. 19527a0bc89SDoug Rabson */ 196952a6212SJordan K. Hubbard if ((dirclust == MSDOSFSROOT 197952a6212SJordan K. Hubbard || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 198952a6212SJordan K. Hubbard && diroffset == MSDOSFSROOT_OFS) { 19927a0bc89SDoug Rabson /* 20027a0bc89SDoug Rabson * Directory entry for the root directory. There isn't one, 20127a0bc89SDoug Rabson * so we manufacture one. We should probably rummage 20227a0bc89SDoug Rabson * through the root directory and find a label entry (if it 20327a0bc89SDoug Rabson * exists), and then use the time and date from that entry 20427a0bc89SDoug Rabson * as the time and date for the root denode. 20527a0bc89SDoug Rabson */ 206e6e370a7SJeff Roberson nvp->v_vflag |= VV_ROOT; /* should be further down XXX */ 207952a6212SJordan K. Hubbard 20827a0bc89SDoug Rabson ldep->de_Attributes = ATTR_DIRECTORY; 209bad3d41dSDmitrij Tejblum ldep->de_LowerCase = 0; 210952a6212SJordan K. Hubbard if (FAT32(pmp)) 211952a6212SJordan K. Hubbard ldep->de_StartCluster = pmp->pm_rootdirblk; 212952a6212SJordan K. Hubbard /* de_FileSize will be filled in further down */ 213952a6212SJordan K. Hubbard else { 21427a0bc89SDoug Rabson ldep->de_StartCluster = MSDOSFSROOT; 21501f6cfbaSYoshihiro Takahashi ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 216952a6212SJordan K. Hubbard } 21727a0bc89SDoug Rabson /* 2183c960d93SPoul-Henning Kamp * fill in time and date so that fattime2timespec() doesn't 21927a0bc89SDoug Rabson * spit up when called from msdosfs_getattr() with root 22027a0bc89SDoug Rabson * denode 22127a0bc89SDoug Rabson */ 222952a6212SJordan K. Hubbard ldep->de_CHun = 0; 223952a6212SJordan K. Hubbard ldep->de_CTime = 0x0000; /* 00:00:00 */ 224952a6212SJordan K. Hubbard ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 22527a0bc89SDoug Rabson | (1 << DD_DAY_SHIFT); 22627a0bc89SDoug Rabson /* Jan 1, 1980 */ 227952a6212SJordan K. Hubbard ldep->de_ADate = ldep->de_CDate; 228952a6212SJordan K. Hubbard ldep->de_MTime = ldep->de_CTime; 229952a6212SJordan K. Hubbard ldep->de_MDate = ldep->de_CDate; 23027a0bc89SDoug Rabson /* leave the other fields as garbage */ 23127a0bc89SDoug Rabson } else { 232952a6212SJordan K. Hubbard error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 2335097a20dSBruce Evans if (error) { 2345097a20dSBruce Evans /* 2355097a20dSBruce Evans * The denode does not contain anything useful, so 2365097a20dSBruce Evans * it would be wrong to leave it on its hash chain. 2375097a20dSBruce Evans * Arrange for vput() to just forget about it. 2385097a20dSBruce Evans */ 2395097a20dSBruce Evans ldep->de_Name[0] = SLOT_DELETED; 2405097a20dSBruce Evans 2415097a20dSBruce Evans vput(nvp); 2425097a20dSBruce Evans *depp = NULL; 243952a6212SJordan K. Hubbard return (error); 2445097a20dSBruce Evans } 24527a0bc89SDoug Rabson DE_INTERNALIZE(ldep, direntptr); 24627a0bc89SDoug Rabson brelse(bp); 24727a0bc89SDoug Rabson } 24827a0bc89SDoug Rabson 24927a0bc89SDoug Rabson /* 25027a0bc89SDoug Rabson * Fill in a few fields of the vnode and finish filling in the 25127a0bc89SDoug Rabson * denode. Then return the address of the found denode. 25227a0bc89SDoug Rabson */ 25327a0bc89SDoug Rabson if (ldep->de_Attributes & ATTR_DIRECTORY) { 25427a0bc89SDoug Rabson /* 25527a0bc89SDoug Rabson * Since DOS directory entries that describe directories 25627a0bc89SDoug Rabson * have 0 in the filesize field, we take this opportunity 25727a0bc89SDoug Rabson * to find out the length of the directory and plug it into 25827a0bc89SDoug Rabson * the denode structure. 25927a0bc89SDoug Rabson */ 26027a0bc89SDoug Rabson u_long size; 26127a0bc89SDoug Rabson 262b5cdbc6dSTom Rhodes /* 263e3117f85SBruce Evans * XXX it sometimes happens that the "." entry has cluster 264e3117f85SBruce Evans * number 0 when it shouldn't. Use the actual cluster number 265b5cdbc6dSTom Rhodes * instead of what is written in directory entry. 266b5cdbc6dSTom Rhodes */ 267e3117f85SBruce Evans if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 268e3117f85SBruce Evans printf("deget(): \".\" entry at clust %lu != %lu\n", 269b5cdbc6dSTom Rhodes dirclust, ldep->de_StartCluster); 270b5cdbc6dSTom Rhodes ldep->de_StartCluster = dirclust; 271b5cdbc6dSTom Rhodes } 272b5cdbc6dSTom Rhodes 27327a0bc89SDoug Rabson nvp->v_type = VDIR; 274952a6212SJordan K. Hubbard if (ldep->de_StartCluster != MSDOSFSROOT) { 275952a6212SJordan K. Hubbard error = pcbmap(ldep, 0xffff, 0, &size, 0); 27627a0bc89SDoug Rabson if (error == E2BIG) { 277952a6212SJordan K. Hubbard ldep->de_FileSize = de_cn2off(pmp, size); 27827a0bc89SDoug Rabson error = 0; 27927a0bc89SDoug Rabson } else 28027a0bc89SDoug Rabson printf("deget(): pcbmap returned %d\n", error); 28127a0bc89SDoug Rabson } 28227a0bc89SDoug Rabson } else 28327a0bc89SDoug Rabson nvp->v_type = VREG; 2841affa3adSPoul-Henning Kamp ldep->de_modrev = init_va_filerev(); 28527a0bc89SDoug Rabson *depp = ldep; 286952a6212SJordan K. Hubbard return (0); 28727a0bc89SDoug Rabson } 28827a0bc89SDoug Rabson 28927a0bc89SDoug Rabson int 290952a6212SJordan K. Hubbard deupdat(dep, waitfor) 29127a0bc89SDoug Rabson struct denode *dep; 29227a0bc89SDoug Rabson int waitfor; 29327a0bc89SDoug Rabson { 29427a0bc89SDoug Rabson int error; 29527a0bc89SDoug Rabson struct buf *bp; 29627a0bc89SDoug Rabson struct direntry *dirp; 297952a6212SJordan K. Hubbard struct timespec ts; 29827a0bc89SDoug Rabson 299952a6212SJordan K. Hubbard if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 300952a6212SJordan K. Hubbard return (0); 301a0502b19SPoul-Henning Kamp getnanotime(&ts); 302952a6212SJordan K. Hubbard DETIMES(dep, &ts, &ts, &ts); 303952a6212SJordan K. Hubbard if ((dep->de_flag & DE_MODIFIED) == 0) 304952a6212SJordan K. Hubbard return (0); 305952a6212SJordan K. Hubbard dep->de_flag &= ~DE_MODIFIED; 306952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 307952a6212SJordan K. Hubbard return (0); 308952a6212SJordan K. Hubbard if (dep->de_refcnt <= 0) 309952a6212SJordan K. Hubbard return (0); 310c3c6d51eSPoul-Henning Kamp error = readde(dep, &bp, &dirp); 311c3c6d51eSPoul-Henning Kamp if (error) 312952a6212SJordan K. Hubbard return (error); 31327a0bc89SDoug Rabson DE_EXTERNALIZE(dirp, dep); 31427a0bc89SDoug Rabson if (waitfor) 315952a6212SJordan K. Hubbard return (bwrite(bp)); 316952a6212SJordan K. Hubbard else { 31727a0bc89SDoug Rabson bdwrite(bp); 318952a6212SJordan K. Hubbard return (0); 319952a6212SJordan K. Hubbard } 32027a0bc89SDoug Rabson } 32127a0bc89SDoug Rabson 32227a0bc89SDoug Rabson /* 32327a0bc89SDoug Rabson * Truncate the file described by dep to the length specified by length. 32427a0bc89SDoug Rabson */ 32527a0bc89SDoug Rabson int 326b40ce416SJulian Elischer detrunc(dep, length, flags, cred, td) 32727a0bc89SDoug Rabson struct denode *dep; 32827a0bc89SDoug Rabson u_long length; 32927a0bc89SDoug Rabson int flags; 33027a0bc89SDoug Rabson struct ucred *cred; 331b40ce416SJulian Elischer struct thread *td; 33227a0bc89SDoug Rabson { 33327a0bc89SDoug Rabson int error; 33427a0bc89SDoug Rabson int allerror; 33527a0bc89SDoug Rabson u_long eofentry; 33627a0bc89SDoug Rabson u_long chaintofree; 33727a0bc89SDoug Rabson daddr_t bn; 33827a0bc89SDoug Rabson int boff; 33927a0bc89SDoug Rabson int isadir = dep->de_Attributes & ATTR_DIRECTORY; 34027a0bc89SDoug Rabson struct buf *bp; 34127a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 34227a0bc89SDoug Rabson 34327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 344952a6212SJordan K. Hubbard printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 34527a0bc89SDoug Rabson #endif 34627a0bc89SDoug Rabson 34727a0bc89SDoug Rabson /* 34827a0bc89SDoug Rabson * Disallow attempts to truncate the root directory since it is of 34927a0bc89SDoug Rabson * fixed size. That's just the way dos filesystems are. We use 35027a0bc89SDoug Rabson * the VROOT bit in the vnode because checking for the directory 35127a0bc89SDoug Rabson * bit and a startcluster of 0 in the denode is not adequate to 35227a0bc89SDoug Rabson * recognize the root directory at this point in a file or 35327a0bc89SDoug Rabson * directory's life. 35427a0bc89SDoug Rabson */ 355e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) { 356952a6212SJordan K. Hubbard printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 35727a0bc89SDoug Rabson dep->de_dirclust, dep->de_diroffset); 358952a6212SJordan K. Hubbard return (EINVAL); 35927a0bc89SDoug Rabson } 36027a0bc89SDoug Rabson 361bd7e5f99SJohn Dyson if (dep->de_FileSize < length) { 362bd7e5f99SJohn Dyson vnode_pager_setsize(DETOV(dep), length); 36327a0bc89SDoug Rabson return deextend(dep, length, cred); 364bd7e5f99SJohn Dyson } 36527a0bc89SDoug Rabson 36627a0bc89SDoug Rabson /* 36727a0bc89SDoug Rabson * If the desired length is 0 then remember the starting cluster of 36827a0bc89SDoug Rabson * the file and set the StartCluster field in the directory entry 36927a0bc89SDoug Rabson * to 0. If the desired length is not zero, then get the number of 37027a0bc89SDoug Rabson * the last cluster in the shortened file. Then get the number of 37127a0bc89SDoug Rabson * the first cluster in the part of the file that is to be freed. 37227a0bc89SDoug Rabson * Then set the next cluster pointer in the last cluster of the 37327a0bc89SDoug Rabson * file to CLUST_EOFE. 37427a0bc89SDoug Rabson */ 37527a0bc89SDoug Rabson if (length == 0) { 37627a0bc89SDoug Rabson chaintofree = dep->de_StartCluster; 37727a0bc89SDoug Rabson dep->de_StartCluster = 0; 37827a0bc89SDoug Rabson eofentry = ~0; 37927a0bc89SDoug Rabson } else { 380952a6212SJordan K. Hubbard error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 381952a6212SJordan K. Hubbard &eofentry, 0); 382c3c6d51eSPoul-Henning Kamp if (error) { 38327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 38427a0bc89SDoug Rabson printf("detrunc(): pcbmap fails %d\n", error); 38527a0bc89SDoug Rabson #endif 386952a6212SJordan K. Hubbard return (error); 38727a0bc89SDoug Rabson } 38827a0bc89SDoug Rabson } 38927a0bc89SDoug Rabson 390952a6212SJordan K. Hubbard fc_purge(dep, de_clcount(pmp, length)); 39127a0bc89SDoug Rabson 39227a0bc89SDoug Rabson /* 39327a0bc89SDoug Rabson * If the new length is not a multiple of the cluster size then we 39427a0bc89SDoug Rabson * must zero the tail end of the new last cluster in case it 39527a0bc89SDoug Rabson * becomes part of the file again because of a seek. 39627a0bc89SDoug Rabson */ 39727a0bc89SDoug Rabson if ((boff = length & pmp->pm_crbomask) != 0) { 39827a0bc89SDoug Rabson if (isadir) { 39927a0bc89SDoug Rabson bn = cntobn(pmp, eofentry); 40027a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 40127a0bc89SDoug Rabson NOCRED, &bp); 40227a0bc89SDoug Rabson if (error) { 403952a6212SJordan K. Hubbard brelse(bp); 40427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 40527a0bc89SDoug Rabson printf("detrunc(): bread fails %d\n", error); 40627a0bc89SDoug Rabson #endif 407952a6212SJordan K. Hubbard return (error); 40827a0bc89SDoug Rabson } 40927a0bc89SDoug Rabson bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 41027a0bc89SDoug Rabson if (flags & IO_SYNC) 41127a0bc89SDoug Rabson bwrite(bp); 41227a0bc89SDoug Rabson else 41327a0bc89SDoug Rabson bdwrite(bp); 41427a0bc89SDoug Rabson } 415c2f95f66STom Rhodes } 41627a0bc89SDoug Rabson 41727a0bc89SDoug Rabson /* 41827a0bc89SDoug Rabson * Write out the updated directory entry. Even if the update fails 41927a0bc89SDoug Rabson * we free the trailing clusters. 42027a0bc89SDoug Rabson */ 42127a0bc89SDoug Rabson dep->de_FileSize = length; 422952a6212SJordan K. Hubbard if (!isadir) 423952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE | DE_MODIFIED; 424b40ce416SJulian Elischer allerror = vtruncbuf(DETOV(dep), cred, td, length, pmp->pm_bpcluster); 42544fdad99SPeter Wemm #ifdef MSDOSFS_DEBUG 42644fdad99SPeter Wemm if (allerror) 42744fdad99SPeter Wemm printf("detrunc(): vtruncbuf error %d\n", allerror); 42844fdad99SPeter Wemm #endif 429cb65c1eeSBruce Evans error = deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC)); 430e3117f85SBruce Evans if (error != 0 && allerror == 0) 43144fdad99SPeter Wemm allerror = error; 43227a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 433952a6212SJordan K. Hubbard printf("detrunc(): allerror %d, eofentry %lu\n", 43427a0bc89SDoug Rabson allerror, eofentry); 43527a0bc89SDoug Rabson #endif 43627a0bc89SDoug Rabson 43727a0bc89SDoug Rabson /* 43827a0bc89SDoug Rabson * If we need to break the cluster chain for the file then do it 43927a0bc89SDoug Rabson * now. 44027a0bc89SDoug Rabson */ 44127a0bc89SDoug Rabson if (eofentry != ~0) { 44227a0bc89SDoug Rabson error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 44327a0bc89SDoug Rabson &chaintofree, CLUST_EOFE); 44427a0bc89SDoug Rabson if (error) { 44527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 44627a0bc89SDoug Rabson printf("detrunc(): fatentry errors %d\n", error); 44727a0bc89SDoug Rabson #endif 448952a6212SJordan K. Hubbard return (error); 44927a0bc89SDoug Rabson } 450952a6212SJordan K. Hubbard fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 45127a0bc89SDoug Rabson eofentry); 45227a0bc89SDoug Rabson } 45327a0bc89SDoug Rabson 45427a0bc89SDoug Rabson /* 45527a0bc89SDoug Rabson * Now free the clusters removed from the file because of the 45627a0bc89SDoug Rabson * truncation. 45727a0bc89SDoug Rabson */ 458952a6212SJordan K. Hubbard if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 45927a0bc89SDoug Rabson freeclusterchain(pmp, chaintofree); 46027a0bc89SDoug Rabson 461952a6212SJordan K. Hubbard return (allerror); 46227a0bc89SDoug Rabson } 46327a0bc89SDoug Rabson 46427a0bc89SDoug Rabson /* 46527a0bc89SDoug Rabson * Extend the file described by dep to length specified by length. 46627a0bc89SDoug Rabson */ 46727a0bc89SDoug Rabson int 46827a0bc89SDoug Rabson deextend(dep, length, cred) 46927a0bc89SDoug Rabson struct denode *dep; 470952a6212SJordan K. Hubbard u_long length; 47127a0bc89SDoug Rabson struct ucred *cred; 47227a0bc89SDoug Rabson { 47327a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 47427a0bc89SDoug Rabson u_long count; 47527a0bc89SDoug Rabson int error; 47627a0bc89SDoug Rabson 47727a0bc89SDoug Rabson /* 47827a0bc89SDoug Rabson * The root of a DOS filesystem cannot be extended. 47927a0bc89SDoug Rabson */ 480e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 481952a6212SJordan K. Hubbard return (EINVAL); 48227a0bc89SDoug Rabson 48327a0bc89SDoug Rabson /* 484952a6212SJordan K. Hubbard * Directories cannot be extended. 48527a0bc89SDoug Rabson */ 486952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 487952a6212SJordan K. Hubbard return (EISDIR); 48827a0bc89SDoug Rabson 48927a0bc89SDoug Rabson if (length <= dep->de_FileSize) 49027a0bc89SDoug Rabson panic("deextend: file too large"); 49127a0bc89SDoug Rabson 49227a0bc89SDoug Rabson /* 49327a0bc89SDoug Rabson * Compute the number of clusters to allocate. 49427a0bc89SDoug Rabson */ 49527a0bc89SDoug Rabson count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 49627a0bc89SDoug Rabson if (count > 0) { 49727a0bc89SDoug Rabson if (count > pmp->pm_freeclustercount) 498952a6212SJordan K. Hubbard return (ENOSPC); 499c3c6d51eSPoul-Henning Kamp error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 500c3c6d51eSPoul-Henning Kamp if (error) { 50127a0bc89SDoug Rabson /* truncate the added clusters away again */ 50227a0bc89SDoug Rabson (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 503952a6212SJordan K. Hubbard return (error); 50427a0bc89SDoug Rabson } 50527a0bc89SDoug Rabson } 50627a0bc89SDoug Rabson dep->de_FileSize = length; 507952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE | DE_MODIFIED; 508cb65c1eeSBruce Evans return (deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC))); 50927a0bc89SDoug Rabson } 51027a0bc89SDoug Rabson 51127a0bc89SDoug Rabson /* 51227a0bc89SDoug Rabson * Move a denode to its correct hash queue after the file it represents has 51327a0bc89SDoug Rabson * been moved to a new directory. 51427a0bc89SDoug Rabson */ 515952a6212SJordan K. Hubbard void 516952a6212SJordan K. Hubbard reinsert(dep) 51727a0bc89SDoug Rabson struct denode *dep; 51827a0bc89SDoug Rabson { 519a30fc63bSPoul-Henning Kamp struct vnode *vp; 520a30fc63bSPoul-Henning Kamp 52127a0bc89SDoug Rabson /* 52227a0bc89SDoug Rabson * Fix up the denode cache. If the denode is for a directory, 52327a0bc89SDoug Rabson * there is nothing to do since the hash is based on the starting 52427a0bc89SDoug Rabson * cluster of the directory file and that hasn't changed. If for a 52527a0bc89SDoug Rabson * file the hash is based on the location of the directory entry, 52627a0bc89SDoug Rabson * so we must remove it from the cache and re-enter it with the 52727a0bc89SDoug Rabson * hash based on the new location of the directory entry. 52827a0bc89SDoug Rabson */ 529f4b423aeSPoul-Henning Kamp #if 0 530952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 531952a6212SJordan K. Hubbard return; 532f4b423aeSPoul-Henning Kamp #endif 533a30fc63bSPoul-Henning Kamp vp = DETOV(dep); 5345ddf2985SDavid E. O'Brien dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust + 535f4b423aeSPoul-Henning Kamp dep->de_diroffset; 536f4b423aeSPoul-Henning Kamp vfs_hash_rehash(vp, dep->de_inode); 53727a0bc89SDoug Rabson } 53827a0bc89SDoug Rabson 53927a0bc89SDoug Rabson int 54027a0bc89SDoug Rabson msdosfs_reclaim(ap) 54127a0bc89SDoug Rabson struct vop_reclaim_args /* { 54227a0bc89SDoug Rabson struct vnode *a_vp; 54327a0bc89SDoug Rabson } */ *ap; 54427a0bc89SDoug Rabson { 54527a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 54627a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 54727a0bc89SDoug Rabson 54827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 549f0707215SPoul-Henning Kamp printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 55027a0bc89SDoug Rabson dep, dep->de_Name, dep->de_refcnt); 55127a0bc89SDoug Rabson #endif 55227a0bc89SDoug Rabson 5534d93c0beSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 55427a0bc89SDoug Rabson vprint("msdosfs_reclaim(): pushing active", vp); 55527a0bc89SDoug Rabson /* 55692e73f57SAlfred Perlstein * Destroy the vm object and flush associated pages. 55792e73f57SAlfred Perlstein */ 55892e73f57SAlfred Perlstein vnode_destroy_vobject(vp); 55992e73f57SAlfred Perlstein /* 560952a6212SJordan K. Hubbard * Remove the denode from its hash chain. 56127a0bc89SDoug Rabson */ 562a30fc63bSPoul-Henning Kamp vfs_hash_remove(vp); 56327a0bc89SDoug Rabson /* 564952a6212SJordan K. Hubbard * Purge old data structures associated with the denode. 56527a0bc89SDoug Rabson */ 566952a6212SJordan K. Hubbard #if 0 /* XXX */ 56727a0bc89SDoug Rabson dep->de_flag = 0; 568952a6212SJordan K. Hubbard #endif 5691ede983cSDag-Erling Smørgrav free(dep, M_MSDOSFSNODE); 57027a0bc89SDoug Rabson vp->v_data = NULL; 57127a0bc89SDoug Rabson 572952a6212SJordan K. Hubbard return (0); 57327a0bc89SDoug Rabson } 57427a0bc89SDoug Rabson 57527a0bc89SDoug Rabson int 57627a0bc89SDoug Rabson msdosfs_inactive(ap) 57727a0bc89SDoug Rabson struct vop_inactive_args /* { 57827a0bc89SDoug Rabson struct vnode *a_vp; 579b40ce416SJulian Elischer struct thread *a_td; 58027a0bc89SDoug Rabson } */ *ap; 58127a0bc89SDoug Rabson { 58227a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 58327a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 584b40ce416SJulian Elischer struct thread *td = ap->a_td; 58527a0bc89SDoug Rabson int error = 0; 58627a0bc89SDoug Rabson 58727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 588f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 58927a0bc89SDoug Rabson #endif 59027a0bc89SDoug Rabson 5914d93c0beSJeff Roberson if (prtactive && vrefcnt(vp) != 0) 59227a0bc89SDoug Rabson vprint("msdosfs_inactive(): pushing active", vp); 59327a0bc89SDoug Rabson 59427a0bc89SDoug Rabson /* 595952a6212SJordan K. Hubbard * Ignore denodes related to stale file handles. 59627a0bc89SDoug Rabson */ 597af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 598af3f60d5SBruce Evans goto out; 59927a0bc89SDoug Rabson 60027a0bc89SDoug Rabson /* 60127a0bc89SDoug Rabson * If the file has been deleted and it is on a read/write 60227a0bc89SDoug Rabson * filesystem, then truncate the file, and mark the directory slot 60327a0bc89SDoug Rabson * as empty. (This may not be necessary for the dos filesystem.) 60427a0bc89SDoug Rabson */ 60527a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 606f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", 60727a0bc89SDoug Rabson dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 60827a0bc89SDoug Rabson #endif 60927a0bc89SDoug Rabson if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 610b40ce416SJulian Elischer error = detrunc(dep, (u_long) 0, 0, NOCRED, td); 61127a0bc89SDoug Rabson dep->de_flag |= DE_UPDATE; 61227a0bc89SDoug Rabson dep->de_Name[0] = SLOT_DELETED; 61327a0bc89SDoug Rabson } 614952a6212SJordan K. Hubbard deupdat(dep, 0); 615952a6212SJordan K. Hubbard 616af3f60d5SBruce Evans out: 61727a0bc89SDoug Rabson /* 618952a6212SJordan K. Hubbard * If we are done with the denode, reclaim it 619952a6212SJordan K. Hubbard * so that it can be reused immediately. 62027a0bc89SDoug Rabson */ 62127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 6224d93c0beSJeff Roberson printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 6234d93c0beSJeff Roberson vrefcnt(vp), dep->de_Name[0]); 62427a0bc89SDoug Rabson #endif 625af3f60d5SBruce Evans if (dep->de_Name[0] == SLOT_DELETED) 626d4eb29baSPoul-Henning Kamp vrecycle(vp, td); 627952a6212SJordan K. Hubbard return (error); 62827a0bc89SDoug Rabson } 629