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