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