1 /* $FreeBSD$ */
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/vmmeter.h>
59 #include <sys/vnode.h>
60
61 #include <vm/vm.h>
62 #include <vm/vm_extern.h>
63
64 #include <fs/msdosfs/bpb.h>
65 #include <fs/msdosfs/direntry.h>
66 #include <fs/msdosfs/denode.h>
67 #include <fs/msdosfs/fat.h>
68 #include <fs/msdosfs/msdosfsmount.h>
69
70 static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part");
71
72 static int
73 de_vncmpf(struct vnode *vp, void *arg)
74 {
75 struct denode *de;
76 uint64_t *a;
77
78 a = arg;
79 de = VTODE(vp);
80 return (de->de_inode != *a) || (de->de_refcnt <= 0);
81 }
82
83 /*
84 * If deget() succeeds it returns with the gotten denode locked().
85 *
86 * pmp - address of msdosfsmount structure of the filesystem containing
87 * the denode of interest. The address of
88 * the msdosfsmount structure are used.
89 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
90 * diroffset is relative to the beginning of the root directory,
91 * otherwise it is cluster relative.
92 * diroffset - offset past begin of cluster of denode we want
93 * depp - returns the address of the gotten denode.
94 */
95 int
96 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
97 struct denode **depp)
98 {
99 int error;
100 uint64_t inode;
101 struct mount *mntp = pmp->pm_mountp;
102 struct direntry *direntptr;
103 struct denode *ldep;
104 struct vnode *nvp, *xvp;
105 struct buf *bp;
106
107 #ifdef MSDOSFS_DEBUG
108 printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
109 pmp, dirclust, diroffset, depp);
110 #endif
111
112 /*
113 * On FAT32 filesystems, root is a (more or less) normal
114 * directory
115 */
116 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
117 dirclust = pmp->pm_rootdirblk;
118
119 /*
120 * See if the denode is in the denode cache. Use the location of
121 * the directory entry to compute the hash value. For subdir use
122 * address of "." entry. For root dir (if not FAT32) use cluster
123 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
124 *
125 * NOTE: de_vncmpf will explicitly skip any denodes that do not have
126 * a de_refcnt > 0. This insures that that we do not attempt to use
127 * a denode that represents an unlinked but still open file.
128 * These files are not to be accessible even when the directory
129 * entry that represented the file happens to be reused while the
130 * deleted file is still open.
131 */
132 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
133
134 error = vfs_hash_get(mntp, inode, LK_EXCLUSIVE, curthread, &nvp,
135 de_vncmpf, &inode);
136 if (error)
137 return (error);
138 if (nvp != NULL) {
139 *depp = VTODE(nvp);
140 KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust"));
141 KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset"));
142 return (0);
143 }
144 ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
145
146 /*
147 * Directory entry was not in cache, have to create a vnode and
148 * copy it from the passed disk buffer.
149 */
150 /* getnewvnode() does a VREF() on the vnode */
151 error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp);
152 if (error) {
153 *depp = NULL;
154 free(ldep, M_MSDOSFSNODE);
155 return error;
156 }
157 nvp->v_data = ldep;
158 ldep->de_vnode = nvp;
159 ldep->de_flag = 0;
160 ldep->de_dirclust = dirclust;
161 ldep->de_diroffset = diroffset;
162 ldep->de_inode = inode;
163 lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL);
164 fc_purge(ldep, 0); /* init the fat cache for this denode */
165 error = insmntque(nvp, mntp);
166 if (error != 0) {
167 free(ldep, M_MSDOSFSNODE);
168 *depp = NULL;
169 return (error);
170 }
171 error = vfs_hash_insert(nvp, inode, LK_EXCLUSIVE, curthread, &xvp,
172 de_vncmpf, &inode);
173 if (error) {
174 *depp = NULL;
175 return (error);
176 }
177 if (xvp != NULL) {
178 *depp = xvp->v_data;
179 return (0);
180 }
181
182 ldep->de_pmp = pmp;
183 ldep->de_refcnt = 1;
184 /*
185 * Copy the directory entry into the denode area of the vnode.
186 */
187 if ((dirclust == MSDOSFSROOT
188 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
189 && diroffset == MSDOSFSROOT_OFS) {
190 /*
191 * Directory entry for the root directory. There isn't one,
192 * so we manufacture one. We should probably rummage
193 * through the root directory and find a label entry (if it
194 * exists), and then use the time and date from that entry
195 * as the time and date for the root denode.
196 */
197 nvp->v_vflag |= VV_ROOT; /* should be further down XXX */
198
199 ldep->de_Attributes = ATTR_DIRECTORY;
200 ldep->de_LowerCase = 0;
201 if (FAT32(pmp))
202 ldep->de_StartCluster = pmp->pm_rootdirblk;
203 /* de_FileSize will be filled in further down */
204 else {
205 ldep->de_StartCluster = MSDOSFSROOT;
206 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
207 }
208 /*
209 * fill in time and date so that fattime2timespec() doesn't
210 * spit up when called from msdosfs_getattr() with root
211 * denode
212 */
213 ldep->de_CHun = 0;
214 ldep->de_CTime = 0x0000; /* 00:00:00 */
215 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
216 | (1 << DD_DAY_SHIFT);
217 /* Jan 1, 1980 */
218 ldep->de_ADate = ldep->de_CDate;
219 ldep->de_MTime = ldep->de_CTime;
220 ldep->de_MDate = ldep->de_CDate;
221 /* leave the other fields as garbage */
222 } else {
223 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
224 if (error) {
225 /*
226 * The denode does not contain anything useful, so
227 * it would be wrong to leave it on its hash chain.
228 * Arrange for vput() to just forget about it.
229 */
230 ldep->de_Name[0] = SLOT_DELETED;
231
232 vput(nvp);
233 *depp = NULL;
234 return (error);
235 }
236 (void)DE_INTERNALIZE(ldep, direntptr);
237 brelse(bp);
238 }
239
240 /*
241 * Fill in a few fields of the vnode and finish filling in the
242 * denode. Then return the address of the found denode.
243 */
244 if (ldep->de_Attributes & ATTR_DIRECTORY) {
245 /*
246 * Since DOS directory entries that describe directories
247 * have 0 in the filesize field, we take this opportunity
248 * to find out the length of the directory and plug it into
249 * the denode structure.
250 */
251 u_long size;
252
253 /*
254 * XXX it sometimes happens that the "." entry has cluster
255 * number 0 when it shouldn't. Use the actual cluster number
256 * instead of what is written in directory entry.
257 */
258 if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
259 #ifdef MSDOSFS_DEBUG
260 printf("deget(): \".\" entry at clust %lu != %lu\n",
261 dirclust, ldep->de_StartCluster);
262 #endif
263 ldep->de_StartCluster = dirclust;
264 }
265
266 nvp->v_type = VDIR;
267 if (ldep->de_StartCluster != MSDOSFSROOT) {
268 error = pcbmap(ldep, 0xffff, 0, &size, 0);
269 if (error == E2BIG) {
270 ldep->de_FileSize = de_cn2off(pmp, size);
271 error = 0;
272 } else {
273 #ifdef MSDOSFS_DEBUG
274 printf("deget(): pcbmap returned %d\n", error);
275 #endif
276 }
277 }
278 } else
279 nvp->v_type = VREG;
280 ldep->de_modrev = init_va_filerev();
281 *depp = ldep;
282 return (0);
283 }
284
285 int
286 deupdat(struct denode *dep, int waitfor)
287 {
288 struct direntry dir;
289 struct timespec ts;
290 struct buf *bp;
291 struct direntry *dirp;
292 int error;
293
294 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) {
295 dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS |
296 DE_MODIFIED);
297 return (0);
298 }
299 vfs_timestamp(&ts);
300 DETIMES(dep, &ts, &ts, &ts);
301 if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0)
302 return (0);
303 dep->de_flag &= ~DE_MODIFIED;
304 if (DETOV(dep)->v_vflag & VV_ROOT)
305 return (EINVAL);
306 if (dep->de_refcnt <= 0)
307 return (0);
308 error = readde(dep, &bp, &dirp);
309 if (error)
310 return (error);
311 DE_EXTERNALIZE(&dir, dep);
312 if (bcmp(dirp, &dir, sizeof(dir)) == 0) {
313 if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) {
314 brelse(bp);
315 return (0);
316 }
317 } else
318 *dirp = dir;
319 if ((DETOV(dep)->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
320 bp->b_flags |= B_CLUSTEROK;
321 if (waitfor)
322 error = bwrite(bp);
323 else if (vm_page_count_severe() || buf_dirty_count_severe())
324 bawrite(bp);
325 else
326 bdwrite(bp);
327 return (error);
328 }
329
330 /*
331 * Truncate the file described by dep to the length specified by length.
332 */
333 int
334 detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred)
335 {
336 int error;
337 int allerror;
338 u_long eofentry;
339 u_long chaintofree;
340 daddr_t bn;
341 int boff;
342 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
343 struct buf *bp;
344 struct msdosfsmount *pmp = dep->de_pmp;
345
346 #ifdef MSDOSFS_DEBUG
347 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
348 #endif
349
350 /*
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
356 * directory's life.
357 */
358 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
359 #ifdef MSDOSFS_DEBUG
360 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
361 dep->de_dirclust, dep->de_diroffset);
362 #endif
363 return (EINVAL);
364 }
365
366 if (dep->de_FileSize < length) {
367 vnode_pager_setsize(DETOV(dep), length);
368 return deextend(dep, length, cred);
369 }
370
371 /*
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.
379 */
380 if (length == 0) {
381 chaintofree = dep->de_StartCluster;
382 dep->de_StartCluster = 0;
383 eofentry = ~0;
384 } else {
385 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
386 &eofentry, 0);
387 if (error) {
388 #ifdef MSDOSFS_DEBUG
389 printf("detrunc(): pcbmap fails %d\n", error);
390 #endif
391 return (error);
392 }
393 }
394
395 fc_purge(dep, de_clcount(pmp, length));
396
397 /*
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.
401 */
402 if ((boff = length & pmp->pm_crbomask) != 0) {
403 if (isadir) {
404 bn = cntobn(pmp, eofentry);
405 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
406 NOCRED, &bp);
407 } else {
408 error = bread(DETOV(dep), de_cluster(pmp, length),
409 pmp->pm_bpcluster, cred, &bp);
410 }
411 if (error) {
412 #ifdef MSDOSFS_DEBUG
413 printf("detrunc(): bread fails %d\n", error);
414 #endif
415 return (error);
416 }
417 bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
418 if ((flags & IO_SYNC) != 0)
419 bwrite(bp);
420 else
421 bdwrite(bp);
422 }
423
424 /*
425 * Write out the updated directory entry. Even if the update fails
426 * we free the trailing clusters.
427 */
428 dep->de_FileSize = length;
429 if (!isadir)
430 dep->de_flag |= DE_UPDATE | DE_MODIFIED;
431 allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
432 #ifdef MSDOSFS_DEBUG
433 if (allerror)
434 printf("detrunc(): vtruncbuf error %d\n", allerror);
435 #endif
436 error = deupdat(dep, !DOINGASYNC((DETOV(dep))));
437 if (error != 0 && allerror == 0)
438 allerror = error;
439 #ifdef MSDOSFS_DEBUG
440 printf("detrunc(): allerror %d, eofentry %lu\n",
441 allerror, eofentry);
442 #endif
443
444 /*
445 * If we need to break the cluster chain for the file then do it
446 * now.
447 */
448 if (eofentry != ~0) {
449 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
450 &chaintofree, CLUST_EOFE);
451 if (error) {
452 #ifdef MSDOSFS_DEBUG
453 printf("detrunc(): fatentry errors %d\n", error);
454 #endif
455 return (error);
456 }
457 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
458 eofentry);
459 }
460
461 /*
462 * Now free the clusters removed from the file because of the
463 * truncation.
464 */
465 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
466 freeclusterchain(pmp, chaintofree);
467
468 return (allerror);
469 }
470
471 /*
472 * Extend the file described by dep to length specified by length.
473 */
474 int
475 deextend(struct denode *dep, u_long length, struct ucred *cred)
476 {
477 struct msdosfsmount *pmp = dep->de_pmp;
478 u_long count;
479 int error;
480
481 /*
482 * The root of a DOS filesystem cannot be extended.
483 */
484 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
485 return (EINVAL);
486
487 /*
488 * Directories cannot be extended.
489 */
490 if (dep->de_Attributes & ATTR_DIRECTORY)
491 return (EISDIR);
492
493 if (length <= dep->de_FileSize)
494 panic("deextend: file too large");
495
496 /*
497 * Compute the number of clusters to allocate.
498 */
499 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
500 if (count > 0) {
501 if (count > pmp->pm_freeclustercount)
502 return (ENOSPC);
503 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
504 if (error) {
505 /* truncate the added clusters away again */
506 (void) detrunc(dep, dep->de_FileSize, 0, cred);
507 return (error);
508 }
509 }
510 dep->de_FileSize = length;
511 dep->de_flag |= DE_UPDATE | DE_MODIFIED;
512 return (deupdat(dep, !DOINGASYNC(DETOV(dep))));
513 }
514
515 /*
516 * Move a denode to its correct hash queue after the file it represents has
517 * been moved to a new directory.
518 */
519 void
520 reinsert(struct denode *dep)
521 {
522 struct vnode *vp;
523
524 /*
525 * Fix up the denode cache. If the denode is for a directory,
526 * there is nothing to do since the hash is based on the starting
527 * cluster of the directory file and that hasn't changed. If for a
528 * file the hash is based on the location of the directory entry,
529 * so we must remove it from the cache and re-enter it with the
530 * hash based on the new location of the directory entry.
531 */
532 #if 0
533 if (dep->de_Attributes & ATTR_DIRECTORY)
534 return;
535 #endif
536 vp = DETOV(dep);
537 dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust +
538 dep->de_diroffset;
539 vfs_hash_rehash(vp, dep->de_inode);
540 }
541
542 int
543 msdosfs_reclaim(struct vop_reclaim_args *ap)
544 {
545 struct vnode *vp = ap->a_vp;
546 struct denode *dep = VTODE(vp);
547
548 #ifdef MSDOSFS_DEBUG
549 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
550 dep, dep->de_Name, dep->de_refcnt);
551 #endif
552
553 /*
554 * Destroy the vm object and flush associated pages.
555 */
556 vnode_destroy_vobject(vp);
557 /*
558 * Remove the denode from its hash chain.
559 */
560 vfs_hash_remove(vp);
561 /*
562 * Purge old data structures associated with the denode.
563 */
564 #if 0 /* XXX */
565 dep->de_flag = 0;
566 #endif
567 free(dep, M_MSDOSFSNODE);
568 vp->v_data = NULL;
569
570 return (0);
571 }
572
573 int
574 msdosfs_inactive(struct vop_inactive_args *ap)
575 {
576 struct vnode *vp = ap->a_vp;
577 struct denode *dep = VTODE(vp);
578 int error = 0;
579
580 #ifdef MSDOSFS_DEBUG
581 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
582 #endif
583
584 /*
585 * Ignore denodes related to stale file handles.
586 */
587 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
588 goto out;
589
590 /*
591 * If the file has been deleted and it is on a read/write
592 * filesystem, then truncate the file, and mark the directory slot
593 * as empty. (This may not be necessary for the dos filesystem.)
594 */
595 #ifdef MSDOSFS_DEBUG
596 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
597 dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
598 #endif
599 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
600 error = detrunc(dep, (u_long) 0, 0, NOCRED);
601 dep->de_flag |= DE_UPDATE;
602 dep->de_Name[0] = SLOT_DELETED;
603 }
604 deupdat(dep, 0);
605
606 out:
607 /*
608 * If we are done with the denode, reclaim it
609 * so that it can be reused immediately.
610 */
611 #ifdef MSDOSFS_DEBUG
612 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
613 vrefcnt(vp), dep->de_Name[0]);
614 #endif
615 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
616 vrecycle(vp);
617 return (error);
618 }
Cache object: c2ecf1b79f9dedca2a5ce75ac1ae8eea
|