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