1 /* $NetBSD: msdosfs_denode.c,v 1.7 2015/03/29 05:52:59 agc Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-4-Clause 5 * 6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 8 * All rights reserved. 9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by TooLs GmbH. 22 * 4. The name of TooLs GmbH may not be used to endorse or promote products 23 * derived from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 /*- 37 * Written by Paul Popelka (paulp@uts.amdahl.com) 38 * 39 * You can do anything you want with this software, just don't say you wrote 40 * it, and don't remove this notice. 41 * 42 * This software is provided "as is". 43 * 44 * The author supplies this software to be publicly redistributed on the 45 * understanding that the author is not responsible for the correct 46 * functioning of this software in any circumstances and is not liable for 47 * any damages caused by this software. 48 * 49 * October 1992 50 */ 51 52 #include <sys/cdefs.h> 53 __FBSDID("$FreeBSD$"); 54 55 #include <sys/param.h> 56 #include <sys/errno.h> 57 58 #include <stdbool.h> 59 #include <stdio.h> 60 #include <string.h> 61 #include <stdlib.h> 62 #include <util.h> 63 64 #include <vfs/msdosfs/bpb.h> 65 #include "msdos/denode.h" 66 #include <vfs/msdosfs/fat.h> 67 #include <vfs/msdosfs/msdosfsmount.h> 68 69 #include "makefs.h" 70 #include "msdos.h" 71 72 73 /* 74 * If deget() succeeds it returns with the gotten denode locked(). 75 * 76 * pmp - address of msdosfsmount structure of the filesystem containing 77 * the denode of interest. The pm_dev field and the address of 78 * the msdosfsmount structure are used. 79 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 80 * diroffset is relative to the beginning of the root directory, 81 * otherwise it is cluster relative. 82 * diroffset - offset past begin of cluster of denode we want 83 * depp - returns the address of the gotten denode. 84 */ 85 int 86 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 87 struct denode **depp) 88 { 89 int error; 90 uint64_t inode; 91 struct direntry *direntptr; 92 struct denode *ldep; 93 struct m_buf *bp; 94 95 MSDOSFS_DPRINTF(("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 96 pmp, dirclust, diroffset, depp)); 97 98 /* 99 * On FAT32 filesystems, root is a (more or less) normal 100 * directory 101 */ 102 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 103 dirclust = pmp->pm_rootdirblk; 104 105 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 106 107 ldep = ecalloc(1, sizeof(*ldep)); 108 ldep->de_vnode = NULL; 109 ldep->de_flag = 0; 110 ldep->de_dirclust = dirclust; 111 ldep->de_diroffset = diroffset; 112 /* XXX unused + non existent in DragonFly */ 113 //ldep->de_inode = inode; 114 ldep->de_pmp = pmp; 115 ldep->de_refcnt = 1; 116 fc_purge(ldep, 0); /* init the FAT cache for this denode */ 117 /* 118 * Copy the directory entry into the denode area of the vnode. 119 */ 120 if ((dirclust == MSDOSFSROOT 121 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 122 && diroffset == MSDOSFSROOT_OFS) { 123 /* 124 * Directory entry for the root directory. There isn't one, 125 * so we manufacture one. We should probably rummage 126 * through the root directory and find a label entry (if it 127 * exists), and then use the time and date from that entry 128 * as the time and date for the root denode. 129 */ 130 ldep->de_vnode = (struct vnode *)-1; 131 132 ldep->de_Attributes = ATTR_DIRECTORY; 133 ldep->de_LowerCase = 0; 134 if (FAT32(pmp)) 135 ldep->de_StartCluster = pmp->pm_rootdirblk; 136 /* de_FileSize will be filled in further down */ 137 else { 138 ldep->de_StartCluster = MSDOSFSROOT; 139 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 140 } 141 /* 142 * fill in time and date so that dos2unixtime() doesn't 143 * spit up when called from msdosfs_getattr() with root 144 * denode 145 */ 146 ldep->de_CHun = 0; 147 ldep->de_CTime = 0x0000; /* 00:00:00 */ 148 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 149 | (1 << DD_DAY_SHIFT); 150 /* Jan 1, 1980 */ 151 ldep->de_ADate = ldep->de_CDate; 152 ldep->de_MTime = ldep->de_CTime; 153 ldep->de_MDate = ldep->de_CDate; 154 /* leave the other fields as garbage */ 155 } else { 156 error = m_readep(pmp, dirclust, diroffset, &bp, &direntptr); 157 if (error) { 158 ldep->de_Name[0] = SLOT_DELETED; 159 160 *depp = NULL; 161 return (error); 162 } 163 (void)DE_INTERNALIZE(ldep, direntptr); 164 brelse(bp); 165 } 166 167 /* 168 * Fill in a few fields of the vnode and finish filling in the 169 * denode. Then return the address of the found denode. 170 */ 171 if (ldep->de_Attributes & ATTR_DIRECTORY) { 172 /* 173 * Since DOS directory entries that describe directories 174 * have 0 in the filesize field, we take this opportunity 175 * to find out the length of the directory and plug it into 176 * the denode structure. 177 */ 178 u_long size; 179 180 /* 181 * XXX it sometimes happens that the "." entry has cluster 182 * number 0 when it shouldn't. Use the actual cluster number 183 * instead of what is written in directory entry. 184 */ 185 if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 186 MSDOSFS_DPRINTF(("deget(): \".\" entry at clust %lu != %lu\n", 187 dirclust, ldep->de_StartCluster)); 188 189 ldep->de_StartCluster = dirclust; 190 } 191 192 if (ldep->de_StartCluster != MSDOSFSROOT) { 193 error = pcbmap(ldep, 0xffff, 0, &size, 0); 194 if (error == E2BIG) { 195 ldep->de_FileSize = de_cn2off(pmp, size); 196 error = 0; 197 } else { 198 MSDOSFS_DPRINTF(("deget(): pcbmap returned %d\n", 199 error)); 200 } 201 } 202 } 203 *depp = ldep; 204 return (0); 205 } 206 207 /* 208 * Truncate the file described by dep to the length specified by length. 209 */ 210 int 211 detrunc(struct denode *dep, u_long length, int flags) 212 { 213 int error; 214 int allerror; 215 u_long eofentry; 216 u_long chaintofree; 217 daddr_t bn; 218 int boff; 219 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 220 struct m_buf *bp; 221 struct msdosfsmount *pmp = dep->de_pmp; 222 223 MSDOSFS_DPRINTF(("detrunc(): file %s, length %lu, flags %x\n", 224 dep->de_Name, length, flags)); 225 226 /* 227 * Disallow attempts to truncate the root directory since it is of 228 * fixed size. That's just the way dos filesystems are. We use 229 * the VROOT bit in the vnode because checking for the directory 230 * bit and a startcluster of 0 in the denode is not adequate to 231 * recognize the root directory at this point in a file or 232 * directory's life. 233 */ 234 if (dep->de_vnode != NULL && !FAT32(pmp)) { 235 MSDOSFS_DPRINTF(("detrunc(): can't truncate root directory, " 236 "clust %ld, offset %ld\n", 237 dep->de_dirclust, dep->de_diroffset)); 238 239 return (EINVAL); 240 } 241 242 if (dep->de_FileSize < length) 243 return deextend(dep, length); 244 245 /* 246 * If the desired length is 0 then remember the starting cluster of 247 * the file and set the StartCluster field in the directory entry 248 * to 0. If the desired length is not zero, then get the number of 249 * the last cluster in the shortened file. Then get the number of 250 * the first cluster in the part of the file that is to be freed. 251 * Then set the next cluster pointer in the last cluster of the 252 * file to CLUST_EOFE. 253 */ 254 if (length == 0) { 255 chaintofree = dep->de_StartCluster; 256 dep->de_StartCluster = 0; 257 eofentry = ~0; 258 } else { 259 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 260 &eofentry, 0); 261 if (error) { 262 MSDOSFS_DPRINTF(("detrunc(): pcbmap fails %d\n", 263 error)); 264 return (error); 265 } 266 } 267 268 fc_purge(dep, de_clcount(pmp, length)); 269 270 /* 271 * If the new length is not a multiple of the cluster size then we 272 * must zero the tail end of the new last cluster in case it 273 * becomes part of the file again because of a seek. 274 */ 275 if ((boff = length & pmp->pm_crbomask) != 0) { 276 if (isadir) { 277 bn = cntobn(pmp, eofentry); 278 error = bread((void *)pmp->pm_devvp, bn, 279 pmp->pm_bpcluster, 0, &bp); 280 if (error) { 281 brelse(bp); 282 MSDOSFS_DPRINTF(("detrunc(): bread fails %d\n", 283 error)); 284 285 return (error); 286 } 287 memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff); 288 bwrite(bp); 289 } 290 } 291 292 /* 293 * Write out the updated directory entry. Even if the update fails 294 * we free the trailing clusters. 295 */ 296 dep->de_FileSize = length; 297 if (!isadir) 298 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 299 MSDOSFS_DPRINTF(("detrunc(): allerror %d, eofentry %lu\n", 300 allerror, eofentry)); 301 302 /* 303 * If we need to break the cluster chain for the file then do it 304 * now. 305 */ 306 if (eofentry != ~0) { 307 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 308 &chaintofree, CLUST_EOFE); 309 if (error) { 310 MSDOSFS_DPRINTF(("detrunc(): fatentry errors %d\n", 311 error)); 312 return (error); 313 } 314 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 315 eofentry); 316 } 317 318 /* 319 * Now free the clusters removed from the file because of the 320 * truncation. 321 */ 322 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 323 freeclusterchain(pmp, chaintofree); 324 325 return (allerror); 326 } 327 328 /* 329 * Extend the file described by dep to length specified by length. 330 */ 331 int 332 deextend(struct denode *dep, u_long length) 333 { 334 struct msdosfsmount *pmp = dep->de_pmp; 335 u_long count; 336 int error; 337 338 /* 339 * The root of a DOS filesystem cannot be extended. 340 */ 341 if (dep->de_vnode != NULL && !FAT32(pmp)) 342 return (EINVAL); 343 344 /* 345 * Directories cannot be extended. 346 */ 347 if (dep->de_Attributes & ATTR_DIRECTORY) 348 return (EISDIR); 349 350 if (length <= dep->de_FileSize) 351 return (E2BIG); 352 353 /* 354 * Compute the number of clusters to allocate. 355 */ 356 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 357 if (count > 0) { 358 if (count > pmp->pm_freeclustercount) 359 return (ENOSPC); 360 error = m_extendfile(dep, count, NULL, NULL, DE_CLEAR); 361 if (error) { 362 /* truncate the added clusters away again */ 363 (void) detrunc(dep, dep->de_FileSize, 0); 364 return (error); 365 } 366 } 367 368 /* 369 * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a 370 * memset(); we set the write size so ubc won't read in file data that 371 * is zero'd later. 372 */ 373 dep->de_FileSize = length; 374 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 375 return 0; 376 } 377