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