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