1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vnops.c,v 1.95.2.4 2003/06/13 15:05:47 trhodes Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vnops.c,v 1.32 2006/03/29 18:44:59 dillon Exp $ */
3 /* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
55 #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>
77 #include "msdosfsmount.h"
80 #define DOS_FILESIZE_MAX 0xffffffff
83 * Prototypes for MSDOSFS vnode operations
85 static int msdosfs_create (struct vop_old_create_args *);
86 static int msdosfs_mknod (struct vop_old_mknod_args *);
87 static int msdosfs_open (struct vop_open_args *);
88 static int msdosfs_close (struct vop_close_args *);
89 static int msdosfs_access (struct vop_access_args *);
90 static int msdosfs_getattr (struct vop_getattr_args *);
91 static int msdosfs_setattr (struct vop_setattr_args *);
92 static int msdosfs_read (struct vop_read_args *);
93 static int msdosfs_write (struct vop_write_args *);
94 static int msdosfs_fsync (struct vop_fsync_args *);
95 static int msdosfs_remove (struct vop_old_remove_args *);
96 static int msdosfs_link (struct vop_old_link_args *);
97 static int msdosfs_rename (struct vop_old_rename_args *);
98 static int msdosfs_mkdir (struct vop_old_mkdir_args *);
99 static int msdosfs_rmdir (struct vop_old_rmdir_args *);
100 static int msdosfs_symlink (struct vop_old_symlink_args *);
101 static int msdosfs_readdir (struct vop_readdir_args *);
102 static int msdosfs_bmap (struct vop_bmap_args *);
103 static int msdosfs_strategy (struct vop_strategy_args *);
104 static int msdosfs_print (struct vop_print_args *);
105 static int msdosfs_pathconf (struct vop_pathconf_args *ap);
106 static int msdosfs_getpages (struct vop_getpages_args *);
107 static int msdosfs_putpages (struct vop_putpages_args *);
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 printf("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)
217 msdosfs_open(struct vop_open_args *ap)
219 struct vnode *vp = ap->a_vp;
221 if (vp->v_type == VREG || vp->v_type == VDIR)
227 * msdosfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
228 * struct thread *a_td)
231 msdosfs_close(struct vop_close_args *ap)
233 struct vnode *vp = ap->a_vp;
234 struct denode *dep = VTODE(vp);
237 if (vp->v_usecount > 1) {
239 if (vp->v_usecount > 1) {
240 DETIMES(dep, &ts, &ts, &ts);
247 * msdosfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
248 * struct thread *a_td)
251 msdosfs_access(struct vop_access_args *ap)
253 struct vnode *vp = ap->a_vp;
254 struct denode *dep = VTODE(ap->a_vp);
255 struct msdosfsmount *pmp = dep->de_pmp;
256 struct ucred *cred = ap->a_cred;
257 mode_t mask, file_mode, mode = ap->a_mode;
261 file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
262 ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
263 file_mode &= pmp->pm_mask;
266 * Disallow write attempts on read-only file systems;
267 * unless the file is a socket, fifo, or a block or
268 * character device resident on the file system.
271 switch (vp->v_type) {
275 if (vp->v_mount->mnt_flag & MNT_RDONLY)
283 /* User id 0 always gets access. */
284 if (cred->cr_uid == 0)
289 /* Otherwise, check the owner. */
290 if (cred->cr_uid == pmp->pm_uid) {
297 return (file_mode & mask) == mask ? 0 : EACCES;
300 /* Otherwise, check the groups. */
301 for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
302 if (pmp->pm_gid == *gp) {
309 return (file_mode & mask) == mask ? 0 : EACCES;
312 /* Otherwise, check everyone else. */
319 return (file_mode & mask) == mask ? 0 : EACCES;
323 * msdosfs_getattr(struct vnode *a_vp, struct vattr *a_vap,
324 * struct ucred *a_cred, struct thread *a_td)
327 msdosfs_getattr(struct vop_getattr_args *ap)
329 struct denode *dep = VTODE(ap->a_vp);
330 struct msdosfsmount *pmp = dep->de_pmp;
331 struct vattr *vap = ap->a_vap;
334 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
338 DETIMES(dep, &ts, &ts, &ts);
339 vap->va_fsid = dev2udev(dep->de_dev);
341 * The following computation of the fileid must be the same as that
342 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
345 if (dep->de_Attributes & ATTR_DIRECTORY) {
346 fileid = xcntobn(pmp, dep->de_StartCluster) * dirsperblk;
347 if (dep->de_StartCluster == MSDOSFSROOT)
350 fileid = xcntobn(pmp, dep->de_dirclust) * dirsperblk;
351 if (dep->de_dirclust == MSDOSFSROOT)
352 fileid = roottobn(pmp, 0) * dirsperblk;
353 fileid += dep->de_diroffset / sizeof(struct direntry);
355 vap->va_fileid = fileid;
356 if ((dep->de_Attributes & ATTR_READONLY) == 0)
357 mode = S_IRWXU|S_IRWXG|S_IRWXO;
359 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
360 vap->va_mode = mode & pmp->pm_mask;
361 vap->va_uid = pmp->pm_uid;
362 vap->va_gid = pmp->pm_gid;
365 vap->va_size = dep->de_FileSize;
366 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
367 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
368 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
369 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
371 vap->va_atime = vap->va_mtime;
372 vap->va_ctime = vap->va_mtime;
375 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
376 vap->va_flags |= SF_ARCHIVED;
378 vap->va_blocksize = pmp->pm_bpcluster;
380 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
381 vap->va_type = ap->a_vp->v_type;
382 vap->va_filerev = dep->de_modrev;
387 * msdosfs_setattr(struct vnode *a_vp, struct vattr *a_vap,
388 * struct ucred *a_cred, struct thread *a_td)
391 msdosfs_setattr(struct vop_setattr_args *ap)
393 struct vnode *vp = ap->a_vp;
394 struct denode *dep = VTODE(ap->a_vp);
395 struct msdosfsmount *pmp = dep->de_pmp;
396 struct vattr *vap = ap->a_vap;
397 struct ucred *cred = ap->a_cred;
401 printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
402 ap->a_vp, vap, cred, ap->a_td);
406 * Check for unsettable attributes.
408 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
409 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
410 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
411 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
413 printf("msdosfs_setattr(): returning EINVAL\n");
414 printf(" va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
415 vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
416 printf(" va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
417 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
418 printf(" va_uid %x, va_gid %x\n",
419 vap->va_uid, vap->va_gid);
423 if (vap->va_flags != VNOVAL) {
424 if (vp->v_mount->mnt_flag & MNT_RDONLY)
426 if (cred->cr_uid != pmp->pm_uid &&
427 (error = suser_cred(cred, PRISON_ROOT)))
430 * We are very inconsistent about handling unsupported
431 * attributes. We ignored the access time and the
432 * read and execute bits. We were strict for the other
435 * Here we are strict, stricter than ufs in not allowing
436 * users to attempt to set SF_SETTABLE bits or anyone to
437 * set unsupported bits. However, we ignore attempts to
438 * set ATTR_ARCHIVE for directories `cp -pr' from a more
439 * sensible file system attempts it a lot.
441 if (cred->cr_uid != 0) {
442 if (vap->va_flags & SF_SETTABLE)
445 if (vap->va_flags & ~SF_ARCHIVED)
447 if (vap->va_flags & SF_ARCHIVED)
448 dep->de_Attributes &= ~ATTR_ARCHIVE;
449 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
450 dep->de_Attributes |= ATTR_ARCHIVE;
451 dep->de_flag |= DE_MODIFIED;
454 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
458 if (vp->v_mount->mnt_flag & MNT_RDONLY)
461 if (uid == (uid_t)VNOVAL)
464 if (gid == (gid_t)VNOVAL)
466 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
467 (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
468 (error = suser_cred(cred, PRISON_ROOT)))
470 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
474 if (vap->va_size != VNOVAL) {
476 * Disallow write attempts on read-only file systems;
477 * unless the file is a socket, fifo, or a block or
478 * character device resident on the file system.
480 switch (vp->v_type) {
486 if (vp->v_mount->mnt_flag & MNT_RDONLY)
492 error = detrunc(dep, vap->va_size, 0, ap->a_td);
496 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
497 if (vp->v_mount->mnt_flag & MNT_RDONLY)
499 if (cred->cr_uid != pmp->pm_uid &&
500 (error = suser_cred(cred, PRISON_ROOT)) &&
501 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
502 (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_td))))
504 if (vp->v_type != VDIR) {
505 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
506 vap->va_atime.tv_sec != VNOVAL) {
507 dep->de_flag &= ~DE_ACCESS;
508 unix2dostime(&vap->va_atime, &dep->de_ADate,
511 if (vap->va_mtime.tv_sec != VNOVAL) {
512 dep->de_flag &= ~DE_UPDATE;
513 unix2dostime(&vap->va_mtime, &dep->de_MDate,
514 &dep->de_MTime, NULL);
516 dep->de_Attributes |= ATTR_ARCHIVE;
517 dep->de_flag |= DE_MODIFIED;
521 * DOS files only have the ability to have their writability
522 * attribute set, so we use the owner write bit to set the readonly
525 if (vap->va_mode != (mode_t)VNOVAL) {
526 if (vp->v_mount->mnt_flag & MNT_RDONLY)
528 if (cred->cr_uid != pmp->pm_uid &&
529 (error = suser_cred(cred, PRISON_ROOT)))
531 if (vp->v_type != VDIR) {
532 /* We ignore the read and execute bits. */
533 if (vap->va_mode & VWRITE)
534 dep->de_Attributes &= ~ATTR_READONLY;
536 dep->de_Attributes |= ATTR_READONLY;
537 dep->de_Attributes |= ATTR_ARCHIVE;
538 dep->de_flag |= DE_MODIFIED;
541 return (deupdat(dep, 1));
545 * msdosfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
546 * struct ucred *a_cred)
549 msdosfs_read(struct vop_read_args *ap)
565 struct vnode *vp = ap->a_vp;
566 struct denode *dep = VTODE(vp);
567 struct msdosfsmount *pmp = dep->de_pmp;
568 struct uio *uio = ap->a_uio;
570 if (uio->uio_offset < 0)
573 if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
576 * If they didn't ask for any data, then we are done.
578 orig_resid = uio->uio_resid;
582 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
584 isadir = dep->de_Attributes & ATTR_DIRECTORY;
586 if (uio->uio_offset >= dep->de_FileSize)
590 * note: lbn is a cluster number, not a device block number.
592 lbn = de_off2cn(pmp, uio->uio_offset);
593 loffset = de_cn2doff(pmp, lbn);
596 * If we are operating on a directory file then be sure to
597 * do i/o with the vnode for the filesystem instead of the
598 * vnode for the directory.
602 * convert cluster # to block #. lbn is a
603 * device block number after this.
605 error = pcbmap(dep, lbn, &lbn, NULL, &blsize);
606 loffset = de_bntodoff(pmp, lbn);
607 if (error == E2BIG) {
612 error = bread(pmp->pm_devvp, loffset, blsize, &bp);
614 blsize = pmp->pm_bpcluster;
616 raoffset = de_cn2doff(pmp, rablock);
618 raoffset < dep->de_FileSize) {
619 rasize = pmp->pm_bpcluster;
620 error = breadn(vp, loffset, blsize,
621 &raoffset, &rasize, 1, &bp);
623 error = bread(vp, loffset, blsize, &bp);
630 on = uio->uio_offset & pmp->pm_crbomask;
631 diff = pmp->pm_bpcluster - on;
632 n = diff > uio->uio_resid ? uio->uio_resid : diff;
633 diff = dep->de_FileSize - uio->uio_offset;
636 diff = blsize - bp->b_resid;
639 error = uiomove(bp->b_data + on, (int) n, uio);
641 } while (error == 0 && uio->uio_resid > 0 && n != 0);
642 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
643 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
644 dep->de_flag |= DE_ACCESS;
649 * Write data to a file or directory.
651 * msdosfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
652 * struct ucred *a_cred)
655 msdosfs_write(struct vop_write_args *ap)
665 int ioflag = ap->a_ioflag;
666 struct uio *uio = ap->a_uio;
667 struct thread *td = uio->uio_td;
668 struct vnode *vp = ap->a_vp;
669 struct vnode *thisvp;
670 struct denode *dep = VTODE(vp);
671 struct msdosfsmount *pmp = dep->de_pmp;
672 struct proc *p = (td ? td->td_proc : NULL);
675 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
676 vp, uio, ioflag, cred);
677 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
678 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
681 switch (vp->v_type) {
683 if (ioflag & IO_APPEND)
684 uio->uio_offset = dep->de_FileSize;
690 panic("msdosfs_write(): bad file type");
693 if (uio->uio_offset < 0)
696 if (uio->uio_resid == 0)
700 * If they've exceeded their filesize limit, tell them about it.
703 ((uoff_t)uio->uio_offset + uio->uio_resid >
704 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
709 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
713 * If the offset we are starting the write at is beyond the end of
714 * the file, then they've done a seek. Unix filesystems allow
715 * files with holes in them, DOS doesn't so we must fill the hole
716 * with zeroed blocks.
718 if (uio->uio_offset > dep->de_FileSize) {
719 error = deextend(dep, uio->uio_offset);
725 * Remember some values in case the write fails.
727 resid = uio->uio_resid;
728 osize = dep->de_FileSize;
731 * If we write beyond the end of the file, extend it to its ultimate
732 * size ahead of the time to hopefully get a contiguous area.
734 if (uio->uio_offset + resid > osize) {
735 count = de_clcount(pmp, uio->uio_offset + resid) -
736 de_clcount(pmp, osize);
737 error = extendfile(dep, count, NULL, NULL, 0);
738 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
740 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
742 lastcn = de_clcount(pmp, osize) - 1;
745 if (de_off2cn(pmp, uio->uio_offset) > lastcn) {
750 croffset = uio->uio_offset & pmp->pm_crbomask;
751 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
752 if (uio->uio_offset + n > dep->de_FileSize) {
753 dep->de_FileSize = uio->uio_offset + n;
754 /* The object size needs to be set before buffer is allocated */
755 vnode_pager_setsize(vp, dep->de_FileSize);
758 cn = de_off2cn(pmp, uio->uio_offset);
759 if ((uio->uio_offset & pmp->pm_crbomask) == 0
760 && (de_off2cn(pmp, uio->uio_offset + uio->uio_resid)
761 > de_off2cn(pmp, uio->uio_offset)
762 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
764 * If either the whole cluster gets written,
765 * or we write the cluster from its start beyond EOF,
766 * then no need to read data from disk.
768 bp = getblk(thisvp, de_cn2doff(pmp, cn),
769 pmp->pm_bpcluster, 0, 0);
772 * Do the bmap now, since pcbmap needs buffers
773 * for the fat table. (see msdosfs_strategy)
775 if (bp->b_bio2.bio_offset == NOOFFSET) {
776 daddr_t lblkno = de_off2cn(pmp, bp->b_loffset);
779 error = pcbmap(dep, lblkno,
780 &dblkno, NULL, NULL);
781 if (error || dblkno == (daddr_t)-1) {
782 bp->b_bio2.bio_offset = NOOFFSET;
784 bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
787 if (bp->b_bio2.bio_offset == NOOFFSET) {
790 error = EIO; /* XXX */
795 * The block we need to write into exists, so read
798 error = bread(thisvp, de_cn2doff(pmp, cn),
799 pmp->pm_bpcluster, &bp);
807 * Should these vnode_pager_* functions be done on dir
812 * Copy the data from user space into the buf header.
814 error = uiomove(bp->b_data + croffset, n, uio);
821 * If they want this synchronous then write it and wait for
822 * it. Otherwise, if on a cluster boundary write it
823 * asynchronously so we can move on to the next block
824 * without delay. Otherwise do a delayed write because we
825 * may want to write somemore into the block later.
827 if (ioflag & IO_SYNC)
829 else if (n + croffset == pmp->pm_bpcluster)
833 dep->de_flag |= DE_UPDATE;
834 } while (error == 0 && uio->uio_resid > 0);
837 * If the write failed and they want us to, truncate the file back
838 * to the size it was before the write was attempted.
842 if (ioflag & IO_UNIT) {
843 detrunc(dep, osize, ioflag & IO_SYNC, NULL);
844 uio->uio_offset -= resid - uio->uio_resid;
845 uio->uio_resid = resid;
847 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NULL);
848 if (uio->uio_resid != resid)
851 } else if (ioflag & IO_SYNC)
852 error = deupdat(dep, 1);
857 * Flush the blocks of a file to disk.
859 * This function is worthless for vnodes that represent directories. Maybe we
860 * could just do a sync if they try an fsync on a directory file.
862 * msdosfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
863 * struct thread *a_td)
866 msdosfs_fsync(struct vop_fsync_args *ap)
868 struct vnode *vp = ap->a_vp;
871 * Flush all dirty buffers associated with a vnode.
876 vfsync(vp, ap->a_waitfor, 0, NOOFFSET, NULL, NULL);
878 if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
879 vprint("msdosfs_fsync: dirty", vp);
883 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
887 * msdosfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
888 * struct componentname *a_cnp)
891 msdosfs_remove(struct vop_old_remove_args *ap)
893 struct denode *dep = VTODE(ap->a_vp);
894 struct denode *ddep = VTODE(ap->a_dvp);
897 if (ap->a_vp->v_type == VDIR)
900 error = removede(ddep, dep);
902 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
908 * DOS filesystems don't know what links are. But since we already called
909 * msdosfs_lookup() with create and lockparent, the parent is locked so we
910 * have to free it before we return the error.
912 * msdosfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
913 * struct componentname *a_cnp)
916 msdosfs_link(struct vop_old_link_args *ap)
922 * Renames on files require moving the denode to a new hash queue since the
923 * denode's location is used to compute which hash queue to put the file
924 * in. Unless it is a rename in place. For example "mv a b".
926 * What follows is the basic algorithm:
929 * if (dest file exists) {
932 * if (dest and src in same directory) {
933 * rewrite name in existing directory slot
935 * write new entry in dest directory
936 * update offset and dirclust in denode
937 * move denode to new hash chain
938 * clear old directory entry
942 * if (dest directory exists) {
943 * if (dest is not empty) {
946 * remove dest directory
948 * if (dest and src in same directory) {
949 * rewrite name in existing entry
951 * be sure dest is not a child of src directory
952 * write entry in dest directory
953 * update "." and ".." in moved directory
954 * clear old directory entry for moved directory
959 * source's parent directory is unlocked
960 * source file or directory is unlocked
961 * destination's parent directory is locked
962 * destination file or directory is locked if it exists
965 * all denodes should be released
968 * I'm not sure how the memory containing the pathnames pointed at by the
969 * componentname structures is freed, there may be some memory bleeding
970 * for each rename done.
972 * msdosfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
973 * struct componentname *a_fcnp, struct vnode *a_tdvp,
974 * struct vnode *a_tvp, struct componentname *a_tcnp)
977 msdosfs_rename(struct vop_old_rename_args *ap)
979 struct vnode *tdvp = ap->a_tdvp;
980 struct vnode *fvp = ap->a_fvp;
981 struct vnode *fdvp = ap->a_fdvp;
982 struct vnode *tvp = ap->a_tvp;
983 struct componentname *tcnp = ap->a_tcnp;
984 struct componentname *fcnp = ap->a_fcnp;
985 struct thread *td = fcnp->cn_td;
986 struct denode *ip, *xp, *dp, *zp;
987 u_char toname[11], oldname[11];
988 u_long from_diroffset, to_diroffset;
990 int doingdirectory = 0, newparent = 0;
994 struct denode *fddep; /* from file's parent directory */
995 struct msdosfsmount *pmp;
996 struct direntry *dotdotp;
999 fddep = VTODE(ap->a_fdvp);
1000 pmp = fddep->de_pmp;
1002 pmp = VFSTOMSDOSFS(fdvp->v_mount);
1005 * Check for cross-device rename.
1007 if ((fvp->v_mount != tdvp->v_mount) ||
1008 (tvp && (fvp->v_mount != tvp->v_mount))) {
1023 * If source and dest are the same, do nothing.
1031 * fvp, fdvp are unlocked, tvp, tdvp are locked. Lock fvp and note
1032 * that we have to unlock it to use the abortit target.
1034 error = vn_lock(fvp, LK_EXCLUSIVE, td);
1041 * Be sure we are not renaming ".", "..", or an alias of ".". This
1042 * leads to a crippled directory tree. It's pretty tough to do a
1043 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1044 * doesn't work if the ".." entry is missing.
1046 if (ip->de_Attributes & ATTR_DIRECTORY) {
1048 * Avoid ".", "..", and aliases of "." for obvious reasons.
1050 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1052 (fcnp->cn_flags & CNP_ISDOTDOT) ||
1053 (tcnp->cn_flags & CNP_ISDOTDOT) ||
1054 (ip->de_flag & DE_RENAME)) {
1055 VOP_UNLOCK(fvp, 0, td);
1059 ip->de_flag |= DE_RENAME;
1064 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only
1065 * set if doingdirectory. We will get fvp unlocked in fairly
1066 * short order. dp and xp must be setup and fvp must be unlocked
1067 * for the out and bad targets to work properly.
1070 xp = tvp ? VTODE(tvp) : NULL;
1073 * Remember direntry place to use for destination
1075 to_diroffset = dp->de_fndoffset;
1076 to_count = dp->de_fndcnt;
1079 * If ".." must be changed (ie the directory gets a new
1080 * parent) then the source directory must not be in the
1081 * directory heirarchy above the target, as this would
1082 * orphan everything below the source directory. Also
1083 * the user must have write permission in the source so
1084 * as to be able to change "..". We must repeat the call
1085 * to namei, as the parent directory is unlocked by the
1086 * call to doscheckpath().
1088 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
1089 VOP_UNLOCK(fvp, 0, td);
1090 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1094 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1095 * DE_RENAME only set if doingdirectory.
1097 if (doingdirectory && newparent) {
1098 if (error) /* write access check above */
1105 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1106 * get an extra ref so we wind up with just an unlocked, ref'd
1107 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp
1108 * isn't locked so we can't use the out target).
1111 error = doscheckpath(ip, dp);
1112 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1118 * relookup no longer messes with the ref count. tdvp must
1119 * be unlocked on entry and on success will be locked on
1122 error = relookup(tdvp, &tvp, tcnp);
1124 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1132 * tvp and tdvp are now locked again.
1135 xp = tvp ? VTODE(tvp) : NULL;
1139 * tvp and tdvp are now locked again, the 'bad' target can be used
1140 * to clean them up again. Delete an existant target and clean
1141 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up.
1145 * Target must be empty if a directory and have no links
1146 * to it. Also, ensure source and target are compatible
1147 * (both directories, or both not directories).
1149 if (xp->de_Attributes & ATTR_DIRECTORY) {
1150 if (!dosdirempty(xp)) {
1154 if (!doingdirectory) {
1158 } else if (doingdirectory) {
1162 error = removede(dp, xp);
1171 * Convert the filename in tcnp into a dos filename. We copy this
1172 * into the denode and directory entry for the destination
1175 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1180 * Since from wasn't locked at various places above, we have to do
1181 * a relookup here. If the target and source are the same directory
1182 * we have to unlock the target directory in order to safely relookup
1183 * the source, because relookup expects its directory to be unlocked.
1185 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must
1186 * now take that into account.
1188 * The tdvp locking issues make this a real mess.
1190 fcnp->cn_flags &= ~CNP_MODMASK;
1191 fcnp->cn_flags |= CNP_LOCKPARENT;
1193 VOP_UNLOCK(tdvp, 0, td);
1194 error = relookup(fdvp, &fvp, fcnp);
1195 if (error || fvp == NULL) {
1197 * From name has disappeared. Note: fdvp might == tdvp.
1199 * DE_RENAME is only set if doingdirectory.
1202 panic("rename: lost dir entry");
1203 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1216 * No error occured. tdvp, fdvp and fvp are all locked. If
1217 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned
1218 * up. ap->a_fvp is still refd.
1222 from_diroffset = zp->de_fndoffset;
1225 * Ensure that the directory entry still exists and has not
1226 * changed till now. If the source is a file the entry may
1227 * have been unlinked or renamed. In either case there is
1228 * no further work to be done. If the source is a directory
1229 * then it cannot have been rmdir'ed or renamed; this is
1230 * prohibited by the DE_RENAME flag.
1232 * DE_RENAME is only set if doingdirectory.
1236 panic("rename: lost dir entry");
1239 u_long new_dirclust;
1240 u_long new_diroffset;
1243 * First write a new entry in the destination
1244 * directory and mark the entry in the source directory
1245 * as deleted. Then move the denode to the correct hash
1246 * chain for its new location in the filesystem. And, if
1247 * we moved a directory, then update its .. entry to point
1248 * to the new parent directory.
1250 bcopy(ip->de_Name, oldname, 11);
1251 bcopy(toname, ip->de_Name, 11); /* update denode */
1252 dp->de_fndoffset = to_diroffset;
1253 dp->de_fndcnt = to_count;
1254 error = createde(ip, dp, (struct denode **)0, tcnp);
1256 bcopy(oldname, ip->de_Name, 11);
1260 zp->de_fndoffset = from_diroffset;
1261 error = removede(zp, ip);
1263 /* XXX should really panic here, fs is corrupt */
1266 if (!doingdirectory) {
1267 error = pcbmap(dp, de_cluster(pmp, to_diroffset),
1268 NULL, &new_dirclust, NULL);
1270 /* XXX should really panic here, fs is corrupt */
1273 if (new_dirclust == MSDOSFSROOT)
1274 new_diroffset = to_diroffset;
1276 new_diroffset = to_diroffset & pmp->pm_crbomask;
1277 msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1282 * If we moved a directory to a new parent directory, then we must
1283 * fixup the ".." entry in the moved directory.
1285 if (doingdirectory && newparent) {
1286 cn = ip->de_StartCluster;
1287 if (cn == MSDOSFSROOT) {
1288 /* this should never happen */
1289 panic("msdosfs_rename(): updating .. in root directory?");
1291 bn = xcntobn(pmp, cn);
1293 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
1295 /* XXX should really panic here, fs is corrupt */
1299 dotdotp = (struct direntry *)bp->b_data + 1;
1300 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1302 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1305 /* XXX should really panic here, fs is corrupt */
1311 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd
1315 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */
1326 * 'bad' target: xp governs tvp. tvp and tdvp arel ocked, fdvp and fvp
1327 * are not locked. ip points to fvp's inode which may have DE_RENAME
1336 * 'out' target: tvp and tdvp have already been cleaned up.
1339 ip->de_flag &= ~DE_RENAME;
1347 struct direntry dot;
1348 struct direntry dotdot;
1349 } dosdirtemplate = {
1350 { ". ", " ", /* the . entry */
1351 ATTR_DIRECTORY, /* file attribute */
1353 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1354 { 0, 0 }, /* access date */
1355 { 0, 0 }, /* high bits of start cluster */
1356 { 210, 4 }, { 210, 4 }, /* modify time & date */
1357 { 0, 0 }, /* startcluster */
1358 { 0, 0, 0, 0 } /* filesize */
1360 { ".. ", " ", /* the .. entry */
1361 ATTR_DIRECTORY, /* file attribute */
1363 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1364 { 0, 0 }, /* access date */
1365 { 0, 0 }, /* high bits of start cluster */
1366 { 210, 4 }, { 210, 4 }, /* modify time & date */
1367 { 0, 0 }, /* startcluster */
1368 { 0, 0, 0, 0 } /* filesize */
1373 * msdosfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1374 * struct componentname *a_cnp, struct vattr *a_vap)
1377 msdosfs_mkdir(struct vop_old_mkdir_args *ap)
1379 struct componentname *cnp = ap->a_cnp;
1381 struct denode *pdep = VTODE(ap->a_dvp);
1382 struct direntry *denp;
1383 struct msdosfsmount *pmp = pdep->de_pmp;
1385 u_long newcluster, pcl;
1388 struct denode ndirent;
1392 * If this is the root directory and there is no space left we
1393 * can't do anything. This is because the root directory can not
1396 if (pdep->de_StartCluster == MSDOSFSROOT
1397 && pdep->de_fndoffset >= pdep->de_FileSize) {
1403 * Allocate a cluster to hold the about to be created directory.
1405 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1409 bzero(&ndirent, sizeof(ndirent));
1410 ndirent.de_pmp = pmp;
1411 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1413 DETIMES(&ndirent, &ts, &ts, &ts);
1416 * Now fill the cluster with the "." and ".." entries. And write
1417 * the cluster to disk. This way it is there for the parent
1418 * directory to be pointing at if there were a crash.
1420 bn = xcntobn(pmp, newcluster);
1421 /* always succeeds */
1422 bp = getblk(pmp->pm_devvp, de_bntodoff(pmp, bn),
1423 pmp->pm_bpcluster, 0, 0);
1424 bzero(bp->b_data, pmp->pm_bpcluster);
1425 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1426 denp = (struct direntry *)bp->b_data;
1427 putushort(denp[0].deStartCluster, newcluster);
1428 putushort(denp[0].deCDate, ndirent.de_CDate);
1429 putushort(denp[0].deCTime, ndirent.de_CTime);
1430 denp[0].deCHundredth = ndirent.de_CHun;
1431 putushort(denp[0].deADate, ndirent.de_ADate);
1432 putushort(denp[0].deMDate, ndirent.de_MDate);
1433 putushort(denp[0].deMTime, ndirent.de_MTime);
1434 pcl = pdep->de_StartCluster;
1435 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1437 putushort(denp[1].deStartCluster, pcl);
1438 putushort(denp[1].deCDate, ndirent.de_CDate);
1439 putushort(denp[1].deCTime, ndirent.de_CTime);
1440 denp[1].deCHundredth = ndirent.de_CHun;
1441 putushort(denp[1].deADate, ndirent.de_ADate);
1442 putushort(denp[1].deMDate, ndirent.de_MDate);
1443 putushort(denp[1].deMTime, ndirent.de_MTime);
1445 putushort(denp[0].deHighClust, newcluster >> 16);
1446 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1454 * Now build up a directory entry pointing to the newly allocated
1455 * cluster. This will be written to an empty slot in the parent
1458 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1462 ndirent.de_Attributes = ATTR_DIRECTORY;
1463 ndirent.de_LowerCase = 0;
1464 ndirent.de_StartCluster = newcluster;
1465 ndirent.de_FileSize = 0;
1466 ndirent.de_dev = pdep->de_dev;
1467 ndirent.de_devvp = pdep->de_devvp;
1468 error = createde(&ndirent, pdep, &dep, cnp);
1471 *ap->a_vpp = DETOV(dep);
1475 clusterfree(pmp, newcluster, NULL);
1481 * msdosfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1482 * struct componentname *a_cnp)
1485 msdosfs_rmdir(struct vop_old_rmdir_args *ap)
1487 struct vnode *vp = ap->a_vp;
1488 struct vnode *dvp = ap->a_dvp;
1489 struct componentname *cnp = ap->a_cnp;
1490 struct denode *ip, *dp;
1491 struct thread *td = cnp->cn_td;
1498 * Verify the directory is empty (and valid).
1499 * (Rmdir ".." won't be valid since
1500 * ".." will contain a reference to
1501 * the current directory and thus be
1505 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1510 * Delete the entry from the directory. For dos filesystems this
1511 * gets rid of the directory entry on disk, the in memory copy
1512 * still exists but the de_refcnt is <= 0. This prevents it from
1513 * being found by deget(). When the vput() on dep is done we give
1514 * up access and eventually msdosfs_reclaim() will be called which
1515 * will remove it from the denode cache.
1517 error = removede(dp, ip);
1521 * This is where we decrement the link count in the parent
1522 * directory. Since dos filesystems don't do this we just purge
1525 VOP_UNLOCK(dvp, 0, td);
1527 * Truncate the directory that is being deleted.
1529 error = detrunc(ip, (u_long)0, IO_SYNC, td);
1531 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1537 * DOS filesystems don't know what symlinks are.
1539 * msdosfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1540 * struct componentname *a_cnp, struct vattr *a_vap,
1544 msdosfs_symlink(struct vop_old_symlink_args *ap)
1546 return (EOPNOTSUPP);
1550 * msdosfs_readdir(struct vnode *a_vp, struct uio *a_uio,
1551 * struct ucred *a_cred, int *a_eofflag, int *a_ncookies,
1552 * u_long **a_cookies)
1555 msdosfs_readdir(struct vop_readdir_args *ap)
1567 struct denode *dep = VTODE(ap->a_vp);
1568 struct msdosfsmount *pmp = dep->de_pmp;
1569 struct direntry *dentp;
1570 struct uio *uio = ap->a_uio;
1571 u_long *cookies = NULL;
1578 char *d_name_storage = NULL;
1581 #ifdef MSDOSFS_DEBUG
1582 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1583 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1587 * msdosfs_readdir() won't operate properly on regular files since
1588 * it does i/o only with the the filesystem vnode, and hence can
1589 * retrieve the wrong block from the buffer cache for a plain file.
1590 * So, fail attempts to readdir() on a plain file.
1592 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1596 * If the user buffer is smaller than the size of one dos directory
1597 * entry or the file offset is not a multiple of the size of a
1598 * directory entry, then we fail the read.
1600 off = offset = uio->uio_offset;
1601 if (uio->uio_resid < sizeof(struct direntry) ||
1602 (offset & (sizeof(struct direntry) - 1)))
1605 if (ap->a_ncookies) {
1606 ncookies = uio->uio_resid / 16;
1607 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1609 *ap->a_cookies = cookies;
1610 *ap->a_ncookies = ncookies;
1613 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1616 * If they are reading from the root directory then, we simulate
1617 * the . and .. entries since these don't exist in the root
1618 * directory. We also set the offset bias to make up for having to
1619 * simulate these entries. By this I mean that at file offset 64 we
1620 * read the first entry in the root directory that lives on disk.
1622 if (dep->de_StartCluster == MSDOSFSROOT
1623 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1625 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1628 bias = 2 * sizeof(struct direntry);
1629 if (offset < bias) {
1630 for (n = (int)offset / sizeof(struct direntry); n < 2;
1633 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1641 } else /* if (n == 1) */{
1645 if (vop_write_dirent(&error, uio, d_ino, d_type,
1650 offset += sizeof(struct direntry);
1653 *cookies++ = offset;
1654 if (--ncookies <= 0)
1661 d_name_storage = malloc(WIN_MAXLEN, M_TEMP, M_WAITOK);
1664 while (uio->uio_resid > 0) {
1665 lbn = de_off2cn(pmp, offset - bias);
1666 on = (offset - bias) & pmp->pm_crbomask;
1667 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1668 diff = dep->de_FileSize - (offset - bias);
1672 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1675 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
1678 free(d_name_storage, M_TEMP);
1681 n = min(n, blsize - bp->b_resid);
1684 * Convert from dos directory entries to fs-independent
1685 * directory entries.
1687 for (dentp = (struct direntry *)(bp->b_data + on);
1688 (char *)dentp < bp->b_data + on + n;
1689 dentp++, offset += sizeof(struct direntry)) {
1691 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1692 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1694 d_name = d_name_storage;
1696 * If this is an unused entry, we can stop.
1698 if (dentp->deName[0] == SLOT_EMPTY) {
1703 * Skip deleted entries.
1705 if (dentp->deName[0] == SLOT_DELETED) {
1711 * Handle Win95 long directory entries
1713 if (dentp->deAttributes == ATTR_WIN95) {
1714 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1716 chksum = win2unixfn((struct winentry *)dentp,
1717 d_name, &d_namlen, chksum,
1718 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1724 * Skip volume labels
1726 if (dentp->deAttributes & ATTR_VOLUME) {
1731 * This computation of d_ino must match
1732 * the computation of va_fileid in
1735 if (dentp->deAttributes & ATTR_DIRECTORY) {
1736 d_ino = getushort(dentp->deStartCluster);
1738 d_ino |= getushort(dentp->deHighClust) << 16;
1739 /* if this is the root directory */
1740 if (d_ino != MSDOSFSROOT)
1741 d_ino = xcntobn(pmp, d_ino) * dirsperblk;
1742 else if (FAT32(pmp))
1743 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1749 d_ino = offset / sizeof(struct direntry);
1752 if (chksum != winChksum(dentp->deName)) {
1753 d_namlen = dos2unixfn(dentp->deName,
1755 dentp->deLowerCase |
1756 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1757 (LCASE_BASE | LCASE_EXT) : 0),
1758 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1760 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1764 if (vop_write_dirent(&error, uio, d_ino, d_type,
1765 d_namlen, d_name)) {
1775 *cookies++ = offset + sizeof(struct direntry);
1776 if (--ncookies <= 0) {
1781 off = offset + sizeof(struct direntry);
1786 if (d_name_storage != NULL)
1787 free(d_name_storage, M_TEMP);
1789 /* Subtract unused cookies */
1791 *ap->a_ncookies -= ncookies;
1793 uio->uio_offset = off;
1796 * Set the eofflag (NFS uses it)
1798 if (ap->a_eofflag) {
1799 if (dep->de_FileSize - (offset - bias) <= 0)
1808 * vp - address of vnode file the file
1809 * bn - which cluster we are interested in mapping to a filesystem block number.
1810 * vpp - returns the vnode for the block special file holding the filesystem
1811 * containing the file of interest
1812 * bnp - address of where to return the filesystem relative block number
1814 * msdosfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
1815 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1818 msdosfs_bmap(struct vop_bmap_args *ap)
1820 struct denode *dep = VTODE(ap->a_vp);
1821 struct msdosfsmount *pmp = dep->de_pmp;
1826 if (ap->a_vpp != NULL)
1827 *ap->a_vpp = dep->de_devvp;
1828 if (ap->a_doffsetp == NULL)
1832 * Sequential clusters should be counted here.
1839 KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0);
1840 lbn = de_off2cn(pmp, ap->a_loffset);
1841 error = pcbmap(dep, lbn, &dbn, NULL, NULL);
1842 if (error || dbn == (daddr_t)-1) {
1843 *ap->a_doffsetp = NOOFFSET;
1845 *ap->a_doffsetp = de_bntodoff(pmp, dbn);
1851 * msdosfs_strategy(struct vnode *a_vp, struct bio *a_bio)
1854 msdosfs_strategy(struct vop_strategy_args *ap)
1856 struct bio *bio = ap->a_bio;
1858 struct buf *bp = bio->bio_buf;
1859 struct vnode *vp = ap->a_vp;
1860 struct denode *dep = VTODE(vp);
1861 struct msdosfsmount *pmp = dep->de_pmp;
1865 if (vp->v_type == VBLK || vp->v_type == VCHR)
1866 panic("msdosfs_strategy: spec");
1868 * If we don't already know the filesystem relative block number
1869 * then get it using pcbmap(). If pcbmap() returns the block
1870 * number as -1 then we've got a hole in the file. DOS filesystems
1871 * don't allow files with holes, so we shouldn't ever see this.
1873 nbio = push_bio(bio);
1874 if (nbio->bio_offset == NOOFFSET) {
1875 error = pcbmap(dep, de_off2cn(pmp, bio->bio_offset),
1876 &dblkno, NULL, NULL);
1878 bp->b_error = error;
1879 bp->b_flags |= B_ERROR;
1880 /* I/O was never started on nbio, must biodone(bio) */
1884 if (dblkno == (daddr_t)-1) {
1885 nbio->bio_offset = NOOFFSET;
1888 nbio->bio_offset = de_bntodoff(pmp, dblkno);
1891 if (nbio->bio_offset == NOOFFSET) {
1892 /* I/O was never started on nbio, must biodone(bio) */
1897 * Read/write the block from/to the disk that contains the desired
1900 vn_strategy(dep->de_devvp, nbio);
1905 * msdosfs_print(struct vnode *vp)
1908 msdosfs_print(struct vop_print_args *ap)
1910 struct denode *dep = VTODE(ap->a_vp);
1913 "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1914 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1915 printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1916 lockmgr_printinfo(&ap->a_vp->v_lock);
1922 * msdosfs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1925 msdosfs_pathconf(struct vop_pathconf_args *ap)
1927 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1929 switch (ap->a_name) {
1934 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1937 *ap->a_retval = PATH_MAX;
1939 case _PC_CHOWN_RESTRICTED:
1954 * XXX By default, wimp out... note that a_offset is ignored (and always
1958 msdosfs_getpages(struct vop_getpages_args *ap)
1960 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1967 * XXX By default, wimp out... note that a_offset is ignored (and always
1971 msdosfs_putpages(struct vop_putpages_args *ap)
1973 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1974 ap->a_sync, ap->a_rtvals);
1977 /* Global vfs data structures for msdosfs */
1978 struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1979 { &vop_default_desc, vop_defaultop },
1980 { &vop_access_desc, (vnodeopv_entry_t) msdosfs_access },
1981 { &vop_bmap_desc, (vnodeopv_entry_t) msdosfs_bmap },
1982 { &vop_old_lookup_desc, (vnodeopv_entry_t) msdosfs_lookup },
1983 { &vop_open_desc, (vnodeopv_entry_t) msdosfs_open },
1984 { &vop_close_desc, (vnodeopv_entry_t) msdosfs_close },
1985 { &vop_old_create_desc, (vnodeopv_entry_t) msdosfs_create },
1986 { &vop_fsync_desc, (vnodeopv_entry_t) msdosfs_fsync },
1987 { &vop_getattr_desc, (vnodeopv_entry_t) msdosfs_getattr },
1988 { &vop_inactive_desc, (vnodeopv_entry_t) msdosfs_inactive },
1989 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
1990 { &vop_old_link_desc, (vnodeopv_entry_t) msdosfs_link },
1991 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
1992 { &vop_old_mkdir_desc, (vnodeopv_entry_t) msdosfs_mkdir },
1993 { &vop_old_mknod_desc, (vnodeopv_entry_t) msdosfs_mknod },
1994 { &vop_pathconf_desc, (vnodeopv_entry_t) msdosfs_pathconf },
1995 { &vop_print_desc, (vnodeopv_entry_t) msdosfs_print },
1996 { &vop_read_desc, (vnodeopv_entry_t) msdosfs_read },
1997 { &vop_readdir_desc, (vnodeopv_entry_t) msdosfs_readdir },
1998 { &vop_reclaim_desc, (vnodeopv_entry_t) msdosfs_reclaim },
1999 { &vop_old_remove_desc, (vnodeopv_entry_t) msdosfs_remove },
2000 { &vop_old_rename_desc, (vnodeopv_entry_t) msdosfs_rename },
2001 { &vop_old_rmdir_desc, (vnodeopv_entry_t) msdosfs_rmdir },
2002 { &vop_setattr_desc, (vnodeopv_entry_t) msdosfs_setattr },
2003 { &vop_strategy_desc, (vnodeopv_entry_t) msdosfs_strategy },
2004 { &vop_old_symlink_desc, (vnodeopv_entry_t) msdosfs_symlink },
2005 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
2006 { &vop_write_desc, (vnodeopv_entry_t) msdosfs_write },
2007 { &vop_getpages_desc, (vnodeopv_entry_t) msdosfs_getpages },
2008 { &vop_putpages_desc, (vnodeopv_entry_t) msdosfs_putpages },