1237d1b14SEd Maste /* $NetBSD: msdosfs_denode.c,v 1.7 2015/03/29 05:52:59 agc Exp $ */ 2237d1b14SEd Maste 3237d1b14SEd Maste /*- 4237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 5237d1b14SEd Maste * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 6237d1b14SEd Maste * All rights reserved. 7237d1b14SEd Maste * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 8237d1b14SEd Maste * 9237d1b14SEd Maste * Redistribution and use in source and binary forms, with or without 10237d1b14SEd Maste * modification, are permitted provided that the following conditions 11237d1b14SEd Maste * are met: 12237d1b14SEd Maste * 1. Redistributions of source code must retain the above copyright 13237d1b14SEd Maste * notice, this list of conditions and the following disclaimer. 14237d1b14SEd Maste * 2. Redistributions in binary form must reproduce the above copyright 15237d1b14SEd Maste * notice, this list of conditions and the following disclaimer in the 16237d1b14SEd Maste * documentation and/or other materials provided with the distribution. 17237d1b14SEd Maste * 3. All advertising materials mentioning features or use of this software 18237d1b14SEd Maste * must display the following acknowledgement: 19237d1b14SEd Maste * This product includes software developed by TooLs GmbH. 20237d1b14SEd Maste * 4. The name of TooLs GmbH may not be used to endorse or promote products 21237d1b14SEd Maste * derived from this software without specific prior written permission. 22237d1b14SEd Maste * 23237d1b14SEd Maste * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 24237d1b14SEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25237d1b14SEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26237d1b14SEd Maste * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27237d1b14SEd Maste * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28237d1b14SEd Maste * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29237d1b14SEd Maste * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30237d1b14SEd Maste * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31237d1b14SEd Maste * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32237d1b14SEd Maste * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33237d1b14SEd Maste */ 34237d1b14SEd Maste /* 35237d1b14SEd Maste * Written by Paul Popelka (paulp@uts.amdahl.com) 36237d1b14SEd Maste * 37237d1b14SEd Maste * You can do anything you want with this software, just don't say you wrote 38237d1b14SEd Maste * it, and don't remove this notice. 39237d1b14SEd Maste * 40237d1b14SEd Maste * This software is provided "as is". 41237d1b14SEd Maste * 42237d1b14SEd Maste * The author supplies this software to be publicly redistributed on the 43237d1b14SEd Maste * understanding that the author is not responsible for the correct 44237d1b14SEd Maste * functioning of this software in any circumstances and is not liable for 45237d1b14SEd Maste * any damages caused by this software. 46237d1b14SEd Maste * 47237d1b14SEd Maste * October 1992 48237d1b14SEd Maste */ 49237d1b14SEd Maste 50237d1b14SEd Maste #include <sys/cdefs.h> 51237d1b14SEd Maste __FBSDID("$FreeBSD$"); 52237d1b14SEd Maste 53237d1b14SEd Maste #include <sys/param.h> 54*98dc8da5SEd Maste #include <sys/errno.h> 55*98dc8da5SEd Maste #include <sys/vnode.h> 56237d1b14SEd Maste 57*98dc8da5SEd Maste #include <stdio.h> 58*98dc8da5SEd Maste #include <string.h> 59*98dc8da5SEd Maste #include <stdlib.h> 60*98dc8da5SEd Maste #include <util.h> 61237d1b14SEd Maste 62237d1b14SEd Maste #include <fs/msdosfs/bpb.h> 63237d1b14SEd Maste 64*98dc8da5SEd Maste #include "makefs.h" 65*98dc8da5SEd Maste #include "msdos.h" 66*98dc8da5SEd Maste 67*98dc8da5SEd Maste #include "ffs/buf.h" 68*98dc8da5SEd Maste 69*98dc8da5SEd Maste #include "msdos/denode.h" 70*98dc8da5SEd Maste #include "msdos/direntry.h" 71*98dc8da5SEd Maste #include "msdos/fat.h" 72*98dc8da5SEd Maste #include "msdos/msdosfsmount.h" 73237d1b14SEd Maste 74237d1b14SEd Maste /* 75237d1b14SEd Maste * If deget() succeeds it returns with the gotten denode locked(). 76237d1b14SEd Maste * 77237d1b14SEd Maste * pmp - address of msdosfsmount structure of the filesystem containing 78237d1b14SEd Maste * the denode of interest. The pm_dev field and the address of 79237d1b14SEd Maste * the msdosfsmount structure are used. 80237d1b14SEd Maste * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 81237d1b14SEd Maste * diroffset is relative to the beginning of the root directory, 82237d1b14SEd Maste * otherwise it is cluster relative. 83237d1b14SEd Maste * diroffset - offset past begin of cluster of denode we want 84237d1b14SEd Maste * depp - returns the address of the gotten denode. 85237d1b14SEd Maste */ 86237d1b14SEd Maste int 87237d1b14SEd Maste deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 88237d1b14SEd Maste struct denode **depp) 89237d1b14SEd Maste { 90237d1b14SEd Maste int error; 91*98dc8da5SEd Maste uint64_t inode; 92237d1b14SEd Maste struct direntry *direntptr; 93237d1b14SEd Maste struct denode *ldep; 94237d1b14SEd Maste struct buf *bp; 95237d1b14SEd Maste 96*98dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 97*98dc8da5SEd Maste pmp, dirclust, diroffset, depp)); 98237d1b14SEd Maste 99237d1b14SEd Maste /* 100237d1b14SEd Maste * On FAT32 filesystems, root is a (more or less) normal 101237d1b14SEd Maste * directory 102237d1b14SEd Maste */ 103237d1b14SEd Maste if (FAT32(pmp) && dirclust == MSDOSFSROOT) 104237d1b14SEd Maste dirclust = pmp->pm_rootdirblk; 105237d1b14SEd Maste 106*98dc8da5SEd Maste inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 107*98dc8da5SEd Maste 108237d1b14SEd Maste ldep = ecalloc(1, sizeof(*ldep)); 109237d1b14SEd Maste ldep->de_vnode = NULL; 110237d1b14SEd Maste ldep->de_flag = 0; 111237d1b14SEd Maste ldep->de_dirclust = dirclust; 112237d1b14SEd Maste ldep->de_diroffset = diroffset; 113*98dc8da5SEd Maste ldep->de_inode = inode; 114237d1b14SEd Maste ldep->de_pmp = pmp; 115237d1b14SEd Maste ldep->de_refcnt = 1; 116*98dc8da5SEd Maste fc_purge(ldep, 0); /* init the FAT cache for this denode */ 117237d1b14SEd Maste /* 118237d1b14SEd Maste * Copy the directory entry into the denode area of the vnode. 119237d1b14SEd Maste */ 120237d1b14SEd Maste if ((dirclust == MSDOSFSROOT 121237d1b14SEd Maste || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 122237d1b14SEd Maste && diroffset == MSDOSFSROOT_OFS) { 123237d1b14SEd Maste /* 124237d1b14SEd Maste * Directory entry for the root directory. There isn't one, 125237d1b14SEd Maste * so we manufacture one. We should probably rummage 126237d1b14SEd Maste * through the root directory and find a label entry (if it 127237d1b14SEd Maste * exists), and then use the time and date from that entry 128237d1b14SEd Maste * as the time and date for the root denode. 129237d1b14SEd Maste */ 130237d1b14SEd Maste ldep->de_vnode = (struct vnode *)-1; 131237d1b14SEd Maste 132237d1b14SEd Maste ldep->de_Attributes = ATTR_DIRECTORY; 133*98dc8da5SEd Maste ldep->de_LowerCase = 0; 134237d1b14SEd Maste if (FAT32(pmp)) 135237d1b14SEd Maste ldep->de_StartCluster = pmp->pm_rootdirblk; 136237d1b14SEd Maste /* de_FileSize will be filled in further down */ 137237d1b14SEd Maste else { 138237d1b14SEd Maste ldep->de_StartCluster = MSDOSFSROOT; 139*98dc8da5SEd Maste ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 140237d1b14SEd Maste } 141237d1b14SEd Maste /* 142237d1b14SEd Maste * fill in time and date so that dos2unixtime() doesn't 143237d1b14SEd Maste * spit up when called from msdosfs_getattr() with root 144237d1b14SEd Maste * denode 145237d1b14SEd Maste */ 146237d1b14SEd Maste ldep->de_CHun = 0; 147237d1b14SEd Maste ldep->de_CTime = 0x0000; /* 00:00:00 */ 148237d1b14SEd Maste ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 149237d1b14SEd Maste | (1 << DD_DAY_SHIFT); 150237d1b14SEd Maste /* Jan 1, 1980 */ 151237d1b14SEd Maste ldep->de_ADate = ldep->de_CDate; 152237d1b14SEd Maste ldep->de_MTime = ldep->de_CTime; 153237d1b14SEd Maste ldep->de_MDate = ldep->de_CDate; 154237d1b14SEd Maste /* leave the other fields as garbage */ 155237d1b14SEd Maste } else { 156237d1b14SEd Maste error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 157237d1b14SEd Maste if (error) { 158237d1b14SEd Maste ldep->de_Name[0] = SLOT_DELETED; 159*98dc8da5SEd Maste 160*98dc8da5SEd Maste *depp = NULL; 161237d1b14SEd Maste return (error); 162237d1b14SEd Maste } 163*98dc8da5SEd Maste (void)DE_INTERNALIZE(ldep, direntptr); 1645b292f9aSEd Maste brelse(bp); 165237d1b14SEd Maste } 166237d1b14SEd Maste 167237d1b14SEd Maste /* 168237d1b14SEd Maste * Fill in a few fields of the vnode and finish filling in the 169237d1b14SEd Maste * denode. Then return the address of the found denode. 170237d1b14SEd Maste */ 171237d1b14SEd Maste if (ldep->de_Attributes & ATTR_DIRECTORY) { 172237d1b14SEd Maste /* 173237d1b14SEd Maste * Since DOS directory entries that describe directories 174237d1b14SEd Maste * have 0 in the filesize field, we take this opportunity 175237d1b14SEd Maste * to find out the length of the directory and plug it into 176237d1b14SEd Maste * the denode structure. 177237d1b14SEd Maste */ 178237d1b14SEd Maste u_long size; 179237d1b14SEd Maste 180*98dc8da5SEd Maste /* 181*98dc8da5SEd Maste * XXX it sometimes happens that the "." entry has cluster 182*98dc8da5SEd Maste * number 0 when it shouldn't. Use the actual cluster number 183*98dc8da5SEd Maste * instead of what is written in directory entry. 184*98dc8da5SEd Maste */ 185*98dc8da5SEd Maste if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 186*98dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): \".\" entry at clust %lu != %lu\n", 187*98dc8da5SEd Maste dirclust, ldep->de_StartCluster)); 188*98dc8da5SEd Maste 189*98dc8da5SEd Maste ldep->de_StartCluster = dirclust; 190*98dc8da5SEd Maste } 191*98dc8da5SEd Maste 192237d1b14SEd Maste if (ldep->de_StartCluster != MSDOSFSROOT) { 193*98dc8da5SEd Maste error = pcbmap(ldep, 0xffff, 0, &size, 0); 194237d1b14SEd Maste if (error == E2BIG) { 195237d1b14SEd Maste ldep->de_FileSize = de_cn2off(pmp, size); 196237d1b14SEd Maste error = 0; 197*98dc8da5SEd Maste } else { 198*98dc8da5SEd Maste MSDOSFS_DPRINTF(("deget(): pcbmap returned %d\n", 199*98dc8da5SEd Maste error)); 200*98dc8da5SEd Maste } 201237d1b14SEd Maste } 202237d1b14SEd Maste } 203237d1b14SEd Maste *depp = ldep; 204237d1b14SEd Maste return (0); 205237d1b14SEd Maste } 206237d1b14SEd Maste 207237d1b14SEd Maste /* 208237d1b14SEd Maste * Truncate the file described by dep to the length specified by length. 209237d1b14SEd Maste */ 210237d1b14SEd Maste int 211*98dc8da5SEd Maste detrunc(struct denode *dep, u_long length, int flags) 212237d1b14SEd Maste { 213237d1b14SEd Maste int error; 214*98dc8da5SEd Maste int allerror; 215237d1b14SEd Maste u_long eofentry; 216*98dc8da5SEd Maste u_long chaintofree; 217*98dc8da5SEd Maste daddr_t bn; 218237d1b14SEd Maste int boff; 219237d1b14SEd Maste int isadir = dep->de_Attributes & ATTR_DIRECTORY; 220237d1b14SEd Maste struct buf *bp; 221237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 222237d1b14SEd Maste 223*98dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): file %s, length %lu, flags %x\n", 224*98dc8da5SEd Maste dep->de_Name, length, flags)); 225237d1b14SEd Maste 226237d1b14SEd Maste /* 227237d1b14SEd Maste * Disallow attempts to truncate the root directory since it is of 228237d1b14SEd Maste * fixed size. That's just the way dos filesystems are. We use 229237d1b14SEd Maste * the VROOT bit in the vnode because checking for the directory 230237d1b14SEd Maste * bit and a startcluster of 0 in the denode is not adequate to 231237d1b14SEd Maste * recognize the root directory at this point in a file or 232237d1b14SEd Maste * directory's life. 233237d1b14SEd Maste */ 234237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp)) { 235*98dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): can't truncate root directory, " 236*98dc8da5SEd Maste "clust %ld, offset %ld\n", 237*98dc8da5SEd Maste dep->de_dirclust, dep->de_diroffset)); 238*98dc8da5SEd Maste 239237d1b14SEd Maste return (EINVAL); 240237d1b14SEd Maste } 241237d1b14SEd Maste 242237d1b14SEd Maste if (dep->de_FileSize < length) 243*98dc8da5SEd Maste return deextend(dep, length); 244237d1b14SEd Maste 245237d1b14SEd Maste /* 246237d1b14SEd Maste * If the desired length is 0 then remember the starting cluster of 247237d1b14SEd Maste * the file and set the StartCluster field in the directory entry 248237d1b14SEd Maste * to 0. If the desired length is not zero, then get the number of 249237d1b14SEd Maste * the last cluster in the shortened file. Then get the number of 250237d1b14SEd Maste * the first cluster in the part of the file that is to be freed. 251237d1b14SEd Maste * Then set the next cluster pointer in the last cluster of the 252237d1b14SEd Maste * file to CLUST_EOFE. 253237d1b14SEd Maste */ 254237d1b14SEd Maste if (length == 0) { 255237d1b14SEd Maste chaintofree = dep->de_StartCluster; 256237d1b14SEd Maste dep->de_StartCluster = 0; 257237d1b14SEd Maste eofentry = ~0; 258237d1b14SEd Maste } else { 259*98dc8da5SEd Maste error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 260*98dc8da5SEd Maste &eofentry, 0); 261237d1b14SEd Maste if (error) { 262*98dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): pcbmap fails %d\n", 263*98dc8da5SEd Maste error)); 264*98dc8da5SEd Maste return error; 265237d1b14SEd Maste } 266237d1b14SEd Maste } 267237d1b14SEd Maste 268*98dc8da5SEd Maste fc_purge(dep, de_clcount(pmp, length)); 269*98dc8da5SEd Maste 270237d1b14SEd Maste /* 271237d1b14SEd Maste * If the new length is not a multiple of the cluster size then we 272237d1b14SEd Maste * must zero the tail end of the new last cluster in case it 273237d1b14SEd Maste * becomes part of the file again because of a seek. 274237d1b14SEd Maste */ 275237d1b14SEd Maste if ((boff = length & pmp->pm_crbomask) != 0) { 276237d1b14SEd Maste if (isadir) { 277237d1b14SEd Maste bn = cntobn(pmp, eofentry); 278*98dc8da5SEd Maste error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 279*98dc8da5SEd Maste 0, &bp); 280237d1b14SEd Maste if (error) { 281*98dc8da5SEd Maste brelse(bp); 282*98dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): bread fails %d\n", 283*98dc8da5SEd Maste error)); 284*98dc8da5SEd Maste 285*98dc8da5SEd Maste return error; 286237d1b14SEd Maste } 287237d1b14SEd Maste memset((char *)bp->b_data + boff, 0, 288237d1b14SEd Maste pmp->pm_bpcluster - boff); 289237d1b14SEd Maste if (flags & IO_SYNC) 290237d1b14SEd Maste bwrite(bp); 291237d1b14SEd Maste else 292237d1b14SEd Maste bdwrite(bp); 293237d1b14SEd Maste } 294237d1b14SEd Maste } 295237d1b14SEd Maste 296237d1b14SEd Maste /* 297237d1b14SEd Maste * Write out the updated directory entry. Even if the update fails 298237d1b14SEd Maste * we free the trailing clusters. 299237d1b14SEd Maste */ 300237d1b14SEd Maste dep->de_FileSize = length; 301237d1b14SEd Maste if (!isadir) 302237d1b14SEd Maste dep->de_flag |= DE_UPDATE|DE_MODIFIED; 303*98dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): allerror %d, eofentry %lu\n", 304*98dc8da5SEd Maste allerror, eofentry)); 305237d1b14SEd Maste 306237d1b14SEd Maste /* 307237d1b14SEd Maste * If we need to break the cluster chain for the file then do it 308237d1b14SEd Maste * now. 309237d1b14SEd Maste */ 310*98dc8da5SEd Maste if (eofentry != ~0) { 311237d1b14SEd Maste error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 312237d1b14SEd Maste &chaintofree, CLUST_EOFE); 313237d1b14SEd Maste if (error) { 314*98dc8da5SEd Maste MSDOSFS_DPRINTF(("detrunc(): fatentry errors %d\n", 315*98dc8da5SEd Maste error)); 316237d1b14SEd Maste return (error); 317237d1b14SEd Maste } 318*98dc8da5SEd Maste fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 319*98dc8da5SEd Maste eofentry); 320237d1b14SEd Maste } 321237d1b14SEd Maste 322237d1b14SEd Maste /* 323237d1b14SEd Maste * Now free the clusters removed from the file because of the 324237d1b14SEd Maste * truncation. 325237d1b14SEd Maste */ 326*98dc8da5SEd Maste if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 327237d1b14SEd Maste freeclusterchain(pmp, chaintofree); 328237d1b14SEd Maste 329*98dc8da5SEd Maste return allerror; 330237d1b14SEd Maste } 331237d1b14SEd Maste 332237d1b14SEd Maste /* 333237d1b14SEd Maste * Extend the file described by dep to length specified by length. 334237d1b14SEd Maste */ 335237d1b14SEd Maste int 336*98dc8da5SEd Maste deextend(struct denode *dep, u_long length) 337237d1b14SEd Maste { 338237d1b14SEd Maste struct msdosfsmount *pmp = dep->de_pmp; 339237d1b14SEd Maste u_long count; 340237d1b14SEd Maste int error; 341237d1b14SEd Maste 342237d1b14SEd Maste /* 343237d1b14SEd Maste * The root of a DOS filesystem cannot be extended. 344237d1b14SEd Maste */ 345237d1b14SEd Maste if (dep->de_vnode != NULL && !FAT32(pmp)) 346237d1b14SEd Maste return EINVAL; 347237d1b14SEd Maste 348237d1b14SEd Maste /* 349237d1b14SEd Maste * Directories cannot be extended. 350237d1b14SEd Maste */ 351237d1b14SEd Maste if (dep->de_Attributes & ATTR_DIRECTORY) 352237d1b14SEd Maste return EISDIR; 353237d1b14SEd Maste 354237d1b14SEd Maste if (length <= dep->de_FileSize) 355237d1b14SEd Maste return E2BIG; 356237d1b14SEd Maste 357237d1b14SEd Maste /* 358237d1b14SEd Maste * Compute the number of clusters to allocate. 359237d1b14SEd Maste */ 360237d1b14SEd Maste count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 361237d1b14SEd Maste if (count > 0) { 362237d1b14SEd Maste if (count > pmp->pm_freeclustercount) 363237d1b14SEd Maste return (ENOSPC); 364237d1b14SEd Maste error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 365237d1b14SEd Maste if (error) { 366237d1b14SEd Maste /* truncate the added clusters away again */ 367*98dc8da5SEd Maste (void) detrunc(dep, dep->de_FileSize, 0); 368237d1b14SEd Maste return (error); 369237d1b14SEd Maste } 370237d1b14SEd Maste } 371237d1b14SEd Maste 372237d1b14SEd Maste /* 373237d1b14SEd Maste * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a 374237d1b14SEd Maste * memset(); we set the write size so ubc won't read in file data that 375237d1b14SEd Maste * is zero'd later. 376237d1b14SEd Maste */ 377237d1b14SEd Maste dep->de_FileSize = length; 378237d1b14SEd Maste dep->de_flag |= DE_UPDATE | DE_MODIFIED; 379237d1b14SEd Maste return 0; 380237d1b14SEd Maste } 381