1 /* $NetBSD: msdosfs_denode.c,v 1.36 2009/03/15 17:15:57 cegger Exp $ */
4 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Written by Paul Popelka (paulp@uts.amdahl.com)
37 * You can do anything you want with this software, just don't say you wrote
38 * it, and don't remove this notice.
40 * This software is provided "as is".
42 * The author supplies this software to be publicly redistributed on the
43 * understanding that the author is not responsible for the correct
44 * functioning of this software in any circumstances and is not liable for
45 * any damages caused by this software.
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_denode.c,v 1.36 2009/03/15 17:15:57 cegger Exp $");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/mount.h>
56 #include <sys/malloc.h>
60 #include <sys/vnode.h>
61 #include <sys/kernel.h> /* defines "time" */
62 #include <sys/dirent.h>
63 #include <sys/namei.h>
64 #include <sys/kauth.h>
66 #include <uvm/uvm_extern.h>
68 #include <fs/msdosfs/bpb.h>
69 #include <fs/msdosfs/msdosfsmount.h>
70 #include <fs/msdosfs/direntry.h>
71 #include <fs/msdosfs/denode.h>
72 #include <fs/msdosfs/fat.h>
74 LIST_HEAD(ihashhead
, denode
) *dehashtbl
;
75 u_long dehash
; /* size of hash table - 1 */
76 #define DEHASH(dev, dcl, doff) \
77 (((dev) + (dcl) + (doff) / sizeof(struct direntry)) & dehash)
79 kmutex_t msdosfs_ihash_lock
;
80 kmutex_t msdosfs_hashlock
;
82 struct pool msdosfs_denode_pool
;
86 static const struct genfs_ops msdosfs_genfsops
= {
87 .gop_size
= genfs_size
,
88 .gop_alloc
= msdosfs_gop_alloc
,
89 .gop_write
= genfs_gop_write
,
90 .gop_markupdate
= msdosfs_gop_markupdate
,
93 static struct denode
*msdosfs_hashget(dev_t
, u_long
, u_long
, int);
94 static void msdosfs_hashins(struct denode
*);
95 static void msdosfs_hashrem(struct denode
*);
97 MALLOC_DECLARE(M_MSDOSFSFAT
);
103 malloc_type_attach(M_MSDOSFSMNT
);
104 malloc_type_attach(M_MSDOSFSFAT
);
105 malloc_type_attach(M_MSDOSFSTMP
);
106 pool_init(&msdosfs_denode_pool
, sizeof(struct denode
), 0, 0, 0,
107 "msdosnopl", &pool_allocator_nointr
, IPL_NONE
);
108 dehashtbl
= hashinit(desiredvnodes
/ 2, HASH_LIST
, true, &dehash
);
109 mutex_init(&msdosfs_ihash_lock
, MUTEX_DEFAULT
, IPL_NONE
);
110 mutex_init(&msdosfs_hashlock
, MUTEX_DEFAULT
, IPL_NONE
);
114 * Reinitialize inode hash table.
121 struct ihashhead
*oldhash
, *hash
;
122 u_long oldmask
, mask
, val
;
125 hash
= hashinit(desiredvnodes
/ 2, HASH_LIST
, true, &mask
);
127 mutex_enter(&msdosfs_ihash_lock
);
132 for (i
= 0; i
<= oldmask
; i
++) {
133 while ((dep
= LIST_FIRST(&oldhash
[i
])) != NULL
) {
134 LIST_REMOVE(dep
, de_hash
);
135 val
= DEHASH(dep
->de_dev
, dep
->de_dirclust
,
137 LIST_INSERT_HEAD(&hash
[val
], dep
, de_hash
);
140 mutex_exit(&msdosfs_ihash_lock
);
141 hashdone(oldhash
, HASH_LIST
, oldmask
);
147 hashdone(dehashtbl
, HASH_LIST
, dehash
);
148 pool_destroy(&msdosfs_denode_pool
);
149 mutex_destroy(&msdosfs_ihash_lock
);
150 mutex_destroy(&msdosfs_hashlock
);
151 malloc_type_detach(M_MSDOSFSTMP
);
152 malloc_type_detach(M_MSDOSFSFAT
);
153 malloc_type_detach(M_MSDOSFSMNT
);
156 static struct denode
*
157 msdosfs_hashget(dev_t dev
, u_long dirclust
, u_long diroff
, int flags
)
163 mutex_enter(&msdosfs_ihash_lock
);
164 LIST_FOREACH(dep
, &dehashtbl
[DEHASH(dev
, dirclust
, diroff
)], de_hash
) {
165 if (dirclust
== dep
->de_dirclust
&&
166 diroff
== dep
->de_diroffset
&&
167 dev
== dep
->de_dev
&&
168 dep
->de_refcnt
!= 0) {
171 mutex_exit(&msdosfs_ihash_lock
);
173 mutex_enter(&vp
->v_interlock
);
174 mutex_exit(&msdosfs_ihash_lock
);
175 if (vget(vp
, flags
| LK_INTERLOCK
))
181 mutex_exit(&msdosfs_ihash_lock
);
186 msdosfs_hashins(struct denode
*dep
)
188 struct ihashhead
*depp
;
191 KASSERT(mutex_owned(&msdosfs_hashlock
));
193 mutex_enter(&msdosfs_ihash_lock
);
194 val
= DEHASH(dep
->de_dev
, dep
->de_dirclust
, dep
->de_diroffset
);
195 depp
= &dehashtbl
[val
];
196 LIST_INSERT_HEAD(depp
, dep
, de_hash
);
197 mutex_exit(&msdosfs_ihash_lock
);
201 msdosfs_hashrem(struct denode
*dep
)
203 mutex_enter(&msdosfs_ihash_lock
);
204 LIST_REMOVE(dep
, de_hash
);
205 mutex_exit(&msdosfs_ihash_lock
);
209 * If deget() succeeds it returns with the gotten denode locked().
211 * pmp - address of msdosfsmount structure of the filesystem containing
212 * the denode of interest. The pm_dev field and the address of
213 * the msdosfsmount structure are used.
214 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
215 * diroffset is relative to the beginning of the root directory,
216 * otherwise it is cluster relative.
217 * diroffset - offset past begin of cluster of denode we want
218 * depp - returns the address of the gotten denode.
221 deget(struct msdosfsmount
*pmp
, u_long dirclust
, u_long diroffset
, struct denode
**depp
)
222 /* pmp: so we know the maj/min number */
223 /* dirclust: cluster this dir entry came from */
224 /* diroffset: index of entry within the cluster */
225 /* depp: returns the addr of the gotten denode */
228 extern int (**msdosfs_vnodeop_p
)(void *);
229 struct direntry
*direntptr
;
235 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
236 pmp
, dirclust
, diroffset
, depp
);
240 * On FAT32 filesystems, root is a (more or less) normal
243 if (FAT32(pmp
) && dirclust
== MSDOSFSROOT
)
244 dirclust
= pmp
->pm_rootdirblk
;
247 * See if the denode is in the denode cache. Use the location of
248 * the directory entry to compute the hash value. For subdir use
249 * address of "." entry. For root dir (if not FAT32) use cluster
250 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
252 * NOTE: The check for de_refcnt > 0 below insures the denode being
253 * examined does not represent an unlinked but still open file.
254 * These files are not to be accessible even when the directory
255 * entry that represented the file happens to be reused while the
256 * deleted file is still open.
259 ldep
= msdosfs_hashget(pmp
->pm_dev
, dirclust
, diroffset
, LK_EXCLUSIVE
);
266 * Directory entry was not in cache, have to create a vnode and
267 * copy it from the passed disk buffer.
269 /* getnewvnode() does a VREF() on the vnode */
270 error
= getnewvnode(VT_MSDOSFS
, pmp
->pm_mountp
,
271 msdosfs_vnodeop_p
, &nvp
);
276 ldep
= pool_get(&msdosfs_denode_pool
, PR_WAITOK
);
279 * If someone beat us to it, put back the freshly allocated
280 * vnode/inode pair and retry.
282 mutex_enter(&msdosfs_hashlock
);
283 if (msdosfs_hashget(pmp
->pm_dev
, dirclust
, diroffset
, 0)) {
284 mutex_exit(&msdosfs_hashlock
);
286 pool_put(&msdosfs_denode_pool
, ldep
);
289 memset(ldep
, 0, sizeof *ldep
);
291 ldep
->de_vnode
= nvp
;
295 ldep
->de_dev
= pmp
->pm_dev
;
296 ldep
->de_dirclust
= dirclust
;
297 ldep
->de_diroffset
= diroffset
;
298 fc_purge(ldep
, 0); /* init the fat cache for this denode */
301 * Insert the denode into the hash queue and lock the denode so it
302 * can't be accessed until we've read it in and have done what we
305 vn_lock(nvp
, LK_EXCLUSIVE
| LK_RETRY
);
306 genfs_node_init(nvp
, &msdosfs_genfsops
);
307 msdosfs_hashins(ldep
);
308 mutex_exit(&msdosfs_hashlock
);
311 ldep
->de_devvp
= pmp
->pm_devvp
;
314 * Copy the directory entry into the denode area of the vnode.
316 if ((dirclust
== MSDOSFSROOT
317 || (FAT32(pmp
) && dirclust
== pmp
->pm_rootdirblk
))
318 && diroffset
== MSDOSFSROOT_OFS
) {
320 * Directory entry for the root directory. There isn't one,
321 * so we manufacture one. We should probably rummage
322 * through the root directory and find a label entry (if it
323 * exists), and then use the time and date from that entry
324 * as the time and date for the root denode.
326 nvp
->v_vflag
|= VV_ROOT
; /* should be further down XXX */
328 ldep
->de_Attributes
= ATTR_DIRECTORY
;
330 ldep
->de_StartCluster
= pmp
->pm_rootdirblk
;
331 /* de_FileSize will be filled in further down */
333 ldep
->de_StartCluster
= MSDOSFSROOT
;
334 ldep
->de_FileSize
= pmp
->pm_rootdirsize
* pmp
->pm_BytesPerSec
;
337 * fill in time and date so that dos2unixtime() doesn't
338 * spit up when called from msdosfs_getattr() with root
342 ldep
->de_CTime
= 0x0000; /* 00:00:00 */
343 ldep
->de_CDate
= (0 << DD_YEAR_SHIFT
) | (1 << DD_MONTH_SHIFT
)
344 | (1 << DD_DAY_SHIFT
);
346 ldep
->de_ADate
= ldep
->de_CDate
;
347 ldep
->de_MTime
= ldep
->de_CTime
;
348 ldep
->de_MDate
= ldep
->de_CDate
;
349 /* leave the other fields as garbage */
351 error
= readep(pmp
, dirclust
, diroffset
, &bp
, &direntptr
);
353 ldep
->de_devvp
= NULL
;
354 ldep
->de_Name
[0] = SLOT_DELETED
;
358 DE_INTERNALIZE(ldep
, direntptr
);
363 * Fill in a few fields of the vnode and finish filling in the
364 * denode. Then return the address of the found denode.
366 if (ldep
->de_Attributes
& ATTR_DIRECTORY
) {
368 * Since DOS directory entries that describe directories
369 * have 0 in the filesize field, we take this opportunity
370 * to find out the length of the directory and plug it into
371 * the denode structure.
376 if (ldep
->de_StartCluster
!= MSDOSFSROOT
) {
377 error
= pcbmap(ldep
, CLUST_END
, 0, &size
, 0);
378 if (error
== E2BIG
) {
379 ldep
->de_FileSize
= de_cn2off(pmp
, size
);
382 printf("deget(): pcbmap returned %d\n", error
);
386 vref(ldep
->de_devvp
);
388 uvm_vnp_setsize(nvp
, ldep
->de_FileSize
);
393 deupdat(struct denode
*dep
, int waitfor
)
396 return (msdosfs_update(DETOV(dep
), NULL
, NULL
,
397 waitfor
? UPDATE_WAIT
: 0));
401 * Truncate the file described by dep to the length specified by length.
404 detrunc(struct denode
*dep
, u_long length
, int flags
, kauth_cred_t cred
)
409 u_long chaintofree
= 0;
410 daddr_t bn
, lastblock
;
412 int isadir
= dep
->de_Attributes
& ATTR_DIRECTORY
;
414 struct msdosfsmount
*pmp
= dep
->de_pmp
;
417 printf("detrunc(): file %s, length %lu, flags %x\n", dep
->de_Name
, length
, flags
);
421 * Disallow attempts to truncate the root directory since it is of
422 * fixed size. That's just the way dos filesystems are. We use
423 * the VROOT bit in the vnode because checking for the directory
424 * bit and a startcluster of 0 in the denode is not adequate to
425 * recognize the root directory at this point in a file or
428 if ((DETOV(dep
)->v_vflag
& VV_ROOT
) && !FAT32(pmp
)) {
429 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
430 dep
->de_dirclust
, dep
->de_diroffset
);
434 uvm_vnp_setsize(DETOV(dep
), length
);
436 if (dep
->de_FileSize
< length
)
437 return (deextend(dep
, length
, cred
));
438 lastblock
= de_clcount(pmp
, length
) - 1;
441 * If the desired length is 0 then remember the starting cluster of
442 * the file and set the StartCluster field in the directory entry
443 * to 0. If the desired length is not zero, then get the number of
444 * the last cluster in the shortened file. Then get the number of
445 * the first cluster in the part of the file that is to be freed.
446 * Then set the next cluster pointer in the last cluster of the
447 * file to CLUST_EOFE.
450 chaintofree
= dep
->de_StartCluster
;
451 dep
->de_StartCluster
= 0;
454 error
= pcbmap(dep
, lastblock
, 0, &eofentry
, 0);
457 printf("detrunc(): pcbmap fails %d\n", error
);
463 fc_purge(dep
, lastblock
+ 1);
466 * If the new length is not a multiple of the cluster size then we
467 * must zero the tail end of the new last cluster in case it
468 * becomes part of the file again because of a seek.
470 if ((boff
= length
& pmp
->pm_crbomask
) != 0) {
472 bn
= cntobn(pmp
, eofentry
);
473 error
= bread(pmp
->pm_devvp
, de_bn2kb(pmp
, bn
),
474 pmp
->pm_bpcluster
, NOCRED
, B_MODIFY
, &bp
);
478 printf("detrunc(): bread fails %d\n", error
);
482 memset((char *)bp
->b_data
+ boff
, 0,
483 pmp
->pm_bpcluster
- boff
);
489 uvm_vnp_zerorange(DETOV(dep
), length
,
490 pmp
->pm_bpcluster
- boff
);
495 * Write out the updated directory entry. Even if the update fails
496 * we free the trailing clusters.
498 dep
->de_FileSize
= length
;
500 dep
->de_flag
|= DE_UPDATE
|DE_MODIFIED
;
501 vtruncbuf(DETOV(dep
), lastblock
+ 1, 0, 0);
502 allerror
= deupdat(dep
, 1);
504 printf("detrunc(): allerror %d, eofentry %lu\n",
509 * If we need to break the cluster chain for the file then do it
512 if (eofentry
!= ~0) {
513 error
= fatentry(FAT_GET_AND_SET
, pmp
, eofentry
,
514 &chaintofree
, CLUST_EOFE
);
517 printf("detrunc(): fatentry errors %d\n", error
);
521 fc_setcache(dep
, FC_LASTFC
, de_cluster(pmp
, length
- 1),
526 * Now free the clusters removed from the file because of the
529 if (chaintofree
!= 0 && !MSDOSFSEOF(chaintofree
, pmp
->pm_fatmask
))
530 freeclusterchain(pmp
, chaintofree
);
536 * Extend the file described by dep to length specified by length.
539 deextend(struct denode
*dep
, u_long length
, kauth_cred_t cred
)
541 struct msdosfsmount
*pmp
= dep
->de_pmp
;
546 * The root of a DOS filesystem cannot be extended.
548 if ((DETOV(dep
)->v_vflag
& VV_ROOT
) && !FAT32(pmp
))
552 * Directories cannot be extended.
554 if (dep
->de_Attributes
& ATTR_DIRECTORY
)
557 if (length
<= dep
->de_FileSize
)
558 panic("deextend: file too large");
561 * Compute the number of clusters to allocate.
563 count
= de_clcount(pmp
, length
) - de_clcount(pmp
, dep
->de_FileSize
);
565 if (count
> pmp
->pm_freeclustercount
)
567 error
= extendfile(dep
, count
, NULL
, NULL
, DE_CLEAR
);
569 /* truncate the added clusters away again */
570 (void) detrunc(dep
, dep
->de_FileSize
, 0, cred
);
576 * Zero extend file range; uvm_vnp_zerorange() uses ubc_alloc() and a
577 * memset(); we set the write size so ubc won't read in file data that
580 osize
= dep
->de_FileSize
;
581 dep
->de_FileSize
= length
;
582 uvm_vnp_setwritesize(DETOV(dep
), (voff_t
)dep
->de_FileSize
);
583 dep
->de_flag
|= DE_UPDATE
|DE_MODIFIED
;
584 uvm_vnp_zerorange(DETOV(dep
), (off_t
)osize
,
585 (size_t)(dep
->de_FileSize
- osize
));
586 uvm_vnp_setsize(DETOV(dep
), (voff_t
)dep
->de_FileSize
);
587 return (deupdat(dep
, 1));
591 * Move a denode to its correct hash queue after the file it represents has
592 * been moved to a new directory.
595 reinsert(struct denode
*dep
)
598 * Fix up the denode cache. If the denode is for a directory,
599 * there is nothing to do since the hash is based on the starting
600 * cluster of the directory file and that hasn't changed. If for a
601 * file the hash is based on the location of the directory entry,
602 * so we must remove it from the cache and re-enter it with the
603 * hash based on the new location of the directory entry.
605 if (dep
->de_Attributes
& ATTR_DIRECTORY
)
607 mutex_enter(&msdosfs_hashlock
);
608 msdosfs_hashrem(dep
);
609 msdosfs_hashins(dep
);
610 mutex_exit(&msdosfs_hashlock
);
614 msdosfs_reclaim(void *v
)
616 struct vop_reclaim_args
/* {
619 struct vnode
*vp
= ap
->a_vp
;
620 struct denode
*dep
= VTODE(vp
);
623 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
624 dep
, dep
->de_Name
, dep
->de_refcnt
);
627 if (prtactive
&& vp
->v_usecount
> 1)
628 vprint("msdosfs_reclaim(): pushing active", vp
);
630 * Remove the denode from its hash chain.
632 msdosfs_hashrem(dep
);
634 * Purge old data structures associated with the denode.
638 vrele(dep
->de_devvp
);
644 genfs_node_destroy(vp
);
645 pool_put(&msdosfs_denode_pool
, dep
);
651 msdosfs_inactive(void *v
)
653 struct vop_inactive_args
/* {
657 struct vnode
*vp
= ap
->a_vp
;
658 struct denode
*dep
= VTODE(vp
);
662 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep
, dep
->de_Name
[0]);
666 * Get rid of denodes related to stale file handles.
668 if (dep
->de_Name
[0] == SLOT_DELETED
)
672 * If the file has been deleted and it is on a read/write
673 * filesystem, then truncate the file, and mark the directory slot
674 * as empty. (This may not be necessary for the dos filesystem.)
677 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x %s\n",
678 dep
, dep
->de_refcnt
, vp
->v_mount
->mnt_flag
,
679 (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) ? "MNT_RDONLY" : "");
681 if (dep
->de_refcnt
<= 0 && (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) == 0) {
682 if (dep
->de_FileSize
!= 0) {
683 error
= detrunc(dep
, (u_long
)0, 0, NOCRED
);
685 dep
->de_Name
[0] = SLOT_DELETED
;
690 * If we are done with the denode, reclaim it
691 * so that it can be reused immediately.
694 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
695 vp
->v_usecount
, dep
->de_Name
[0]);
697 *ap
->a_recycle
= (dep
->de_Name
[0] == SLOT_DELETED
);
703 msdosfs_gop_alloc(struct vnode
*vp
, off_t off
,
704 off_t len
, int flags
, kauth_cred_t cred
)
710 msdosfs_gop_markupdate(struct vnode
*vp
, int flags
)
714 if ((flags
& GOP_UPDATE_ACCESSED
) != 0) {
717 if ((flags
& GOP_UPDATE_MODIFIED
) != 0) {
721 struct denode
*dep
= VTODE(vp
);
723 dep
->de_flag
|= mask
;