1237d1b14SEd Maste /* $NetBSD: msdosfs_denode.c,v 1.7 2015/03/29 05:52:59 agc Exp $ */ 2237d1b14SEd Maste 3237d1b14SEd Maste /*- 42037e988SEd Maste * SPDX-License-Identifier: BSD-4-Clause 52037e988SEd Maste * 6237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 7237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 8237d1b14SEd Maste * All rights reserved. 9237d1b14SEd Maste * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 10237d1b14SEd Maste * 11237d1b14SEd Maste * Redistribution and use in source and binary forms, with or without 12237d1b14SEd Maste * modification, are permitted provided that the following conditions 13237d1b14SEd Maste * are met: 14237d1b14SEd Maste * 1. Redistributions of source code must retain the above copyright 15237d1b14SEd Maste * notice, this list of conditions and the following disclaimer. 16237d1b14SEd Maste * 2. Redistributions in binary form must reproduce the above copyright 17237d1b14SEd Maste * notice, this list of conditions and the following disclaimer in the 18237d1b14SEd Maste * documentation and/or other materials provided with the distribution. 19237d1b14SEd Maste * 3. All advertising materials mentioning features or use of this software 20237d1b14SEd Maste * must display the following acknowledgement: 21237d1b14SEd Maste * This product includes software developed by TooLs GmbH. 22237d1b14SEd Maste * 4. The name of TooLs GmbH may not be used to endorse or promote products 23237d1b14SEd Maste * derived from this software without specific prior written permission. 24237d1b14SEd Maste * 25237d1b14SEd Maste * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 26237d1b14SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27237d1b14SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28237d1b14SEd Maste * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29237d1b14SEd Maste * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30237d1b14SEd Maste * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 31237d1b14SEd Maste * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32237d1b14SEd Maste * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33237d1b14SEd Maste * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34237d1b14SEd Maste * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35237d1b14SEd Maste */ 362037e988SEd Maste /*- 37237d1b14SEd Maste * Written by Paul Popelka (paulp@uts.amdahl.com) 38237d1b14SEd Maste * 39237d1b14SEd Maste * You can do anything you want with this software, just don't say you wrote 40237d1b14SEd Maste * it, and don't remove this notice. 41237d1b14SEd Maste * 42237d1b14SEd Maste * This software is provided "as is". 43237d1b14SEd Maste * 44237d1b14SEd Maste * The author supplies this software to be publicly redistributed on the 45237d1b14SEd Maste * understanding that the author is not responsible for the correct 46237d1b14SEd Maste * functioning of this software in any circumstances and is not liable for 47237d1b14SEd Maste * any damages caused by this software. 48237d1b14SEd Maste * 49237d1b14SEd Maste * October 1992 50237d1b14SEd Maste */ 51237d1b14SEd Maste 52237d1b14SEd Maste #include <sys/cdefs.h> 53237d1b14SEd Maste __FBSDID("$FreeBSD$"); 54237d1b14SEd Maste 55237d1b14SEd Maste #include <sys/param.h> 5698dc8da5SEd Maste #include <sys/errno.h> 57237d1b14SEd Maste 58aaa38524SConrad Meyer #include <stdbool.h> 5998dc8da5SEd Maste #include <stdio.h> 6098dc8da5SEd Maste #include <string.h> 6198dc8da5SEd Maste #include <stdlib.h> 6298dc8da5SEd Maste #include <util.h> 63237d1b14SEd Maste 64237d1b14SEd Maste #include <fs/msdosfs/bpb.h> 65476b0ab7SEd Maste #include <fs/msdosfs/direntry.h> 66476b0ab7SEd Maste #include <fs/msdosfs/denode.h> 6751e79affSEd Maste #include <fs/msdosfs/fat.h> 68840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h> 69237d1b14SEd Maste 70*d485c77fSKonstantin Belousov #undef clrbuf 71*d485c77fSKonstantin Belousov #include "ffs/buf.h" 7298dc8da5SEd Maste #include "makefs.h" 7398dc8da5SEd Maste #include "msdos.h" 7498dc8da5SEd Maste 75237d1b14SEd Maste 76237d1b14SEd Maste /* 77237d1b14SEd Maste * If deget() succeeds it returns with the gotten denode locked(). 78237d1b14SEd Maste * 79237d1b14SEd Maste * pmp - address of msdosfsmount structure of the filesystem containing 80237d1b14SEd Maste * the denode of interest. The pm_dev field and the address of 81237d1b14SEd Maste * the msdosfsmount structure are used. 82237d1b14SEd Maste * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 83237d1b14SEd Maste * diroffset is relative to the beginning of the root directory, 84237d1b14SEd Maste * otherwise it is cluster relative. 85237d1b14SEd Maste * diroffset - offset past begin of cluster of denode we want 86237d1b14SEd Maste * depp - returns the address of the gotten denode. 87237d1b14SEd Maste */ 88237d1b14SEd Maste int 89237d1b14SEd Maste deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 90237d1b14SEd Maste struct denode **depp) 91237d1b14SEd Maste { 92237d1b14SEd Maste int error; 9398dc8da5SEd Maste uint64_t inode; 94237d1b14SEd Maste struct direntry *direntptr; 95237d1b14SEd Maste struct denode *ldep; 96*d485c77fSKonstantin Belousov struct m_buf *bp; 97237d1b14SEd Maste 9898dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 9998dc8da5SEd Maste pmp, dirclust, diroffset, depp)); 100237d1b14SEd Maste 101237d1b14SEd Maste /* 102237d1b14SEd Maste * On FAT32 filesystems, root is a (more or less) normal 103237d1b14SEd Maste * directory 104237d1b14SEd Maste */ 105237d1b14SEd Maste if (FAT32(pmp) && dirclust == MSDOSFSROOT) 106237d1b14SEd Maste dirclust = pmp->pm_rootdirblk; 107237d1b14SEd Maste 10898dc8da5SEd Maste inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 10998dc8da5SEd Maste 110237d1b14SEd Maste ldep = ecalloc(1, sizeof(*ldep)); 111237d1b14SEd Maste ldep->de_vnode = NULL; 112237d1b14SEd Maste ldep->de_flag = 0; 113237d1b14SEd Maste ldep->de_dirclust = dirclust; 114237d1b14SEd Maste ldep->de_diroffset = diroffset; 11598dc8da5SEd Maste ldep->de_inode = inode; 116237d1b14SEd Maste ldep->de_pmp = pmp; 117237d1b14SEd Maste ldep->de_refcnt = 1; 11898dc8da5SEd Maste fc_purge(ldep, 0); /* init the FAT cache for this denode */ 119237d1b14SEd Maste /* 120237d1b14SEd Maste * Copy the directory entry into the denode area of the vnode. 121237d1b14SEd Maste */ 122237d1b14SEd Maste if ((dirclust == MSDOSFSROOT 123237d1b14SEd Maste || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 124237d1b14SEd Maste && diroffset == MSDOSFSROOT_OFS) { 125237d1b14SEd Maste /* 126237d1b14SEd Maste * Directory entry for the root directory. There isn't one, 127237d1b14SEd Maste * so we manufacture one. We should probably rummage 128237d1b14SEd Maste * through the root directory and find a label entry (if it 129237d1b14SEd Maste * exists), and then use the time and date from that entry 130237d1b14SEd Maste * as the time and date for the root denode. 131237d1b14SEd Maste */ 132237d1b14SEd Maste ldep->de_vnode = (struct vnode *)-1; 133237d1b14SEd Maste 134237d1b14SEd Maste ldep->de_Attributes = ATTR_DIRECTORY; 13598dc8da5SEd Maste ldep->de_LowerCase = 0; 136237d1b14SEd Maste if (FAT32(pmp)) 137237d1b14SEd Maste ldep->de_StartCluster = pmp->pm_rootdirblk; 138237d1b14SEd Maste /* de_FileSize will be filled in further down */ 139237d1b14SEd Maste else { 140237d1b14SEd Maste ldep->de_StartCluster = MSDOSFSROOT; 14198dc8da5SEd Maste ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 142237d1b14SEd Maste } 143237d1b14SEd Maste /* 144237d1b14SEd Maste * fill in time and date so that dos2unixtime() doesn't 145237d1b14SEd Maste * spit up when called from msdosfs_getattr() with root 146237d1b14SEd Maste * denode 147237d1b14SEd Maste */ 148237d1b14SEd Maste ldep->de_CHun = 0; 149237d1b14SEd Maste ldep->de_CTime = 0x0000; /* 00:00:00 */ 150237d1b14SEd Maste ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 151237d1b14SEd Maste | (1 << DD_DAY_SHIFT); 152237d1b14SEd Maste /* Jan 1, 1980 */ 153237d1b14SEd Maste ldep->de_ADate = ldep->de_CDate; 154237d1b14SEd Maste ldep->de_MTime = ldep->de_CTime; 155237d1b14SEd Maste ldep->de_MDate = ldep->de_CDate; 156237d1b14SEd Maste /* leave the other fields as garbage */ 157237d1b14SEd Maste } else { 158*d485c77fSKonstantin Belousov error = m_readep(pmp, dirclust, diroffset, &bp, &direntptr); 159237d1b14SEd Maste if (error) { 160237d1b14SEd Maste ldep->de_Name[0] = SLOT_DELETED; 16198dc8da5SEd Maste 16298dc8da5SEd Maste *depp = NULL; 163237d1b14SEd Maste return (error); 164237d1b14SEd Maste } 16598dc8da5SEd Maste (void)DE_INTERNALIZE(ldep, direntptr); 1665b292f9aSEd Maste brelse(bp); 167237d1b14SEd Maste } 168237d1b14SEd Maste 169237d1b14SEd Maste /* 170237d1b14SEd Maste * Fill in a few fields of the vnode and finish filling in the 171237d1b14SEd Maste * denode. Then return the address of the found denode. 172237d1b14SEd Maste */ 173237d1b14SEd Maste if (ldep->de_Attributes & ATTR_DIRECTORY) { 174237d1b14SEd Maste /* 175237d1b14SEd Maste * Since DOS directory entries that describe directories 176237d1b14SEd Maste * have 0 in the filesize field, we take this opportunity 177237d1b14SEd Maste * to find out the length of the directory and plug it into 178237d1b14SEd Maste * the denode structure. 179237d1b14SEd Maste */ 180237d1b14SEd Maste u_long size; 181237d1b14SEd Maste 18298dc8da5SEd Maste /* 18398dc8da5SEd Maste * XXX it sometimes happens that the "." entry has cluster 18498dc8da5SEd Maste * number 0 when it shouldn't. Use the actual cluster number 18598dc8da5SEd Maste * instead of what is written in directory entry. 18698dc8da5SEd Maste */ 18798dc8da5SEd Maste if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 18898dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): \".\" entry at clust %lu != %lu\n", 18998dc8da5SEd Maste dirclust, ldep->de_StartCluster)); 19098dc8da5SEd Maste 19198dc8da5SEd Maste ldep->de_StartCluster = dirclust; 19298dc8da5SEd Maste } 19398dc8da5SEd Maste 194237d1b14SEd Maste if (ldep->de_StartCluster != MSDOSFSROOT) { 19598dc8da5SEd Maste error = pcbmap(ldep, 0xffff, 0, &size, 0); 196237d1b14SEd Maste if (error == E2BIG) { 197237d1b14SEd Maste ldep->de_FileSize = de_cn2off(pmp, size); 198237d1b14SEd Maste error = 0; 19998dc8da5SEd Maste } else { 20098dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): pcbmap returned %d\n", 20198dc8da5SEd Maste error)); 20298dc8da5SEd Maste } 203237d1b14SEd Maste } 204237d1b14SEd Maste } 205237d1b14SEd Maste *depp = ldep; 206237d1b14SEd Maste return (0); 207237d1b14SEd Maste } 208237d1b14SEd Maste 209237d1b14SEd Maste /* 210237d1b14SEd Maste * Truncate the file described by dep to the length specified by length. 211237d1b14SEd Maste */ 212237d1b14SEd Maste int 213476b0ab7SEd Maste detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred) 214237d1b14SEd Maste { 215237d1b14SEd Maste int error; 21698dc8da5SEd Maste int allerror; 217237d1b14SEd Maste u_long eofentry; 21898dc8da5SEd Maste u_long chaintofree; 21998dc8da5SEd Maste daddr_t bn; 220237d1b14SEd Maste int boff; 221237d1b14SEd Maste int isadir = dep->de_Attributes & ATTR_DIRECTORY; 222*d485c77fSKonstantin Belousov struct m_buf *bp; 223237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 224237d1b14SEd Maste 22598dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): file %s, length %lu, flags %x\n", 22698dc8da5SEd Maste dep->de_Name, length, flags)); 227237d1b14SEd Maste 228237d1b14SEd Maste /* 229237d1b14SEd Maste * Disallow attempts to truncate the root directory since it is of 230237d1b14SEd Maste * fixed size. That's just the way dos filesystems are. We use 231237d1b14SEd Maste * the VROOT bit in the vnode because checking for the directory 232237d1b14SEd Maste * bit and a startcluster of 0 in the denode is not adequate to 233237d1b14SEd Maste * recognize the root directory at this point in a file or 234237d1b14SEd Maste * directory's life. 235237d1b14SEd Maste */ 236237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp)) { 23798dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): can't truncate root directory, " 23898dc8da5SEd Maste "clust %ld, offset %ld\n", 23998dc8da5SEd Maste dep->de_dirclust, dep->de_diroffset)); 24098dc8da5SEd Maste 241237d1b14SEd Maste return (EINVAL); 242237d1b14SEd Maste } 243237d1b14SEd Maste 244237d1b14SEd Maste if (dep->de_FileSize < length) 245476b0ab7SEd Maste return deextend(dep, length, cred); 246237d1b14SEd Maste 247237d1b14SEd Maste /* 248237d1b14SEd Maste * If the desired length is 0 then remember the starting cluster of 249237d1b14SEd Maste * the file and set the StartCluster field in the directory entry 250237d1b14SEd Maste * to 0. If the desired length is not zero, then get the number of 251237d1b14SEd Maste * the last cluster in the shortened file. Then get the number of 252237d1b14SEd Maste * the first cluster in the part of the file that is to be freed. 253237d1b14SEd Maste * Then set the next cluster pointer in the last cluster of the 254237d1b14SEd Maste * file to CLUST_EOFE. 255237d1b14SEd Maste */ 256237d1b14SEd Maste if (length == 0) { 257237d1b14SEd Maste chaintofree = dep->de_StartCluster; 258237d1b14SEd Maste dep->de_StartCluster = 0; 259237d1b14SEd Maste eofentry = ~0; 260237d1b14SEd Maste } else { 26198dc8da5SEd Maste error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 26298dc8da5SEd Maste &eofentry, 0); 263237d1b14SEd Maste if (error) { 26498dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): pcbmap fails %d\n", 26598dc8da5SEd Maste error)); 2662037e988SEd Maste return (error); 267237d1b14SEd Maste } 268237d1b14SEd Maste } 269237d1b14SEd Maste 27098dc8da5SEd Maste fc_purge(dep, de_clcount(pmp, length)); 27198dc8da5SEd Maste 272237d1b14SEd Maste /* 273237d1b14SEd Maste * If the new length is not a multiple of the cluster size then we 274237d1b14SEd Maste * must zero the tail end of the new last cluster in case it 275237d1b14SEd Maste * becomes part of the file again because of a seek. 276237d1b14SEd Maste */ 277237d1b14SEd Maste if ((boff = length & pmp->pm_crbomask) != 0) { 278237d1b14SEd Maste if (isadir) { 279237d1b14SEd Maste bn = cntobn(pmp, eofentry); 280*d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, 281*d485c77fSKonstantin Belousov pmp->pm_bpcluster, 0, &bp); 282237d1b14SEd Maste if (error) { 28398dc8da5SEd Maste brelse(bp); 28498dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): bread fails %d\n", 28598dc8da5SEd Maste error)); 28698dc8da5SEd Maste 2872037e988SEd Maste return (error); 288237d1b14SEd Maste } 2892037e988SEd Maste memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff); 290237d1b14SEd Maste bwrite(bp); 291237d1b14SEd Maste } 292237d1b14SEd Maste } 293237d1b14SEd Maste 294237d1b14SEd Maste /* 295237d1b14SEd Maste * Write out the updated directory entry. Even if the update fails 296237d1b14SEd Maste * we free the trailing clusters. 297237d1b14SEd Maste */ 298237d1b14SEd Maste dep->de_FileSize = length; 299237d1b14SEd Maste if (!isadir) 300237d1b14SEd Maste dep->de_flag |= DE_UPDATE|DE_MODIFIED; 30198dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): allerror %d, eofentry %lu\n", 30298dc8da5SEd Maste allerror, eofentry)); 303237d1b14SEd Maste 304237d1b14SEd Maste /* 305237d1b14SEd Maste * If we need to break the cluster chain for the file then do it 306237d1b14SEd Maste * now. 307237d1b14SEd Maste */ 30898dc8da5SEd Maste if (eofentry != ~0) { 309237d1b14SEd Maste error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 310237d1b14SEd Maste &chaintofree, CLUST_EOFE); 311237d1b14SEd Maste if (error) { 31298dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): fatentry errors %d\n", 31398dc8da5SEd Maste error)); 314237d1b14SEd Maste return (error); 315237d1b14SEd Maste } 31698dc8da5SEd Maste fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 31798dc8da5SEd Maste eofentry); 318237d1b14SEd Maste } 319237d1b14SEd Maste 320237d1b14SEd Maste /* 321237d1b14SEd Maste * Now free the clusters removed from the file because of the 322237d1b14SEd Maste * truncation. 323237d1b14SEd Maste */ 32498dc8da5SEd Maste if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 325237d1b14SEd Maste freeclusterchain(pmp, chaintofree); 326237d1b14SEd Maste 3272037e988SEd Maste return (allerror); 328237d1b14SEd Maste } 329237d1b14SEd Maste 330237d1b14SEd Maste /* 331237d1b14SEd Maste * Extend the file described by dep to length specified by length. 332237d1b14SEd Maste */ 333237d1b14SEd Maste int 334476b0ab7SEd Maste deextend(struct denode *dep, u_long length, struct ucred *cred) 335237d1b14SEd Maste { 336237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 337237d1b14SEd Maste u_long count; 338237d1b14SEd Maste int error; 339237d1b14SEd Maste 340237d1b14SEd Maste /* 341237d1b14SEd Maste * The root of a DOS filesystem cannot be extended. 342237d1b14SEd Maste */ 343237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp)) 3442037e988SEd Maste return (EINVAL); 345237d1b14SEd Maste 346237d1b14SEd Maste /* 347237d1b14SEd Maste * Directories cannot be extended. 348237d1b14SEd Maste */ 349237d1b14SEd Maste if (dep->de_Attributes & ATTR_DIRECTORY) 3502037e988SEd Maste return (EISDIR); 351237d1b14SEd Maste 352237d1b14SEd Maste if (length <= dep->de_FileSize) 3532037e988SEd Maste return (E2BIG); 354237d1b14SEd Maste 355237d1b14SEd Maste /* 356237d1b14SEd Maste * Compute the number of clusters to allocate. 357237d1b14SEd Maste */ 358237d1b14SEd Maste count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 359237d1b14SEd Maste if (count > 0) { 360237d1b14SEd Maste if (count > pmp->pm_freeclustercount) 361237d1b14SEd Maste return (ENOSPC); 362*d485c77fSKonstantin Belousov error = m_extendfile(dep, count, NULL, NULL, DE_CLEAR); 363237d1b14SEd Maste if (error) { 364237d1b14SEd Maste /* truncate the added clusters away again */ 365476b0ab7SEd Maste (void) detrunc(dep, dep->de_FileSize, 0, cred); 366237d1b14SEd Maste return (error); 367237d1b14SEd Maste } 368237d1b14SEd Maste } 369237d1b14SEd Maste 370237d1b14SEd Maste /* 371237d1b14SEd Maste * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a 372237d1b14SEd Maste * memset(); we set the write size so ubc won't read in file data that 373237d1b14SEd Maste * is zero'd later. 374237d1b14SEd Maste */ 375237d1b14SEd Maste dep->de_FileSize = length; 376237d1b14SEd Maste dep->de_flag |= DE_UPDATE | DE_MODIFIED; 377237d1b14SEd Maste return 0; 378237d1b14SEd Maste } 379