1 /* $FreeBSD$ */
2 /* $NetBSD: msdosfs_denode.c,v 1.28 1998/02/10 14:10:00 mrg Exp $ */
3
4 /*-
5 * SPDX-License-Identifier: BSD-4-Clause
6 *
7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * All rights reserved.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by TooLs GmbH.
23 * 4. The name of TooLs GmbH may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 /*-
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 *
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
42 *
43 * This software is provided "as is".
44 *
45 * The author supplies this software to be publicly redistributed on the
46 * understanding that the author is not responsible for the correct
47 * functioning of this software in any circumstances and is not liable for
48 * any damages caused by this software.
49 *
50 * October 1992
51 */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/buf.h>
56 #include <sys/clock.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/vmmeter.h>
61 #include <sys/vnode.h>
62
63 #include <vm/vm.h>
64 #include <vm/vm_extern.h>
65
66 #include <fs/msdosfs/bpb.h>
67 #include <fs/msdosfs/direntry.h>
68 #include <fs/msdosfs/denode.h>
69 #include <fs/msdosfs/fat.h>
70 #include <fs/msdosfs/msdosfsmount.h>
71
72 static MALLOC_DEFINE(M_MSDOSFSNODE, "msdosfs_node", "MSDOSFS vnode private part");
73
74 static int
75 de_vncmpf(struct vnode *vp, void *arg)
76 {
77 struct denode *de;
78 uint64_t *a;
79
80 a = arg;
81 de = VTODE(vp);
82 return (de->de_inode != *a) || (de->de_refcnt <= 0);
83 }
84
85 /*
86 * If deget() succeeds it returns with the gotten denode locked().
87 *
88 * pmp - address of msdosfsmount structure of the filesystem containing
89 * the denode of interest. The address of
90 * the msdosfsmount structure are used.
91 * dirclust - which cluster bp contains, if dirclust is 0 (root directory)
92 * diroffset is relative to the beginning of the root directory,
93 * otherwise it is cluster relative.
94 * diroffset - offset past begin of cluster of denode we want
95 * lkflags - locking flags (LK_NOWAIT)
96 * depp - returns the address of the gotten denode.
97 */
98 int
99 deget(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
100 int lkflags, struct denode **depp)
101 {
102 int error;
103 uint64_t inode;
104 struct mount *mntp = pmp->pm_mountp;
105 struct direntry *direntptr;
106 struct denode *ldep;
107 struct vnode *nvp, *xvp;
108 struct buf *bp;
109
110 #ifdef MSDOSFS_DEBUG
111 printf("deget(pmp %p, dirclust %lu, diroffset %lx, flags %#x, "
112 "depp %p)\n",
113 pmp, dirclust, diroffset, flags, depp);
114 #endif
115 MPASS((lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE);
116
117 /*
118 * On FAT32 filesystems, root is a (more or less) normal
119 * directory
120 */
121 if (FAT32(pmp) && dirclust == MSDOSFSROOT)
122 dirclust = pmp->pm_rootdirblk;
123
124 /*
125 * See if the denode is in the denode cache. Use the location of
126 * the directory entry to compute the hash value. For subdir use
127 * address of "." entry. For root dir (if not FAT32) use cluster
128 * MSDOSFSROOT, offset MSDOSFSROOT_OFS
129 *
130 * NOTE: de_vncmpf will explicitly skip any denodes that do not have
131 * a de_refcnt > 0. This insures that that we do not attempt to use
132 * a denode that represents an unlinked but still open file.
133 * These files are not to be accessible even when the directory
134 * entry that represented the file happens to be reused while the
135 * deleted file is still open.
136 */
137 inode = (uint64_t)pmp->pm_bpcluster * dirclust + diroffset;
138
139 error = vfs_hash_get(mntp, inode, lkflags, curthread, &nvp,
140 de_vncmpf, &inode);
141 if (error)
142 return (error);
143 if (nvp != NULL) {
144 *depp = VTODE(nvp);
145 KASSERT((*depp)->de_dirclust == dirclust, ("wrong dirclust"));
146 KASSERT((*depp)->de_diroffset == diroffset, ("wrong diroffset"));
147 return (0);
148 }
149 ldep = malloc(sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK | M_ZERO);
150
151 /*
152 * Directory entry was not in cache, have to create a vnode and
153 * copy it from the passed disk buffer.
154 */
155 /* getnewvnode() does a VREF() on the vnode */
156 error = getnewvnode("msdosfs", mntp, &msdosfs_vnodeops, &nvp);
157 if (error) {
158 *depp = NULL;
159 free(ldep, M_MSDOSFSNODE);
160 return error;
161 }
162 nvp->v_data = ldep;
163 ldep->de_vnode = nvp;
164 ldep->de_flag = 0;
165 ldep->de_dirclust = dirclust;
166 ldep->de_diroffset = diroffset;
167 ldep->de_inode = inode;
168 lockmgr(nvp->v_vnlock, LK_EXCLUSIVE, NULL);
169 VN_LOCK_AREC(nvp); /* for doscheckpath */
170 fc_purge(ldep, 0); /* init the FAT cache for this denode */
171 error = insmntque(nvp, mntp);
172 if (error != 0) {
173 free(ldep, M_MSDOSFSNODE);
174 *depp = NULL;
175 return (error);
176 }
177 error = vfs_hash_insert(nvp, inode, lkflags, curthread, &xvp,
178 de_vncmpf, &inode);
179 if (error) {
180 *depp = NULL;
181 return (error);
182 }
183 if (xvp != NULL) {
184 *depp = xvp->v_data;
185 return (0);
186 }
187
188 ldep->de_pmp = pmp;
189 ldep->de_refcnt = 1;
190 /*
191 * Copy the directory entry into the denode area of the vnode.
192 */
193 if ((dirclust == MSDOSFSROOT
194 || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
195 && diroffset == MSDOSFSROOT_OFS) {
196 /*
197 * Directory entry for the root directory. There isn't one,
198 * so we manufacture one. We should probably rummage
199 * through the root directory and find a label entry (if it
200 * exists), and then use the time and date from that entry
201 * as the time and date for the root denode.
202 */
203 nvp->v_vflag |= VV_ROOT; /* should be further down XXX */
204
205 ldep->de_Attributes = ATTR_DIRECTORY;
206 ldep->de_LowerCase = 0;
207 if (FAT32(pmp))
208 ldep->de_StartCluster = pmp->pm_rootdirblk;
209 /* de_FileSize will be filled in further down */
210 else {
211 ldep->de_StartCluster = MSDOSFSROOT;
212 ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
213 }
214 /*
215 * fill in time and date so that fattime2timespec() doesn't
216 * spit up when called from msdosfs_getattr() with root
217 * denode
218 */
219 ldep->de_CHun = 0;
220 ldep->de_CTime = 0x0000; /* 00:00:00 */
221 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
222 | (1 << DD_DAY_SHIFT);
223 /* Jan 1, 1980 */
224 ldep->de_ADate = ldep->de_CDate;
225 ldep->de_MTime = ldep->de_CTime;
226 ldep->de_MDate = ldep->de_CDate;
227 /* leave the other fields as garbage */
228 } else {
229 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
230 if (error) {
231 /*
232 * The denode does not contain anything useful, so
233 * it would be wrong to leave it on its hash chain.
234 * Arrange for vput() to just forget about it.
235 */
236 ldep->de_Name[0] = SLOT_DELETED;
237 vgone(nvp);
238 vput(nvp);
239 *depp = NULL;
240 return (error);
241 }
242 (void)DE_INTERNALIZE(ldep, direntptr);
243 brelse(bp);
244 }
245
246 /*
247 * Fill in a few fields of the vnode and finish filling in the
248 * denode. Then return the address of the found denode.
249 */
250 if (ldep->de_Attributes & ATTR_DIRECTORY) {
251 /*
252 * Since DOS directory entries that describe directories
253 * have 0 in the filesize field, we take this opportunity
254 * to find out the length of the directory and plug it into
255 * the denode structure.
256 */
257 u_long size;
258
259 /*
260 * XXX it sometimes happens that the "." entry has cluster
261 * number 0 when it shouldn't. Use the actual cluster number
262 * instead of what is written in directory entry.
263 */
264 if (diroffset == 0 && ldep->de_StartCluster != dirclust) {
265 #ifdef MSDOSFS_DEBUG
266 printf("deget(): \".\" entry at clust %lu != %lu\n",
267 dirclust, ldep->de_StartCluster);
268 #endif
269 ldep->de_StartCluster = dirclust;
270 }
271
272 nvp->v_type = VDIR;
273 if (ldep->de_StartCluster != MSDOSFSROOT) {
274 error = pcbmap(ldep, 0xffff, 0, &size, 0);
275 if (error == E2BIG) {
276 ldep->de_FileSize = de_cn2off(pmp, size);
277 error = 0;
278 } else {
279 #ifdef MSDOSFS_DEBUG
280 printf("deget(): pcbmap returned %d\n", error);
281 #endif
282 }
283 }
284 } else
285 nvp->v_type = VREG;
286 ldep->de_modrev = init_va_filerev();
287 *depp = ldep;
288 return (0);
289 }
290
291 int
292 deupdat(struct denode *dep, int waitfor)
293 {
294 struct direntry dir;
295 struct timespec ts;
296 struct buf *bp;
297 struct direntry *dirp;
298 int error;
299
300 if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY) {
301 dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS |
302 DE_MODIFIED);
303 return (0);
304 }
305 vfs_timestamp(&ts);
306 DETIMES(dep, &ts, &ts, &ts);
307 if ((dep->de_flag & DE_MODIFIED) == 0 && waitfor == 0)
308 return (0);
309 dep->de_flag &= ~DE_MODIFIED;
310 if (DETOV(dep)->v_vflag & VV_ROOT)
311 return (EINVAL);
312 if (dep->de_refcnt <= 0)
313 return (0);
314 error = readde(dep, &bp, &dirp);
315 if (error)
316 return (error);
317 DE_EXTERNALIZE(&dir, dep);
318 if (bcmp(dirp, &dir, sizeof(dir)) == 0) {
319 if (waitfor == 0 || (bp->b_flags & B_DELWRI) == 0) {
320 brelse(bp);
321 return (0);
322 }
323 } else
324 *dirp = dir;
325 if ((DETOV(dep)->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
326 bp->b_flags |= B_CLUSTEROK;
327 if (waitfor)
328 error = bwrite(bp);
329 else if (vm_page_count_severe() || buf_dirty_count_severe())
330 bawrite(bp);
331 else
332 bdwrite(bp);
333 return (error);
334 }
335
336 /*
337 * Truncate the file described by dep to the length specified by length.
338 */
339 int
340 detrunc(struct denode *dep, u_long length, int flags, struct ucred *cred)
341 {
342 int error;
343 int allerror;
344 u_long eofentry;
345 u_long chaintofree;
346 daddr_t bn;
347 int boff;
348 int isadir = dep->de_Attributes & ATTR_DIRECTORY;
349 struct buf *bp;
350 struct msdosfsmount *pmp = dep->de_pmp;
351
352 #ifdef MSDOSFS_DEBUG
353 printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
354 #endif
355
356 /*
357 * Disallow attempts to truncate the root directory since it is of
358 * fixed size. That's just the way dos filesystems are. We use
359 * the VROOT bit in the vnode because checking for the directory
360 * bit and a startcluster of 0 in the denode is not adequate to
361 * recognize the root directory at this point in a file or
362 * directory's life.
363 */
364 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp)) {
365 #ifdef MSDOSFS_DEBUG
366 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
367 dep->de_dirclust, dep->de_diroffset);
368 #endif
369 return (EINVAL);
370 }
371
372 if (dep->de_FileSize < length) {
373 vnode_pager_setsize(DETOV(dep), length);
374 return deextend(dep, length, cred);
375 }
376
377 /*
378 * If the desired length is 0 then remember the starting cluster of
379 * the file and set the StartCluster field in the directory entry
380 * to 0. If the desired length is not zero, then get the number of
381 * the last cluster in the shortened file. Then get the number of
382 * the first cluster in the part of the file that is to be freed.
383 * Then set the next cluster pointer in the last cluster of the
384 * file to CLUST_EOFE.
385 */
386 if (length == 0) {
387 chaintofree = dep->de_StartCluster;
388 dep->de_StartCluster = 0;
389 eofentry = ~0;
390 } else {
391 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0,
392 &eofentry, 0);
393 if (error) {
394 #ifdef MSDOSFS_DEBUG
395 printf("detrunc(): pcbmap fails %d\n", error);
396 #endif
397 return (error);
398 }
399 }
400
401 fc_purge(dep, de_clcount(pmp, length));
402
403 /*
404 * If the new length is not a multiple of the cluster size then we
405 * must zero the tail end of the new last cluster in case it
406 * becomes part of the file again because of a seek.
407 */
408 if ((boff = length & pmp->pm_crbomask) != 0) {
409 if (isadir) {
410 bn = cntobn(pmp, eofentry);
411 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
412 NOCRED, &bp);
413 } else {
414 error = bread(DETOV(dep), de_cluster(pmp, length),
415 pmp->pm_bpcluster, cred, &bp);
416 }
417 if (error) {
418 #ifdef MSDOSFS_DEBUG
419 printf("detrunc(): bread fails %d\n", error);
420 #endif
421 return (error);
422 }
423 memset(bp->b_data + boff, 0, pmp->pm_bpcluster - boff);
424 if ((flags & IO_SYNC) != 0)
425 bwrite(bp);
426 else
427 bdwrite(bp);
428 }
429
430 /*
431 * Write out the updated directory entry. Even if the update fails
432 * we free the trailing clusters.
433 */
434 dep->de_FileSize = length;
435 if (!isadir)
436 dep->de_flag |= DE_UPDATE | DE_MODIFIED;
437 allerror = vtruncbuf(DETOV(dep), length, pmp->pm_bpcluster);
438 #ifdef MSDOSFS_DEBUG
439 if (allerror)
440 printf("detrunc(): vtruncbuf error %d\n", allerror);
441 #endif
442 error = deupdat(dep, !DOINGASYNC((DETOV(dep))));
443 if (error != 0 && allerror == 0)
444 allerror = error;
445 #ifdef MSDOSFS_DEBUG
446 printf("detrunc(): allerror %d, eofentry %lu\n",
447 allerror, eofentry);
448 #endif
449
450 /*
451 * If we need to break the cluster chain for the file then do it
452 * now.
453 */
454 if (eofentry != ~0) {
455 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
456 &chaintofree, CLUST_EOFE);
457 if (error) {
458 #ifdef MSDOSFS_DEBUG
459 printf("detrunc(): fatentry errors %d\n", error);
460 #endif
461 return (error);
462 }
463 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
464 eofentry);
465 }
466
467 /*
468 * Now free the clusters removed from the file because of the
469 * truncation.
470 */
471 if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
472 freeclusterchain(pmp, chaintofree);
473
474 return (allerror);
475 }
476
477 /*
478 * Extend the file described by dep to length specified by length.
479 */
480 int
481 deextend(struct denode *dep, u_long length, struct ucred *cred)
482 {
483 struct msdosfsmount *pmp = dep->de_pmp;
484 u_long count;
485 int error;
486
487 /*
488 * The root of a DOS filesystem cannot be extended.
489 */
490 if ((DETOV(dep)->v_vflag & VV_ROOT) && !FAT32(pmp))
491 return (EINVAL);
492
493 /*
494 * Directories cannot be extended.
495 */
496 if (dep->de_Attributes & ATTR_DIRECTORY)
497 return (EISDIR);
498
499 if (length <= dep->de_FileSize)
500 panic("deextend: file too large");
501
502 /*
503 * Compute the number of clusters to allocate.
504 */
505 count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
506 if (count > 0) {
507 if (count > pmp->pm_freeclustercount)
508 return (ENOSPC);
509 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
510 if (error) {
511 /* truncate the added clusters away again */
512 (void) detrunc(dep, dep->de_FileSize, 0, cred);
513 return (error);
514 }
515 }
516 dep->de_FileSize = length;
517 dep->de_flag |= DE_UPDATE | DE_MODIFIED;
518 return (deupdat(dep, !DOINGASYNC(DETOV(dep))));
519 }
520
521 /*
522 * Move a denode to its correct hash queue after the file it represents has
523 * been moved to a new directory.
524 */
525 void
526 reinsert(struct denode *dep)
527 {
528 struct vnode *vp;
529
530 /*
531 * Fix up the denode cache. If the denode is for a directory,
532 * there is nothing to do since the hash is based on the starting
533 * cluster of the directory file and that hasn't changed. If for a
534 * file the hash is based on the location of the directory entry,
535 * so we must remove it from the cache and re-enter it with the
536 * hash based on the new location of the directory entry.
537 */
538 #if 0
539 if (dep->de_Attributes & ATTR_DIRECTORY)
540 return;
541 #endif
542 vp = DETOV(dep);
543 dep->de_inode = (uint64_t)dep->de_pmp->pm_bpcluster * dep->de_dirclust +
544 dep->de_diroffset;
545 vfs_hash_rehash(vp, dep->de_inode);
546 }
547
548 int
549 msdosfs_reclaim(struct vop_reclaim_args *ap)
550 {
551 struct vnode *vp = ap->a_vp;
552 struct denode *dep = VTODE(vp);
553
554 #ifdef MSDOSFS_DEBUG
555 printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
556 dep, dep->de_Name, dep->de_refcnt);
557 #endif
558
559 /*
560 * Remove the denode from its hash chain.
561 */
562 vfs_hash_remove(vp);
563 /*
564 * Purge old data structures associated with the denode.
565 */
566 #if 0 /* XXX */
567 dep->de_flag = 0;
568 #endif
569 free(dep, M_MSDOSFSNODE);
570 vp->v_data = NULL;
571
572 return (0);
573 }
574
575 int
576 msdosfs_inactive(struct vop_inactive_args *ap)
577 {
578 struct vnode *vp = ap->a_vp;
579 struct denode *dep = VTODE(vp);
580 int error = 0;
581
582 #ifdef MSDOSFS_DEBUG
583 printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
584 #endif
585
586 /*
587 * Ignore denodes related to stale file handles.
588 */
589 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
590 goto out;
591
592 /*
593 * If the file has been deleted and it is on a read/write
594 * filesystem, then truncate the file, and mark the directory slot
595 * as empty. (This may not be necessary for the dos filesystem.)
596 */
597 #ifdef MSDOSFS_DEBUG
598 printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %llx, MNT_RDONLY %llx\n",
599 dep, dep->de_refcnt, (unsigned long long)vp->v_mount->mnt_flag,
600 (unsigned long long)MNT_RDONLY);
601 #endif
602 if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
603 error = detrunc(dep, (u_long) 0, 0, NOCRED);
604 dep->de_flag |= DE_UPDATE;
605 dep->de_Name[0] = SLOT_DELETED;
606 }
607 deupdat(dep, 0);
608
609 out:
610 /*
611 * If we are done with the denode, reclaim it
612 * so that it can be reused immediately.
613 */
614 #ifdef MSDOSFS_DEBUG
615 printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n",
616 vrefcnt(vp), dep->de_Name[0]);
617 #endif
618 if (dep->de_Name[0] == SLOT_DELETED || dep->de_Name[0] == SLOT_EMPTY)
619 vrecycle(vp);
620 return (error);
621 }
Cache object: 79c19f7979716074250d82540b923510
|