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