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> 65*ba2cfa80SAlex Richardson #include "msdos/denode.h" 6651e79affSEd Maste #include <fs/msdosfs/fat.h> 67840aca28SEd Maste #include <fs/msdosfs/msdosfsmount.h> 68237d1b14SEd Maste 6998dc8da5SEd Maste #include "makefs.h" 7098dc8da5SEd Maste #include "msdos.h" 7198dc8da5SEd Maste 72237d1b14SEd Maste 73237d1b14SEd Maste /* 74237d1b14SEd Maste * If deget() succeeds it returns with the gotten denode locked(). 75237d1b14SEd Maste * 76237d1b14SEd Maste * pmp - address of msdosfsmount structure of the filesystem containing 77237d1b14SEd Maste * the denode of interest. The pm_dev field and the address of 78237d1b14SEd Maste * the msdosfsmount structure are used. 79237d1b14SEd Maste * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 80237d1b14SEd Maste * diroffset is relative to the beginning of the root directory, 81237d1b14SEd Maste * otherwise it is cluster relative. 82237d1b14SEd Maste * diroffset - offset past begin of cluster of denode we want 83237d1b14SEd Maste * depp - returns the address of the gotten denode. 84237d1b14SEd Maste */ 85237d1b14SEd Maste int 86237d1b14SEd Maste deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 87237d1b14SEd Maste struct denode **depp) 88237d1b14SEd Maste { 89237d1b14SEd Maste int error; 9098dc8da5SEd Maste uint64_t inode; 91237d1b14SEd Maste struct direntry *direntptr; 92237d1b14SEd Maste struct denode *ldep; 93d485c77fSKonstantin Belousov struct m_buf *bp; 94237d1b14SEd Maste 9598dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 9698dc8da5SEd Maste pmp, dirclust, diroffset, depp)); 97237d1b14SEd Maste 98237d1b14SEd Maste /* 99237d1b14SEd Maste * On FAT32 filesystems, root is a (more or less) normal 100237d1b14SEd Maste * directory 101237d1b14SEd Maste */ 102237d1b14SEd Maste if (FAT32(pmp) && dirclust == MSDOSFSROOT) 103237d1b14SEd Maste dirclust = pmp->pm_rootdirblk; 104237d1b14SEd Maste 10598dc8da5SEd Maste inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 10698dc8da5SEd Maste 107237d1b14SEd Maste ldep = ecalloc(1, sizeof(*ldep)); 108237d1b14SEd Maste ldep->de_vnode = NULL; 109237d1b14SEd Maste ldep->de_flag = 0; 110237d1b14SEd Maste ldep->de_dirclust = dirclust; 111237d1b14SEd Maste ldep->de_diroffset = diroffset; 11298dc8da5SEd Maste ldep->de_inode = inode; 113237d1b14SEd Maste ldep->de_pmp = pmp; 114237d1b14SEd Maste ldep->de_refcnt = 1; 11598dc8da5SEd Maste fc_purge(ldep, 0); /* init the FAT cache for this denode */ 116237d1b14SEd Maste /* 117237d1b14SEd Maste * Copy the directory entry into the denode area of the vnode. 118237d1b14SEd Maste */ 119237d1b14SEd Maste if ((dirclust == MSDOSFSROOT 120237d1b14SEd Maste || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 121237d1b14SEd Maste && diroffset == MSDOSFSROOT_OFS) { 122237d1b14SEd Maste /* 123237d1b14SEd Maste * Directory entry for the root directory. There isn't one, 124237d1b14SEd Maste * so we manufacture one. We should probably rummage 125237d1b14SEd Maste * through the root directory and find a label entry (if it 126237d1b14SEd Maste * exists), and then use the time and date from that entry 127237d1b14SEd Maste * as the time and date for the root denode. 128237d1b14SEd Maste */ 129237d1b14SEd Maste ldep->de_vnode = (struct vnode *)-1; 130237d1b14SEd Maste 131237d1b14SEd Maste ldep->de_Attributes = ATTR_DIRECTORY; 13298dc8da5SEd Maste ldep->de_LowerCase = 0; 133237d1b14SEd Maste if (FAT32(pmp)) 134237d1b14SEd Maste ldep->de_StartCluster = pmp->pm_rootdirblk; 135237d1b14SEd Maste /* de_FileSize will be filled in further down */ 136237d1b14SEd Maste else { 137237d1b14SEd Maste ldep->de_StartCluster = MSDOSFSROOT; 13898dc8da5SEd Maste ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 139237d1b14SEd Maste } 140237d1b14SEd Maste /* 141237d1b14SEd Maste * fill in time and date so that dos2unixtime() doesn't 142237d1b14SEd Maste * spit up when called from msdosfs_getattr() with root 143237d1b14SEd Maste * denode 144237d1b14SEd Maste */ 145237d1b14SEd Maste ldep->de_CHun = 0; 146237d1b14SEd Maste ldep->de_CTime = 0x0000; /* 00:00:00 */ 147237d1b14SEd Maste ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 148237d1b14SEd Maste | (1 << DD_DAY_SHIFT); 149237d1b14SEd Maste /* Jan 1, 1980 */ 150237d1b14SEd Maste ldep->de_ADate = ldep->de_CDate; 151237d1b14SEd Maste ldep->de_MTime = ldep->de_CTime; 152237d1b14SEd Maste ldep->de_MDate = ldep->de_CDate; 153237d1b14SEd Maste /* leave the other fields as garbage */ 154237d1b14SEd Maste } else { 155d485c77fSKonstantin Belousov error = m_readep(pmp, dirclust, diroffset, &bp, &direntptr); 156237d1b14SEd Maste if (error) { 157237d1b14SEd Maste ldep->de_Name[0] = SLOT_DELETED; 15898dc8da5SEd Maste 15998dc8da5SEd Maste *depp = NULL; 160237d1b14SEd Maste return (error); 161237d1b14SEd Maste } 16298dc8da5SEd Maste (void)DE_INTERNALIZE(ldep, direntptr); 1635b292f9aSEd Maste brelse(bp); 164237d1b14SEd Maste } 165237d1b14SEd Maste 166237d1b14SEd Maste /* 167237d1b14SEd Maste * Fill in a few fields of the vnode and finish filling in the 168237d1b14SEd Maste * denode. Then return the address of the found denode. 169237d1b14SEd Maste */ 170237d1b14SEd Maste if (ldep->de_Attributes & ATTR_DIRECTORY) { 171237d1b14SEd Maste /* 172237d1b14SEd Maste * Since DOS directory entries that describe directories 173237d1b14SEd Maste * have 0 in the filesize field, we take this opportunity 174237d1b14SEd Maste * to find out the length of the directory and plug it into 175237d1b14SEd Maste * the denode structure. 176237d1b14SEd Maste */ 177237d1b14SEd Maste u_long size; 178237d1b14SEd Maste 17998dc8da5SEd Maste /* 18098dc8da5SEd Maste * XXX it sometimes happens that the "." entry has cluster 18198dc8da5SEd Maste * number 0 when it shouldn't. Use the actual cluster number 18298dc8da5SEd Maste * instead of what is written in directory entry. 18398dc8da5SEd Maste */ 18498dc8da5SEd Maste if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 18598dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): \".\" entry at clust %lu != %lu\n", 18698dc8da5SEd Maste dirclust, ldep->de_StartCluster)); 18798dc8da5SEd Maste 18898dc8da5SEd Maste ldep->de_StartCluster = dirclust; 18998dc8da5SEd Maste } 19098dc8da5SEd Maste 191237d1b14SEd Maste if (ldep->de_StartCluster != MSDOSFSROOT) { 19298dc8da5SEd Maste error = pcbmap(ldep, 0xffff, 0, &size, 0); 193237d1b14SEd Maste if (error == E2BIG) { 194237d1b14SEd Maste ldep->de_FileSize = de_cn2off(pmp, size); 195237d1b14SEd Maste error = 0; 19698dc8da5SEd Maste } else { 19798dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): pcbmap returned %d\n", 19898dc8da5SEd Maste error)); 19998dc8da5SEd Maste } 200237d1b14SEd Maste } 201237d1b14SEd Maste } 202237d1b14SEd Maste *depp = ldep; 203237d1b14SEd Maste return (0); 204237d1b14SEd Maste } 205237d1b14SEd Maste 206237d1b14SEd Maste /* 207237d1b14SEd Maste * Truncate the file described by dep to the length specified by length. 208237d1b14SEd Maste */ 209237d1b14SEd Maste int 210476b0ab7SEd Maste detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred) 211237d1b14SEd Maste { 212237d1b14SEd Maste int error; 21398dc8da5SEd Maste int allerror; 214237d1b14SEd Maste u_long eofentry; 21598dc8da5SEd Maste u_long chaintofree; 21698dc8da5SEd Maste daddr_t bn; 217237d1b14SEd Maste int boff; 218237d1b14SEd Maste int isadir = dep->de_Attributes & ATTR_DIRECTORY; 219d485c77fSKonstantin Belousov struct m_buf *bp; 220237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 221237d1b14SEd Maste 22298dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): file %s, length %lu, flags %x\n", 22398dc8da5SEd Maste dep->de_Name, length, flags)); 224237d1b14SEd Maste 225237d1b14SEd Maste /* 226237d1b14SEd Maste * Disallow attempts to truncate the root directory since it is of 227237d1b14SEd Maste * fixed size. That's just the way dos filesystems are. We use 228237d1b14SEd Maste * the VROOT bit in the vnode because checking for the directory 229237d1b14SEd Maste * bit and a startcluster of 0 in the denode is not adequate to 230237d1b14SEd Maste * recognize the root directory at this point in a file or 231237d1b14SEd Maste * directory's life. 232237d1b14SEd Maste */ 233237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp)) { 23498dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): can't truncate root directory, " 23598dc8da5SEd Maste "clust %ld, offset %ld\n", 23698dc8da5SEd Maste dep->de_dirclust, dep->de_diroffset)); 23798dc8da5SEd Maste 238237d1b14SEd Maste return (EINVAL); 239237d1b14SEd Maste } 240237d1b14SEd Maste 241237d1b14SEd Maste if (dep->de_FileSize < length) 242476b0ab7SEd Maste return deextend(dep, length, cred); 243237d1b14SEd Maste 244237d1b14SEd Maste /* 245237d1b14SEd Maste * If the desired length is 0 then remember the starting cluster of 246237d1b14SEd Maste * the file and set the StartCluster field in the directory entry 247237d1b14SEd Maste * to 0. If the desired length is not zero, then get the number of 248237d1b14SEd Maste * the last cluster in the shortened file. Then get the number of 249237d1b14SEd Maste * the first cluster in the part of the file that is to be freed. 250237d1b14SEd Maste * Then set the next cluster pointer in the last cluster of the 251237d1b14SEd Maste * file to CLUST_EOFE. 252237d1b14SEd Maste */ 253237d1b14SEd Maste if (length == 0) { 254237d1b14SEd Maste chaintofree = dep->de_StartCluster; 255237d1b14SEd Maste dep->de_StartCluster = 0; 256237d1b14SEd Maste eofentry = ~0; 257237d1b14SEd Maste } else { 25898dc8da5SEd Maste error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 25998dc8da5SEd Maste &eofentry, 0); 260237d1b14SEd Maste if (error) { 26198dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): pcbmap fails %d\n", 26298dc8da5SEd Maste error)); 2632037e988SEd Maste return (error); 264237d1b14SEd Maste } 265237d1b14SEd Maste } 266237d1b14SEd Maste 26798dc8da5SEd Maste fc_purge(dep, de_clcount(pmp, length)); 26898dc8da5SEd Maste 269237d1b14SEd Maste /* 270237d1b14SEd Maste * If the new length is not a multiple of the cluster size then we 271237d1b14SEd Maste * must zero the tail end of the new last cluster in case it 272237d1b14SEd Maste * becomes part of the file again because of a seek. 273237d1b14SEd Maste */ 274237d1b14SEd Maste if ((boff = length & pmp->pm_crbomask) != 0) { 275237d1b14SEd Maste if (isadir) { 276237d1b14SEd Maste bn = cntobn(pmp, eofentry); 277d485c77fSKonstantin Belousov error = bread((void *)pmp->pm_devvp, bn, 278d485c77fSKonstantin Belousov pmp->pm_bpcluster, 0, &bp); 279237d1b14SEd Maste if (error) { 28098dc8da5SEd Maste brelse(bp); 28198dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): bread fails %d\n", 28298dc8da5SEd Maste error)); 28398dc8da5SEd Maste 2842037e988SEd Maste return (error); 285237d1b14SEd Maste } 2862037e988SEd Maste memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff); 287237d1b14SEd Maste bwrite(bp); 288237d1b14SEd Maste } 289237d1b14SEd Maste } 290237d1b14SEd Maste 291237d1b14SEd Maste /* 292237d1b14SEd Maste * Write out the updated directory entry. Even if the update fails 293237d1b14SEd Maste * we free the trailing clusters. 294237d1b14SEd Maste */ 295237d1b14SEd Maste dep->de_FileSize = length; 296237d1b14SEd Maste if (!isadir) 297237d1b14SEd Maste dep->de_flag |= DE_UPDATE|DE_MODIFIED; 29898dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): allerror %d, eofentry %lu\n", 29998dc8da5SEd Maste allerror, eofentry)); 300237d1b14SEd Maste 301237d1b14SEd Maste /* 302237d1b14SEd Maste * If we need to break the cluster chain for the file then do it 303237d1b14SEd Maste * now. 304237d1b14SEd Maste */ 30598dc8da5SEd Maste if (eofentry != ~0) { 306237d1b14SEd Maste error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 307237d1b14SEd Maste &chaintofree, CLUST_EOFE); 308237d1b14SEd Maste if (error) { 30998dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): fatentry errors %d\n", 31098dc8da5SEd Maste error)); 311237d1b14SEd Maste return (error); 312237d1b14SEd Maste } 31398dc8da5SEd Maste fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 31498dc8da5SEd Maste eofentry); 315237d1b14SEd Maste } 316237d1b14SEd Maste 317237d1b14SEd Maste /* 318237d1b14SEd Maste * Now free the clusters removed from the file because of the 319237d1b14SEd Maste * truncation. 320237d1b14SEd Maste */ 32198dc8da5SEd Maste if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 322237d1b14SEd Maste freeclusterchain(pmp, chaintofree); 323237d1b14SEd Maste 3242037e988SEd Maste return (allerror); 325237d1b14SEd Maste } 326237d1b14SEd Maste 327237d1b14SEd Maste /* 328237d1b14SEd Maste * Extend the file described by dep to length specified by length. 329237d1b14SEd Maste */ 330237d1b14SEd Maste int 331476b0ab7SEd Maste deextend(struct denode *dep, u_long length, struct ucred *cred) 332237d1b14SEd Maste { 333237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 334237d1b14SEd Maste u_long count; 335237d1b14SEd Maste int error; 336237d1b14SEd Maste 337237d1b14SEd Maste /* 338237d1b14SEd Maste * The root of a DOS filesystem cannot be extended. 339237d1b14SEd Maste */ 340237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp)) 3412037e988SEd Maste return (EINVAL); 342237d1b14SEd Maste 343237d1b14SEd Maste /* 344237d1b14SEd Maste * Directories cannot be extended. 345237d1b14SEd Maste */ 346237d1b14SEd Maste if (dep->de_Attributes & ATTR_DIRECTORY) 3472037e988SEd Maste return (EISDIR); 348237d1b14SEd Maste 349237d1b14SEd Maste if (length <= dep->de_FileSize) 3502037e988SEd Maste return (E2BIG); 351237d1b14SEd Maste 352237d1b14SEd Maste /* 353237d1b14SEd Maste * Compute the number of clusters to allocate. 354237d1b14SEd Maste */ 355237d1b14SEd Maste count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 356237d1b14SEd Maste if (count > 0) { 357237d1b14SEd Maste if (count > pmp->pm_freeclustercount) 358237d1b14SEd Maste return (ENOSPC); 359d485c77fSKonstantin Belousov error = m_extendfile(dep, count, NULL, NULL, DE_CLEAR); 360237d1b14SEd Maste if (error) { 361237d1b14SEd Maste /* truncate the added clusters away again */ 362476b0ab7SEd Maste (void) detrunc(dep, dep->de_FileSize, 0, cred); 363237d1b14SEd Maste return (error); 364237d1b14SEd Maste } 365237d1b14SEd Maste } 366237d1b14SEd Maste 367237d1b14SEd Maste /* 368237d1b14SEd Maste * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a 369237d1b14SEd Maste * memset(); we set the write size so ubc won't read in file data that 370237d1b14SEd Maste * is zero'd later. 371237d1b14SEd Maste */ 372237d1b14SEd Maste dep->de_FileSize = length; 373237d1b14SEd Maste dep->de_flag |= DE_UPDATE | DE_MODIFIED; 374237d1b14SEd Maste return 0; 375237d1b14SEd Maste } 376