1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vnops.c,v 1.95.2.4 2003/06/13 15:05:47 trhodes Exp $ */
2 /* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
43 * The author supplies this software to be publicly redistributed on the
44 * understanding that the author is not responsible for the correct
45 * functioning of this software in any circumstances and is not liable for
46 * any damages caused by this software.
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
54 #include <sys/kernel.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/unistd.h>
62 #include <sys/vnode.h>
63 #include <sys/malloc.h>
64 #include <sys/dirent.h>
65 #include <sys/signalvar.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_zone.h>
70 #include <vm/vnode_pager.h>
74 #include <machine/inttypes.h>
79 #include "msdosfsmount.h"
82 #define DOS_FILESIZE_MAX 0xffffffff
85 * Prototypes for MSDOSFS vnode operations
87 static int msdosfs_create (struct vop_old_create_args *);
88 static int msdosfs_mknod (struct vop_old_mknod_args *);
89 static int msdosfs_open (struct vop_open_args *);
90 static int msdosfs_close (struct vop_close_args *);
91 static int msdosfs_access (struct vop_access_args *);
92 static int msdosfs_getattr (struct vop_getattr_args *);
93 static int msdosfs_setattr (struct vop_setattr_args *);
94 static int msdosfs_read (struct vop_read_args *);
95 static int msdosfs_write (struct vop_write_args *);
96 static int msdosfs_fsync (struct vop_fsync_args *);
97 static int msdosfs_remove (struct vop_old_remove_args *);
98 static int msdosfs_link (struct vop_old_link_args *);
99 static int msdosfs_rename (struct vop_old_rename_args *);
100 static int msdosfs_mkdir (struct vop_old_mkdir_args *);
101 static int msdosfs_rmdir (struct vop_old_rmdir_args *);
102 static int msdosfs_symlink (struct vop_old_symlink_args *);
103 static int msdosfs_readdir (struct vop_readdir_args *);
104 static int msdosfs_bmap (struct vop_bmap_args *);
105 static int msdosfs_strategy (struct vop_strategy_args *);
106 static int msdosfs_print (struct vop_print_args *);
107 static int msdosfs_pathconf (struct vop_pathconf_args *ap);
110 * Some general notes:
112 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
113 * read/written using the vnode for the filesystem. Blocks that represent
114 * the contents of a file are read/written using the vnode for the file
115 * (including directories when they are read/written as files). This
116 * presents problems for the dos filesystem because data that should be in
117 * an inode (if dos had them) resides in the directory itself. Since we
118 * must update directory entries without the benefit of having the vnode
119 * for the directory we must use the vnode for the filesystem. This means
120 * that when a directory is actually read/written (via read, write, or
121 * readdir, or seek) we must use the vnode for the filesystem instead of
122 * the vnode for the directory as would happen in ufs. This is to insure we
123 * retreive the correct block from the buffer cache since the hash value is
124 * based upon the vnode address and the desired block number.
128 * Create a regular file. On entry the directory to contain the file being
129 * created is locked. We must release before we return.
131 * msdosfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
132 * struct componentname *a_cnp, struct vattr *a_vap)
135 msdosfs_create(struct vop_old_create_args *ap)
137 struct componentname *cnp = ap->a_cnp;
138 struct denode ndirent;
140 struct denode *pdep = VTODE(ap->a_dvp);
145 kprintf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
149 * If this is the root directory and there is no space left we
150 * can't do anything. This is because the root directory can not
153 if (pdep->de_StartCluster == MSDOSFSROOT
154 && pdep->de_fndoffset >= pdep->de_FileSize) {
160 * Create a directory entry for the file, then call createde() to
161 * have it installed. NOTE: DOS files are always executable. We
162 * use the absence of the owner write bit to make the file
165 bzero(&ndirent, sizeof(ndirent));
166 error = uniqdosname(pdep, cnp, ndirent.de_Name);
170 ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
171 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
172 ndirent.de_LowerCase = 0;
173 ndirent.de_StartCluster = 0;
174 ndirent.de_FileSize = 0;
175 ndirent.de_dev = pdep->de_dev;
176 ndirent.de_devvp = pdep->de_devvp;
177 ndirent.de_pmp = pdep->de_pmp;
178 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
180 DETIMES(&ndirent, &ts, &ts, &ts);
181 error = createde(&ndirent, pdep, &dep, cnp);
184 *ap->a_vpp = DETOV(dep);
192 * msdosfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
193 * struct componentname *a_cnp, struct vattr *a_vap)
196 msdosfs_mknod(struct vop_old_mknod_args *ap)
198 switch (ap->a_vap->va_type) {
200 return (msdosfs_mkdir((struct vop_old_mkdir_args *)ap));
204 return (msdosfs_create((struct vop_old_create_args *)ap));
214 * msdosfs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
218 msdosfs_open(struct vop_open_args *ap)
220 return(vop_stdopen(ap));
224 * msdosfs_close(struct vnode *a_vp, int a_fflag)
227 msdosfs_close(struct vop_close_args *ap)
229 struct vnode *vp = ap->a_vp;
230 struct denode *dep = VTODE(vp);
233 if (VREFCNT(vp) > 1) {
235 DETIMES(dep, &ts, &ts, &ts);
237 return (vop_stdclose(ap));
241 * msdosfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred)
244 msdosfs_access(struct vop_access_args *ap)
246 struct denode *dep = VTODE(ap->a_vp);
247 struct msdosfsmount *pmp = dep->de_pmp;
250 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
251 ((dep->de_Attributes & ATTR_READONLY) ?
252 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
253 file_mode &= pmp->pm_mask;
255 return (vop_helper_access(ap, pmp->pm_uid, pmp->pm_gid, file_mode, 0));
259 * msdosfs_getattr(struct vnode *a_vp, struct vattr *a_vap)
262 msdosfs_getattr(struct vop_getattr_args *ap)
264 struct denode *dep = VTODE(ap->a_vp);
265 struct msdosfsmount *pmp = dep->de_pmp;
266 struct vattr *vap = ap->a_vap;
269 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
273 DETIMES(dep, &ts, &ts, &ts);
274 vap->va_fsid = dev2udev(dep->de_dev);
276 * The following computation of the fileid must be the same as that
277 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
280 if (dep->de_Attributes & ATTR_DIRECTORY) {
281 fileid = xcntobn(pmp, dep->de_StartCluster) * dirsperblk;
282 if (dep->de_StartCluster == MSDOSFSROOT)
285 fileid = xcntobn(pmp, dep->de_dirclust) * dirsperblk;
286 if (dep->de_dirclust == MSDOSFSROOT)
287 fileid = roottobn(pmp, 0) * dirsperblk;
288 fileid += dep->de_diroffset / sizeof(struct direntry);
290 vap->va_fileid = fileid;
291 if ((dep->de_Attributes & ATTR_READONLY) == 0)
292 mode = S_IRWXU|S_IRWXG|S_IRWXO;
294 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
295 vap->va_mode = mode & pmp->pm_mask;
296 vap->va_uid = pmp->pm_uid;
297 vap->va_gid = pmp->pm_gid;
299 vap->va_rmajor = VNOVAL;
300 vap->va_rminor = VNOVAL;
301 vap->va_size = dep->de_FileSize;
302 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
303 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
304 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
305 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
307 vap->va_atime = vap->va_mtime;
308 vap->va_ctime = vap->va_mtime;
311 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
312 vap->va_flags |= SF_ARCHIVED;
314 vap->va_blocksize = pmp->pm_bpcluster;
316 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
317 vap->va_type = ap->a_vp->v_type;
318 vap->va_filerev = dep->de_modrev;
323 * msdosfs_setattr(struct vnode *a_vp, struct vattr *a_vap,
324 * struct ucred *a_cred)
327 msdosfs_setattr(struct vop_setattr_args *ap)
329 struct vnode *vp = ap->a_vp;
330 struct denode *dep = VTODE(ap->a_vp);
331 struct msdosfsmount *pmp = dep->de_pmp;
332 struct vattr *vap = ap->a_vap;
333 struct ucred *cred = ap->a_cred;
337 kprintf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
338 ap->a_vp, vap, cred);
342 * Check for unsettable attributes.
344 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
345 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
346 (vap->va_blocksize != VNOVAL) || (vap->va_rmajor != VNOVAL) ||
347 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
349 kprintf("msdosfs_setattr(): returning EINVAL\n");
350 kprintf(" va_type %u, va_nlink %"PRIx64", va_fsid %x, va_fileid %"PRIx64"\n",
351 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
352 kprintf(" va_blocksize %lx, va_rmajor %x, va_bytes %"PRIx64", va_gen %"PRIx64"\n",
353 vap->va_blocksize, vap->va_rmajor, vap->va_bytes, vap->va_gen);
354 kprintf(" va_uid %x, va_gid %x\n",
355 vap->va_uid, vap->va_gid);
359 if (vap->va_flags != VNOVAL) {
360 if (vp->v_mount->mnt_flag & MNT_RDONLY)
362 if (cred->cr_uid != pmp->pm_uid &&
363 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
366 * We are very inconsistent about handling unsupported
367 * attributes. We ignored the access time and the
368 * read and execute bits. We were strict for the other
371 * Here we are strict, stricter than ufs in not allowing
372 * users to attempt to set SF_SETTABLE bits or anyone to
373 * set unsupported bits. However, we ignore attempts to
374 * set ATTR_ARCHIVE for directories `cp -pr' from a more
375 * sensible file system attempts it a lot.
377 if (cred->cr_uid != 0) {
378 if (vap->va_flags & SF_SETTABLE)
381 if (vap->va_flags & ~SF_ARCHIVED)
383 if (vap->va_flags & SF_ARCHIVED)
384 dep->de_Attributes &= ~ATTR_ARCHIVE;
385 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
386 dep->de_Attributes |= ATTR_ARCHIVE;
387 dep->de_flag |= DE_MODIFIED;
390 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
394 if (vp->v_mount->mnt_flag & MNT_RDONLY)
397 if (uid == (uid_t)VNOVAL)
400 if (gid == (gid_t)VNOVAL)
402 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
403 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
404 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
406 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
410 if (vap->va_size != VNOVAL) {
412 * Disallow write attempts on read-only file systems;
413 * unless the file is a socket, fifo, or a block or
414 * character device resident on the file system.
416 switch (vp->v_type) {
422 if (vp->v_mount->mnt_flag & MNT_RDONLY)
428 error = detrunc(dep, vap->va_size, 0);
432 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
433 if (vp->v_mount->mnt_flag & MNT_RDONLY)
435 if (cred->cr_uid != pmp->pm_uid &&
436 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)) &&
437 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
438 (error = VOP_EACCESS(ap->a_vp, VWRITE, cred))))
440 if (vp->v_type != VDIR) {
441 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
442 vap->va_atime.tv_sec != VNOVAL) {
443 dep->de_flag &= ~DE_ACCESS;
444 unix2dostime(&vap->va_atime, &dep->de_ADate,
447 if (vap->va_mtime.tv_sec != VNOVAL) {
448 dep->de_flag &= ~DE_UPDATE;
449 unix2dostime(&vap->va_mtime, &dep->de_MDate,
450 &dep->de_MTime, NULL);
452 dep->de_Attributes |= ATTR_ARCHIVE;
453 dep->de_flag |= DE_MODIFIED;
457 * DOS files only have the ability to have their writability
458 * attribute set, so we use the owner write bit to set the readonly
461 if (vap->va_mode != (mode_t)VNOVAL) {
462 if (vp->v_mount->mnt_flag & MNT_RDONLY)
464 if (cred->cr_uid != pmp->pm_uid &&
465 (error = priv_check_cred(cred, PRIV_VFS_SETATTR, 0)))
467 if (vp->v_type != VDIR) {
468 /* We ignore the read and execute bits. */
469 if (vap->va_mode & VWRITE)
470 dep->de_Attributes &= ~ATTR_READONLY;
472 dep->de_Attributes |= ATTR_READONLY;
473 dep->de_Attributes |= ATTR_ARCHIVE;
474 dep->de_flag |= DE_MODIFIED;
477 return (deupdat(dep, 1));
481 * msdosfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
482 * struct ucred *a_cred)
485 msdosfs_read(struct vop_read_args *ap)
501 struct vnode *vp = ap->a_vp;
502 struct denode *dep = VTODE(vp);
503 struct msdosfsmount *pmp = dep->de_pmp;
504 struct uio *uio = ap->a_uio;
506 if (uio->uio_offset < 0)
509 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
512 * If they didn't ask for any data, then we are done.
514 orig_resid = uio->uio_resid;
518 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
520 isadir = dep->de_Attributes & ATTR_DIRECTORY;
522 if (uio->uio_offset >= dep->de_FileSize)
526 * note: lbn is a cluster number, not a device block number.
528 lbn = de_off2cn(pmp, uio->uio_offset);
529 loffset = de_cn2doff(pmp, lbn);
532 * If we are operating on a directory file then be sure to
533 * do i/o with the vnode for the filesystem instead of the
534 * vnode for the directory.
538 * convert cluster # to block #. lbn is a
539 * device block number after this.
541 error = pcbmap(dep, lbn, &lbn, NULL, &blsize);
542 loffset = de_bntodoff(pmp, lbn);
543 if (error == E2BIG) {
548 error = bread(pmp->pm_devvp, loffset, blsize, &bp);
550 blsize = pmp->pm_bpcluster;
552 raoffset = de_cn2doff(pmp, rablock);
554 raoffset < dep->de_FileSize) {
555 rasize = pmp->pm_bpcluster;
556 error = breadn(vp, loffset, blsize,
557 &raoffset, &rasize, 1, &bp);
559 error = bread(vp, loffset, blsize, &bp);
566 on = uio->uio_offset & pmp->pm_crbomask;
567 diff = pmp->pm_bpcluster - on;
568 n = szmin(uio->uio_resid, diff);
569 diff = dep->de_FileSize - uio->uio_offset;
572 diff = blsize - bp->b_resid;
575 error = uiomovebp(bp, bp->b_data + on, (size_t)n, uio);
577 } while (error == 0 && uio->uio_resid > 0 && n != 0);
578 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
579 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
580 dep->de_flag |= DE_ACCESS;
585 * Write data to a file or directory.
587 * msdosfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
588 * struct ucred *a_cred)
591 msdosfs_write(struct vop_write_args *ap)
601 int ioflag = ap->a_ioflag;
602 struct uio *uio = ap->a_uio;
603 struct thread *td = uio->uio_td;
604 struct vnode *vp = ap->a_vp;
605 struct vnode *thisvp;
606 struct denode *dep = VTODE(vp);
607 struct msdosfsmount *pmp = dep->de_pmp;
608 struct proc *p = (td ? td->td_proc : NULL);
609 struct lwp *lp = (td ? td->td_lwp : NULL);
612 kprintf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
613 vp, uio, ioflag, ap->a_cred);
614 kprintf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
615 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
618 switch (vp->v_type) {
620 if (ioflag & IO_APPEND)
621 uio->uio_offset = dep->de_FileSize;
627 panic("msdosfs_write(): bad file type");
630 if (uio->uio_offset < 0)
633 if (uio->uio_resid == 0)
637 * If they've exceeded their filesize limit, tell them about it.
640 ((uoff_t)uio->uio_offset + uio->uio_resid >
641 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
642 lwpsignal(p, lp, SIGXFSZ);
646 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
648 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
652 * If the offset we are starting the write at is beyond the end of
653 * the file, then they've done a seek. Unix filesystems allow
654 * files with holes in them, DOS doesn't so we must fill the hole
655 * with zeroed blocks.
657 if (uio->uio_offset > dep->de_FileSize) {
658 error = deextend(dep, uio->uio_offset);
664 * Remember some values in case the write fails.
666 resid = uio->uio_resid;
667 osize = dep->de_FileSize;
670 * If we write beyond the end of the file, extend it to its ultimate
671 * size ahead of the time to hopefully get a contiguous area.
673 if (uio->uio_offset + resid > osize) {
674 count = de_clcount(pmp, uio->uio_offset + resid) -
675 de_clcount(pmp, osize);
676 error = extendfile(dep, count, NULL, NULL, 0);
677 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
679 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
681 lastcn = de_clcount(pmp, osize) - 1;
684 if (de_off2cn(pmp, uio->uio_offset) > lastcn) {
689 croffset = uio->uio_offset & pmp->pm_crbomask;
690 n = (int)szmin(uio->uio_resid, pmp->pm_bpcluster - croffset);
691 if (uio->uio_offset + n > dep->de_FileSize) {
692 dep->de_FileSize = uio->uio_offset + n;
693 /* The object size needs to be set before buffer is allocated */
694 vnode_pager_setsize(vp, dep->de_FileSize);
698 * If either the whole cluster gets written, or we write
699 * the cluster from its start beyond EOF, then no need to
700 * read data from disk.
702 * If UIO_NOCOPY is set we have to do a read-before-write
703 * to fill in any missing pieces of the buffer since no
704 * actual overwrite will occur.
706 cn = de_off2cn(pmp, uio->uio_offset);
707 if ((uio->uio_offset & pmp->pm_crbomask) == 0
708 && uio->uio_segflg != UIO_NOCOPY
709 && (de_off2cn(pmp, uio->uio_offset + uio->uio_resid)
710 > de_off2cn(pmp, uio->uio_offset)
711 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
712 bp = getblk(thisvp, de_cn2doff(pmp, cn),
713 pmp->pm_bpcluster, 0, 0);
716 * Do the bmap now, since pcbmap needs buffers
717 * for the fat table. (see msdosfs_strategy)
719 if (bp->b_bio2.bio_offset == NOOFFSET) {
720 daddr_t lblkno = de_off2cn(pmp, bp->b_loffset);
723 error = pcbmap(dep, lblkno,
724 &dblkno, NULL, NULL);
725 if (error || dblkno == (daddr_t)-1) {
726 bp->b_bio2.bio_offset = NOOFFSET;
728 bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
731 if (bp->b_bio2.bio_offset == NOOFFSET) {
734 error = EIO; /* XXX */
739 * The block we need to write into exists, so read
742 error = bread(thisvp, de_cn2doff(pmp, cn),
743 pmp->pm_bpcluster, &bp);
751 * Should these vnode_pager_* functions be done on dir
756 * Copy the data from user space into the buf header.
758 error = uiomovebp(bp, bp->b_data + croffset, (size_t)n, uio);
765 * If they want this synchronous then write it and wait for
766 * it. Otherwise, if on a cluster boundary write it
767 * asynchronously so we can move on to the next block
768 * without delay. Otherwise do a delayed write because we
769 * may want to write somemore into the block later.
771 if (ioflag & IO_SYNC)
773 else if (n + croffset == pmp->pm_bpcluster)
777 dep->de_flag |= DE_UPDATE;
778 } while (error == 0 && uio->uio_resid > 0);
781 * If the write failed and they want us to, truncate the file back
782 * to the size it was before the write was attempted.
786 if (ioflag & IO_UNIT) {
787 detrunc(dep, osize, ioflag & IO_SYNC);
788 uio->uio_offset -= resid - uio->uio_resid;
789 uio->uio_resid = resid;
791 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC);
792 if (uio->uio_resid != resid)
795 } else if (ioflag & IO_SYNC)
796 error = deupdat(dep, 1);
801 * Flush the blocks of a file to disk.
803 * This function is worthless for vnodes that represent directories. Maybe we
804 * could just do a sync if they try an fsync on a directory file.
806 * msdosfs_fsync(struct vnode *a_vp, int a_waitfor)
809 msdosfs_fsync(struct vop_fsync_args *ap)
811 struct vnode *vp = ap->a_vp;
814 * Flush all dirty buffers associated with a vnode.
819 vfsync(vp, ap->a_waitfor, 0, NULL, NULL);
821 if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
822 vprint("msdosfs_fsync: dirty", vp);
826 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
830 * msdosfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
831 * struct componentname *a_cnp)
834 msdosfs_remove(struct vop_old_remove_args *ap)
836 struct denode *dep = VTODE(ap->a_vp);
837 struct denode *ddep = VTODE(ap->a_dvp);
840 if (ap->a_vp->v_type == VDIR)
843 error = removede(ddep, dep);
845 kprintf("msdosfs_remove(), dep %p, v_refcnt 0x%08x\n",
846 dep, ap->a_vp->v_refcnt);
852 * DOS filesystems don't know what links are. But since we already called
853 * msdosfs_lookup() with create and lockparent, the parent is locked so we
854 * have to free it before we return the error.
856 * msdosfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
857 * struct componentname *a_cnp)
860 msdosfs_link(struct vop_old_link_args *ap)
866 * Renames on files require moving the denode to a new hash queue since the
867 * denode's location is used to compute which hash queue to put the file
868 * in. Unless it is a rename in place. For example "mv a b".
870 * What follows is the basic algorithm:
873 * if (dest file exists) {
876 * if (dest and src in same directory) {
877 * rewrite name in existing directory slot
879 * write new entry in dest directory
880 * update offset and dirclust in denode
881 * move denode to new hash chain
882 * clear old directory entry
886 * if (dest directory exists) {
887 * if (dest is not empty) {
890 * remove dest directory
892 * if (dest and src in same directory) {
893 * rewrite name in existing entry
895 * be sure dest is not a child of src directory
896 * write entry in dest directory
897 * update "." and ".." in moved directory
898 * clear old directory entry for moved directory
903 * source's parent directory is unlocked
904 * source file or directory is unlocked
905 * destination's parent directory is locked
906 * destination file or directory is locked if it exists
909 * all denodes should be released
912 * I'm not sure how the memory containing the pathnames pointed at by the
913 * componentname structures is freed, there may be some memory bleeding
914 * for each rename done.
916 * msdosfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
917 * struct componentname *a_fcnp, struct vnode *a_tdvp,
918 * struct vnode *a_tvp, struct componentname *a_tcnp)
921 msdosfs_rename(struct vop_old_rename_args *ap)
923 struct vnode *tdvp = ap->a_tdvp;
924 struct vnode *fvp = ap->a_fvp;
925 struct vnode *fdvp = ap->a_fdvp;
926 struct vnode *tvp = ap->a_tvp;
927 struct componentname *tcnp = ap->a_tcnp;
928 struct componentname *fcnp = ap->a_fcnp;
929 struct denode *ip, *xp, *dp, *zp;
930 u_char toname[11], oldname[11];
931 u_long from_diroffset, to_diroffset;
933 int doingdirectory = 0, newparent = 0;
937 struct msdosfsmount *pmp;
938 struct direntry *dotdotp;
941 pmp = VFSTOMSDOSFS(fdvp->v_mount);
944 * Check for cross-device rename.
946 if ((fvp->v_mount != tdvp->v_mount) ||
947 (tvp && (fvp->v_mount != tvp->v_mount))) {
962 * If source and dest are the same, do nothing.
970 * fvp, fdvp are unlocked, tvp, tdvp are locked. Lock fvp and note
971 * that we have to unlock it to use the abortit target.
973 error = vn_lock(fvp, LK_EXCLUSIVE | LK_FAILRECLAIM);
980 * Be sure we are not renaming ".", "..", or an alias of ".". This
981 * leads to a crippled directory tree. It's pretty tough to do a
982 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
983 * doesn't work if the ".." entry is missing.
985 if (ip->de_Attributes & ATTR_DIRECTORY) {
987 * Avoid ".", "..", and aliases of "." for obvious reasons.
989 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
991 (fcnp->cn_flags & CNP_ISDOTDOT) ||
992 (tcnp->cn_flags & CNP_ISDOTDOT) ||
993 (ip->de_flag & DE_RENAME)) {
998 ip->de_flag |= DE_RENAME;
1003 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only
1004 * set if doingdirectory. We will get fvp unlocked in fairly
1005 * short order. dp and xp must be setup and fvp must be unlocked
1006 * for the out and bad targets to work properly.
1009 xp = tvp ? VTODE(tvp) : NULL;
1012 * Remember direntry place to use for destination
1014 to_diroffset = dp->de_fndoffset;
1015 to_count = dp->de_fndcnt;
1018 * If ".." must be changed (ie the directory gets a new
1019 * parent) then the source directory must not be in the
1020 * directory heirarchy above the target, as this would
1021 * orphan everything below the source directory. Also
1022 * the user must have write permission in the source so
1023 * as to be able to change "..". We must repeat the call
1024 * to namei, as the parent directory is unlocked by the
1025 * call to doscheckpath().
1027 error = VOP_EACCESS(fvp, VWRITE, tcnp->cn_cred);
1029 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1033 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1034 * DE_RENAME only set if doingdirectory.
1036 if (doingdirectory && newparent) {
1037 if (error) /* write access check above */
1044 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1045 * get an extra ref so we wind up with just an unlocked, ref'd
1046 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp
1047 * isn't locked so we can't use the out target).
1050 error = doscheckpath(ip, dp);
1051 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1057 * relookup no longer messes with the ref count. tdvp must
1058 * be unlocked on entry and on success will be locked on
1061 error = relookup(tdvp, &tvp, tcnp);
1063 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1071 * tvp and tdvp are now locked again.
1074 xp = tvp ? VTODE(tvp) : NULL;
1078 * tvp and tdvp are now locked again, the 'bad' target can be used
1079 * to clean them up again. Delete an existant target and clean
1080 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up.
1084 * Target must be empty if a directory and have no links
1085 * to it. Also, ensure source and target are compatible
1086 * (both directories, or both not directories).
1088 if (xp->de_Attributes & ATTR_DIRECTORY) {
1089 if (!dosdirempty(xp)) {
1093 if (!doingdirectory) {
1097 } else if (doingdirectory) {
1101 error = removede(dp, xp);
1110 * Convert the filename in tcnp into a dos filename. We copy this
1111 * into the denode and directory entry for the destination
1114 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1119 * Since from wasn't locked at various places above, we have to do
1120 * a relookup here. If the target and source are the same directory
1121 * we have to unlock the target directory in order to safely relookup
1122 * the source, because relookup expects its directory to be unlocked.
1124 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must
1125 * now take that into account.
1127 * The tdvp locking issues make this a real mess.
1129 fcnp->cn_flags &= ~CNP_MODMASK;
1130 fcnp->cn_flags |= CNP_LOCKPARENT;
1133 error = relookup(fdvp, &fvp, fcnp);
1134 if (error || fvp == NULL) {
1136 * From name has disappeared. Note: fdvp might == tdvp.
1138 * DE_RENAME is only set if doingdirectory.
1141 panic("rename: lost dir entry");
1142 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1155 * No error occured. tdvp, fdvp and fvp are all locked. If
1156 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned
1157 * up. ap->a_fvp is still refd.
1161 from_diroffset = zp->de_fndoffset;
1164 * Ensure that the directory entry still exists and has not
1165 * changed till now. If the source is a file the entry may
1166 * have been unlinked or renamed. In either case there is
1167 * no further work to be done. If the source is a directory
1168 * then it cannot have been rmdir'ed or renamed; this is
1169 * prohibited by the DE_RENAME flag.
1171 * DE_RENAME is only set if doingdirectory.
1175 panic("rename: lost dir entry");
1178 u_long new_dirclust;
1179 u_long new_diroffset;
1182 * First write a new entry in the destination
1183 * directory and mark the entry in the source directory
1184 * as deleted. Then move the denode to the correct hash
1185 * chain for its new location in the filesystem. And, if
1186 * we moved a directory, then update its .. entry to point
1187 * to the new parent directory.
1189 bcopy(ip->de_Name, oldname, 11);
1190 bcopy(toname, ip->de_Name, 11); /* update denode */
1191 dp->de_fndoffset = to_diroffset;
1192 dp->de_fndcnt = to_count;
1193 error = createde(ip, dp, NULL, tcnp);
1195 bcopy(oldname, ip->de_Name, 11);
1199 zp->de_fndoffset = from_diroffset;
1200 error = removede(zp, ip);
1202 /* XXX should really panic here, fs is corrupt */
1205 if (!doingdirectory) {
1206 error = pcbmap(dp, de_cluster(pmp, to_diroffset),
1207 NULL, &new_dirclust, NULL);
1209 /* XXX should really panic here, fs is corrupt */
1212 if (new_dirclust == MSDOSFSROOT)
1213 new_diroffset = to_diroffset;
1215 new_diroffset = to_diroffset & pmp->pm_crbomask;
1216 msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1221 * If we moved a directory to a new parent directory, then we must
1222 * fixup the ".." entry in the moved directory.
1224 if (doingdirectory && newparent) {
1225 cn = ip->de_StartCluster;
1226 if (cn == MSDOSFSROOT) {
1227 /* this should never happen */
1228 panic("msdosfs_rename(): updating .. in root directory?");
1230 bn = xcntobn(pmp, cn);
1232 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
1234 /* XXX should really panic here, fs is corrupt */
1238 dotdotp = (struct direntry *)bp->b_data + 1;
1239 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1241 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1244 /* XXX should really panic here, fs is corrupt */
1250 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd
1254 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */
1265 * 'bad' target: xp governs tvp. tvp and tdvp arel ocked, fdvp and fvp
1266 * are not locked. ip points to fvp's inode which may have DE_RENAME
1275 * 'out' target: tvp and tdvp have already been cleaned up.
1278 ip->de_flag &= ~DE_RENAME;
1286 struct direntry dot;
1287 struct direntry dotdot;
1288 } dosdirtemplate = {
1289 { ". ", /* the . entry */
1290 ATTR_DIRECTORY, /* file attribute */
1292 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1293 { 0, 0 }, /* access date */
1294 { 0, 0 }, /* high bits of start cluster */
1295 { 210, 4 }, { 210, 4 }, /* modify time & date */
1296 { 0, 0 }, /* startcluster */
1297 { 0, 0, 0, 0 } /* filesize */
1299 { ".. ", /* the .. entry */
1300 ATTR_DIRECTORY, /* file attribute */
1302 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1303 { 0, 0 }, /* access date */
1304 { 0, 0 }, /* high bits of start cluster */
1305 { 210, 4 }, { 210, 4 }, /* modify time & date */
1306 { 0, 0 }, /* startcluster */
1307 { 0, 0, 0, 0 } /* filesize */
1312 * msdosfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1313 * struct componentname *a_cnp, struct vattr *a_vap)
1316 msdosfs_mkdir(struct vop_old_mkdir_args *ap)
1318 struct componentname *cnp = ap->a_cnp;
1320 struct denode *pdep = VTODE(ap->a_dvp);
1321 struct direntry *denp;
1322 struct msdosfsmount *pmp = pdep->de_pmp;
1324 u_long newcluster, pcl;
1327 struct denode ndirent;
1331 * If this is the root directory and there is no space left we
1332 * can't do anything. This is because the root directory can not
1335 if (pdep->de_StartCluster == MSDOSFSROOT
1336 && pdep->de_fndoffset >= pdep->de_FileSize) {
1342 * Allocate a cluster to hold the about to be created directory.
1344 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1348 bzero(&ndirent, sizeof(ndirent));
1349 ndirent.de_pmp = pmp;
1350 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1352 DETIMES(&ndirent, &ts, &ts, &ts);
1355 * Now fill the cluster with the "." and ".." entries. And write
1356 * the cluster to disk. This way it is there for the parent
1357 * directory to be pointing at if there were a crash.
1359 bn = xcntobn(pmp, newcluster);
1360 /* always succeeds */
1361 bp = getblk(pmp->pm_devvp, de_bntodoff(pmp, bn),
1362 pmp->pm_bpcluster, 0, 0);
1363 bzero(bp->b_data, pmp->pm_bpcluster);
1364 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1365 denp = (struct direntry *)bp->b_data;
1366 putushort(denp[0].deStartCluster, newcluster);
1367 putushort(denp[0].deCDate, ndirent.de_CDate);
1368 putushort(denp[0].deCTime, ndirent.de_CTime);
1369 denp[0].deCHundredth = ndirent.de_CHun;
1370 putushort(denp[0].deADate, ndirent.de_ADate);
1371 putushort(denp[0].deMDate, ndirent.de_MDate);
1372 putushort(denp[0].deMTime, ndirent.de_MTime);
1373 pcl = pdep->de_StartCluster;
1374 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1376 putushort(denp[1].deStartCluster, pcl);
1377 putushort(denp[1].deCDate, ndirent.de_CDate);
1378 putushort(denp[1].deCTime, ndirent.de_CTime);
1379 denp[1].deCHundredth = ndirent.de_CHun;
1380 putushort(denp[1].deADate, ndirent.de_ADate);
1381 putushort(denp[1].deMDate, ndirent.de_MDate);
1382 putushort(denp[1].deMTime, ndirent.de_MTime);
1384 putushort(denp[0].deHighClust, newcluster >> 16);
1385 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1393 * Now build up a directory entry pointing to the newly allocated
1394 * cluster. This will be written to an empty slot in the parent
1397 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1401 ndirent.de_Attributes = ATTR_DIRECTORY;
1402 ndirent.de_LowerCase = 0;
1403 ndirent.de_StartCluster = newcluster;
1404 ndirent.de_FileSize = 0;
1405 ndirent.de_dev = pdep->de_dev;
1406 ndirent.de_devvp = pdep->de_devvp;
1407 error = createde(&ndirent, pdep, &dep, cnp);
1410 *ap->a_vpp = DETOV(dep);
1414 clusterfree(pmp, newcluster, NULL);
1420 * msdosfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1421 * struct componentname *a_cnp)
1424 msdosfs_rmdir(struct vop_old_rmdir_args *ap)
1426 struct vnode *vp = ap->a_vp;
1427 struct vnode *dvp = ap->a_dvp;
1428 struct denode *ip, *dp;
1435 * Verify the directory is empty (and valid).
1436 * (Rmdir ".." won't be valid since
1437 * ".." will contain a reference to
1438 * the current directory and thus be
1442 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1447 * Delete the entry from the directory. For dos filesystems this
1448 * gets rid of the directory entry on disk, the in memory copy
1449 * still exists but the de_refcnt is <= 0. This prevents it from
1450 * being found by deget(). When the vput() on dep is done we give
1451 * up access and eventually msdosfs_reclaim() will be called which
1452 * will remove it from the denode cache.
1454 error = removede(dp, ip);
1458 * This is where we decrement the link count in the parent
1459 * directory. Since dos filesystems don't do this we just purge
1464 * Truncate the directory that is being deleted.
1466 error = detrunc(ip, (u_long)0, IO_SYNC);
1468 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1474 * DOS filesystems don't know what symlinks are.
1476 * msdosfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1477 * struct componentname *a_cnp, struct vattr *a_vap,
1481 msdosfs_symlink(struct vop_old_symlink_args *ap)
1483 return (EOPNOTSUPP);
1487 * msdosfs_readdir(struct vnode *a_vp, struct uio *a_uio,
1488 * struct ucred *a_cred, int *a_eofflag, int *a_ncookies,
1489 * off_t **a_cookies)
1492 msdosfs_readdir(struct vop_readdir_args *ap)
1506 struct msdosfsmount *pmp;
1507 struct direntry *dentp;
1508 struct uio *uio = ap->a_uio;
1509 off_t *cookies = NULL;
1516 char *d_name_storage = NULL;
1517 char *d_name = NULL;
1519 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
1523 dep = VTODE(ap->a_vp);
1526 #ifdef MSDOSFS_DEBUG
1527 kprintf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1528 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1532 * msdosfs_readdir() won't operate properly on regular files since
1533 * it does i/o only with the the filesystem vnode, and hence can
1534 * retrieve the wrong block from the buffer cache for a plain file.
1535 * So, fail attempts to readdir() on a plain file.
1537 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
1543 * If the user buffer is smaller than the size of one dos directory
1544 * entry or the file offset is not a multiple of the size of a
1545 * directory entry, then we fail the read.
1547 off = offset = uio->uio_offset;
1548 if (uio->uio_resid < sizeof(struct direntry) ||
1549 (offset & (sizeof(struct direntry) - 1))) {
1554 if (ap->a_ncookies) {
1555 ncookies = uio->uio_resid / 16 + 1;
1556 if (ncookies > 1024)
1558 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1559 *ap->a_cookies = cookies;
1560 *ap->a_ncookies = ncookies;
1563 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1566 * If they are reading from the root directory then, we simulate
1567 * the . and .. entries since these don't exist in the root
1568 * directory. We also set the offset bias to make up for having to
1569 * simulate these entries. By this I mean that at file offset 64 we
1570 * read the first entry in the root directory that lives on disk.
1572 if (dep->de_StartCluster == MSDOSFSROOT
1573 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1575 kprintf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1578 bias = 2 * sizeof(struct direntry);
1579 if (offset < bias) {
1580 for (n = (int)offset / sizeof(struct direntry); n < 2;
1583 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1591 } else if (n == 1) {
1595 if (vop_write_dirent(&error, uio, d_ino, d_type,
1600 offset += sizeof(struct direntry);
1603 *cookies++ = offset;
1604 if (--ncookies <= 0)
1611 d_name_storage = kmalloc(WIN_MAXLEN, M_TEMP, M_WAITOK);
1615 while (uio->uio_resid > 0) {
1616 lbn = de_off2cn(pmp, offset - bias);
1617 on = (offset - bias) & pmp->pm_crbomask;
1618 n = szmin(pmp->pm_bpcluster - on, uio->uio_resid);
1619 diff = dep->de_FileSize - (offset - bias);
1623 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1626 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
1629 kfree(d_name_storage, M_TEMP);
1632 n = min(n, blsize - bp->b_resid);
1635 * Convert from dos directory entries to fs-independent
1636 * directory entries.
1638 for (dentp = (struct direntry *)(bp->b_data + on);
1639 (char *)dentp < bp->b_data + on + n;
1640 dentp++, offset += sizeof(struct direntry)) {
1642 kprintf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1643 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1645 d_name = d_name_storage;
1648 * If this is an unused entry, we can stop.
1650 if (dentp->deName[0] == SLOT_EMPTY) {
1655 * Skip deleted entries.
1657 if (dentp->deName[0] == SLOT_DELETED) {
1663 * Handle Win95 long directory entries
1665 if (dentp->deAttributes == ATTR_WIN95) {
1666 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1668 chksum = win2unixfn(&nb,
1669 (struct winentry *)dentp,
1676 * Skip volume labels
1678 if (dentp->deAttributes & ATTR_VOLUME) {
1684 * This computation of d_ino must match
1685 * the computation of va_fileid in
1688 if (dentp->deAttributes & ATTR_DIRECTORY) {
1689 d_ino = getushort(dentp->deStartCluster);
1691 d_ino |= getushort(dentp->deHighClust) << 16;
1692 /* if this is the root directory */
1693 if (d_ino != MSDOSFSROOT)
1694 d_ino = xcntobn(pmp, d_ino) * dirsperblk;
1695 else if (FAT32(pmp))
1696 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1702 d_ino = offset / sizeof(struct direntry);
1705 if (chksum != winChksum(dentp->deName)) {
1706 d_namlen = dos2unixfn(dentp->deName,
1708 dentp->deLowerCase |
1709 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1710 (LCASE_BASE | LCASE_EXT) : 0),
1714 mbnambuf_flush(&nb, d_name, &d_namlen);
1717 if (vop_write_dirent(&error, uio, d_ino, d_type,
1718 d_namlen, d_name)) {
1728 *cookies++ = offset + sizeof(struct direntry);
1729 if (--ncookies <= 0) {
1734 off = offset + sizeof(struct direntry);
1739 if (d_name_storage != NULL)
1740 kfree(d_name_storage, M_TEMP);
1742 /* Subtract unused cookies */
1744 *ap->a_ncookies -= ncookies;
1746 uio->uio_offset = off;
1749 * Set the eofflag (NFS uses it)
1751 if (ap->a_eofflag) {
1752 if (dep->de_FileSize - (offset - bias) <= 0)
1758 vn_unlock(ap->a_vp);
1763 * vp - address of vnode file the file
1764 * bn - which cluster we are interested in mapping to a filesystem block number.
1765 * vpp - returns the vnode for the block special file holding the filesystem
1766 * containing the file of interest
1767 * bnp - address of where to return the filesystem relative block number
1769 * msdosfs_bmap(struct vnode *a_vp, off_t a_loffset,
1770 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1773 msdosfs_bmap(struct vop_bmap_args *ap)
1775 struct denode *dep = VTODE(ap->a_vp);
1776 struct msdosfsmount *pmp = dep->de_pmp;
1781 if (ap->a_doffsetp == NULL)
1785 * Sequential clusters should be counted here.
1792 KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0);
1793 lbn = de_off2cn(pmp, ap->a_loffset);
1794 error = pcbmap(dep, lbn, &dbn, NULL, NULL);
1795 if (error || dbn == (daddr_t)-1) {
1796 *ap->a_doffsetp = NOOFFSET;
1798 *ap->a_doffsetp = de_bntodoff(pmp, dbn);
1804 * msdosfs_strategy(struct vnode *a_vp, struct bio *a_bio)
1807 msdosfs_strategy(struct vop_strategy_args *ap)
1809 struct bio *bio = ap->a_bio;
1811 struct buf *bp = bio->bio_buf;
1812 struct vnode *vp = ap->a_vp;
1813 struct denode *dep = VTODE(vp);
1814 struct msdosfsmount *pmp = dep->de_pmp;
1818 if (vp->v_type == VBLK || vp->v_type == VCHR)
1819 panic("msdosfs_strategy: spec");
1821 * If we don't already know the filesystem relative block number
1822 * then get it using pcbmap(). If pcbmap() returns the block
1823 * number as -1 then we've got a hole in the file. DOS filesystems
1824 * don't allow files with holes, so we shouldn't ever see this.
1826 nbio = push_bio(bio);
1827 if (nbio->bio_offset == NOOFFSET) {
1828 error = pcbmap(dep, de_off2cn(pmp, bio->bio_offset),
1829 &dblkno, NULL, NULL);
1831 bp->b_error = error;
1832 bp->b_flags |= B_ERROR;
1833 /* I/O was never started on nbio, must biodone(bio) */
1837 if (dblkno == (daddr_t)-1) {
1838 nbio->bio_offset = NOOFFSET;
1841 nbio->bio_offset = de_bntodoff(pmp, dblkno);
1844 if (nbio->bio_offset == NOOFFSET) {
1845 /* I/O was never started on nbio, must biodone(bio) */
1850 * Read/write the block from/to the disk that contains the desired
1853 vn_strategy(dep->de_devvp, nbio);
1858 * msdosfs_print(struct vnode *vp)
1861 msdosfs_print(struct vop_print_args *ap)
1863 struct denode *dep = VTODE(ap->a_vp);
1866 "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1867 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1868 kprintf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1869 lockmgr_printinfo(&ap->a_vp->v_lock);
1875 * msdosfs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1878 msdosfs_pathconf(struct vop_pathconf_args *ap)
1880 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1882 switch (ap->a_name) {
1887 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1890 *ap->a_retval = PATH_MAX;
1892 case _PC_CHOWN_RESTRICTED:
1904 /* Global vfs data structures for msdosfs */
1905 struct vop_ops msdosfs_vnode_vops = {
1906 .vop_default = vop_defaultop,
1907 .vop_access = msdosfs_access,
1908 .vop_bmap = msdosfs_bmap,
1909 .vop_old_lookup = msdosfs_lookup,
1910 .vop_open = msdosfs_open,
1911 .vop_close = msdosfs_close,
1912 .vop_old_create = msdosfs_create,
1913 .vop_fsync = msdosfs_fsync,
1914 .vop_getattr = msdosfs_getattr,
1915 .vop_inactive = msdosfs_inactive,
1916 .vop_old_link = msdosfs_link,
1917 .vop_old_mkdir = msdosfs_mkdir,
1918 .vop_old_mknod = msdosfs_mknod,
1919 .vop_pathconf = msdosfs_pathconf,
1920 .vop_print = msdosfs_print,
1921 .vop_read = msdosfs_read,
1922 .vop_readdir = msdosfs_readdir,
1923 .vop_reclaim = msdosfs_reclaim,
1924 .vop_old_remove = msdosfs_remove,
1925 .vop_old_rename = msdosfs_rename,
1926 .vop_old_rmdir = msdosfs_rmdir,
1927 .vop_setattr = msdosfs_setattr,
1928 .vop_strategy = msdosfs_strategy,
1929 .vop_old_symlink = msdosfs_symlink,
1930 .vop_write = msdosfs_write,
1931 .vop_getpages = vop_stdgetpages,
1932 .vop_putpages = vop_stdputpages