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 = (ap->a_vap->va_mode & VWRITE) ?
176 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
177 ndirent.de_LowerCase = 0;
178 ndirent.de_StartCluster = 0;
179 ndirent.de_FileSize = 0;
180 ndirent.de_pmp = pdep->de_pmp;
181 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
183 DETIMES(&ndirent, &ts, &ts, &ts);
184 error = createde(&ndirent, pdep, &dep, cnp);
187 *ap->a_vpp = DETOV(dep);
196 struct vop_mknod_args /* {
198 struct vnode **a_vpp;
199 struct componentname *a_cnp;
209 struct vop_open_args /* {
212 struct ucred *a_cred;
217 struct denode *dep = VTODE(ap->a_vp);
218 vnode_create_vobject(ap->a_vp, dep->de_FileSize, ap->a_td);
224 struct vop_close_args /* {
227 struct ucred *a_cred;
231 struct vnode *vp = ap->a_vp;
232 struct denode *dep = VTODE(vp);
236 if (vp->v_usecount > 1) {
238 DETIMES(dep, &ts, &ts, &ts);
246 struct vop_access_args /* {
249 struct ucred *a_cred;
253 struct vnode *vp = ap->a_vp;
254 struct denode *dep = VTODE(ap->a_vp);
255 struct msdosfsmount *pmp = dep->de_pmp;
257 accmode_t accmode = ap->a_accmode;
259 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
260 ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
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 if ((dep->de_Attributes & ATTR_READONLY) == 0)
326 mode = S_IRWXU|S_IRWXG|S_IRWXO;
328 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
329 vap->va_mode = mode &
330 (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
331 vap->va_uid = pmp->pm_uid;
332 vap->va_gid = pmp->pm_gid;
334 vap->va_rdev = NODEV;
335 vap->va_size = dep->de_FileSize;
336 fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
337 vap->va_ctime = vap->va_mtime;
338 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
339 fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
340 fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
341 0, &vap->va_birthtime);
343 vap->va_atime = vap->va_mtime;
344 vap->va_birthtime.tv_sec = -1;
345 vap->va_birthtime.tv_nsec = 0;
348 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
349 vap->va_flags |= SF_ARCHIVED;
351 vap->va_blocksize = pmp->pm_bpcluster;
353 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
354 vap->va_type = ap->a_vp->v_type;
355 vap->va_filerev = dep->de_modrev;
361 struct vop_setattr_args /* {
364 struct ucred *a_cred;
367 struct vnode *vp = ap->a_vp;
368 struct denode *dep = VTODE(ap->a_vp);
369 struct msdosfsmount *pmp = dep->de_pmp;
370 struct vattr *vap = ap->a_vap;
371 struct ucred *cred = ap->a_cred;
372 struct thread *td = curthread;
376 printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
377 ap->a_vp, vap, cred);
381 * Check for unsettable attributes.
383 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
384 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
385 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
386 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
388 printf("msdosfs_setattr(): returning EINVAL\n");
389 printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
390 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
391 printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
392 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
393 printf(" va_uid %x, va_gid %x\n",
394 vap->va_uid, vap->va_gid);
398 if (vap->va_flags != VNOVAL) {
399 if (vp->v_mount->mnt_flag & MNT_RDONLY)
401 if (cred->cr_uid != pmp->pm_uid) {
402 error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
407 * We are very inconsistent about handling unsupported
408 * attributes. We ignored the access time and the
409 * read and execute bits. We were strict for the other
412 * Here we are strict, stricter than ufs in not allowing
413 * users to attempt to set SF_SETTABLE bits or anyone to
414 * set unsupported bits. However, we ignore attempts to
415 * set ATTR_ARCHIVE for directories `cp -pr' from a more
416 * sensible filesystem attempts it a lot.
418 if (vap->va_flags & SF_SETTABLE) {
419 error = priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0);
423 if (vap->va_flags & ~SF_ARCHIVED)
425 if (vap->va_flags & SF_ARCHIVED)
426 dep->de_Attributes &= ~ATTR_ARCHIVE;
427 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
428 dep->de_Attributes |= ATTR_ARCHIVE;
429 dep->de_flag |= DE_MODIFIED;
432 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
436 if (vp->v_mount->mnt_flag & MNT_RDONLY)
439 if (uid == (uid_t)VNOVAL)
442 if (gid == (gid_t)VNOVAL)
444 if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
445 (gid != pmp->pm_gid && !groupmember(gid, cred))) {
446 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
450 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
454 if (vap->va_size != VNOVAL) {
455 switch (vp->v_type) {
460 * Truncation is only supported for regular files,
461 * Disallow it if the filesystem is read-only.
463 if (vp->v_mount->mnt_flag & MNT_RDONLY)
468 * According to POSIX, the result is unspecified
469 * for file types other than regular files,
470 * directories and shared memory objects. We
471 * don't support any file types except regular
472 * files and directories in this file system, so
473 * this (default) case is unreachable and can do
474 * anything. Keep falling through to detrunc()
479 error = detrunc(dep, vap->va_size, 0, cred, td);
483 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
484 if (vp->v_mount->mnt_flag & MNT_RDONLY)
486 if (vap->va_vaflags & VA_UTIMES_NULL) {
487 error = VOP_ACCESS(vp, VADMIN, cred, td);
489 error = VOP_ACCESS(vp, VWRITE, cred, td);
491 error = VOP_ACCESS(vp, VADMIN, cred, td);
492 if (vp->v_type != VDIR) {
493 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
494 vap->va_atime.tv_sec != VNOVAL) {
495 dep->de_flag &= ~DE_ACCESS;
496 timespec2fattime(&vap->va_atime, 0,
497 &dep->de_ADate, NULL, NULL);
499 if (vap->va_mtime.tv_sec != VNOVAL) {
500 dep->de_flag &= ~DE_UPDATE;
501 timespec2fattime(&vap->va_mtime, 0,
502 &dep->de_MDate, &dep->de_MTime, NULL);
504 dep->de_Attributes |= ATTR_ARCHIVE;
505 dep->de_flag |= DE_MODIFIED;
509 * DOS files only have the ability to have their writability
510 * attribute set, so we use the owner write bit to set the readonly
513 if (vap->va_mode != (mode_t)VNOVAL) {
514 if (vp->v_mount->mnt_flag & MNT_RDONLY)
516 if (cred->cr_uid != pmp->pm_uid) {
517 error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
521 if (vp->v_type != VDIR) {
522 /* We ignore the read and execute bits. */
523 if (vap->va_mode & VWRITE)
524 dep->de_Attributes &= ~ATTR_READONLY;
526 dep->de_Attributes |= ATTR_READONLY;
527 dep->de_Attributes |= ATTR_ARCHIVE;
528 dep->de_flag |= DE_MODIFIED;
531 return (deupdat(dep, 0));
536 struct vop_read_args /* {
540 struct ucred *a_cred;
555 struct vnode *vp = ap->a_vp;
556 struct denode *dep = VTODE(vp);
557 struct msdosfsmount *pmp = dep->de_pmp;
558 struct uio *uio = ap->a_uio;
561 * If they didn't ask for any data, then we are done.
563 orig_resid = uio->uio_resid;
568 * The caller is supposed to ensure that
569 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
570 * We don't need to check for large offsets as in ffs because
571 * dep->de_FileSize <= DOS_FILESIZE_MAX < OFF_MAX, so large
572 * offsets cannot cause overflow even in theory.
575 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
577 isadir = dep->de_Attributes & ATTR_DIRECTORY;
579 if (uio->uio_offset >= dep->de_FileSize)
581 lbn = de_cluster(pmp, uio->uio_offset);
583 blsize = pmp->pm_bpcluster;
584 on = uio->uio_offset & pmp->pm_crbomask;
586 * If we are operating on a directory file then be sure to
587 * do i/o with the vnode for the filesystem instead of the
588 * vnode for the directory.
591 /* convert cluster # to block # */
592 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
593 if (error == E2BIG) {
598 error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
599 } else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
600 error = bread(vp, lbn, blsize, NOCRED, &bp);
601 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
602 error = cluster_read(vp, dep->de_FileSize, lbn, blsize,
603 NOCRED, on + uio->uio_resid, seqcount, &bp);
604 } else if (seqcount > 1) {
606 error = breadn(vp, lbn,
607 blsize, &rablock, &rasize, 1, NOCRED, &bp);
609 error = bread(vp, lbn, blsize, NOCRED, &bp);
615 diff = pmp->pm_bpcluster - on;
616 n = diff > uio->uio_resid ? uio->uio_resid : diff;
617 diff = dep->de_FileSize - uio->uio_offset;
620 diff = blsize - bp->b_resid;
623 error = uiomove(bp->b_data + on, (int) n, uio);
625 } while (error == 0 && uio->uio_resid > 0 && n != 0);
626 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
627 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
628 dep->de_flag |= DE_ACCESS;
633 * Write data to a file or directory.
637 struct vop_write_args /* {
641 struct ucred *a_cred;
653 int ioflag = ap->a_ioflag;
654 struct uio *uio = ap->a_uio;
655 struct thread *td = uio->uio_td;
656 struct vnode *vp = ap->a_vp;
657 struct vnode *thisvp;
658 struct denode *dep = VTODE(vp);
659 struct msdosfsmount *pmp = dep->de_pmp;
660 struct ucred *cred = ap->a_cred;
663 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
664 vp, uio, ioflag, cred);
665 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
666 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
669 switch (vp->v_type) {
671 if (ioflag & IO_APPEND)
672 uio->uio_offset = dep->de_FileSize;
678 panic("msdosfs_write(): bad file type");
682 * This is needed (unlike in ffs_write()) because we extend the
683 * file outside of the loop but we don't want to extend the file
684 * for writes of 0 bytes.
686 if (uio->uio_resid == 0)
690 * The caller is supposed to ensure that
691 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
693 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
697 * If they've exceeded their filesize limit, tell them about it.
699 if (vn_rlimit_fsize(vp, uio, td))
703 * If the offset we are starting the write at is beyond the end of
704 * the file, then they've done a seek. Unix filesystems allow
705 * files with holes in them, DOS doesn't so we must fill the hole
706 * with zeroed blocks.
708 if (uio->uio_offset > dep->de_FileSize) {
709 error = deextend(dep, uio->uio_offset, cred);
715 * Remember some values in case the write fails.
717 resid = uio->uio_resid;
718 osize = dep->de_FileSize;
721 * If we write beyond the end of the file, extend it to its ultimate
722 * size ahead of the time to hopefully get a contiguous area.
724 if (uio->uio_offset + resid > osize) {
725 count = de_clcount(pmp, uio->uio_offset + resid) -
726 de_clcount(pmp, osize);
727 error = extendfile(dep, count, NULL, NULL, 0);
728 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
730 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
732 lastcn = de_clcount(pmp, osize) - 1;
734 seqcount = ioflag >> IO_SEQSHIFT;
736 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
741 croffset = uio->uio_offset & pmp->pm_crbomask;
742 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
743 if (uio->uio_offset + n > dep->de_FileSize) {
744 dep->de_FileSize = uio->uio_offset + n;
745 /* The object size needs to be set before buffer is allocated */
746 vnode_pager_setsize(vp, dep->de_FileSize);
749 bn = de_cluster(pmp, uio->uio_offset);
750 if ((uio->uio_offset & pmp->pm_crbomask) == 0
751 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
752 > de_cluster(pmp, uio->uio_offset)
753 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
755 * If either the whole cluster gets written,
756 * or we write the cluster from its start beyond EOF,
757 * then no need to read data from disk.
759 bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0, 0);
762 * Do the bmap now, since pcbmap needs buffers
763 * for the fat table. (see msdosfs_strategy)
765 if (bp->b_blkno == bp->b_lblkno) {
766 error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
772 if (bp->b_blkno == -1) {
775 error = EIO; /* XXX */
780 * The block we need to write into exists, so read it in.
782 error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
790 * Should these vnode_pager_* functions be done on dir
795 * Copy the data from user space into the buf header.
797 error = uiomove(bp->b_data + croffset, n, uio);
803 /* Prepare for clustered writes in some else clauses. */
804 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
805 bp->b_flags |= B_CLUSTEROK;
808 * If IO_SYNC, then each buffer is written synchronously.
809 * Otherwise, if we have a severe page deficiency then
810 * write the buffer asynchronously. Otherwise, if on a
811 * cluster boundary then write the buffer asynchronously,
812 * combining it with contiguous clusters if permitted and
813 * possible, since we don't expect more writes into this
814 * buffer soon. Otherwise, do a delayed write because we
815 * expect more writes into this buffer soon.
817 if (ioflag & IO_SYNC)
819 else if (vm_page_count_severe() || buf_dirty_count_severe())
821 else if (n + croffset == pmp->pm_bpcluster) {
822 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
823 cluster_write(vp, bp, dep->de_FileSize,
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, NOCRED, NULL);
840 uio->uio_offset -= resid - uio->uio_resid;
841 uio->uio_resid = resid;
843 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED, NULL);
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.
860 struct vop_fsync_args /* {
862 struct ucred *a_cred;
869 return (deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT));
874 struct vop_remove_args /* {
877 struct componentname *a_cnp;
880 struct denode *dep = VTODE(ap->a_vp);
881 struct denode *ddep = VTODE(ap->a_dvp);
884 if (ap->a_vp->v_type == VDIR)
887 error = removede(ddep, dep);
889 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
895 * DOS filesystems don't know what links are.
899 struct vop_link_args /* {
900 struct vnode *a_tdvp;
902 struct componentname *a_cnp;
909 * Renames on files require moving the denode to a new hash queue since the
910 * denode's location is used to compute which hash queue to put the file
911 * in. Unless it is a rename in place. For example "mv a b".
913 * What follows is the basic algorithm:
916 * if (dest file exists) {
919 * if (dest and src in same directory) {
920 * rewrite name in existing directory slot
922 * write new entry in dest directory
923 * update offset and dirclust in denode
924 * move denode to new hash chain
925 * clear old directory entry
929 * if (dest directory exists) {
930 * if (dest is not empty) {
933 * remove dest directory
935 * if (dest and src in same directory) {
936 * rewrite name in existing entry
938 * be sure dest is not a child of src directory
939 * write entry in dest directory
940 * update "." and ".." in moved directory
941 * clear old directory entry for moved directory
946 * source's parent directory is unlocked
947 * source file or directory is unlocked
948 * destination's parent directory is locked
949 * destination file or directory is locked if it exists
952 * all denodes should be released
956 struct vop_rename_args /* {
957 struct vnode *a_fdvp;
959 struct componentname *a_fcnp;
960 struct vnode *a_tdvp;
962 struct componentname *a_tcnp;
965 struct vnode *tdvp = ap->a_tdvp;
966 struct vnode *fvp = ap->a_fvp;
967 struct vnode *fdvp = ap->a_fdvp;
968 struct vnode *tvp = ap->a_tvp;
969 struct componentname *tcnp = ap->a_tcnp;
970 struct componentname *fcnp = ap->a_fcnp;
971 struct denode *ip, *xp, *dp, *zp;
972 u_char toname[12], oldname[11];
973 u_long from_diroffset, to_diroffset;
975 int doingdirectory = 0, newparent = 0;
979 struct denode *fddep; /* from file's parent directory */
980 struct msdosfsmount *pmp;
981 struct direntry *dotdotp;
984 fddep = VTODE(ap->a_fdvp);
987 pmp = VFSTOMSDOSFS(fdvp->v_mount);
990 if ((tcnp->cn_flags & HASBUF) == 0 ||
991 (fcnp->cn_flags & HASBUF) == 0)
992 panic("msdosfs_rename: no name");
995 * Check for cross-device rename.
997 if (fvp->v_mount != tdvp->v_mount ||
998 (tvp && fvp->v_mount != tvp->v_mount)) {
1013 * If source and dest are the same, do nothing.
1020 error = vn_lock(fvp, LK_EXCLUSIVE);
1027 * Be sure we are not renaming ".", "..", or an alias of ".". This
1028 * leads to a crippled directory tree. It's pretty tough to do a
1029 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1030 * doesn't work if the ".." entry is missing.
1032 if (ip->de_Attributes & ATTR_DIRECTORY) {
1034 * Avoid ".", "..", and aliases of "." for obvious reasons.
1036 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1038 (fcnp->cn_flags & ISDOTDOT) ||
1039 (tcnp->cn_flags & ISDOTDOT) ||
1040 (ip->de_flag & DE_RENAME)) {
1045 ip->de_flag |= DE_RENAME;
1050 * When the target exists, both the directory
1051 * and target vnodes are returned locked.
1054 xp = tvp ? VTODE(tvp) : NULL;
1056 * Remember direntry place to use for destination
1058 to_diroffset = dp->de_fndoffset;
1059 to_count = dp->de_fndcnt;
1062 * If ".." must be changed (ie the directory gets a new
1063 * parent) then the source directory must not be in the
1064 * directory hierarchy above the target, as this would
1065 * orphan everything below the source directory. Also
1066 * the user must have write permission in the source so
1067 * as to be able to change "..". We must repeat the call
1068 * to namei, as the parent directory is unlocked by the
1069 * call to doscheckpath().
1071 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1073 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1075 if (doingdirectory && newparent) {
1076 if (error) /* write access check above */
1081 * doscheckpath() vput()'s dp,
1082 * so we have to do a relookup afterwards
1084 error = doscheckpath(ip, dp);
1087 if ((tcnp->cn_flags & SAVESTART) == 0)
1088 panic("msdosfs_rename: lost to startdir");
1089 error = relookup(tdvp, &tvp, tcnp);
1093 xp = tvp ? VTODE(tvp) : NULL;
1098 * Target must be empty if a directory and have no links
1099 * to it. Also, ensure source and target are compatible
1100 * (both directories, or both not directories).
1102 if (xp->de_Attributes & ATTR_DIRECTORY) {
1103 if (!dosdirempty(xp)) {
1107 if (!doingdirectory) {
1112 } else if (doingdirectory) {
1116 error = removede(dp, xp);
1124 * Convert the filename in tcnp into a dos filename. We copy this
1125 * into the denode and directory entry for the destination
1128 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1133 * Since from wasn't locked at various places above,
1134 * have to do a relookup here.
1136 fcnp->cn_flags &= ~MODMASK;
1137 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1138 if ((fcnp->cn_flags & SAVESTART) == 0)
1139 panic("msdosfs_rename: lost from startdir");
1141 VOP_UNLOCK(tdvp, 0);
1142 if (relookup(fdvp, &fvp, fcnp) == 0)
1146 * From name has disappeared.
1149 panic("rename: lost dir entry");
1151 VOP_UNLOCK(tdvp, 0);
1158 from_diroffset = zp->de_fndoffset;
1161 * Ensure that the directory entry still exists and has not
1162 * changed till now. If the source is a file the entry may
1163 * have been unlinked or renamed. In either case there is
1164 * no further work to be done. If the source is a directory
1165 * then it cannot have been rmdir'ed or renamed; this is
1166 * prohibited by the DE_RENAME flag.
1170 panic("rename: lost dir entry");
1173 VOP_UNLOCK(fdvp, 0);
1181 * First write a new entry in the destination
1182 * directory and mark the entry in the source directory
1183 * as deleted. Then move the denode to the correct hash
1184 * chain for its new location in the filesystem. And, if
1185 * we moved a directory, then update its .. entry to point
1186 * to the new parent directory.
1188 bcopy(ip->de_Name, oldname, 11);
1189 bcopy(toname, ip->de_Name, 11); /* update denode */
1190 dp->de_fndoffset = to_diroffset;
1191 dp->de_fndcnt = to_count;
1192 error = createde(ip, dp, (struct denode **)0, tcnp);
1194 bcopy(oldname, ip->de_Name, 11);
1196 VOP_UNLOCK(fdvp, 0);
1201 zp->de_fndoffset = from_diroffset;
1202 error = removede(zp, ip);
1204 /* XXX should downgrade to ro here, fs is corrupt */
1206 VOP_UNLOCK(fdvp, 0);
1210 if (!doingdirectory) {
1211 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1212 &ip->de_dirclust, 0);
1214 /* XXX should downgrade to ro here, fs is corrupt */
1216 VOP_UNLOCK(fdvp, 0);
1220 if (ip->de_dirclust == MSDOSFSROOT)
1221 ip->de_diroffset = to_diroffset;
1223 ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1227 VOP_UNLOCK(fdvp, 0);
1231 * If we moved a directory to a new parent directory, then we must
1232 * fixup the ".." entry in the moved directory.
1234 if (doingdirectory && newparent) {
1235 cn = ip->de_StartCluster;
1236 if (cn == MSDOSFSROOT) {
1237 /* this should never happen */
1238 panic("msdosfs_rename(): updating .. in root directory?");
1240 bn = cntobn(pmp, cn);
1241 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1244 /* XXX should downgrade to ro here, fs is corrupt */
1249 dotdotp = (struct direntry *)bp->b_data + 1;
1250 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1252 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1253 if (fvp->v_mount->mnt_flag & MNT_ASYNC)
1255 else if ((error = bwrite(bp)) != 0) {
1256 /* XXX should downgrade to ro here, fs is corrupt */
1263 * The msdosfs lookup is case insensitive. Several aliases may
1264 * be inserted for a single directory entry. As a consequnce,
1265 * name cache purge done by lookup for fvp when DELETE op for
1266 * namei is specified, might be not enough to expunge all
1267 * namecache entries that were installed for this direntry.
1276 ip->de_flag &= ~DE_RENAME;
1284 struct direntry dot;
1285 struct direntry dotdot;
1286 } dosdirtemplate = {
1287 { ". ", /* the . entry */
1288 ATTR_DIRECTORY, /* file attribute */
1290 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1291 { 0, 0 }, /* access date */
1292 { 0, 0 }, /* high bits of start cluster */
1293 { 210, 4 }, { 210, 4 }, /* modify time & date */
1294 { 0, 0 }, /* startcluster */
1295 { 0, 0, 0, 0 } /* filesize */
1297 { ".. ", /* the .. entry */
1298 ATTR_DIRECTORY, /* file attribute */
1300 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1301 { 0, 0 }, /* access date */
1302 { 0, 0 }, /* high bits of start cluster */
1303 { 210, 4 }, { 210, 4 }, /* modify time & date */
1304 { 0, 0 }, /* startcluster */
1305 { 0, 0, 0, 0 } /* filesize */
1311 struct vop_mkdir_args /* {
1312 struct vnode *a_dvp;
1313 struct vnode **a_vpp;
1314 struvt componentname *a_cnp;
1315 struct vattr *a_vap;
1318 struct componentname *cnp = ap->a_cnp;
1320 struct denode *pdep = VTODE(ap->a_dvp);
1321 struct direntry *denp;
1322 struct msdosfsmount *pmp = pdep->de_pmp;
1324 u_long newcluster, pcl;
1327 struct denode ndirent;
1331 * If this is the root directory and there is no space left we
1332 * can't do anything. This is because the root directory can not
1335 if (pdep->de_StartCluster == MSDOSFSROOT
1336 && pdep->de_fndoffset >= pdep->de_FileSize) {
1342 * Allocate a cluster to hold the about to be created directory.
1344 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1348 bzero(&ndirent, sizeof(ndirent));
1349 ndirent.de_pmp = pmp;
1350 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1352 DETIMES(&ndirent, &ts, &ts, &ts);
1355 * Now fill the cluster with the "." and ".." entries. And write
1356 * the cluster to disk. This way it is there for the parent
1357 * directory to be pointing at if there were a crash.
1359 bn = cntobn(pmp, newcluster);
1360 /* always succeeds */
1361 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
1362 bzero(bp->b_data, pmp->pm_bpcluster);
1363 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1364 denp = (struct direntry *)bp->b_data;
1365 putushort(denp[0].deStartCluster, newcluster);
1366 putushort(denp[0].deCDate, ndirent.de_CDate);
1367 putushort(denp[0].deCTime, ndirent.de_CTime);
1368 denp[0].deCHundredth = ndirent.de_CHun;
1369 putushort(denp[0].deADate, ndirent.de_ADate);
1370 putushort(denp[0].deMDate, ndirent.de_MDate);
1371 putushort(denp[0].deMTime, ndirent.de_MTime);
1372 pcl = pdep->de_StartCluster;
1373 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1375 putushort(denp[1].deStartCluster, pcl);
1376 putushort(denp[1].deCDate, ndirent.de_CDate);
1377 putushort(denp[1].deCTime, ndirent.de_CTime);
1378 denp[1].deCHundredth = ndirent.de_CHun;
1379 putushort(denp[1].deADate, ndirent.de_ADate);
1380 putushort(denp[1].deMDate, ndirent.de_MDate);
1381 putushort(denp[1].deMTime, ndirent.de_MTime);
1383 putushort(denp[0].deHighClust, newcluster >> 16);
1384 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1387 if (ap->a_dvp->v_mount->mnt_flag & MNT_ASYNC)
1389 else if ((error = bwrite(bp)) != 0)
1393 * Now build up a directory entry pointing to the newly allocated
1394 * cluster. This will be written to an empty slot in the parent
1398 if ((cnp->cn_flags & HASBUF) == 0)
1399 panic("msdosfs_mkdir: no name");
1401 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1405 ndirent.de_Attributes = ATTR_DIRECTORY;
1406 ndirent.de_LowerCase = 0;
1407 ndirent.de_StartCluster = newcluster;
1408 ndirent.de_FileSize = 0;
1409 error = createde(&ndirent, pdep, &dep, cnp);
1412 *ap->a_vpp = DETOV(dep);
1416 clusterfree(pmp, newcluster, NULL);
1423 struct vop_rmdir_args /* {
1424 struct vnode *a_dvp;
1426 struct componentname *a_cnp;
1429 struct vnode *vp = ap->a_vp;
1430 struct vnode *dvp = ap->a_dvp;
1431 struct componentname *cnp = ap->a_cnp;
1432 struct denode *ip, *dp;
1433 struct thread *td = cnp->cn_thread;
1440 * Verify the directory is empty (and valid).
1441 * (Rmdir ".." won't be valid since
1442 * ".." will contain a reference to
1443 * the current directory and thus be
1447 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1452 * Delete the entry from the directory. For dos filesystems this
1453 * gets rid of the directory entry on disk, the in memory copy
1454 * still exists but the de_refcnt is <= 0. This prevents it from
1455 * being found by deget(). When the vput() on dep is done we give
1456 * up access and eventually msdosfs_reclaim() will be called which
1457 * will remove it from the denode cache.
1459 error = removede(dp, ip);
1463 * This is where we decrement the link count in the parent
1464 * directory. Since dos filesystems don't do this we just purge
1469 * Truncate the directory that is being deleted.
1471 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred, td);
1479 * DOS filesystems don't know what symlinks are.
1483 struct vop_symlink_args /* {
1484 struct vnode *a_dvp;
1485 struct vnode **a_vpp;
1486 struct componentname *a_cnp;
1487 struct vattr *a_vap;
1491 return (EOPNOTSUPP);
1496 struct vop_readdir_args /* {
1499 struct ucred *a_cred;
1517 struct denode *dep = VTODE(ap->a_vp);
1518 struct msdosfsmount *pmp = dep->de_pmp;
1519 struct direntry *dentp;
1520 struct dirent dirbuf;
1521 struct uio *uio = ap->a_uio;
1522 u_long *cookies = NULL;
1527 #ifdef MSDOSFS_DEBUG
1528 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1529 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1533 * msdosfs_readdir() won't operate properly on regular files since
1534 * it does i/o only with the filesystem vnode, and hence can
1535 * retrieve the wrong block from the buffer cache for a plain file.
1536 * So, fail attempts to readdir() on a plain file.
1538 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1542 * To be safe, initialize dirbuf
1544 bzero(dirbuf.d_name, sizeof(dirbuf.d_name));
1547 * If the user buffer is smaller than the size of one dos directory
1548 * entry or the file offset is not a multiple of the size of a
1549 * directory entry, then we fail the read.
1551 off = offset = uio->uio_offset;
1552 if (uio->uio_resid < sizeof(struct direntry) ||
1553 (offset & (sizeof(struct direntry) - 1)))
1556 if (ap->a_ncookies) {
1557 ncookies = uio->uio_resid / 16;
1558 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
1560 *ap->a_cookies = cookies;
1561 *ap->a_ncookies = ncookies;
1564 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1567 * If they are reading from the root directory then, we simulate
1568 * the . and .. entries since these don't exist in the root
1569 * directory. We also set the offset bias to make up for having to
1570 * simulate these entries. By this I mean that at file offset 64 we
1571 * read the first entry in the root directory that lives on disk.
1573 if (dep->de_StartCluster == MSDOSFSROOT
1574 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1576 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1579 bias = 2 * sizeof(struct direntry);
1580 if (offset < bias) {
1581 for (n = (int)offset / sizeof(struct direntry);
1584 fileno = (uint64_t)cntobn(pmp,
1589 if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1591 msdosfs_fileno_map(pmp->pm_mountp,
1595 dirbuf.d_fileno = (uint32_t)fileno;
1597 dirbuf.d_type = DT_DIR;
1600 dirbuf.d_namlen = 1;
1601 strcpy(dirbuf.d_name, ".");
1604 dirbuf.d_namlen = 2;
1605 strcpy(dirbuf.d_name, "..");
1608 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1609 if (uio->uio_resid < dirbuf.d_reclen)
1611 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1614 offset += sizeof(struct direntry);
1617 *cookies++ = offset;
1618 if (--ncookies <= 0)
1627 while (uio->uio_resid > 0) {
1628 lbn = de_cluster(pmp, offset - bias);
1629 on = (offset - bias) & pmp->pm_crbomask;
1630 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1631 diff = dep->de_FileSize - (offset - bias);
1635 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1638 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1643 n = min(n, blsize - bp->b_resid);
1650 * Convert from dos directory entries to fs-independent
1651 * directory entries.
1653 for (dentp = (struct direntry *)(bp->b_data + on);
1654 (char *)dentp < bp->b_data + on + n;
1655 dentp++, offset += sizeof(struct direntry)) {
1657 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1658 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1661 * If this is an unused entry, we can stop.
1663 if (dentp->deName[0] == SLOT_EMPTY) {
1668 * Skip deleted entries.
1670 if (dentp->deName[0] == SLOT_DELETED) {
1677 * Handle Win95 long directory entries
1679 if (dentp->deAttributes == ATTR_WIN95) {
1680 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1682 chksum = win2unixfn(&nb,
1683 (struct winentry *)dentp, chksum, pmp);
1688 * Skip volume labels
1690 if (dentp->deAttributes & ATTR_VOLUME) {
1696 * This computation of d_fileno must match
1697 * the computation of va_fileid in
1700 if (dentp->deAttributes & ATTR_DIRECTORY) {
1701 fileno = getushort(dentp->deStartCluster);
1703 fileno |= getushort(dentp->deHighClust) << 16;
1704 /* if this is the root directory */
1705 if (fileno == MSDOSFSROOT)
1707 fileno = (uint64_t)cntobn(pmp,
1713 fileno = (uint64_t)cntobn(pmp, fileno) *
1715 dirbuf.d_type = DT_DIR;
1717 fileno = (uoff_t)offset /
1718 sizeof(struct direntry);
1719 dirbuf.d_type = DT_REG;
1721 if (pmp->pm_flags & MSDOSFS_LARGEFS) {
1723 msdosfs_fileno_map(pmp->pm_mountp, fileno);
1725 dirbuf.d_fileno = (uint32_t)fileno;
1727 if (chksum != winChksum(dentp->deName)) {
1728 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1729 (u_char *)dirbuf.d_name,
1730 dentp->deLowerCase |
1731 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1732 (LCASE_BASE | LCASE_EXT) : 0),
1736 mbnambuf_flush(&nb, &dirbuf);
1738 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1739 if (uio->uio_resid < dirbuf.d_reclen) {
1743 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1749 *cookies++ = offset + sizeof(struct direntry);
1750 if (--ncookies <= 0) {
1755 off = offset + sizeof(struct direntry);
1760 /* Subtract unused cookies */
1762 *ap->a_ncookies -= ncookies;
1764 uio->uio_offset = off;
1767 * Set the eofflag (NFS uses it)
1769 if (ap->a_eofflag) {
1770 if (dep->de_FileSize - (offset - bias) <= 0)
1779 * a_vp - pointer to the file's vnode
1780 * a_bn - logical block number within the file (cluster number for us)
1781 * a_bop - where to return the bufobj of the special file containing the fs
1782 * a_bnp - where to return the "physical" block number corresponding to a_bn
1783 * (relative to the special file; units are blocks of size DEV_BSIZE)
1784 * a_runp - where to return the "run past" a_bn. This is the count of logical
1785 * blocks whose physical blocks (together with a_bn's physical block)
1787 * a_runb - where to return the "run before" a_bn.
1791 struct vop_bmap_args /* {
1794 struct bufobj **a_bop;
1802 struct msdosfsmount *pmp;
1806 int bnpercn, error, maxio, maxrun, run;
1811 if (ap->a_bop != NULL)
1812 *ap->a_bop = &pmp->pm_devvp->v_bufobj;
1813 if (ap->a_bnp == NULL)
1815 if (ap->a_runp != NULL)
1817 if (ap->a_runb != NULL)
1822 error = pcbmap(dep, cn, ap->a_bnp, NULL, NULL);
1823 if (error != 0 || (ap->a_runp == NULL && ap->a_runb == NULL))
1827 maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1828 bnpercn = de_cn2bn(pmp, 1);
1829 if (ap->a_runp != NULL) {
1830 maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1831 for (run = 1; run <= maxrun; run++) {
1832 if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1833 runbn != *ap->a_bnp + run * bnpercn)
1836 *ap->a_runp = run - 1;
1838 if (ap->a_runb != NULL) {
1839 maxrun = ulmin(maxio - 1, cn);
1840 for (run = 1; run < maxrun; run++) {
1841 if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1842 runbn != *ap->a_bnp - run * bnpercn)
1845 *ap->a_runb = run - 1;
1851 msdosfs_strategy(ap)
1852 struct vop_strategy_args /* {
1857 struct buf *bp = ap->a_bp;
1858 struct denode *dep = VTODE(ap->a_vp);
1864 * If we don't already know the filesystem relative block number
1865 * then get it using pcbmap(). If pcbmap() returns the block
1866 * number as -1 then we've got a hole in the file. DOS filesystems
1867 * don't allow files with holes, so we shouldn't ever see this.
1869 if (bp->b_blkno == bp->b_lblkno) {
1870 error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1871 bp->b_blkno = blkno;
1873 bp->b_error = error;
1874 bp->b_ioflags |= BIO_ERROR;
1878 if ((long)bp->b_blkno == -1)
1881 if (bp->b_blkno == -1) {
1886 * Read/write the block from/to the disk that contains the desired
1889 bp->b_iooffset = dbtob(bp->b_blkno);
1890 bo = dep->de_pmp->pm_bo;
1891 BO_STRATEGY(bo, bp);
1897 struct vop_print_args /* {
1901 struct denode *dep = VTODE(ap->a_vp);
1903 printf("\tstartcluster %lu, dircluster %lu, diroffset %lu, ",
1904 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1905 printf("on dev %s\n", devtoname(dep->de_pmp->pm_dev));
1910 msdosfs_pathconf(ap)
1911 struct vop_pathconf_args /* {
1917 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1919 switch (ap->a_name) {
1924 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1927 *ap->a_retval = PATH_MAX;
1929 case _PC_CHOWN_RESTRICTED:
1943 struct vop_vptofh_args /* {
1949 struct defid *defhp;
1951 dep = VTODE(ap->a_vp);
1952 defhp = (struct defid *)ap->a_fhp;
1953 defhp->defid_len = sizeof(struct defid);
1954 defhp->defid_dirclust = dep->de_dirclust;
1955 defhp->defid_dirofs = dep->de_diroffset;
1956 /* defhp->defid_gen = dep->de_gen; */
1960 /* Global vfs data structures for msdosfs */
1961 struct vop_vector msdosfs_vnodeops = {
1962 .vop_default = &default_vnodeops,
1964 .vop_access = msdosfs_access,
1965 .vop_bmap = msdosfs_bmap,
1966 .vop_cachedlookup = msdosfs_lookup,
1967 .vop_open = msdosfs_open,
1968 .vop_close = msdosfs_close,
1969 .vop_create = msdosfs_create,
1970 .vop_fsync = msdosfs_fsync,
1971 .vop_getattr = msdosfs_getattr,
1972 .vop_inactive = msdosfs_inactive,
1973 .vop_link = msdosfs_link,
1974 .vop_lookup = vfs_cache_lookup,
1975 .vop_mkdir = msdosfs_mkdir,
1976 .vop_mknod = msdosfs_mknod,
1977 .vop_pathconf = msdosfs_pathconf,
1978 .vop_print = msdosfs_print,
1979 .vop_read = msdosfs_read,
1980 .vop_readdir = msdosfs_readdir,
1981 .vop_reclaim = msdosfs_reclaim,
1982 .vop_remove = msdosfs_remove,
1983 .vop_rename = msdosfs_rename,
1984 .vop_rmdir = msdosfs_rmdir,
1985 .vop_setattr = msdosfs_setattr,
1986 .vop_strategy = msdosfs_strategy,
1987 .vop_symlink = msdosfs_symlink,
1988 .vop_write = msdosfs_write,
1989 .vop_vptofh = msdosfs_vptofh,