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