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/param.h> 53 #include <sys/errno.h> 54 55 #include <stdbool.h> 56 #include <stdio.h> 57 #include <string.h> 58 #include <stdlib.h> 59 #include <util.h> 60 61 #include <fs/msdosfs/bpb.h> 62 #include "msdos/denode.h" 63 #include <fs/msdosfs/fat.h> 64 #include <fs/msdosfs/msdosfsmount.h> 65 66 #include "makefs.h" 67 #include "msdos.h" 68 69 70 /* 71 * If deget() succeeds it returns with the gotten denode locked(). 72 * 73 * pmp - address of msdosfsmount structure of the filesystem containing 74 * the denode of interest. The pm_dev field and the address of 75 * the msdosfsmount structure are used. 76 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 77 * diroffset is relative to the beginning of the root directory, 78 * otherwise it is cluster relative. 79 * diroffset - offset past begin of cluster of denode we want 80 * depp - returns the address of the gotten denode. 81 */ 82 int 83 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset, 84 int lkflags __unused, struct denode **depp) 85 { 86 int error; 87 uint64_t inode; 88 struct direntry *direntptr; 89 struct denode *ldep; 90 struct m_buf *bp; 91 92 MSDOSFS_DPRINTF(("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 93 pmp, dirclust, diroffset, depp)); 94 95 /* 96 * On FAT32 filesystems, root is a (more or less) normal 97 * directory 98 */ 99 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 100 dirclust = pmp->pm_rootdirblk; 101 102 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 103 104 ldep = ecalloc(1, sizeof(*ldep)); 105 ldep->de_vnode = NULL; 106 ldep->de_flag = 0; 107 ldep->de_dirclust = dirclust; 108 ldep->de_diroffset = diroffset; 109 ldep->de_inode = inode; 110 ldep->de_pmp = pmp; 111 ldep->de_refcnt = 1; 112 fc_purge(ldep, 0); /* init the FAT cache for this denode */ 113 /* 114 * Copy the directory entry into the denode area of the vnode. 115 */ 116 if ((dirclust == MSDOSFSROOT 117 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 118 && diroffset == MSDOSFSROOT_OFS) { 119 /* 120 * Directory entry for the root directory. There isn't one, 121 * so we manufacture one. We should probably rummage 122 * through the root directory and find a label entry (if it 123 * exists), and then use the time and date from that entry 124 * as the time and date for the root denode. 125 */ 126 ldep->de_vnode = (struct vnode *)-1; 127 128 ldep->de_Attributes = ATTR_DIRECTORY; 129 ldep->de_LowerCase = 0; 130 if (FAT32(pmp)) 131 ldep->de_StartCluster = pmp->pm_rootdirblk; 132 /* de_FileSize will be filled in further down */ 133 else { 134 ldep->de_StartCluster = MSDOSFSROOT; 135 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 136 } 137 /* 138 * fill in time and date so that dos2unixtime() doesn't 139 * spit up when called from msdosfs_getattr() with root 140 * denode 141 */ 142 ldep->de_CHun = 0; 143 ldep->de_CTime = 0x0000; /* 00:00:00 */ 144 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 145 | (1 << DD_DAY_SHIFT); 146 /* Jan 1, 1980 */ 147 ldep->de_ADate = ldep->de_CDate; 148 ldep->de_MTime = ldep->de_CTime; 149 ldep->de_MDate = ldep->de_CDate; 150 /* leave the other fields as garbage */ 151 } else { 152 error = m_readep(pmp, dirclust, diroffset, &bp, &direntptr); 153 if (error) { 154 ldep->de_Name[0] = SLOT_DELETED; 155 156 *depp = NULL; 157 return (error); 158 } 159 (void)DE_INTERNALIZE(ldep, direntptr); 160 brelse(bp); 161 } 162 163 /* 164 * Fill in a few fields of the vnode and finish filling in the 165 * denode. Then return the address of the found denode. 166 */ 167 if (ldep->de_Attributes & ATTR_DIRECTORY) { 168 /* 169 * Since DOS directory entries that describe directories 170 * have 0 in the filesize field, we take this opportunity 171 * to find out the length of the directory and plug it into 172 * the denode structure. 173 */ 174 u_long size; 175 176 /* 177 * XXX it sometimes happens that the "." entry has cluster 178 * number 0 when it shouldn't. Use the actual cluster number 179 * instead of what is written in directory entry. 180 */ 181 if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 182 MSDOSFS_DPRINTF(("deget(): \".\" entry at clust %lu != %lu\n", 183 dirclust, ldep->de_StartCluster)); 184 185 ldep->de_StartCluster = dirclust; 186 } 187 188 if (ldep->de_StartCluster != MSDOSFSROOT) { 189 error = pcbmap(ldep, 0xffff, 0, &size, 0); 190 if (error == E2BIG) { 191 ldep->de_FileSize = de_cn2off(pmp, size); 192 error = 0; 193 } else { 194 MSDOSFS_DPRINTF(("deget(): pcbmap returned %d\n", 195 error)); 196 } 197 } 198 } 199 *depp = ldep; 200 return (0); 201 } 202 203 /* 204 * Truncate the file described by dep to the length specified by length. 205 */ 206 int 207 detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred) 208 { 209 int error; 210 u_long eofentry; 211 u_long chaintofree; 212 daddr_t bn; 213 int boff; 214 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 215 struct m_buf *bp; 216 struct msdosfsmount *pmp = dep->de_pmp; 217 218 MSDOSFS_DPRINTF(("detrunc(): file %s, length %lu, flags %x\n", 219 dep->de_Name, length, flags)); 220 221 /* 222 * Disallow attempts to truncate the root directory since it is of 223 * fixed size. That's just the way dos filesystems are. We use 224 * the VROOT bit in the vnode because checking for the directory 225 * bit and a startcluster of 0 in the denode is not adequate to 226 * recognize the root directory at this point in a file or 227 * directory's life. 228 */ 229 if (dep->de_vnode != NULL && !FAT32(pmp)) { 230 MSDOSFS_DPRINTF(("detrunc(): can't truncate root directory, " 231 "clust %ld, offset %ld\n", 232 dep->de_dirclust, dep->de_diroffset)); 233 234 return (EINVAL); 235 } 236 237 if (dep->de_FileSize < length) 238 return deextend(dep, length, cred); 239 240 /* 241 * If the desired length is 0 then remember the starting cluster of 242 * the file and set the StartCluster field in the directory entry 243 * to 0. If the desired length is not zero, then get the number of 244 * the last cluster in the shortened file. Then get the number of 245 * the first cluster in the part of the file that is to be freed. 246 * Then set the next cluster pointer in the last cluster of the 247 * file to CLUST_EOFE. 248 */ 249 if (length == 0) { 250 chaintofree = dep->de_StartCluster; 251 dep->de_StartCluster = 0; 252 eofentry = ~0ul; 253 } else { 254 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 255 &eofentry, 0); 256 if (error) { 257 MSDOSFS_DPRINTF(("detrunc(): pcbmap fails %d\n", 258 error)); 259 return (error); 260 } 261 } 262 263 fc_purge(dep, de_clcount(pmp, length)); 264 265 /* 266 * If the new length is not a multiple of the cluster size then we 267 * must zero the tail end of the new last cluster in case it 268 * becomes part of the file again because of a seek. 269 */ 270 if ((boff = length & pmp->pm_crbomask) != 0) { 271 if (isadir) { 272 bn = cntobn(pmp, eofentry); 273 error = bread((void *)pmp->pm_devvp, bn, 274 pmp->pm_bpcluster, 0, &bp); 275 if (error) { 276 brelse(bp); 277 MSDOSFS_DPRINTF(("detrunc(): bread fails %d\n", 278 error)); 279 280 return (error); 281 } 282 memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff); 283 bwrite(bp); 284 } 285 } 286 287 /* 288 * Write out the updated directory entry. Even if the update fails 289 * we free the trailing clusters. 290 */ 291 dep->de_FileSize = length; 292 if (!isadir) 293 dep->de_flag |= DE_UPDATE|DE_MODIFIED; 294 MSDOSFS_DPRINTF(("detrunc(): eofentry %lu\n", eofentry)); 295 296 /* 297 * If we need to break the cluster chain for the file then do it 298 * now. 299 */ 300 if (eofentry != ~0ul) { 301 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 302 &chaintofree, CLUST_EOFE); 303 if (error) { 304 MSDOSFS_DPRINTF(("detrunc(): fatentry errors %d\n", 305 error)); 306 return (error); 307 } 308 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 309 eofentry); 310 } 311 312 /* 313 * Now free the clusters removed from the file because of the 314 * truncation. 315 */ 316 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 317 freeclusterchain(pmp, chaintofree); 318 319 return (0); 320 } 321 322 /* 323 * Extend the file described by dep to length specified by length. 324 */ 325 int 326 deextend(struct denode *dep, u_long length, struct ucred *cred) 327 { 328 struct msdosfsmount *pmp = dep->de_pmp; 329 u_long count; 330 int error; 331 332 /* 333 * The root of a DOS filesystem cannot be extended. 334 */ 335 if (dep->de_vnode != NULL && !FAT32(pmp)) 336 return (EINVAL); 337 338 /* 339 * Directories cannot be extended. 340 */ 341 if (dep->de_Attributes & ATTR_DIRECTORY) 342 return (EISDIR); 343 344 if (length <= dep->de_FileSize) 345 return (E2BIG); 346 347 /* 348 * Compute the number of clusters to allocate. 349 */ 350 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 351 if (count > 0) { 352 if (count > pmp->pm_freeclustercount) 353 return (ENOSPC); 354 error = m_extendfile(dep, count, NULL, NULL, DE_CLEAR); 355 if (error) { 356 /* truncate the added clusters away again */ 357 (void) detrunc(dep, dep->de_FileSize, 0, cred); 358 return (error); 359 } 360 } 361 362 /* 363 * Zero extend file range; ubc_zerorange() uses ubc_alloc() and a 364 * memset(); we set the write size so ubc won't read in file data that 365 * is zero'd later. 366 */ 367 dep->de_FileSize = length; 368 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 369 return 0; 370 } 371