1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vnops.c,v 1.95.2.4 2003/06/13 15:05:47 trhodes Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vnops.c,v 1.22 2004/12/22 02:17:08 dillon Exp $ */
3 /* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
55 #include <sys/kernel.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/unistd.h>
62 #include <sys/vnode.h>
63 #include <sys/malloc.h>
64 #include <sys/dirent.h>
65 #include <sys/signalvar.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_zone.h>
70 #include <vm/vnode_pager.h>
77 #include "msdosfsmount.h"
80 #define DOS_FILESIZE_MAX 0xffffffff
83 * Prototypes for MSDOSFS vnode operations
85 static int msdosfs_create (struct vop_create_args *);
86 static int msdosfs_mknod (struct vop_mknod_args *);
87 static int msdosfs_close (struct vop_close_args *);
88 static int msdosfs_access (struct vop_access_args *);
89 static int msdosfs_getattr (struct vop_getattr_args *);
90 static int msdosfs_setattr (struct vop_setattr_args *);
91 static int msdosfs_read (struct vop_read_args *);
92 static int msdosfs_write (struct vop_write_args *);
93 static int msdosfs_fsync (struct vop_fsync_args *);
94 static int msdosfs_remove (struct vop_remove_args *);
95 static int msdosfs_link (struct vop_link_args *);
96 static int msdosfs_rename (struct vop_rename_args *);
97 static int msdosfs_mkdir (struct vop_mkdir_args *);
98 static int msdosfs_rmdir (struct vop_rmdir_args *);
99 static int msdosfs_symlink (struct vop_symlink_args *);
100 static int msdosfs_readdir (struct vop_readdir_args *);
101 static int msdosfs_bmap (struct vop_bmap_args *);
102 static int msdosfs_strategy (struct vop_strategy_args *);
103 static int msdosfs_print (struct vop_print_args *);
104 static int msdosfs_pathconf (struct vop_pathconf_args *ap);
105 static int msdosfs_getpages (struct vop_getpages_args *);
106 static int msdosfs_putpages (struct vop_putpages_args *);
109 * Some general notes:
111 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
112 * read/written using the vnode for the filesystem. Blocks that represent
113 * the contents of a file are read/written using the vnode for the file
114 * (including directories when they are read/written as files). This
115 * presents problems for the dos filesystem because data that should be in
116 * an inode (if dos had them) resides in the directory itself. Since we
117 * must update directory entries without the benefit of having the vnode
118 * for the directory we must use the vnode for the filesystem. This means
119 * that when a directory is actually read/written (via read, write, or
120 * readdir, or seek) we must use the vnode for the filesystem instead of
121 * the vnode for the directory as would happen in ufs. This is to insure we
122 * retreive the correct block from the buffer cache since the hash value is
123 * based upon the vnode address and the desired block number.
127 * Create a regular file. On entry the directory to contain the file being
128 * created is locked. We must release before we return.
130 * msdosfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
131 * struct componentname *a_cnp, struct vattr *a_vap)
134 msdosfs_create(struct vop_create_args *ap)
136 struct componentname *cnp = ap->a_cnp;
137 struct denode ndirent;
139 struct denode *pdep = VTODE(ap->a_dvp);
144 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
148 * If this is the root directory and there is no space left we
149 * can't do anything. This is because the root directory can not
152 if (pdep->de_StartCluster == MSDOSFSROOT
153 && pdep->de_fndoffset >= pdep->de_FileSize) {
159 * Create a directory entry for the file, then call createde() to
160 * have it installed. NOTE: DOS files are always executable. We
161 * use the absence of the owner write bit to make the file
164 bzero(&ndirent, sizeof(ndirent));
165 error = uniqdosname(pdep, cnp, ndirent.de_Name);
169 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
170 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
171 ndirent.de_LowerCase = 0;
172 ndirent.de_StartCluster = 0;
173 ndirent.de_FileSize = 0;
174 ndirent.de_dev = pdep->de_dev;
175 ndirent.de_devvp = pdep->de_devvp;
176 ndirent.de_pmp = pdep->de_pmp;
177 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
179 DETIMES(&ndirent, &ts, &ts, &ts);
180 error = createde(&ndirent, pdep, &dep, cnp);
183 *ap->a_vpp = DETOV(dep);
191 * msdosfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
192 * struct componentname *a_cnp, struct vattr *a_vap)
195 msdosfs_mknod(struct vop_mknod_args *ap)
197 switch (ap->a_vap->va_type) {
199 return (msdosfs_mkdir((struct vop_mkdir_args *)ap));
203 return (msdosfs_create((struct vop_create_args *)ap));
213 * msdosfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
214 * struct thread *a_td)
217 msdosfs_close(struct vop_close_args *ap)
219 struct vnode *vp = ap->a_vp;
220 struct denode *dep = VTODE(vp);
223 if (vp->v_usecount > 1) {
225 if (vp->v_usecount > 1) {
226 DETIMES(dep, &ts, &ts, &ts);
233 * msdosfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
234 * struct thread *a_td)
237 msdosfs_access(struct vop_access_args *ap)
239 struct vnode *vp = ap->a_vp;
240 struct denode *dep = VTODE(ap->a_vp);
241 struct msdosfsmount *pmp = dep->de_pmp;
242 struct ucred *cred = ap->a_cred;
243 mode_t mask, file_mode, mode = ap->a_mode;
247 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
248 ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
249 file_mode &= pmp->pm_mask;
252 * Disallow write attempts on read-only file systems;
253 * unless the file is a socket, fifo, or a block or
254 * character device resident on the file system.
257 switch (vp->v_type) {
261 if (vp->v_mount->mnt_flag & MNT_RDONLY)
269 /* User id 0 always gets access. */
270 if (cred->cr_uid == 0)
275 /* Otherwise, check the owner. */
276 if (cred->cr_uid == pmp->pm_uid) {
283 return (file_mode & mask) == mask ? 0 : EACCES;
286 /* Otherwise, check the groups. */
287 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
288 if (pmp->pm_gid == *gp) {
295 return (file_mode & mask) == mask ? 0 : EACCES;
298 /* Otherwise, check everyone else. */
305 return (file_mode & mask) == mask ? 0 : EACCES;
309 * msdosfs_getattr(struct vnode *a_vp, struct vattr *a_vap,
310 * struct ucred *a_cred, struct thread *a_td)
313 msdosfs_getattr(struct vop_getattr_args *ap)
315 struct denode *dep = VTODE(ap->a_vp);
316 struct msdosfsmount *pmp = dep->de_pmp;
317 struct vattr *vap = ap->a_vap;
320 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
324 DETIMES(dep, &ts, &ts, &ts);
325 vap->va_fsid = dev2udev(dep->de_dev);
327 * The following computation of the fileid must be the same as that
328 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
331 if (dep->de_Attributes & ATTR_DIRECTORY) {
332 fileid = cntobn(pmp, dep->de_StartCluster) * dirsperblk;
333 if (dep->de_StartCluster == MSDOSFSROOT)
336 fileid = cntobn(pmp, dep->de_dirclust) * dirsperblk;
337 if (dep->de_dirclust == MSDOSFSROOT)
338 fileid = roottobn(pmp, 0) * dirsperblk;
339 fileid += dep->de_diroffset / sizeof(struct direntry);
341 vap->va_fileid = fileid;
342 if ((dep->de_Attributes & ATTR_READONLY) == 0)
343 mode = S_IRWXU|S_IRWXG|S_IRWXO;
345 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
346 vap->va_mode = mode & pmp->pm_mask;
347 vap->va_uid = pmp->pm_uid;
348 vap->va_gid = pmp->pm_gid;
351 vap->va_size = dep->de_FileSize;
352 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
353 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
354 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
355 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
357 vap->va_atime = vap->va_mtime;
358 vap->va_ctime = vap->va_mtime;
361 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
362 vap->va_flags |= SF_ARCHIVED;
364 vap->va_blocksize = pmp->pm_bpcluster;
366 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
367 vap->va_type = ap->a_vp->v_type;
368 vap->va_filerev = dep->de_modrev;
373 * msdosfs_setattr(struct vnode *a_vp, struct vattr *a_vap,
374 * struct ucred *a_cred, struct thread *a_td)
377 msdosfs_setattr(struct vop_setattr_args *ap)
379 struct vnode *vp = ap->a_vp;
380 struct denode *dep = VTODE(ap->a_vp);
381 struct msdosfsmount *pmp = dep->de_pmp;
382 struct vattr *vap = ap->a_vap;
383 struct ucred *cred = ap->a_cred;
387 printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
388 ap->a_vp, vap, cred, ap->a_td);
392 * Check for unsettable attributes.
394 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
395 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
396 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
397 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
399 printf("msdosfs_setattr(): returning EINVAL\n");
400 printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
401 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
402 printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
403 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
404 printf(" va_uid %x, va_gid %x\n",
405 vap->va_uid, vap->va_gid);
409 if (vap->va_flags != VNOVAL) {
410 if (vp->v_mount->mnt_flag & MNT_RDONLY)
412 if (cred->cr_uid != pmp->pm_uid &&
413 (error = suser_cred(cred, PRISON_ROOT)))
416 * We are very inconsistent about handling unsupported
417 * attributes. We ignored the access time and the
418 * read and execute bits. We were strict for the other
421 * Here we are strict, stricter than ufs in not allowing
422 * users to attempt to set SF_SETTABLE bits or anyone to
423 * set unsupported bits. However, we ignore attempts to
424 * set ATTR_ARCHIVE for directories `cp -pr' from a more
425 * sensible file system attempts it a lot.
427 if (cred->cr_uid != 0) {
428 if (vap->va_flags & SF_SETTABLE)
431 if (vap->va_flags & ~SF_ARCHIVED)
433 if (vap->va_flags & SF_ARCHIVED)
434 dep->de_Attributes &= ~ATTR_ARCHIVE;
435 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
436 dep->de_Attributes |= ATTR_ARCHIVE;
437 dep->de_flag |= DE_MODIFIED;
440 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
444 if (vp->v_mount->mnt_flag & MNT_RDONLY)
447 if (uid == (uid_t)VNOVAL)
450 if (gid == (gid_t)VNOVAL)
452 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
453 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
454 (error = suser_cred(cred, PRISON_ROOT)))
456 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
460 if (vap->va_size != VNOVAL) {
462 * Disallow write attempts on read-only file systems;
463 * unless the file is a socket, fifo, or a block or
464 * character device resident on the file system.
466 switch (vp->v_type) {
472 if (vp->v_mount->mnt_flag & MNT_RDONLY)
478 error = detrunc(dep, vap->va_size, 0, ap->a_td);
482 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
483 if (vp->v_mount->mnt_flag & MNT_RDONLY)
485 if (cred->cr_uid != pmp->pm_uid &&
486 (error = suser_cred(cred, PRISON_ROOT)) &&
487 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
488 (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_td))))
490 if (vp->v_type != VDIR) {
491 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
492 vap->va_atime.tv_sec != VNOVAL) {
493 dep->de_flag &= ~DE_ACCESS;
494 unix2dostime(&vap->va_atime, &dep->de_ADate,
497 if (vap->va_mtime.tv_sec != VNOVAL) {
498 dep->de_flag &= ~DE_UPDATE;
499 unix2dostime(&vap->va_mtime, &dep->de_MDate,
500 &dep->de_MTime, NULL);
502 dep->de_Attributes |= ATTR_ARCHIVE;
503 dep->de_flag |= DE_MODIFIED;
507 * DOS files only have the ability to have their writability
508 * attribute set, so we use the owner write bit to set the readonly
511 if (vap->va_mode != (mode_t)VNOVAL) {
512 if (vp->v_mount->mnt_flag & MNT_RDONLY)
514 if (cred->cr_uid != pmp->pm_uid &&
515 (error = suser_cred(cred, PRISON_ROOT)))
517 if (vp->v_type != VDIR) {
518 /* We ignore the read and execute bits. */
519 if (vap->va_mode & VWRITE)
520 dep->de_Attributes &= ~ATTR_READONLY;
522 dep->de_Attributes |= ATTR_READONLY;
523 dep->de_Attributes |= ATTR_ARCHIVE;
524 dep->de_flag |= DE_MODIFIED;
527 return (deupdat(dep, 1));
531 * msdosfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
532 * struct ucred *a_cred)
535 msdosfs_read(struct vop_read_args *ap)
549 struct vnode *vp = ap->a_vp;
550 struct denode *dep = VTODE(vp);
551 struct msdosfsmount *pmp = dep->de_pmp;
552 struct uio *uio = ap->a_uio;
554 if (uio->uio_offset < 0)
557 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
560 * If they didn't ask for any data, then we are done.
562 orig_resid = uio->uio_resid;
566 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
568 isadir = dep->de_Attributes & ATTR_DIRECTORY;
570 if (uio->uio_offset >= dep->de_FileSize)
572 lbn = de_cluster(pmp, uio->uio_offset);
574 * If we are operating on a directory file then be sure to
575 * do i/o with the vnode for the filesystem instead of the
576 * vnode for the directory.
579 /* convert cluster # to block # */
580 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
581 if (error == E2BIG) {
586 error = bread(pmp->pm_devvp, lbn, blsize, &bp);
588 blsize = pmp->pm_bpcluster;
591 de_cn2off(pmp, rablock) < dep->de_FileSize) {
592 rasize = pmp->pm_bpcluster;
593 error = breadn(vp, lbn, blsize,
594 &rablock, &rasize, 1, &bp);
596 error = bread(vp, lbn, blsize, &bp);
603 on = uio->uio_offset & pmp->pm_crbomask;
604 diff = pmp->pm_bpcluster - on;
605 n = diff > uio->uio_resid ? uio->uio_resid : diff;
606 diff = dep->de_FileSize - uio->uio_offset;
609 diff = blsize - bp->b_resid;
612 error = uiomove(bp->b_data + on, (int) n, uio);
614 } while (error == 0 && uio->uio_resid > 0 && n != 0);
615 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
616 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
617 dep->de_flag |= DE_ACCESS;
622 * Write data to a file or directory.
624 * msdosfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
625 * struct ucred *a_cred)
628 msdosfs_write(struct vop_write_args *ap)
638 int ioflag = ap->a_ioflag;
639 struct uio *uio = ap->a_uio;
640 struct thread *td = uio->uio_td;
641 struct vnode *vp = ap->a_vp;
642 struct vnode *thisvp;
643 struct denode *dep = VTODE(vp);
644 struct msdosfsmount *pmp = dep->de_pmp;
645 struct proc *p = (td ? td->td_proc : NULL);
648 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
649 vp, uio, ioflag, cred);
650 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
651 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
654 switch (vp->v_type) {
656 if (ioflag & IO_APPEND)
657 uio->uio_offset = dep->de_FileSize;
663 panic("msdosfs_write(): bad file type");
666 if (uio->uio_offset < 0)
669 if (uio->uio_resid == 0)
673 * If they've exceeded their filesize limit, tell them about it.
676 ((uoff_t)uio->uio_offset + uio->uio_resid >
677 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
682 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
686 * If the offset we are starting the write at is beyond the end of
687 * the file, then they've done a seek. Unix filesystems allow
688 * files with holes in them, DOS doesn't so we must fill the hole
689 * with zeroed blocks.
691 if (uio->uio_offset > dep->de_FileSize) {
692 error = deextend(dep, uio->uio_offset);
698 * Remember some values in case the write fails.
700 resid = uio->uio_resid;
701 osize = dep->de_FileSize;
704 * If we write beyond the end of the file, extend it to its ultimate
705 * size ahead of the time to hopefully get a contiguous area.
707 if (uio->uio_offset + resid > osize) {
708 count = de_clcount(pmp, uio->uio_offset + resid) -
709 de_clcount(pmp, osize);
710 error = extendfile(dep, count, NULL, NULL, 0);
711 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
713 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
715 lastcn = de_clcount(pmp, osize) - 1;
718 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
723 croffset = uio->uio_offset & pmp->pm_crbomask;
724 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
725 if (uio->uio_offset + n > dep->de_FileSize) {
726 dep->de_FileSize = uio->uio_offset + n;
727 /* The object size needs to be set before buffer is allocated */
728 vnode_pager_setsize(vp, dep->de_FileSize);
731 bn = de_cluster(pmp, uio->uio_offset);
732 if ((uio->uio_offset & pmp->pm_crbomask) == 0
733 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
734 > de_cluster(pmp, uio->uio_offset)
735 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
737 * If either the whole cluster gets written,
738 * or we write the cluster from its start beyond EOF,
739 * then no need to read data from disk.
741 bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0);
744 * Do the bmap now, since pcbmap needs buffers
745 * for the fat table. (see msdosfs_strategy)
747 if (bp->b_blkno == bp->b_lblkno) {
748 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno,
753 if (bp->b_blkno == -1) {
756 error = EIO; /* XXX */
761 * The block we need to write into exists, so read it in.
763 error = bread(thisvp, bn, pmp->pm_bpcluster, &bp);
771 * Should these vnode_pager_* functions be done on dir
776 * Copy the data from user space into the buf header.
778 error = uiomove(bp->b_data + croffset, n, uio);
785 * If they want this synchronous then write it and wait for
786 * it. Otherwise, if on a cluster boundary write it
787 * asynchronously so we can move on to the next block
788 * without delay. Otherwise do a delayed write because we
789 * may want to write somemore into the block later.
791 if (ioflag & IO_SYNC)
793 else if (n + croffset == pmp->pm_bpcluster)
797 dep->de_flag |= DE_UPDATE;
798 } while (error == 0 && uio->uio_resid > 0);
801 * If the write failed and they want us to, truncate the file back
802 * to the size it was before the write was attempted.
806 if (ioflag & IO_UNIT) {
807 detrunc(dep, osize, ioflag & IO_SYNC, NULL);
808 uio->uio_offset -= resid - uio->uio_resid;
809 uio->uio_resid = resid;
811 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NULL);
812 if (uio->uio_resid != resid)
815 } else if (ioflag & IO_SYNC)
816 error = deupdat(dep, 1);
821 * Flush the blocks of a file to disk.
823 * This function is worthless for vnodes that represent directories. Maybe we
824 * could just do a sync if they try an fsync on a directory file.
826 * msdosfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
827 * struct thread *a_td)
830 msdosfs_fsync(struct vop_fsync_args *ap)
832 struct vnode *vp = ap->a_vp;
834 struct buf *bp, *nbp;
837 * Flush all dirty buffers associated with a vnode.
841 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
842 nbp = TAILQ_NEXT(bp, b_vnbufs);
843 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
845 if ((bp->b_flags & B_DELWRI) == 0)
846 panic("msdosfs_fsync: not dirty");
852 while (vp->v_numoutput) {
853 vp->v_flag |= VBWAIT;
854 (void) tsleep((caddr_t)&vp->v_numoutput, 0, "msdosfsn", 0);
857 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
858 vprint("msdosfs_fsync: dirty", vp);
863 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
867 * msdosfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
868 * struct componentname *a_cnp)
871 msdosfs_remove(struct vop_remove_args *ap)
873 struct denode *dep = VTODE(ap->a_vp);
874 struct denode *ddep = VTODE(ap->a_dvp);
877 if (ap->a_vp->v_type == VDIR)
880 error = removede(ddep, dep);
882 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
888 * DOS filesystems don't know what links are. But since we already called
889 * msdosfs_lookup() with create and lockparent, the parent is locked so we
890 * have to free it before we return the error.
892 * msdosfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
893 * struct componentname *a_cnp)
896 msdosfs_link(struct vop_link_args *ap)
902 * Renames on files require moving the denode to a new hash queue since the
903 * denode's location is used to compute which hash queue to put the file
904 * in. Unless it is a rename in place. For example "mv a b".
906 * What follows is the basic algorithm:
909 * if (dest file exists) {
912 * if (dest and src in same directory) {
913 * rewrite name in existing directory slot
915 * write new entry in dest directory
916 * update offset and dirclust in denode
917 * move denode to new hash chain
918 * clear old directory entry
922 * if (dest directory exists) {
923 * if (dest is not empty) {
926 * remove dest directory
928 * if (dest and src in same directory) {
929 * rewrite name in existing entry
931 * be sure dest is not a child of src directory
932 * write entry in dest directory
933 * update "." and ".." in moved directory
934 * clear old directory entry for moved directory
939 * source's parent directory is unlocked
940 * source file or directory is unlocked
941 * destination's parent directory is locked
942 * destination file or directory is locked if it exists
945 * all denodes should be released
948 * I'm not sure how the memory containing the pathnames pointed at by the
949 * componentname structures is freed, there may be some memory bleeding
950 * for each rename done.
952 * msdosfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
953 * struct componentname *a_fcnp, struct vnode *a_tdvp,
954 * struct vnode *a_tvp, struct componentname *a_tcnp)
957 msdosfs_rename(struct vop_rename_args *ap)
959 struct vnode *tdvp = ap->a_tdvp;
960 struct vnode *fvp = ap->a_fvp;
961 struct vnode *fdvp = ap->a_fdvp;
962 struct vnode *tvp = ap->a_tvp;
963 struct componentname *tcnp = ap->a_tcnp;
964 struct componentname *fcnp = ap->a_fcnp;
965 struct thread *td = fcnp->cn_td;
966 struct denode *ip, *xp, *dp, *zp;
967 u_char toname[11], oldname[11];
968 u_long from_diroffset, to_diroffset;
970 int doingdirectory = 0, newparent = 0;
974 struct denode *fddep; /* from file's parent directory */
975 struct msdosfsmount *pmp;
976 struct direntry *dotdotp;
979 fddep = VTODE(ap->a_fdvp);
982 pmp = VFSTOMSDOSFS(fdvp->v_mount);
985 * Check for cross-device rename.
987 if ((fvp->v_mount != tdvp->v_mount) ||
988 (tvp && (fvp->v_mount != tvp->v_mount))) {
1003 * If source and dest are the same, do nothing.
1011 * fvp, fdvp are unlocked, tvp, tdvp are locked. Lock fvp and note
1012 * that we have to unlock it to use the abortit target.
1014 error = vn_lock(fvp, LK_EXCLUSIVE, td);
1021 * Be sure we are not renaming ".", "..", or an alias of ".". This
1022 * leads to a crippled directory tree. It's pretty tough to do a
1023 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1024 * doesn't work if the ".." entry is missing.
1026 if (ip->de_Attributes & ATTR_DIRECTORY) {
1028 * Avoid ".", "..", and aliases of "." for obvious reasons.
1030 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1032 (fcnp->cn_flags & CNP_ISDOTDOT) ||
1033 (tcnp->cn_flags & CNP_ISDOTDOT) ||
1034 (ip->de_flag & DE_RENAME)) {
1035 VOP_UNLOCK(fvp, 0, td);
1039 ip->de_flag |= DE_RENAME;
1044 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only
1045 * set if doingdirectory. We will get fvp unlocked in fairly
1046 * short order. dp and xp must be setup and fvp must be unlocked
1047 * for the out and bad targets to work properly.
1050 xp = tvp ? VTODE(tvp) : NULL;
1053 * Remember direntry place to use for destination
1055 to_diroffset = dp->de_fndoffset;
1056 to_count = dp->de_fndcnt;
1059 * If ".." must be changed (ie the directory gets a new
1060 * parent) then the source directory must not be in the
1061 * directory heirarchy above the target, as this would
1062 * orphan everything below the source directory. Also
1063 * the user must have write permission in the source so
1064 * as to be able to change "..". We must repeat the call
1065 * to namei, as the parent directory is unlocked by the
1066 * call to doscheckpath().
1068 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
1069 VOP_UNLOCK(fvp, 0, td);
1070 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1074 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1075 * DE_RENAME only set if doingdirectory.
1077 if (doingdirectory && newparent) {
1078 if (error) /* write access check above */
1085 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1086 * get an extra ref so we wind up with just an unlocked, ref'd
1087 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp
1088 * isn't locked so we can't use the out target).
1091 error = doscheckpath(ip, dp);
1092 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1098 * relookup no longer messes with the ref count. tdvp must
1099 * be unlocked on entry and on success will be locked on
1102 error = relookup(tdvp, &tvp, tcnp);
1104 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1112 * tvp and tdvp are now locked again.
1115 xp = tvp ? VTODE(tvp) : NULL;
1119 * tvp and tdvp are now locked again, the 'bad' target can be used
1120 * to clean them up again. Delete an existant target and clean
1121 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up.
1125 * Target must be empty if a directory and have no links
1126 * to it. Also, ensure source and target are compatible
1127 * (both directories, or both not directories).
1129 if (xp->de_Attributes & ATTR_DIRECTORY) {
1130 if (!dosdirempty(xp)) {
1134 if (!doingdirectory) {
1138 } else if (doingdirectory) {
1142 error = removede(dp, xp);
1151 * Convert the filename in tcnp into a dos filename. We copy this
1152 * into the denode and directory entry for the destination
1155 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1160 * Since from wasn't locked at various places above, we have to do
1161 * a relookup here. If the target and source are the same directory
1162 * we have to unlock the target directory in order to safely relookup
1163 * the source, because relookup expects its directory to be unlocked.
1165 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must
1166 * now take that into account.
1168 * The tdvp locking issues make this a real mess.
1170 fcnp->cn_flags &= ~CNP_MODMASK;
1171 fcnp->cn_flags |= CNP_LOCKPARENT;
1173 VOP_UNLOCK(tdvp, 0, td);
1174 error = relookup(fdvp, &fvp, fcnp);
1175 if (error || fvp == NULL) {
1177 * From name has disappeared. Note: fdvp might == tdvp.
1179 * DE_RENAME is only set if doingdirectory.
1182 panic("rename: lost dir entry");
1183 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1196 * No error occured. tdvp, fdvp and fvp are all locked. If
1197 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned
1198 * up. ap->a_fvp is still refd.
1202 from_diroffset = zp->de_fndoffset;
1205 * Ensure that the directory entry still exists and has not
1206 * changed till now. If the source is a file the entry may
1207 * have been unlinked or renamed. In either case there is
1208 * no further work to be done. If the source is a directory
1209 * then it cannot have been rmdir'ed or renamed; this is
1210 * prohibited by the DE_RENAME flag.
1212 * DE_RENAME is only set if doingdirectory.
1216 panic("rename: lost dir entry");
1219 u_long new_dirclust;
1220 u_long new_diroffset;
1223 * First write a new entry in the destination
1224 * directory and mark the entry in the source directory
1225 * as deleted. Then move the denode to the correct hash
1226 * chain for its new location in the filesystem. And, if
1227 * we moved a directory, then update its .. entry to point
1228 * to the new parent directory.
1230 bcopy(ip->de_Name, oldname, 11);
1231 bcopy(toname, ip->de_Name, 11); /* update denode */
1232 dp->de_fndoffset = to_diroffset;
1233 dp->de_fndcnt = to_count;
1234 error = createde(ip, dp, (struct denode **)0, tcnp);
1236 bcopy(oldname, ip->de_Name, 11);
1240 zp->de_fndoffset = from_diroffset;
1241 error = removede(zp, ip);
1243 /* XXX should really panic here, fs is corrupt */
1246 if (!doingdirectory) {
1247 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1250 /* XXX should really panic here, fs is corrupt */
1253 if (new_dirclust == MSDOSFSROOT)
1254 new_diroffset = to_diroffset;
1256 new_diroffset = to_diroffset & pmp->pm_crbomask;
1257 msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1262 * If we moved a directory to a new parent directory, then we must
1263 * fixup the ".." entry in the moved directory.
1265 if (doingdirectory && newparent) {
1266 cn = ip->de_StartCluster;
1267 if (cn == MSDOSFSROOT) {
1268 /* this should never happen */
1269 panic("msdosfs_rename(): updating .. in root directory?");
1271 bn = cntobn(pmp, cn);
1273 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster, &bp);
1275 /* XXX should really panic here, fs is corrupt */
1279 dotdotp = (struct direntry *)bp->b_data + 1;
1280 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1282 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1285 /* XXX should really panic here, fs is corrupt */
1291 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd
1295 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */
1306 * 'bad' target: xp governs tvp. tvp and tdvp arel ocked, fdvp and fvp
1307 * are not locked. ip points to fvp's inode which may have DE_RENAME
1316 * 'out' target: tvp and tdvp have already been cleaned up.
1319 ip->de_flag &= ~DE_RENAME;
1327 struct direntry dot;
1328 struct direntry dotdot;
1329 } dosdirtemplate = {
1330 { ". ", " ", /* the . entry */
1331 ATTR_DIRECTORY, /* file attribute */
1333 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1334 { 0, 0 }, /* access date */
1335 { 0, 0 }, /* high bits of start cluster */
1336 { 210, 4 }, { 210, 4 }, /* modify time & date */
1337 { 0, 0 }, /* startcluster */
1338 { 0, 0, 0, 0 } /* filesize */
1340 { ".. ", " ", /* the .. entry */
1341 ATTR_DIRECTORY, /* file attribute */
1343 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1344 { 0, 0 }, /* access date */
1345 { 0, 0 }, /* high bits of start cluster */
1346 { 210, 4 }, { 210, 4 }, /* modify time & date */
1347 { 0, 0 }, /* startcluster */
1348 { 0, 0, 0, 0 } /* filesize */
1353 * msdosfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1354 * struct componentname *a_cnp, struct vattr *a_vap)
1357 msdosfs_mkdir(struct vop_mkdir_args *ap)
1359 struct componentname *cnp = ap->a_cnp;
1361 struct denode *pdep = VTODE(ap->a_dvp);
1362 struct direntry *denp;
1363 struct msdosfsmount *pmp = pdep->de_pmp;
1365 u_long newcluster, pcl;
1368 struct denode ndirent;
1372 * If this is the root directory and there is no space left we
1373 * can't do anything. This is because the root directory can not
1376 if (pdep->de_StartCluster == MSDOSFSROOT
1377 && pdep->de_fndoffset >= pdep->de_FileSize) {
1383 * Allocate a cluster to hold the about to be created directory.
1385 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1389 bzero(&ndirent, sizeof(ndirent));
1390 ndirent.de_pmp = pmp;
1391 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1393 DETIMES(&ndirent, &ts, &ts, &ts);
1396 * Now fill the cluster with the "." and ".." entries. And write
1397 * the cluster to disk. This way it is there for the parent
1398 * directory to be pointing at if there were a crash.
1400 bn = cntobn(pmp, newcluster);
1401 /* always succeeds */
1402 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0);
1403 bzero(bp->b_data, pmp->pm_bpcluster);
1404 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1405 denp = (struct direntry *)bp->b_data;
1406 putushort(denp[0].deStartCluster, newcluster);
1407 putushort(denp[0].deCDate, ndirent.de_CDate);
1408 putushort(denp[0].deCTime, ndirent.de_CTime);
1409 denp[0].deCHundredth = ndirent.de_CHun;
1410 putushort(denp[0].deADate, ndirent.de_ADate);
1411 putushort(denp[0].deMDate, ndirent.de_MDate);
1412 putushort(denp[0].deMTime, ndirent.de_MTime);
1413 pcl = pdep->de_StartCluster;
1414 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1416 putushort(denp[1].deStartCluster, pcl);
1417 putushort(denp[1].deCDate, ndirent.de_CDate);
1418 putushort(denp[1].deCTime, ndirent.de_CTime);
1419 denp[1].deCHundredth = ndirent.de_CHun;
1420 putushort(denp[1].deADate, ndirent.de_ADate);
1421 putushort(denp[1].deMDate, ndirent.de_MDate);
1422 putushort(denp[1].deMTime, ndirent.de_MTime);
1424 putushort(denp[0].deHighClust, newcluster >> 16);
1425 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1433 * Now build up a directory entry pointing to the newly allocated
1434 * cluster. This will be written to an empty slot in the parent
1437 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1441 ndirent.de_Attributes = ATTR_DIRECTORY;
1442 ndirent.de_LowerCase = 0;
1443 ndirent.de_StartCluster = newcluster;
1444 ndirent.de_FileSize = 0;
1445 ndirent.de_dev = pdep->de_dev;
1446 ndirent.de_devvp = pdep->de_devvp;
1447 error = createde(&ndirent, pdep, &dep, cnp);
1450 *ap->a_vpp = DETOV(dep);
1454 clusterfree(pmp, newcluster, NULL);
1460 * msdosfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1461 * struct componentname *a_cnp)
1464 msdosfs_rmdir(struct vop_rmdir_args *ap)
1466 struct vnode *vp = ap->a_vp;
1467 struct vnode *dvp = ap->a_dvp;
1468 struct componentname *cnp = ap->a_cnp;
1469 struct denode *ip, *dp;
1470 struct thread *td = cnp->cn_td;
1477 * Verify the directory is empty (and valid).
1478 * (Rmdir ".." won't be valid since
1479 * ".." will contain a reference to
1480 * the current directory and thus be
1484 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1489 * Delete the entry from the directory. For dos filesystems this
1490 * gets rid of the directory entry on disk, the in memory copy
1491 * still exists but the de_refcnt is <= 0. This prevents it from
1492 * being found by deget(). When the vput() on dep is done we give
1493 * up access and eventually msdosfs_reclaim() will be called which
1494 * will remove it from the denode cache.
1496 error = removede(dp, ip);
1500 * This is where we decrement the link count in the parent
1501 * directory. Since dos filesystems don't do this we just purge
1504 VOP_UNLOCK(dvp, 0, td);
1506 * Truncate the directory that is being deleted.
1508 error = detrunc(ip, (u_long)0, IO_SYNC, td);
1510 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1516 * DOS filesystems don't know what symlinks are.
1518 * msdosfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1519 * struct componentname *a_cnp, struct vattr *a_vap,
1523 msdosfs_symlink(struct vop_symlink_args *ap)
1525 return (EOPNOTSUPP);
1529 * msdosfs_readdir(struct vnode *a_vp, struct uio *a_uio,
1530 * struct ucred *a_cred, int *a_eofflag, int *a_ncookies,
1531 * u_long **a_cookies)
1534 msdosfs_readdir(struct vop_readdir_args *ap)
1547 struct denode *dep = VTODE(ap->a_vp);
1548 struct msdosfsmount *pmp = dep->de_pmp;
1549 struct direntry *dentp;
1550 struct dirent dirbuf;
1551 struct uio *uio = ap->a_uio;
1552 u_long *cookies = NULL;
1557 #ifdef MSDOSFS_DEBUG
1558 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1559 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1563 * msdosfs_readdir() won't operate properly on regular files since
1564 * it does i/o only with the the filesystem vnode, and hence can
1565 * retrieve the wrong block from the buffer cache for a plain file.
1566 * So, fail attempts to readdir() on a plain file.
1568 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1572 * To be safe, initialize dirbuf
1574 bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1577 * If the user buffer is smaller than the size of one dos directory
1578 * entry or the file offset is not a multiple of the size of a
1579 * directory entry, then we fail the read.
1581 off = offset = uio->uio_offset;
1582 if (uio->uio_resid < sizeof(struct direntry) ||
1583 (offset & (sizeof(struct direntry) - 1)))
1586 if (ap->a_ncookies) {
1587 ncookies = uio->uio_resid / 16;
1588 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1590 *ap->a_cookies = cookies;
1591 *ap->a_ncookies = ncookies;
1594 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1597 * If they are reading from the root directory then, we simulate
1598 * the . and .. entries since these don't exist in the root
1599 * directory. We also set the offset bias to make up for having to
1600 * simulate these entries. By this I mean that at file offset 64 we
1601 * read the first entry in the root directory that lives on disk.
1603 if (dep->de_StartCluster == MSDOSFSROOT
1604 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1606 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1609 bias = 2 * sizeof(struct direntry);
1610 if (offset < bias) {
1611 for (n = (int)offset / sizeof(struct direntry);
1614 dirbuf.d_fileno = cntobn(pmp,
1618 dirbuf.d_fileno = 1;
1619 dirbuf.d_type = DT_DIR;
1622 dirbuf.d_namlen = 1;
1623 strcpy(dirbuf.d_name, ".");
1626 dirbuf.d_namlen = 2;
1627 strcpy(dirbuf.d_name, "..");
1630 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1631 if (uio->uio_resid < dirbuf.d_reclen)
1633 error = uiomove((caddr_t) &dirbuf,
1634 dirbuf.d_reclen, uio);
1637 offset += sizeof(struct direntry);
1640 *cookies++ = offset;
1641 if (--ncookies <= 0)
1649 while (uio->uio_resid > 0) {
1650 lbn = de_cluster(pmp, offset - bias);
1651 on = (offset - bias) & pmp->pm_crbomask;
1652 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1653 diff = dep->de_FileSize - (offset - bias);
1657 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1660 error = bread(pmp->pm_devvp, bn, blsize, &bp);
1665 n = min(n, blsize - bp->b_resid);
1668 * Convert from dos directory entries to fs-independent
1669 * directory entries.
1671 for (dentp = (struct direntry *)(bp->b_data + on);
1672 (char *)dentp < bp->b_data + on + n;
1673 dentp++, offset += sizeof(struct direntry)) {
1675 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1676 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1679 * If this is an unused entry, we can stop.
1681 if (dentp->deName[0] == SLOT_EMPTY) {
1686 * Skip deleted entries.
1688 if (dentp->deName[0] == SLOT_DELETED) {
1694 * Handle Win95 long directory entries
1696 if (dentp->deAttributes == ATTR_WIN95) {
1697 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1699 chksum = win2unixfn((struct winentry *)dentp,
1701 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1707 * Skip volume labels
1709 if (dentp->deAttributes & ATTR_VOLUME) {
1714 * This computation of d_fileno must match
1715 * the computation of va_fileid in
1718 if (dentp->deAttributes & ATTR_DIRECTORY) {
1719 fileno = getushort(dentp->deStartCluster);
1721 fileno |= getushort(dentp->deHighClust) << 16;
1722 /* if this is the root directory */
1723 if (fileno == MSDOSFSROOT)
1725 fileno = cntobn(pmp,
1731 fileno = cntobn(pmp, fileno) * dirsperblk;
1732 dirbuf.d_fileno = fileno;
1733 dirbuf.d_type = DT_DIR;
1735 dirbuf.d_fileno = offset / sizeof(struct direntry);
1736 dirbuf.d_type = DT_REG;
1738 if (chksum != winChksum(dentp->deName))
1739 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1740 (u_char *)dirbuf.d_name,
1741 dentp->deLowerCase |
1742 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1743 (LCASE_BASE | LCASE_EXT) : 0),
1744 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1746 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1749 dirbuf.d_name[dirbuf.d_namlen] = 0;
1751 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1752 if (uio->uio_resid < dirbuf.d_reclen) {
1756 error = uiomove((caddr_t) &dirbuf,
1757 dirbuf.d_reclen, uio);
1763 *cookies++ = offset + sizeof(struct direntry);
1764 if (--ncookies <= 0) {
1769 off = offset + sizeof(struct direntry);
1774 /* Subtract unused cookies */
1776 *ap->a_ncookies -= ncookies;
1778 uio->uio_offset = off;
1781 * Set the eofflag (NFS uses it)
1783 if (ap->a_eofflag) {
1784 if (dep->de_FileSize - (offset - bias) <= 0)
1793 * vp - address of vnode file the file
1794 * bn - which cluster we are interested in mapping to a filesystem block number.
1795 * vpp - returns the vnode for the block special file holding the filesystem
1796 * containing the file of interest
1797 * bnp - address of where to return the filesystem relative block number
1799 * msdosfs_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
1800 * daddr_t *a_bnp, int *a_runp, int *a_runb)
1803 msdosfs_bmap(struct vop_bmap_args *ap)
1805 struct denode *dep = VTODE(ap->a_vp);
1807 if (ap->a_vpp != NULL)
1808 *ap->a_vpp = dep->de_devvp;
1809 if (ap->a_bnp == NULL)
1813 * Sequential clusters should be counted here.
1820 return (pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0));
1824 * msdosfs_strategy(struct vnode *a_vp, struct buf *a_bp)
1827 msdosfs_strategy(struct vop_strategy_args *ap)
1829 struct buf *bp = ap->a_bp;
1830 struct denode *dep = VTODE(bp->b_vp);
1834 if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
1835 panic("msdosfs_strategy: spec");
1837 * If we don't already know the filesystem relative block number
1838 * then get it using pcbmap(). If pcbmap() returns the block
1839 * number as -1 then we've got a hole in the file. DOS filesystems
1840 * don't allow files with holes, so we shouldn't ever see this.
1842 if (bp->b_blkno == bp->b_lblkno) {
1843 error = pcbmap(dep, bp->b_lblkno, &bp->b_blkno, 0, 0);
1845 bp->b_error = error;
1846 bp->b_flags |= B_ERROR;
1850 if ((long)bp->b_blkno == -1)
1853 if (bp->b_blkno == -1) {
1858 * Read/write the block from/to the disk that contains the desired
1862 bp->b_dev = vp->v_rdev;
1863 VOP_STRATEGY(vp, bp);
1868 * msdosfs_print(struct vnode *vp)
1871 msdosfs_print(struct vop_print_args *ap)
1873 struct denode *dep = VTODE(ap->a_vp);
1876 "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1877 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1878 printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1879 lockmgr_printinfo(&ap->a_vp->v_lock);
1885 * msdosfs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1888 msdosfs_pathconf(struct vop_pathconf_args *ap)
1890 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1892 switch (ap->a_name) {
1897 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1900 *ap->a_retval = PATH_MAX;
1902 case _PC_CHOWN_RESTRICTED:
1917 * XXX By default, wimp out... note that a_offset is ignored (and always
1921 msdosfs_getpages(struct vop_getpages_args *ap)
1923 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1930 * XXX By default, wimp out... note that a_offset is ignored (and always
1934 msdosfs_putpages(struct vop_putpages_args *ap)
1936 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1937 ap->a_sync, ap->a_rtvals);
1940 /* Global vfs data structures for msdosfs */
1941 struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1942 { &vop_default_desc, vop_defaultop },
1943 { &vop_access_desc, (void *) msdosfs_access },
1944 { &vop_bmap_desc, (void *) msdosfs_bmap },
1945 { &vop_lookup_desc, (void *) msdosfs_lookup },
1946 { &vop_close_desc, (void *) msdosfs_close },
1947 { &vop_create_desc, (void *) msdosfs_create },
1948 { &vop_fsync_desc, (void *) msdosfs_fsync },
1949 { &vop_getattr_desc, (void *) msdosfs_getattr },
1950 { &vop_inactive_desc, (void *) msdosfs_inactive },
1951 { &vop_islocked_desc, (void *) vop_stdislocked },
1952 { &vop_link_desc, (void *) msdosfs_link },
1953 { &vop_lock_desc, (void *) vop_stdlock },
1954 { &vop_mkdir_desc, (void *) msdosfs_mkdir },
1955 { &vop_mknod_desc, (void *) msdosfs_mknod },
1956 { &vop_pathconf_desc, (void *) msdosfs_pathconf },
1957 { &vop_print_desc, (void *) msdosfs_print },
1958 { &vop_read_desc, (void *) msdosfs_read },
1959 { &vop_readdir_desc, (void *) msdosfs_readdir },
1960 { &vop_reclaim_desc, (void *) msdosfs_reclaim },
1961 { &vop_remove_desc, (void *) msdosfs_remove },
1962 { &vop_rename_desc, (void *) msdosfs_rename },
1963 { &vop_rmdir_desc, (void *) msdosfs_rmdir },
1964 { &vop_setattr_desc, (void *) msdosfs_setattr },
1965 { &vop_strategy_desc, (void *) msdosfs_strategy },
1966 { &vop_symlink_desc, (void *) msdosfs_symlink },
1967 { &vop_unlock_desc, (void *) vop_stdunlock },
1968 { &vop_write_desc, (void *) msdosfs_write },
1969 { &vop_getpages_desc, (void *) msdosfs_getpages },
1970 { &vop_putpages_desc, (void *) msdosfs_putpages },