]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/msdosfs/msdosfs_vnops.c
Upgrade Unbound to 1.6.3. More to follow.
[FreeBSD/FreeBSD.git] / sys / fs / msdosfs / msdosfs_vnops.c
1 /* $FreeBSD$ */
2 /*      $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $   */
3
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9  * All rights reserved.
10  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by TooLs GmbH.
23  * 4. The name of TooLs GmbH may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*-
38  * Written by Paul Popelka (paulp@uts.amdahl.com)
39  *
40  * You can do anything you want with this software, just don't say you wrote
41  * it, and don't remove this notice.
42  *
43  * This software is provided "as is".
44  *
45  * The author supplies this software to be publicly redistributed on the
46  * understanding that the author is not responsible for the correct
47  * functioning of this software in any circumstances and is not liable for
48  * any damages caused by this software.
49  *
50  * October 1992
51  */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/bio.h>
56 #include <sys/buf.h>
57 #include <sys/clock.h>
58 #include <sys/dirent.h>
59 #include <sys/lock.h>
60 #include <sys/lockf.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/mutex.h>
64 #include <sys/namei.h>
65 #include <sys/priv.h>
66 #include <sys/stat.h>
67 #include <sys/sysctl.h>
68 #include <sys/unistd.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vnode_pager.h>
75
76 #include <fs/msdosfs/bpb.h>
77 #include <fs/msdosfs/direntry.h>
78 #include <fs/msdosfs/denode.h>
79 #include <fs/msdosfs/fat.h>
80 #include <fs/msdosfs/msdosfsmount.h>
81
82 #define DOS_FILESIZE_MAX        0xffffffff
83
84 /*
85  * Prototypes for MSDOSFS vnode operations
86  */
87 static vop_create_t     msdosfs_create;
88 static vop_mknod_t      msdosfs_mknod;
89 static vop_open_t       msdosfs_open;
90 static vop_close_t      msdosfs_close;
91 static vop_access_t     msdosfs_access;
92 static vop_getattr_t    msdosfs_getattr;
93 static vop_setattr_t    msdosfs_setattr;
94 static vop_read_t       msdosfs_read;
95 static vop_write_t      msdosfs_write;
96 static vop_fsync_t      msdosfs_fsync;
97 static vop_remove_t     msdosfs_remove;
98 static vop_link_t       msdosfs_link;
99 static vop_rename_t     msdosfs_rename;
100 static vop_mkdir_t      msdosfs_mkdir;
101 static vop_rmdir_t      msdosfs_rmdir;
102 static vop_symlink_t    msdosfs_symlink;
103 static vop_readdir_t    msdosfs_readdir;
104 static vop_bmap_t       msdosfs_bmap;
105 static vop_getpages_t   msdosfs_getpages;
106 static vop_strategy_t   msdosfs_strategy;
107 static vop_print_t      msdosfs_print;
108 static vop_pathconf_t   msdosfs_pathconf;
109 static vop_vptofh_t     msdosfs_vptofh;
110
111 /*
112  * Some general notes:
113  *
114  * In the ufs filesystem the inodes, superblocks, and indirect blocks are
115  * read/written using the vnode for the filesystem. Blocks that represent
116  * the contents of a file are read/written using the vnode for the file
117  * (including directories when they are read/written as files). This
118  * presents problems for the dos filesystem because data that should be in
119  * an inode (if dos had them) resides in the directory itself.  Since we
120  * must update directory entries without the benefit of having the vnode
121  * for the directory we must use the vnode for the filesystem.  This means
122  * that when a directory is actually read/written (via read, write, or
123  * readdir, or seek) we must use the vnode for the filesystem instead of
124  * the vnode for the directory as would happen in ufs. This is to insure we
125  * retrieve the correct block from the buffer cache since the hash value is
126  * based upon the vnode address and the desired block number.
127  */
128
129 /*
130  * Create a regular file. On entry the directory to contain the file being
131  * created is locked.  We must release before we return. We must also free
132  * the pathname buffer pointed at by cnp->cn_pnbuf, always on error, or
133  * only if the SAVESTART bit in cn_flags is clear on success.
134  */
135 static int
136 msdosfs_create(struct vop_create_args *ap)
137 {
138         struct componentname *cnp = ap->a_cnp;
139         struct denode ndirent;
140         struct denode *dep;
141         struct denode *pdep = VTODE(ap->a_dvp);
142         struct timespec ts;
143         int error;
144
145 #ifdef MSDOSFS_DEBUG
146         printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
147 #endif
148
149         /*
150          * If this is the root directory and there is no space left we
151          * can't do anything.  This is because the root directory can not
152          * change size.
153          */
154         if (pdep->de_StartCluster == MSDOSFSROOT
155             && pdep->de_fndoffset >= pdep->de_FileSize) {
156                 error = ENOSPC;
157                 goto bad;
158         }
159
160         /*
161          * Create a directory entry for the file, then call createde() to
162          * have it installed. NOTE: DOS files are always executable.  We
163          * use the absence of the owner write bit to make the file
164          * readonly.
165          */
166 #ifdef DIAGNOSTIC
167         if ((cnp->cn_flags & HASBUF) == 0)
168                 panic("msdosfs_create: no name");
169 #endif
170         memset(&ndirent, 0, sizeof(ndirent));
171         error = uniqdosname(pdep, cnp, ndirent.de_Name);
172         if (error)
173                 goto bad;
174
175         ndirent.de_Attributes = ATTR_ARCHIVE;
176         ndirent.de_LowerCase = 0;
177         ndirent.de_StartCluster = 0;
178         ndirent.de_FileSize = 0;
179         ndirent.de_pmp = pdep->de_pmp;
180         ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
181         vfs_timestamp(&ts);
182         DETIMES(&ndirent, &ts, &ts, &ts);
183         error = createde(&ndirent, pdep, &dep, cnp);
184         if (error)
185                 goto bad;
186         *ap->a_vpp = DETOV(dep);
187         if ((cnp->cn_flags & MAKEENTRY) != 0)
188                 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
189         return (0);
190
191 bad:
192         return (error);
193 }
194
195 static int
196 msdosfs_mknod(struct vop_mknod_args *ap)
197 {
198
199     return (EINVAL);
200 }
201
202 static int
203 msdosfs_open(struct vop_open_args *ap)
204 {
205         struct denode *dep = VTODE(ap->a_vp);
206         vnode_create_vobject(ap->a_vp, dep->de_FileSize, ap->a_td);
207         return 0;
208 }
209
210 static int
211 msdosfs_close(struct vop_close_args *ap)
212 {
213         struct vnode *vp = ap->a_vp;
214         struct denode *dep = VTODE(vp);
215         struct timespec ts;
216
217         VI_LOCK(vp);
218         if (vp->v_usecount > 1) {
219                 vfs_timestamp(&ts);
220                 DETIMES(dep, &ts, &ts, &ts);
221         }
222         VI_UNLOCK(vp);
223         return 0;
224 }
225
226 static int
227 msdosfs_access(struct vop_access_args *ap)
228 {
229         struct vnode *vp = ap->a_vp;
230         struct denode *dep = VTODE(ap->a_vp);
231         struct msdosfsmount *pmp = dep->de_pmp;
232         mode_t file_mode;
233         accmode_t accmode = ap->a_accmode;
234
235         file_mode = S_IRWXU|S_IRWXG|S_IRWXO;
236         file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
237
238         /*
239          * Disallow writing to directories and regular files if the
240          * filesystem is read-only.
241          */
242         if (accmode & VWRITE) {
243                 switch (vp->v_type) {
244                 case VREG:
245                 case VDIR:
246                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
247                                 return (EROFS);
248                         break;
249                 default:
250                         break;
251                 }
252         }
253
254         return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
255             ap->a_accmode, ap->a_cred, NULL));
256 }
257
258 static int
259 msdosfs_getattr(struct vop_getattr_args *ap)
260 {
261         struct denode *dep = VTODE(ap->a_vp);
262         struct msdosfsmount *pmp = dep->de_pmp;
263         struct vattr *vap = ap->a_vap;
264         mode_t mode;
265         struct timespec ts;
266         u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
267         uint64_t fileid;
268
269         vfs_timestamp(&ts);
270         DETIMES(dep, &ts, &ts, &ts);
271         vap->va_fsid = dev2udev(pmp->pm_dev);
272         /*
273          * The following computation of the fileid must be the same as that
274          * used in msdosfs_readdir() to compute d_fileno. If not, pwd
275          * doesn't work.
276          */
277         if (dep->de_Attributes & ATTR_DIRECTORY) {
278                 fileid = (uint64_t)cntobn(pmp, dep->de_StartCluster) *
279                     dirsperblk;
280                 if (dep->de_StartCluster == MSDOSFSROOT)
281                         fileid = 1;
282         } else {
283                 fileid = (uint64_t)cntobn(pmp, dep->de_dirclust) *
284                     dirsperblk;
285                 if (dep->de_dirclust == MSDOSFSROOT)
286                         fileid = (uint64_t)roottobn(pmp, 0) * dirsperblk;
287                 fileid += (uoff_t)dep->de_diroffset / sizeof(struct direntry);
288         }
289         vap->va_fileid = fileid;
290
291         mode = S_IRWXU|S_IRWXG|S_IRWXO;
292         if (dep->de_Attributes & ATTR_READONLY)
293                 mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
294         vap->va_mode = mode & 
295             (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
296         vap->va_uid = pmp->pm_uid;
297         vap->va_gid = pmp->pm_gid;
298         vap->va_nlink = 1;
299         vap->va_rdev = NODEV;
300         vap->va_size = dep->de_FileSize;
301         fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
302         vap->va_ctime = vap->va_mtime;
303         if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
304                 fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
305                 fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
306                     0, &vap->va_birthtime);
307         } else {
308                 vap->va_atime = vap->va_mtime;
309                 vap->va_birthtime.tv_sec = -1;
310                 vap->va_birthtime.tv_nsec = 0;
311         }
312         vap->va_flags = 0;
313         if (dep->de_Attributes & ATTR_ARCHIVE)
314                 vap->va_flags |= UF_ARCHIVE;
315         if (dep->de_Attributes & ATTR_HIDDEN)
316                 vap->va_flags |= UF_HIDDEN;
317         if (dep->de_Attributes & ATTR_READONLY)
318                 vap->va_flags |= UF_READONLY;
319         if (dep->de_Attributes & ATTR_SYSTEM)
320                 vap->va_flags |= UF_SYSTEM;
321         vap->va_gen = 0;
322         vap->va_blocksize = pmp->pm_bpcluster;
323         vap->va_bytes =
324             (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
325         vap->va_type = ap->a_vp->v_type;
326         vap->va_filerev = dep->de_modrev;
327         return (0);
328 }
329
330 static int
331 msdosfs_setattr(struct vop_setattr_args *ap)
332 {
333         struct vnode *vp = ap->a_vp;
334         struct denode *dep = VTODE(ap->a_vp);
335         struct msdosfsmount *pmp = dep->de_pmp;
336         struct vattr *vap = ap->a_vap;
337         struct ucred *cred = ap->a_cred;
338         struct thread *td = curthread;
339         int error = 0;
340
341 #ifdef MSDOSFS_DEBUG
342         printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
343             ap->a_vp, vap, cred);
344 #endif
345
346         /*
347          * Check for unsettable attributes.
348          */
349         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
350             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
351             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
352             (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
353 #ifdef MSDOSFS_DEBUG
354                 printf("msdosfs_setattr(): returning EINVAL\n");
355                 printf("    va_type %d, va_nlink %llx, va_fsid %llx, va_fileid %llx\n",
356                     vap->va_type, (unsigned long long)vap->va_nlink,
357                     (unsigned long long)vap->va_fsid,
358                     (unsigned long long)vap->va_fileid);
359                 printf("    va_blocksize %lx, va_rdev %llx, va_bytes %llx, va_gen %lx\n",
360                     vap->va_blocksize, (unsigned long long)vap->va_rdev,
361                     (unsigned long long)vap->va_bytes, vap->va_gen);
362                 printf("    va_uid %x, va_gid %x\n",
363                     vap->va_uid, vap->va_gid);
364 #endif
365                 return (EINVAL);
366         }
367
368         /*
369          * We don't allow setting attributes on the root directory.
370          * The special case for the root directory is because before
371          * FAT32, the root directory didn't have an entry for itself
372          * (and was otherwise special).  With FAT32, the root
373          * directory is not so special, but still doesn't have an
374          * entry for itself.
375          */
376         if (vp->v_vflag & VV_ROOT)
377                 return (EINVAL);
378
379         if (vap->va_flags != VNOVAL) {
380                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
381                         return (EROFS);
382                 if (cred->cr_uid != pmp->pm_uid) {
383                         error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
384                         if (error)
385                                 return (error);
386                 }
387                 /*
388                  * We are very inconsistent about handling unsupported
389                  * attributes.  We ignored the access time and the
390                  * read and execute bits.  We were strict for the other
391                  * attributes.
392                  */
393                 if (vap->va_flags & ~(UF_ARCHIVE | UF_HIDDEN | UF_READONLY |
394                     UF_SYSTEM))
395                         return EOPNOTSUPP;
396                 if (vap->va_flags & UF_ARCHIVE)
397                         dep->de_Attributes |= ATTR_ARCHIVE;
398                 else
399                         dep->de_Attributes &= ~ATTR_ARCHIVE;
400                 if (vap->va_flags & UF_HIDDEN)
401                         dep->de_Attributes |= ATTR_HIDDEN;
402                 else
403                         dep->de_Attributes &= ~ATTR_HIDDEN;
404                 /* We don't allow changing the readonly bit on directories. */
405                 if (vp->v_type != VDIR) {
406                         if (vap->va_flags & UF_READONLY)
407                                 dep->de_Attributes |= ATTR_READONLY;
408                         else
409                                 dep->de_Attributes &= ~ATTR_READONLY;
410                 }
411                 if (vap->va_flags & UF_SYSTEM)
412                         dep->de_Attributes |= ATTR_SYSTEM;
413                 else
414                         dep->de_Attributes &= ~ATTR_SYSTEM;
415                 dep->de_flag |= DE_MODIFIED;
416         }
417
418         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
419                 uid_t uid;
420                 gid_t gid;
421
422                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
423                         return (EROFS);
424                 uid = vap->va_uid;
425                 if (uid == (uid_t)VNOVAL)
426                         uid = pmp->pm_uid;
427                 gid = vap->va_gid;
428                 if (gid == (gid_t)VNOVAL)
429                         gid = pmp->pm_gid;
430                 if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
431                     (gid != pmp->pm_gid && !groupmember(gid, cred))) {
432                         error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0);
433                         if (error)
434                                 return (error);
435                 }
436                 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
437                         return EINVAL;
438         }
439
440         if (vap->va_size != VNOVAL) {
441                 switch (vp->v_type) {
442                 case VDIR:
443                         return (EISDIR);
444                 case VREG:
445                         /*
446                          * Truncation is only supported for regular files,
447                          * Disallow it if the filesystem is read-only.
448                          */
449                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
450                                 return (EROFS);
451                         break;
452                 default:
453                         /*
454                          * According to POSIX, the result is unspecified
455                          * for file types other than regular files,
456                          * directories and shared memory objects.  We
457                          * don't support any file types except regular
458                          * files and directories in this file system, so
459                          * this (default) case is unreachable and can do
460                          * anything.  Keep falling through to detrunc()
461                          * for now.
462                          */
463                         break;
464                 }
465                 error = detrunc(dep, vap->va_size, 0, cred);
466                 if (error)
467                         return error;
468         }
469         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
470                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
471                         return (EROFS);
472                 error = vn_utimes_perm(vp, vap, cred, td);
473                 if (error != 0)
474                         return (error);
475                 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
476                     vap->va_atime.tv_sec != VNOVAL) {
477                         dep->de_flag &= ~DE_ACCESS;
478                         timespec2fattime(&vap->va_atime, 0,
479                             &dep->de_ADate, NULL, NULL);
480                 }
481                 if (vap->va_mtime.tv_sec != VNOVAL) {
482                         dep->de_flag &= ~DE_UPDATE;
483                         timespec2fattime(&vap->va_mtime, 0,
484                             &dep->de_MDate, &dep->de_MTime, NULL);
485                 }
486                 /*
487                  * We don't set the archive bit when modifying the time of
488                  * a directory to emulate the Windows/DOS behavior.
489                  */
490                 if (vp->v_type != VDIR)
491                         dep->de_Attributes |= ATTR_ARCHIVE;
492                 dep->de_flag |= DE_MODIFIED;
493         }
494         /*
495          * DOS files only have the ability to have their writability
496          * attribute set, so we use the owner write bit to set the readonly
497          * attribute.
498          */
499         if (vap->va_mode != (mode_t)VNOVAL) {
500                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
501                         return (EROFS);
502                 if (cred->cr_uid != pmp->pm_uid) {
503                         error = priv_check_cred(cred, PRIV_VFS_ADMIN, 0);
504                         if (error)
505                                 return (error);
506                 }
507                 if (vp->v_type != VDIR) {
508                         /* We ignore the read and execute bits. */
509                         if (vap->va_mode & S_IWUSR)
510                                 dep->de_Attributes &= ~ATTR_READONLY;
511                         else
512                                 dep->de_Attributes |= ATTR_READONLY;
513                         dep->de_Attributes |= ATTR_ARCHIVE;
514                         dep->de_flag |= DE_MODIFIED;
515                 }
516         }
517         return (deupdat(dep, 0));
518 }
519
520 static int
521 msdosfs_read(struct vop_read_args *ap)
522 {
523         int error = 0;
524         int blsize;
525         int isadir;
526         ssize_t orig_resid;
527         u_int n;
528         u_long diff;
529         u_long on;
530         daddr_t lbn;
531         daddr_t rablock;
532         int rasize;
533         int seqcount;
534         struct buf *bp;
535         struct vnode *vp = ap->a_vp;
536         struct denode *dep = VTODE(vp);
537         struct msdosfsmount *pmp = dep->de_pmp;
538         struct uio *uio = ap->a_uio;
539
540         /*
541          * If they didn't ask for any data, then we are done.
542          */
543         orig_resid = uio->uio_resid;
544         if (orig_resid == 0)
545                 return (0);
546
547         /*
548          * The caller is supposed to ensure that
549          * uio->uio_offset >= 0 and uio->uio_resid >= 0.
550          * We don't need to check for large offsets as in ffs because
551          * dep->de_FileSize <= DOS_FILESIZE_MAX < OFF_MAX, so large
552          * offsets cannot cause overflow even in theory.
553          */
554
555         seqcount = ap->a_ioflag >> IO_SEQSHIFT;
556
557         isadir = dep->de_Attributes & ATTR_DIRECTORY;
558         do {
559                 if (uio->uio_offset >= dep->de_FileSize)
560                         break;
561                 lbn = de_cluster(pmp, uio->uio_offset);
562                 rablock = lbn + 1;
563                 blsize = pmp->pm_bpcluster;
564                 on = uio->uio_offset & pmp->pm_crbomask;
565                 /*
566                  * If we are operating on a directory file then be sure to
567                  * do i/o with the vnode for the filesystem instead of the
568                  * vnode for the directory.
569                  */
570                 if (isadir) {
571                         /* convert cluster # to block # */
572                         error = pcbmap(dep, lbn, &lbn, 0, &blsize);
573                         if (error == E2BIG) {
574                                 error = EINVAL;
575                                 break;
576                         } else if (error)
577                                 break;
578                         error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
579                 } else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
580                         error = bread(vp, lbn, blsize, NOCRED, &bp);
581                 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
582                         error = cluster_read(vp, dep->de_FileSize, lbn, blsize,
583                             NOCRED, on + uio->uio_resid, seqcount, 0, &bp);
584                 } else if (seqcount > 1) {
585                         rasize = blsize;
586                         error = breadn(vp, lbn,
587                             blsize, &rablock, &rasize, 1, NOCRED, &bp);
588                 } else {
589                         error = bread(vp, lbn, blsize, NOCRED, &bp);
590                 }
591                 if (error) {
592                         brelse(bp);
593                         break;
594                 }
595                 diff = pmp->pm_bpcluster - on;
596                 n = diff > uio->uio_resid ? uio->uio_resid : diff;
597                 diff = dep->de_FileSize - uio->uio_offset;
598                 if (diff < n)
599                         n = diff;
600                 diff = blsize - bp->b_resid;
601                 if (diff < n)
602                         n = diff;
603                 error = vn_io_fault_uiomove(bp->b_data + on, (int) n, uio);
604                 brelse(bp);
605         } while (error == 0 && uio->uio_resid > 0 && n != 0);
606         if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
607             (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
608                 dep->de_flag |= DE_ACCESS;
609         return (error);
610 }
611
612 /*
613  * Write data to a file or directory.
614  */
615 static int
616 msdosfs_write(struct vop_write_args *ap)
617 {
618         int n;
619         int croffset;
620         ssize_t resid;
621         u_long osize;
622         int error = 0;
623         u_long count;
624         int seqcount;
625         daddr_t bn, lastcn;
626         struct buf *bp;
627         int ioflag = ap->a_ioflag;
628         struct uio *uio = ap->a_uio;
629         struct vnode *vp = ap->a_vp;
630         struct vnode *thisvp;
631         struct denode *dep = VTODE(vp);
632         struct msdosfsmount *pmp = dep->de_pmp;
633         struct ucred *cred = ap->a_cred;
634
635 #ifdef MSDOSFS_DEBUG
636         printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
637             vp, uio, ioflag, cred);
638         printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
639             dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
640 #endif
641
642         switch (vp->v_type) {
643         case VREG:
644                 if (ioflag & IO_APPEND)
645                         uio->uio_offset = dep->de_FileSize;
646                 thisvp = vp;
647                 break;
648         case VDIR:
649                 return EISDIR;
650         default:
651                 panic("msdosfs_write(): bad file type");
652         }
653
654         /*
655          * This is needed (unlike in ffs_write()) because we extend the
656          * file outside of the loop but we don't want to extend the file
657          * for writes of 0 bytes.
658          */
659         if (uio->uio_resid == 0)
660                 return (0);
661
662         /*
663          * The caller is supposed to ensure that
664          * uio->uio_offset >= 0 and uio->uio_resid >= 0.
665          */
666         if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
667                 return (EFBIG);
668
669         /*
670          * If they've exceeded their filesize limit, tell them about it.
671          */
672         if (vn_rlimit_fsize(vp, uio, uio->uio_td))
673                 return (EFBIG);
674
675         /*
676          * If the offset we are starting the write at is beyond the end of
677          * the file, then they've done a seek.  Unix filesystems allow
678          * files with holes in them, DOS doesn't so we must fill the hole
679          * with zeroed blocks.
680          */
681         if (uio->uio_offset > dep->de_FileSize) {
682                 error = deextend(dep, uio->uio_offset, cred);
683                 if (error)
684                         return (error);
685         }
686
687         /*
688          * Remember some values in case the write fails.
689          */
690         resid = uio->uio_resid;
691         osize = dep->de_FileSize;
692
693         /*
694          * If we write beyond the end of the file, extend it to its ultimate
695          * size ahead of the time to hopefully get a contiguous area.
696          */
697         if (uio->uio_offset + resid > osize) {
698                 count = de_clcount(pmp, uio->uio_offset + resid) -
699                         de_clcount(pmp, osize);
700                 error = extendfile(dep, count, NULL, NULL, 0);
701                 if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
702                         goto errexit;
703                 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
704         } else
705                 lastcn = de_clcount(pmp, osize) - 1;
706
707         seqcount = ioflag >> IO_SEQSHIFT;
708         do {
709                 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
710                         error = ENOSPC;
711                         break;
712                 }
713
714                 croffset = uio->uio_offset & pmp->pm_crbomask;
715                 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
716                 if (uio->uio_offset + n > dep->de_FileSize) {
717                         dep->de_FileSize = uio->uio_offset + n;
718                         /* The object size needs to be set before buffer is allocated */
719                         vnode_pager_setsize(vp, dep->de_FileSize);
720                 }
721
722                 bn = de_cluster(pmp, uio->uio_offset);
723                 if ((uio->uio_offset & pmp->pm_crbomask) == 0
724                     && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
725                         > de_cluster(pmp, uio->uio_offset)
726                         || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
727                         /*
728                          * If either the whole cluster gets written,
729                          * or we write the cluster from its start beyond EOF,
730                          * then no need to read data from disk.
731                          */
732                         bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0, 0);
733                         /*
734                          * This call to vfs_bio_clrbuf() ensures that
735                          * even if vn_io_fault_uiomove() below faults,
736                          * garbage from the newly instantiated buffer
737                          * is not exposed to the userspace via mmap().
738                          */
739                         vfs_bio_clrbuf(bp);
740                         /*
741                          * Do the bmap now, since pcbmap needs buffers
742                          * for the FAT table. (see msdosfs_strategy)
743                          */
744                         if (bp->b_blkno == bp->b_lblkno) {
745                                 error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
746                                 if (error)
747                                         bp->b_blkno = -1;
748                                 else
749                                         bp->b_blkno = bn;
750                         }
751                         if (bp->b_blkno == -1) {
752                                 brelse(bp);
753                                 if (!error)
754                                         error = EIO;            /* XXX */
755                                 break;
756                         }
757                 } else {
758                         /*
759                          * The block we need to write into exists, so read it in.
760                          */
761                         error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
762                         if (error) {
763                                 brelse(bp);
764                                 break;
765                         }
766                 }
767
768                 /*
769                  * Should these vnode_pager_* functions be done on dir
770                  * files?
771                  */
772
773                 /*
774                  * Copy the data from user space into the buf header.
775                  */
776                 error = vn_io_fault_uiomove(bp->b_data + croffset, n, uio);
777                 if (error) {
778                         brelse(bp);
779                         break;
780                 }
781
782                 /* Prepare for clustered writes in some else clauses. */
783                 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
784                         bp->b_flags |= B_CLUSTEROK;
785
786                 /*
787                  * If IO_SYNC, then each buffer is written synchronously.
788                  * Otherwise, if we have a severe page deficiency then
789                  * write the buffer asynchronously.  Otherwise, if on a
790                  * cluster boundary then write the buffer asynchronously,
791                  * combining it with contiguous clusters if permitted and
792                  * possible, since we don't expect more writes into this
793                  * buffer soon.  Otherwise, do a delayed write because we
794                  * expect more writes into this buffer soon.
795                  */
796                 if (ioflag & IO_SYNC)
797                         (void)bwrite(bp);
798                 else if (vm_page_count_severe() || buf_dirty_count_severe())
799                         bawrite(bp);
800                 else if (n + croffset == pmp->pm_bpcluster) {
801                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
802                                 cluster_write(vp, bp, dep->de_FileSize,
803                                     seqcount, 0);
804                         else
805                                 bawrite(bp);
806                 } else
807                         bdwrite(bp);
808                 dep->de_flag |= DE_UPDATE;
809         } while (error == 0 && uio->uio_resid > 0);
810
811         /*
812          * If the write failed and they want us to, truncate the file back
813          * to the size it was before the write was attempted.
814          */
815 errexit:
816         if (error) {
817                 if (ioflag & IO_UNIT) {
818                         detrunc(dep, osize, ioflag & IO_SYNC, NOCRED);
819                         uio->uio_offset -= resid - uio->uio_resid;
820                         uio->uio_resid = resid;
821                 } else {
822                         detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED);
823                         if (uio->uio_resid != resid)
824                                 error = 0;
825                 }
826         } else if (ioflag & IO_SYNC)
827                 error = deupdat(dep, 1);
828         return (error);
829 }
830
831 /*
832  * Flush the blocks of a file to disk.
833  */
834 static int
835 msdosfs_fsync(struct vop_fsync_args *ap)
836 {
837         struct vnode *devvp;
838         int allerror, error;
839
840         vop_stdfsync(ap);
841
842         /*
843         * If the syncing request comes from fsync(2), sync the entire
844         * FAT and any other metadata that happens to be on devvp.  We
845         * need this mainly for the FAT.  We write the FAT sloppily, and
846         * syncing it all now is the best we can easily do to get all
847         * directory entries associated with the file (not just the file)
848         * fully synced.  The other metadata includes critical metadata
849         * for all directory entries, but only in the MNT_ASYNC case.  We
850         * will soon sync all metadata in the file's directory entry.
851         * Non-critical metadata for associated directory entries only
852         * gets synced accidentally, as in most file systems.
853         */
854         if (ap->a_waitfor == MNT_WAIT) {
855                 devvp = VTODE(ap->a_vp)->de_pmp->pm_devvp;
856                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
857                 allerror = VOP_FSYNC(devvp, MNT_WAIT, ap->a_td);
858                 VOP_UNLOCK(devvp, 0);
859         } else
860                 allerror = 0;
861
862         error = deupdat(VTODE(ap->a_vp), ap->a_waitfor == MNT_WAIT);
863         if (allerror == 0)
864                 allerror = error;
865         return (allerror);
866 }
867
868 static int
869 msdosfs_remove(struct vop_remove_args *ap)
870 {
871         struct denode *dep = VTODE(ap->a_vp);
872         struct denode *ddep = VTODE(ap->a_dvp);
873         int error;
874
875         if (ap->a_vp->v_type == VDIR)
876                 error = EPERM;
877         else
878                 error = removede(ddep, dep);
879 #ifdef MSDOSFS_DEBUG
880         printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
881 #endif
882         return (error);
883 }
884
885 /*
886  * DOS filesystems don't know what links are.
887  */
888 static int
889 msdosfs_link(struct vop_link_args *ap)
890 {
891         return (EOPNOTSUPP);
892 }
893
894 /*
895  * Renames on files require moving the denode to a new hash queue since the
896  * denode's location is used to compute which hash queue to put the file
897  * in. Unless it is a rename in place.  For example "mv a b".
898  *
899  * What follows is the basic algorithm:
900  *
901  * if (file move) {
902  *      if (dest file exists) {
903  *              remove dest file
904  *      }
905  *      if (dest and src in same directory) {
906  *              rewrite name in existing directory slot
907  *      } else {
908  *              write new entry in dest directory
909  *              update offset and dirclust in denode
910  *              move denode to new hash chain
911  *              clear old directory entry
912  *      }
913  * } else {
914  *      directory move
915  *      if (dest directory exists) {
916  *              if (dest is not empty) {
917  *                      return ENOTEMPTY
918  *              }
919  *              remove dest directory
920  *      }
921  *      if (dest and src in same directory) {
922  *              rewrite name in existing entry
923  *      } else {
924  *              be sure dest is not a child of src directory
925  *              write entry in dest directory
926  *              update "." and ".." in moved directory
927  *              clear old directory entry for moved directory
928  *      }
929  * }
930  *
931  * On entry:
932  *      source's parent directory is unlocked
933  *      source file or directory is unlocked
934  *      destination's parent directory is locked
935  *      destination file or directory is locked if it exists
936  *
937  * On exit:
938  *      all denodes should be released
939  */
940 static int
941 msdosfs_rename(struct vop_rename_args *ap)
942 {
943         struct vnode *tdvp = ap->a_tdvp;
944         struct vnode *fvp = ap->a_fvp;
945         struct vnode *fdvp = ap->a_fdvp;
946         struct vnode *tvp = ap->a_tvp;
947         struct componentname *tcnp = ap->a_tcnp;
948         struct componentname *fcnp = ap->a_fcnp;
949         struct denode *ip, *xp, *dp, *zp;
950         u_char toname[12], oldname[11];
951         u_long from_diroffset, to_diroffset;
952         u_char to_count;
953         int doingdirectory = 0, newparent = 0;
954         int error;
955         u_long cn, pcl;
956         daddr_t bn;
957         struct msdosfsmount *pmp;
958         struct direntry *dotdotp;
959         struct buf *bp;
960
961         pmp = VFSTOMSDOSFS(fdvp->v_mount);
962
963 #ifdef DIAGNOSTIC
964         if ((tcnp->cn_flags & HASBUF) == 0 ||
965             (fcnp->cn_flags & HASBUF) == 0)
966                 panic("msdosfs_rename: no name");
967 #endif
968         /*
969          * Check for cross-device rename.
970          */
971         if (fvp->v_mount != tdvp->v_mount ||
972             (tvp && fvp->v_mount != tvp->v_mount)) {
973                 error = EXDEV;
974 abortit:
975                 if (tdvp == tvp)
976                         vrele(tdvp);
977                 else
978                         vput(tdvp);
979                 if (tvp)
980                         vput(tvp);
981                 vrele(fdvp);
982                 vrele(fvp);
983                 return (error);
984         }
985
986         /*
987          * If source and dest are the same, do nothing.
988          */
989         if (tvp == fvp) {
990                 error = 0;
991                 goto abortit;
992         }
993
994         error = vn_lock(fvp, LK_EXCLUSIVE);
995         if (error)
996                 goto abortit;
997         dp = VTODE(fdvp);
998         ip = VTODE(fvp);
999
1000         /*
1001          * Be sure we are not renaming ".", "..", or an alias of ".". This
1002          * leads to a crippled directory tree.  It's pretty tough to do a
1003          * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1004          * doesn't work if the ".." entry is missing.
1005          */
1006         if (ip->de_Attributes & ATTR_DIRECTORY) {
1007                 /*
1008                  * Avoid ".", "..", and aliases of "." for obvious reasons.
1009                  */
1010                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1011                     dp == ip ||
1012                     (fcnp->cn_flags & ISDOTDOT) ||
1013                     (tcnp->cn_flags & ISDOTDOT) ||
1014                     (ip->de_flag & DE_RENAME)) {
1015                         VOP_UNLOCK(fvp, 0);
1016                         error = EINVAL;
1017                         goto abortit;
1018                 }
1019                 ip->de_flag |= DE_RENAME;
1020                 doingdirectory++;
1021         }
1022
1023         /*
1024          * When the target exists, both the directory
1025          * and target vnodes are returned locked.
1026          */
1027         dp = VTODE(tdvp);
1028         xp = tvp ? VTODE(tvp) : NULL;
1029         /*
1030          * Remember direntry place to use for destination
1031          */
1032         to_diroffset = dp->de_fndoffset;
1033         to_count = dp->de_fndcnt;
1034
1035         /*
1036          * If ".." must be changed (ie the directory gets a new
1037          * parent) then the source directory must not be in the
1038          * directory hierarchy above the target, as this would
1039          * orphan everything below the source directory. Also
1040          * the user must have write permission in the source so
1041          * as to be able to change "..". We must repeat the call
1042          * to namei, as the parent directory is unlocked by the
1043          * call to doscheckpath().
1044          */
1045         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread);
1046         VOP_UNLOCK(fvp, 0);
1047         if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1048                 newparent = 1;
1049         if (doingdirectory && newparent) {
1050                 if (error)      /* write access check above */
1051                         goto bad;
1052                 if (xp != NULL)
1053                         vput(tvp);
1054                 /*
1055                  * doscheckpath() vput()'s dp,
1056                  * so we have to do a relookup afterwards
1057                  */
1058                 error = doscheckpath(ip, dp);
1059                 if (error)
1060                         goto out;
1061                 if ((tcnp->cn_flags & SAVESTART) == 0)
1062                         panic("msdosfs_rename: lost to startdir");
1063                 error = relookup(tdvp, &tvp, tcnp);
1064                 if (error)
1065                         goto out;
1066                 dp = VTODE(tdvp);
1067                 xp = tvp ? VTODE(tvp) : NULL;
1068         }
1069
1070         if (xp != NULL) {
1071                 /*
1072                  * Target must be empty if a directory and have no links
1073                  * to it. Also, ensure source and target are compatible
1074                  * (both directories, or both not directories).
1075                  */
1076                 if (xp->de_Attributes & ATTR_DIRECTORY) {
1077                         if (!dosdirempty(xp)) {
1078                                 error = ENOTEMPTY;
1079                                 goto bad;
1080                         }
1081                         if (!doingdirectory) {
1082                                 error = ENOTDIR;
1083                                 goto bad;
1084                         }
1085                         cache_purge(tdvp);
1086                 } else if (doingdirectory) {
1087                         error = EISDIR;
1088                         goto bad;
1089                 }
1090                 error = removede(dp, xp);
1091                 if (error)
1092                         goto bad;
1093                 vput(tvp);
1094                 xp = NULL;
1095         }
1096
1097         /*
1098          * Convert the filename in tcnp into a dos filename. We copy this
1099          * into the denode and directory entry for the destination
1100          * file/directory.
1101          */
1102         error = uniqdosname(VTODE(tdvp), tcnp, toname);
1103         if (error)
1104                 goto abortit;
1105
1106         /*
1107          * Since from wasn't locked at various places above,
1108          * have to do a relookup here.
1109          */
1110         fcnp->cn_flags &= ~MODMASK;
1111         fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1112         if ((fcnp->cn_flags & SAVESTART) == 0)
1113                 panic("msdosfs_rename: lost from startdir");
1114         if (!newparent)
1115                 VOP_UNLOCK(tdvp, 0);
1116         if (relookup(fdvp, &fvp, fcnp) == 0)
1117                 vrele(fdvp);
1118         if (fvp == NULL) {
1119                 /*
1120                  * From name has disappeared.
1121                  */
1122                 if (doingdirectory)
1123                         panic("rename: lost dir entry");
1124                 if (newparent)
1125                         VOP_UNLOCK(tdvp, 0);
1126                 vrele(tdvp);
1127                 vrele(ap->a_fvp);
1128                 return 0;
1129         }
1130         xp = VTODE(fvp);
1131         zp = VTODE(fdvp);
1132         from_diroffset = zp->de_fndoffset;
1133
1134         /*
1135          * Ensure that the directory entry still exists and has not
1136          * changed till now. If the source is a file the entry may
1137          * have been unlinked or renamed. In either case there is
1138          * no further work to be done. If the source is a directory
1139          * then it cannot have been rmdir'ed or renamed; this is
1140          * prohibited by the DE_RENAME flag.
1141          */
1142         if (xp != ip) {
1143                 if (doingdirectory)
1144                         panic("rename: lost dir entry");
1145                 VOP_UNLOCK(fvp, 0);
1146                 if (newparent)
1147                         VOP_UNLOCK(fdvp, 0);
1148                 vrele(ap->a_fvp);
1149                 xp = NULL;
1150         } else {
1151                 vrele(fvp);
1152                 xp = NULL;
1153
1154                 /*
1155                  * First write a new entry in the destination
1156                  * directory and mark the entry in the source directory
1157                  * as deleted.  Then move the denode to the correct hash
1158                  * chain for its new location in the filesystem.  And, if
1159                  * we moved a directory, then update its .. entry to point
1160                  * to the new parent directory.
1161                  */
1162                 memcpy(oldname, ip->de_Name, 11);
1163                 memcpy(ip->de_Name, toname, 11);        /* update denode */
1164                 dp->de_fndoffset = to_diroffset;
1165                 dp->de_fndcnt = to_count;
1166                 error = createde(ip, dp, (struct denode **)0, tcnp);
1167                 if (error) {
1168                         memcpy(ip->de_Name, oldname, 11);
1169                         if (newparent)
1170                                 VOP_UNLOCK(fdvp, 0);
1171                         VOP_UNLOCK(fvp, 0);
1172                         goto bad;
1173                 }
1174                 /*
1175                  * If ip is for a directory, then its name should always
1176                  * be "." since it is for the directory entry in the
1177                  * directory itself (msdosfs_lookup() always translates
1178                  * to the "." entry so as to get a unique denode, except
1179                  * for the root directory there are different
1180                  * complications).  However, we just corrupted its name
1181                  * to pass the correct name to createde().  Undo this.
1182                  */
1183                 if ((ip->de_Attributes & ATTR_DIRECTORY) != 0)
1184                         memcpy(ip->de_Name, oldname, 11);
1185                 ip->de_refcnt++;
1186                 zp->de_fndoffset = from_diroffset;
1187                 error = removede(zp, ip);
1188                 if (error) {
1189                         /* XXX should downgrade to ro here, fs is corrupt */
1190                         if (newparent)
1191                                 VOP_UNLOCK(fdvp, 0);
1192                         VOP_UNLOCK(fvp, 0);
1193                         goto bad;
1194                 }
1195                 if (!doingdirectory) {
1196                         error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1197                                        &ip->de_dirclust, 0);
1198                         if (error) {
1199                                 /* XXX should downgrade to ro here, fs is corrupt */
1200                                 if (newparent)
1201                                         VOP_UNLOCK(fdvp, 0);
1202                                 VOP_UNLOCK(fvp, 0);
1203                                 goto bad;
1204                         }
1205                         if (ip->de_dirclust == MSDOSFSROOT)
1206                                 ip->de_diroffset = to_diroffset;
1207                         else
1208                                 ip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1209                 }
1210                 reinsert(ip);
1211                 if (newparent)
1212                         VOP_UNLOCK(fdvp, 0);
1213         }
1214
1215         /*
1216          * If we moved a directory to a new parent directory, then we must
1217          * fixup the ".." entry in the moved directory.
1218          */
1219         if (doingdirectory && newparent) {
1220                 cn = ip->de_StartCluster;
1221                 if (cn == MSDOSFSROOT) {
1222                         /* this should never happen */
1223                         panic("msdosfs_rename(): updating .. in root directory?");
1224                 } else
1225                         bn = cntobn(pmp, cn);
1226                 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1227                               NOCRED, &bp);
1228                 if (error) {
1229                         /* XXX should downgrade to ro here, fs is corrupt */
1230                         brelse(bp);
1231                         VOP_UNLOCK(fvp, 0);
1232                         goto bad;
1233                 }
1234                 dotdotp = (struct direntry *)bp->b_data + 1;
1235                 pcl = dp->de_StartCluster;
1236                 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1237                         pcl = MSDOSFSROOT;
1238                 putushort(dotdotp->deStartCluster, pcl);
1239                 if (FAT32(pmp))
1240                         putushort(dotdotp->deHighClust, pcl >> 16);
1241                 if (DOINGASYNC(fvp))
1242                         bdwrite(bp);
1243                 else if ((error = bwrite(bp)) != 0) {
1244                         /* XXX should downgrade to ro here, fs is corrupt */
1245                         VOP_UNLOCK(fvp, 0);
1246                         goto bad;
1247                 }
1248         }
1249
1250         /*
1251          * The msdosfs lookup is case insensitive. Several aliases may
1252          * be inserted for a single directory entry. As a consequnce,
1253          * name cache purge done by lookup for fvp when DELETE op for
1254          * namei is specified, might be not enough to expunge all
1255          * namecache entries that were installed for this direntry.
1256          */
1257         cache_purge(fvp);
1258         VOP_UNLOCK(fvp, 0);
1259 bad:
1260         if (xp)
1261                 vput(tvp);
1262         vput(tdvp);
1263 out:
1264         ip->de_flag &= ~DE_RENAME;
1265         vrele(fdvp);
1266         vrele(fvp);
1267         return (error);
1268
1269 }
1270
1271 static struct {
1272         struct direntry dot;
1273         struct direntry dotdot;
1274 } dosdirtemplate = {
1275         {       ".          ",                          /* the . entry */
1276                 ATTR_DIRECTORY,                         /* file attribute */
1277                 0,                                      /* reserved */
1278                 0, { 0, 0 }, { 0, 0 },                  /* create time & date */
1279                 { 0, 0 },                               /* access date */
1280                 { 0, 0 },                               /* high bits of start cluster */
1281                 { 210, 4 }, { 210, 4 },                 /* modify time & date */
1282                 { 0, 0 },                               /* startcluster */
1283                 { 0, 0, 0, 0 }                          /* filesize */
1284         },
1285         {       "..         ",                          /* the .. entry */
1286                 ATTR_DIRECTORY,                         /* file attribute */
1287                 0,                                      /* reserved */
1288                 0, { 0, 0 }, { 0, 0 },                  /* create time & date */
1289                 { 0, 0 },                               /* access date */
1290                 { 0, 0 },                               /* high bits of start cluster */
1291                 { 210, 4 }, { 210, 4 },                 /* modify time & date */
1292                 { 0, 0 },                               /* startcluster */
1293                 { 0, 0, 0, 0 }                          /* filesize */
1294         }
1295 };
1296
1297 static int
1298 msdosfs_mkdir(struct vop_mkdir_args *ap)
1299 {
1300         struct componentname *cnp = ap->a_cnp;
1301         struct denode *dep;
1302         struct denode *pdep = VTODE(ap->a_dvp);
1303         struct direntry *denp;
1304         struct msdosfsmount *pmp = pdep->de_pmp;
1305         struct buf *bp;
1306         u_long newcluster, pcl;
1307         int bn;
1308         int error;
1309         struct denode ndirent;
1310         struct timespec ts;
1311
1312         /*
1313          * If this is the root directory and there is no space left we
1314          * can't do anything.  This is because the root directory can not
1315          * change size.
1316          */
1317         if (pdep->de_StartCluster == MSDOSFSROOT
1318             && pdep->de_fndoffset >= pdep->de_FileSize) {
1319                 error = ENOSPC;
1320                 goto bad2;
1321         }
1322
1323         /*
1324          * Allocate a cluster to hold the about to be created directory.
1325          */
1326         error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1327         if (error)
1328                 goto bad2;
1329
1330         memset(&ndirent, 0, sizeof(ndirent));
1331         ndirent.de_pmp = pmp;
1332         ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1333         vfs_timestamp(&ts);
1334         DETIMES(&ndirent, &ts, &ts, &ts);
1335
1336         /*
1337          * Now fill the cluster with the "." and ".." entries. And write
1338          * the cluster to disk.  This way it is there for the parent
1339          * directory to be pointing at if there were a crash.
1340          */
1341         bn = cntobn(pmp, newcluster);
1342         /* always succeeds */
1343         bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
1344         memset(bp->b_data, 0, pmp->pm_bpcluster);
1345         memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
1346         denp = (struct direntry *)bp->b_data;
1347         putushort(denp[0].deStartCluster, newcluster);
1348         putushort(denp[0].deCDate, ndirent.de_CDate);
1349         putushort(denp[0].deCTime, ndirent.de_CTime);
1350         denp[0].deCHundredth = ndirent.de_CHun;
1351         putushort(denp[0].deADate, ndirent.de_ADate);
1352         putushort(denp[0].deMDate, ndirent.de_MDate);
1353         putushort(denp[0].deMTime, ndirent.de_MTime);
1354         pcl = pdep->de_StartCluster;
1355         /*
1356          * Although the root directory has a non-magic starting cluster
1357          * number for FAT32, chkdsk and fsck_msdosfs still require
1358          * references to it in dotdot entries to be magic.
1359          */
1360         if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1361                 pcl = MSDOSFSROOT;
1362         putushort(denp[1].deStartCluster, pcl);
1363         putushort(denp[1].deCDate, ndirent.de_CDate);
1364         putushort(denp[1].deCTime, ndirent.de_CTime);
1365         denp[1].deCHundredth = ndirent.de_CHun;
1366         putushort(denp[1].deADate, ndirent.de_ADate);
1367         putushort(denp[1].deMDate, ndirent.de_MDate);
1368         putushort(denp[1].deMTime, ndirent.de_MTime);
1369         if (FAT32(pmp)) {
1370                 putushort(denp[0].deHighClust, newcluster >> 16);
1371                 putushort(denp[1].deHighClust, pcl >> 16);
1372         }
1373
1374         if (DOINGASYNC(ap->a_dvp))
1375                 bdwrite(bp);
1376         else if ((error = bwrite(bp)) != 0)
1377                 goto bad;
1378
1379         /*
1380          * Now build up a directory entry pointing to the newly allocated
1381          * cluster.  This will be written to an empty slot in the parent
1382          * directory.
1383          */
1384 #ifdef DIAGNOSTIC
1385         if ((cnp->cn_flags & HASBUF) == 0)
1386                 panic("msdosfs_mkdir: no name");
1387 #endif
1388         error = uniqdosname(pdep, cnp, ndirent.de_Name);
1389         if (error)
1390                 goto bad;
1391
1392         ndirent.de_Attributes = ATTR_DIRECTORY;
1393         ndirent.de_LowerCase = 0;
1394         ndirent.de_StartCluster = newcluster;
1395         ndirent.de_FileSize = 0;
1396         error = createde(&ndirent, pdep, &dep, cnp);
1397         if (error)
1398                 goto bad;
1399         *ap->a_vpp = DETOV(dep);
1400         return (0);
1401
1402 bad:
1403         clusterfree(pmp, newcluster, NULL);
1404 bad2:
1405         return (error);
1406 }
1407
1408 static int
1409 msdosfs_rmdir(struct vop_rmdir_args *ap)
1410 {
1411         struct vnode *vp = ap->a_vp;
1412         struct vnode *dvp = ap->a_dvp;
1413         struct componentname *cnp = ap->a_cnp;
1414         struct denode *ip, *dp;
1415         int error;
1416
1417         ip = VTODE(vp);
1418         dp = VTODE(dvp);
1419
1420         /*
1421          * Verify the directory is empty (and valid).
1422          * (Rmdir ".." won't be valid since
1423          *  ".." will contain a reference to
1424          *  the current directory and thus be
1425          *  non-empty.)
1426          */
1427         error = 0;
1428         if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1429                 error = ENOTEMPTY;
1430                 goto out;
1431         }
1432         /*
1433          * Delete the entry from the directory.  For dos filesystems this
1434          * gets rid of the directory entry on disk, the in memory copy
1435          * still exists but the de_refcnt is <= 0.  This prevents it from
1436          * being found by deget().  When the vput() on dep is done we give
1437          * up access and eventually msdosfs_reclaim() will be called which
1438          * will remove it from the denode cache.
1439          */
1440         error = removede(dp, ip);
1441         if (error)
1442                 goto out;
1443         /*
1444          * This is where we decrement the link count in the parent
1445          * directory.  Since dos filesystems don't do this we just purge
1446          * the name cache.
1447          */
1448         cache_purge(dvp);
1449         /*
1450          * Truncate the directory that is being deleted.
1451          */
1452         error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
1453         cache_purge(vp);
1454
1455 out:
1456         return (error);
1457 }
1458
1459 /*
1460  * DOS filesystems don't know what symlinks are.
1461  */
1462 static int
1463 msdosfs_symlink(struct vop_symlink_args *ap)
1464 {
1465         return (EOPNOTSUPP);
1466 }
1467
1468 static int
1469 msdosfs_readdir(struct vop_readdir_args *ap)
1470 {
1471         struct mbnambuf nb;
1472         int error = 0;
1473         int diff;
1474         long n;
1475         int blsize;
1476         long on;
1477         u_long cn;
1478         u_long dirsperblk;
1479         long bias = 0;
1480         daddr_t bn, lbn;
1481         struct buf *bp;
1482         struct denode *dep = VTODE(ap->a_vp);
1483         struct msdosfsmount *pmp = dep->de_pmp;
1484         struct direntry *dentp;
1485         struct dirent dirbuf;
1486         struct uio *uio = ap->a_uio;
1487         u_long *cookies = NULL;
1488         int ncookies = 0;
1489         off_t offset, off;
1490         int chksum = -1;
1491
1492 #ifdef MSDOSFS_DEBUG
1493         printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1494             ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1495 #endif
1496
1497         /*
1498          * msdosfs_readdir() won't operate properly on regular files since
1499          * it does i/o only with the filesystem vnode, and hence can
1500          * retrieve the wrong block from the buffer cache for a plain file.
1501          * So, fail attempts to readdir() on a plain file.
1502          */
1503         if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1504                 return (ENOTDIR);
1505
1506         /*
1507          * To be safe, initialize dirbuf
1508          */
1509         memset(dirbuf.d_name, 0, sizeof(dirbuf.d_name));
1510
1511         /*
1512          * If the user buffer is smaller than the size of one dos directory
1513          * entry or the file offset is not a multiple of the size of a
1514          * directory entry, then we fail the read.
1515          */
1516         off = offset = uio->uio_offset;
1517         if (uio->uio_resid < sizeof(struct direntry) ||
1518             (offset & (sizeof(struct direntry) - 1)))
1519                 return (EINVAL);
1520
1521         if (ap->a_ncookies) {
1522                 ncookies = uio->uio_resid / 16;
1523                 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
1524                        M_WAITOK);
1525                 *ap->a_cookies = cookies;
1526                 *ap->a_ncookies = ncookies;
1527         }
1528
1529         dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1530
1531         /*
1532          * If they are reading from the root directory then, we simulate
1533          * the . and .. entries since these don't exist in the root
1534          * directory.  We also set the offset bias to make up for having to
1535          * simulate these entries. By this I mean that at file offset 64 we
1536          * read the first entry in the root directory that lives on disk.
1537          */
1538         if (dep->de_StartCluster == MSDOSFSROOT
1539             || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1540 #if 0
1541                 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1542                     offset);
1543 #endif
1544                 bias = 2 * sizeof(struct direntry);
1545                 if (offset < bias) {
1546                         for (n = (int)offset / sizeof(struct direntry);
1547                              n < 2; n++) {
1548                                 dirbuf.d_fileno = FAT32(pmp) ?
1549                                     (uint64_t)cntobn(pmp, pmp->pm_rootdirblk) *
1550                                     dirsperblk : 1;
1551                                 dirbuf.d_type = DT_DIR;
1552                                 switch (n) {
1553                                 case 0:
1554                                         dirbuf.d_namlen = 1;
1555                                         strcpy(dirbuf.d_name, ".");
1556                                         break;
1557                                 case 1:
1558                                         dirbuf.d_namlen = 2;
1559                                         strcpy(dirbuf.d_name, "..");
1560                                         break;
1561                                 }
1562                                 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1563                                 if (uio->uio_resid < dirbuf.d_reclen)
1564                                         goto out;
1565                                 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1566                                 if (error)
1567                                         goto out;
1568                                 offset += sizeof(struct direntry);
1569                                 off = offset;
1570                                 if (cookies) {
1571                                         *cookies++ = offset;
1572                                         if (--ncookies <= 0)
1573                                                 goto out;
1574                                 }
1575                         }
1576                 }
1577         }
1578
1579         mbnambuf_init(&nb);
1580         off = offset;
1581         while (uio->uio_resid > 0) {
1582                 lbn = de_cluster(pmp, offset - bias);
1583                 on = (offset - bias) & pmp->pm_crbomask;
1584                 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1585                 diff = dep->de_FileSize - (offset - bias);
1586                 if (diff <= 0)
1587                         break;
1588                 n = min(n, diff);
1589                 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1590                 if (error)
1591                         break;
1592                 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1593                 if (error) {
1594                         brelse(bp);
1595                         return (error);
1596                 }
1597                 n = min(n, blsize - bp->b_resid);
1598                 if (n == 0) {
1599                         brelse(bp);
1600                         return (EIO);
1601                 }
1602
1603                 /*
1604                  * Convert from dos directory entries to fs-independent
1605                  * directory entries.
1606                  */
1607                 for (dentp = (struct direntry *)(bp->b_data + on);
1608                      (char *)dentp < bp->b_data + on + n;
1609                      dentp++, offset += sizeof(struct direntry)) {
1610 #if 0
1611                         printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1612                             dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1613 #endif
1614                         /*
1615                          * If this is an unused entry, we can stop.
1616                          */
1617                         if (dentp->deName[0] == SLOT_EMPTY) {
1618                                 brelse(bp);
1619                                 goto out;
1620                         }
1621                         /*
1622                          * Skip deleted entries.
1623                          */
1624                         if (dentp->deName[0] == SLOT_DELETED) {
1625                                 chksum = -1;
1626                                 mbnambuf_init(&nb);
1627                                 continue;
1628                         }
1629
1630                         /*
1631                          * Handle Win95 long directory entries
1632                          */
1633                         if (dentp->deAttributes == ATTR_WIN95) {
1634                                 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1635                                         continue;
1636                                 chksum = win2unixfn(&nb,
1637                                     (struct winentry *)dentp, chksum, pmp);
1638                                 continue;
1639                         }
1640
1641                         /*
1642                          * Skip volume labels
1643                          */
1644                         if (dentp->deAttributes & ATTR_VOLUME) {
1645                                 chksum = -1;
1646                                 mbnambuf_init(&nb);
1647                                 continue;
1648                         }
1649                         /*
1650                          * This computation of d_fileno must match
1651                          * the computation of va_fileid in
1652                          * msdosfs_getattr.
1653                          */
1654                         if (dentp->deAttributes & ATTR_DIRECTORY) {
1655                                 cn = getushort(dentp->deStartCluster);
1656                                 if (FAT32(pmp)) {
1657                                         cn |= getushort(dentp->deHighClust) <<
1658                                             16;
1659                                         if (cn == MSDOSFSROOT)
1660                                                 cn = pmp->pm_rootdirblk;
1661                                 }
1662                                 if (cn == MSDOSFSROOT && !FAT32(pmp))
1663                                         dirbuf.d_fileno = 1;
1664                                 else
1665                                         dirbuf.d_fileno = cntobn(pmp, cn) *
1666                                             dirsperblk;
1667                                 dirbuf.d_type = DT_DIR;
1668                         } else {
1669                                 dirbuf.d_fileno = (uoff_t)offset /
1670                                     sizeof(struct direntry);
1671                                 dirbuf.d_type = DT_REG;
1672                         }
1673
1674                         if (chksum != winChksum(dentp->deName)) {
1675                                 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1676                                     (u_char *)dirbuf.d_name,
1677                                     dentp->deLowerCase |
1678                                         ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1679                                         (LCASE_BASE | LCASE_EXT) : 0),
1680                                     pmp);
1681                                 mbnambuf_init(&nb);
1682                         } else
1683                                 mbnambuf_flush(&nb, &dirbuf);
1684                         chksum = -1;
1685                         dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1686                         if (uio->uio_resid < dirbuf.d_reclen) {
1687                                 brelse(bp);
1688                                 goto out;
1689                         }
1690                         error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1691                         if (error) {
1692                                 brelse(bp);
1693                                 goto out;
1694                         }
1695                         if (cookies) {
1696                                 *cookies++ = offset + sizeof(struct direntry);
1697                                 if (--ncookies <= 0) {
1698                                         brelse(bp);
1699                                         goto out;
1700                                 }
1701                         }
1702                         off = offset + sizeof(struct direntry);
1703                 }
1704                 brelse(bp);
1705         }
1706 out:
1707         /* Subtract unused cookies */
1708         if (ap->a_ncookies)
1709                 *ap->a_ncookies -= ncookies;
1710
1711         uio->uio_offset = off;
1712
1713         /*
1714          * Set the eofflag (NFS uses it)
1715          */
1716         if (ap->a_eofflag) {
1717                 if (dep->de_FileSize - (offset - bias) <= 0)
1718                         *ap->a_eofflag = 1;
1719                 else
1720                         *ap->a_eofflag = 0;
1721         }
1722         return (error);
1723 }
1724
1725 /*-
1726  * a_vp   - pointer to the file's vnode
1727  * a_bn   - logical block number within the file (cluster number for us)
1728  * a_bop  - where to return the bufobj of the special file containing the fs
1729  * a_bnp  - where to return the "physical" block number corresponding to a_bn
1730  *          (relative to the special file; units are blocks of size DEV_BSIZE)
1731  * a_runp - where to return the "run past" a_bn.  This is the count of logical
1732  *          blocks whose physical blocks (together with a_bn's physical block)
1733  *          are contiguous.
1734  * a_runb - where to return the "run before" a_bn.
1735  */
1736 static int
1737 msdosfs_bmap(struct vop_bmap_args *ap)
1738 {
1739         struct denode *dep;
1740         struct mount *mp;
1741         struct msdosfsmount *pmp;
1742         struct vnode *vp;
1743         daddr_t runbn;
1744         u_long cn;
1745         int bnpercn, error, maxio, maxrun, run;
1746
1747         vp = ap->a_vp;
1748         dep = VTODE(vp);
1749         pmp = dep->de_pmp;
1750         if (ap->a_bop != NULL)
1751                 *ap->a_bop = &pmp->pm_devvp->v_bufobj;
1752         if (ap->a_bnp == NULL)
1753                 return (0);
1754         if (ap->a_runp != NULL)
1755                 *ap->a_runp = 0;
1756         if (ap->a_runb != NULL)
1757                 *ap->a_runb = 0;
1758         cn = ap->a_bn;
1759         if (cn != ap->a_bn)
1760                 return (EFBIG);
1761         error = pcbmap(dep, cn, ap->a_bnp, NULL, NULL);
1762         if (error != 0 || (ap->a_runp == NULL && ap->a_runb == NULL))
1763                 return (error);
1764
1765         mp = vp->v_mount;
1766         maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1767         bnpercn = de_cn2bn(pmp, 1);
1768         if (ap->a_runp != NULL) {
1769                 maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1770                 for (run = 1; run <= maxrun; run++) {
1771                         if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1772                             runbn != *ap->a_bnp + run * bnpercn)
1773                                 break;
1774                 }
1775                 *ap->a_runp = run - 1;
1776         }
1777         if (ap->a_runb != NULL) {
1778                 maxrun = ulmin(maxio - 1, cn);
1779                 for (run = 1; run < maxrun; run++) {
1780                         if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1781                             runbn != *ap->a_bnp - run * bnpercn)
1782                                 break;
1783                 }
1784                 *ap->a_runb = run - 1;
1785         }
1786         return (0);
1787 }
1788
1789 SYSCTL_NODE(_vfs, OID_AUTO, msdosfs, CTLFLAG_RW, 0, "msdos filesystem");
1790 static int use_buf_pager = 1;
1791 SYSCTL_INT(_vfs_msdosfs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
1792     &use_buf_pager, 0,
1793     "Use buffer pager instead of bmap");
1794
1795 static daddr_t
1796 msdosfs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1797 {
1798
1799         return (de_cluster(VTODE(vp)->de_pmp, off));
1800 }
1801
1802 static int
1803 msdosfs_gbp_getblksz(struct vnode *vp, daddr_t lbn)
1804 {
1805
1806         return (VTODE(vp)->de_pmp->pm_bpcluster);
1807 }
1808
1809 static int
1810 msdosfs_getpages(struct vop_getpages_args *ap)
1811 {
1812
1813         if (use_buf_pager)
1814                 return (vfs_bio_getpages(ap->a_vp, ap->a_m, ap->a_count,
1815                     ap->a_rbehind, ap->a_rahead, msdosfs_gbp_getblkno,
1816                     msdosfs_gbp_getblksz));
1817         return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1818             ap->a_rbehind, ap->a_rahead, NULL, NULL));
1819 }
1820
1821 static int
1822 msdosfs_strategy(struct vop_strategy_args *ap)
1823 {
1824         struct buf *bp = ap->a_bp;
1825         struct denode *dep = VTODE(ap->a_vp);
1826         struct bufobj *bo;
1827         int error = 0;
1828         daddr_t blkno;
1829
1830         /*
1831          * If we don't already know the filesystem relative block number
1832          * then get it using pcbmap().  If pcbmap() returns the block
1833          * number as -1 then we've got a hole in the file.  DOS filesystems
1834          * don't allow files with holes, so we shouldn't ever see this.
1835          */
1836         if (bp->b_blkno == bp->b_lblkno) {
1837                 error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1838                 bp->b_blkno = blkno;
1839                 if (error) {
1840                         bp->b_error = error;
1841                         bp->b_ioflags |= BIO_ERROR;
1842                         bufdone(bp);
1843                         return (0);
1844                 }
1845                 if ((long)bp->b_blkno == -1)
1846                         vfs_bio_clrbuf(bp);
1847         }
1848         if (bp->b_blkno == -1) {
1849                 bufdone(bp);
1850                 return (0);
1851         }
1852         /*
1853          * Read/write the block from/to the disk that contains the desired
1854          * file block.
1855          */
1856         bp->b_iooffset = dbtob(bp->b_blkno);
1857         bo = dep->de_pmp->pm_bo;
1858         BO_STRATEGY(bo, bp);
1859         return (0);
1860 }
1861
1862 static int
1863 msdosfs_print(struct vop_print_args *ap)
1864 {
1865         struct denode *dep = VTODE(ap->a_vp);
1866
1867         printf("\tstartcluster %lu, dircluster %lu, diroffset %lu, ",
1868                dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1869         printf("on dev %s\n", devtoname(dep->de_pmp->pm_dev));
1870         return (0);
1871 }
1872
1873 static int
1874 msdosfs_pathconf(struct vop_pathconf_args *ap)
1875 {
1876         struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1877
1878         switch (ap->a_name) {
1879         case _PC_FILESIZEBITS:
1880                 *ap->a_retval = 32;
1881                 return (0);
1882         case _PC_LINK_MAX:
1883                 *ap->a_retval = 1;
1884                 return (0);
1885         case _PC_NAME_MAX:
1886                 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1887                 return (0);
1888         case _PC_CHOWN_RESTRICTED:
1889                 *ap->a_retval = 1;
1890                 return (0);
1891         case _PC_NO_TRUNC:
1892                 *ap->a_retval = 0;
1893                 return (0);
1894         default:
1895                 return (vop_stdpathconf(ap));
1896         }
1897         /* NOTREACHED */
1898 }
1899
1900 static int
1901 msdosfs_vptofh(struct vop_vptofh_args *ap)
1902 {
1903         struct denode *dep;
1904         struct defid *defhp;
1905
1906         dep = VTODE(ap->a_vp);
1907         defhp = (struct defid *)ap->a_fhp;
1908         defhp->defid_len = sizeof(struct defid);
1909         defhp->defid_dirclust = dep->de_dirclust;
1910         defhp->defid_dirofs = dep->de_diroffset;
1911         /* defhp->defid_gen = dep->de_gen; */
1912         return (0);
1913 }
1914
1915 /* Global vfs data structures for msdosfs */
1916 struct vop_vector msdosfs_vnodeops = {
1917         .vop_default =          &default_vnodeops,
1918
1919         .vop_access =           msdosfs_access,
1920         .vop_bmap =             msdosfs_bmap,
1921         .vop_getpages =         msdosfs_getpages,
1922         .vop_cachedlookup =     msdosfs_lookup,
1923         .vop_open =             msdosfs_open,
1924         .vop_close =            msdosfs_close,
1925         .vop_create =           msdosfs_create,
1926         .vop_fsync =            msdosfs_fsync,
1927         .vop_fdatasync =        vop_stdfdatasync_buf,
1928         .vop_getattr =          msdosfs_getattr,
1929         .vop_inactive =         msdosfs_inactive,
1930         .vop_link =             msdosfs_link,
1931         .vop_lookup =           vfs_cache_lookup,
1932         .vop_mkdir =            msdosfs_mkdir,
1933         .vop_mknod =            msdosfs_mknod,
1934         .vop_pathconf =         msdosfs_pathconf,
1935         .vop_print =            msdosfs_print,
1936         .vop_read =             msdosfs_read,
1937         .vop_readdir =          msdosfs_readdir,
1938         .vop_reclaim =          msdosfs_reclaim,
1939         .vop_remove =           msdosfs_remove,
1940         .vop_rename =           msdosfs_rename,
1941         .vop_rmdir =            msdosfs_rmdir,
1942         .vop_setattr =          msdosfs_setattr,
1943         .vop_strategy =         msdosfs_strategy,
1944         .vop_symlink =          msdosfs_symlink,
1945         .vop_write =            msdosfs_write,
1946         .vop_vptofh =           msdosfs_vptofh,
1947 };