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