2 /* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/clock.h>
56 #include <sys/dirent.h>
58 #include <sys/lockf.h>
59 #include <sys/malloc.h>
60 #include <sys/mount.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
65 #include <sys/unistd.h>
66 #include <sys/vnode.h>
69 #include <vm/vm_extern.h>
71 #include <fs/msdosfs/bpb.h>
72 #include <fs/msdosfs/direntry.h>
73 #include <fs/msdosfs/denode.h>
74 #include <fs/msdosfs/fat.h>
75 #include <fs/msdosfs/msdosfsmount.h>
77 #define DOS_FILESIZE_MAX 0xffffffff
80 * Prototypes for MSDOSFS vnode operations
82 static vop_create_t msdosfs_create;
83 static vop_mknod_t msdosfs_mknod;
84 static vop_open_t msdosfs_open;
85 static vop_close_t msdosfs_close;
86 static vop_access_t msdosfs_access;
87 static vop_getattr_t msdosfs_getattr;
88 static vop_setattr_t msdosfs_setattr;
89 static vop_read_t msdosfs_read;
90 static vop_write_t msdosfs_write;
91 static vop_fsync_t msdosfs_fsync;
92 static vop_remove_t msdosfs_remove;
93 static vop_link_t msdosfs_link;
94 static vop_rename_t msdosfs_rename;
95 static vop_mkdir_t msdosfs_mkdir;
96 static vop_rmdir_t msdosfs_rmdir;
97 static vop_symlink_t msdosfs_symlink;
98 static vop_readdir_t msdosfs_readdir;
99 static vop_bmap_t msdosfs_bmap;
100 static vop_strategy_t msdosfs_strategy;
101 static vop_print_t msdosfs_print;
102 static vop_pathconf_t msdosfs_pathconf;
103 static vop_vptofh_t msdosfs_vptofh;
106 * Some general notes:
108 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
109 * read/written using the vnode for the filesystem. Blocks that represent
110 * the contents of a file are read/written using the vnode for the file
111 * (including directories when they are read/written as files). This
112 * presents problems for the dos filesystem because data that should be in
113 * an inode (if dos had them) resides in the directory itself. Since we
114 * must update directory entries without the benefit of having the vnode
115 * for the directory we must use the vnode for the filesystem. This means
116 * that when a directory is actually read/written (via read, write, or
117 * readdir, or seek) we must use the vnode for the filesystem instead of
118 * the vnode for the directory as would happen in ufs. This is to insure we
119 * retreive the correct block from the buffer cache since the hash value is
120 * based upon the vnode address and the desired block number.
124 * Create a regular file. On entry the directory to contain the file being
125 * created is locked. We must release before we return. We must also free
126 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
127 * only if the SAVESTART bit in cn_flags is clear on success.
131 struct vop_create_args /* {
133 struct vnode **a_vpp;
134 struct componentname *a_cnp;
138 struct componentname *cnp = ap->a_cnp;
139 struct denode ndirent;
141 struct denode *pdep = VTODE(ap->a_dvp);
146 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
150 * If this is the root directory and there is no space left we
151 * can't do anything. This is because the root directory can not
154 if (pdep->de_StartCluster == MSDOSFSROOT
155 && pdep->de_fndoffset >= pdep->de_FileSize) {
161 * Create a directory entry for the file, then call createde() to
162 * have it installed. NOTE: DOS files are always executable. We
163 * use the absence of the owner write bit to make the file
167 if ((cnp->cn_flags & HASBUF) == 0)
168 panic("msdosfs_create: no name");
170 bzero(&ndirent, sizeof(ndirent));
171 error = uniqdosname(pdep, cnp, ndirent.de_Name);
175 ndirent.de_Attributes = ATTR_ARCHIVE;
176 ndirent.de_LowerCase = 0;
177 ndirent.de_StartCluster = 0;
178 ndirent.de_FileSize = 0;
179 ndirent.de_pmp = pdep->de_pmp;
180 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
182 DETIMES(&ndirent, &ts, &ts, &ts);
183 error = createde(&ndirent, pdep, &dep, cnp);
186 *ap->a_vpp = DETOV(dep);
187 if ((cnp->cn_flags & MAKEENTRY) != 0)
188 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
197 struct vop_mknod_args /* {
199 struct vnode **a_vpp;
200 struct componentname *a_cnp;
210 struct vop_open_args /* {
213 struct ucred *a_cred;
218 struct denode *dep = VTODE(ap->a_vp);
219 vnode_create_vobject(ap->a_vp, dep->de_FileSize, ap->a_td);
225 struct vop_close_args /* {
228 struct ucred *a_cred;
232 struct vnode *vp = ap->a_vp;
233 struct denode *dep = VTODE(vp);
237 if (vp->v_usecount > 1) {
239 DETIMES(dep, &ts, &ts, &ts);
247 struct vop_access_args /* {
250 struct ucred *a_cred;
254 struct vnode *vp = ap->a_vp;
255 struct denode *dep = VTODE(ap->a_vp);
256 struct msdosfsmount *pmp = dep->de_pmp;
258 accmode_t accmode = ap->a_accmode;
260 file_mode = S_IRWXU|S_IRWXG|S_IRWXO;
261 file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
264 * Disallow writing to directories and regular files if the
265 * filesystem is read-only.
267 if (accmode & VWRITE) {
268 switch (vp->v_type) {
271 if (vp->v_mount->mnt_flag & MNT_RDONLY)
279 return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
280 ap->a_accmode, ap->a_cred, NULL));
285 struct vop_getattr_args /* {
288 struct ucred *a_cred;
291 struct denode *dep = VTODE(ap->a_vp);
292 struct msdosfsmount *pmp = dep->de_pmp;
293 struct vattr *vap = ap->a_vap;
296 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
300 DETIMES(dep, &ts, &ts, &ts);
301 vap->va_fsid = dev2udev(pmp->pm_dev);
303 * The following computation of the fileid must be the same as that
304 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
307 if (dep->de_Attributes & ATTR_DIRECTORY) {
308 fileid = (uint64_t)cntobn(pmp, dep->de_StartCluster) *
310 if (dep->de_StartCluster == MSDOSFSROOT)
313 fileid = (uint64_t)cntobn(pmp, dep->de_dirclust) *
315 if (dep->de_dirclust == MSDOSFSROOT)
316 fileid = (uint64_t)roottobn(pmp, 0) * dirsperblk;
317 fileid += (uoff_t)dep->de_diroffset / sizeof(struct direntry);
320 if (pmp->pm_flags & MSDOSFS_LARGEFS)
321 vap->va_fileid = msdosfs_fileno_map(pmp->pm_mountp, fileid);
323 vap->va_fileid = (long)fileid;
325 mode = S_IRWXU|S_IRWXG|S_IRWXO;
326 vap->va_mode = mode &
327 (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
328 vap->va_uid = pmp->pm_uid;
329 vap->va_gid = pmp->pm_gid;
331 vap->va_rdev = NODEV;
332 vap->va_size = dep->de_FileSize;
333 fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
334 vap->va_ctime = vap->va_mtime;
335 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
336 fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
337 fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
338 0, &vap->va_birthtime);
340 vap->va_atime = vap->va_mtime;
341 vap->va_birthtime.tv_sec = -1;
342 vap->va_birthtime.tv_nsec = 0;
345 if (dep->de_Attributes & ATTR_ARCHIVE)
346 vap->va_flags |= UF_ARCHIVE;
347 if (dep->de_Attributes & ATTR_HIDDEN)
348 vap->va_flags |= UF_HIDDEN;
349 if (dep->de_Attributes & ATTR_READONLY)
350 vap->va_flags |= UF_READONLY;
351 if (dep->de_Attributes & ATTR_SYSTEM)
352 vap->va_flags |= UF_SYSTEM;
354 vap->va_blocksize = pmp->pm_bpcluster;
356 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
357 vap->va_type = ap->a_vp->v_type;
358 vap->va_filerev = dep->de_modrev;
364 struct vop_setattr_args /* {
367 struct ucred *a_cred;
370 struct vnode *vp = ap->a_vp;
371 struct denode *dep = VTODE(ap->a_vp);
372 struct msdosfsmount *pmp = dep->de_pmp;
373 struct vattr *vap = ap->a_vap;
374 struct ucred *cred = ap->a_cred;
375 struct thread *td = curthread;
379 printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
380 ap->a_vp, vap, cred);
384 * Check for unsettable attributes.
386 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
387 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
388 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
389 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
391 printf("msdosfs_setattr(): returning EINVAL\n");
392 printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
393 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
394 printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
395 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
396 printf(" va_uid %x, va_gid %x\n",
397 vap->va_uid, vap->va_gid);
403 * We don't allow setting attributes on the root directory.
404 * The special case for the root directory is because before
405 * FAT32, the root directory didn't have an entry for itself
406 * (and was otherwise special). With FAT32, the root
407 * directory is not so special, but still doesn't have an
410 if (vp->v_vflag & VV_ROOT)
413 if (vap->va_flags != VNOVAL) {
414 if (vp->v_mount->mnt_flag & MNT_RDONLY)
416 if (cred->cr_uid != pmp->pm_uid) {
417 error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
422 * We are very inconsistent about handling unsupported
423 * attributes. We ignored the access time and the
424 * read and execute bits. We were strict for the other
427 if (vap->va_flags & ~(UF_ARCHIVE | UF_HIDDEN | UF_READONLY |
430 if (vap->va_flags & UF_ARCHIVE)
431 dep->de_Attributes |= ATTR_ARCHIVE;
433 dep->de_Attributes &= ~ATTR_ARCHIVE;
434 if (vap->va_flags & UF_HIDDEN)
435 dep->de_Attributes |= ATTR_HIDDEN;
437 dep->de_Attributes &= ~ATTR_HIDDEN;
438 /* We don't allow changing the readonly bit on directories. */
439 if (vp->v_type != VDIR) {
440 if (vap->va_flags & UF_READONLY)
441 dep->de_Attributes |= ATTR_READONLY;
443 dep->de_Attributes &= ~ATTR_READONLY;
445 if (vap->va_flags & UF_SYSTEM)
446 dep->de_Attributes |= ATTR_SYSTEM;
448 dep->de_Attributes &= ~ATTR_SYSTEM;
449 dep->de_flag |= DE_MODIFIED;
452 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
456 if (vp->v_mount->mnt_flag & MNT_RDONLY)
459 if (uid == (uid_t)VNOVAL)
462 if (gid == (gid_t)VNOVAL)
464 if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
465 (gid != pmp->pm_gid && !groupmember(gid, cred))) {
466 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
470 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
474 if (vap->va_size != VNOVAL) {
475 switch (vp->v_type) {
480 * Truncation is only supported for regular files,
481 * Disallow it if the filesystem is read-only.
483 if (vp->v_mount->mnt_flag & MNT_RDONLY)
488 * According to POSIX, the result is unspecified
489 * for file types other than regular files,
490 * directories and shared memory objects. We
491 * don't support any file types except regular
492 * files and directories in this file system, so
493 * this (default) case is unreachable and can do
494 * anything. Keep falling through to detrunc()
499 error = detrunc(dep, vap->va_size, 0, cred);
503 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
504 if (vp->v_mount->mnt_flag & MNT_RDONLY)
506 error = vn_utimes_perm(vp, vap, cred, td);
509 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
510 vap->va_atime.tv_sec != VNOVAL) {
511 dep->de_flag &= ~DE_ACCESS;
512 timespec2fattime(&vap->va_atime, 0,
513 &dep->de_ADate, NULL, NULL);
515 if (vap->va_mtime.tv_sec != VNOVAL) {
516 dep->de_flag &= ~DE_UPDATE;
517 timespec2fattime(&vap->va_mtime, 0,
518 &dep->de_MDate, &dep->de_MTime, NULL);
521 * We don't set the archive bit when modifying the time of
522 * a directory to emulate the Windows/DOS behavior.
524 if (vp->v_type != VDIR)
525 dep->de_Attributes |= ATTR_ARCHIVE;
526 dep->de_flag |= DE_MODIFIED;
529 * DOS files only have the ability to have their writability
530 * attribute set, so we use the owner write bit to set the readonly
533 if (vap->va_mode != (mode_t)VNOVAL) {
534 if (vp->v_mount->mnt_flag & MNT_RDONLY)
536 if (cred->cr_uid != pmp->pm_uid) {
537 error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
541 if (vp->v_type != VDIR) {
542 /* We ignore the read and execute bits. */
543 if (vap->va_mode & VWRITE)
544 dep->de_Attributes &= ~ATTR_READONLY;
546 dep->de_Attributes |= ATTR_READONLY;
547 dep->de_Attributes |= ATTR_ARCHIVE;
548 dep->de_flag |= DE_MODIFIED;
551 return (deupdat(dep, 0));
556 struct vop_read_args /* {
560 struct ucred *a_cred;
575 struct vnode *vp = ap->a_vp;
576 struct denode *dep = VTODE(vp);
577 struct msdosfsmount *pmp = dep->de_pmp;
578 struct uio *uio = ap->a_uio;
581 * If they didn't ask for any data, then we are done.
583 orig_resid = uio->uio_resid;
588 * The caller is supposed to ensure that
589 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
590 * We don't need to check for large offsets as in ffs because
591 * dep->de_FileSize <= DOS_FILESIZE_MAX < OFF_MAX, so large
592 * offsets cannot cause overflow even in theory.
595 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
597 isadir = dep->de_Attributes & ATTR_DIRECTORY;
599 if (uio->uio_offset >= dep->de_FileSize)
601 lbn = de_cluster(pmp, uio->uio_offset);
603 blsize = pmp->pm_bpcluster;
604 on = uio->uio_offset & pmp->pm_crbomask;
606 * If we are operating on a directory file then be sure to
607 * do i/o with the vnode for the filesystem instead of the
608 * vnode for the directory.
611 /* convert cluster # to block # */
612 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
613 if (error == E2BIG) {
618 error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
619 } else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
620 error = bread(vp, lbn, blsize, NOCRED, &bp);
621 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
622 error = cluster_read(vp, dep->de_FileSize, lbn, blsize,
623 NOCRED, on + uio->uio_resid, seqcount, 0, &bp);
624 } else if (seqcount > 1) {
626 error = breadn(vp, lbn,
627 blsize, &rablock, &rasize, 1, NOCRED, &bp);
629 error = bread(vp, lbn, blsize, NOCRED, &bp);
635 diff = pmp->pm_bpcluster - on;
636 n = diff > uio->uio_resid ? uio->uio_resid : diff;
637 diff = dep->de_FileSize - uio->uio_offset;
640 diff = blsize - bp->b_resid;
643 error = uiomove(bp->b_data + on, (int) n, uio);
645 } while (error == 0 && uio->uio_resid > 0 && n != 0);
646 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
647 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
648 dep->de_flag |= DE_ACCESS;
653 * Write data to a file or directory.
657 struct vop_write_args /* {
661 struct ucred *a_cred;
673 int ioflag = ap->a_ioflag;
674 struct uio *uio = ap->a_uio;
675 struct vnode *vp = ap->a_vp;
676 struct vnode *thisvp;
677 struct denode *dep = VTODE(vp);
678 struct msdosfsmount *pmp = dep->de_pmp;
679 struct ucred *cred = ap->a_cred;
682 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
683 vp, uio, ioflag, cred);
684 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
685 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
688 switch (vp->v_type) {
690 if (ioflag & IO_APPEND)
691 uio->uio_offset = dep->de_FileSize;
697 panic("msdosfs_write(): bad file type");
701 * This is needed (unlike in ffs_write()) because we extend the
702 * file outside of the loop but we don't want to extend the file
703 * for writes of 0 bytes.
705 if (uio->uio_resid == 0)
709 * The caller is supposed to ensure that
710 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
712 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
716 * If they've exceeded their filesize limit, tell them about it.
718 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
722 * If the offset we are starting the write at is beyond the end of
723 * the file, then they've done a seek. Unix filesystems allow
724 * files with holes in them, DOS doesn't so we must fill the hole
725 * with zeroed blocks.
727 if (uio->uio_offset > dep->de_FileSize) {
728 error = deextend(dep, uio->uio_offset, cred);
734 * Remember some values in case the write fails.
736 resid = uio->uio_resid;
737 osize = dep->de_FileSize;
740 * If we write beyond the end of the file, extend it to its ultimate
741 * size ahead of the time to hopefully get a contiguous area.
743 if (uio->uio_offset + resid > osize) {
744 count = de_clcount(pmp, uio->uio_offset + resid) -
745 de_clcount(pmp, osize);
746 error = extendfile(dep, count, NULL, NULL, 0);
747 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
749 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
751 lastcn = de_clcount(pmp, osize) - 1;
753 seqcount = ioflag >> IO_SEQSHIFT;
755 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
760 croffset = uio->uio_offset & pmp->pm_crbomask;
761 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
762 if (uio->uio_offset + n > dep->de_FileSize) {
763 dep->de_FileSize = uio->uio_offset + n;
764 /* The object size needs to be set before buffer is allocated */
765 vnode_pager_setsize(vp, dep->de_FileSize);
768 bn = de_cluster(pmp, uio->uio_offset);
769 if ((uio->uio_offset & pmp->pm_crbomask) == 0
770 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
771 > de_cluster(pmp, uio->uio_offset)
772 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
774 * If either the whole cluster gets written,
775 * or we write the cluster from its start beyond EOF,
776 * then no need to read data from disk.
778 bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0, 0);
781 * Do the bmap now, since pcbmap needs buffers
782 * for the fat table. (see msdosfs_strategy)
784 if (bp->b_blkno == bp->b_lblkno) {
785 error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
791 if (bp->b_blkno == -1) {
794 error = EIO; /* XXX */
799 * The block we need to write into exists, so read it in.
801 error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
809 * Should these vnode_pager_* functions be done on dir
814 * Copy the data from user space into the buf header.
816 error = uiomove(bp->b_data + croffset, n, uio);
822 /* Prepare for clustered writes in some else clauses. */
823 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
824 bp->b_flags |= B_CLUSTEROK;
827 * If IO_SYNC, then each buffer is written synchronously.
828 * Otherwise, if we have a severe page deficiency then
829 * write the buffer asynchronously. Otherwise, if on a
830 * cluster boundary then write the buffer asynchronously,
831 * combining it with contiguous clusters if permitted and
832 * possible, since we don't expect more writes into this
833 * buffer soon. Otherwise, do a delayed write because we
834 * expect more writes into this buffer soon.
836 if (ioflag & IO_SYNC)
838 else if (vm_page_count_severe() || buf_dirty_count_severe())
840 else if (n + croffset == pmp->pm_bpcluster) {
841 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
842 cluster_write(vp, bp, dep->de_FileSize,
848 dep->de_flag |= DE_UPDATE;
849 } while (error == 0 && uio->uio_resid > 0);
852 * If the write failed and they want us to, truncate the file back
853 * to the size it was before the write was attempted.
857 if (ioflag & IO_UNIT) {
858 detrunc(dep, osize, ioflag & IO_SYNC, NOCRED);
859 uio->uio_offset -= resid - uio->uio_resid;
860 uio->uio_resid = resid;
862 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED);
863 if (uio->uio_resid != resid)
866 } else if (ioflag & IO_SYNC)
867 error = deupdat(dep, 1);
872 * Flush the blocks of a file to disk.
876 struct vop_fsync_args /* {
878 struct ucred *a_cred;
889 * If the syncing request comes from fsync(2), sync the entire
890 * FAT and any other metadata that happens to be on devvp. We
891 * need this mainly for the FAT. We write the FAT sloppily, and
892 * syncing it all now is the best we can easily do to get all
893 * directory entries associated with the file (not just the file)
894 * fully synced. The other metadata includes critical metadata
895 * for all directory entries, but only in the MNT_ASYNC case. We
896 * will soon sync all metadata in the file's directory entry.
897 * Non-critical metadata for associated directory entries only
898 * gets synced accidentally, as in most file systems.
900 if (ap->a_waitfor == MNT_WAIT) {
901 devvp = VTODE(ap->a_vp)->de_pmp->pm_devvp;
902 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
903 allerror = VOP_FSYNC(devvp, MNT_WAIT, ap->a_td);
904 VOP_UNLOCK(devvp, 0);
908 error = deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT);
916 struct vop_remove_args /* {
919 struct componentname *a_cnp;
922 struct denode *dep = VTODE(ap->a_vp);
923 struct denode *ddep = VTODE(ap->a_dvp);
926 if (ap->a_vp->v_type == VDIR)
929 error = removede(ddep, dep);
931 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
937 * DOS filesystems don't know what links are.
941 struct vop_link_args /* {
942 struct vnode *a_tdvp;
944 struct componentname *a_cnp;
951 * Renames on files require moving the denode to a new hash queue since the
952 * denode's location is used to compute which hash queue to put the file
953 * in. Unless it is a rename in place. For example "mv a b".
955 * What follows is the basic algorithm:
958 * if (dest file exists) {
961 * if (dest and src in same directory) {
962 * rewrite name in existing directory slot
964 * write new entry in dest directory
965 * update offset and dirclust in denode
966 * move denode to new hash chain
967 * clear old directory entry
971 * if (dest directory exists) {
972 * if (dest is not empty) {
975 * remove dest directory
977 * if (dest and src in same directory) {
978 * rewrite name in existing entry
980 * be sure dest is not a child of src directory
981 * write entry in dest directory
982 * update "." and ".." in moved directory
983 * clear old directory entry for moved directory
988 * source's parent directory is unlocked
989 * source file or directory is unlocked
990 * destination's parent directory is locked
991 * destination file or directory is locked if it exists
994 * all denodes should be released
998 struct vop_rename_args /* {
999 struct vnode *a_fdvp;
1000 struct vnode *a_fvp;
1001 struct componentname *a_fcnp;
1002 struct vnode *a_tdvp;
1003 struct vnode *a_tvp;
1004 struct componentname *a_tcnp;
1007 struct vnode *tdvp = ap->a_tdvp;
1008 struct vnode *fvp = ap->a_fvp;
1009 struct vnode *fdvp = ap->a_fdvp;
1010 struct vnode *tvp = ap->a_tvp;
1011 struct componentname *tcnp = ap->a_tcnp;
1012 struct componentname *fcnp = ap->a_fcnp;
1013 struct denode *ip, *xp, *dp, *zp;
1014 u_char toname[12], oldname[11];
1015 u_long from_diroffset, to_diroffset;
1017 int doingdirectory = 0, newparent = 0;
1021 struct denode *fddep; /* from file's parent directory */
1022 struct msdosfsmount *pmp;
1023 struct direntry *dotdotp;
1026 fddep = VTODE(ap->a_fdvp);
1027 pmp = fddep->de_pmp;
1029 pmp = VFSTOMSDOSFS(fdvp->v_mount);
1032 if ((tcnp->cn_flags & HASBUF) == 0 ||
1033 (fcnp->cn_flags & HASBUF) == 0)
1034 panic("msdosfs_rename: no name");
1037 * Check for cross-device rename.
1039 if (fvp->v_mount != tdvp->v_mount ||
1040 (tvp && fvp->v_mount != tvp->v_mount)) {
1055 * If source and dest are the same, do nothing.
1062 error = vn_lock(fvp, LK_EXCLUSIVE);
1069 * Be sure we are not renaming ".", "..", or an alias of ".". This
1070 * leads to a crippled directory tree. It's pretty tough to do a
1071 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1072 * doesn't work if the ".." entry is missing.
1074 if (ip->de_Attributes & ATTR_DIRECTORY) {
1076 * Avoid ".", "..", and aliases of "." for obvious reasons.
1078 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1080 (fcnp->cn_flags & ISDOTDOT) ||
1081 (tcnp->cn_flags & ISDOTDOT) ||
1082 (ip->de_flag & DE_RENAME)) {
1087 ip->de_flag |= DE_RENAME;
1092 * When the target exists, both the directory
1093 * and target vnodes are returned locked.
1096 xp = tvp ? VTODE(tvp) : NULL;
1098 * Remember direntry place to use for destination
1100 to_diroffset = dp->de_fndoffset;
1101 to_count = dp->de_fndcnt;
1104 * If ".." must be changed (ie the directory gets a new
1105 * parent) then the source directory must not be in the
1106 * directory hierarchy above the target, as this would
1107 * orphan everything below the source directory. Also
1108 * the user must have write permission in the source so
1109 * as to be able to change "..". We must repeat the call
1110 * to namei, as the parent directory is unlocked by the
1111 * call to doscheckpath().
1113 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1115 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1117 if (doingdirectory && newparent) {
1118 if (error) /* write access check above */
1123 * doscheckpath() vput()'s dp,
1124 * so we have to do a relookup afterwards
1126 error = doscheckpath(ip, dp);
1129 if ((tcnp->cn_flags & SAVESTART) == 0)
1130 panic("msdosfs_rename: lost to startdir");
1131 error = relookup(tdvp, &tvp, tcnp);
1135 xp = tvp ? VTODE(tvp) : NULL;
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) {
1154 } else if (doingdirectory) {
1158 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,
1176 * have to do a relookup here.
1178 fcnp->cn_flags &= ~MODMASK;
1179 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1180 if ((fcnp->cn_flags & SAVESTART) == 0)
1181 panic("msdosfs_rename: lost from startdir");
1183 VOP_UNLOCK(tdvp, 0);
1184 if (relookup(fdvp, &fvp, fcnp) == 0)
1188 * From name has disappeared.
1191 panic("rename: lost dir entry");
1193 VOP_UNLOCK(tdvp, 0);
1200 from_diroffset = zp->de_fndoffset;
1203 * Ensure that the directory entry still exists and has not
1204 * changed till now. If the source is a file the entry may
1205 * have been unlinked or renamed. In either case there is
1206 * no further work to be done. If the source is a directory
1207 * then it cannot have been rmdir'ed or renamed; this is
1208 * prohibited by the DE_RENAME flag.
1212 panic("rename: lost dir entry");
1215 VOP_UNLOCK(fdvp, 0);
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);
1238 VOP_UNLOCK(fdvp, 0);
1243 * If ip is for a directory, then its name should always
1244 * be "." since it is for the directory entry in the
1245 * directory itself (msdosfs_lookup() always translates
1246 * to the "." entry so as to get a unique denode, except
1247 * for the root directory there are different
1248 * complications). However, we just corrupted its name
1249 * to pass the correct name to createde(). Undo this.
1251 if ((ip->de_Attributes & ATTR_DIRECTORY) != 0)
1252 bcopy(oldname, ip->de_Name, 11);
1254 zp->de_fndoffset = from_diroffset;
1255 error = removede(zp, ip);
1257 /* XXX should downgrade to ro here, fs is corrupt */
1259 VOP_UNLOCK(fdvp, 0);
1263 if (!doingdirectory) {
1264 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1265 &ip->de_dirclust, 0);
1267 /* XXX should downgrade to ro here, fs is corrupt */
1269 VOP_UNLOCK(fdvp, 0);
1273 if (ip->de_dirclust == MSDOSFSROOT)
1274 ip->de_diroffset = to_diroffset;
1276 ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1280 VOP_UNLOCK(fdvp, 0);
1284 * If we moved a directory to a new parent directory, then we must
1285 * fixup the ".." entry in the moved directory.
1287 if (doingdirectory && newparent) {
1288 cn = ip->de_StartCluster;
1289 if (cn == MSDOSFSROOT) {
1290 /* this should never happen */
1291 panic("msdosfs_rename(): updating .. in root directory?");
1293 bn = cntobn(pmp, cn);
1294 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1297 /* XXX should downgrade to ro here, fs is corrupt */
1302 dotdotp = (struct direntry *)bp->b_data + 1;
1303 pcl = dp->de_StartCluster;
1304 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1306 putushort(dotdotp->deStartCluster, pcl);
1308 putushort(dotdotp->deHighClust, pcl >> 16);
1309 if (DOINGASYNC(fvp))
1311 else if ((error = bwrite(bp)) != 0) {
1312 /* XXX should downgrade to ro here, fs is corrupt */
1319 * The msdosfs lookup is case insensitive. Several aliases may
1320 * be inserted for a single directory entry. As a consequnce,
1321 * name cache purge done by lookup for fvp when DELETE op for
1322 * namei is specified, might be not enough to expunge all
1323 * namecache entries that were installed for this direntry.
1332 ip->de_flag &= ~DE_RENAME;
1340 struct direntry dot;
1341 struct direntry dotdot;
1342 } dosdirtemplate = {
1343 { ". ", /* the . entry */
1344 ATTR_DIRECTORY, /* file attribute */
1346 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1347 { 0, 0 }, /* access date */
1348 { 0, 0 }, /* high bits of start cluster */
1349 { 210, 4 }, { 210, 4 }, /* modify time & date */
1350 { 0, 0 }, /* startcluster */
1351 { 0, 0, 0, 0 } /* filesize */
1353 { ".. ", /* the .. entry */
1354 ATTR_DIRECTORY, /* file attribute */
1356 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1357 { 0, 0 }, /* access date */
1358 { 0, 0 }, /* high bits of start cluster */
1359 { 210, 4 }, { 210, 4 }, /* modify time & date */
1360 { 0, 0 }, /* startcluster */
1361 { 0, 0, 0, 0 } /* filesize */
1367 struct vop_mkdir_args /* {
1368 struct vnode *a_dvp;
1369 struct vnode **a_vpp;
1370 struvt componentname *a_cnp;
1371 struct vattr *a_vap;
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 = cntobn(pmp, newcluster);
1416 /* always succeeds */
1417 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
1418 bzero(bp->b_data, pmp->pm_bpcluster);
1419 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1420 denp = (struct direntry *)bp->b_data;
1421 putushort(denp[0].deStartCluster, newcluster);
1422 putushort(denp[0].deCDate, ndirent.de_CDate);
1423 putushort(denp[0].deCTime, ndirent.de_CTime);
1424 denp[0].deCHundredth = ndirent.de_CHun;
1425 putushort(denp[0].deADate, ndirent.de_ADate);
1426 putushort(denp[0].deMDate, ndirent.de_MDate);
1427 putushort(denp[0].deMTime, ndirent.de_MTime);
1428 pcl = pdep->de_StartCluster;
1430 * Although the root directory has a non-magic starting cluster
1431 * number for FAT32, chkdsk and fsck_msdosfs still require
1432 * references to it in dotdot entries to be magic.
1434 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1436 putushort(denp[1].deStartCluster, pcl);
1437 putushort(denp[1].deCDate, ndirent.de_CDate);
1438 putushort(denp[1].deCTime, ndirent.de_CTime);
1439 denp[1].deCHundredth = ndirent.de_CHun;
1440 putushort(denp[1].deADate, ndirent.de_ADate);
1441 putushort(denp[1].deMDate, ndirent.de_MDate);
1442 putushort(denp[1].deMTime, ndirent.de_MTime);
1444 putushort(denp[0].deHighClust, newcluster >> 16);
1445 putushort(denp[1].deHighClust, pcl >> 16);
1448 if (DOINGASYNC(ap->a_dvp))
1450 else if ((error = bwrite(bp)) != 0)
1454 * Now build up a directory entry pointing to the newly allocated
1455 * cluster. This will be written to an empty slot in the parent
1459 if ((cnp->cn_flags & HASBUF) == 0)
1460 panic("msdosfs_mkdir: no name");
1462 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1466 ndirent.de_Attributes = ATTR_DIRECTORY;
1467 ndirent.de_LowerCase = 0;
1468 ndirent.de_StartCluster = newcluster;
1469 ndirent.de_FileSize = 0;
1470 error = createde(&ndirent, pdep, &dep, cnp);
1473 *ap->a_vpp = DETOV(dep);
1477 clusterfree(pmp, newcluster, NULL);
1484 struct vop_rmdir_args /* {
1485 struct vnode *a_dvp;
1487 struct componentname *a_cnp;
1490 struct vnode *vp = ap->a_vp;
1491 struct vnode *dvp = ap->a_dvp;
1492 struct componentname *cnp = ap->a_cnp;
1493 struct denode *ip, *dp;
1500 * Verify the directory is empty (and valid).
1501 * (Rmdir ".." won't be valid since
1502 * ".." will contain a reference to
1503 * the current directory and thus be
1507 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1512 * Delete the entry from the directory. For dos filesystems this
1513 * gets rid of the directory entry on disk, the in memory copy
1514 * still exists but the de_refcnt is <= 0. This prevents it from
1515 * being found by deget(). When the vput() on dep is done we give
1516 * up access and eventually msdosfs_reclaim() will be called which
1517 * will remove it from the denode cache.
1519 error = removede(dp, ip);
1523 * This is where we decrement the link count in the parent
1524 * directory. Since dos filesystems don't do this we just purge
1529 * Truncate the directory that is being deleted.
1531 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
1539 * DOS filesystems don't know what symlinks are.
1543 struct vop_symlink_args /* {
1544 struct vnode *a_dvp;
1545 struct vnode **a_vpp;
1546 struct componentname *a_cnp;
1547 struct vattr *a_vap;
1551 return (EOPNOTSUPP);
1556 struct vop_readdir_args /* {
1559 struct ucred *a_cred;
1577 struct denode *dep = VTODE(ap->a_vp);
1578 struct msdosfsmount *pmp = dep->de_pmp;
1579 struct direntry *dentp;
1580 struct dirent dirbuf;
1581 struct uio *uio = ap->a_uio;
1582 u_long *cookies = NULL;
1587 #ifdef MSDOSFS_DEBUG
1588 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1589 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1593 * msdosfs_readdir() won't operate properly on regular files since
1594 * it does i/o only with the filesystem vnode, and hence can
1595 * retrieve the wrong block from the buffer cache for a plain file.
1596 * So, fail attempts to readdir() on a plain file.
1598 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1602 * To be safe, initialize dirbuf
1604 bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1607 * If the user buffer is smaller than the size of one dos directory
1608 * entry or the file offset is not a multiple of the size of a
1609 * directory entry, then we fail the read.
1611 off = offset = uio->uio_offset;
1612 if (uio->uio_resid < sizeof(struct direntry) ||
1613 (offset & (sizeof(struct direntry) - 1)))
1616 if (ap->a_ncookies) {
1617 ncookies = uio->uio_resid / 16;
1618 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
1620 *ap->a_cookies = cookies;
1621 *ap->a_ncookies = ncookies;
1624 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1627 * If they are reading from the root directory then, we simulate
1628 * the . and .. entries since these don't exist in the root
1629 * directory. We also set the offset bias to make up for having to
1630 * simulate these entries. By this I mean that at file offset 64 we
1631 * read the first entry in the root directory that lives on disk.
1633 if (dep->de_StartCluster == MSDOSFSROOT
1634 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1636 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1639 bias = 2 * sizeof(struct direntry);
1640 if (offset < bias) {
1641 for (n = (int)offset / sizeof(struct direntry);
1644 fileno = (uint64_t)cntobn(pmp,
1649 if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1651 msdosfs_fileno_map(pmp->pm_mountp,
1655 dirbuf.d_fileno = (uint32_t)fileno;
1657 dirbuf.d_type = DT_DIR;
1660 dirbuf.d_namlen = 1;
1661 strcpy(dirbuf.d_name, ".");
1664 dirbuf.d_namlen = 2;
1665 strcpy(dirbuf.d_name, "..");
1668 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1669 if (uio->uio_resid < dirbuf.d_reclen)
1671 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1674 offset += sizeof(struct direntry);
1677 *cookies++ = offset;
1678 if (--ncookies <= 0)
1687 while (uio->uio_resid > 0) {
1688 lbn = de_cluster(pmp, offset - bias);
1689 on = (offset - bias) & pmp->pm_crbomask;
1690 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1691 diff = dep->de_FileSize - (offset - bias);
1695 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1698 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1703 n = min(n, blsize - bp->b_resid);
1710 * Convert from dos directory entries to fs-independent
1711 * directory entries.
1713 for (dentp = (struct direntry *)(bp->b_data + on);
1714 (char *)dentp < bp->b_data + on + n;
1715 dentp++, offset += sizeof(struct direntry)) {
1717 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1718 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1721 * If this is an unused entry, we can stop.
1723 if (dentp->deName[0] == SLOT_EMPTY) {
1728 * Skip deleted entries.
1730 if (dentp->deName[0] == SLOT_DELETED) {
1737 * Handle Win95 long directory entries
1739 if (dentp->deAttributes == ATTR_WIN95) {
1740 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1742 chksum = win2unixfn(&nb,
1743 (struct winentry *)dentp, chksum, pmp);
1748 * Skip volume labels
1750 if (dentp->deAttributes & ATTR_VOLUME) {
1756 * This computation of d_fileno must match
1757 * the computation of va_fileid in
1760 if (dentp->deAttributes & ATTR_DIRECTORY) {
1761 fileno = getushort(dentp->deStartCluster);
1763 fileno |= getushort(dentp->deHighClust) << 16;
1764 /* if this is the root directory */
1765 if (fileno == MSDOSFSROOT)
1767 fileno = (uint64_t)cntobn(pmp,
1773 fileno = (uint64_t)cntobn(pmp, fileno) *
1775 dirbuf.d_type = DT_DIR;
1777 fileno = (uoff_t)offset /
1778 sizeof(struct direntry);
1779 dirbuf.d_type = DT_REG;
1781 if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1783 msdosfs_fileno_map(pmp->pm_mountp, fileno);
1785 dirbuf.d_fileno = (uint32_t)fileno;
1787 if (chksum != winChksum(dentp->deName)) {
1788 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1789 (u_char *)dirbuf.d_name,
1790 dentp->deLowerCase |
1791 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1792 (LCASE_BASE | LCASE_EXT) : 0),
1796 mbnambuf_flush(&nb, &dirbuf);
1798 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1799 if (uio->uio_resid < dirbuf.d_reclen) {
1803 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1809 *cookies++ = offset + sizeof(struct direntry);
1810 if (--ncookies <= 0) {
1815 off = offset + sizeof(struct direntry);
1820 /* Subtract unused cookies */
1822 *ap->a_ncookies -= ncookies;
1824 uio->uio_offset = off;
1827 * Set the eofflag (NFS uses it)
1829 if (ap->a_eofflag) {
1830 if (dep->de_FileSize - (offset - bias) <= 0)
1839 * a_vp - pointer to the file's vnode
1840 * a_bn - logical block number within the file (cluster number for us)
1841 * a_bop - where to return the bufobj of the special file containing the fs
1842 * a_bnp - where to return the "physical" block number corresponding to a_bn
1843 * (relative to the special file; units are blocks of size DEV_BSIZE)
1844 * a_runp - where to return the "run past" a_bn. This is the count of logical
1845 * blocks whose physical blocks (together with a_bn's physical block)
1847 * a_runb - where to return the "run before" a_bn.
1851 struct vop_bmap_args /* {
1854 struct bufobj **a_bop;
1862 struct msdosfsmount *pmp;
1866 int bnpercn, error, maxio, maxrun, run;
1871 if (ap->a_bop != NULL)
1872 *ap->a_bop = &pmp->pm_devvp->v_bufobj;
1873 if (ap->a_bnp == NULL)
1875 if (ap->a_runp != NULL)
1877 if (ap->a_runb != NULL)
1882 error = pcbmap(dep, cn, ap->a_bnp, NULL, NULL);
1883 if (error != 0 || (ap->a_runp == NULL && ap->a_runb == NULL))
1887 maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1888 bnpercn = de_cn2bn(pmp, 1);
1889 if (ap->a_runp != NULL) {
1890 maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1891 for (run = 1; run <= maxrun; run++) {
1892 if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1893 runbn != *ap->a_bnp + run * bnpercn)
1896 *ap->a_runp = run - 1;
1898 if (ap->a_runb != NULL) {
1899 maxrun = ulmin(maxio - 1, cn);
1900 for (run = 1; run < maxrun; run++) {
1901 if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1902 runbn != *ap->a_bnp - run * bnpercn)
1905 *ap->a_runb = run - 1;
1911 msdosfs_strategy(ap)
1912 struct vop_strategy_args /* {
1917 struct buf *bp = ap->a_bp;
1918 struct denode *dep = VTODE(ap->a_vp);
1924 * If we don't already know the filesystem relative block number
1925 * then get it using pcbmap(). If pcbmap() returns the block
1926 * number as -1 then we've got a hole in the file. DOS filesystems
1927 * don't allow files with holes, so we shouldn't ever see this.
1929 if (bp->b_blkno == bp->b_lblkno) {
1930 error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1931 bp->b_blkno = blkno;
1933 bp->b_error = error;
1934 bp->b_ioflags |= BIO_ERROR;
1938 if ((long)bp->b_blkno == -1)
1941 if (bp->b_blkno == -1) {
1946 * Read/write the block from/to the disk that contains the desired
1949 bp->b_iooffset = dbtob(bp->b_blkno);
1950 bo = dep->de_pmp->pm_bo;
1951 BO_STRATEGY(bo, bp);
1957 struct vop_print_args /* {
1961 struct denode *dep = VTODE(ap->a_vp);
1963 printf("\tstartcluster %lu, dircluster %lu, diroffset %lu, ",
1964 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1965 printf("on dev %s\n", devtoname(dep->de_pmp->pm_dev));
1970 msdosfs_pathconf(ap)
1971 struct vop_pathconf_args /* {
1977 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1979 switch (ap->a_name) {
1984 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1987 *ap->a_retval = PATH_MAX;
1989 case _PC_CHOWN_RESTRICTED:
2003 struct vop_vptofh_args /* {
2009 struct defid *defhp;
2011 dep = VTODE(ap->a_vp);
2012 defhp = (struct defid *)ap->a_fhp;
2013 defhp->defid_len = sizeof(struct defid);
2014 defhp->defid_dirclust = dep->de_dirclust;
2015 defhp->defid_dirofs = dep->de_diroffset;
2016 /* defhp->defid_gen = dep->de_gen; */
2020 /* Global vfs data structures for msdosfs */
2021 struct vop_vector msdosfs_vnodeops = {
2022 .vop_default = &default_vnodeops,
2024 .vop_access = msdosfs_access,
2025 .vop_bmap = msdosfs_bmap,
2026 .vop_cachedlookup = msdosfs_lookup,
2027 .vop_open = msdosfs_open,
2028 .vop_close = msdosfs_close,
2029 .vop_create = msdosfs_create,
2030 .vop_fsync = msdosfs_fsync,
2031 .vop_getattr = msdosfs_getattr,
2032 .vop_inactive = msdosfs_inactive,
2033 .vop_link = msdosfs_link,
2034 .vop_lookup = vfs_cache_lookup,
2035 .vop_mkdir = msdosfs_mkdir,
2036 .vop_mknod = msdosfs_mknod,
2037 .vop_pathconf = msdosfs_pathconf,
2038 .vop_print = msdosfs_print,
2039 .vop_read = msdosfs_read,
2040 .vop_readdir = msdosfs_readdir,
2041 .vop_reclaim = msdosfs_reclaim,
2042 .vop_remove = msdosfs_remove,
2043 .vop_rename = msdosfs_rename,
2044 .vop_rmdir = msdosfs_rmdir,
2045 .vop_setattr = msdosfs_setattr,
2046 .vop_strategy = msdosfs_strategy,
2047 .vop_symlink = msdosfs_symlink,
2048 .vop_write = msdosfs_write,
2049 .vop_vptofh = msdosfs_vptofh,