2/* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */ 3 4/*- 5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 7 * All rights reserved. 8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35/*- 36 * Written by Paul Popelka (paulp@uts.amdahl.com) 37 * 38 * You can do anything you want with this software, just don't say you wrote 39 * it, and don't remove this notice. 40 * 41 * This software is provided "as is". 42 * 43 * The author supplies this software to be publicly redistributed on the 44 * understanding that the author is not responsible for the correct 45 * functioning of this software in any circumstances and is not liable for 46 * any damages caused by this software. 47 * 48 * October 1992 49 */ 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/buf.h> 54#include <sys/clock.h> 55#include <sys/kernel.h> 56#include <sys/malloc.h> 57#include <sys/mount.h> 58#include <sys/vnode.h> 59 60#include <vm/vm.h> 61#include <vm/vm_extern.h> 62 63#include <fs/msdosfs/bpb.h> 64#include <fs/msdosfs/direntry.h> 65#include <fs/msdosfs/denode.h> 66#include <fs/msdosfs/fat.h> 67#include <fs/msdosfs/msdosfsmount.h> 68 69static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part"); 70 71static int 72de_vncmpf(struct vnode *vp, void *arg) 73{ 74 struct denode *de; 75 uint64_t *a; 76 77 a = arg; 78 de = VTODE(vp); 79 return (de->de_inode != *a); 80} 81 82/* 83 * If deget() succeeds it returns with the gotten denode locked(). 84 * 85 * pmp - address of msdosfsmount structure of the filesystem containing 86 * the denode of interest. The address of 87 * the msdosfsmount structure are used. 88 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 89 * diroffset is relative to the beginning of the root directory, 90 * otherwise it is cluster relative. 91 * diroffset - offset past begin of cluster of denode we want 92 * depp - returns the address of the gotten denode. 93 */ 94int 95deget(pmp, dirclust, diroffset, depp) 96 struct msdosfsmount *pmp; /* so we know the maj/min number */ 97 u_long dirclust; /* cluster this dir entry came from */ 98 u_long diroffset; /* index of entry within the cluster */ 99 struct denode **depp; /* returns the addr of the gotten denode */ 100{ 101 int error; 102 uint64_t inode; 103 struct mount *mntp = pmp->pm_mountp; 104 struct direntry *direntptr; 105 struct denode *ldep; 106 struct vnode *nvp, *xvp; 107 struct buf *bp; 108 109#ifdef MSDOSFS_DEBUG 110 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 111 pmp, dirclust, diroffset, depp); 112#endif 113 114 /* 115 * On FAT32 filesystems, root is a (more or less) normal 116 * directory 117 */ 118 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 119 dirclust = pmp->pm_rootdirblk; 120 121 /* 122 * See if the denode is in the denode cache. Use the location of 123 * the directory entry to compute the hash value. For subdir use 124 * address of "." entry. For root dir (if not FAT32) use cluster 125 * MSDOSFSROOT, offset MSDOSFSROOT_OFS 126 * 127 * NOTE: The check for de_refcnt > 0 below insures the denode being 128 * examined does not represent an unlinked but still open file. 129 * These files are not to be accessible even when the directory 130 * entry that represented the file happens to be reused while the 131 * deleted file is still open. 132 */ 133 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 134 135 error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp, 136 de_vncmpf, &inode); 137 if (error) 138 return (error); 139 if (nvp != NULL) { 140 *depp = VTODE(nvp); 141 KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust")); 142 KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset")); 143 return (0); 144 } 145 146 /* 147 * Do the MALLOC before the getnewvnode since doing so afterward 148 * might cause a bogus v_data pointer to get dereferenced 149 * elsewhere if MALLOC should block. 150 */ 151 ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK); 152 153 /* 154 * Directory entry was not in cache, have to create a vnode and 155 * copy it from the passed disk buffer. 156 */ 157 /* getnewvnode() does a VREF() on the vnode */ 158 error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp); 159 if (error) { 160 *depp = NULL; 161 free(ldep, M_MSDOSFSNODE); 162 return error; 163 } 164 bzero((caddr_t)ldep, sizeof *ldep); 165 nvp->v_data = ldep; 166 ldep->de_vnode = nvp; 167 ldep->de_flag = 0; 168 ldep->de_dirclust = dirclust; 169 ldep->de_diroffset = diroffset; 170 ldep->de_inode = inode;
| 2/* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */ 3 4/*- 5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank. 6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH. 7 * All rights reserved. 8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by TooLs GmbH. 21 * 4. The name of TooLs GmbH may not be used to endorse or promote products 22 * derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35/*- 36 * Written by Paul Popelka (paulp@uts.amdahl.com) 37 * 38 * You can do anything you want with this software, just don't say you wrote 39 * it, and don't remove this notice. 40 * 41 * This software is provided "as is". 42 * 43 * The author supplies this software to be publicly redistributed on the 44 * understanding that the author is not responsible for the correct 45 * functioning of this software in any circumstances and is not liable for 46 * any damages caused by this software. 47 * 48 * October 1992 49 */ 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/buf.h> 54#include <sys/clock.h> 55#include <sys/kernel.h> 56#include <sys/malloc.h> 57#include <sys/mount.h> 58#include <sys/vnode.h> 59 60#include <vm/vm.h> 61#include <vm/vm_extern.h> 62 63#include <fs/msdosfs/bpb.h> 64#include <fs/msdosfs/direntry.h> 65#include <fs/msdosfs/denode.h> 66#include <fs/msdosfs/fat.h> 67#include <fs/msdosfs/msdosfsmount.h> 68 69static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part"); 70 71static int 72de_vncmpf(struct vnode *vp, void *arg) 73{ 74 struct denode *de; 75 uint64_t *a; 76 77 a = arg; 78 de = VTODE(vp); 79 return (de->de_inode != *a); 80} 81 82/* 83 * If deget() succeeds it returns with the gotten denode locked(). 84 * 85 * pmp - address of msdosfsmount structure of the filesystem containing 86 * the denode of interest. The address of 87 * the msdosfsmount structure are used. 88 * dirclust - which cluster bp contains, if dirclust is 0 (root directory) 89 * diroffset is relative to the beginning of the root directory, 90 * otherwise it is cluster relative. 91 * diroffset - offset past begin of cluster of denode we want 92 * depp - returns the address of the gotten denode. 93 */ 94int 95deget(pmp, dirclust, diroffset, depp) 96 struct msdosfsmount *pmp; /* so we know the maj/min number */ 97 u_long dirclust; /* cluster this dir entry came from */ 98 u_long diroffset; /* index of entry within the cluster */ 99 struct denode **depp; /* returns the addr of the gotten denode */ 100{ 101 int error; 102 uint64_t inode; 103 struct mount *mntp = pmp->pm_mountp; 104 struct direntry *direntptr; 105 struct denode *ldep; 106 struct vnode *nvp, *xvp; 107 struct buf *bp; 108 109#ifdef MSDOSFS_DEBUG 110 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", 111 pmp, dirclust, diroffset, depp); 112#endif 113 114 /* 115 * On FAT32 filesystems, root is a (more or less) normal 116 * directory 117 */ 118 if (FAT32(pmp) && dirclust == MSDOSFSROOT) 119 dirclust = pmp->pm_rootdirblk; 120 121 /* 122 * See if the denode is in the denode cache. Use the location of 123 * the directory entry to compute the hash value. For subdir use 124 * address of "." entry. For root dir (if not FAT32) use cluster 125 * MSDOSFSROOT, offset MSDOSFSROOT_OFS 126 * 127 * NOTE: The check for de_refcnt > 0 below insures the denode being 128 * examined does not represent an unlinked but still open file. 129 * These files are not to be accessible even when the directory 130 * entry that represented the file happens to be reused while the 131 * deleted file is still open. 132 */ 133 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset; 134 135 error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp, 136 de_vncmpf, &inode); 137 if (error) 138 return (error); 139 if (nvp != NULL) { 140 *depp = VTODE(nvp); 141 KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust")); 142 KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset")); 143 return (0); 144 } 145 146 /* 147 * Do the MALLOC before the getnewvnode since doing so afterward 148 * might cause a bogus v_data pointer to get dereferenced 149 * elsewhere if MALLOC should block. 150 */ 151 ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK); 152 153 /* 154 * Directory entry was not in cache, have to create a vnode and 155 * copy it from the passed disk buffer. 156 */ 157 /* getnewvnode() does a VREF() on the vnode */ 158 error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp); 159 if (error) { 160 *depp = NULL; 161 free(ldep, M_MSDOSFSNODE); 162 return error; 163 } 164 bzero((caddr_t)ldep, sizeof *ldep); 165 nvp->v_data = ldep; 166 ldep->de_vnode = nvp; 167 ldep->de_flag = 0; 168 ldep->de_dirclust = dirclust; 169 ldep->de_diroffset = diroffset; 170 ldep->de_inode = inode;
|
172 fc_purge(ldep, 0); /* init the fat cache for this denode */ 173 174 lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL); 175 error = insmntque(nvp, mntp); 176 if (error != 0) { 177 free(ldep, M_MSDOSFSNODE); 178 *depp = NULL; 179 return (error); 180 } 181 error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp, 182 de_vncmpf, &inode); 183 if (error) { 184 *depp = NULL; 185 return (error); 186 } 187 if (xvp != NULL) { 188 /* XXX: Not sure this is right */ 189 nvp = xvp; 190 ldep->de_vnode = nvp; 191 } 192 193 ldep->de_pmp = pmp; 194 ldep->de_refcnt = 1; 195 /* 196 * Copy the directory entry into the denode area of the vnode. 197 */ 198 if ((dirclust == MSDOSFSROOT 199 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 200 && diroffset == MSDOSFSROOT_OFS) { 201 /* 202 * Directory entry for the root directory. There isn't one, 203 * so we manufacture one. We should probably rummage 204 * through the root directory and find a label entry (if it 205 * exists), and then use the time and date from that entry 206 * as the time and date for the root denode. 207 */ 208 nvp->v_vflag |= VV_ROOT; /* should be further down XXX */ 209 210 ldep->de_Attributes = ATTR_DIRECTORY; 211 ldep->de_LowerCase = 0; 212 if (FAT32(pmp)) 213 ldep->de_StartCluster = pmp->pm_rootdirblk; 214 /* de_FileSize will be filled in further down */ 215 else { 216 ldep->de_StartCluster = MSDOSFSROOT; 217 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 218 } 219 /* 220 * fill in time and date so that fattime2timespec() doesn't 221 * spit up when called from msdosfs_getattr() with root 222 * denode 223 */ 224 ldep->de_CHun = 0; 225 ldep->de_CTime = 0x0000; /* 00:00:00 */ 226 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 227 | (1 << DD_DAY_SHIFT); 228 /* Jan 1, 1980 */ 229 ldep->de_ADate = ldep->de_CDate; 230 ldep->de_MTime = ldep->de_CTime; 231 ldep->de_MDate = ldep->de_CDate; 232 /* leave the other fields as garbage */ 233 } else { 234 error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 235 if (error) { 236 /* 237 * The denode does not contain anything useful, so 238 * it would be wrong to leave it on its hash chain. 239 * Arrange for vput() to just forget about it. 240 */ 241 ldep->de_Name[0] = SLOT_DELETED; 242 243 vput(nvp); 244 *depp = NULL; 245 return (error); 246 } 247 DE_INTERNALIZE(ldep, direntptr); 248 brelse(bp); 249 } 250 251 /* 252 * Fill in a few fields of the vnode and finish filling in the 253 * denode. Then return the address of the found denode. 254 */ 255 if (ldep->de_Attributes & ATTR_DIRECTORY) { 256 /* 257 * Since DOS directory entries that describe directories 258 * have 0 in the filesize field, we take this opportunity 259 * to find out the length of the directory and plug it into 260 * the denode structure. 261 */ 262 u_long size; 263 264 /* 265 * XXX it sometimes happens that the "." entry has cluster 266 * number 0 when it shouldn't. Use the actual cluster number 267 * instead of what is written in directory entry. 268 */ 269 if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 270 printf("deget(): \".\" entry at clust %lu != %lu\n", 271 dirclust, ldep->de_StartCluster); 272 ldep->de_StartCluster = dirclust; 273 } 274 275 nvp->v_type = VDIR; 276 if (ldep->de_StartCluster != MSDOSFSROOT) { 277 error = pcbmap(ldep, 0xffff, 0, &size, 0); 278 if (error == E2BIG) { 279 ldep->de_FileSize = de_cn2off(pmp, size); 280 error = 0; 281 } else 282 printf("deget(): pcbmap returned %d\n", error); 283 } 284 } else 285 nvp->v_type = VREG; 286 ldep->de_modrev = init_va_filerev(); 287 *depp = ldep; 288 return (0); 289} 290 291int 292deupdat(dep, waitfor) 293 struct denode *dep; 294 int waitfor; 295{ 296 int error; 297 struct buf *bp; 298 struct direntry *dirp; 299 struct timespec ts; 300 301 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 302 return (0); 303 getnanotime(&ts); 304 DETIMES(dep, &ts, &ts, &ts); 305 if ((dep->de_flag & DE_MODIFIED) == 0) 306 return (0); 307 dep->de_flag &= ~DE_MODIFIED; 308 if (dep->de_Attributes & ATTR_DIRECTORY) 309 return (0); 310 if (dep->de_refcnt <= 0) 311 return (0); 312 error = readde(dep, &bp, &dirp); 313 if (error) 314 return (error); 315 DE_EXTERNALIZE(dirp, dep); 316 if (waitfor) 317 return (bwrite(bp)); 318 else { 319 bdwrite(bp); 320 return (0); 321 } 322} 323 324/* 325 * Truncate the file described by dep to the length specified by length. 326 */ 327int 328detrunc(dep, length, flags, cred, td) 329 struct denode *dep; 330 u_long length; 331 int flags; 332 struct ucred *cred; 333 struct thread *td; 334{ 335 int error; 336 int allerror; 337 u_long eofentry; 338 u_long chaintofree; 339 daddr_t bn; 340 int boff; 341 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 342 struct buf *bp; 343 struct msdosfsmount *pmp = dep->de_pmp; 344 345#ifdef MSDOSFS_DEBUG 346 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 347#endif 348 349 /* 350 * Disallow attempts to truncate the root directory since it is of 351 * fixed size. That's just the way dos filesystems are. We use 352 * the VROOT bit in the vnode because checking for the directory 353 * bit and a startcluster of 0 in the denode is not adequate to 354 * recognize the root directory at this point in a file or 355 * directory's life. 356 */ 357 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) { 358 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 359 dep->de_dirclust, dep->de_diroffset); 360 return (EINVAL); 361 } 362 363 if (dep->de_FileSize < length) { 364 vnode_pager_setsize(DETOV(dep), length); 365 return deextend(dep, length, cred); 366 } 367 368 /* 369 * If the desired length is 0 then remember the starting cluster of 370 * the file and set the StartCluster field in the directory entry 371 * to 0. If the desired length is not zero, then get the number of 372 * the last cluster in the shortened file. Then get the number of 373 * the first cluster in the part of the file that is to be freed. 374 * Then set the next cluster pointer in the last cluster of the 375 * file to CLUST_EOFE. 376 */ 377 if (length == 0) { 378 chaintofree = dep->de_StartCluster; 379 dep->de_StartCluster = 0; 380 eofentry = ~0; 381 } else { 382 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 383 &eofentry, 0); 384 if (error) { 385#ifdef MSDOSFS_DEBUG 386 printf("detrunc(): pcbmap fails %d\n", error); 387#endif 388 return (error); 389 } 390 } 391 392 fc_purge(dep, de_clcount(pmp, length)); 393 394 /* 395 * If the new length is not a multiple of the cluster size then we 396 * must zero the tail end of the new last cluster in case it 397 * becomes part of the file again because of a seek. 398 */ 399 if ((boff = length & pmp->pm_crbomask) != 0) { 400 if (isadir) { 401 bn = cntobn(pmp, eofentry); 402 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 403 NOCRED, &bp); 404 if (error) { 405 brelse(bp); 406#ifdef MSDOSFS_DEBUG 407 printf("detrunc(): bread fails %d\n", error); 408#endif 409 return (error); 410 } 411 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 412 if (flags & IO_SYNC) 413 bwrite(bp); 414 else 415 bdwrite(bp); 416 } 417 } 418 419 /* 420 * Write out the updated directory entry. Even if the update fails 421 * we free the trailing clusters. 422 */ 423 dep->de_FileSize = length; 424 if (!isadir) 425 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 426 allerror = vtruncbuf(DETOV(dep), cred, td, length, pmp->pm_bpcluster); 427#ifdef MSDOSFS_DEBUG 428 if (allerror) 429 printf("detrunc(): vtruncbuf error %d\n", allerror); 430#endif 431 error = deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC)); 432 if (error != 0 && allerror == 0) 433 allerror = error; 434#ifdef MSDOSFS_DEBUG 435 printf("detrunc(): allerror %d, eofentry %lu\n", 436 allerror, eofentry); 437#endif 438 439 /* 440 * If we need to break the cluster chain for the file then do it 441 * now. 442 */ 443 if (eofentry != ~0) { 444 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 445 &chaintofree, CLUST_EOFE); 446 if (error) { 447#ifdef MSDOSFS_DEBUG 448 printf("detrunc(): fatentry errors %d\n", error); 449#endif 450 return (error); 451 } 452 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 453 eofentry); 454 } 455 456 /* 457 * Now free the clusters removed from the file because of the 458 * truncation. 459 */ 460 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 461 freeclusterchain(pmp, chaintofree); 462 463 return (allerror); 464} 465 466/* 467 * Extend the file described by dep to length specified by length. 468 */ 469int 470deextend(dep, length, cred) 471 struct denode *dep; 472 u_long length; 473 struct ucred *cred; 474{ 475 struct msdosfsmount *pmp = dep->de_pmp; 476 u_long count; 477 int error; 478 479 /* 480 * The root of a DOS filesystem cannot be extended. 481 */ 482 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 483 return (EINVAL); 484 485 /* 486 * Directories cannot be extended. 487 */ 488 if (dep->de_Attributes & ATTR_DIRECTORY) 489 return (EISDIR); 490 491 if (length <= dep->de_FileSize) 492 panic("deextend: file too large"); 493 494 /* 495 * Compute the number of clusters to allocate. 496 */ 497 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 498 if (count > 0) { 499 if (count > pmp->pm_freeclustercount) 500 return (ENOSPC); 501 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 502 if (error) { 503 /* truncate the added clusters away again */ 504 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 505 return (error); 506 } 507 } 508 dep->de_FileSize = length; 509 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 510 return (deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC))); 511} 512 513/* 514 * Move a denode to its correct hash queue after the file it represents has 515 * been moved to a new directory. 516 */ 517void 518reinsert(dep) 519 struct denode *dep; 520{ 521 struct vnode *vp; 522 523 /* 524 * Fix up the denode cache. If the denode is for a directory, 525 * there is nothing to do since the hash is based on the starting 526 * cluster of the directory file and that hasn't changed. If for a 527 * file the hash is based on the location of the directory entry, 528 * so we must remove it from the cache and re-enter it with the 529 * hash based on the new location of the directory entry. 530 */ 531#if 0 532 if (dep->de_Attributes & ATTR_DIRECTORY) 533 return; 534#endif 535 vp = DETOV(dep); 536 dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust + 537 dep->de_diroffset; 538 vfs_hash_rehash(vp, dep->de_inode); 539} 540 541int 542msdosfs_reclaim(ap) 543 struct vop_reclaim_args /* { 544 struct vnode *a_vp; 545 } */ *ap; 546{ 547 struct vnode *vp = ap->a_vp; 548 struct denode *dep = VTODE(vp); 549 550#ifdef MSDOSFS_DEBUG 551 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 552 dep, dep->de_Name, dep->de_refcnt); 553#endif 554 555 if (prtactive && vrefcnt(vp) != 0) 556 vprint("msdosfs_reclaim(): pushing active", vp); 557 /* 558 * Destroy the vm object and flush associated pages. 559 */ 560 vnode_destroy_vobject(vp); 561 /* 562 * Remove the denode from its hash chain. 563 */ 564 vfs_hash_remove(vp); 565 /* 566 * Purge old data structures associated with the denode. 567 */ 568#if 0 /* XXX */ 569 dep->de_flag = 0; 570#endif 571 free(dep, M_MSDOSFSNODE); 572 vp->v_data = NULL; 573 574 return (0); 575} 576 577int 578msdosfs_inactive(ap) 579 struct vop_inactive_args /* { 580 struct vnode *a_vp; 581 struct thread *a_td; 582 } */ *ap; 583{ 584 struct vnode *vp = ap->a_vp; 585 struct denode *dep = VTODE(vp); 586 struct thread *td = ap->a_td; 587 int error = 0; 588 589#ifdef MSDOSFS_DEBUG 590 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 591#endif 592 593 if (prtactive && vrefcnt(vp) != 0) 594 vprint("msdosfs_inactive(): pushing active", vp); 595 596 /* 597 * Ignore denodes related to stale file handles. 598 */ 599 if (dep->de_Name[0] == SLOT_DELETED) 600 goto out; 601 602 /* 603 * If the file has been deleted and it is on a read/write 604 * filesystem, then truncate the file, and mark the directory slot 605 * as empty. (This may not be necessary for the dos filesystem.) 606 */ 607#ifdef MSDOSFS_DEBUG 608 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", 609 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 610#endif 611 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 612 error = detrunc(dep, (u_long) 0, 0, NOCRED, td); 613 dep->de_flag |= DE_UPDATE; 614 dep->de_Name[0] = SLOT_DELETED; 615 } 616 deupdat(dep, 0); 617 618out: 619 /* 620 * If we are done with the denode, reclaim it 621 * so that it can be reused immediately. 622 */ 623#ifdef MSDOSFS_DEBUG 624 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 625 vrefcnt(vp), dep->de_Name[0]); 626#endif 627 if (dep->de_Name[0] == SLOT_DELETED) 628 vrecycle(vp, td); 629 return (error); 630}
| 171 fc_purge(ldep, 0); /* init the fat cache for this denode */ 172 173 lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL); 174 error = insmntque(nvp, mntp); 175 if (error != 0) { 176 free(ldep, M_MSDOSFSNODE); 177 *depp = NULL; 178 return (error); 179 } 180 error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp, 181 de_vncmpf, &inode); 182 if (error) { 183 *depp = NULL; 184 return (error); 185 } 186 if (xvp != NULL) { 187 /* XXX: Not sure this is right */ 188 nvp = xvp; 189 ldep->de_vnode = nvp; 190 } 191 192 ldep->de_pmp = pmp; 193 ldep->de_refcnt = 1; 194 /* 195 * Copy the directory entry into the denode area of the vnode. 196 */ 197 if ((dirclust == MSDOSFSROOT 198 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) 199 && diroffset == MSDOSFSROOT_OFS) { 200 /* 201 * Directory entry for the root directory. There isn't one, 202 * so we manufacture one. We should probably rummage 203 * through the root directory and find a label entry (if it 204 * exists), and then use the time and date from that entry 205 * as the time and date for the root denode. 206 */ 207 nvp->v_vflag |= VV_ROOT; /* should be further down XXX */ 208 209 ldep->de_Attributes = ATTR_DIRECTORY; 210 ldep->de_LowerCase = 0; 211 if (FAT32(pmp)) 212 ldep->de_StartCluster = pmp->pm_rootdirblk; 213 /* de_FileSize will be filled in further down */ 214 else { 215 ldep->de_StartCluster = MSDOSFSROOT; 216 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; 217 } 218 /* 219 * fill in time and date so that fattime2timespec() doesn't 220 * spit up when called from msdosfs_getattr() with root 221 * denode 222 */ 223 ldep->de_CHun = 0; 224 ldep->de_CTime = 0x0000; /* 00:00:00 */ 225 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) 226 | (1 << DD_DAY_SHIFT); 227 /* Jan 1, 1980 */ 228 ldep->de_ADate = ldep->de_CDate; 229 ldep->de_MTime = ldep->de_CTime; 230 ldep->de_MDate = ldep->de_CDate; 231 /* leave the other fields as garbage */ 232 } else { 233 error = readep(pmp, dirclust, diroffset, &bp, &direntptr); 234 if (error) { 235 /* 236 * The denode does not contain anything useful, so 237 * it would be wrong to leave it on its hash chain. 238 * Arrange for vput() to just forget about it. 239 */ 240 ldep->de_Name[0] = SLOT_DELETED; 241 242 vput(nvp); 243 *depp = NULL; 244 return (error); 245 } 246 DE_INTERNALIZE(ldep, direntptr); 247 brelse(bp); 248 } 249 250 /* 251 * Fill in a few fields of the vnode and finish filling in the 252 * denode. Then return the address of the found denode. 253 */ 254 if (ldep->de_Attributes & ATTR_DIRECTORY) { 255 /* 256 * Since DOS directory entries that describe directories 257 * have 0 in the filesize field, we take this opportunity 258 * to find out the length of the directory and plug it into 259 * the denode structure. 260 */ 261 u_long size; 262 263 /* 264 * XXX it sometimes happens that the "." entry has cluster 265 * number 0 when it shouldn't. Use the actual cluster number 266 * instead of what is written in directory entry. 267 */ 268 if (diroffset == 0 && ldep->de_StartCluster != dirclust) { 269 printf("deget(): \".\" entry at clust %lu != %lu\n", 270 dirclust, ldep->de_StartCluster); 271 ldep->de_StartCluster = dirclust; 272 } 273 274 nvp->v_type = VDIR; 275 if (ldep->de_StartCluster != MSDOSFSROOT) { 276 error = pcbmap(ldep, 0xffff, 0, &size, 0); 277 if (error == E2BIG) { 278 ldep->de_FileSize = de_cn2off(pmp, size); 279 error = 0; 280 } else 281 printf("deget(): pcbmap returned %d\n", error); 282 } 283 } else 284 nvp->v_type = VREG; 285 ldep->de_modrev = init_va_filerev(); 286 *depp = ldep; 287 return (0); 288} 289 290int 291deupdat(dep, waitfor) 292 struct denode *dep; 293 int waitfor; 294{ 295 int error; 296 struct buf *bp; 297 struct direntry *dirp; 298 struct timespec ts; 299 300 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) 301 return (0); 302 getnanotime(&ts); 303 DETIMES(dep, &ts, &ts, &ts); 304 if ((dep->de_flag & DE_MODIFIED) == 0) 305 return (0); 306 dep->de_flag &= ~DE_MODIFIED; 307 if (dep->de_Attributes & ATTR_DIRECTORY) 308 return (0); 309 if (dep->de_refcnt <= 0) 310 return (0); 311 error = readde(dep, &bp, &dirp); 312 if (error) 313 return (error); 314 DE_EXTERNALIZE(dirp, dep); 315 if (waitfor) 316 return (bwrite(bp)); 317 else { 318 bdwrite(bp); 319 return (0); 320 } 321} 322 323/* 324 * Truncate the file described by dep to the length specified by length. 325 */ 326int 327detrunc(dep, length, flags, cred, td) 328 struct denode *dep; 329 u_long length; 330 int flags; 331 struct ucred *cred; 332 struct thread *td; 333{ 334 int error; 335 int allerror; 336 u_long eofentry; 337 u_long chaintofree; 338 daddr_t bn; 339 int boff; 340 int isadir = dep->de_Attributes & ATTR_DIRECTORY; 341 struct buf *bp; 342 struct msdosfsmount *pmp = dep->de_pmp; 343 344#ifdef MSDOSFS_DEBUG 345 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); 346#endif 347 348 /* 349 * Disallow attempts to truncate the root directory since it is of 350 * fixed size. That's just the way dos filesystems are. We use 351 * the VROOT bit in the vnode because checking for the directory 352 * bit and a startcluster of 0 in the denode is not adequate to 353 * recognize the root directory at this point in a file or 354 * directory's life. 355 */ 356 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) { 357 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", 358 dep->de_dirclust, dep->de_diroffset); 359 return (EINVAL); 360 } 361 362 if (dep->de_FileSize < length) { 363 vnode_pager_setsize(DETOV(dep), length); 364 return deextend(dep, length, cred); 365 } 366 367 /* 368 * If the desired length is 0 then remember the starting cluster of 369 * the file and set the StartCluster field in the directory entry 370 * to 0. If the desired length is not zero, then get the number of 371 * the last cluster in the shortened file. Then get the number of 372 * the first cluster in the part of the file that is to be freed. 373 * Then set the next cluster pointer in the last cluster of the 374 * file to CLUST_EOFE. 375 */ 376 if (length == 0) { 377 chaintofree = dep->de_StartCluster; 378 dep->de_StartCluster = 0; 379 eofentry = ~0; 380 } else { 381 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 382 &eofentry, 0); 383 if (error) { 384#ifdef MSDOSFS_DEBUG 385 printf("detrunc(): pcbmap fails %d\n", error); 386#endif 387 return (error); 388 } 389 } 390 391 fc_purge(dep, de_clcount(pmp, length)); 392 393 /* 394 * If the new length is not a multiple of the cluster size then we 395 * must zero the tail end of the new last cluster in case it 396 * becomes part of the file again because of a seek. 397 */ 398 if ((boff = length & pmp->pm_crbomask) != 0) { 399 if (isadir) { 400 bn = cntobn(pmp, eofentry); 401 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, 402 NOCRED, &bp); 403 if (error) { 404 brelse(bp); 405#ifdef MSDOSFS_DEBUG 406 printf("detrunc(): bread fails %d\n", error); 407#endif 408 return (error); 409 } 410 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); 411 if (flags & IO_SYNC) 412 bwrite(bp); 413 else 414 bdwrite(bp); 415 } 416 } 417 418 /* 419 * Write out the updated directory entry. Even if the update fails 420 * we free the trailing clusters. 421 */ 422 dep->de_FileSize = length; 423 if (!isadir) 424 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 425 allerror = vtruncbuf(DETOV(dep), cred, td, length, pmp->pm_bpcluster); 426#ifdef MSDOSFS_DEBUG 427 if (allerror) 428 printf("detrunc(): vtruncbuf error %d\n", allerror); 429#endif 430 error = deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC)); 431 if (error != 0 && allerror == 0) 432 allerror = error; 433#ifdef MSDOSFS_DEBUG 434 printf("detrunc(): allerror %d, eofentry %lu\n", 435 allerror, eofentry); 436#endif 437 438 /* 439 * If we need to break the cluster chain for the file then do it 440 * now. 441 */ 442 if (eofentry != ~0) { 443 error = fatentry(FAT_GET_AND_SET, pmp, eofentry, 444 &chaintofree, CLUST_EOFE); 445 if (error) { 446#ifdef MSDOSFS_DEBUG 447 printf("detrunc(): fatentry errors %d\n", error); 448#endif 449 return (error); 450 } 451 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), 452 eofentry); 453 } 454 455 /* 456 * Now free the clusters removed from the file because of the 457 * truncation. 458 */ 459 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) 460 freeclusterchain(pmp, chaintofree); 461 462 return (allerror); 463} 464 465/* 466 * Extend the file described by dep to length specified by length. 467 */ 468int 469deextend(dep, length, cred) 470 struct denode *dep; 471 u_long length; 472 struct ucred *cred; 473{ 474 struct msdosfsmount *pmp = dep->de_pmp; 475 u_long count; 476 int error; 477 478 /* 479 * The root of a DOS filesystem cannot be extended. 480 */ 481 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) 482 return (EINVAL); 483 484 /* 485 * Directories cannot be extended. 486 */ 487 if (dep->de_Attributes & ATTR_DIRECTORY) 488 return (EISDIR); 489 490 if (length <= dep->de_FileSize) 491 panic("deextend: file too large"); 492 493 /* 494 * Compute the number of clusters to allocate. 495 */ 496 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); 497 if (count > 0) { 498 if (count > pmp->pm_freeclustercount) 499 return (ENOSPC); 500 error = extendfile(dep, count, NULL, NULL, DE_CLEAR); 501 if (error) { 502 /* truncate the added clusters away again */ 503 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL); 504 return (error); 505 } 506 } 507 dep->de_FileSize = length; 508 dep->de_flag |= DE_UPDATE | DE_MODIFIED; 509 return (deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC))); 510} 511 512/* 513 * Move a denode to its correct hash queue after the file it represents has 514 * been moved to a new directory. 515 */ 516void 517reinsert(dep) 518 struct denode *dep; 519{ 520 struct vnode *vp; 521 522 /* 523 * Fix up the denode cache. If the denode is for a directory, 524 * there is nothing to do since the hash is based on the starting 525 * cluster of the directory file and that hasn't changed. If for a 526 * file the hash is based on the location of the directory entry, 527 * so we must remove it from the cache and re-enter it with the 528 * hash based on the new location of the directory entry. 529 */ 530#if 0 531 if (dep->de_Attributes & ATTR_DIRECTORY) 532 return; 533#endif 534 vp = DETOV(dep); 535 dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust + 536 dep->de_diroffset; 537 vfs_hash_rehash(vp, dep->de_inode); 538} 539 540int 541msdosfs_reclaim(ap) 542 struct vop_reclaim_args /* { 543 struct vnode *a_vp; 544 } */ *ap; 545{ 546 struct vnode *vp = ap->a_vp; 547 struct denode *dep = VTODE(vp); 548 549#ifdef MSDOSFS_DEBUG 550 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", 551 dep, dep->de_Name, dep->de_refcnt); 552#endif 553 554 if (prtactive && vrefcnt(vp) != 0) 555 vprint("msdosfs_reclaim(): pushing active", vp); 556 /* 557 * Destroy the vm object and flush associated pages. 558 */ 559 vnode_destroy_vobject(vp); 560 /* 561 * Remove the denode from its hash chain. 562 */ 563 vfs_hash_remove(vp); 564 /* 565 * Purge old data structures associated with the denode. 566 */ 567#if 0 /* XXX */ 568 dep->de_flag = 0; 569#endif 570 free(dep, M_MSDOSFSNODE); 571 vp->v_data = NULL; 572 573 return (0); 574} 575 576int 577msdosfs_inactive(ap) 578 struct vop_inactive_args /* { 579 struct vnode *a_vp; 580 struct thread *a_td; 581 } */ *ap; 582{ 583 struct vnode *vp = ap->a_vp; 584 struct denode *dep = VTODE(vp); 585 struct thread *td = ap->a_td; 586 int error = 0; 587 588#ifdef MSDOSFS_DEBUG 589 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); 590#endif 591 592 if (prtactive && vrefcnt(vp) != 0) 593 vprint("msdosfs_inactive(): pushing active", vp); 594 595 /* 596 * Ignore denodes related to stale file handles. 597 */ 598 if (dep->de_Name[0] == SLOT_DELETED) 599 goto out; 600 601 /* 602 * If the file has been deleted and it is on a read/write 603 * filesystem, then truncate the file, and mark the directory slot 604 * as empty. (This may not be necessary for the dos filesystem.) 605 */ 606#ifdef MSDOSFS_DEBUG 607 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", 608 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); 609#endif 610 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 611 error = detrunc(dep, (u_long) 0, 0, NOCRED, td); 612 dep->de_flag |= DE_UPDATE; 613 dep->de_Name[0] = SLOT_DELETED; 614 } 615 deupdat(dep, 0); 616 617out: 618 /* 619 * If we are done with the denode, reclaim it 620 * so that it can be reused immediately. 621 */ 622#ifdef MSDOSFS_DEBUG 623 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", 624 vrefcnt(vp), dep->de_Name[0]); 625#endif 626 if (dep->de_Name[0] == SLOT_DELETED) 627 vrecycle(vp, td); 628 return (error); 629}
|