2 /* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by TooLs GmbH.
23 * 4. The name of TooLs GmbH may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
43 * This software is provided "as is".
45 * The author supplies this software to be publicly redistributed on the
46 * understanding that the author is not responsible for the correct
47 * functioning of this software in any circumstances and is not liable for
48 * any damages caused by this software.
53 #include <sys/param.h>
54 #include <sys/systm.h>
56 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
57 #include <sys/kernel.h>
62 #include <sys/namei.h>
63 #include <sys/mount.h>
64 #include <sys/unistd.h>
65 #include <sys/vnode.h>
66 #include <sys/malloc.h>
67 #include <sys/dirent.h>
68 #include <sys/signalvar.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_zone.h>
74 #include <vm/vnode_pager.h>
75 #include <vm/vm_page2.h>
79 #include <machine/inttypes.h>
81 #include <vfs/msdosfs/bpb.h>
82 #include <vfs/msdosfs/direntry.h>
83 #include <vfs/msdosfs/denode.h>
84 #include <vfs/msdosfs/fat.h>
85 #include <vfs/msdosfs/msdosfsmount.h>
90 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
91 * read/written using the vnode for the filesystem. Blocks that represent
92 * the contents of a file are read/written using the vnode for the file
93 * (including directories when they are read/written as files). This
94 * presents problems for the dos filesystem because data that should be in
95 * an inode (if dos had them) resides in the directory itself. Since we
96 * must update directory entries without the benefit of having the vnode
97 * for the directory we must use the vnode for the filesystem. This means
98 * that when a directory is actually read/written (via read, write, or
99 * readdir, or seek) we must use the vnode for the filesystem instead of
100 * the vnode for the directory as would happen in ufs. This is to insure we
101 * retrieve the correct block from the buffer cache since the hash value is
102 * based upon the vnode address and the desired block number.
106 * Create a regular file. On entry the directory to contain the file being
107 * created is locked. We must release before we return. We must also free
108 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
109 * only if the SAVESTART bit in cn_flags is clear on success.
112 msdosfs_create(struct vop_old_create_args *ap)
114 struct componentname *cnp = ap->a_cnp;
115 struct denode ndirent;
117 struct denode *pdep = VTODE(ap->a_dvp);
121 mprintf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
124 * If this is the root directory and there is no space left we
125 * can't do anything. This is because the root directory can not
128 if (pdep->de_StartCluster == MSDOSFSROOT
129 && pdep->de_fndoffset >= pdep->de_FileSize) {
135 * Create a directory entry for the file, then call createde() to
136 * have it installed. NOTE: DOS files are always executable. We
137 * use the absence of the owner write bit to make the file
140 memset(&ndirent, 0, sizeof(ndirent));
141 error = uniqdosname(pdep, cnp, ndirent.de_Name);
145 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
146 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
147 ndirent.de_LowerCase = 0;
148 ndirent.de_StartCluster = 0;
149 ndirent.de_FileSize = 0;
150 ndirent.de_dev = pdep->de_dev;
151 ndirent.de_devvp = pdep->de_devvp;
152 ndirent.de_pmp = pdep->de_pmp;
153 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
155 DETIMES(&ndirent, &ts, &ts, &ts);
156 error = createde(&ndirent, pdep, &dep, cnp);
159 *ap->a_vpp = DETOV(dep);
167 msdosfs_mknod(struct vop_old_mknod_args *ap)
173 msdosfs_open(struct vop_open_args *ap)
175 return(vop_stdopen(ap));
179 msdosfs_close(struct vop_close_args *ap)
181 struct vnode *vp = ap->a_vp;
182 struct denode *dep = VTODE(vp);
185 if (VREFCNT(vp) > 1) {
187 DETIMES(dep, &ts, &ts, &ts);
189 return (vop_stdclose(ap));
193 msdosfs_access(struct vop_access_args *ap)
195 struct denode *dep = VTODE(ap->a_vp);
196 struct msdosfsmount *pmp = dep->de_pmp;
199 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
200 ((dep->de_Attributes & ATTR_READONLY) ?
201 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
202 file_mode &= (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
204 return (vop_helper_access(ap, pmp->pm_uid, pmp->pm_gid, file_mode, 0));
208 msdosfs_getattr(struct vop_getattr_args *ap)
210 struct denode *dep = VTODE(ap->a_vp);
211 struct msdosfsmount *pmp = dep->de_pmp;
212 struct vattr *vap = ap->a_vap;
215 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
219 DETIMES(dep, &ts, &ts, &ts);
220 vap->va_fsid = devid_from_dev(dep->de_dev);
222 * The following computation of the fileid must be the same as that
223 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
226 if (dep->de_Attributes & ATTR_DIRECTORY) {
227 fileid = (uint64_t)cntobn(pmp, dep->de_StartCluster) *
229 if (dep->de_StartCluster == MSDOSFSROOT)
232 fileid = (uint64_t)cntobn(pmp, dep->de_dirclust) *
234 if (dep->de_dirclust == MSDOSFSROOT)
235 fileid = (uint64_t)roottobn(pmp, 0) * dirsperblk;
236 fileid += (uoff_t)dep->de_diroffset / sizeof(struct direntry);
238 vap->va_fileid = fileid;
239 if ((dep->de_Attributes & ATTR_READONLY) == 0)
240 mode = S_IRWXU|S_IRWXG|S_IRWXO;
242 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
243 vap->va_mode = mode &
244 (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
245 vap->va_uid = pmp->pm_uid;
246 vap->va_gid = pmp->pm_gid;
248 vap->va_rmajor = VNOVAL;
249 vap->va_rminor = VNOVAL;
250 vap->va_size = dep->de_FileSize;
251 fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
252 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
253 fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
254 fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
257 vap->va_atime = vap->va_mtime;
258 vap->va_ctime = vap->va_mtime;
261 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
262 vap->va_flags |= SF_ARCHIVED;
264 vap->va_blocksize = pmp->pm_bpcluster;
266 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
267 vap->va_type = ap->a_vp->v_type;
268 vap->va_filerev = dep->de_modrev;
273 msdosfs_setattr(struct vop_setattr_args *ap)
275 struct vnode *vp = ap->a_vp;
276 struct denode *dep = VTODE(vp);
277 struct msdosfsmount *pmp = dep->de_pmp;
278 struct vattr *vap = ap->a_vap;
279 struct ucred *cred = ap->a_cred;
282 mprintf("msdosfs_setattr(): vp %p, vap %p, cred %p\n", vp, vap, cred);
285 * Check for unsettable attributes.
287 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
288 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
289 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
290 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
291 mprintf("msdosfs_setattr(): returning EINVAL\n");
292 mprintf(" va_type %d, va_nlink %llx, va_fsid %llx, va_fileid %llx\n",
293 vap->va_type, (unsigned long long)vap->va_nlink,
294 (unsigned long long)vap->va_fsid,
295 (unsigned long long)vap->va_fileid);
296 mprintf(" va_blocksize %lx, va_rdev %llx, va_bytes %llx, va_gen %lx\n",
297 vap->va_blocksize, (unsigned long long)0,
298 (unsigned long long)vap->va_bytes, vap->va_gen);
299 mprintf(" va_uid %x, va_gid %x\n",
300 vap->va_uid, vap->va_gid);
305 * We don't allow setting attributes on the root directory.
306 * The special case for the root directory is because before
307 * FAT32, the root directory didn't have an entry for itself
308 * (and was otherwise special). With FAT32, the root
309 * directory is not so special, but still doesn't have an
312 if (vp->v_flag & VROOT)
315 if (vap->va_flags != VNOVAL) {
316 if (vp->v_mount->mnt_flag & MNT_RDONLY)
318 if (cred->cr_uid != pmp->pm_uid &&
319 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
322 * We are very inconsistent about handling unsupported
323 * attributes. We ignored the access time and the
324 * read and execute bits. We were strict for the other
327 * Here we are strict, stricter than ufs in not allowing
328 * users to attempt to set SF_SETTABLE bits or anyone to
329 * set unsupported bits. However, we ignore attempts to
330 * set ATTR_ARCHIVE for directories `cp -pr' from a more
331 * sensible file system attempts it a lot.
333 if (cred->cr_uid != 0) {
334 if (vap->va_flags & SF_SETTABLE)
337 if (vap->va_flags & ~SF_ARCHIVED)
339 if (vap->va_flags & SF_ARCHIVED)
340 dep->de_Attributes &= ~ATTR_ARCHIVE;
341 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
342 dep->de_Attributes |= ATTR_ARCHIVE;
343 dep->de_flag |= DE_MODIFIED;
346 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
350 if (vp->v_mount->mnt_flag & MNT_RDONLY)
353 if (uid == (uid_t)VNOVAL)
356 if (gid == (gid_t)VNOVAL)
358 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
359 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
360 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
362 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
366 if (vap->va_size != VNOVAL) {
367 switch (vp->v_type) {
372 * Truncation is only supported for regular files,
373 * Disallow it if the filesystem is read-only.
375 if (vp->v_mount->mnt_flag & MNT_RDONLY)
380 * According to POSIX, the result is unspecified
381 * for file types other than regular files,
382 * directories and shared memory objects. We
383 * don't support any file types except regular
384 * files and directories in this file system, so
385 * this (default) case is unreachable and can do
386 * anything. Keep falling through to detrunc()
391 error = detrunc(dep, vap->va_size, 0);
395 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
396 if (vp->v_mount->mnt_flag & MNT_RDONLY)
398 if (cred->cr_uid != pmp->pm_uid &&
399 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
400 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
401 (error = VOP_EACCESS(vp, VWRITE, cred))))
403 if (vp->v_type != VDIR) {
404 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
405 vap->va_atime.tv_sec != VNOVAL) {
406 dep->de_flag &= ~DE_ACCESS;
407 timespec2fattime(&vap->va_atime, 0,
408 &dep->de_ADate, NULL, NULL);
410 if (vap->va_mtime.tv_sec != VNOVAL) {
411 dep->de_flag &= ~DE_UPDATE;
412 timespec2fattime(&vap->va_mtime, 0,
413 &dep->de_MDate, &dep->de_MTime, NULL);
415 dep->de_Attributes |= ATTR_ARCHIVE;
416 dep->de_flag |= DE_MODIFIED;
420 * DOS files only have the ability to have their writability
421 * attribute set, so we use the owner write bit to set the readonly
424 if (vap->va_mode != (mode_t)VNOVAL) {
425 if (vp->v_mount->mnt_flag & MNT_RDONLY)
427 if (cred->cr_uid != pmp->pm_uid &&
428 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
430 if (vp->v_type != VDIR) {
431 /* We ignore the read and execute bits. */
432 if (vap->va_mode & VWRITE)
433 dep->de_Attributes &= ~ATTR_READONLY;
435 dep->de_Attributes |= ATTR_READONLY;
436 dep->de_Attributes |= ATTR_ARCHIVE;
437 dep->de_flag |= DE_MODIFIED;
440 return (deupdat(dep, 0));
444 msdosfs_read(struct vop_read_args *ap)
460 struct vnode *vp = ap->a_vp;
461 struct denode *dep = VTODE(vp);
462 struct msdosfsmount *pmp = dep->de_pmp;
463 struct uio *uio = ap->a_uio;
465 if (uio->uio_offset < 0)
468 if ((uoff_t)uio->uio_offset > MSDOSFS_FILESIZE_MAX)
471 * If they didn't ask for any data, then we are done.
473 orig_resid = uio->uio_resid;
478 * The caller is supposed to ensure that
479 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
480 * We don't need to check for large offsets as in ffs because
481 * dep->de_FileSize <= MSDOSFS_FILESIZE_MAX < OFF_MAX, so large
482 * offsets cannot cause overflow even in theory.
485 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
487 isadir = dep->de_Attributes & ATTR_DIRECTORY;
489 if (uio->uio_offset >= dep->de_FileSize)
492 cn = de_cluster(pmp, uio->uio_offset);
493 loffset = de_cn2doff(pmp, cn);
495 blsize = pmp->pm_bpcluster;
496 on = uio->uio_offset & pmp->pm_crbomask;
499 * If we are operating on a directory file then be sure to
500 * do i/o with the vnode for the filesystem instead of the
501 * vnode for the directory.
505 /* convert cluster # to block # */
506 error = pcbmap(dep, cn, &lbn, NULL, &blsize);
507 loffset = de_bn2doff(pmp, lbn);
508 if (error == E2BIG) {
513 error = bread(pmp->pm_devvp, loffset, blsize, &bp);
514 } else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
515 error = bread(vp, loffset, blsize, &bp);
516 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
517 error = cluster_readx(vp, dep->de_FileSize, loffset,
518 blsize, B_NOTMETA, on + uio->uio_resid,
519 seqcount * MAXBSIZE, &bp);
520 bp->b_flags |= B_CLUSTEROK;
521 } else if (seqcount > 1) {
522 off_t raoffset = de_cn2doff(pmp, rablock);
524 error = breadn(vp, loffset,
525 blsize, &raoffset, &rasize, 1, &bp);
527 error = bread(vp, loffset, blsize, &bp);
533 diff = pmp->pm_bpcluster - on;
534 n = szmin(uio->uio_resid, diff);
535 diff = dep->de_FileSize - uio->uio_offset;
538 diff = blsize - bp->b_resid;
541 error = uiomovebp(bp, bp->b_data + on, (size_t)n, uio);
543 } while (error == 0 && uio->uio_resid > 0 && n != 0);
545 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
546 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
547 dep->de_flag |= DE_ACCESS;
552 * Write data to a file or directory.
555 msdosfs_write(struct vop_write_args *ap)
565 struct buf *bp = NULL;
566 int ioflag = ap->a_ioflag;
567 struct uio *uio = ap->a_uio;
568 struct thread *td = uio->uio_td;
569 struct vnode *vp = ap->a_vp;
570 struct vnode *thisvp;
571 struct denode *dep = VTODE(vp);
572 struct msdosfsmount *pmp = dep->de_pmp;
573 struct proc *p = (td ? td->td_proc : NULL);
574 struct lwp *lp = (td ? td->td_lwp : NULL);
576 mprintf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
577 vp, uio, ioflag, ap->a_cred);
578 mprintf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
579 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
581 switch (vp->v_type) {
583 if (ioflag & IO_APPEND)
584 uio->uio_offset = dep->de_FileSize;
590 panic("msdosfs_write(): bad file type");
593 if (uio->uio_offset < 0)
597 * This is needed (unlike in ffs_write()) because we extend the
598 * file outside of the loop but we don't want to extend the file
599 * for writes of 0 bytes.
601 if (uio->uio_resid == 0)
605 * The caller is supposed to ensure that
606 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
608 if ((uoff_t)uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
612 * If they've exceeded their filesize limit, tell them about it.
615 ((uoff_t)uio->uio_offset + uio->uio_resid >
616 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
617 lwpsignal(p, lp, SIGXFSZ);
622 * If the offset we are starting the write at is beyond the end of
623 * the file, then they've done a seek. Unix filesystems allow
624 * files with holes in them, DOS doesn't so we must fill the hole
625 * with zeroed blocks.
627 if (uio->uio_offset > dep->de_FileSize) {
628 error = deextend(dep, uio->uio_offset);
634 * Remember some values in case the write fails.
636 resid = uio->uio_resid;
637 osize = dep->de_FileSize;
640 * If we write beyond the end of the file, extend it to its ultimate
641 * size ahead of the time to hopefully get a contiguous area.
643 if (uio->uio_offset + resid > osize) {
644 count = de_clcount(pmp, uio->uio_offset + resid) -
645 de_clcount(pmp, osize);
646 error = extendfile(dep, count, NULL, NULL, 0);
647 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
649 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
651 lastcn = de_clcount(pmp, osize) - 1;
653 seqcount = ioflag >> IO_SEQSHIFT;
655 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
660 croffset = uio->uio_offset & pmp->pm_crbomask;
661 n = (int)szmin(uio->uio_resid, pmp->pm_bpcluster - croffset);
662 if (uio->uio_offset + n > dep->de_FileSize) {
663 dep->de_FileSize = uio->uio_offset + n;
664 /* The object size needs to be set before buffer is allocated */
665 vnode_pager_setsize(vp, dep->de_FileSize);
669 * If either the whole cluster gets written, or we write
670 * the cluster from its start beyond EOF, then no need to
671 * read data from disk.
673 * If UIO_NOCOPY is set we have to do a read-before-write
674 * to fill in any missing pieces of the buffer since no
675 * actual overwrite will occur.
677 cn = de_cluster(pmp, uio->uio_offset);
678 if ((uio->uio_offset & pmp->pm_crbomask) == 0
679 && uio->uio_segflg != UIO_NOCOPY
680 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
681 > de_cluster(pmp, uio->uio_offset)
682 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
683 bp = getblk(thisvp, de_cn2doff(pmp, cn),
684 pmp->pm_bpcluster, 0, 0);
687 * Do the bmap now, since pcbmap needs buffers
688 * for the FAT table. (see msdosfs_strategy)
690 if (bp->b_bio2.bio_offset == NOOFFSET) {
694 de_cluster(pmp, bp->b_loffset),
695 &dblkno, NULL, NULL);
696 if (error || dblkno == (daddr_t)-1) {
697 bp->b_bio2.bio_offset = NOOFFSET;
699 bp->b_bio2.bio_offset = de_bn2doff(pmp,
703 if (bp->b_bio2.bio_offset == NOOFFSET) {
706 error = EIO; /* XXX */
711 * The block we need to write into exists, so read
714 error = bread(thisvp, de_cn2doff(pmp, cn),
715 pmp->pm_bpcluster, &bp);
723 * Should these vnode_pager_* functions be done on dir
728 * Copy the data from user space into the buf header.
730 error = uiomovebp(bp, bp->b_data + croffset, (size_t)n, uio);
736 /* Prepare for clustered writes in some else clauses. */
737 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
738 bp->b_flags |= B_CLUSTEROK;
741 * If IO_SYNC, then each buffer is written synchronously.
742 * Otherwise, if we have a severe page deficiency then
743 * write the buffer asynchronously. Otherwise, if on a
744 * cluster boundary then write the buffer asynchronously,
745 * combining it with contiguous clusters if permitted and
746 * possible, since we don't expect more writes into this
747 * buffer soon. Otherwise, do a delayed write because we
748 * expect more writes into this buffer soon.
750 if (ioflag & IO_SYNC)
752 else if (vm_page_count_severe() || buf_dirty_count_severe())
754 else if (n + croffset == pmp->pm_bpcluster) {
755 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
756 cluster_write(bp, dep->de_FileSize,
757 pmp->pm_bpcluster, seqcount);
762 dep->de_flag |= DE_UPDATE;
763 } while (error == 0 && uio->uio_resid > 0);
766 * If the write failed and they want us to, truncate the file back
767 * to the size it was before the write was attempted.
771 if (ioflag & IO_UNIT) {
772 detrunc(dep, osize, ioflag & IO_SYNC);
773 uio->uio_offset -= resid - uio->uio_resid;
774 uio->uio_resid = resid;
776 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC);
777 if (uio->uio_resid != resid)
780 } else if (ioflag & IO_SYNC)
781 error = deupdat(dep, 1);
786 * Flush the blocks of a file to disk.
789 msdosfs_fsync(struct vop_fsync_args *ap)
791 struct vnode *vp = ap->a_vp;
796 * Flush all dirty buffers associated with a vnode.
801 vfsync(vp, ap->a_waitfor, 0, NULL, NULL);
803 if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
804 vprint("msdosfs_fsync: dirty", vp);
809 * If the syncing request comes from fsync(2), sync the entire
810 * FAT and any other metadata that happens to be on devvp. We
811 * need this mainly for the FAT. We write the FAT sloppily, and
812 * syncing it all now is the best we can easily do to get all
813 * directory entries associated with the file (not just the file)
814 * fully synced. The other metadata includes critical metadata
815 * for all directory entries, but only in the MNT_ASYNC case. We
816 * will soon sync all metadata in the file's directory entry.
817 * Non-critical metadata for associated directory entries only
818 * gets synced accidentally, as in most file systems.
820 if (ap->a_waitfor == MNT_WAIT) {
821 devvp = VTODE(ap->a_vp)->de_pmp->pm_devvp;
822 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
823 allerror = VOP_FSYNC(devvp, MNT_WAIT, 0);
828 error = deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT);
835 msdosfs_remove(struct vop_old_remove_args *ap)
837 struct denode *dep = VTODE(ap->a_vp);
838 struct denode *ddep = VTODE(ap->a_dvp);
841 if (ap->a_vp->v_type == VDIR)
844 error = removede(ddep, dep);
845 mprintf("msdosfs_remove(), dep %p, v_refcnt 0x%08x\n",
846 dep, ap->a_vp->v_refcnt);
851 * DOS filesystems don't know what links are.
854 msdosfs_link(struct vop_old_link_args *ap)
860 * Renames on files require moving the denode to a new hash queue since the
861 * denode's location is used to compute which hash queue to put the file
862 * in. Unless it is a rename in place. For example "mv a b".
864 * What follows is the basic algorithm:
867 * if (dest file exists) {
870 * if (dest and src in same directory) {
871 * rewrite name in existing directory slot
873 * write new entry in dest directory
874 * update offset and dirclust in denode
875 * move denode to new hash chain
876 * clear old directory entry
880 * if (dest directory exists) {
881 * if (dest is not empty) {
884 * remove dest directory
886 * if (dest and src in same directory) {
887 * rewrite name in existing entry
889 * be sure dest is not a child of src directory
890 * write entry in dest directory
891 * update "." and ".." in moved directory
892 * clear old directory entry for moved directory
897 * source's parent directory is unlocked
898 * source file or directory is unlocked
899 * destination's parent directory is locked
900 * destination file or directory is locked if it exists
903 * all denodes should be released
906 msdosfs_rename(struct vop_old_rename_args *ap)
908 struct vnode *tdvp = ap->a_tdvp;
909 struct vnode *fvp = ap->a_fvp;
910 struct vnode *fdvp = ap->a_fdvp;
911 struct vnode *tvp = ap->a_tvp;
912 struct componentname *tcnp = ap->a_tcnp;
913 struct componentname *fcnp = ap->a_fcnp;
914 struct denode *ip, *xp, *dp, *zp;
915 u_char toname[12], oldname[11];
916 u_long from_diroffset, to_diroffset;
918 int doingdirectory = 0, newparent = 0;
922 struct msdosfsmount *pmp;
923 struct direntry *dotdotp;
924 struct buf *bp = NULL;
926 pmp = VFSTOMSDOSFS(fdvp->v_mount);
929 * Check for cross-device rename.
931 if (fvp->v_mount != tdvp->v_mount ||
932 (tvp && fvp->v_mount != tvp->v_mount)) {
947 * If source and dest are the same, do nothing.
955 * fvp is unlocked, fdvp, tvp, tdvp are locked. Lock fvp and note
956 * that we have to unlock it to use the abortit target.
958 error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
965 * Be sure we are not renaming ".", "..", or an alias of ".". This
966 * leads to a crippled directory tree. It's pretty tough to do a
967 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
968 * doesn't work if the ".." entry is missing.
970 if (ip->de_Attributes & ATTR_DIRECTORY) {
972 * Avoid ".", "..", and aliases of "." for obvious reasons.
974 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
976 (fcnp->cn_flags & CNP_ISDOTDOT) ||
977 (tcnp->cn_flags & CNP_ISDOTDOT) ||
978 (ip->de_flag & DE_RENAME)) {
983 ip->de_flag |= DE_RENAME;
988 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only
989 * set if doingdirectory. We will get fvp unlocked in fairly
990 * short order. dp and xp must be setup and fvp must be unlocked
991 * for the out and bad targets to work properly.
994 xp = tvp ? VTODE(tvp) : NULL;
997 * Remember direntry place to use for destination
999 to_diroffset = dp->de_fndoffset;
1000 to_count = dp->de_fndcnt;
1003 * If ".." must be changed (ie the directory gets a new
1004 * parent) then the source directory must not be in the
1005 * directory hierarchy above the target, as this would
1006 * orphan everything below the source directory. Also
1007 * the user must have write permission in the source so
1008 * as to be able to change "..". We must repeat the call
1009 * to namei, as the parent directory is unlocked by the
1010 * call to doscheckpath().
1012 error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
1014 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1018 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1019 * DE_RENAME only set if doingdirectory.
1021 if (doingdirectory && newparent) {
1022 if (error) /* write access check above */
1029 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1030 * get an extra ref so we wind up with just an unlocked, ref'd
1031 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp
1032 * isn't locked so we can't use the out target).
1035 error = doscheckpath(ip, dp);
1036 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1042 * relookup no longer messes with the ref count. tdvp must
1043 * be unlocked on entry and on success will be locked on
1046 error = relookup(tdvp, &tvp, tcnp);
1048 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1056 * tvp and tdvp are now locked again.
1059 xp = tvp ? VTODE(tvp) : NULL;
1063 * tvp and tdvp are now locked again, the 'bad' target can be used
1064 * to clean them up again. Delete an existant target and clean
1065 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up.
1069 * Target must be empty if a directory and have no links
1070 * to it. Also, ensure source and target are compatible
1071 * (both directories, or both not directories).
1073 if (xp->de_Attributes & ATTR_DIRECTORY) {
1074 if (!dosdirempty(xp)) {
1078 if (!doingdirectory) {
1082 } else if (doingdirectory) {
1086 error = removede(dp, xp);
1095 * Convert the filename in tcnp into a dos filename. We copy this
1096 * into the denode and directory entry for the destination
1099 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1104 * Since from wasn't locked at various places above, we have to do
1105 * a relookup here. If the target and source are the same directory
1106 * we have to unlock the target directory in order to safely relookup
1107 * the source, because relookup expects its directory to be unlocked.
1109 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must
1110 * now take that into account.
1112 * The tdvp locking issues make this a real mess.
1114 fcnp->cn_flags &= ~CNP_MODMASK;
1115 fcnp->cn_flags |= CNP_LOCKPARENT;
1118 error = relookup(fdvp, &fvp, fcnp);
1119 if (error || fvp == NULL) {
1121 * From name has disappeared. Note: fdvp might == tdvp.
1123 * DE_RENAME is only set if doingdirectory.
1126 panic("rename: lost dir entry");
1127 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1140 * No error occured. tdvp, fdvp and fvp are all locked. If
1141 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned
1142 * up. ap->a_fvp is still refd.
1146 from_diroffset = zp->de_fndoffset;
1149 * Ensure that the directory entry still exists and has not
1150 * changed till now. If the source is a file the entry may
1151 * have been unlinked or renamed. In either case there is
1152 * no further work to be done. If the source is a directory
1153 * then it cannot have been rmdir'ed or renamed; this is
1154 * prohibited by the DE_RENAME flag.
1156 * DE_RENAME is only set if doingdirectory.
1160 panic("rename: lost dir entry");
1163 u_long new_dirclust;
1164 u_long new_diroffset;
1167 * First write a new entry in the destination
1168 * directory and mark the entry in the source directory
1169 * as deleted. Then move the denode to the correct hash
1170 * chain for its new location in the filesystem. And, if
1171 * we moved a directory, then update its .. entry to point
1172 * to the new parent directory.
1174 memcpy(oldname, ip->de_Name, 11);
1175 memcpy(ip->de_Name, toname, 11); /* update denode */
1176 dp->de_fndoffset = to_diroffset;
1177 dp->de_fndcnt = to_count;
1178 error = createde(ip, dp, NULL, tcnp);
1180 memcpy(ip->de_Name, oldname, 11);
1184 * If ip is for a directory, then its name should always
1185 * be "." since it is for the directory entry in the
1186 * directory itself (msdosfs_lookup() always translates
1187 * to the "." entry so as to get a unique denode, except
1188 * for the root directory there are different
1189 * complications). However, we just corrupted its name
1190 * to pass the correct name to createde(). Undo this.
1192 if ((ip->de_Attributes & ATTR_DIRECTORY) != 0)
1193 memcpy(ip->de_Name, oldname, 11);
1195 zp->de_fndoffset = from_diroffset;
1196 error = removede(zp, ip);
1198 /* XXX should downgrade to ro here, fs is corrupt */
1201 if (!doingdirectory) {
1202 error = pcbmap(dp, de_cluster(pmp, to_diroffset),
1203 NULL, &new_dirclust, NULL);
1205 /* XXX should downgrade to ro here, fs is corrupt */
1208 if (new_dirclust == MSDOSFSROOT)
1209 new_diroffset = to_diroffset;
1211 new_diroffset = to_diroffset & pmp->pm_crbomask;
1212 msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1217 * If we moved a directory to a new parent directory, then we must
1218 * fixup the ".." entry in the moved directory.
1220 if (doingdirectory && newparent) {
1221 cn = ip->de_StartCluster;
1222 if (cn == MSDOSFSROOT) {
1223 /* this should never happen */
1224 panic("msdosfs_rename(): updating .. in root directory?");
1226 bn = cntobn(pmp, cn);
1228 error = bread(pmp->pm_devvp, de_bn2doff(pmp, bn),
1229 pmp->pm_bpcluster, &bp);
1231 /* XXX should downgrade to ro here, fs is corrupt */
1235 dotdotp = (struct direntry *)bp->b_data + 1;
1236 pcl = dp->de_StartCluster;
1237 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1239 putushort(dotdotp->deStartCluster, pcl);
1241 putushort(dotdotp->deHighClust, pcl >> 16);
1242 if (DOINGASYNC(fvp))
1244 else if ((error = bwrite(bp)) != 0) {
1245 /* XXX should downgrade to ro here, fs is corrupt */
1251 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd
1255 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */
1266 * 'bad' target: xp governs tvp. tvp and tdvp are locked, fdvp and fvp
1267 * are not locked. ip points to fvp's inode which may have DE_RENAME
1276 * 'out' target: tvp and tdvp have already been cleaned up.
1279 ip->de_flag &= ~DE_RENAME;
1287 struct direntry dot;
1288 struct direntry dotdot;
1289 } dosdirtemplate = {
1290 { ". ", /* the . entry */
1291 ATTR_DIRECTORY, /* file attribute */
1293 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1294 { 0, 0 }, /* access date */
1295 { 0, 0 }, /* high bits of start cluster */
1296 { 210, 4 }, { 210, 4 }, /* modify time & date */
1297 { 0, 0 }, /* startcluster */
1298 { 0, 0, 0, 0 } /* filesize */
1300 { ".. ", /* the .. entry */
1301 ATTR_DIRECTORY, /* file attribute */
1303 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1304 { 0, 0 }, /* access date */
1305 { 0, 0 }, /* high bits of start cluster */
1306 { 210, 4 }, { 210, 4 }, /* modify time & date */
1307 { 0, 0 }, /* startcluster */
1308 { 0, 0, 0, 0 } /* filesize */
1313 msdosfs_mkdir(struct vop_old_mkdir_args *ap)
1315 struct componentname *cnp = ap->a_cnp;
1317 struct denode *pdep = VTODE(ap->a_dvp);
1318 struct direntry *denp;
1319 struct msdosfsmount *pmp = pdep->de_pmp;
1321 u_long newcluster, pcl;
1324 struct denode ndirent;
1328 * If this is the root directory and there is no space left we
1329 * can't do anything. This is because the root directory can not
1332 if (pdep->de_StartCluster == MSDOSFSROOT
1333 && pdep->de_fndoffset >= pdep->de_FileSize) {
1339 * Allocate a cluster to hold the about to be created directory.
1341 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1345 memset(&ndirent, 0, sizeof(ndirent));
1346 ndirent.de_pmp = pmp;
1347 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1349 DETIMES(&ndirent, &ts, &ts, &ts);
1352 * Now fill the cluster with the "." and ".." entries. And write
1353 * the cluster to disk. This way it is there for the parent
1354 * directory to be pointing at if there were a crash.
1356 bn = cntobn(pmp, newcluster);
1357 /* always succeeds */
1358 bp = getblk(pmp->pm_devvp, de_bn2doff(pmp, bn),
1359 pmp->pm_bpcluster, 0, 0);
1360 memset(bp->b_data, 0, pmp->pm_bpcluster);
1361 memcpy(bp->b_data, &dosdirtemplate, sizeof(dosdirtemplate));
1362 denp = (struct direntry *)bp->b_data;
1363 putushort(denp[0].deStartCluster, newcluster);
1364 putushort(denp[0].deCDate, ndirent.de_CDate);
1365 putushort(denp[0].deCTime, ndirent.de_CTime);
1366 denp[0].deCHundredth = ndirent.de_CHun;
1367 putushort(denp[0].deADate, ndirent.de_ADate);
1368 putushort(denp[0].deMDate, ndirent.de_MDate);
1369 putushort(denp[0].deMTime, ndirent.de_MTime);
1370 pcl = pdep->de_StartCluster;
1372 * Although the root directory has a non-magic starting cluster
1373 * number for FAT32, chkdsk and fsck_msdosfs still require
1374 * references to it in dotdot entries to be magic.
1376 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1378 putushort(denp[1].deStartCluster, pcl);
1379 putushort(denp[1].deCDate, ndirent.de_CDate);
1380 putushort(denp[1].deCTime, ndirent.de_CTime);
1381 denp[1].deCHundredth = ndirent.de_CHun;
1382 putushort(denp[1].deADate, ndirent.de_ADate);
1383 putushort(denp[1].deMDate, ndirent.de_MDate);
1384 putushort(denp[1].deMTime, ndirent.de_MTime);
1386 putushort(denp[0].deHighClust, newcluster >> 16);
1387 putushort(denp[1].deHighClust, pcl >> 16);
1390 if (DOINGASYNC(ap->a_dvp))
1392 else if ((error = bwrite(bp)) != 0)
1396 * Now build up a directory entry pointing to the newly allocated
1397 * cluster. This will be written to an empty slot in the parent
1400 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1404 ndirent.de_Attributes = ATTR_DIRECTORY;
1405 ndirent.de_LowerCase = 0;
1406 ndirent.de_StartCluster = newcluster;
1407 ndirent.de_FileSize = 0;
1408 ndirent.de_dev = pdep->de_dev;
1409 ndirent.de_devvp = pdep->de_devvp;
1410 error = createde(&ndirent, pdep, &dep, cnp);
1413 *ap->a_vpp = DETOV(dep);
1417 clusterfree(pmp, newcluster, NULL);
1423 msdosfs_rmdir(struct vop_old_rmdir_args *ap)
1425 struct vnode *vp = ap->a_vp;
1426 struct vnode *dvp = ap->a_dvp;
1427 struct denode *ip, *dp;
1434 * Verify the directory is empty (and valid).
1435 * (Rmdir ".." won't be valid since
1436 * ".." will contain a reference to
1437 * the current directory and thus be
1441 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1446 * Delete the entry from the directory. For dos filesystems this
1447 * gets rid of the directory entry on disk, the in memory copy
1448 * still exists but the de_refcnt is <= 0. This prevents it from
1449 * being found by deget(). When the vput() on dep is done we give
1450 * up access and eventually msdosfs_reclaim() will be called which
1451 * will remove it from the denode cache.
1453 error = removede(dp, ip);
1457 * Truncate the directory that is being deleted.
1459 error = detrunc(ip, (u_long)0, IO_SYNC);
1465 * DOS filesystems don't know what symlinks are.
1468 msdosfs_symlink(struct vop_old_symlink_args *ap)
1470 return (EOPNOTSUPP);
1474 msdosfs_readdir(struct vop_readdir_args *ap)
1486 struct buf *bp = NULL;
1488 struct msdosfsmount *pmp;
1489 struct direntry *dentp;
1490 struct dirent dirbuf;
1491 struct uio *uio = ap->a_uio;
1492 off_t *cookies = NULL;
1497 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
1501 dep = VTODE(ap->a_vp);
1504 mprintf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1505 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1508 * msdosfs_readdir() won't operate properly on regular files since
1509 * it does i/o only with the filesystem vnode, and hence can
1510 * retrieve the wrong block from the buffer cache for a plain file.
1511 * So, fail attempts to readdir() on a plain file.
1513 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
1519 * To be safe, initialize dirbuf
1521 memset(dirbuf.d_name, 0, sizeof(dirbuf.d_name));
1524 * If the user buffer is smaller than the size of one dos directory
1525 * entry or the file offset is not a multiple of the size of a
1526 * directory entry, then we fail the read.
1528 off = offset = uio->uio_offset;
1529 if (uio->uio_resid < sizeof(struct direntry) ||
1530 (offset & (sizeof(struct direntry) - 1))) {
1535 if (ap->a_ncookies) {
1536 ncookies = uio->uio_resid / 16 + 1;
1537 if (ncookies > 1024)
1539 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1540 *ap->a_cookies = cookies;
1541 *ap->a_ncookies = ncookies;
1544 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1546 #define d_fileno d_ino
1548 * If they are reading from the root directory then, we simulate
1549 * the . and .. entries since these don't exist in the root
1550 * directory. We also set the offset bias to make up for having to
1551 * simulate these entries. By this I mean that at file offset 64 we
1552 * read the first entry in the root directory that lives on disk.
1554 if (dep->de_StartCluster == MSDOSFSROOT
1555 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1557 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1560 bias = 2 * sizeof(struct direntry);
1561 if (offset < bias) {
1562 for (n = (int)offset / sizeof(struct direntry);
1564 dirbuf.d_fileno = FAT32(pmp) ?
1565 (uint64_t)cntobn(pmp, pmp->pm_rootdirblk) *
1567 dirbuf.d_type = DT_DIR;
1570 dirbuf.d_namlen = 1;
1571 dirbuf.d_name[0] = '.';
1574 dirbuf.d_namlen = 2;
1575 dirbuf.d_name[0] = '.';
1576 dirbuf.d_name[1] = '.';
1579 if (vop_write_dirent(&error, uio,
1580 dirbuf.d_fileno, dirbuf.d_type,
1581 dirbuf.d_namlen, dirbuf.d_name))
1585 offset += sizeof(struct direntry);
1588 *cookies++ = offset;
1589 if (--ncookies <= 0)
1599 while (uio->uio_resid > 0) {
1600 lbn = de_cluster(pmp, offset - bias);
1601 on = (offset - bias) & pmp->pm_crbomask;
1602 n = szmin(pmp->pm_bpcluster - on, uio->uio_resid);
1603 diff = dep->de_FileSize - (offset - bias);
1607 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1610 error = bread(pmp->pm_devvp, de_bn2doff(pmp, bn), blsize, &bp);
1615 n = min(n, blsize - bp->b_resid);
1623 * Convert from dos directory entries to fs-independent
1624 * directory entries.
1626 for (dentp = (struct direntry *)(bp->b_data + on);
1627 (char *)dentp < bp->b_data + on + n;
1628 dentp++, offset += sizeof(struct direntry)) {
1630 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1631 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1634 * If this is an unused entry, we can stop.
1636 if (dentp->deName[0] == SLOT_EMPTY) {
1641 * Skip deleted entries.
1643 if (dentp->deName[0] == SLOT_DELETED) {
1650 * Handle Win95 long directory entries
1652 if (dentp->deAttributes == ATTR_WIN95) {
1653 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1655 chksum = win2unixfn(&nb,
1656 (struct winentry *)dentp, chksum, pmp);
1661 * Skip volume labels
1663 if (dentp->deAttributes & ATTR_VOLUME) {
1669 * This computation of d_fileno must match
1670 * the computation of va_fileid in
1673 if (dentp->deAttributes & ATTR_DIRECTORY) {
1674 cn = getushort(dentp->deStartCluster);
1676 cn |= getushort(dentp->deHighClust) <<
1678 if (cn == MSDOSFSROOT)
1679 cn = pmp->pm_rootdirblk;
1681 if (cn == MSDOSFSROOT && !FAT32(pmp))
1682 dirbuf.d_fileno = 1;
1684 dirbuf.d_fileno = cntobn(pmp, cn) *
1686 dirbuf.d_type = DT_DIR;
1688 dirbuf.d_fileno = (uoff_t)offset /
1689 sizeof(struct direntry);
1690 dirbuf.d_type = DT_REG;
1693 if (chksum != winChksum(dentp->deName)) {
1694 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1695 (u_char *)dirbuf.d_name,
1696 dentp->deLowerCase |
1697 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1698 (LCASE_BASE | LCASE_EXT) : 0),
1702 mbnambuf_flush(&nb, &dirbuf);
1704 if (vop_write_dirent(&error, uio, dirbuf.d_fileno,
1705 dirbuf.d_type, dirbuf.d_namlen, dirbuf.d_name)) {
1715 *cookies++ = offset + sizeof(struct direntry);
1716 if (--ncookies <= 0) {
1721 off = offset + sizeof(struct direntry);
1726 /* Subtract unused cookies */
1728 *ap->a_ncookies -= ncookies;
1730 uio->uio_offset = off;
1733 * Set the eofflag (NFS uses it)
1735 if (ap->a_eofflag) {
1736 if (dep->de_FileSize - (offset - bias) <= 0)
1742 vn_unlock(ap->a_vp);
1747 * a_vp - pointer to the file's vnode
1748 * a_runp - where to return the "run past" a_bn. This is the count of logical
1749 * blocks whose physical blocks (together with a_bn's physical block)
1751 * a_runb - where to return the "run before" a_bn.
1754 msdosfs_bmap(struct vop_bmap_args *ap)
1756 struct fatcache savefc;
1759 struct msdosfsmount *pmp;
1763 int bnpercn, error, maxio, maxrun, run;
1769 if (ap->a_doffsetp == NULL)
1771 if (ap->a_runp != NULL)
1773 if (ap->a_runb != NULL)
1776 KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0);
1777 error = pcbmap(dep, de_cluster(pmp, ap->a_loffset), &dbn, NULL, NULL);
1778 if (error || dbn == (daddr_t)-1)
1779 *ap->a_doffsetp = NOOFFSET;
1781 *ap->a_doffsetp = de_bn2doff(pmp, dbn);
1782 if (error != 0 || dbn == (daddr_t)-1 ||
1783 (ap->a_runp == NULL && ap->a_runb == NULL))
1787 * Prepare to back out updates of the fatchain cache after the one
1788 * for the first block done by pcbmap() above. Without the backout,
1789 * then whenever the caller doesn't do i/o to all of the blocks that
1790 * we find, the single useful cache entry would be too far in advance
1791 * of the actual i/o to work for the next sequential i/o. Then the
1792 * FAT would be searched from the beginning. With the backout, the
1793 * FAT is searched starting at most a few blocks early. This wastes
1794 * much less time. Time is also wasted finding more blocks than the
1795 * caller will do i/o to. This is necessary because the runlength
1796 * parameters are output-only.
1798 savefc = dep->de_fc[FC_LASTMAP];
1800 cn = de_cluster(pmp, ap->a_loffset);
1802 maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1803 bnpercn = de_cn2bn(pmp, 1);
1804 if (ap->a_runp != NULL) {
1805 maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1806 for (run = 1; run <= maxrun; run++) {
1807 if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1808 runbn != dbn + run * bnpercn)
1811 *ap->a_runp = run - 1;
1813 if (ap->a_runb != NULL) {
1814 maxrun = ulmin(maxio - 1, cn);
1815 for (run = 1; run < maxrun; run++) {
1816 if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1817 runbn != dbn - run * bnpercn)
1820 *ap->a_runb = run - 1;
1822 dep->de_fc[FC_LASTMAP] = savefc;
1828 msdosfs_strategy(struct vop_strategy_args *ap)
1830 struct bio *bio = ap->a_bio;
1832 struct buf *bp = bio->bio_buf;
1833 struct vnode *vp = ap->a_vp;
1834 struct denode *dep = VTODE(vp);
1835 struct msdosfsmount *pmp = dep->de_pmp;
1839 if (vp->v_type == VBLK || vp->v_type == VCHR)
1840 panic("msdosfs_strategy: spec");
1842 * If we don't already know the filesystem relative block number
1843 * then get it using pcbmap(). If pcbmap() returns the block
1844 * number as -1 then we've got a hole in the file. DOS filesystems
1845 * don't allow files with holes, so we shouldn't ever see this.
1847 nbio = push_bio(bio);
1848 if (nbio->bio_offset == NOOFFSET) {
1849 error = pcbmap(dep, de_cluster(pmp, bio->bio_offset),
1850 &dblkno, NULL, NULL);
1852 bp->b_error = error;
1853 bp->b_flags |= B_ERROR;
1854 /* I/O was never started on nbio, must biodone(bio) */
1858 if (dblkno == (daddr_t)-1) {
1859 nbio->bio_offset = NOOFFSET;
1862 nbio->bio_offset = de_bn2doff(pmp, dblkno);
1865 if (nbio->bio_offset == NOOFFSET) {
1866 /* I/O was never started on nbio, must biodone(bio) */
1871 * Read/write the block from/to the disk that contains the desired
1874 vn_strategy(dep->de_devvp, nbio);
1879 msdosfs_print(struct vop_print_args *ap)
1881 struct denode *dep = VTODE(ap->a_vp);
1883 kprintf("tag VT_MSDOSFS, startcluster %lu, dircluster %lu, "
1884 "diroffset %lu, dev %d, %d",
1885 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset,
1886 major(dep->de_dev), minor(dep->de_dev));
1887 lockmgr_printinfo(&ap->a_vp->v_lock);
1892 msdosfs_pathconf(struct vop_pathconf_args *ap)
1894 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1896 switch (ap->a_name) {
1897 case _PC_FILESIZEBITS:
1898 *ap->a_retval = FAT12(pmp) ? 32 : 33;
1905 pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1907 case _PC_CHOWN_RESTRICTED:
1914 return (vop_stdpathconf(ap));
1919 /* Global vfs data structures for msdosfs */
1920 struct vop_ops msdosfs_vnode_vops = {
1921 .vop_default = vop_defaultop,
1922 .vop_access = msdosfs_access,
1923 .vop_bmap = msdosfs_bmap,
1924 .vop_getpages = vop_stdgetpages,
1925 .vop_putpages = vop_stdputpages,
1926 .vop_open = msdosfs_open,
1927 .vop_close = msdosfs_close,
1928 .vop_old_create = msdosfs_create,
1929 .vop_fsync = msdosfs_fsync,
1930 .vop_getattr = msdosfs_getattr,
1931 .vop_inactive = msdosfs_inactive,
1932 .vop_old_link = msdosfs_link,
1933 .vop_old_lookup = msdosfs_lookup,
1934 .vop_old_mkdir = msdosfs_mkdir,
1935 .vop_old_mknod = msdosfs_mknod,
1936 .vop_pathconf = msdosfs_pathconf,
1937 .vop_print = msdosfs_print,
1938 .vop_read = msdosfs_read,
1939 .vop_readdir = msdosfs_readdir,
1940 .vop_reclaim = msdosfs_reclaim,
1941 .vop_old_remove = msdosfs_remove,
1942 .vop_old_rename = msdosfs_rename,
1943 .vop_old_rmdir = msdosfs_rmdir,
1944 .vop_setattr = msdosfs_setattr,
1945 .vop_strategy = msdosfs_strategy,
1946 .vop_old_symlink = msdosfs_symlink,
1947 .vop_write = msdosfs_write,