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 /*- 5d63027b6SPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause 6d63027b6SPedro F. Giffuni * 7952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 8952a6212SJordan K. Hubbard * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 927a0bc89SDoug Rabson * All rights reserved. 1027a0bc89SDoug Rabson * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 1127a0bc89SDoug Rabson * 1227a0bc89SDoug Rabson * Redistribution and use in source and binary forms, with or without 1327a0bc89SDoug Rabson * modification, are permitted provided that the following conditions 1427a0bc89SDoug Rabson * are met: 1527a0bc89SDoug Rabson * 1. Redistributions of source code must retain the above copyright 1627a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer. 1727a0bc89SDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright 1827a0bc89SDoug Rabson * notice, this list of conditions and the following disclaimer in the 1927a0bc89SDoug Rabson * documentation and/or other materials provided with the distribution. 2027a0bc89SDoug Rabson * 3. All advertising materials mentioning features or use of this software 2127a0bc89SDoug Rabson * must display the following acknowledgement: 2227a0bc89SDoug Rabson * This product includes software developed by TooLs GmbH. 2327a0bc89SDoug Rabson * 4. The name of TooLs GmbH may not be used to endorse or promote products 2427a0bc89SDoug Rabson * derived from this software without specific prior written permission. 2527a0bc89SDoug Rabson * 2627a0bc89SDoug Rabson * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 2727a0bc89SDoug Rabson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2827a0bc89SDoug Rabson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2927a0bc89SDoug Rabson * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3027a0bc89SDoug Rabson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 3127a0bc89SDoug Rabson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 3227a0bc89SDoug Rabson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3327a0bc89SDoug Rabson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3427a0bc89SDoug Rabson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3527a0bc89SDoug Rabson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3627a0bc89SDoug Rabson */ 37d167cf6fSWarner Losh /*- 3827a0bc89SDoug Rabson * Written by Paul Popelka (paulp@uts.amdahl.com) 3927a0bc89SDoug Rabson * 4027a0bc89SDoug Rabson * You can do anything you want with this software, just don't say you wrote 4127a0bc89SDoug Rabson * it, and don't remove this notice. 4227a0bc89SDoug Rabson * 4327a0bc89SDoug Rabson * This software is provided "as is". 4427a0bc89SDoug Rabson * 4527a0bc89SDoug Rabson * The author supplies this software to be publicly redistributed on the 4627a0bc89SDoug Rabson * understanding that the author is not responsible for the correct 4727a0bc89SDoug Rabson * functioning of this software in any circumstances and is not liable for 4827a0bc89SDoug Rabson * any damages caused by this software. 4927a0bc89SDoug Rabson * 5027a0bc89SDoug Rabson * October 1992 5127a0bc89SDoug Rabson */ 5227a0bc89SDoug Rabson 5327a0bc89SDoug Rabson #include <sys/param.h> 5427a0bc89SDoug Rabson #include <sys/systm.h> 5527a0bc89SDoug Rabson #include <sys/buf.h> 563c960d93SPoul-Henning Kamp #include <sys/clock.h> 57a878a31cSBruce Evans #include <sys/kernel.h> 58a878a31cSBruce Evans #include <sys/malloc.h> 59a878a31cSBruce Evans #include <sys/mount.h> 609ed01c32SGleb Smirnoff #include <sys/vmmeter.h> 6127a0bc89SDoug Rabson #include <sys/vnode.h> 6227a0bc89SDoug Rabson 63c3c6d51eSPoul-Henning Kamp #include <vm/vm.h> 64efeaf95aSDavid Greenman #include <vm/vm_extern.h> 65c3c6d51eSPoul-Henning Kamp 661166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h> 671166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h> 681166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h> 691166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h> 70a878a31cSBruce Evans #include <fs/msdosfs/msdosfsmount.h> 7127a0bc89SDoug Rabson 725bb84bc8SRobert Watson static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part"); 7355166637SPoul-Henning Kamp 74f4b423aeSPoul-Henning Kamp static int 75f4b423aeSPoul-Henning Kamp de_vncmpf(struct vnode *vp, void *arg) 76f4b423aeSPoul-Henning Kamp { 77f4b423aeSPoul-Henning Kamp struct denode *de; 78f4b423aeSPoul-Henning Kamp uint64_t *a; 79f4b423aeSPoul-Henning Kamp 80f4b423aeSPoul-Henning Kamp a = arg; 81f4b423aeSPoul-Henning Kamp de = VTODE(vp); 82f4b423aeSPoul-Henning Kamp return (de->de_inode != *a); 83f4b423aeSPoul-Henning Kamp } 8427a0bc89SDoug Rabson 8527a0bc89SDoug Rabson /* 8627a0bc89SDoug Rabson * If deget() succeeds it returns with the gotten denode locked(). 8727a0bc89SDoug Rabson * 8827a0bc89SDoug Rabson * pmp - address of msdosfsmount structure of the filesystem containing 893b97f388SPoul-Henning Kamp * the denode of interest. The address of 9027a0bc89SDoug Rabson * the msdosfsmount structure are used. 9127a0bc89SDoug Rabson * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 9227a0bc89SDoug Rabson * diroffset is relative to the beginning of the root directory, 9327a0bc89SDoug Rabson * otherwise it is cluster relative. 9427a0bc89SDoug Rabson * diroffset - offset past begin of cluster of denode we want 9527a0bc89SDoug Rabson * depp - returns the address of the gotten denode. 9627a0bc89SDoug Rabson */ 9727a0bc89SDoug Rabson int 9810c9700fSEd Maste deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 9910c9700fSEd Maste struct denode **depp) 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 } 1458b36e813SKonstantin Belousov ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO); 14627a0bc89SDoug Rabson 14727a0bc89SDoug Rabson /* 14827a0bc89SDoug Rabson * Directory entry was not in cache, have to create a vnode and 14927a0bc89SDoug Rabson * copy it from the passed disk buffer. 15027a0bc89SDoug Rabson */ 15127a0bc89SDoug Rabson /* getnewvnode() does a VREF() on the vnode */ 152aec0fb7bSPoul-Henning Kamp error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp); 153c3c6d51eSPoul-Henning Kamp if (error) { 1542f9bae59SDavid Greenman *depp = NULL; 1551ede983cSDag-Erling Smørgrav free(ldep, M_MSDOSFSNODE); 15627a0bc89SDoug Rabson return error; 15727a0bc89SDoug Rabson } 15827a0bc89SDoug Rabson nvp->v_data = ldep; 15927a0bc89SDoug Rabson ldep->de_vnode = nvp; 16027a0bc89SDoug Rabson ldep->de_flag = 0; 16127a0bc89SDoug Rabson ldep->de_dirclust = dirclust; 16227a0bc89SDoug Rabson ldep->de_diroffset = diroffset; 163f4b423aeSPoul-Henning Kamp ldep->de_inode = inode; 1640e9eb108SAttilio Rao lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL); 1659287dbaaSEd Maste fc_purge(ldep, 0); /* init the FAT cache for this denode */ 16661b9d89fSTor Egge error = insmntque(nvp, mntp); 16761b9d89fSTor Egge if (error != 0) { 1681ede983cSDag-Erling Smørgrav free(ldep, M_MSDOSFSNODE); 16961b9d89fSTor Egge *depp = NULL; 17061b9d89fSTor Egge return (error); 17161b9d89fSTor Egge } 1720e9eb108SAttilio Rao error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp, 173f4b423aeSPoul-Henning Kamp de_vncmpf, &inode); 174a30fc63bSPoul-Henning Kamp if (error) { 175a30fc63bSPoul-Henning Kamp *depp = NULL; 176a30fc63bSPoul-Henning Kamp return (error); 177a30fc63bSPoul-Henning Kamp } 178a30fc63bSPoul-Henning Kamp if (xvp != NULL) { 1790bdbd627SKonstantin Belousov *depp = xvp->v_data; 1800bdbd627SKonstantin Belousov return (0); 181a30fc63bSPoul-Henning Kamp } 18227a0bc89SDoug Rabson 183952a6212SJordan K. Hubbard ldep->de_pmp = pmp; 184952a6212SJordan K. Hubbard ldep->de_refcnt = 1; 18527a0bc89SDoug Rabson /* 18627a0bc89SDoug Rabson * Copy the directory entry into the denode area of the vnode. 18727a0bc89SDoug Rabson */ 188952a6212SJordan K. Hubbard if ((dirclust == MSDOSFSROOT 189952a6212SJordan K. Hubbard || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 190952a6212SJordan K. Hubbard && diroffset == MSDOSFSROOT_OFS) { 19127a0bc89SDoug Rabson /* 19227a0bc89SDoug Rabson * Directory entry for the root directory. There isn't one, 19327a0bc89SDoug Rabson * so we manufacture one. We should probably rummage 19427a0bc89SDoug Rabson * through the root directory and find a label entry (if it 19527a0bc89SDoug Rabson * exists), and then use the time and date from that entry 19627a0bc89SDoug Rabson * as the time and date for the root denode. 19727a0bc89SDoug Rabson */ 198e6e370a7SJeff Roberson nvp->v_vflag |= VV_ROOT; /* should be further down XXX */ 199952a6212SJordan K. Hubbard 20027a0bc89SDoug Rabson ldep->de_Attributes = ATTR_DIRECTORY; 201bad3d41dSDmitrij Tejblum ldep->de_LowerCase = 0; 202952a6212SJordan K. Hubbard if (FAT32(pmp)) 203952a6212SJordan K. Hubbard ldep->de_StartCluster = pmp->pm_rootdirblk; 204952a6212SJordan K. Hubbard /* de_FileSize will be filled in further down */ 205952a6212SJordan K. Hubbard else { 20627a0bc89SDoug Rabson ldep->de_StartCluster = MSDOSFSROOT; 20701f6cfbaSYoshihiro Takahashi ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 208952a6212SJordan K. Hubbard } 20927a0bc89SDoug Rabson /* 2103c960d93SPoul-Henning Kamp * fill in time and date so that fattime2timespec() doesn't 21127a0bc89SDoug Rabson * spit up when called from msdosfs_getattr() with root 21227a0bc89SDoug Rabson * denode 21327a0bc89SDoug Rabson */ 214952a6212SJordan K. Hubbard ldep->de_CHun = 0; 215952a6212SJordan K. Hubbard ldep->de_CTime = 0x0000; /* 00:00:00 */ 216952a6212SJordan K. Hubbard ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 21727a0bc89SDoug Rabson | (1 << DD_DAY_SHIFT); 21827a0bc89SDoug Rabson /* Jan 1, 1980 */ 219952a6212SJordan K. Hubbard ldep->de_ADate = ldep->de_CDate; 220952a6212SJordan K. Hubbard ldep->de_MTime = ldep->de_CTime; 221952a6212SJordan K. Hubbard ldep->de_MDate = ldep->de_CDate; 22227a0bc89SDoug Rabson /* leave the other fields as garbage */ 22327a0bc89SDoug Rabson } else { 224952a6212SJordan K. Hubbard error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 2255097a20dSBruce Evans if (error) { 2265097a20dSBruce Evans /* 2275097a20dSBruce Evans * The denode does not contain anything useful, so 2285097a20dSBruce Evans * it would be wrong to leave it on its hash chain. 2295097a20dSBruce Evans * Arrange for vput() to just forget about it. 2305097a20dSBruce Evans */ 2315097a20dSBruce Evans ldep->de_Name[0] = SLOT_DELETED; 2325097a20dSBruce Evans 2335097a20dSBruce Evans vput(nvp); 2345097a20dSBruce Evans *depp = NULL; 235952a6212SJordan K. Hubbard return (error); 2365097a20dSBruce Evans } 2370b53cc9fSRui Paulo (void)DE_INTERNALIZE(ldep, direntptr); 23827a0bc89SDoug Rabson brelse(bp); 23927a0bc89SDoug Rabson } 24027a0bc89SDoug Rabson 24127a0bc89SDoug Rabson /* 24227a0bc89SDoug Rabson * Fill in a few fields of the vnode and finish filling in the 24327a0bc89SDoug Rabson * denode. Then return the address of the found denode. 24427a0bc89SDoug Rabson */ 24527a0bc89SDoug Rabson if (ldep->de_Attributes & ATTR_DIRECTORY) { 24627a0bc89SDoug Rabson /* 24727a0bc89SDoug Rabson * Since DOS directory entries that describe directories 24827a0bc89SDoug Rabson * have 0 in the filesize field, we take this opportunity 24927a0bc89SDoug Rabson * to find out the length of the directory and plug it into 25027a0bc89SDoug Rabson * the denode structure. 25127a0bc89SDoug Rabson */ 25227a0bc89SDoug Rabson u_long size; 25327a0bc89SDoug Rabson 254b5cdbc6dSTom Rhodes /* 255e3117f85SBruce Evans * XXX it sometimes happens that the "." entry has cluster 256e3117f85SBruce Evans * number 0 when it shouldn't. Use the actual cluster number 257b5cdbc6dSTom Rhodes * instead of what is written in directory entry. 258b5cdbc6dSTom Rhodes */ 259e3117f85SBruce Evans if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 26054cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 261e3117f85SBruce Evans printf("deget(): \".\" entry at clust %lu != %lu\n", 262b5cdbc6dSTom Rhodes dirclust, ldep->de_StartCluster); 26354cf9198SKonstantin Belousov #endif 264b5cdbc6dSTom Rhodes ldep->de_StartCluster = dirclust; 265b5cdbc6dSTom Rhodes } 266b5cdbc6dSTom Rhodes 26727a0bc89SDoug Rabson nvp->v_type = VDIR; 268952a6212SJordan K. Hubbard if (ldep->de_StartCluster != MSDOSFSROOT) { 269952a6212SJordan K. Hubbard error = pcbmap(ldep, 0xffff, 0, &size, 0); 27027a0bc89SDoug Rabson if (error == E2BIG) { 271952a6212SJordan K. Hubbard ldep->de_FileSize = de_cn2off(pmp, size); 27227a0bc89SDoug Rabson error = 0; 27354cf9198SKonstantin Belousov } else { 27454cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 27527a0bc89SDoug Rabson printf("deget(): pcbmap returned %d\n", error); 27654cf9198SKonstantin Belousov #endif 27754cf9198SKonstantin Belousov } 27827a0bc89SDoug Rabson } 27927a0bc89SDoug Rabson } else 28027a0bc89SDoug Rabson nvp->v_type = VREG; 2811affa3adSPoul-Henning Kamp ldep->de_modrev = init_va_filerev(); 28227a0bc89SDoug Rabson *depp = ldep; 283952a6212SJordan K. Hubbard return (0); 28427a0bc89SDoug Rabson } 28527a0bc89SDoug Rabson 28627a0bc89SDoug Rabson int 28710c9700fSEd Maste deupdat(struct denode *dep, int waitfor) 28827a0bc89SDoug Rabson { 289293e4eb6SKonstantin Belousov struct direntry dir; 290293e4eb6SKonstantin Belousov struct timespec ts; 29127a0bc89SDoug Rabson struct buf *bp; 29227a0bc89SDoug Rabson struct direntry *dirp; 293293e4eb6SKonstantin Belousov int error; 29427a0bc89SDoug Rabson 295293e4eb6SKonstantin Belousov if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) { 296293e4eb6SKonstantin Belousov dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS | 297293e4eb6SKonstantin Belousov DE_MODIFIED); 298952a6212SJordan K. Hubbard return (0); 299293e4eb6SKonstantin Belousov } 300*b732ceb6SPedro F. Giffuni vfs_timestamp(&ts); 301952a6212SJordan K. Hubbard DETIMES(dep, &ts, &ts, &ts); 302293e4eb6SKonstantin Belousov if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0) 303952a6212SJordan K. Hubbard return (0); 304952a6212SJordan K. Hubbard dep->de_flag &= ~DE_MODIFIED; 3057da1a731SKenneth D. Merry if (DETOV(dep)->v_vflag & VV_ROOT) 3067da1a731SKenneth D. Merry return (EINVAL); 307952a6212SJordan K. Hubbard if (dep->de_refcnt <= 0) 308952a6212SJordan K. Hubbard return (0); 309c3c6d51eSPoul-Henning Kamp error = readde(dep, &bp, &dirp); 310c3c6d51eSPoul-Henning Kamp if (error) 311952a6212SJordan K. Hubbard return (error); 312293e4eb6SKonstantin Belousov DE_EXTERNALIZE(&dir, dep); 313293e4eb6SKonstantin Belousov if (bcmp(dirp, &dir, sizeof(dir)) == 0) { 314293e4eb6SKonstantin Belousov if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) { 315293e4eb6SKonstantin Belousov brelse(bp); 316952a6212SJordan K. Hubbard return (0); 317952a6212SJordan K. Hubbard } 318293e4eb6SKonstantin Belousov } else 319293e4eb6SKonstantin Belousov *dirp = dir; 320293e4eb6SKonstantin Belousov if ((DETOV(dep)->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) 321293e4eb6SKonstantin Belousov bp->b_flags |= B_CLUSTEROK; 322293e4eb6SKonstantin Belousov if (waitfor) 323293e4eb6SKonstantin Belousov error = bwrite(bp); 324293e4eb6SKonstantin Belousov else if (vm_page_count_severe() || buf_dirty_count_severe()) 325293e4eb6SKonstantin Belousov bawrite(bp); 326293e4eb6SKonstantin Belousov else 327293e4eb6SKonstantin Belousov bdwrite(bp); 328293e4eb6SKonstantin Belousov return (error); 32927a0bc89SDoug Rabson } 33027a0bc89SDoug Rabson 33127a0bc89SDoug Rabson /* 33227a0bc89SDoug Rabson * Truncate the file described by dep to the length specified by length. 33327a0bc89SDoug Rabson */ 33427a0bc89SDoug Rabson int 33510c9700fSEd Maste detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred) 33627a0bc89SDoug Rabson { 33727a0bc89SDoug Rabson int error; 33827a0bc89SDoug Rabson int allerror; 33927a0bc89SDoug Rabson u_long eofentry; 34027a0bc89SDoug Rabson u_long chaintofree; 34127a0bc89SDoug Rabson daddr_t bn; 34227a0bc89SDoug Rabson int boff; 34327a0bc89SDoug Rabson int isadir = dep->de_Attributes & ATTR_DIRECTORY; 34427a0bc89SDoug Rabson struct buf *bp; 34527a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 34627a0bc89SDoug Rabson 34727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 348952a6212SJordan K. Hubbard printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 34927a0bc89SDoug Rabson #endif 35027a0bc89SDoug Rabson 35127a0bc89SDoug Rabson /* 35227a0bc89SDoug Rabson * Disallow attempts to truncate the root directory since it is of 35327a0bc89SDoug Rabson * fixed size. That's just the way dos filesystems are. We use 35427a0bc89SDoug Rabson * the VROOT bit in the vnode because checking for the directory 35527a0bc89SDoug Rabson * bit and a startcluster of 0 in the denode is not adequate to 35627a0bc89SDoug Rabson * recognize the root directory at this point in a file or 35727a0bc89SDoug Rabson * directory's life. 35827a0bc89SDoug Rabson */ 359e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) { 36054cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG 361952a6212SJordan K. Hubbard printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 36227a0bc89SDoug Rabson dep->de_dirclust, dep->de_diroffset); 36354cf9198SKonstantin Belousov #endif 364952a6212SJordan K. Hubbard return (EINVAL); 36527a0bc89SDoug Rabson } 36627a0bc89SDoug Rabson 367bd7e5f99SJohn Dyson if (dep->de_FileSize < length) { 368bd7e5f99SJohn Dyson vnode_pager_setsize(DETOV(dep), length); 36927a0bc89SDoug Rabson return deextend(dep, length, cred); 370bd7e5f99SJohn Dyson } 37127a0bc89SDoug Rabson 37227a0bc89SDoug Rabson /* 37327a0bc89SDoug Rabson * If the desired length is 0 then remember the starting cluster of 37427a0bc89SDoug Rabson * the file and set the StartCluster field in the directory entry 37527a0bc89SDoug Rabson * to 0. If the desired length is not zero, then get the number of 37627a0bc89SDoug Rabson * the last cluster in the shortened file. Then get the number of 37727a0bc89SDoug Rabson * the first cluster in the part of the file that is to be freed. 37827a0bc89SDoug Rabson * Then set the next cluster pointer in the last cluster of the 37927a0bc89SDoug Rabson * file to CLUST_EOFE. 38027a0bc89SDoug Rabson */ 38127a0bc89SDoug Rabson if (length == 0) { 38227a0bc89SDoug Rabson chaintofree = dep->de_StartCluster; 38327a0bc89SDoug Rabson dep->de_StartCluster = 0; 38427a0bc89SDoug Rabson eofentry = ~0; 38527a0bc89SDoug Rabson } else { 386952a6212SJordan K. Hubbard error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 387952a6212SJordan K. Hubbard &eofentry, 0); 388c3c6d51eSPoul-Henning Kamp if (error) { 38927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 39027a0bc89SDoug Rabson printf("detrunc(): pcbmap fails %d\n", error); 39127a0bc89SDoug Rabson #endif 392952a6212SJordan K. Hubbard return (error); 39327a0bc89SDoug Rabson } 39427a0bc89SDoug Rabson } 39527a0bc89SDoug Rabson 396952a6212SJordan K. Hubbard fc_purge(dep, de_clcount(pmp, length)); 39727a0bc89SDoug Rabson 39827a0bc89SDoug Rabson /* 39927a0bc89SDoug Rabson * If the new length is not a multiple of the cluster size then we 40027a0bc89SDoug Rabson * must zero the tail end of the new last cluster in case it 40127a0bc89SDoug Rabson * becomes part of the file again because of a seek. 40227a0bc89SDoug Rabson */ 40327a0bc89SDoug Rabson if ((boff = length & pmp->pm_crbomask) != 0) { 40427a0bc89SDoug Rabson if (isadir) { 40527a0bc89SDoug Rabson bn = cntobn(pmp, eofentry); 40627a0bc89SDoug Rabson error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 40727a0bc89SDoug Rabson NOCRED, &bp); 40827a0bc89SDoug Rabson if (error) { 409952a6212SJordan K. Hubbard brelse(bp); 41027a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 41127a0bc89SDoug Rabson printf("detrunc(): bread fails %d\n", error); 41227a0bc89SDoug Rabson #endif 413952a6212SJordan K. Hubbard return (error); 41427a0bc89SDoug Rabson } 4156a1c2e1fSEd Maste memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff); 41627a0bc89SDoug Rabson if (flags & IO_SYNC) 41727a0bc89SDoug Rabson bwrite(bp); 41827a0bc89SDoug Rabson else 41927a0bc89SDoug Rabson bdwrite(bp); 42027a0bc89SDoug Rabson } 421c2f95f66STom Rhodes } 42227a0bc89SDoug Rabson 42327a0bc89SDoug Rabson /* 42427a0bc89SDoug Rabson * Write out the updated directory entry. Even if the update fails 42527a0bc89SDoug Rabson * we free the trailing clusters. 42627a0bc89SDoug Rabson */ 42727a0bc89SDoug Rabson dep->de_FileSize = length; 428952a6212SJordan K. Hubbard if (!isadir) 429952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE | DE_MODIFIED; 430c52fd858SEdward Tomasz Napierala allerror = vtruncbuf(DETOV(dep), cred, length, pmp->pm_bpcluster); 43144fdad99SPeter Wemm #ifdef MSDOSFS_DEBUG 43244fdad99SPeter Wemm if (allerror) 43344fdad99SPeter Wemm printf("detrunc(): vtruncbuf error %d\n", allerror); 43444fdad99SPeter Wemm #endif 4352aacee77SKonstantin Belousov error = deupdat(dep, !DOINGASYNC((DETOV(dep)))); 436e3117f85SBruce Evans if (error != 0 && allerror == 0) 43744fdad99SPeter Wemm allerror = error; 43827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 439952a6212SJordan K. Hubbard printf("detrunc(): allerror %d, eofentry %lu\n", 44027a0bc89SDoug Rabson allerror, eofentry); 44127a0bc89SDoug Rabson #endif 44227a0bc89SDoug Rabson 44327a0bc89SDoug Rabson /* 44427a0bc89SDoug Rabson * If we need to break the cluster chain for the file then do it 44527a0bc89SDoug Rabson * now. 44627a0bc89SDoug Rabson */ 44727a0bc89SDoug Rabson if (eofentry != ~0) { 44827a0bc89SDoug Rabson error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 44927a0bc89SDoug Rabson &chaintofree, CLUST_EOFE); 45027a0bc89SDoug Rabson if (error) { 45127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 45227a0bc89SDoug Rabson printf("detrunc(): fatentry errors %d\n", error); 45327a0bc89SDoug Rabson #endif 454952a6212SJordan K. Hubbard return (error); 45527a0bc89SDoug Rabson } 456952a6212SJordan K. Hubbard fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 45727a0bc89SDoug Rabson eofentry); 45827a0bc89SDoug Rabson } 45927a0bc89SDoug Rabson 46027a0bc89SDoug Rabson /* 46127a0bc89SDoug Rabson * Now free the clusters removed from the file because of the 46227a0bc89SDoug Rabson * truncation. 46327a0bc89SDoug Rabson */ 464952a6212SJordan K. Hubbard if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 46527a0bc89SDoug Rabson freeclusterchain(pmp, chaintofree); 46627a0bc89SDoug Rabson 467952a6212SJordan K. Hubbard return (allerror); 46827a0bc89SDoug Rabson } 46927a0bc89SDoug Rabson 47027a0bc89SDoug Rabson /* 47127a0bc89SDoug Rabson * Extend the file described by dep to length specified by length. 47227a0bc89SDoug Rabson */ 47327a0bc89SDoug Rabson int 47410c9700fSEd Maste deextend(struct denode *dep, u_long length, struct ucred *cred) 47527a0bc89SDoug Rabson { 47627a0bc89SDoug Rabson struct msdosfsmount *pmp = dep->de_pmp; 47727a0bc89SDoug Rabson u_long count; 47827a0bc89SDoug Rabson int error; 47927a0bc89SDoug Rabson 48027a0bc89SDoug Rabson /* 48127a0bc89SDoug Rabson * The root of a DOS filesystem cannot be extended. 48227a0bc89SDoug Rabson */ 483e6e370a7SJeff Roberson if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 484952a6212SJordan K. Hubbard return (EINVAL); 48527a0bc89SDoug Rabson 48627a0bc89SDoug Rabson /* 487952a6212SJordan K. Hubbard * Directories cannot be extended. 48827a0bc89SDoug Rabson */ 489952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 490952a6212SJordan K. Hubbard return (EISDIR); 49127a0bc89SDoug Rabson 49227a0bc89SDoug Rabson if (length <= dep->de_FileSize) 49327a0bc89SDoug Rabson panic("deextend: file too large"); 49427a0bc89SDoug Rabson 49527a0bc89SDoug Rabson /* 49627a0bc89SDoug Rabson * Compute the number of clusters to allocate. 49727a0bc89SDoug Rabson */ 49827a0bc89SDoug Rabson count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 49927a0bc89SDoug Rabson if (count > 0) { 50027a0bc89SDoug Rabson if (count > pmp->pm_freeclustercount) 501952a6212SJordan K. Hubbard return (ENOSPC); 502c3c6d51eSPoul-Henning Kamp error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 503c3c6d51eSPoul-Henning Kamp if (error) { 50427a0bc89SDoug Rabson /* truncate the added clusters away again */ 505c52fd858SEdward Tomasz Napierala (void) detrunc(dep, dep->de_FileSize, 0, cred); 506952a6212SJordan K. Hubbard return (error); 50727a0bc89SDoug Rabson } 50827a0bc89SDoug Rabson } 50927a0bc89SDoug Rabson dep->de_FileSize = length; 510952a6212SJordan K. Hubbard dep->de_flag |= DE_UPDATE | DE_MODIFIED; 5112aacee77SKonstantin Belousov return (deupdat(dep, !DOINGASYNC(DETOV(dep)))); 51227a0bc89SDoug Rabson } 51327a0bc89SDoug Rabson 51427a0bc89SDoug Rabson /* 51527a0bc89SDoug Rabson * Move a denode to its correct hash queue after the file it represents has 51627a0bc89SDoug Rabson * been moved to a new directory. 51727a0bc89SDoug Rabson */ 518952a6212SJordan K. Hubbard void 51910c9700fSEd Maste reinsert(struct denode *dep) 52027a0bc89SDoug Rabson { 521a30fc63bSPoul-Henning Kamp struct vnode *vp; 522a30fc63bSPoul-Henning Kamp 52327a0bc89SDoug Rabson /* 52427a0bc89SDoug Rabson * Fix up the denode cache. If the denode is for a directory, 52527a0bc89SDoug Rabson * there is nothing to do since the hash is based on the starting 52627a0bc89SDoug Rabson * cluster of the directory file and that hasn't changed. If for a 52727a0bc89SDoug Rabson * file the hash is based on the location of the directory entry, 52827a0bc89SDoug Rabson * so we must remove it from the cache and re-enter it with the 52927a0bc89SDoug Rabson * hash based on the new location of the directory entry. 53027a0bc89SDoug Rabson */ 531f4b423aeSPoul-Henning Kamp #if 0 532952a6212SJordan K. Hubbard if (dep->de_Attributes & ATTR_DIRECTORY) 533952a6212SJordan K. Hubbard return; 534f4b423aeSPoul-Henning Kamp #endif 535a30fc63bSPoul-Henning Kamp vp = DETOV(dep); 5365ddf2985SDavid E. O'Brien dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust + 537f4b423aeSPoul-Henning Kamp dep->de_diroffset; 538f4b423aeSPoul-Henning Kamp vfs_hash_rehash(vp, dep->de_inode); 53927a0bc89SDoug Rabson } 54027a0bc89SDoug Rabson 54127a0bc89SDoug Rabson int 54210c9700fSEd Maste msdosfs_reclaim(struct vop_reclaim_args *ap) 54327a0bc89SDoug Rabson { 54427a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 54527a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 54627a0bc89SDoug Rabson 54727a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 548f0707215SPoul-Henning Kamp printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 54927a0bc89SDoug Rabson dep, dep->de_Name, dep->de_refcnt); 55027a0bc89SDoug Rabson #endif 55127a0bc89SDoug Rabson 55227a0bc89SDoug Rabson /* 55392e73f57SAlfred Perlstein * Destroy the vm object and flush associated pages. 55492e73f57SAlfred Perlstein */ 55592e73f57SAlfred Perlstein vnode_destroy_vobject(vp); 55692e73f57SAlfred Perlstein /* 557952a6212SJordan K. Hubbard * Remove the denode from its hash chain. 55827a0bc89SDoug Rabson */ 559a30fc63bSPoul-Henning Kamp vfs_hash_remove(vp); 56027a0bc89SDoug Rabson /* 561952a6212SJordan K. Hubbard * Purge old data structures associated with the denode. 56227a0bc89SDoug Rabson */ 563952a6212SJordan K. Hubbard #if 0 /* XXX */ 56427a0bc89SDoug Rabson dep->de_flag = 0; 565952a6212SJordan K. Hubbard #endif 5661ede983cSDag-Erling Smørgrav free(dep, M_MSDOSFSNODE); 56727a0bc89SDoug Rabson vp->v_data = NULL; 56827a0bc89SDoug Rabson 569952a6212SJordan K. Hubbard return (0); 57027a0bc89SDoug Rabson } 57127a0bc89SDoug Rabson 57227a0bc89SDoug Rabson int 57310c9700fSEd Maste msdosfs_inactive(struct vop_inactive_args *ap) 57427a0bc89SDoug Rabson { 57527a0bc89SDoug Rabson struct vnode *vp = ap->a_vp; 57627a0bc89SDoug Rabson struct denode *dep = VTODE(vp); 57727a0bc89SDoug Rabson int error = 0; 57827a0bc89SDoug Rabson 57927a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 580f0707215SPoul-Henning Kamp printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 58127a0bc89SDoug Rabson #endif 58227a0bc89SDoug Rabson 58327a0bc89SDoug Rabson /* 584952a6212SJordan K. Hubbard * Ignore denodes related to stale file handles. 58527a0bc89SDoug Rabson */ 586740a7201SKonstantin Belousov if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY) 587af3f60d5SBruce Evans goto out; 58827a0bc89SDoug Rabson 58927a0bc89SDoug Rabson /* 59027a0bc89SDoug Rabson * If the file has been deleted and it is on a read/write 59127a0bc89SDoug Rabson * filesystem, then truncate the file, and mark the directory slot 59227a0bc89SDoug Rabson * as empty. (This may not be necessary for the dos filesystem.) 59327a0bc89SDoug Rabson */ 59427a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 595027bebe8SEd Maste printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %llx, MNT_RDONLY %llx\n", 596027bebe8SEd Maste dep, dep->de_refcnt, (unsigned long long)vp->v_mount->mnt_flag, 597027bebe8SEd Maste (unsigned long long)MNT_RDONLY); 59827a0bc89SDoug Rabson #endif 59927a0bc89SDoug Rabson if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 600c52fd858SEdward Tomasz Napierala error = detrunc(dep, (u_long) 0, 0, NOCRED); 60127a0bc89SDoug Rabson dep->de_flag |= DE_UPDATE; 60227a0bc89SDoug Rabson dep->de_Name[0] = SLOT_DELETED; 60327a0bc89SDoug Rabson } 604952a6212SJordan K. Hubbard deupdat(dep, 0); 605952a6212SJordan K. Hubbard 606af3f60d5SBruce Evans out: 60727a0bc89SDoug Rabson /* 608952a6212SJordan K. Hubbard * If we are done with the denode, reclaim it 609952a6212SJordan K. Hubbard * so that it can be reused immediately. 61027a0bc89SDoug Rabson */ 61127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG 6124d93c0beSJeff Roberson printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 6134d93c0beSJeff Roberson vrefcnt(vp), dep->de_Name[0]); 61427a0bc89SDoug Rabson #endif 615740a7201SKonstantin Belousov if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY) 616af6e6b87SEdward Tomasz Napierala vrecycle(vp); 617952a6212SJordan K. Hubbard return (error); 61827a0bc89SDoug Rabson } 619