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 = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR)))
325 * We are very inconsistent about handling unsupported
326 * attributes. We ignored the access time and the
327 * read and execute bits. We were strict for the other
330 * Here we are strict, stricter than ufs in not allowing
331 * users to attempt to set SF_SETTABLE bits or anyone to
332 * set unsupported bits. However, we ignore attempts to
333 * set ATTR_ARCHIVE for directories `cp -pr' from a more
334 * sensible file system attempts it a lot.
336 if (cred->cr_uid != 0) {
337 if (vap->va_flags & SF_SETTABLE)
340 if (vap->va_flags & ~SF_ARCHIVED)
342 if (vap->va_flags & SF_ARCHIVED)
343 dep->de_Attributes &= ~ATTR_ARCHIVE;
344 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
345 dep->de_Attributes |= ATTR_ARCHIVE;
346 dep->de_flag |= DE_MODIFIED;
349 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
353 if (vp->v_mount->mnt_flag & MNT_RDONLY)
356 if (uid == (uid_t)VNOVAL)
359 if (gid == (gid_t)VNOVAL)
361 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
362 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
363 (error = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR)))
367 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
371 if (vap->va_size != VNOVAL) {
372 switch (vp->v_type) {
377 * Truncation is only supported for regular files,
378 * Disallow it if the filesystem is read-only.
380 if (vp->v_mount->mnt_flag & MNT_RDONLY)
385 * According to POSIX, the result is unspecified
386 * for file types other than regular files,
387 * directories and shared memory objects. We
388 * don't support any file types except regular
389 * files and directories in this file system, so
390 * this (default) case is unreachable and can do
391 * anything. Keep falling through to detrunc()
396 error = detrunc(dep, vap->va_size, 0);
400 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
401 if (vp->v_mount->mnt_flag & MNT_RDONLY)
403 if (cred->cr_uid != pmp->pm_uid &&
404 (error = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR)) &&
405 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
406 (error = VOP_EACCESS(vp, VWRITE, cred))))
410 if (vp->v_type != VDIR) {
411 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
412 vap->va_atime.tv_sec != VNOVAL) {
413 dep->de_flag &= ~DE_ACCESS;
414 timespec2fattime(&vap->va_atime, 0,
415 &dep->de_ADate, NULL, NULL);
417 if (vap->va_mtime.tv_sec != VNOVAL) {
418 dep->de_flag &= ~DE_UPDATE;
419 timespec2fattime(&vap->va_mtime, 0,
420 &dep->de_MDate, &dep->de_MTime, NULL);
422 dep->de_Attributes |= ATTR_ARCHIVE;
423 dep->de_flag |= DE_MODIFIED;
427 * DOS files only have the ability to have their writability
428 * attribute set, so we use the owner write bit to set the readonly
431 if (vap->va_mode != (mode_t)VNOVAL) {
432 if (vp->v_mount->mnt_flag & MNT_RDONLY)
434 if (cred->cr_uid != pmp->pm_uid &&
435 (error = caps_priv_check(cred, SYSCAP_NOVFS_SETATTR)))
439 if (vp->v_type != VDIR) {
440 /* We ignore the read and execute bits. */
441 if (vap->va_mode & VWRITE)
442 dep->de_Attributes &= ~ATTR_READONLY;
444 dep->de_Attributes |= ATTR_READONLY;
445 dep->de_Attributes |= ATTR_ARCHIVE;
446 dep->de_flag |= DE_MODIFIED;
449 return (deupdat(dep, 0));
453 msdosfs_read(struct vop_read_args *ap)
469 struct vnode *vp = ap->a_vp;
470 struct denode *dep = VTODE(vp);
471 struct msdosfsmount *pmp = dep->de_pmp;
472 struct uio *uio = ap->a_uio;
474 if (uio->uio_offset < 0)
477 if ((uoff_t)uio->uio_offset > MSDOSFS_FILESIZE_MAX)
480 * If they didn't ask for any data, then we are done.
482 orig_resid = uio->uio_resid;
487 * The caller is supposed to ensure that
488 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
489 * We don't need to check for large offsets as in ffs because
490 * dep->de_FileSize <= MSDOSFS_FILESIZE_MAX < OFF_MAX, so large
491 * offsets cannot cause overflow even in theory.
494 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
496 isadir = dep->de_Attributes & ATTR_DIRECTORY;
498 if (uio->uio_offset >= dep->de_FileSize)
501 cn = de_cluster(pmp, uio->uio_offset);
502 loffset = de_cn2doff(pmp, cn);
504 blsize = pmp->pm_bpcluster;
505 on = uio->uio_offset & pmp->pm_crbomask;
508 * If we are operating on a directory file then be sure to
509 * do i/o with the vnode for the filesystem instead of the
510 * vnode for the directory.
514 /* convert cluster # to block # */
515 error = pcbmap(dep, cn, &lbn, NULL, &blsize);
516 loffset = de_bn2doff(pmp, lbn);
517 if (error == E2BIG) {
522 error = bread(pmp->pm_devvp, loffset, blsize, &bp);
523 } else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
524 error = bread(vp, loffset, blsize, &bp);
525 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
526 error = cluster_readx(vp,
527 ((dep->de_FileSize + blsize - 1) / blsize) * blsize,
528 loffset, blsize, B_NOTMETA, on + uio->uio_resid,
529 seqcount * MAXBSIZE, &bp);
530 bp->b_flags |= B_CLUSTEROK;
531 } else if (seqcount > 1) {
532 off_t raoffset = de_cn2doff(pmp, rablock);
534 error = breadn(vp, loffset,
535 blsize, &raoffset, &rasize, 1, &bp);
537 error = bread(vp, loffset, blsize, &bp);
543 diff = pmp->pm_bpcluster - on;
544 n = szmin(uio->uio_resid, diff);
545 diff = dep->de_FileSize - uio->uio_offset;
548 diff = blsize - bp->b_resid;
551 error = uiomovebp(bp, bp->b_data + on, (size_t)n, uio);
553 } while (error == 0 && uio->uio_resid > 0 && n != 0);
555 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
556 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
557 dep->de_flag |= DE_ACCESS;
562 * Write data to a file or directory.
565 msdosfs_write(struct vop_write_args *ap)
575 struct buf *bp = NULL;
576 int ioflag = ap->a_ioflag;
577 struct uio *uio = ap->a_uio;
578 struct thread *td = uio->uio_td;
579 struct vnode *vp = ap->a_vp;
580 struct vnode *thisvp;
581 struct denode *dep = VTODE(vp);
582 struct msdosfsmount *pmp = dep->de_pmp;
583 struct proc *p = (td ? td->td_proc : NULL);
584 struct lwp *lp = (td ? td->td_lwp : NULL);
586 mprintf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
587 vp, uio, ioflag, ap->a_cred);
588 mprintf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
589 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
591 switch (vp->v_type) {
593 if (ioflag & IO_APPEND)
594 uio->uio_offset = dep->de_FileSize;
600 panic("msdosfs_write(): bad file type");
603 if (uio->uio_offset < 0)
607 * This is needed (unlike in ffs_write()) because we extend the
608 * file outside of the loop but we don't want to extend the file
609 * for writes of 0 bytes.
611 if (uio->uio_resid == 0)
615 * The caller is supposed to ensure that
616 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
618 if ((uoff_t)uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
622 * If they've exceeded their filesize limit, tell them about it.
625 ((uoff_t)uio->uio_offset + uio->uio_resid >
626 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
627 lwpsignal(p, lp, SIGXFSZ);
632 * If the offset we are starting the write at is beyond the end of
633 * the file, then they've done a seek. Unix filesystems allow
634 * files with holes in them, DOS doesn't so we must fill the hole
635 * with zeroed blocks.
637 if (uio->uio_offset > dep->de_FileSize) {
638 error = deextend(dep, uio->uio_offset);
644 * Remember some values in case the write fails.
646 resid = uio->uio_resid;
647 osize = dep->de_FileSize;
650 * If we write beyond the end of the file, extend it to its ultimate
651 * size ahead of the time to hopefully get a contiguous area.
653 if (uio->uio_offset + resid > osize) {
654 count = de_clcount(pmp, uio->uio_offset + resid) -
655 de_clcount(pmp, osize);
656 error = extendfile(dep, count, NULL, NULL, 0);
657 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
659 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
661 lastcn = de_clcount(pmp, osize) - 1;
663 seqcount = ioflag >> IO_SEQSHIFT;
665 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
670 croffset = uio->uio_offset & pmp->pm_crbomask;
671 n = (int)szmin(uio->uio_resid, pmp->pm_bpcluster - croffset);
672 if (uio->uio_offset + n > dep->de_FileSize) {
673 dep->de_FileSize = uio->uio_offset + n;
674 /* The object size needs to be set before buffer is allocated */
675 vnode_pager_setsize(vp, dep->de_FileSize);
679 * If either the whole cluster gets written, or we write
680 * the cluster from its start beyond EOF, then no need to
681 * read data from disk.
683 * If UIO_NOCOPY is set we have to do a read-before-write
684 * to fill in any missing pieces of the buffer since no
685 * actual overwrite will occur.
687 cn = de_cluster(pmp, uio->uio_offset);
688 if ((uio->uio_offset & pmp->pm_crbomask) == 0
689 && uio->uio_segflg != UIO_NOCOPY
690 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
691 > de_cluster(pmp, uio->uio_offset)
692 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
693 bp = getblk(thisvp, de_cn2doff(pmp, cn),
694 pmp->pm_bpcluster, 0, 0);
697 * Do the bmap now, since pcbmap needs buffers
698 * for the FAT table. (see msdosfs_strategy)
700 if (bp->b_bio2.bio_offset == NOOFFSET) {
704 de_cluster(pmp, bp->b_loffset),
705 &dblkno, NULL, NULL);
706 if (error || dblkno == (daddr_t)-1) {
707 bp->b_bio2.bio_offset = NOOFFSET;
709 bp->b_bio2.bio_offset = de_bn2doff(pmp,
713 if (bp->b_bio2.bio_offset == NOOFFSET) {
716 error = EIO; /* XXX */
721 * The block we need to write into exists, so read
724 error = bread(thisvp, de_cn2doff(pmp, cn),
725 pmp->pm_bpcluster, &bp);
733 * Should these vnode_pager_* functions be done on dir
738 * Copy the data from user space into the buf header.
740 error = uiomovebp(bp, bp->b_data + croffset, (size_t)n, uio);
746 /* Prepare for clustered writes in some else clauses. */
747 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
748 bp->b_flags |= B_CLUSTEROK;
751 * If IO_SYNC, then each buffer is written synchronously.
752 * Otherwise, if we have a severe page deficiency then
753 * write the buffer asynchronously. Otherwise, if on a
754 * cluster boundary then write the buffer asynchronously,
755 * combining it with contiguous clusters if permitted and
756 * possible, since we don't expect more writes into this
757 * buffer soon. Otherwise, do a delayed write because we
758 * expect more writes into this buffer soon.
760 if (ioflag & IO_SYNC)
762 else if (vm_paging_severe() || buf_dirty_count_severe())
764 else if (n + croffset == pmp->pm_bpcluster) {
765 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
766 cluster_write(bp, dep->de_FileSize,
767 pmp->pm_bpcluster, seqcount);
772 dep->de_flag |= DE_UPDATE;
773 } while (error == 0 && uio->uio_resid > 0);
776 * If the write failed and they want us to, truncate the file back
777 * to the size it was before the write was attempted.
781 if (ioflag & IO_UNIT) {
782 detrunc(dep, osize, ioflag & IO_SYNC);
783 uio->uio_offset -= resid - uio->uio_resid;
784 uio->uio_resid = resid;
786 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC);
787 if (uio->uio_resid != resid)
790 } else if (ioflag & IO_SYNC)
791 error = deupdat(dep, 1);
796 * Flush the blocks of a file to disk.
799 msdosfs_fsync(struct vop_fsync_args *ap)
801 struct vnode *vp = ap->a_vp;
806 * Flush all dirty buffers associated with a vnode.
811 vfsync(vp, ap->a_waitfor, 0, NULL, NULL);
813 if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
814 vprint("msdosfs_fsync: dirty", vp);
819 * If the syncing request comes from fsync(2), sync the entire
820 * FAT and any other metadata that happens to be on devvp. We
821 * need this mainly for the FAT. We write the FAT sloppily, and
822 * syncing it all now is the best we can easily do to get all
823 * directory entries associated with the file (not just the file)
824 * fully synced. The other metadata includes critical metadata
825 * for all directory entries, but only in the MNT_ASYNC case. We
826 * will soon sync all metadata in the file's directory entry.
827 * Non-critical metadata for associated directory entries only
828 * gets synced accidentally, as in most file systems.
830 if (ap->a_waitfor == MNT_WAIT) {
831 devvp = VTODE(ap->a_vp)->de_pmp->pm_devvp;
832 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
833 allerror = VOP_FSYNC(devvp, MNT_WAIT, 0);
838 error = deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT);
845 msdosfs_remove(struct vop_old_remove_args *ap)
847 struct denode *dep = VTODE(ap->a_vp);
848 struct denode *ddep = VTODE(ap->a_dvp);
851 if (ap->a_vp->v_type == VDIR)
854 error = removede(ddep, dep);
855 mprintf("msdosfs_remove(), dep %p, v_refcnt 0x%08x\n",
856 dep, ap->a_vp->v_refcnt);
861 * DOS filesystems don't know what links are.
864 msdosfs_link(struct vop_old_link_args *ap)
870 * Renames on files require moving the denode to a new hash queue since the
871 * denode's location is used to compute which hash queue to put the file
872 * in. Unless it is a rename in place. For example "mv a b".
874 * What follows is the basic algorithm:
877 * if (dest file exists) {
880 * if (dest and src in same directory) {
881 * rewrite name in existing directory slot
883 * write new entry in dest directory
884 * update offset and dirclust in denode
885 * move denode to new hash chain
886 * clear old directory entry
890 * if (dest directory exists) {
891 * if (dest is not empty) {
894 * remove dest directory
896 * if (dest and src in same directory) {
897 * rewrite name in existing entry
899 * be sure dest is not a child of src directory
900 * write entry in dest directory
901 * update "." and ".." in moved directory
902 * clear old directory entry for moved directory
907 * source's parent directory is unlocked
908 * source file or directory is unlocked
909 * destination's parent directory is locked
910 * destination file or directory is locked if it exists
913 * all denodes should be released
916 msdosfs_rename(struct vop_old_rename_args *ap)
918 struct vnode *tdvp = ap->a_tdvp;
919 struct vnode *fvp = ap->a_fvp;
920 struct vnode *fdvp = ap->a_fdvp;
921 struct vnode *tvp = ap->a_tvp;
922 struct componentname *tcnp = ap->a_tcnp;
923 struct componentname *fcnp = ap->a_fcnp;
924 struct denode *ip, *xp, *dp, *zp;
925 u_char toname[12], oldname[11];
926 u_long from_diroffset, to_diroffset;
928 int doingdirectory = 0, newparent = 0;
932 struct msdosfsmount *pmp;
933 struct direntry *dotdotp;
934 struct buf *bp = NULL;
936 pmp = VFSTOMSDOSFS(fdvp->v_mount);
939 * Check for cross-device rename.
941 if (fvp->v_mount != tdvp->v_mount ||
942 (tvp && fvp->v_mount != tvp->v_mount)) {
957 * If source and dest are the same, do nothing.
965 * fvp is unlocked, fdvp, tvp, tdvp are locked. Lock fvp and note
966 * that we have to unlock it to use the abortit target.
968 error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
975 * Be sure we are not renaming ".", "..", or an alias of ".". This
976 * leads to a crippled directory tree. It's pretty tough to do a
977 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
978 * doesn't work if the ".." entry is missing.
980 if (ip->de_Attributes & ATTR_DIRECTORY) {
982 * Avoid ".", "..", and aliases of "." for obvious reasons.
984 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
986 (fcnp->cn_flags & CNP_ISDOTDOT) ||
987 (tcnp->cn_flags & CNP_ISDOTDOT) ||
988 (ip->de_flag & DE_RENAME)) {
993 ip->de_flag |= DE_RENAME;
998 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only
999 * set if doingdirectory. We will get fvp unlocked in fairly
1000 * short order. dp and xp must be setup and fvp must be unlocked
1001 * for the out and bad targets to work properly.
1004 xp = tvp ? VTODE(tvp) : NULL;
1007 * Remember direntry place to use for destination
1009 to_diroffset = dp->de_fndoffset;
1010 to_count = dp->de_fndcnt;
1013 * If ".." must be changed (ie the directory gets a new
1014 * parent) then the source directory must not be in the
1015 * directory hierarchy above the target, as this would
1016 * orphan everything below the source directory. Also
1017 * the user must have write permission in the source so
1018 * as to be able to change "..". We must repeat the call
1019 * to namei, as the parent directory is unlocked by the
1020 * call to doscheckpath().
1022 error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
1024 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1028 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1029 * DE_RENAME only set if doingdirectory.
1031 if (doingdirectory && newparent) {
1032 if (error) /* write access check above */
1039 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1040 * get an extra ref so we wind up with just an unlocked, ref'd
1041 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp
1042 * isn't locked so we can't use the out target).
1045 error = doscheckpath(ip, dp);
1046 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1052 * relookup no longer messes with the ref count. tdvp must
1053 * be unlocked on entry and on success will be locked on
1056 error = relookup(tdvp, &tvp, tcnp);
1058 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1066 * tvp and tdvp are now locked again.
1069 xp = tvp ? VTODE(tvp) : NULL;
1073 * tvp and tdvp are now locked again, the 'bad' target can be used
1074 * to clean them up again. Delete an existant target and clean
1075 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up.
1079 * Target must be empty if a directory and have no links
1080 * to it. Also, ensure source and target are compatible
1081 * (both directories, or both not directories).
1083 if (xp->de_Attributes & ATTR_DIRECTORY) {
1084 if (!dosdirempty(xp)) {
1088 if (!doingdirectory) {
1092 } else if (doingdirectory) {
1096 error = removede(dp, xp);
1105 * Convert the filename in tcnp into a dos filename. We copy this
1106 * into the denode and directory entry for the destination
1109 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1114 * Since from wasn't locked at various places above, we have to do
1115 * a relookup here. If the target and source are the same directory
1116 * we have to unlock the target directory in order to safely relookup
1117 * the source, because relookup expects its directory to be unlocked.
1119 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must
1120 * now take that into account.
1122 * The tdvp locking issues make this a real mess.
1124 fcnp->cn_flags &= ~CNP_MODMASK;
1125 fcnp->cn_flags |= CNP_LOCKPARENT;
1128 error = relookup(fdvp, &fvp, fcnp);
1129 if (error || fvp == NULL) {
1131 * From name has disappeared. Note: fdvp might == tdvp.
1133 * DE_RENAME is only set if doingdirectory.
1136 panic("rename: lost dir entry");
1137 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1150 * No error occured. tdvp, fdvp and fvp are all locked. If
1151 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned
1152 * up. ap->a_fvp is still refd.
1156 from_diroffset = zp->de_fndoffset;
1159 * Ensure that the directory entry still exists and has not
1160 * changed till now. If the source is a file the entry may
1161 * have been unlinked or renamed. In either case there is
1162 * no further work to be done. If the source is a directory
1163 * then it cannot have been rmdir'ed or renamed; this is
1164 * prohibited by the DE_RENAME flag.
1166 * DE_RENAME is only set if doingdirectory.
1170 panic("rename: lost dir entry");
1173 u_long new_dirclust;
1174 u_long new_diroffset;
1177 * First write a new entry in the destination
1178 * directory and mark the entry in the source directory
1179 * as deleted. Then move the denode to the correct hash
1180 * chain for its new location in the filesystem. And, if
1181 * we moved a directory, then update its .. entry to point
1182 * to the new parent directory.
1184 memcpy(oldname, ip->de_Name, 11);
1185 memcpy(ip->de_Name, toname, 11); /* update denode */
1186 dp->de_fndoffset = to_diroffset;
1187 dp->de_fndcnt = to_count;
1188 error = createde(ip, dp, NULL, tcnp);
1190 memcpy(ip->de_Name, oldname, 11);
1194 * If ip is for a directory, then its name should always
1195 * be "." since it is for the directory entry in the
1196 * directory itself (msdosfs_lookup() always translates
1197 * to the "." entry so as to get a unique denode, except
1198 * for the root directory there are different
1199 * complications). However, we just corrupted its name
1200 * to pass the correct name to createde(). Undo this.
1202 if ((ip->de_Attributes & ATTR_DIRECTORY) != 0)
1203 memcpy(ip->de_Name, oldname, 11);
1205 zp->de_fndoffset = from_diroffset;
1206 error = removede(zp, ip);
1208 /* XXX should downgrade to ro here, fs is corrupt */
1211 if (!doingdirectory) {
1212 error = pcbmap(dp, de_cluster(pmp, to_diroffset),
1213 NULL, &new_dirclust, NULL);
1215 /* XXX should downgrade to ro here, fs is corrupt */
1218 if (new_dirclust == MSDOSFSROOT)
1219 new_diroffset = to_diroffset;
1221 new_diroffset = to_diroffset & pmp->pm_crbomask;
1222 msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1227 * If we moved a directory to a new parent directory, then we must
1228 * fixup the ".." entry in the moved directory.
1230 if (doingdirectory && newparent) {
1231 cn = ip->de_StartCluster;
1232 if (cn == MSDOSFSROOT) {
1233 /* this should never happen */
1234 panic("msdosfs_rename(): updating .. in root directory?");
1236 bn = cntobn(pmp, cn);
1238 error = bread(pmp->pm_devvp, de_bn2doff(pmp, bn),
1239 pmp->pm_bpcluster, &bp);
1241 /* XXX should downgrade to ro here, fs is corrupt */
1245 dotdotp = (struct direntry *)bp->b_data + 1;
1246 pcl = dp->de_StartCluster;
1247 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1249 putushort(dotdotp->deStartCluster, pcl);
1251 putushort(dotdotp->deHighClust, pcl >> 16);
1252 if (DOINGASYNC(fvp))
1254 else if ((error = bwrite(bp)) != 0) {
1255 /* XXX should downgrade to ro here, fs is corrupt */
1261 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd
1265 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */
1276 * 'bad' target: xp governs tvp. tvp and tdvp are locked, fdvp and fvp
1277 * are not locked. ip points to fvp's inode which may have DE_RENAME
1286 * 'out' target: tvp and tdvp have already been cleaned up.
1289 ip->de_flag &= ~DE_RENAME;
1297 struct direntry dot;
1298 struct direntry dotdot;
1299 } dosdirtemplate = {
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 */
1310 { ".. ", /* the .. entry */
1311 ATTR_DIRECTORY, /* file attribute */
1313 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1314 { 0, 0 }, /* access date */
1315 { 0, 0 }, /* high bits of start cluster */
1316 { 210, 4 }, { 210, 4 }, /* modify time & date */
1317 { 0, 0 }, /* startcluster */
1318 { 0, 0, 0, 0 } /* filesize */
1323 msdosfs_mkdir(struct vop_old_mkdir_args *ap)
1325 struct componentname *cnp = ap->a_cnp;
1327 struct denode *pdep = VTODE(ap->a_dvp);
1328 struct direntry *denp;
1329 struct msdosfsmount *pmp = pdep->de_pmp;
1331 u_long newcluster, pcl;
1334 struct denode ndirent;
1338 * If this is the root directory and there is no space left we
1339 * can't do anything. This is because the root directory can not
1342 if (pdep->de_StartCluster == MSDOSFSROOT
1343 && pdep->de_fndoffset >= pdep->de_FileSize) {
1349 * Allocate a cluster to hold the about to be created directory.
1351 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1355 memset(&ndirent, 0, sizeof(ndirent));
1356 ndirent.de_pmp = pmp;
1357 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1359 DETIMES(&ndirent, &ts, &ts, &ts);
1362 * Now fill the cluster with the "." and ".." entries. And write
1363 * the cluster to disk. This way it is there for the parent
1364 * directory to be pointing at if there were a crash.
1366 bn = cntobn(pmp, newcluster);
1367 /* always succeeds */
1368 bp = getblk(pmp->pm_devvp, de_bn2doff(pmp, bn),
1369 pmp->pm_bpcluster, 0, 0);
1370 memset(bp->b_data, 0, pmp->pm_bpcluster);
1371 memcpy(bp->b_data, &dosdirtemplate, sizeof(dosdirtemplate));
1372 denp = (struct direntry *)bp->b_data;
1373 putushort(denp[0].deStartCluster, newcluster);
1374 putushort(denp[0].deCDate, ndirent.de_CDate);
1375 putushort(denp[0].deCTime, ndirent.de_CTime);
1376 denp[0].deCHundredth = ndirent.de_CHun;
1377 putushort(denp[0].deADate, ndirent.de_ADate);
1378 putushort(denp[0].deMDate, ndirent.de_MDate);
1379 putushort(denp[0].deMTime, ndirent.de_MTime);
1380 pcl = pdep->de_StartCluster;
1382 * Although the root directory has a non-magic starting cluster
1383 * number for FAT32, chkdsk and fsck_msdosfs still require
1384 * references to it in dotdot entries to be magic.
1386 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1388 putushort(denp[1].deStartCluster, pcl);
1389 putushort(denp[1].deCDate, ndirent.de_CDate);
1390 putushort(denp[1].deCTime, ndirent.de_CTime);
1391 denp[1].deCHundredth = ndirent.de_CHun;
1392 putushort(denp[1].deADate, ndirent.de_ADate);
1393 putushort(denp[1].deMDate, ndirent.de_MDate);
1394 putushort(denp[1].deMTime, ndirent.de_MTime);
1396 putushort(denp[0].deHighClust, newcluster >> 16);
1397 putushort(denp[1].deHighClust, pcl >> 16);
1400 if (DOINGASYNC(ap->a_dvp))
1402 else if ((error = bwrite(bp)) != 0)
1406 * Now build up a directory entry pointing to the newly allocated
1407 * cluster. This will be written to an empty slot in the parent
1410 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1414 ndirent.de_Attributes = ATTR_DIRECTORY;
1415 ndirent.de_LowerCase = 0;
1416 ndirent.de_StartCluster = newcluster;
1417 ndirent.de_FileSize = 0;
1418 ndirent.de_dev = pdep->de_dev;
1419 ndirent.de_devvp = pdep->de_devvp;
1420 error = createde(&ndirent, pdep, &dep, cnp);
1423 *ap->a_vpp = DETOV(dep);
1427 clusterfree(pmp, newcluster);
1433 msdosfs_rmdir(struct vop_old_rmdir_args *ap)
1435 struct vnode *vp = ap->a_vp;
1436 struct vnode *dvp = ap->a_dvp;
1437 struct denode *ip, *dp;
1444 * Verify the directory is empty (and valid).
1445 * (Rmdir ".." won't be valid since
1446 * ".." will contain a reference to
1447 * the current directory and thus be
1451 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1456 * Delete the entry from the directory. For dos filesystems this
1457 * gets rid of the directory entry on disk, the in memory copy
1458 * still exists but the de_refcnt is <= 0. This prevents it from
1459 * being found by deget(). When the vput() on dep is done we give
1460 * up access and eventually msdosfs_reclaim() will be called which
1461 * will remove it from the denode cache.
1463 error = removede(dp, ip);
1467 * Truncate the directory that is being deleted.
1469 error = detrunc(ip, (u_long)0, IO_SYNC);
1475 * DOS filesystems don't know what symlinks are.
1478 msdosfs_symlink(struct vop_old_symlink_args *ap)
1480 return (EOPNOTSUPP);
1484 msdosfs_readdir(struct vop_readdir_args *ap)
1496 struct buf *bp = NULL;
1498 struct msdosfsmount *pmp;
1499 struct direntry *dentp;
1500 struct dirent dirbuf;
1501 struct uio *uio = ap->a_uio;
1502 off_t *cookies = NULL;
1507 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
1511 dep = VTODE(ap->a_vp);
1514 mprintf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1515 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1518 * msdosfs_readdir() won't operate properly on regular files since
1519 * it does i/o only with the filesystem vnode, and hence can
1520 * retrieve the wrong block from the buffer cache for a plain file.
1521 * So, fail attempts to readdir() on a plain file.
1523 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
1529 * To be safe, initialize dirbuf
1531 memset(dirbuf.d_name, 0, sizeof(dirbuf.d_name));
1534 * If the user buffer is smaller than the size of one dos directory
1535 * entry or the file offset is not a multiple of the size of a
1536 * directory entry, then we fail the read.
1538 off = offset = uio->uio_offset;
1539 if (uio->uio_resid < sizeof(struct direntry) ||
1540 (offset & (sizeof(struct direntry) - 1))) {
1545 if (ap->a_ncookies) {
1546 ncookies = uio->uio_resid / 16 + 1;
1547 if (ncookies > 1024)
1549 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1550 *ap->a_cookies = cookies;
1551 *ap->a_ncookies = ncookies;
1554 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1556 #define d_fileno d_ino
1558 * If they are reading from the root directory then, we simulate
1559 * the . and .. entries since these don't exist in the root
1560 * directory. We also set the offset bias to make up for having to
1561 * simulate these entries. By this I mean that at file offset 64 we
1562 * read the first entry in the root directory that lives on disk.
1564 if (dep->de_StartCluster == MSDOSFSROOT
1565 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1567 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1570 bias = 2 * sizeof(struct direntry);
1571 if (offset < bias) {
1572 for (n = (int)offset / sizeof(struct direntry);
1574 dirbuf.d_fileno = FAT32(pmp) ?
1575 (uint64_t)cntobn(pmp, pmp->pm_rootdirblk) *
1577 dirbuf.d_type = DT_DIR;
1580 dirbuf.d_namlen = 1;
1581 dirbuf.d_name[0] = '.';
1584 dirbuf.d_namlen = 2;
1585 dirbuf.d_name[0] = '.';
1586 dirbuf.d_name[1] = '.';
1589 if (vop_write_dirent(&error, uio,
1590 dirbuf.d_fileno, dirbuf.d_type,
1591 dirbuf.d_namlen, dirbuf.d_name))
1595 offset += sizeof(struct direntry);
1598 *cookies++ = offset;
1599 if (--ncookies <= 0)
1609 while (uio->uio_resid > 0) {
1610 lbn = de_cluster(pmp, offset - bias);
1611 on = (offset - bias) & pmp->pm_crbomask;
1612 n = szmin(pmp->pm_bpcluster - on, uio->uio_resid);
1613 diff = dep->de_FileSize - (offset - bias);
1617 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1620 error = bread(pmp->pm_devvp, de_bn2doff(pmp, bn), blsize, &bp);
1625 n = min(n, blsize - bp->b_resid);
1633 * Convert from dos directory entries to fs-independent
1634 * directory entries.
1636 for (dentp = (struct direntry *)(bp->b_data + on);
1637 (char *)dentp < bp->b_data + on + n;
1638 dentp++, offset += sizeof(struct direntry)) {
1640 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1641 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1644 * If this is an unused entry, we can stop.
1646 if (dentp->deName[0] == SLOT_EMPTY) {
1651 * Skip deleted entries.
1653 if (dentp->deName[0] == SLOT_DELETED) {
1660 * Handle Win95 long directory entries
1662 if (dentp->deAttributes == ATTR_WIN95) {
1663 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1665 chksum = win2unixfn(&nb,
1666 (struct winentry *)dentp, chksum, pmp);
1671 * Skip volume labels
1673 if (dentp->deAttributes & ATTR_VOLUME) {
1679 * This computation of d_fileno must match
1680 * the computation of va_fileid in
1683 if (dentp->deAttributes & ATTR_DIRECTORY) {
1684 cn = getushort(dentp->deStartCluster);
1686 cn |= getushort(dentp->deHighClust) <<
1688 if (cn == MSDOSFSROOT)
1689 cn = pmp->pm_rootdirblk;
1691 if (cn == MSDOSFSROOT && !FAT32(pmp))
1692 dirbuf.d_fileno = 1;
1694 dirbuf.d_fileno = cntobn(pmp, cn) *
1696 dirbuf.d_type = DT_DIR;
1698 dirbuf.d_fileno = (uoff_t)offset /
1699 sizeof(struct direntry);
1700 dirbuf.d_type = DT_REG;
1703 if (chksum != winChksum(dentp->deName)) {
1704 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1705 (u_char *)dirbuf.d_name,
1706 dentp->deLowerCase |
1707 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1708 (LCASE_BASE | LCASE_EXT) : 0),
1712 mbnambuf_flush(&nb, &dirbuf);
1714 if (vop_write_dirent(&error, uio, dirbuf.d_fileno,
1715 dirbuf.d_type, dirbuf.d_namlen, dirbuf.d_name)) {
1725 *cookies++ = offset + sizeof(struct direntry);
1726 if (--ncookies <= 0) {
1731 off = offset + sizeof(struct direntry);
1736 /* Subtract unused cookies */
1738 *ap->a_ncookies -= ncookies;
1740 uio->uio_offset = off;
1743 * Set the eofflag (NFS uses it)
1745 if (ap->a_eofflag) {
1746 if (dep->de_FileSize - (offset - bias) <= 0)
1752 vn_unlock(ap->a_vp);
1757 * a_vp - pointer to the file's vnode
1758 * a_runp - where to return the "run past" a_bn. This is the count of logical
1759 * blocks whose physical blocks (together with a_bn's physical block)
1761 * a_runb - where to return the "run before" a_bn.
1764 msdosfs_bmap(struct vop_bmap_args *ap)
1766 struct fatcache savefc;
1769 struct msdosfsmount *pmp;
1773 int bnpercn, error, maxio, maxrun, run;
1779 if (ap->a_doffsetp == NULL)
1781 if (ap->a_runp != NULL)
1783 if (ap->a_runb != NULL)
1786 KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0);
1787 error = pcbmap(dep, de_cluster(pmp, ap->a_loffset), &dbn, NULL, NULL);
1788 if (error || dbn == (daddr_t)-1)
1789 *ap->a_doffsetp = NOOFFSET;
1791 *ap->a_doffsetp = de_bn2doff(pmp, dbn);
1792 if (error != 0 || dbn == (daddr_t)-1 ||
1793 (ap->a_runp == NULL && ap->a_runb == NULL))
1797 * Prepare to back out updates of the fatchain cache after the one
1798 * for the first block done by pcbmap() above. Without the backout,
1799 * then whenever the caller doesn't do i/o to all of the blocks that
1800 * we find, the single useful cache entry would be too far in advance
1801 * of the actual i/o to work for the next sequential i/o. Then the
1802 * FAT would be searched from the beginning. With the backout, the
1803 * FAT is searched starting at most a few blocks early. This wastes
1804 * much less time. Time is also wasted finding more blocks than the
1805 * caller will do i/o to. This is necessary because the runlength
1806 * parameters are output-only.
1808 savefc = dep->de_fc[FC_LASTMAP];
1810 cn = de_cluster(pmp, ap->a_loffset);
1812 maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1813 bnpercn = de_cn2bn(pmp, 1);
1814 if (ap->a_runp != NULL) {
1815 maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1816 for (run = 1; run <= maxrun; run++) {
1817 if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1818 runbn != dbn + run * bnpercn)
1821 *ap->a_runp = run - 1;
1823 if (ap->a_runb != NULL) {
1824 maxrun = ulmin(maxio - 1, cn);
1825 for (run = 1; run < maxrun; run++) {
1826 if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1827 runbn != dbn - run * bnpercn)
1830 *ap->a_runb = run - 1;
1832 dep->de_fc[FC_LASTMAP] = savefc;
1838 msdosfs_strategy(struct vop_strategy_args *ap)
1840 struct bio *bio = ap->a_bio;
1842 struct buf *bp = bio->bio_buf;
1843 struct vnode *vp = ap->a_vp;
1844 struct denode *dep = VTODE(vp);
1845 struct msdosfsmount *pmp = dep->de_pmp;
1849 if (vp->v_type == VBLK || vp->v_type == VCHR)
1850 panic("msdosfs_strategy: spec");
1852 * If we don't already know the filesystem relative block number
1853 * then get it using pcbmap(). If pcbmap() returns the block
1854 * number as -1 then we've got a hole in the file. DOS filesystems
1855 * don't allow files with holes, so we shouldn't ever see this.
1857 nbio = push_bio(bio);
1858 if (nbio->bio_offset == NOOFFSET) {
1859 error = pcbmap(dep, de_cluster(pmp, bio->bio_offset),
1860 &dblkno, NULL, NULL);
1862 bp->b_error = error;
1863 bp->b_flags |= B_ERROR;
1864 /* I/O was never started on nbio, must biodone(bio) */
1868 if (dblkno == (daddr_t)-1) {
1869 nbio->bio_offset = NOOFFSET;
1872 nbio->bio_offset = de_bn2doff(pmp, dblkno);
1875 if (nbio->bio_offset == NOOFFSET) {
1876 /* I/O was never started on nbio, must biodone(bio) */
1881 * Read/write the block from/to the disk that contains the desired
1884 vn_strategy(dep->de_devvp, nbio);
1889 msdosfs_print(struct vop_print_args *ap)
1891 struct denode *dep = VTODE(ap->a_vp);
1893 kprintf("tag VT_MSDOSFS, startcluster %lu, dircluster %lu, "
1894 "diroffset %lu, dev %d, %d",
1895 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset,
1896 major(dep->de_dev), minor(dep->de_dev));
1897 lockmgr_printinfo(&ap->a_vp->v_lock);
1902 msdosfs_pathconf(struct vop_pathconf_args *ap)
1904 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1906 switch (ap->a_name) {
1907 case _PC_FILESIZEBITS:
1908 *ap->a_retval = FAT12(pmp) ? 32 : 33;
1915 pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1917 case _PC_CHOWN_RESTRICTED:
1924 return (vop_stdpathconf(ap));
1929 /* Global vfs data structures for msdosfs */
1930 struct vop_ops msdosfs_vnode_vops = {
1931 .vop_default = vop_defaultop,
1932 .vop_access = msdosfs_access,
1933 .vop_bmap = msdosfs_bmap,
1934 .vop_getpages = vop_stdgetpages,
1935 .vop_putpages = vop_stdputpages,
1936 .vop_open = msdosfs_open,
1937 .vop_close = msdosfs_close,
1938 .vop_old_create = msdosfs_create,
1939 .vop_fsync = msdosfs_fsync,
1940 .vop_getattr = msdosfs_getattr,
1941 .vop_inactive = msdosfs_inactive,
1942 .vop_old_link = msdosfs_link,
1943 .vop_old_lookup = msdosfs_lookup,
1944 .vop_old_mkdir = msdosfs_mkdir,
1945 .vop_old_mknod = msdosfs_mknod,
1946 .vop_pathconf = msdosfs_pathconf,
1947 .vop_print = msdosfs_print,
1948 .vop_read = msdosfs_read,
1949 .vop_readdir = msdosfs_readdir,
1950 .vop_reclaim = msdosfs_reclaim,
1951 .vop_old_remove = msdosfs_remove,
1952 .vop_old_rename = msdosfs_rename,
1953 .vop_old_rmdir = msdosfs_rmdir,
1954 .vop_setattr = msdosfs_setattr,
1955 .vop_strategy = msdosfs_strategy,
1956 .vop_old_symlink = msdosfs_symlink,
1957 .vop_write = msdosfs_write,