2 /* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
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.
51 #include <sys/param.h>
52 #include <sys/systm.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>
61 #include <vm/vm_extern.h>
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>
69 static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part");
72 de_vncmpf(struct vnode *vp, void *arg)
79 return (de->de_inode != *a);
83 * If deget() succeeds it returns with the gotten denode locked().
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.
95 deget(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 */
103 struct mount *mntp = pmp->pm_mountp;
104 struct direntry *direntptr;
106 struct vnode *nvp, *xvp;
110 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
111 pmp, dirclust, diroffset, depp);
115 * On FAT32 filesystems, root is a (more or less) normal
118 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
119 dirclust = pmp->pm_rootdirblk;
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
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.
133 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
135 error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp,
141 KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust"));
142 KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset"));
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.
151 ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
154 * Directory entry was not in cache, have to create a vnode and
155 * copy it from the passed disk buffer.
157 /* getnewvnode() does a VREF() on the vnode */
158 error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp);
161 free(ldep, M_MSDOSFSNODE);
165 ldep->de_vnode = nvp;
167 ldep->de_dirclust = dirclust;
168 ldep->de_diroffset = diroffset;
169 ldep->de_inode = inode;
170 lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL);
171 fc_purge(ldep, 0); /* init the fat cache for this denode */
172 error = insmntque(nvp, mntp);
174 free(ldep, M_MSDOSFSNODE);
178 error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp,
192 * Copy the directory entry into the denode area of the vnode.
194 if ((dirclust == MSDOSFSROOT
195 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
196 && diroffset == MSDOSFSROOT_OFS) {
198 * Directory entry for the root directory. There isn't one,
199 * so we manufacture one. We should probably rummage
200 * through the root directory and find a label entry (if it
201 * exists), and then use the time and date from that entry
202 * as the time and date for the root denode.
204 nvp->v_vflag |= VV_ROOT; /* should be further down XXX */
206 ldep->de_Attributes = ATTR_DIRECTORY;
207 ldep->de_LowerCase = 0;
209 ldep->de_StartCluster = pmp->pm_rootdirblk;
210 /* de_FileSize will be filled in further down */
212 ldep->de_StartCluster = MSDOSFSROOT;
213 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
216 * fill in time and date so that fattime2timespec() doesn't
217 * spit up when called from msdosfs_getattr() with root
221 ldep->de_CTime = 0x0000; /* 00:00:00 */
222 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
223 | (1 << DD_DAY_SHIFT);
225 ldep->de_ADate = ldep->de_CDate;
226 ldep->de_MTime = ldep->de_CTime;
227 ldep->de_MDate = ldep->de_CDate;
228 /* leave the other fields as garbage */
230 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
233 * The denode does not contain anything useful, so
234 * it would be wrong to leave it on its hash chain.
235 * Arrange for vput() to just forget about it.
237 ldep->de_Name[0] = SLOT_DELETED;
243 (void)DE_INTERNALIZE(ldep, direntptr);
248 * Fill in a few fields of the vnode and finish filling in the
249 * denode. Then return the address of the found denode.
251 if (ldep->de_Attributes & ATTR_DIRECTORY) {
253 * Since DOS directory entries that describe directories
254 * have 0 in the filesize field, we take this opportunity
255 * to find out the length of the directory and plug it into
256 * the denode structure.
261 * XXX it sometimes happens that the "." entry has cluster
262 * number 0 when it shouldn't. Use the actual cluster number
263 * instead of what is written in directory entry.
265 if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
267 printf("deget(): \".\" entry at clust %lu != %lu\n",
268 dirclust, ldep->de_StartCluster);
270 ldep->de_StartCluster = dirclust;
274 if (ldep->de_StartCluster != MSDOSFSROOT) {
275 error = pcbmap(ldep, 0xffff, 0, &size, 0);
276 if (error == E2BIG) {
277 ldep->de_FileSize = de_cn2off(pmp, size);
281 printf("deget(): pcbmap returned %d\n", error);
287 ldep->de_modrev = init_va_filerev();
293 deupdat(dep, waitfor)
299 struct direntry *dirp;
302 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
305 DETIMES(dep, &ts, &ts, &ts);
306 if ((dep->de_flag & DE_MODIFIED) == 0)
308 dep->de_flag &= ~DE_MODIFIED;
309 if (dep->de_Attributes & ATTR_DIRECTORY)
311 if (dep->de_refcnt <= 0)
313 error = readde(dep, &bp, &dirp);
316 DE_EXTERNALIZE(dirp, dep);
326 * Truncate the file described by dep to the length specified by length.
329 detrunc(dep, length, flags, cred, td)
342 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
344 struct msdosfsmount *pmp = dep->de_pmp;
347 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
351 * Disallow attempts to truncate the root directory since it is of
352 * fixed size. That's just the way dos filesystems are. We use
353 * the VROOT bit in the vnode because checking for the directory
354 * bit and a startcluster of 0 in the denode is not adequate to
355 * recognize the root directory at this point in a file or
358 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
360 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
361 dep->de_dirclust, dep->de_diroffset);
366 if (dep->de_FileSize < length) {
367 vnode_pager_setsize(DETOV(dep), length);
368 return deextend(dep, length, cred);
372 * If the desired length is 0 then remember the starting cluster of
373 * the file and set the StartCluster field in the directory entry
374 * to 0. If the desired length is not zero, then get the number of
375 * the last cluster in the shortened file. Then get the number of
376 * the first cluster in the part of the file that is to be freed.
377 * Then set the next cluster pointer in the last cluster of the
378 * file to CLUST_EOFE.
381 chaintofree = dep->de_StartCluster;
382 dep->de_StartCluster = 0;
385 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
389 printf("detrunc(): pcbmap fails %d\n", error);
395 fc_purge(dep, de_clcount(pmp, length));
398 * If the new length is not a multiple of the cluster size then we
399 * must zero the tail end of the new last cluster in case it
400 * becomes part of the file again because of a seek.
402 if ((boff = length & pmp->pm_crbomask) != 0) {
404 bn = cntobn(pmp, eofentry);
405 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
410 printf("detrunc(): bread fails %d\n", error);
414 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
423 * Write out the updated directory entry. Even if the update fails
424 * we free the trailing clusters.
426 dep->de_FileSize = length;
428 dep->de_flag |= DE_UPDATE | DE_MODIFIED;
429 allerror = vtruncbuf(DETOV(dep), cred, td, length, pmp->pm_bpcluster);
432 printf("detrunc(): vtruncbuf error %d\n", allerror);
434 error = deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC));
435 if (error != 0 && allerror == 0)
438 printf("detrunc(): allerror %d, eofentry %lu\n",
443 * If we need to break the cluster chain for the file then do it
446 if (eofentry != ~0) {
447 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
448 &chaintofree, CLUST_EOFE);
451 printf("detrunc(): fatentry errors %d\n", error);
455 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
460 * Now free the clusters removed from the file because of the
463 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
464 freeclusterchain(pmp, chaintofree);
470 * Extend the file described by dep to length specified by length.
473 deextend(dep, length, cred)
478 struct msdosfsmount *pmp = dep->de_pmp;
483 * The root of a DOS filesystem cannot be extended.
485 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
489 * Directories cannot be extended.
491 if (dep->de_Attributes & ATTR_DIRECTORY)
494 if (length <= dep->de_FileSize)
495 panic("deextend: file too large");
498 * Compute the number of clusters to allocate.
500 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
502 if (count > pmp->pm_freeclustercount)
504 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
506 /* truncate the added clusters away again */
507 (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL);
511 dep->de_FileSize = length;
512 dep->de_flag |= DE_UPDATE | DE_MODIFIED;
513 return (deupdat(dep, !(DETOV(dep)->v_mount->mnt_flag & MNT_ASYNC)));
517 * Move a denode to its correct hash queue after the file it represents has
518 * been moved to a new directory.
527 * Fix up the denode cache. If the denode is for a directory,
528 * there is nothing to do since the hash is based on the starting
529 * cluster of the directory file and that hasn't changed. If for a
530 * file the hash is based on the location of the directory entry,
531 * so we must remove it from the cache and re-enter it with the
532 * hash based on the new location of the directory entry.
535 if (dep->de_Attributes & ATTR_DIRECTORY)
539 dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust +
541 vfs_hash_rehash(vp, dep->de_inode);
546 struct vop_reclaim_args /* {
550 struct vnode *vp = ap->a_vp;
551 struct denode *dep = VTODE(vp);
554 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
555 dep, dep->de_Name, dep->de_refcnt);
559 * Destroy the vm object and flush associated pages.
561 vnode_destroy_vobject(vp);
563 * Remove the denode from its hash chain.
567 * Purge old data structures associated with the denode.
572 free(dep, M_MSDOSFSNODE);
580 struct vop_inactive_args /* {
585 struct vnode *vp = ap->a_vp;
586 struct denode *dep = VTODE(vp);
587 struct thread *td = ap->a_td;
591 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
595 * Ignore denodes related to stale file handles.
597 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
601 * If the file has been deleted and it is on a read/write
602 * filesystem, then truncate the file, and mark the directory slot
603 * as empty. (This may not be necessary for the dos filesystem.)
606 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
607 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
609 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
610 error = detrunc(dep, (u_long) 0, 0, NOCRED, td);
611 dep->de_flag |= DE_UPDATE;
612 dep->de_Name[0] = SLOT_DELETED;
618 * If we are done with the denode, reclaim it
619 * so that it can be reused immediately.
622 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
623 vrefcnt(vp), dep->de_Name[0]);
625 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)