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.43 2006/09/05 00:55:50 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_old_create_args *);
86 static int msdosfs_mknod (struct vop_old_mknod_args *);
87 static int msdosfs_open (struct vop_open_args *);
88 static int msdosfs_close (struct vop_close_args *);
89 static int msdosfs_access (struct vop_access_args *);
90 static int msdosfs_getattr (struct vop_getattr_args *);
91 static int msdosfs_setattr (struct vop_setattr_args *);
92 static int msdosfs_read (struct vop_read_args *);
93 static int msdosfs_write (struct vop_write_args *);
94 static int msdosfs_fsync (struct vop_fsync_args *);
95 static int msdosfs_remove (struct vop_old_remove_args *);
96 static int msdosfs_link (struct vop_old_link_args *);
97 static int msdosfs_rename (struct vop_old_rename_args *);
98 static int msdosfs_mkdir (struct vop_old_mkdir_args *);
99 static int msdosfs_rmdir (struct vop_old_rmdir_args *);
100 static int msdosfs_symlink (struct vop_old_symlink_args *);
101 static int msdosfs_readdir (struct vop_readdir_args *);
102 static int msdosfs_bmap (struct vop_bmap_args *);
103 static int msdosfs_strategy (struct vop_strategy_args *);
104 static int msdosfs_print (struct vop_print_args *);
105 static int msdosfs_pathconf (struct vop_pathconf_args *ap);
106 static int msdosfs_getpages (struct vop_getpages_args *);
107 static int msdosfs_putpages (struct vop_putpages_args *);
110 * Some general notes:
112 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
113 * read/written using the vnode for the filesystem. Blocks that represent
114 * the contents of a file are read/written using the vnode for the file
115 * (including directories when they are read/written as files). This
116 * presents problems for the dos filesystem because data that should be in
117 * an inode (if dos had them) resides in the directory itself. Since we
118 * must update directory entries without the benefit of having the vnode
119 * for the directory we must use the vnode for the filesystem. This means
120 * that when a directory is actually read/written (via read, write, or
121 * readdir, or seek) we must use the vnode for the filesystem instead of
122 * the vnode for the directory as would happen in ufs. This is to insure we
123 * retreive the correct block from the buffer cache since the hash value is
124 * based upon the vnode address and the desired block number.
128 * Create a regular file. On entry the directory to contain the file being
129 * created is locked. We must release before we return.
131 * msdosfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
132 * struct componentname *a_cnp, struct vattr *a_vap)
135 msdosfs_create(struct vop_old_create_args *ap)
137 struct componentname *cnp = ap->a_cnp;
138 struct denode ndirent;
140 struct denode *pdep = VTODE(ap->a_dvp);
145 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
149 * If this is the root directory and there is no space left we
150 * can't do anything. This is because the root directory can not
153 if (pdep->de_StartCluster == MSDOSFSROOT
154 && pdep->de_fndoffset >= pdep->de_FileSize) {
160 * Create a directory entry for the file, then call createde() to
161 * have it installed. NOTE: DOS files are always executable. We
162 * use the absence of the owner write bit to make the file
165 bzero(&ndirent, sizeof(ndirent));
166 error = uniqdosname(pdep, cnp, ndirent.de_Name);
170 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
171 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
172 ndirent.de_LowerCase = 0;
173 ndirent.de_StartCluster = 0;
174 ndirent.de_FileSize = 0;
175 ndirent.de_dev = pdep->de_dev;
176 ndirent.de_devvp = pdep->de_devvp;
177 ndirent.de_pmp = pdep->de_pmp;
178 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
180 DETIMES(&ndirent, &ts, &ts, &ts);
181 error = createde(&ndirent, pdep, &dep, cnp);
184 *ap->a_vpp = DETOV(dep);
192 * msdosfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
193 * struct componentname *a_cnp, struct vattr *a_vap)
196 msdosfs_mknod(struct vop_old_mknod_args *ap)
198 switch (ap->a_vap->va_type) {
200 return (msdosfs_mkdir((struct vop_old_mkdir_args *)ap));
204 return (msdosfs_create((struct vop_old_create_args *)ap));
214 * msdosfs_open(struct vnode *a_vp)
217 msdosfs_open(struct vop_open_args *ap)
219 return(vop_stdopen(ap));
223 * msdosfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
224 * struct thread *a_td)
227 msdosfs_close(struct vop_close_args *ap)
229 struct vnode *vp = ap->a_vp;
230 struct denode *dep = VTODE(vp);
233 if (vp->v_usecount > 1) {
235 if (vp->v_usecount > 1) {
236 DETIMES(dep, &ts, &ts, &ts);
239 return (vop_stdclose(ap));
243 * msdosfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
244 * struct thread *a_td)
247 msdosfs_access(struct vop_access_args *ap)
249 struct vnode *vp = ap->a_vp;
250 struct denode *dep = VTODE(ap->a_vp);
251 struct msdosfsmount *pmp = dep->de_pmp;
252 struct ucred *cred = ap->a_cred;
253 mode_t mask, file_mode, mode = ap->a_mode;
257 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
258 ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
259 file_mode &= pmp->pm_mask;
262 * Disallow write attempts on read-only file systems;
263 * unless the file is a socket, fifo, or a block or
264 * character device resident on the file system.
267 switch (vp->v_type) {
271 if (vp->v_mount->mnt_flag & MNT_RDONLY)
279 /* User id 0 always gets access. */
280 if (cred->cr_uid == 0)
285 /* Otherwise, check the owner. */
286 if (cred->cr_uid == pmp->pm_uid) {
293 return (file_mode & mask) == mask ? 0 : EACCES;
296 /* Otherwise, check the groups. */
297 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
298 if (pmp->pm_gid == *gp) {
305 return (file_mode & mask) == mask ? 0 : EACCES;
308 /* Otherwise, check everyone else. */
315 return (file_mode & mask) == mask ? 0 : EACCES;
319 * msdosfs_getattr(struct vnode *a_vp, struct vattr *a_vap,
320 * struct ucred *a_cred, struct thread *a_td)
323 msdosfs_getattr(struct vop_getattr_args *ap)
325 struct denode *dep = VTODE(ap->a_vp);
326 struct msdosfsmount *pmp = dep->de_pmp;
327 struct vattr *vap = ap->a_vap;
330 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
334 DETIMES(dep, &ts, &ts, &ts);
335 vap->va_fsid = dev2udev(dep->de_dev);
337 * The following computation of the fileid must be the same as that
338 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
341 if (dep->de_Attributes & ATTR_DIRECTORY) {
342 fileid = xcntobn(pmp, dep->de_StartCluster) * dirsperblk;
343 if (dep->de_StartCluster == MSDOSFSROOT)
346 fileid = xcntobn(pmp, dep->de_dirclust) * dirsperblk;
347 if (dep->de_dirclust == MSDOSFSROOT)
348 fileid = roottobn(pmp, 0) * dirsperblk;
349 fileid += dep->de_diroffset / sizeof(struct direntry);
351 vap->va_fileid = fileid;
352 if ((dep->de_Attributes & ATTR_READONLY) == 0)
353 mode = S_IRWXU|S_IRWXG|S_IRWXO;
355 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
356 vap->va_mode = mode & pmp->pm_mask;
357 vap->va_uid = pmp->pm_uid;
358 vap->va_gid = pmp->pm_gid;
361 vap->va_size = dep->de_FileSize;
362 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
363 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
364 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
365 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
367 vap->va_atime = vap->va_mtime;
368 vap->va_ctime = vap->va_mtime;
371 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
372 vap->va_flags |= SF_ARCHIVED;
374 vap->va_blocksize = pmp->pm_bpcluster;
376 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
377 vap->va_type = ap->a_vp->v_type;
378 vap->va_filerev = dep->de_modrev;
383 * msdosfs_setattr(struct vnode *a_vp, struct vattr *a_vap,
384 * struct ucred *a_cred, struct thread *a_td)
387 msdosfs_setattr(struct vop_setattr_args *ap)
389 struct vnode *vp = ap->a_vp;
390 struct denode *dep = VTODE(ap->a_vp);
391 struct msdosfsmount *pmp = dep->de_pmp;
392 struct vattr *vap = ap->a_vap;
393 struct ucred *cred = ap->a_cred;
397 printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
398 ap->a_vp, vap, cred, ap->a_td);
402 * Check for unsettable attributes.
404 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
405 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
406 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
407 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
409 printf("msdosfs_setattr(): returning EINVAL\n");
410 printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
411 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
412 printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
413 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
414 printf(" va_uid %x, va_gid %x\n",
415 vap->va_uid, vap->va_gid);
419 if (vap->va_flags != VNOVAL) {
420 if (vp->v_mount->mnt_flag & MNT_RDONLY)
422 if (cred->cr_uid != pmp->pm_uid &&
423 (error = suser_cred(cred, PRISON_ROOT)))
426 * We are very inconsistent about handling unsupported
427 * attributes. We ignored the access time and the
428 * read and execute bits. We were strict for the other
431 * Here we are strict, stricter than ufs in not allowing
432 * users to attempt to set SF_SETTABLE bits or anyone to
433 * set unsupported bits. However, we ignore attempts to
434 * set ATTR_ARCHIVE for directories `cp -pr' from a more
435 * sensible file system attempts it a lot.
437 if (cred->cr_uid != 0) {
438 if (vap->va_flags & SF_SETTABLE)
441 if (vap->va_flags & ~SF_ARCHIVED)
443 if (vap->va_flags & SF_ARCHIVED)
444 dep->de_Attributes &= ~ATTR_ARCHIVE;
445 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
446 dep->de_Attributes |= ATTR_ARCHIVE;
447 dep->de_flag |= DE_MODIFIED;
450 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
454 if (vp->v_mount->mnt_flag & MNT_RDONLY)
457 if (uid == (uid_t)VNOVAL)
460 if (gid == (gid_t)VNOVAL)
462 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
463 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
464 (error = suser_cred(cred, PRISON_ROOT)))
466 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
470 if (vap->va_size != VNOVAL) {
472 * Disallow write attempts on read-only file systems;
473 * unless the file is a socket, fifo, or a block or
474 * character device resident on the file system.
476 switch (vp->v_type) {
482 if (vp->v_mount->mnt_flag & MNT_RDONLY)
488 error = detrunc(dep, vap->va_size, 0);
492 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
493 if (vp->v_mount->mnt_flag & MNT_RDONLY)
495 if (cred->cr_uid != pmp->pm_uid &&
496 (error = suser_cred(cred, PRISON_ROOT)) &&
497 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
498 (error = VOP_ACCESS(ap->a_vp, VWRITE, cred))))
500 if (vp->v_type != VDIR) {
501 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
502 vap->va_atime.tv_sec != VNOVAL) {
503 dep->de_flag &= ~DE_ACCESS;
504 unix2dostime(&vap->va_atime, &dep->de_ADate,
507 if (vap->va_mtime.tv_sec != VNOVAL) {
508 dep->de_flag &= ~DE_UPDATE;
509 unix2dostime(&vap->va_mtime, &dep->de_MDate,
510 &dep->de_MTime, NULL);
512 dep->de_Attributes |= ATTR_ARCHIVE;
513 dep->de_flag |= DE_MODIFIED;
517 * DOS files only have the ability to have their writability
518 * attribute set, so we use the owner write bit to set the readonly
521 if (vap->va_mode != (mode_t)VNOVAL) {
522 if (vp->v_mount->mnt_flag & MNT_RDONLY)
524 if (cred->cr_uid != pmp->pm_uid &&
525 (error = suser_cred(cred, PRISON_ROOT)))
527 if (vp->v_type != VDIR) {
528 /* We ignore the read and execute bits. */
529 if (vap->va_mode & VWRITE)
530 dep->de_Attributes &= ~ATTR_READONLY;
532 dep->de_Attributes |= ATTR_READONLY;
533 dep->de_Attributes |= ATTR_ARCHIVE;
534 dep->de_flag |= DE_MODIFIED;
537 return (deupdat(dep, 1));
541 * msdosfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
542 * struct ucred *a_cred)
545 msdosfs_read(struct vop_read_args *ap)
561 struct vnode *vp = ap->a_vp;
562 struct denode *dep = VTODE(vp);
563 struct msdosfsmount *pmp = dep->de_pmp;
564 struct uio *uio = ap->a_uio;
566 if (uio->uio_offset < 0)
569 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
572 * If they didn't ask for any data, then we are done.
574 orig_resid = uio->uio_resid;
578 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
580 isadir = dep->de_Attributes & ATTR_DIRECTORY;
582 if (uio->uio_offset >= dep->de_FileSize)
586 * note: lbn is a cluster number, not a device block number.
588 lbn = de_off2cn(pmp, uio->uio_offset);
589 loffset = de_cn2doff(pmp, lbn);
592 * If we are operating on a directory file then be sure to
593 * do i/o with the vnode for the filesystem instead of the
594 * vnode for the directory.
598 * convert cluster # to block #. lbn is a
599 * device block number after this.
601 error = pcbmap(dep, lbn, &lbn, NULL, &blsize);
602 loffset = de_bntodoff(pmp, lbn);
603 if (error == E2BIG) {
608 error = bread(pmp->pm_devvp, loffset, blsize, &bp);
610 blsize = pmp->pm_bpcluster;
612 raoffset = de_cn2doff(pmp, rablock);
614 raoffset < dep->de_FileSize) {
615 rasize = pmp->pm_bpcluster;
616 error = breadn(vp, loffset, blsize,
617 &raoffset, &rasize, 1, &bp);
619 error = bread(vp, loffset, blsize, &bp);
626 on = uio->uio_offset & pmp->pm_crbomask;
627 diff = pmp->pm_bpcluster - on;
628 n = diff > uio->uio_resid ? uio->uio_resid : diff;
629 diff = dep->de_FileSize - uio->uio_offset;
632 diff = blsize - bp->b_resid;
635 error = uiomove(bp->b_data + on, (int) n, uio);
637 } while (error == 0 && uio->uio_resid > 0 && n != 0);
638 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
639 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
640 dep->de_flag |= DE_ACCESS;
645 * Write data to a file or directory.
647 * msdosfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
648 * struct ucred *a_cred)
651 msdosfs_write(struct vop_write_args *ap)
661 int ioflag = ap->a_ioflag;
662 struct uio *uio = ap->a_uio;
663 struct thread *td = uio->uio_td;
664 struct vnode *vp = ap->a_vp;
665 struct vnode *thisvp;
666 struct denode *dep = VTODE(vp);
667 struct msdosfsmount *pmp = dep->de_pmp;
668 struct proc *p = (td ? td->td_proc : NULL);
671 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
672 vp, uio, ioflag, cred);
673 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
674 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
677 switch (vp->v_type) {
679 if (ioflag & IO_APPEND)
680 uio->uio_offset = dep->de_FileSize;
686 panic("msdosfs_write(): bad file type");
689 if (uio->uio_offset < 0)
692 if (uio->uio_resid == 0)
696 * If they've exceeded their filesize limit, tell them about it.
699 ((uoff_t)uio->uio_offset + uio->uio_resid >
700 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
705 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
709 * If the offset we are starting the write at is beyond the end of
710 * the file, then they've done a seek. Unix filesystems allow
711 * files with holes in them, DOS doesn't so we must fill the hole
712 * with zeroed blocks.
714 if (uio->uio_offset > dep->de_FileSize) {
715 error = deextend(dep, uio->uio_offset);
721 * Remember some values in case the write fails.
723 resid = uio->uio_resid;
724 osize = dep->de_FileSize;
727 * If we write beyond the end of the file, extend it to its ultimate
728 * size ahead of the time to hopefully get a contiguous area.
730 if (uio->uio_offset + resid > osize) {
731 count = de_clcount(pmp, uio->uio_offset + resid) -
732 de_clcount(pmp, osize);
733 error = extendfile(dep, count, NULL, NULL, 0);
734 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
736 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
738 lastcn = de_clcount(pmp, osize) - 1;
741 if (de_off2cn(pmp, uio->uio_offset) > lastcn) {
746 croffset = uio->uio_offset & pmp->pm_crbomask;
747 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
748 if (uio->uio_offset + n > dep->de_FileSize) {
749 dep->de_FileSize = uio->uio_offset + n;
750 /* The object size needs to be set before buffer is allocated */
751 vnode_pager_setsize(vp, dep->de_FileSize);
754 cn = de_off2cn(pmp, uio->uio_offset);
755 if ((uio->uio_offset & pmp->pm_crbomask) == 0
756 && (de_off2cn(pmp, uio->uio_offset + uio->uio_resid)
757 > de_off2cn(pmp, uio->uio_offset)
758 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
760 * If either the whole cluster gets written,
761 * or we write the cluster from its start beyond EOF,
762 * then no need to read data from disk.
764 bp = getblk(thisvp, de_cn2doff(pmp, cn),
765 pmp->pm_bpcluster, 0, 0);
768 * Do the bmap now, since pcbmap needs buffers
769 * for the fat table. (see msdosfs_strategy)
771 if (bp->b_bio2.bio_offset == NOOFFSET) {
772 daddr_t lblkno = de_off2cn(pmp, bp->b_loffset);
775 error = pcbmap(dep, lblkno,
776 &dblkno, NULL, NULL);
777 if (error || dblkno == (daddr_t)-1) {
778 bp->b_bio2.bio_offset = NOOFFSET;
780 bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
783 if (bp->b_bio2.bio_offset == NOOFFSET) {
786 error = EIO; /* XXX */
791 * The block we need to write into exists, so read
794 error = bread(thisvp, de_cn2doff(pmp, cn),
795 pmp->pm_bpcluster, &bp);
803 * Should these vnode_pager_* functions be done on dir
808 * Copy the data from user space into the buf header.
810 error = uiomove(bp->b_data + croffset, n, uio);
817 * If they want this synchronous then write it and wait for
818 * it. Otherwise, if on a cluster boundary write it
819 * asynchronously so we can move on to the next block
820 * without delay. Otherwise do a delayed write because we
821 * may want to write somemore into the block later.
823 if (ioflag & IO_SYNC)
825 else if (n + croffset == pmp->pm_bpcluster)
829 dep->de_flag |= DE_UPDATE;
830 } while (error == 0 && uio->uio_resid > 0);
833 * If the write failed and they want us to, truncate the file back
834 * to the size it was before the write was attempted.
838 if (ioflag & IO_UNIT) {
839 detrunc(dep, osize, ioflag & IO_SYNC);
840 uio->uio_offset -= resid - uio->uio_resid;
841 uio->uio_resid = resid;
843 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC);
844 if (uio->uio_resid != resid)
847 } else if (ioflag & IO_SYNC)
848 error = deupdat(dep, 1);
853 * Flush the blocks of a file to disk.
855 * This function is worthless for vnodes that represent directories. Maybe we
856 * could just do a sync if they try an fsync on a directory file.
858 * msdosfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
859 * struct thread *a_td)
862 msdosfs_fsync(struct vop_fsync_args *ap)
864 struct vnode *vp = ap->a_vp;
867 * Flush all dirty buffers associated with a vnode.
872 vfsync(vp, ap->a_waitfor, 0, NULL, NULL);
874 if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
875 vprint("msdosfs_fsync: dirty", vp);
879 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
883 * msdosfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
884 * struct componentname *a_cnp)
887 msdosfs_remove(struct vop_old_remove_args *ap)
889 struct denode *dep = VTODE(ap->a_vp);
890 struct denode *ddep = VTODE(ap->a_dvp);
893 if (ap->a_vp->v_type == VDIR)
896 error = removede(ddep, dep);
898 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
904 * DOS filesystems don't know what links are. But since we already called
905 * msdosfs_lookup() with create and lockparent, the parent is locked so we
906 * have to free it before we return the error.
908 * msdosfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
909 * struct componentname *a_cnp)
912 msdosfs_link(struct vop_old_link_args *ap)
918 * Renames on files require moving the denode to a new hash queue since the
919 * denode's location is used to compute which hash queue to put the file
920 * in. Unless it is a rename in place. For example "mv a b".
922 * What follows is the basic algorithm:
925 * if (dest file exists) {
928 * if (dest and src in same directory) {
929 * rewrite name in existing directory slot
931 * write new entry in dest directory
932 * update offset and dirclust in denode
933 * move denode to new hash chain
934 * clear old directory entry
938 * if (dest directory exists) {
939 * if (dest is not empty) {
942 * remove dest directory
944 * if (dest and src in same directory) {
945 * rewrite name in existing entry
947 * be sure dest is not a child of src directory
948 * write entry in dest directory
949 * update "." and ".." in moved directory
950 * clear old directory entry for moved directory
955 * source's parent directory is unlocked
956 * source file or directory is unlocked
957 * destination's parent directory is locked
958 * destination file or directory is locked if it exists
961 * all denodes should be released
964 * I'm not sure how the memory containing the pathnames pointed at by the
965 * componentname structures is freed, there may be some memory bleeding
966 * for each rename done.
968 * msdosfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
969 * struct componentname *a_fcnp, struct vnode *a_tdvp,
970 * struct vnode *a_tvp, struct componentname *a_tcnp)
973 msdosfs_rename(struct vop_old_rename_args *ap)
975 struct vnode *tdvp = ap->a_tdvp;
976 struct vnode *fvp = ap->a_fvp;
977 struct vnode *fdvp = ap->a_fdvp;
978 struct vnode *tvp = ap->a_tvp;
979 struct componentname *tcnp = ap->a_tcnp;
980 struct componentname *fcnp = ap->a_fcnp;
981 struct denode *ip, *xp, *dp, *zp;
982 u_char toname[11], oldname[11];
983 u_long from_diroffset, to_diroffset;
985 int doingdirectory = 0, newparent = 0;
989 struct denode *fddep; /* from file's parent directory */
990 struct msdosfsmount *pmp;
991 struct direntry *dotdotp;
994 fddep = VTODE(ap->a_fdvp);
997 pmp = VFSTOMSDOSFS(fdvp->v_mount);
1000 * Check for cross-device rename.
1002 if ((fvp->v_mount != tdvp->v_mount) ||
1003 (tvp && (fvp->v_mount != tvp->v_mount))) {
1018 * If source and dest are the same, do nothing.
1026 * fvp, fdvp are unlocked, tvp, tdvp are locked. Lock fvp and note
1027 * that we have to unlock it to use the abortit target.
1029 error = vn_lock(fvp, LK_EXCLUSIVE);
1036 * Be sure we are not renaming ".", "..", or an alias of ".". This
1037 * leads to a crippled directory tree. It's pretty tough to do a
1038 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1039 * doesn't work if the ".." entry is missing.
1041 if (ip->de_Attributes & ATTR_DIRECTORY) {
1043 * Avoid ".", "..", and aliases of "." for obvious reasons.
1045 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1047 (fcnp->cn_flags & CNP_ISDOTDOT) ||
1048 (tcnp->cn_flags & CNP_ISDOTDOT) ||
1049 (ip->de_flag & DE_RENAME)) {
1054 ip->de_flag |= DE_RENAME;
1059 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only
1060 * set if doingdirectory. We will get fvp unlocked in fairly
1061 * short order. dp and xp must be setup and fvp must be unlocked
1062 * for the out and bad targets to work properly.
1065 xp = tvp ? VTODE(tvp) : NULL;
1068 * Remember direntry place to use for destination
1070 to_diroffset = dp->de_fndoffset;
1071 to_count = dp->de_fndcnt;
1074 * If ".." must be changed (ie the directory gets a new
1075 * parent) then the source directory must not be in the
1076 * directory heirarchy above the target, as this would
1077 * orphan everything below the source directory. Also
1078 * the user must have write permission in the source so
1079 * as to be able to change "..". We must repeat the call
1080 * to namei, as the parent directory is unlocked by the
1081 * call to doscheckpath().
1083 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
1085 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1089 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1090 * DE_RENAME only set if doingdirectory.
1092 if (doingdirectory && newparent) {
1093 if (error) /* write access check above */
1100 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1101 * get an extra ref so we wind up with just an unlocked, ref'd
1102 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp
1103 * isn't locked so we can't use the out target).
1106 error = doscheckpath(ip, dp);
1107 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1113 * relookup no longer messes with the ref count. tdvp must
1114 * be unlocked on entry and on success will be locked on
1117 error = relookup(tdvp, &tvp, tcnp);
1119 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1127 * tvp and tdvp are now locked again.
1130 xp = tvp ? VTODE(tvp) : NULL;
1134 * tvp and tdvp are now locked again, the 'bad' target can be used
1135 * to clean them up again. Delete an existant target and clean
1136 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up.
1140 * Target must be empty if a directory and have no links
1141 * to it. Also, ensure source and target are compatible
1142 * (both directories, or both not directories).
1144 if (xp->de_Attributes & ATTR_DIRECTORY) {
1145 if (!dosdirempty(xp)) {
1149 if (!doingdirectory) {
1153 } else if (doingdirectory) {
1157 error = removede(dp, xp);
1166 * Convert the filename in tcnp into a dos filename. We copy this
1167 * into the denode and directory entry for the destination
1170 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1175 * Since from wasn't locked at various places above, we have to do
1176 * a relookup here. If the target and source are the same directory
1177 * we have to unlock the target directory in order to safely relookup
1178 * the source, because relookup expects its directory to be unlocked.
1180 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must
1181 * now take that into account.
1183 * The tdvp locking issues make this a real mess.
1185 fcnp->cn_flags &= ~CNP_MODMASK;
1186 fcnp->cn_flags |= CNP_LOCKPARENT;
1189 error = relookup(fdvp, &fvp, fcnp);
1190 if (error || fvp == NULL) {
1192 * From name has disappeared. Note: fdvp might == tdvp.
1194 * DE_RENAME is only set if doingdirectory.
1197 panic("rename: lost dir entry");
1198 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1211 * No error occured. tdvp, fdvp and fvp are all locked. If
1212 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned
1213 * up. ap->a_fvp is still refd.
1217 from_diroffset = zp->de_fndoffset;
1220 * Ensure that the directory entry still exists and has not
1221 * changed till now. If the source is a file the entry may
1222 * have been unlinked or renamed. In either case there is
1223 * no further work to be done. If the source is a directory
1224 * then it cannot have been rmdir'ed or renamed; this is
1225 * prohibited by the DE_RENAME flag.
1227 * DE_RENAME is only set if doingdirectory.
1231 panic("rename: lost dir entry");
1234 u_long new_dirclust;
1235 u_long new_diroffset;
1238 * First write a new entry in the destination
1239 * directory and mark the entry in the source directory
1240 * as deleted. Then move the denode to the correct hash
1241 * chain for its new location in the filesystem. And, if
1242 * we moved a directory, then update its .. entry to point
1243 * to the new parent directory.
1245 bcopy(ip->de_Name, oldname, 11);
1246 bcopy(toname, ip->de_Name, 11); /* update denode */
1247 dp->de_fndoffset = to_diroffset;
1248 dp->de_fndcnt = to_count;
1249 error = createde(ip, dp, (struct denode **)0, tcnp);
1251 bcopy(oldname, ip->de_Name, 11);
1255 zp->de_fndoffset = from_diroffset;
1256 error = removede(zp, ip);
1258 /* XXX should really panic here, fs is corrupt */
1261 if (!doingdirectory) {
1262 error = pcbmap(dp, de_cluster(pmp, to_diroffset),
1263 NULL, &new_dirclust, NULL);
1265 /* XXX should really panic here, fs is corrupt */
1268 if (new_dirclust == MSDOSFSROOT)
1269 new_diroffset = to_diroffset;
1271 new_diroffset = to_diroffset & pmp->pm_crbomask;
1272 msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1277 * If we moved a directory to a new parent directory, then we must
1278 * fixup the ".." entry in the moved directory.
1280 if (doingdirectory && newparent) {
1281 cn = ip->de_StartCluster;
1282 if (cn == MSDOSFSROOT) {
1283 /* this should never happen */
1284 panic("msdosfs_rename(): updating .. in root directory?");
1286 bn = xcntobn(pmp, cn);
1288 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
1290 /* XXX should really panic here, fs is corrupt */
1294 dotdotp = (struct direntry *)bp->b_data + 1;
1295 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1297 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1300 /* XXX should really panic here, fs is corrupt */
1306 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd
1310 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */
1321 * 'bad' target: xp governs tvp. tvp and tdvp arel ocked, fdvp and fvp
1322 * are not locked. ip points to fvp's inode which may have DE_RENAME
1331 * 'out' target: tvp and tdvp have already been cleaned up.
1334 ip->de_flag &= ~DE_RENAME;
1342 struct direntry dot;
1343 struct direntry dotdot;
1344 } dosdirtemplate = {
1345 { ". ", " ", /* the . entry */
1346 ATTR_DIRECTORY, /* file attribute */
1348 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1349 { 0, 0 }, /* access date */
1350 { 0, 0 }, /* high bits of start cluster */
1351 { 210, 4 }, { 210, 4 }, /* modify time & date */
1352 { 0, 0 }, /* startcluster */
1353 { 0, 0, 0, 0 } /* filesize */
1355 { ".. ", " ", /* the .. entry */
1356 ATTR_DIRECTORY, /* file attribute */
1358 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1359 { 0, 0 }, /* access date */
1360 { 0, 0 }, /* high bits of start cluster */
1361 { 210, 4 }, { 210, 4 }, /* modify time & date */
1362 { 0, 0 }, /* startcluster */
1363 { 0, 0, 0, 0 } /* filesize */
1368 * msdosfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1369 * struct componentname *a_cnp, struct vattr *a_vap)
1372 msdosfs_mkdir(struct vop_old_mkdir_args *ap)
1374 struct componentname *cnp = ap->a_cnp;
1376 struct denode *pdep = VTODE(ap->a_dvp);
1377 struct direntry *denp;
1378 struct msdosfsmount *pmp = pdep->de_pmp;
1380 u_long newcluster, pcl;
1383 struct denode ndirent;
1387 * If this is the root directory and there is no space left we
1388 * can't do anything. This is because the root directory can not
1391 if (pdep->de_StartCluster == MSDOSFSROOT
1392 && pdep->de_fndoffset >= pdep->de_FileSize) {
1398 * Allocate a cluster to hold the about to be created directory.
1400 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1404 bzero(&ndirent, sizeof(ndirent));
1405 ndirent.de_pmp = pmp;
1406 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1408 DETIMES(&ndirent, &ts, &ts, &ts);
1411 * Now fill the cluster with the "." and ".." entries. And write
1412 * the cluster to disk. This way it is there for the parent
1413 * directory to be pointing at if there were a crash.
1415 bn = xcntobn(pmp, newcluster);
1416 /* always succeeds */
1417 bp = getblk(pmp->pm_devvp, de_bntodoff(pmp, bn),
1418 pmp->pm_bpcluster, 0, 0);
1419 bzero(bp->b_data, pmp->pm_bpcluster);
1420 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1421 denp = (struct direntry *)bp->b_data;
1422 putushort(denp[0].deStartCluster, newcluster);
1423 putushort(denp[0].deCDate, ndirent.de_CDate);
1424 putushort(denp[0].deCTime, ndirent.de_CTime);
1425 denp[0].deCHundredth = ndirent.de_CHun;
1426 putushort(denp[0].deADate, ndirent.de_ADate);
1427 putushort(denp[0].deMDate, ndirent.de_MDate);
1428 putushort(denp[0].deMTime, ndirent.de_MTime);
1429 pcl = pdep->de_StartCluster;
1430 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1432 putushort(denp[1].deStartCluster, pcl);
1433 putushort(denp[1].deCDate, ndirent.de_CDate);
1434 putushort(denp[1].deCTime, ndirent.de_CTime);
1435 denp[1].deCHundredth = ndirent.de_CHun;
1436 putushort(denp[1].deADate, ndirent.de_ADate);
1437 putushort(denp[1].deMDate, ndirent.de_MDate);
1438 putushort(denp[1].deMTime, ndirent.de_MTime);
1440 putushort(denp[0].deHighClust, newcluster >> 16);
1441 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1449 * Now build up a directory entry pointing to the newly allocated
1450 * cluster. This will be written to an empty slot in the parent
1453 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1457 ndirent.de_Attributes = ATTR_DIRECTORY;
1458 ndirent.de_LowerCase = 0;
1459 ndirent.de_StartCluster = newcluster;
1460 ndirent.de_FileSize = 0;
1461 ndirent.de_dev = pdep->de_dev;
1462 ndirent.de_devvp = pdep->de_devvp;
1463 error = createde(&ndirent, pdep, &dep, cnp);
1466 *ap->a_vpp = DETOV(dep);
1470 clusterfree(pmp, newcluster, NULL);
1476 * msdosfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1477 * struct componentname *a_cnp)
1480 msdosfs_rmdir(struct vop_old_rmdir_args *ap)
1482 struct vnode *vp = ap->a_vp;
1483 struct vnode *dvp = ap->a_dvp;
1484 struct denode *ip, *dp;
1491 * Verify the directory is empty (and valid).
1492 * (Rmdir ".." won't be valid since
1493 * ".." will contain a reference to
1494 * the current directory and thus be
1498 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1503 * Delete the entry from the directory. For dos filesystems this
1504 * gets rid of the directory entry on disk, the in memory copy
1505 * still exists but the de_refcnt is <= 0. This prevents it from
1506 * being found by deget(). When the vput() on dep is done we give
1507 * up access and eventually msdosfs_reclaim() will be called which
1508 * will remove it from the denode cache.
1510 error = removede(dp, ip);
1514 * This is where we decrement the link count in the parent
1515 * directory. Since dos filesystems don't do this we just purge
1520 * Truncate the directory that is being deleted.
1522 error = detrunc(ip, (u_long)0, IO_SYNC);
1524 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1530 * DOS filesystems don't know what symlinks are.
1532 * msdosfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1533 * struct componentname *a_cnp, struct vattr *a_vap,
1537 msdosfs_symlink(struct vop_old_symlink_args *ap)
1539 return (EOPNOTSUPP);
1543 * msdosfs_readdir(struct vnode *a_vp, struct uio *a_uio,
1544 * struct ucred *a_cred, int *a_eofflag, int *a_ncookies,
1545 * u_long **a_cookies)
1548 msdosfs_readdir(struct vop_readdir_args *ap)
1561 struct msdosfsmount *pmp;
1562 struct direntry *dentp;
1563 struct uio *uio = ap->a_uio;
1564 u_long *cookies = NULL;
1571 char *d_name_storage = NULL;
1574 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
1577 dep = VTODE(ap->a_vp);
1580 #ifdef MSDOSFS_DEBUG
1581 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1582 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1586 * msdosfs_readdir() won't operate properly on regular files since
1587 * it does i/o only with the the filesystem vnode, and hence can
1588 * retrieve the wrong block from the buffer cache for a plain file.
1589 * So, fail attempts to readdir() on a plain file.
1591 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
1597 * If the user buffer is smaller than the size of one dos directory
1598 * entry or the file offset is not a multiple of the size of a
1599 * directory entry, then we fail the read.
1601 off = offset = uio->uio_offset;
1602 if (uio->uio_resid < sizeof(struct direntry) ||
1603 (offset & (sizeof(struct direntry) - 1))) {
1608 if (ap->a_ncookies) {
1609 ncookies = uio->uio_resid / 16 + 1;
1610 if (ncookies > 1024)
1612 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1614 *ap->a_cookies = cookies;
1615 *ap->a_ncookies = ncookies;
1618 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1621 * If they are reading from the root directory then, we simulate
1622 * the . and .. entries since these don't exist in the root
1623 * directory. We also set the offset bias to make up for having to
1624 * simulate these entries. By this I mean that at file offset 64 we
1625 * read the first entry in the root directory that lives on disk.
1627 if (dep->de_StartCluster == MSDOSFSROOT
1628 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1630 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1633 bias = 2 * sizeof(struct direntry);
1634 if (offset < bias) {
1635 for (n = (int)offset / sizeof(struct direntry); n < 2;
1638 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1646 } else /* if (n == 1) */{
1650 if (vop_write_dirent(&error, uio, d_ino, d_type,
1655 offset += sizeof(struct direntry);
1658 *cookies++ = offset;
1659 if (--ncookies <= 0)
1666 d_name_storage = kmalloc(WIN_MAXLEN, M_TEMP, M_WAITOK);
1669 while (uio->uio_resid > 0) {
1670 lbn = de_off2cn(pmp, offset - bias);
1671 on = (offset - bias) & pmp->pm_crbomask;
1672 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1673 diff = dep->de_FileSize - (offset - bias);
1677 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1680 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
1683 kfree(d_name_storage, M_TEMP);
1686 n = min(n, blsize - bp->b_resid);
1689 * Convert from dos directory entries to fs-independent
1690 * directory entries.
1692 for (dentp = (struct direntry *)(bp->b_data + on);
1693 (char *)dentp < bp->b_data + on + n;
1694 dentp++, offset += sizeof(struct direntry)) {
1696 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1697 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1699 d_name = d_name_storage;
1701 * If this is an unused entry, we can stop.
1703 if (dentp->deName[0] == SLOT_EMPTY) {
1708 * Skip deleted entries.
1710 if (dentp->deName[0] == SLOT_DELETED) {
1716 * Handle Win95 long directory entries
1718 if (dentp->deAttributes == ATTR_WIN95) {
1719 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1721 chksum = win2unixfn((struct winentry *)dentp,
1722 d_name, &d_namlen, chksum,
1723 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1729 * Skip volume labels
1731 if (dentp->deAttributes & ATTR_VOLUME) {
1736 * This computation of d_ino must match
1737 * the computation of va_fileid in
1740 if (dentp->deAttributes & ATTR_DIRECTORY) {
1741 d_ino = getushort(dentp->deStartCluster);
1743 d_ino |= getushort(dentp->deHighClust) << 16;
1744 /* if this is the root directory */
1745 if (d_ino != MSDOSFSROOT)
1746 d_ino = xcntobn(pmp, d_ino) * dirsperblk;
1747 else if (FAT32(pmp))
1748 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1754 d_ino = offset / sizeof(struct direntry);
1757 if (chksum != winChksum(dentp->deName)) {
1758 d_namlen = dos2unixfn(dentp->deName,
1760 dentp->deLowerCase |
1761 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1762 (LCASE_BASE | LCASE_EXT) : 0),
1763 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1765 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1769 if (vop_write_dirent(&error, uio, d_ino, d_type,
1770 d_namlen, d_name)) {
1780 *cookies++ = offset + sizeof(struct direntry);
1781 if (--ncookies <= 0) {
1786 off = offset + sizeof(struct direntry);
1791 if (d_name_storage != NULL)
1792 kfree(d_name_storage, M_TEMP);
1794 /* Subtract unused cookies */
1796 *ap->a_ncookies -= ncookies;
1798 uio->uio_offset = off;
1801 * Set the eofflag (NFS uses it)
1803 if (ap->a_eofflag) {
1804 if (dep->de_FileSize - (offset - bias) <= 0)
1810 vn_unlock(ap->a_vp);
1815 * vp - address of vnode file the file
1816 * bn - which cluster we are interested in mapping to a filesystem block number.
1817 * vpp - returns the vnode for the block special file holding the filesystem
1818 * containing the file of interest
1819 * bnp - address of where to return the filesystem relative block number
1821 * msdosfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
1822 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1825 msdosfs_bmap(struct vop_bmap_args *ap)
1827 struct denode *dep = VTODE(ap->a_vp);
1828 struct msdosfsmount *pmp = dep->de_pmp;
1833 if (ap->a_vpp != NULL)
1834 *ap->a_vpp = dep->de_devvp;
1835 if (ap->a_doffsetp == NULL)
1839 * Sequential clusters should be counted here.
1846 KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0);
1847 lbn = de_off2cn(pmp, ap->a_loffset);
1848 error = pcbmap(dep, lbn, &dbn, NULL, NULL);
1849 if (error || dbn == (daddr_t)-1) {
1850 *ap->a_doffsetp = NOOFFSET;
1852 *ap->a_doffsetp = de_bntodoff(pmp, dbn);
1858 * msdosfs_strategy(struct vnode *a_vp, struct bio *a_bio)
1861 msdosfs_strategy(struct vop_strategy_args *ap)
1863 struct bio *bio = ap->a_bio;
1865 struct buf *bp = bio->bio_buf;
1866 struct vnode *vp = ap->a_vp;
1867 struct denode *dep = VTODE(vp);
1868 struct msdosfsmount *pmp = dep->de_pmp;
1872 if (vp->v_type == VBLK || vp->v_type == VCHR)
1873 panic("msdosfs_strategy: spec");
1875 * If we don't already know the filesystem relative block number
1876 * then get it using pcbmap(). If pcbmap() returns the block
1877 * number as -1 then we've got a hole in the file. DOS filesystems
1878 * don't allow files with holes, so we shouldn't ever see this.
1880 nbio = push_bio(bio);
1881 if (nbio->bio_offset == NOOFFSET) {
1882 error = pcbmap(dep, de_off2cn(pmp, bio->bio_offset),
1883 &dblkno, NULL, NULL);
1885 bp->b_error = error;
1886 bp->b_flags |= B_ERROR;
1887 /* I/O was never started on nbio, must biodone(bio) */
1891 if (dblkno == (daddr_t)-1) {
1892 nbio->bio_offset = NOOFFSET;
1895 nbio->bio_offset = de_bntodoff(pmp, dblkno);
1898 if (nbio->bio_offset == NOOFFSET) {
1899 /* I/O was never started on nbio, must biodone(bio) */
1904 * Read/write the block from/to the disk that contains the desired
1907 vn_strategy(dep->de_devvp, nbio);
1912 * msdosfs_print(struct vnode *vp)
1915 msdosfs_print(struct vop_print_args *ap)
1917 struct denode *dep = VTODE(ap->a_vp);
1920 "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1921 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1922 printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1923 lockmgr_printinfo(&ap->a_vp->v_lock);
1929 * msdosfs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1932 msdosfs_pathconf(struct vop_pathconf_args *ap)
1934 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1936 switch (ap->a_name) {
1941 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1944 *ap->a_retval = PATH_MAX;
1946 case _PC_CHOWN_RESTRICTED:
1961 * XXX By default, wimp out... note that a_offset is ignored (and always
1965 msdosfs_getpages(struct vop_getpages_args *ap)
1967 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1974 * XXX By default, wimp out... note that a_offset is ignored (and always
1978 msdosfs_putpages(struct vop_putpages_args *ap)
1980 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1981 ap->a_sync, ap->a_rtvals);
1984 /* Global vfs data structures for msdosfs */
1985 struct vop_ops msdosfs_vnode_vops = {
1986 .vop_default = vop_defaultop,
1987 .vop_access = msdosfs_access,
1988 .vop_bmap = msdosfs_bmap,
1989 .vop_old_lookup = msdosfs_lookup,
1990 .vop_open = msdosfs_open,
1991 .vop_close = msdosfs_close,
1992 .vop_old_create = msdosfs_create,
1993 .vop_fsync = msdosfs_fsync,
1994 .vop_getattr = msdosfs_getattr,
1995 .vop_inactive = msdosfs_inactive,
1996 .vop_old_link = msdosfs_link,
1997 .vop_old_mkdir = msdosfs_mkdir,
1998 .vop_old_mknod = msdosfs_mknod,
1999 .vop_pathconf = msdosfs_pathconf,
2000 .vop_print = msdosfs_print,
2001 .vop_read = msdosfs_read,
2002 .vop_readdir = msdosfs_readdir,
2003 .vop_reclaim = msdosfs_reclaim,
2004 .vop_old_remove = msdosfs_remove,
2005 .vop_old_rename = msdosfs_rename,
2006 .vop_old_rmdir = msdosfs_rmdir,
2007 .vop_setattr = msdosfs_setattr,
2008 .vop_strategy = msdosfs_strategy,
2009 .vop_old_symlink = msdosfs_symlink,
2010 .vop_write = msdosfs_write,
2011 .vop_getpages = msdosfs_getpages,
2012 .vop_putpages = msdosfs_putpages