| 1 | /* $FreeBSD: src/sys/msdosfs/msdosfs_denode.c,v 1.47.2.3 2002/08/22 16:20:15 trhodes Exp $ */ |
| 2 | /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_denode.c,v 1.14 2004/08/17 18:57:34 dillon Exp $ */ |
| 3 | /* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */ |
| 4 | |
| 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/systm.h> |
| 54 | #include <sys/kernel.h> |
| 55 | #include <sys/mount.h> |
| 56 | #include <sys/malloc.h> |
| 57 | #include <sys/proc.h> |
| 58 | #include <sys/buf.h> |
| 59 | #include <sys/vnode.h> |
| 60 | |
| 61 | #include <vm/vm.h> |
| 62 | #include <vm/vm_extern.h> |
| 63 | |
| 64 | #include "bpb.h" |
| 65 | #include "msdosfsmount.h" |
| 66 | #include "direntry.h" |
| 67 | #include "denode.h" |
| 68 | #include "fat.h" |
| 69 | |
| 70 | static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part"); |
| 71 | |
| 72 | static struct denode **dehashtbl; |
| 73 | static u_long dehash; /* size of hash table - 1 */ |
| 74 | #define DEHASH(dev, dcl, doff) (dehashtbl[(minor(dev) + (dcl) + (doff) / \ |
| 75 | sizeof(struct direntry)) & dehash]) |
| 76 | static struct lwkt_token dehash_token; |
| 77 | |
| 78 | union _qcvt { |
| 79 | quad_t qcvt; |
| 80 | long val[2]; |
| 81 | }; |
| 82 | #define SETHIGH(q, h) { \ |
| 83 | union _qcvt tmp; \ |
| 84 | tmp.qcvt = (q); \ |
| 85 | tmp.val[_QUAD_HIGHWORD] = (h); \ |
| 86 | (q) = tmp.qcvt; \ |
| 87 | } |
| 88 | #define SETLOW(q, l) { \ |
| 89 | union _qcvt tmp; \ |
| 90 | tmp.qcvt = (q); \ |
| 91 | tmp.val[_QUAD_LOWWORD] = (l); \ |
| 92 | (q) = tmp.qcvt; \ |
| 93 | } |
| 94 | |
| 95 | static struct denode * |
| 96 | msdosfs_hashget (dev_t dev, u_long dirclust, |
| 97 | u_long diroff); |
| 98 | static void msdosfs_hashins (struct denode *dep); |
| 99 | static void msdosfs_hashrem (struct denode *dep); |
| 100 | |
| 101 | /*ARGSUSED*/ |
| 102 | int |
| 103 | msdosfs_init(struct vfsconf *vfsp) |
| 104 | { |
| 105 | dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, &dehash); |
| 106 | lwkt_token_init(&dehash_token); |
| 107 | return (0); |
| 108 | } |
| 109 | |
| 110 | int |
| 111 | msdosfs_uninit(struct vfsconf *vfsp) |
| 112 | { |
| 113 | |
| 114 | if (dehashtbl) |
| 115 | free(dehashtbl, M_MSDOSFSMNT); |
| 116 | return (0); |
| 117 | } |
| 118 | |
| 119 | static struct denode * |
| 120 | msdosfs_hashget(dev_t dev, u_long dirclust, u_long diroff) |
| 121 | { |
| 122 | struct thread *td = curthread; /* XXX */ |
| 123 | struct denode *dep; |
| 124 | lwkt_tokref ilock; |
| 125 | lwkt_tokref vlock; |
| 126 | struct vnode *vp; |
| 127 | |
| 128 | lwkt_gettoken(&ilock, &dehash_token); |
| 129 | loop: |
| 130 | for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) { |
| 131 | if (dirclust != dep->de_dirclust |
| 132 | || diroff != dep->de_diroffset |
| 133 | || dev != dep->de_dev |
| 134 | || dep->de_refcnt == 0) { |
| 135 | continue; |
| 136 | } |
| 137 | vp = DETOV(dep); |
| 138 | lwkt_gettoken(&vlock, vp->v_interlock); |
| 139 | /* |
| 140 | * We must check to see if the inode has been ripped |
| 141 | * out from under us after blocking. |
| 142 | */ |
| 143 | for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) { |
| 144 | if (dirclust == dep->de_dirclust |
| 145 | && diroff == dep->de_diroffset |
| 146 | && dev == dep->de_dev |
| 147 | && dep->de_refcnt != 0) { |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | if (dep == NULL || DETOV(dep) != vp) { |
| 152 | lwkt_reltoken(&vlock); |
| 153 | goto loop; |
| 154 | } |
| 155 | if (vget(vp, &vlock, LK_EXCLUSIVE | LK_INTERLOCK, td)) |
| 156 | goto loop; |
| 157 | lwkt_reltoken(&ilock); |
| 158 | return (dep); |
| 159 | } |
| 160 | lwkt_reltoken(&ilock); |
| 161 | return (NULL); |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | msdosfs_hashins(struct denode *dep) |
| 166 | { |
| 167 | struct denode **depp, *deq; |
| 168 | lwkt_tokref ilock; |
| 169 | |
| 170 | lwkt_gettoken(&ilock, &dehash_token); |
| 171 | depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset); |
| 172 | deq = *depp; |
| 173 | if (deq) |
| 174 | deq->de_prev = &dep->de_next; |
| 175 | dep->de_next = deq; |
| 176 | dep->de_prev = depp; |
| 177 | *depp = dep; |
| 178 | lwkt_reltoken(&ilock); |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | msdosfs_hashrem(struct denode *dep) |
| 183 | { |
| 184 | struct denode *deq; |
| 185 | lwkt_tokref ilock; |
| 186 | |
| 187 | lwkt_gettoken(&ilock, &dehash_token); |
| 188 | deq = dep->de_next; |
| 189 | if (deq) |
| 190 | deq->de_prev = dep->de_prev; |
| 191 | *dep->de_prev = deq; |
| 192 | #ifdef DIAGNOSTIC |
| 193 | dep->de_next = NULL; |
| 194 | dep->de_prev = NULL; |
| 195 | #endif |
| 196 | lwkt_reltoken(&ilock); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * If deget() succeeds it returns with the gotten denode locked(). |
| 201 | * |
| 202 | * pmp - address of msdosfsmount structure of the filesystem containing |
| 203 | * the denode of interest. The pm_dev field and the address of |
| 204 | * the msdosfsmount structure are used. |
| 205 | * dirclust - which cluster bp contains, if dirclust is 0 (root directory) |
| 206 | * diroffset is relative to the beginning of the root directory, |
| 207 | * otherwise it is cluster relative. |
| 208 | * diroffset - offset past begin of cluster of denode we want |
| 209 | * depp - returns the address of the gotten denode. |
| 210 | */ |
| 211 | int |
| 212 | deget(struct msdosfsmount *pmp, /* so we know the maj/min number */ |
| 213 | u_long dirclust, /* cluster this dir entry came from */ |
| 214 | u_long diroffset, /* index of entry within the cluster */ |
| 215 | struct denode **depp) /* returns the addr of the gotten denode */ |
| 216 | { |
| 217 | struct thread *td = curthread; /* XXX */ |
| 218 | int error; |
| 219 | dev_t dev = pmp->pm_dev; |
| 220 | struct mount *mntp = pmp->pm_mountp; |
| 221 | struct direntry *direntptr; |
| 222 | struct denode *ldep; |
| 223 | struct vnode *nvp; |
| 224 | struct buf *bp; |
| 225 | struct timeval tv; |
| 226 | |
| 227 | #ifdef MSDOSFS_DEBUG |
| 228 | printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n", |
| 229 | pmp, dirclust, diroffset, depp); |
| 230 | #endif |
| 231 | |
| 232 | /* |
| 233 | * On FAT32 filesystems, root is a (more or less) normal |
| 234 | * directory |
| 235 | */ |
| 236 | if (FAT32(pmp) && dirclust == MSDOSFSROOT) |
| 237 | dirclust = pmp->pm_rootdirblk; |
| 238 | |
| 239 | /* |
| 240 | * See if the denode is in the denode cache. Use the location of |
| 241 | * the directory entry to compute the hash value. For subdir use |
| 242 | * address of "." entry. For root dir (if not FAT32) use cluster |
| 243 | * MSDOSFSROOT, offset MSDOSFSROOT_OFS |
| 244 | * |
| 245 | * NOTE: The check for de_refcnt > 0 below insures the denode being |
| 246 | * examined does not represent an unlinked but still open file. |
| 247 | * These files are not to be accessible even when the directory |
| 248 | * entry that represented the file happens to be reused while the |
| 249 | * deleted file is still open. |
| 250 | */ |
| 251 | ldep = msdosfs_hashget(dev, dirclust, diroffset); |
| 252 | if (ldep) { |
| 253 | *depp = ldep; |
| 254 | return (0); |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Do the MALLOC before the getnewvnode since doing so afterward |
| 259 | * might cause a bogus v_data pointer to get dereferenced |
| 260 | * elsewhere if MALLOC should block. |
| 261 | */ |
| 262 | MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK); |
| 263 | |
| 264 | /* |
| 265 | * Directory entry was not in cache, have to create a vnode and |
| 266 | * copy it from the passed disk buffer. |
| 267 | */ |
| 268 | /* getnewvnode() does a vref() on the vnode */ |
| 269 | error = getnewvnode(VT_MSDOSFS, mntp, mntp->mnt_vn_ops, &nvp); |
| 270 | if (error) { |
| 271 | *depp = NULL; |
| 272 | FREE(ldep, M_MSDOSFSNODE); |
| 273 | return error; |
| 274 | } |
| 275 | bzero((caddr_t)ldep, sizeof *ldep); |
| 276 | lockinit(&ldep->de_lock, 0, "denode", VLKTIMEOUT, 0); |
| 277 | nvp->v_data = ldep; |
| 278 | ldep->de_vnode = nvp; |
| 279 | ldep->de_flag = 0; |
| 280 | ldep->de_devvp = 0; |
| 281 | ldep->de_dev = dev; |
| 282 | ldep->de_dirclust = dirclust; |
| 283 | ldep->de_diroffset = diroffset; |
| 284 | fc_purge(ldep, 0); /* init the fat cache for this denode */ |
| 285 | |
| 286 | /* |
| 287 | * Lock the denode so that it can't be accessed until we've read |
| 288 | * it in and have done what we need to it. Do this here instead |
| 289 | * of at the start of msdosfs_hashins() so that reinsert() can |
| 290 | * call msdosfs_hashins() with a locked denode. |
| 291 | */ |
| 292 | if (lockmgr(&ldep->de_lock, LK_EXCLUSIVE, NULL, td)) |
| 293 | panic("deget: unexpected lock failure"); |
| 294 | |
| 295 | /* |
| 296 | * Insert the denode into the hash queue. |
| 297 | */ |
| 298 | msdosfs_hashins(ldep); |
| 299 | |
| 300 | ldep->de_pmp = pmp; |
| 301 | ldep->de_refcnt = 1; |
| 302 | /* |
| 303 | * Copy the directory entry into the denode area of the vnode. |
| 304 | */ |
| 305 | if ((dirclust == MSDOSFSROOT |
| 306 | || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)) |
| 307 | && diroffset == MSDOSFSROOT_OFS) { |
| 308 | /* |
| 309 | * Directory entry for the root directory. There isn't one, |
| 310 | * so we manufacture one. We should probably rummage |
| 311 | * through the root directory and find a label entry (if it |
| 312 | * exists), and then use the time and date from that entry |
| 313 | * as the time and date for the root denode. |
| 314 | */ |
| 315 | nvp->v_flag |= VROOT; /* should be further down XXX */ |
| 316 | |
| 317 | ldep->de_Attributes = ATTR_DIRECTORY; |
| 318 | ldep->de_LowerCase = 0; |
| 319 | if (FAT32(pmp)) |
| 320 | ldep->de_StartCluster = pmp->pm_rootdirblk; |
| 321 | /* de_FileSize will be filled in further down */ |
| 322 | else { |
| 323 | ldep->de_StartCluster = MSDOSFSROOT; |
| 324 | ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE; |
| 325 | } |
| 326 | /* |
| 327 | * fill in time and date so that dos2unixtime() doesn't |
| 328 | * spit up when called from msdosfs_getattr() with root |
| 329 | * denode |
| 330 | */ |
| 331 | ldep->de_CHun = 0; |
| 332 | ldep->de_CTime = 0x0000; /* 00:00:00 */ |
| 333 | ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT) |
| 334 | | (1 << DD_DAY_SHIFT); |
| 335 | /* Jan 1, 1980 */ |
| 336 | ldep->de_ADate = ldep->de_CDate; |
| 337 | ldep->de_MTime = ldep->de_CTime; |
| 338 | ldep->de_MDate = ldep->de_CDate; |
| 339 | /* leave the other fields as garbage */ |
| 340 | } else { |
| 341 | error = readep(pmp, dirclust, diroffset, &bp, &direntptr); |
| 342 | if (error) { |
| 343 | /* |
| 344 | * The denode does not contain anything useful, so |
| 345 | * it would be wrong to leave it on its hash chain. |
| 346 | * Arrange for vput() to just forget about it. |
| 347 | */ |
| 348 | ldep->de_Name[0] = SLOT_DELETED; |
| 349 | |
| 350 | vput(nvp); |
| 351 | *depp = NULL; |
| 352 | return (error); |
| 353 | } |
| 354 | DE_INTERNALIZE(ldep, direntptr); |
| 355 | brelse(bp); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Fill in a few fields of the vnode and finish filling in the |
| 360 | * denode. Then return the address of the found denode. |
| 361 | */ |
| 362 | if (ldep->de_Attributes & ATTR_DIRECTORY) { |
| 363 | /* |
| 364 | * Since DOS directory entries that describe directories |
| 365 | * have 0 in the filesize field, we take this opportunity |
| 366 | * to find out the length of the directory and plug it into |
| 367 | * the denode structure. |
| 368 | */ |
| 369 | u_long size; |
| 370 | |
| 371 | /* |
| 372 | * XXX Sometimes, these arrives that . entry have cluster |
| 373 | * number 0, when it shouldn't. Use real cluster number |
| 374 | * instead of what is written in directory entry. |
| 375 | */ |
| 376 | if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) { |
| 377 | printf("deget(): . entry at clust %ld != %ld\n", |
| 378 | dirclust, ldep->de_StartCluster); |
| 379 | ldep->de_StartCluster = dirclust; |
| 380 | } |
| 381 | |
| 382 | nvp->v_type = VDIR; |
| 383 | if (ldep->de_StartCluster != MSDOSFSROOT) { |
| 384 | error = pcbmap(ldep, 0xffff, 0, &size, 0); |
| 385 | if (error == E2BIG) { |
| 386 | ldep->de_FileSize = de_cn2off(pmp, size); |
| 387 | error = 0; |
| 388 | } else |
| 389 | printf("deget(): pcbmap returned %d\n", error); |
| 390 | } |
| 391 | } else |
| 392 | nvp->v_type = VREG; |
| 393 | getmicrouptime(&tv); |
| 394 | SETHIGH(ldep->de_modrev, tv.tv_sec); |
| 395 | SETLOW(ldep->de_modrev, tv.tv_usec * 4294); |
| 396 | ldep->de_devvp = pmp->pm_devvp; |
| 397 | vref(ldep->de_devvp); |
| 398 | *depp = ldep; |
| 399 | return (0); |
| 400 | } |
| 401 | |
| 402 | int |
| 403 | deupdat(struct denode *dep, int waitfor) |
| 404 | { |
| 405 | int error; |
| 406 | struct buf *bp; |
| 407 | struct direntry *dirp; |
| 408 | struct timespec ts; |
| 409 | |
| 410 | if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) |
| 411 | return (0); |
| 412 | getnanotime(&ts); |
| 413 | DETIMES(dep, &ts, &ts, &ts); |
| 414 | if ((dep->de_flag & DE_MODIFIED) == 0) |
| 415 | return (0); |
| 416 | dep->de_flag &= ~DE_MODIFIED; |
| 417 | if (dep->de_Attributes & ATTR_DIRECTORY) |
| 418 | return (0); |
| 419 | if (dep->de_refcnt <= 0) |
| 420 | return (0); |
| 421 | error = readde(dep, &bp, &dirp); |
| 422 | if (error) |
| 423 | return (error); |
| 424 | DE_EXTERNALIZE(dirp, dep); |
| 425 | if (waitfor) |
| 426 | return (bwrite(bp)); |
| 427 | else { |
| 428 | bdwrite(bp); |
| 429 | return (0); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Truncate the file described by dep to the length specified by length. |
| 435 | */ |
| 436 | int |
| 437 | detrunc(struct denode *dep, u_long length, int flags, struct thread *td) |
| 438 | { |
| 439 | int error; |
| 440 | int allerror; |
| 441 | u_long eofentry; |
| 442 | u_long chaintofree; |
| 443 | daddr_t bn; |
| 444 | int boff; |
| 445 | int isadir = dep->de_Attributes & ATTR_DIRECTORY; |
| 446 | struct buf *bp; |
| 447 | struct msdosfsmount *pmp = dep->de_pmp; |
| 448 | |
| 449 | #ifdef MSDOSFS_DEBUG |
| 450 | printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags); |
| 451 | #endif |
| 452 | |
| 453 | /* |
| 454 | * Disallow attempts to truncate the root directory since it is of |
| 455 | * fixed size. That's just the way dos filesystems are. We use |
| 456 | * the VROOT bit in the vnode because checking for the directory |
| 457 | * bit and a startcluster of 0 in the denode is not adequate to |
| 458 | * recognize the root directory at this point in a file or |
| 459 | * directory's life. |
| 460 | */ |
| 461 | if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) { |
| 462 | printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n", |
| 463 | dep->de_dirclust, dep->de_diroffset); |
| 464 | return (EINVAL); |
| 465 | } |
| 466 | |
| 467 | |
| 468 | if (dep->de_FileSize < length) { |
| 469 | vnode_pager_setsize(DETOV(dep), length); |
| 470 | return deextend(dep, length); |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * If the desired length is 0 then remember the starting cluster of |
| 475 | * the file and set the StartCluster field in the directory entry |
| 476 | * to 0. If the desired length is not zero, then get the number of |
| 477 | * the last cluster in the shortened file. Then get the number of |
| 478 | * the first cluster in the part of the file that is to be freed. |
| 479 | * Then set the next cluster pointer in the last cluster of the |
| 480 | * file to CLUST_EOFE. |
| 481 | */ |
| 482 | if (length == 0) { |
| 483 | chaintofree = dep->de_StartCluster; |
| 484 | dep->de_StartCluster = 0; |
| 485 | eofentry = ~0; |
| 486 | } else { |
| 487 | error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, |
| 488 | &eofentry, 0); |
| 489 | if (error) { |
| 490 | #ifdef MSDOSFS_DEBUG |
| 491 | printf("detrunc(): pcbmap fails %d\n", error); |
| 492 | #endif |
| 493 | return (error); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | fc_purge(dep, de_clcount(pmp, length)); |
| 498 | |
| 499 | /* |
| 500 | * If the new length is not a multiple of the cluster size then we |
| 501 | * must zero the tail end of the new last cluster in case it |
| 502 | * becomes part of the file again because of a seek. |
| 503 | */ |
| 504 | if ((boff = length & pmp->pm_crbomask) != 0) { |
| 505 | if (isadir) { |
| 506 | bn = cntobn(pmp, eofentry); |
| 507 | error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp); |
| 508 | } else { |
| 509 | bn = de_blk(pmp, length); |
| 510 | error = bread(DETOV(dep), bn, pmp->pm_bpcluster, &bp); |
| 511 | } |
| 512 | if (error) { |
| 513 | brelse(bp); |
| 514 | #ifdef MSDOSFS_DEBUG |
| 515 | printf("detrunc(): bread fails %d\n", error); |
| 516 | #endif |
| 517 | return (error); |
| 518 | } |
| 519 | /* |
| 520 | * is this the right place for it? |
| 521 | */ |
| 522 | bzero(bp->b_data + boff, pmp->pm_bpcluster - boff); |
| 523 | if (flags & IO_SYNC) |
| 524 | bwrite(bp); |
| 525 | else |
| 526 | bdwrite(bp); |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * Write out the updated directory entry. Even if the update fails |
| 531 | * we free the trailing clusters. |
| 532 | */ |
| 533 | dep->de_FileSize = length; |
| 534 | if (!isadir) |
| 535 | dep->de_flag |= DE_UPDATE|DE_MODIFIED; |
| 536 | allerror = vtruncbuf(DETOV(dep), td, length, pmp->pm_bpcluster); |
| 537 | #ifdef MSDOSFS_DEBUG |
| 538 | if (allerror) |
| 539 | printf("detrunc(): vtruncbuf error %d\n", allerror); |
| 540 | #endif |
| 541 | error = deupdat(dep, 1); |
| 542 | if (error && (allerror == 0)) |
| 543 | allerror = error; |
| 544 | #ifdef MSDOSFS_DEBUG |
| 545 | printf("detrunc(): allerror %d, eofentry %lu\n", |
| 546 | allerror, eofentry); |
| 547 | #endif |
| 548 | |
| 549 | /* |
| 550 | * If we need to break the cluster chain for the file then do it |
| 551 | * now. |
| 552 | */ |
| 553 | if (eofentry != ~0) { |
| 554 | error = fatentry(FAT_GET_AND_SET, pmp, eofentry, |
| 555 | &chaintofree, CLUST_EOFE); |
| 556 | if (error) { |
| 557 | #ifdef MSDOSFS_DEBUG |
| 558 | printf("detrunc(): fatentry errors %d\n", error); |
| 559 | #endif |
| 560 | return (error); |
| 561 | } |
| 562 | fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1), |
| 563 | eofentry); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Now free the clusters removed from the file because of the |
| 568 | * truncation. |
| 569 | */ |
| 570 | if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree)) |
| 571 | freeclusterchain(pmp, chaintofree); |
| 572 | |
| 573 | return (allerror); |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Extend the file described by dep to length specified by length. |
| 578 | */ |
| 579 | int |
| 580 | deextend(struct denode *dep, u_long length) |
| 581 | { |
| 582 | struct msdosfsmount *pmp = dep->de_pmp; |
| 583 | u_long count; |
| 584 | int error; |
| 585 | |
| 586 | /* |
| 587 | * The root of a DOS filesystem cannot be extended. |
| 588 | */ |
| 589 | if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) |
| 590 | return (EINVAL); |
| 591 | |
| 592 | /* |
| 593 | * Directories cannot be extended. |
| 594 | */ |
| 595 | if (dep->de_Attributes & ATTR_DIRECTORY) |
| 596 | return (EISDIR); |
| 597 | |
| 598 | if (length <= dep->de_FileSize) |
| 599 | panic("deextend: file too large"); |
| 600 | |
| 601 | /* |
| 602 | * Compute the number of clusters to allocate. |
| 603 | */ |
| 604 | count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize); |
| 605 | if (count > 0) { |
| 606 | if (count > pmp->pm_freeclustercount) |
| 607 | return (ENOSPC); |
| 608 | error = extendfile(dep, count, NULL, NULL, DE_CLEAR); |
| 609 | if (error) { |
| 610 | /* truncate the added clusters away again */ |
| 611 | (void) detrunc(dep, dep->de_FileSize, 0, NULL); |
| 612 | return (error); |
| 613 | } |
| 614 | } |
| 615 | dep->de_FileSize = length; |
| 616 | dep->de_flag |= DE_UPDATE|DE_MODIFIED; |
| 617 | return (deupdat(dep, 1)); |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * Move a denode to its correct hash queue after the file it represents has |
| 622 | * been moved to a new directory. |
| 623 | */ |
| 624 | void |
| 625 | reinsert(struct denode *dep) |
| 626 | { |
| 627 | /* |
| 628 | * Fix up the denode cache. If the denode is for a directory, |
| 629 | * there is nothing to do since the hash is based on the starting |
| 630 | * cluster of the directory file and that hasn't changed. If for a |
| 631 | * file the hash is based on the location of the directory entry, |
| 632 | * so we must remove it from the cache and re-enter it with the |
| 633 | * hash based on the new location of the directory entry. |
| 634 | */ |
| 635 | if (dep->de_Attributes & ATTR_DIRECTORY) |
| 636 | return; |
| 637 | msdosfs_hashrem(dep); |
| 638 | msdosfs_hashins(dep); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * msdosfs_reclaim(struct vnode *a_vp) |
| 643 | */ |
| 644 | int |
| 645 | msdosfs_reclaim(struct vop_reclaim_args *ap) |
| 646 | { |
| 647 | struct vnode *vp = ap->a_vp; |
| 648 | struct denode *dep = VTODE(vp); |
| 649 | |
| 650 | #ifdef MSDOSFS_DEBUG |
| 651 | printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n", |
| 652 | dep, dep->de_Name, dep->de_refcnt); |
| 653 | #endif |
| 654 | |
| 655 | if (prtactive && vp->v_usecount != 0) |
| 656 | vprint("msdosfs_reclaim(): pushing active", vp); |
| 657 | /* |
| 658 | * Remove the denode from its hash chain. |
| 659 | */ |
| 660 | msdosfs_hashrem(dep); |
| 661 | /* |
| 662 | * Purge old data structures associated with the denode. |
| 663 | */ |
| 664 | cache_purge(vp); |
| 665 | if (dep->de_devvp) { |
| 666 | vrele(dep->de_devvp); |
| 667 | dep->de_devvp = 0; |
| 668 | } |
| 669 | #if 0 /* XXX */ |
| 670 | dep->de_flag = 0; |
| 671 | #endif |
| 672 | FREE(dep, M_MSDOSFSNODE); |
| 673 | vp->v_data = NULL; |
| 674 | |
| 675 | return (0); |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * msdosfs_inactive(struct vnode *a_vp, struct thread *a_td) |
| 680 | */ |
| 681 | int |
| 682 | msdosfs_inactive(struct vop_inactive_args *ap) |
| 683 | { |
| 684 | struct vnode *vp = ap->a_vp; |
| 685 | struct denode *dep = VTODE(vp); |
| 686 | int error = 0; |
| 687 | |
| 688 | #ifdef MSDOSFS_DEBUG |
| 689 | printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]); |
| 690 | #endif |
| 691 | |
| 692 | if (prtactive && vp->v_usecount != 0) |
| 693 | vprint("msdosfs_inactive(): pushing active", vp); |
| 694 | |
| 695 | /* |
| 696 | * Ignore denodes related to stale file handles. |
| 697 | */ |
| 698 | if (dep->de_Name[0] == SLOT_DELETED) |
| 699 | goto out; |
| 700 | |
| 701 | /* |
| 702 | * If the file has been deleted and it is on a read/write |
| 703 | * filesystem, then truncate the file, and mark the directory slot |
| 704 | * as empty. (This may not be necessary for the dos filesystem.) |
| 705 | */ |
| 706 | #ifdef MSDOSFS_DEBUG |
| 707 | printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n", |
| 708 | dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY); |
| 709 | #endif |
| 710 | if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { |
| 711 | error = detrunc(dep, (u_long) 0, 0, ap->a_td); |
| 712 | dep->de_flag |= DE_UPDATE; |
| 713 | dep->de_Name[0] = SLOT_DELETED; |
| 714 | } |
| 715 | deupdat(dep, 0); |
| 716 | |
| 717 | out: |
| 718 | VOP_UNLOCK(vp, NULL, 0, ap->a_td); |
| 719 | /* |
| 720 | * If we are done with the denode, reclaim it |
| 721 | * so that it can be reused immediately. |
| 722 | */ |
| 723 | #ifdef MSDOSFS_DEBUG |
| 724 | printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount, |
| 725 | dep->de_Name[0]); |
| 726 | #endif |
| 727 | if (dep->de_Name[0] == SLOT_DELETED) |
| 728 | vrecycle(vp, NULL, ap->a_td); |
| 729 | return (error); |
| 730 | } |