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