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