Remove VOP_GETVOBJECT, VOP_DESTROYVOBJECT, and VOP_CREATEVOBJECT. Rearrange
[dragonfly.git] / sys / vfs / msdosfs / msdosfs_vnops.c
1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vnops.c,v 1.95.2.4 2003/06/13 15:05:47 trhodes Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vnops.c,v 1.32 2006/03/29 18:44:59 dillon Exp $ */
3 /*      $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $   */
4
5 /*-
6  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8  * All rights reserved.
9  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by TooLs GmbH.
22  * 4. The name of TooLs GmbH may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 /*
37  * Written by Paul Popelka (paulp@uts.amdahl.com)
38  *
39  * You can do anything you want with this software, just don't say you wrote
40  * it, and don't remove this notice.
41  *
42  * This software is provided "as is".
43  *
44  * The author supplies this software to be publicly redistributed on the
45  * understanding that the author is not responsible for the correct
46  * functioning of this software in any circumstances and is not liable for
47  * any damages caused by this software.
48  *
49  * October 1992
50  */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/resourcevar.h>    /* defines plimit structure in proc struct */
55 #include <sys/kernel.h>
56 #include <sys/stat.h>
57 #include <sys/buf.h>
58 #include <sys/proc.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/unistd.h>
62 #include <sys/vnode.h>
63 #include <sys/malloc.h>
64 #include <sys/dirent.h>
65 #include <sys/signalvar.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_extern.h>
69 #include <vm/vm_zone.h>
70 #include <vm/vnode_pager.h>
71
72 #include <sys/buf2.h>
73
74 #include "bpb.h"
75 #include "direntry.h"
76 #include "denode.h"
77 #include "msdosfsmount.h"
78 #include "fat.h"
79
80 #define DOS_FILESIZE_MAX        0xffffffff
81
82 /*
83  * Prototypes for MSDOSFS vnode operations
84  */
85 static int msdosfs_create (struct vop_old_create_args *);
86 static int msdosfs_mknod (struct vop_old_mknod_args *);
87 static int msdosfs_open (struct vop_open_args *);
88 static int msdosfs_close (struct vop_close_args *);
89 static int msdosfs_access (struct vop_access_args *);
90 static int msdosfs_getattr (struct vop_getattr_args *);
91 static int msdosfs_setattr (struct vop_setattr_args *);
92 static int msdosfs_read (struct vop_read_args *);
93 static int msdosfs_write (struct vop_write_args *);
94 static int msdosfs_fsync (struct vop_fsync_args *);
95 static int msdosfs_remove (struct vop_old_remove_args *);
96 static int msdosfs_link (struct vop_old_link_args *);
97 static int msdosfs_rename (struct vop_old_rename_args *);
98 static int msdosfs_mkdir (struct vop_old_mkdir_args *);
99 static int msdosfs_rmdir (struct vop_old_rmdir_args *);
100 static int msdosfs_symlink (struct vop_old_symlink_args *);
101 static int msdosfs_readdir (struct vop_readdir_args *);
102 static int msdosfs_bmap (struct vop_bmap_args *);
103 static int msdosfs_strategy (struct vop_strategy_args *);
104 static int msdosfs_print (struct vop_print_args *);
105 static int msdosfs_pathconf (struct vop_pathconf_args *ap);
106 static int msdosfs_getpages (struct vop_getpages_args *);
107 static int msdosfs_putpages (struct vop_putpages_args *);
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. 
130  *
131  * msdosfs_create(struct vnode *a_dvp, struct vnode **a_vpp,
132  *                struct componentname *a_cnp, struct vattr *a_vap)
133  */
134 static int
135 msdosfs_create(struct vop_old_create_args *ap)
136 {
137         struct componentname *cnp = ap->a_cnp;
138         struct denode ndirent;
139         struct denode *dep;
140         struct denode *pdep = VTODE(ap->a_dvp);
141         struct timespec ts;
142         int error;
143
144 #ifdef MSDOSFS_DEBUG
145         printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
146 #endif
147
148         /*
149          * If this is the root directory and there is no space left we
150          * can't do anything.  This is because the root directory can not
151          * change size.
152          */
153         if (pdep->de_StartCluster == MSDOSFSROOT
154             && pdep->de_fndoffset >= pdep->de_FileSize) {
155                 error = ENOSPC;
156                 goto bad;
157         }
158
159         /*
160          * Create a directory entry for the file, then call createde() to
161          * have it installed. NOTE: DOS files are always executable.  We
162          * use the absence of the owner write bit to make the file
163          * readonly.
164          */
165         bzero(&ndirent, sizeof(ndirent));
166         error = uniqdosname(pdep, cnp, ndirent.de_Name);
167         if (error)
168                 goto bad;
169
170         ndirent.de_Attributes = (ap->a_vap->va_mode & VWRITE) ?
171                                 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
172         ndirent.de_LowerCase = 0;
173         ndirent.de_StartCluster = 0;
174         ndirent.de_FileSize = 0;
175         ndirent.de_dev = pdep->de_dev;
176         ndirent.de_devvp = pdep->de_devvp;
177         ndirent.de_pmp = pdep->de_pmp;
178         ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
179         getnanotime(&ts);
180         DETIMES(&ndirent, &ts, &ts, &ts);
181         error = createde(&ndirent, pdep, &dep, cnp);
182         if (error)
183                 goto bad;
184         *ap->a_vpp = DETOV(dep);
185         return (0);
186
187 bad:
188         return (error);
189 }
190
191 /*
192  * msdosfs_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
193  *               struct componentname *a_cnp, struct vattr *a_vap)
194  */
195 static int
196 msdosfs_mknod(struct vop_old_mknod_args *ap)
197 {
198         switch (ap->a_vap->va_type) {
199         case VDIR:
200                 return (msdosfs_mkdir((struct vop_old_mkdir_args *)ap));
201                 break;
202
203         case VREG:
204                 return (msdosfs_create((struct vop_old_create_args *)ap));
205                 break;
206
207         default:
208                 return (EINVAL);
209         }
210         /* NOTREACHED */
211 }
212
213 /*
214  * msdosfs_open(struct vnode *a_vp)
215  */
216 static int
217 msdosfs_open(struct vop_open_args *ap)
218 {
219         struct vnode *vp = ap->a_vp;
220
221         if (vp->v_type == VREG || vp->v_type == VDIR)
222                 vinitvmio(vp);
223         return(0);
224 }
225
226 /*
227  * msdosfs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
228  *               struct thread *a_td)
229  */
230 static int
231 msdosfs_close(struct vop_close_args *ap)
232 {
233         struct vnode *vp = ap->a_vp;
234         struct denode *dep = VTODE(vp);
235         struct timespec ts;
236
237         if (vp->v_usecount > 1) {
238                 getnanotime(&ts);
239                 if (vp->v_usecount > 1) {
240                         DETIMES(dep, &ts, &ts, &ts);
241                 }
242         }
243         return 0;
244 }
245
246 /*
247  * msdosfs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred,
248  *                struct thread *a_td)
249  */
250 static int
251 msdosfs_access(struct vop_access_args *ap)
252 {
253         struct vnode *vp = ap->a_vp;
254         struct denode *dep = VTODE(ap->a_vp);
255         struct msdosfsmount *pmp = dep->de_pmp;
256         struct ucred *cred = ap->a_cred;
257         mode_t mask, file_mode, mode = ap->a_mode;
258         gid_t *gp;
259         int i;
260
261         file_mode = (S_IXUSR|S_IXGRP|S_IXOTH) | (S_IRUSR|S_IRGRP|S_IROTH) |
262             ((dep->de_Attributes & ATTR_READONLY) ? 0 : (S_IWUSR|S_IWGRP|S_IWOTH));
263         file_mode &= pmp->pm_mask;
264
265         /*
266          * Disallow write attempts on read-only file systems;
267          * unless the file is a socket, fifo, or a block or
268          * character device resident on the file system.
269          */
270         if (mode & VWRITE) {
271                 switch (vp->v_type) {
272                 case VDIR:
273                 case VLNK:
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         /* User id 0 always gets access. */
284         if (cred->cr_uid == 0)
285                 return 0;
286
287         mask = 0;
288
289         /* Otherwise, check the owner. */
290         if (cred->cr_uid == pmp->pm_uid) {
291                 if (mode & VEXEC)
292                         mask |= S_IXUSR;
293                 if (mode & VREAD)
294                         mask |= S_IRUSR;
295                 if (mode & VWRITE)
296                         mask |= S_IWUSR;
297                 return (file_mode & mask) == mask ? 0 : EACCES;
298         }
299
300         /* Otherwise, check the groups. */
301         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)
302                 if (pmp->pm_gid == *gp) {
303                         if (mode & VEXEC)
304                                 mask |= S_IXGRP;
305                         if (mode & VREAD)
306                                 mask |= S_IRGRP;
307                         if (mode & VWRITE)
308                                 mask |= S_IWGRP;
309                         return (file_mode & mask) == mask ? 0 : EACCES;
310                 }
311
312         /* Otherwise, check everyone else. */
313         if (mode & VEXEC)
314                 mask |= S_IXOTH;
315         if (mode & VREAD)
316                 mask |= S_IROTH;
317         if (mode & VWRITE)
318                 mask |= S_IWOTH;
319         return (file_mode & mask) == mask ? 0 : EACCES;
320 }
321
322 /*
323  * msdosfs_getattr(struct vnode *a_vp, struct vattr *a_vap,
324  *                 struct ucred *a_cred, struct thread *a_td)
325  */
326 static int
327 msdosfs_getattr(struct vop_getattr_args *ap)
328 {
329         struct denode *dep = VTODE(ap->a_vp);
330         struct msdosfsmount *pmp = dep->de_pmp;
331         struct vattr *vap = ap->a_vap;
332         mode_t mode;
333         struct timespec ts;
334         u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
335         u_long fileid;
336
337         getnanotime(&ts);
338         DETIMES(dep, &ts, &ts, &ts);
339         vap->va_fsid = dev2udev(dep->de_dev);
340         /*
341          * The following computation of the fileid must be the same as that
342          * used in msdosfs_readdir() to compute d_fileno. If not, pwd
343          * doesn't work.
344          */
345         if (dep->de_Attributes & ATTR_DIRECTORY) {
346                 fileid = xcntobn(pmp, dep->de_StartCluster) * dirsperblk;
347                 if (dep->de_StartCluster == MSDOSFSROOT)
348                         fileid = 1;
349         } else {
350                 fileid = xcntobn(pmp, dep->de_dirclust) * dirsperblk;
351                 if (dep->de_dirclust == MSDOSFSROOT)
352                         fileid = roottobn(pmp, 0) * dirsperblk;
353                 fileid += dep->de_diroffset / sizeof(struct direntry);
354         }
355         vap->va_fileid = fileid;
356         if ((dep->de_Attributes & ATTR_READONLY) == 0)
357                 mode = S_IRWXU|S_IRWXG|S_IRWXO;
358         else
359                 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
360         vap->va_mode = mode & pmp->pm_mask;
361         vap->va_uid = pmp->pm_uid;
362         vap->va_gid = pmp->pm_gid;
363         vap->va_nlink = 1;
364         vap->va_rdev = 0;
365         vap->va_size = dep->de_FileSize;
366         dos2unixtime(dep->de_MDate, dep->de_MTime, 0, &vap->va_mtime);
367         if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
368                 dos2unixtime(dep->de_ADate, 0, 0, &vap->va_atime);
369                 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun, &vap->va_ctime);
370         } else {
371                 vap->va_atime = vap->va_mtime;
372                 vap->va_ctime = vap->va_mtime;
373         }
374         vap->va_flags = 0;
375         if ((dep->de_Attributes & ATTR_ARCHIVE) == 0)
376                 vap->va_flags |= SF_ARCHIVED;
377         vap->va_gen = 0;
378         vap->va_blocksize = pmp->pm_bpcluster;
379         vap->va_bytes =
380             (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
381         vap->va_type = ap->a_vp->v_type;
382         vap->va_filerev = dep->de_modrev;
383         return (0);
384 }
385
386 /*
387  * msdosfs_setattr(struct vnode *a_vp, struct vattr *a_vap,
388  *                 struct ucred *a_cred, struct thread *a_td)
389  */
390 static int
391 msdosfs_setattr(struct vop_setattr_args *ap)
392 {
393         struct vnode *vp = ap->a_vp;
394         struct denode *dep = VTODE(ap->a_vp);
395         struct msdosfsmount *pmp = dep->de_pmp;
396         struct vattr *vap = ap->a_vap;
397         struct ucred *cred = ap->a_cred;
398         int error = 0;
399
400 #ifdef MSDOSFS_DEBUG
401         printf("msdosfs_setattr(): vp %p, vap %p, cred %p, p %p\n",
402             ap->a_vp, vap, cred, ap->a_td);
403 #endif
404
405         /*
406          * Check for unsettable attributes.
407          */
408         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
409             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
410             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
411             (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
412 #ifdef MSDOSFS_DEBUG
413                 printf("msdosfs_setattr(): returning EINVAL\n");
414                 printf("    va_type %d, va_nlink %x, va_fsid %lx, va_fileid %lx\n",
415                     vap->va_type, vap->va_nlink, vap->va_fsid, vap->va_fileid);
416                 printf("    va_blocksize %lx, va_rdev %x, va_bytes %qx, va_gen %lx\n",
417                     vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
418                 printf("    va_uid %x, va_gid %x\n",
419                     vap->va_uid, vap->va_gid);
420 #endif
421                 return (EINVAL);
422         }
423         if (vap->va_flags != VNOVAL) {
424                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
425                         return (EROFS);
426                 if (cred->cr_uid != pmp->pm_uid &&
427                     (error = suser_cred(cred, PRISON_ROOT)))
428                         return (error);
429                 /*
430                  * We are very inconsistent about handling unsupported
431                  * attributes.  We ignored the access time and the
432                  * read and execute bits.  We were strict for the other
433                  * attributes.
434                  *
435                  * Here we are strict, stricter than ufs in not allowing
436                  * users to attempt to set SF_SETTABLE bits or anyone to
437                  * set unsupported bits.  However, we ignore attempts to
438                  * set ATTR_ARCHIVE for directories `cp -pr' from a more
439                  * sensible file system attempts it a lot.
440                  */
441                 if (cred->cr_uid != 0) {
442                         if (vap->va_flags & SF_SETTABLE)
443                                 return EPERM;
444                 }
445                 if (vap->va_flags & ~SF_ARCHIVED)
446                         return EOPNOTSUPP;
447                 if (vap->va_flags & SF_ARCHIVED)
448                         dep->de_Attributes &= ~ATTR_ARCHIVE;
449                 else if (!(dep->de_Attributes & ATTR_DIRECTORY))
450                         dep->de_Attributes |= ATTR_ARCHIVE;
451                 dep->de_flag |= DE_MODIFIED;
452         }
453
454         if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
455                 uid_t uid;
456                 gid_t gid;
457                 
458                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
459                         return (EROFS);
460                 uid = vap->va_uid;
461                 if (uid == (uid_t)VNOVAL)
462                         uid = pmp->pm_uid;
463                 gid = vap->va_gid;
464                 if (gid == (gid_t)VNOVAL)
465                         gid = pmp->pm_gid;
466                 if ((cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
467                     (gid != pmp->pm_gid && !groupmember(gid, cred))) &&
468                     (error = suser_cred(cred, PRISON_ROOT)))
469                         return error;
470                 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
471                         return EINVAL;
472         }
473
474         if (vap->va_size != VNOVAL) {
475                 /*
476                  * Disallow write attempts on read-only file systems;
477                  * unless the file is a socket, fifo, or a block or
478                  * character device resident on the file system.
479                  */
480                 switch (vp->v_type) {
481                 case VDIR:
482                         return (EISDIR);
483                         /* NOT REACHED */
484                 case VLNK:
485                 case VREG:
486                         if (vp->v_mount->mnt_flag & MNT_RDONLY)
487                                 return (EROFS);
488                         break;
489                 default:
490                         break;
491                 }
492                 error = detrunc(dep, vap->va_size, 0, ap->a_td);
493                 if (error)
494                         return error;
495         }
496         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
497                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
498                         return (EROFS);
499                 if (cred->cr_uid != pmp->pm_uid &&
500                     (error = suser_cred(cred, PRISON_ROOT)) &&
501                     ((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
502                     (error = VOP_ACCESS(ap->a_vp, VWRITE, cred, ap->a_td))))
503                         return (error);
504                 if (vp->v_type != VDIR) {
505                         if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
506                             vap->va_atime.tv_sec != VNOVAL) {
507                                 dep->de_flag &= ~DE_ACCESS;
508                                 unix2dostime(&vap->va_atime, &dep->de_ADate,
509                                     NULL, NULL);
510                         }
511                         if (vap->va_mtime.tv_sec != VNOVAL) {
512                                 dep->de_flag &= ~DE_UPDATE;
513                                 unix2dostime(&vap->va_mtime, &dep->de_MDate,
514                                     &dep->de_MTime, NULL);
515                         }
516                         dep->de_Attributes |= ATTR_ARCHIVE;
517                         dep->de_flag |= DE_MODIFIED;
518                 }
519         }
520         /*
521          * DOS files only have the ability to have their writability
522          * attribute set, so we use the owner write bit to set the readonly
523          * attribute.
524          */
525         if (vap->va_mode != (mode_t)VNOVAL) {
526                 if (vp->v_mount->mnt_flag & MNT_RDONLY)
527                         return (EROFS);
528                 if (cred->cr_uid != pmp->pm_uid &&
529                     (error = suser_cred(cred, PRISON_ROOT)))
530                         return (error);
531                 if (vp->v_type != VDIR) {
532                         /* We ignore the read and execute bits. */
533                         if (vap->va_mode & VWRITE)
534                                 dep->de_Attributes &= ~ATTR_READONLY;
535                         else
536                                 dep->de_Attributes |= ATTR_READONLY;
537                         dep->de_Attributes |= ATTR_ARCHIVE;
538                         dep->de_flag |= DE_MODIFIED;
539                 }
540         }
541         return (deupdat(dep, 1));
542 }
543
544 /*
545  * msdosfs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
546  *              struct ucred *a_cred)
547  */
548 static int
549 msdosfs_read(struct vop_read_args *ap)
550 {
551         int error = 0;
552         int blsize;
553         int isadir;
554         int orig_resid;
555         u_int n;
556         u_long diff;
557         u_long on;
558         daddr_t lbn;
559         daddr_t rablock;
560         off_t raoffset;
561         off_t loffset;
562         int rasize;
563         int seqcount;
564         struct buf *bp;
565         struct vnode *vp = ap->a_vp;
566         struct denode *dep = VTODE(vp);
567         struct msdosfsmount *pmp = dep->de_pmp;
568         struct uio *uio = ap->a_uio;
569
570         if (uio->uio_offset < 0)
571                 return (EINVAL);
572
573         if ((uoff_t)uio->uio_offset > DOS_FILESIZE_MAX)
574                 return (0);
575         /*
576          * If they didn't ask for any data, then we are done.
577          */
578         orig_resid = uio->uio_resid;
579         if (orig_resid <= 0)
580                 return (0);
581
582         seqcount = ap->a_ioflag >> IO_SEQSHIFT;
583
584         isadir = dep->de_Attributes & ATTR_DIRECTORY;
585         do {
586                 if (uio->uio_offset >= dep->de_FileSize)
587                         break;
588
589                 /*
590                  * note: lbn is a cluster number, not a device block number.
591                  */
592                 lbn = de_off2cn(pmp, uio->uio_offset);
593                 loffset = de_cn2doff(pmp, lbn);
594
595                 /*
596                  * If we are operating on a directory file then be sure to
597                  * do i/o with the vnode for the filesystem instead of the
598                  * vnode for the directory.
599                  */
600                 if (isadir) {
601                         /*
602                          * convert cluster # to block #.  lbn is a
603                          * device block number after this.
604                          */
605                         error = pcbmap(dep, lbn, &lbn, NULL, &blsize);
606                         loffset = de_bntodoff(pmp, lbn);
607                         if (error == E2BIG) {
608                                 error = EINVAL;
609                                 break;
610                         } else if (error)
611                                 break;
612                         error = bread(pmp->pm_devvp, loffset, blsize, &bp);
613                 } else {
614                         blsize = pmp->pm_bpcluster;
615                         rablock = lbn + 1;
616                         raoffset = de_cn2doff(pmp, rablock);
617                         if (seqcount > 1 &&
618                             raoffset < dep->de_FileSize) {
619                                 rasize = pmp->pm_bpcluster;
620                                 error = breadn(vp, loffset, blsize,
621                                                 &raoffset, &rasize, 1, &bp); 
622                         } else {
623                                 error = bread(vp, loffset, blsize, &bp);
624                         }
625                 }
626                 if (error) {
627                         brelse(bp);
628                         break;
629                 }
630                 on = uio->uio_offset & pmp->pm_crbomask;
631                 diff = pmp->pm_bpcluster - on;
632                 n = diff > uio->uio_resid ? uio->uio_resid : diff;
633                 diff = dep->de_FileSize - uio->uio_offset;
634                 if (diff < n)
635                         n = diff;
636                 diff = blsize - bp->b_resid;
637                 if (diff < n)
638                         n = diff;
639                 error = uiomove(bp->b_data + on, (int) n, uio);
640                 brelse(bp);
641         } while (error == 0 && uio->uio_resid > 0 && n != 0);
642         if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
643             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
644                 dep->de_flag |= DE_ACCESS;
645         return (error);
646 }
647
648 /*
649  * Write data to a file or directory.
650  *
651  * msdosfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
652  *               struct ucred *a_cred)
653  */
654 static int
655 msdosfs_write(struct vop_write_args *ap)
656 {
657         int n;
658         int croffset;
659         int resid;
660         u_long osize;
661         int error = 0;
662         u_long count;
663         daddr_t cn, lastcn;
664         struct buf *bp;
665         int ioflag = ap->a_ioflag;
666         struct uio *uio = ap->a_uio;
667         struct thread *td = uio->uio_td;
668         struct vnode *vp = ap->a_vp;
669         struct vnode *thisvp;
670         struct denode *dep = VTODE(vp);
671         struct msdosfsmount *pmp = dep->de_pmp;
672         struct proc *p = (td ? td->td_proc : NULL);
673
674 #ifdef MSDOSFS_DEBUG
675         printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
676             vp, uio, ioflag, cred);
677         printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
678             dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
679 #endif
680
681         switch (vp->v_type) {
682         case VREG:
683                 if (ioflag & IO_APPEND)
684                         uio->uio_offset = dep->de_FileSize;
685                 thisvp = vp;
686                 break;
687         case VDIR:
688                 return EISDIR;
689         default:
690                 panic("msdosfs_write(): bad file type");
691         }
692
693         if (uio->uio_offset < 0)
694                 return (EFBIG);
695
696         if (uio->uio_resid == 0)
697                 return (0);
698
699         /*
700          * If they've exceeded their filesize limit, tell them about it.
701          */
702         if (p &&
703             ((uoff_t)uio->uio_offset + uio->uio_resid >
704             p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
705                 psignal(p, SIGXFSZ);
706                 return (EFBIG);
707         }
708
709         if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
710                 return (EFBIG);
711
712         /*
713          * If the offset we are starting the write at is beyond the end of
714          * the file, then they've done a seek.  Unix filesystems allow
715          * files with holes in them, DOS doesn't so we must fill the hole
716          * with zeroed blocks.
717          */
718         if (uio->uio_offset > dep->de_FileSize) {
719                 error = deextend(dep, uio->uio_offset);
720                 if (error)
721                         return (error);
722         }
723
724         /*
725          * Remember some values in case the write fails.
726          */
727         resid = uio->uio_resid;
728         osize = dep->de_FileSize;
729
730         /*
731          * If we write beyond the end of the file, extend it to its ultimate
732          * size ahead of the time to hopefully get a contiguous area.
733          */
734         if (uio->uio_offset + resid > osize) {
735                 count = de_clcount(pmp, uio->uio_offset + resid) -
736                         de_clcount(pmp, osize);
737                 error = extendfile(dep, count, NULL, NULL, 0);
738                 if (error &&  (error != ENOSPC || (ioflag & IO_UNIT)))
739                         goto errexit;
740                 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
741         } else
742                 lastcn = de_clcount(pmp, osize) - 1;
743
744         do {
745                 if (de_off2cn(pmp, uio->uio_offset) > lastcn) {
746                         error = ENOSPC;
747                         break;
748                 }
749
750                 croffset = uio->uio_offset & pmp->pm_crbomask;
751                 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
752                 if (uio->uio_offset + n > dep->de_FileSize) {
753                         dep->de_FileSize = uio->uio_offset + n;
754                         /* The object size needs to be set before buffer is allocated */
755                         vnode_pager_setsize(vp, dep->de_FileSize);
756                 }
757
758                 cn = de_off2cn(pmp, uio->uio_offset);
759                 if ((uio->uio_offset & pmp->pm_crbomask) == 0
760                     && (de_off2cn(pmp, uio->uio_offset + uio->uio_resid) 
761                         > de_off2cn(pmp, uio->uio_offset)
762                         || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
763                         /*
764                          * If either the whole cluster gets written,
765                          * or we write the cluster from its start beyond EOF,
766                          * then no need to read data from disk.
767                          */
768                         bp = getblk(thisvp, de_cn2doff(pmp, cn),
769                                     pmp->pm_bpcluster, 0, 0);
770                         clrbuf(bp);
771                         /*
772                          * Do the bmap now, since pcbmap needs buffers
773                          * for the fat table. (see msdosfs_strategy)
774                          */
775                         if (bp->b_bio2.bio_offset == NOOFFSET) {
776                                 daddr_t lblkno = de_off2cn(pmp, bp->b_loffset);
777                                 daddr_t dblkno;
778
779                                 error = pcbmap(dep, lblkno,
780                                                &dblkno, NULL, NULL);
781                                 if (error || dblkno == (daddr_t)-1) {
782                                         bp->b_bio2.bio_offset = NOOFFSET;
783                                 } else {
784                                         bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
785                                 }
786                         }
787                         if (bp->b_bio2.bio_offset == NOOFFSET) {
788                                 brelse(bp);
789                                 if (!error)
790                                         error = EIO;            /* XXX */
791                                 break;
792                         }
793                 } else {
794                         /*
795                          * The block we need to write into exists, so read
796                          * it in.
797                          */
798                         error = bread(thisvp, de_cn2doff(pmp, cn),
799                                       pmp->pm_bpcluster, &bp);
800                         if (error) {
801                                 brelse(bp);
802                                 break;
803                         }
804                 }
805
806                 /*
807                  * Should these vnode_pager_* functions be done on dir
808                  * files?
809                  */
810
811                 /*
812                  * Copy the data from user space into the buf header.
813                  */
814                 error = uiomove(bp->b_data + croffset, n, uio);
815                 if (error) {
816                         brelse(bp);
817                         break;
818                 }
819
820                 /*
821                  * If they want this synchronous then write it and wait for
822                  * it.  Otherwise, if on a cluster boundary write it
823                  * asynchronously so we can move on to the next block
824                  * without delay.  Otherwise do a delayed write because we
825                  * may want to write somemore into the block later.
826                  */
827                 if (ioflag & IO_SYNC)
828                         bwrite(bp);
829                 else if (n + croffset == pmp->pm_bpcluster)
830                         bawrite(bp);
831                 else
832                         bdwrite(bp);
833                 dep->de_flag |= DE_UPDATE;
834         } while (error == 0 && uio->uio_resid > 0);
835
836         /*
837          * If the write failed and they want us to, truncate the file back
838          * to the size it was before the write was attempted.
839          */
840 errexit:
841         if (error) {
842                 if (ioflag & IO_UNIT) {
843                         detrunc(dep, osize, ioflag & IO_SYNC, NULL);
844                         uio->uio_offset -= resid - uio->uio_resid;
845                         uio->uio_resid = resid;
846                 } else {
847                         detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NULL);
848                         if (uio->uio_resid != resid)
849                                 error = 0;
850                 }
851         } else if (ioflag & IO_SYNC)
852                 error = deupdat(dep, 1);
853         return (error);
854 }
855
856 /*
857  * Flush the blocks of a file to disk.
858  *
859  * This function is worthless for vnodes that represent directories. Maybe we
860  * could just do a sync if they try an fsync on a directory file.
861  *
862  * msdosfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
863  *               struct thread *a_td)
864  */
865 static int
866 msdosfs_fsync(struct vop_fsync_args *ap)
867 {
868         struct vnode *vp = ap->a_vp;
869
870         /*
871          * Flush all dirty buffers associated with a vnode.
872          */
873 #ifdef DIAGNOSTIC
874 loop:
875 #endif
876         vfsync(vp, ap->a_waitfor, 0, NOOFFSET, NULL, NULL);
877 #ifdef DIAGNOSTIC
878         if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
879                 vprint("msdosfs_fsync: dirty", vp);
880                 goto loop;
881         }
882 #endif
883         return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
884 }
885
886 /*
887  * msdosfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
888  *                struct componentname *a_cnp)
889  */
890 static int
891 msdosfs_remove(struct vop_old_remove_args *ap)
892 {
893         struct denode *dep = VTODE(ap->a_vp);
894         struct denode *ddep = VTODE(ap->a_dvp);
895         int error;
896
897         if (ap->a_vp->v_type == VDIR)
898                 error = EPERM;
899         else
900                 error = removede(ddep, dep);
901 #ifdef MSDOSFS_DEBUG
902         printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
903 #endif
904         return (error);
905 }
906
907 /*
908  * DOS filesystems don't know what links are. But since we already called
909  * msdosfs_lookup() with create and lockparent, the parent is locked so we
910  * have to free it before we return the error.
911  *
912  * msdosfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
913  *              struct componentname *a_cnp)
914  */
915 static int
916 msdosfs_link(struct vop_old_link_args *ap)
917 {
918         return (EOPNOTSUPP);
919 }
920
921 /*
922  * Renames on files require moving the denode to a new hash queue since the
923  * denode's location is used to compute which hash queue to put the file
924  * in. Unless it is a rename in place.  For example "mv a b".
925  *
926  * What follows is the basic algorithm:
927  *
928  * if (file move) {
929  *      if (dest file exists) {
930  *              remove dest file
931  *      }
932  *      if (dest and src in same directory) {
933  *              rewrite name in existing directory slot
934  *      } else {
935  *              write new entry in dest directory
936  *              update offset and dirclust in denode
937  *              move denode to new hash chain
938  *              clear old directory entry
939  *      }
940  * } else {
941  *      directory move
942  *      if (dest directory exists) {
943  *              if (dest is not empty) {
944  *                      return ENOTEMPTY
945  *              }
946  *              remove dest directory
947  *      }
948  *      if (dest and src in same directory) {
949  *              rewrite name in existing entry
950  *      } else {
951  *              be sure dest is not a child of src directory
952  *              write entry in dest directory
953  *              update "." and ".." in moved directory
954  *              clear old directory entry for moved directory
955  *      }
956  * }
957  *
958  * On entry:
959  *      source's parent directory is unlocked
960  *      source file or directory is unlocked
961  *      destination's parent directory is locked
962  *      destination file or directory is locked if it exists
963  *
964  * On exit:
965  *      all denodes should be released
966  *
967  * Notes:
968  * I'm not sure how the memory containing the pathnames pointed at by the
969  * componentname structures is freed, there may be some memory bleeding
970  * for each rename done.
971  *
972  * msdosfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
973  *                struct componentname *a_fcnp, struct vnode *a_tdvp,
974  *                struct vnode *a_tvp, struct componentname *a_tcnp)
975  */
976 static int
977 msdosfs_rename(struct vop_old_rename_args *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_td;
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         /*
1005          * Check for cross-device rename.
1006          */
1007         if ((fvp->v_mount != tdvp->v_mount) ||
1008             (tvp && (fvp->v_mount != tvp->v_mount))) {
1009                 error = EXDEV;
1010 abortit:
1011                 if (tdvp == tvp)
1012                         vrele(tdvp);
1013                 else
1014                         vput(tdvp);
1015                 if (tvp)
1016                         vput(tvp);
1017                 vrele(fdvp);
1018                 vrele(fvp);
1019                 return (error);
1020         }
1021
1022         /*
1023          * If source and dest are the same, do nothing.
1024          */
1025         if (tvp == fvp) {
1026                 error = 0;
1027                 goto abortit;
1028         }
1029
1030         /*
1031          * fvp, fdvp are unlocked, tvp, tdvp are locked.  Lock fvp and note
1032          * that we have to unlock it to use the abortit target.
1033          */
1034         error = vn_lock(fvp, LK_EXCLUSIVE, td);
1035         if (error)
1036                 goto abortit;
1037         dp = VTODE(fdvp);
1038         ip = VTODE(fvp);
1039
1040         /*
1041          * Be sure we are not renaming ".", "..", or an alias of ".". This
1042          * leads to a crippled directory tree.  It's pretty tough to do a
1043          * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1044          * doesn't work if the ".." entry is missing.
1045          */
1046         if (ip->de_Attributes & ATTR_DIRECTORY) {
1047                 /*
1048                  * Avoid ".", "..", and aliases of "." for obvious reasons.
1049                  */
1050                 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1051                     dp == ip ||
1052                     (fcnp->cn_flags & CNP_ISDOTDOT) ||
1053                     (tcnp->cn_flags & CNP_ISDOTDOT) ||
1054                     (ip->de_flag & DE_RENAME)) {
1055                         VOP_UNLOCK(fvp, 0, td);
1056                         error = EINVAL;
1057                         goto abortit;
1058                 }
1059                 ip->de_flag |= DE_RENAME;
1060                 doingdirectory++;
1061         }
1062
1063         /*
1064          * fvp locked, fdvp unlocked, tvp, tdvp locked.  DE_RENAME only
1065          * set if doingdirectory.  We will get fvp unlocked in fairly
1066          * short order.  dp and xp must be setup and fvp must be unlocked
1067          * for the out and bad targets to work properly.
1068          */
1069         dp = VTODE(tdvp);
1070         xp = tvp ? VTODE(tvp) : NULL;
1071
1072         /*
1073          * Remember direntry place to use for destination
1074          */
1075         to_diroffset = dp->de_fndoffset;
1076         to_count = dp->de_fndcnt;
1077
1078         /*
1079          * If ".." must be changed (ie the directory gets a new
1080          * parent) then the source directory must not be in the
1081          * directory heirarchy above the target, as this would
1082          * orphan everything below the source directory. Also
1083          * the user must have write permission in the source so
1084          * as to be able to change "..". We must repeat the call
1085          * to namei, as the parent directory is unlocked by the
1086          * call to doscheckpath().
1087          */
1088         error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
1089         VOP_UNLOCK(fvp, 0, td);
1090         if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1091                 newparent = 1;
1092
1093         /*
1094          * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1095          * DE_RENAME only set if doingdirectory.
1096          */
1097         if (doingdirectory && newparent) {
1098                 if (error)      /* write access check above */
1099                         goto bad;
1100                 if (xp != NULL) {
1101                         vput(tvp);
1102                         xp = NULL;
1103                 }
1104                 /*
1105                  * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1106                  * get an extra ref so we wind up with just an unlocked, ref'd
1107                  * tdvp.  The 'out' target skips tvp and tdvp cleanups (tdvp
1108                  * isn't locked so we can't use the out target).
1109                  */
1110                 vref(tdvp);
1111                 error = doscheckpath(ip, dp);
1112                 tcnp->cn_flags |= CNP_PDIRUNLOCK;
1113                 if (error) {
1114                         vrele(tdvp);
1115                         goto out;
1116                 }
1117                 /*
1118                  * relookup no longer messes with the ref count.  tdvp must
1119                  * be unlocked on entry and on success will be locked on
1120                  * return.
1121                  */
1122                 error = relookup(tdvp, &tvp, tcnp);
1123                 if (error) {
1124                         if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1125                                 vrele(tdvp);
1126                         else
1127                                 vput(tdvp);
1128                         goto out;
1129                 }
1130
1131                 /*
1132                  * tvp and tdvp are now locked again.
1133                  */
1134                 dp = VTODE(tdvp);
1135                 xp = tvp ? VTODE(tvp) : NULL;
1136         }
1137
1138         /*
1139          * tvp and tdvp are now locked again, the 'bad' target can be used
1140          * to clean them up again.  Delete an existant target and clean
1141          * up tvp.  Set xp to NULL to indicate that tvp has been cleaned up.
1142          */
1143         if (xp != NULL) {
1144                 /*
1145                  * Target must be empty if a directory and have no links
1146                  * to it. Also, ensure source and target are compatible
1147                  * (both directories, or both not directories).
1148                  */
1149                 if (xp->de_Attributes & ATTR_DIRECTORY) {
1150                         if (!dosdirempty(xp)) {
1151                                 error = ENOTEMPTY;
1152                                 goto bad;
1153                         }
1154                         if (!doingdirectory) {
1155                                 error = ENOTDIR;
1156                                 goto bad;
1157                         }
1158                 } else if (doingdirectory) {
1159                         error = EISDIR;
1160                         goto bad;
1161                 }
1162                 error = removede(dp, xp);
1163                 if (error)
1164                         goto bad;
1165                 vput(tvp);
1166                 xp = NULL;
1167                 tvp = NULL;
1168         }
1169
1170         /*
1171          * Convert the filename in tcnp into a dos filename. We copy this
1172          * into the denode and directory entry for the destination
1173          * file/directory.
1174          */
1175         error = uniqdosname(VTODE(tdvp), tcnp, toname);
1176         if (error)
1177                 goto bad;
1178
1179         /*
1180          * Since from wasn't locked at various places above, we have to do
1181          * a relookup here.  If the target and source are the same directory
1182          * we have to unlock the target directory in order to safely relookup
1183          * the source, because relookup expects its directory to be unlocked.
1184          *
1185          * Note that ap->a_fvp is still valid and ref'd.  Any cleanup must
1186          * now take that into account.
1187          *
1188          * The tdvp locking issues make this a real mess.
1189          */
1190         fcnp->cn_flags &= ~CNP_MODMASK;
1191         fcnp->cn_flags |= CNP_LOCKPARENT;
1192         if (newparent == 0)
1193                 VOP_UNLOCK(tdvp, 0, td);
1194         error = relookup(fdvp, &fvp, fcnp);
1195         if (error || fvp == NULL) {
1196                 /*
1197                  * From name has disappeared.  Note: fdvp might == tdvp.
1198                  *
1199                  * DE_RENAME is only set if doingdirectory.
1200                  */
1201                 if (doingdirectory)
1202                         panic("rename: lost dir entry");
1203                 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1204                         vrele(fdvp);
1205                 else
1206                         vput(fdvp);
1207                 if (newparent == 0)
1208                         vrele(tdvp);
1209                 else
1210                         vput(tdvp);
1211                 vrele(ap->a_fvp);
1212                 return(0);
1213         }
1214
1215         /*
1216          * No error occured.  tdvp, fdvp and fvp are all locked.  If 
1217          * newparent was 0 be aware that fdvp == tdvp.  tvp has been cleaned
1218          * up.  ap->a_fvp is still refd.
1219          */
1220         xp = VTODE(fvp);
1221         zp = VTODE(fdvp);
1222         from_diroffset = zp->de_fndoffset;
1223
1224         /*
1225          * Ensure that the directory entry still exists and has not
1226          * changed till now. If the source is a file the entry may
1227          * have been unlinked or renamed. In either case there is
1228          * no further work to be done. If the source is a directory
1229          * then it cannot have been rmdir'ed or renamed; this is
1230          * prohibited by the DE_RENAME flag.
1231          *
1232          * DE_RENAME is only set if doingdirectory.
1233          */
1234         if (xp != ip) {
1235                 if (doingdirectory)
1236                         panic("rename: lost dir entry");
1237                 goto done;
1238         } else {
1239                 u_long new_dirclust;
1240                 u_long new_diroffset;
1241
1242                 /*
1243                  * First write a new entry in the destination
1244                  * directory and mark the entry in the source directory
1245                  * as deleted.  Then move the denode to the correct hash
1246                  * chain for its new location in the filesystem.  And, if
1247                  * we moved a directory, then update its .. entry to point
1248                  * to the new parent directory.
1249                  */
1250                 bcopy(ip->de_Name, oldname, 11);
1251                 bcopy(toname, ip->de_Name, 11); /* update denode */
1252                 dp->de_fndoffset = to_diroffset;
1253                 dp->de_fndcnt = to_count;
1254                 error = createde(ip, dp, (struct denode **)0, tcnp);
1255                 if (error) {
1256                         bcopy(oldname, ip->de_Name, 11);
1257                         goto done;
1258                 }
1259                 ip->de_refcnt++;
1260                 zp->de_fndoffset = from_diroffset;
1261                 error = removede(zp, ip);
1262                 if (error) {
1263                         /* XXX should really panic here, fs is corrupt */
1264                         goto done;
1265                 }
1266                 if (!doingdirectory) {
1267                         error = pcbmap(dp, de_cluster(pmp, to_diroffset), 
1268                                        NULL, &new_dirclust, NULL);
1269                         if (error) {
1270                                 /* XXX should really panic here, fs is corrupt */
1271                                 goto done;
1272                         }
1273                         if (new_dirclust == MSDOSFSROOT)
1274                                 new_diroffset = to_diroffset;
1275                         else
1276                                 new_diroffset = to_diroffset & pmp->pm_crbomask;
1277                         msdosfs_reinsert(ip, new_dirclust, new_diroffset);
1278                 }
1279         }
1280
1281         /*
1282          * If we moved a directory to a new parent directory, then we must
1283          * fixup the ".." entry in the moved directory.
1284          */
1285         if (doingdirectory && newparent) {
1286                 cn = ip->de_StartCluster;
1287                 if (cn == MSDOSFSROOT) {
1288                         /* this should never happen */
1289                         panic("msdosfs_rename(): updating .. in root directory?");
1290                 } else {
1291                         bn = xcntobn(pmp, cn);
1292                 }
1293                 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
1294                 if (error) {
1295                         /* XXX should really panic here, fs is corrupt */
1296                         brelse(bp);
1297                         goto done;
1298                 }
1299                 dotdotp = (struct direntry *)bp->b_data + 1;
1300                 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1301                 if (FAT32(pmp))
1302                         putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1303                 error = bwrite(bp);
1304                 if (error) {
1305                         /* XXX should really panic here, fs is corrupt */
1306                         goto done;
1307                 }
1308         }
1309
1310         /*
1311          * done case fvp, fdvp, tdvp are locked.  ap->a_fvp is refd
1312          */
1313 done:
1314         if (doingdirectory)
1315                 ip->de_flag &= ~DE_RENAME;      /* XXX fvp not locked */
1316         vput(fvp);
1317         if (newparent)
1318                 vput(fdvp);
1319         else
1320                 vrele(fdvp);
1321         vput(tdvp);
1322         vrele(ap->a_fvp);
1323         return (error);
1324
1325         /*
1326          * 'bad' target: xp governs tvp.  tvp and tdvp arel ocked, fdvp and fvp
1327          * are not locked.  ip points to fvp's inode which may have DE_RENAME
1328          * set.
1329          */
1330 bad:
1331         if (xp)
1332                 vput(tvp);
1333         vput(tdvp);
1334 out:
1335         /*
1336          * 'out' target: tvp and tdvp have already been cleaned up.
1337          */
1338         if (doingdirectory)
1339                 ip->de_flag &= ~DE_RENAME;
1340         vrele(fdvp);
1341         vrele(fvp);
1342         return (error);
1343
1344 }
1345
1346 static struct {
1347         struct direntry dot;
1348         struct direntry dotdot;
1349 } dosdirtemplate = {
1350         {       ".       ", "   ",                      /* the . entry */
1351                 ATTR_DIRECTORY,                         /* file attribute */
1352                 0,                                      /* reserved */
1353                 0, { 0, 0 }, { 0, 0 },                  /* create time & date */
1354                 { 0, 0 },                               /* access date */
1355                 { 0, 0 },                               /* high bits of start cluster */
1356                 { 210, 4 }, { 210, 4 },                 /* modify time & date */
1357                 { 0, 0 },                               /* startcluster */
1358                 { 0, 0, 0, 0 }                          /* filesize */
1359         },
1360         {       "..      ", "   ",                      /* the .. entry */
1361                 ATTR_DIRECTORY,                         /* file attribute */
1362                 0,                                      /* reserved */
1363                 0, { 0, 0 }, { 0, 0 },                  /* create time & date */
1364                 { 0, 0 },                               /* access date */
1365                 { 0, 0 },                               /* high bits of start cluster */
1366                 { 210, 4 }, { 210, 4 },                 /* modify time & date */
1367                 { 0, 0 },                               /* startcluster */
1368                 { 0, 0, 0, 0 }                          /* filesize */
1369         }
1370 };
1371
1372 /*
1373  * msdosfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1374  *               struct componentname *a_cnp, struct vattr *a_vap)
1375  */
1376 static int
1377 msdosfs_mkdir(struct vop_old_mkdir_args *ap)
1378 {
1379         struct componentname *cnp = ap->a_cnp;
1380         struct denode *dep;
1381         struct denode *pdep = VTODE(ap->a_dvp);
1382         struct direntry *denp;
1383         struct msdosfsmount *pmp = pdep->de_pmp;
1384         struct buf *bp;
1385         u_long newcluster, pcl;
1386         int bn;
1387         int error;
1388         struct denode ndirent;
1389         struct timespec ts;
1390
1391         /*
1392          * If this is the root directory and there is no space left we
1393          * can't do anything.  This is because the root directory can not
1394          * change size.
1395          */
1396         if (pdep->de_StartCluster == MSDOSFSROOT
1397             && pdep->de_fndoffset >= pdep->de_FileSize) {
1398                 error = ENOSPC;
1399                 goto bad2;
1400         }
1401
1402         /*
1403          * Allocate a cluster to hold the about to be created directory.
1404          */
1405         error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1406         if (error)
1407                 goto bad2;
1408
1409         bzero(&ndirent, sizeof(ndirent));
1410         ndirent.de_pmp = pmp;
1411         ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1412         getnanotime(&ts);
1413         DETIMES(&ndirent, &ts, &ts, &ts);
1414
1415         /*
1416          * Now fill the cluster with the "." and ".." entries. And write
1417          * the cluster to disk.  This way it is there for the parent
1418          * directory to be pointing at if there were a crash.
1419          */
1420         bn = xcntobn(pmp, newcluster);
1421         /* always succeeds */
1422         bp = getblk(pmp->pm_devvp, de_bntodoff(pmp, bn),
1423                     pmp->pm_bpcluster, 0, 0);
1424         bzero(bp->b_data, pmp->pm_bpcluster);
1425         bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1426         denp = (struct direntry *)bp->b_data;
1427         putushort(denp[0].deStartCluster, newcluster);
1428         putushort(denp[0].deCDate, ndirent.de_CDate);
1429         putushort(denp[0].deCTime, ndirent.de_CTime);
1430         denp[0].deCHundredth = ndirent.de_CHun;
1431         putushort(denp[0].deADate, ndirent.de_ADate);
1432         putushort(denp[0].deMDate, ndirent.de_MDate);
1433         putushort(denp[0].deMTime, ndirent.de_MTime);
1434         pcl = pdep->de_StartCluster;
1435         if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1436                 pcl = 0;
1437         putushort(denp[1].deStartCluster, pcl);
1438         putushort(denp[1].deCDate, ndirent.de_CDate);
1439         putushort(denp[1].deCTime, ndirent.de_CTime);
1440         denp[1].deCHundredth = ndirent.de_CHun;
1441         putushort(denp[1].deADate, ndirent.de_ADate);
1442         putushort(denp[1].deMDate, ndirent.de_MDate);
1443         putushort(denp[1].deMTime, ndirent.de_MTime);
1444         if (FAT32(pmp)) {
1445                 putushort(denp[0].deHighClust, newcluster >> 16);
1446                 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1447         }
1448
1449         error = bwrite(bp);
1450         if (error)
1451                 goto bad;
1452
1453         /*
1454          * Now build up a directory entry pointing to the newly allocated
1455          * cluster.  This will be written to an empty slot in the parent
1456          * directory.
1457          */
1458         error = uniqdosname(pdep, cnp, ndirent.de_Name);
1459         if (error)
1460                 goto bad;
1461
1462         ndirent.de_Attributes = ATTR_DIRECTORY;
1463         ndirent.de_LowerCase = 0;
1464         ndirent.de_StartCluster = newcluster;
1465         ndirent.de_FileSize = 0;
1466         ndirent.de_dev = pdep->de_dev;
1467         ndirent.de_devvp = pdep->de_devvp;
1468         error = createde(&ndirent, pdep, &dep, cnp);
1469         if (error)
1470                 goto bad;
1471         *ap->a_vpp = DETOV(dep);
1472         return (0);
1473
1474 bad:
1475         clusterfree(pmp, newcluster, NULL);
1476 bad2:
1477         return (error);
1478 }
1479
1480 /*
1481  * msdosfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1482  *               struct componentname *a_cnp)
1483  */
1484 static int
1485 msdosfs_rmdir(struct vop_old_rmdir_args *ap)
1486 {
1487         struct vnode *vp = ap->a_vp;
1488         struct vnode *dvp = ap->a_dvp;
1489         struct componentname *cnp = ap->a_cnp;
1490         struct denode *ip, *dp;
1491         struct thread *td = cnp->cn_td;
1492         int error;
1493         
1494         ip = VTODE(vp);
1495         dp = VTODE(dvp);
1496
1497         /*
1498          * Verify the directory is empty (and valid).
1499          * (Rmdir ".." won't be valid since
1500          *  ".." will contain a reference to
1501          *  the current directory and thus be
1502          *  non-empty.)
1503          */
1504         error = 0;
1505         if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1506                 error = ENOTEMPTY;
1507                 goto out;
1508         }
1509         /*
1510          * Delete the entry from the directory.  For dos filesystems this
1511          * gets rid of the directory entry on disk, the in memory copy
1512          * still exists but the de_refcnt is <= 0.  This prevents it from
1513          * being found by deget().  When the vput() on dep is done we give
1514          * up access and eventually msdosfs_reclaim() will be called which
1515          * will remove it from the denode cache.
1516          */
1517         error = removede(dp, ip);
1518         if (error)
1519                 goto out;
1520         /*
1521          * This is where we decrement the link count in the parent
1522          * directory.  Since dos filesystems don't do this we just purge
1523          * the name cache.
1524          */
1525         VOP_UNLOCK(dvp, 0, td);
1526         /*
1527          * Truncate the directory that is being deleted.
1528          */
1529         error = detrunc(ip, (u_long)0, IO_SYNC, td);
1530
1531         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
1532 out:
1533         return (error);
1534 }
1535
1536 /*
1537  * DOS filesystems don't know what symlinks are.
1538  *
1539  * msdosfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1540  *                 struct componentname *a_cnp, struct vattr *a_vap,
1541  *                 char *a_target)
1542  */
1543 static int
1544 msdosfs_symlink(struct vop_old_symlink_args *ap)
1545 {
1546         return (EOPNOTSUPP);
1547 }
1548
1549 /*
1550  * msdosfs_readdir(struct vnode *a_vp, struct uio *a_uio,
1551  *                 struct ucred *a_cred, int *a_eofflag, int *a_ncookies,
1552  *                 u_long **a_cookies)
1553  */
1554 static int
1555 msdosfs_readdir(struct vop_readdir_args *ap)
1556 {
1557         int error = 0;
1558         int diff;
1559         long n;
1560         int blsize;
1561         long on;
1562         u_long cn;
1563         u_long dirsperblk;
1564         long bias = 0;
1565         daddr_t bn, lbn;
1566         struct buf *bp;
1567         struct denode *dep = VTODE(ap->a_vp);
1568         struct msdosfsmount *pmp = dep->de_pmp;
1569         struct direntry *dentp;
1570         struct uio *uio = ap->a_uio;
1571         u_long *cookies = NULL;
1572         int ncookies = 0;
1573         off_t offset, off;
1574         int chksum = -1;
1575         ino_t d_ino;
1576         uint16_t d_namlen;
1577         uint8_t d_type;
1578         char *d_name_storage = NULL;
1579         char *d_name;
1580
1581 #ifdef MSDOSFS_DEBUG
1582         printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1583             ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1584 #endif
1585
1586         /*
1587          * msdosfs_readdir() won't operate properly on regular files since
1588          * it does i/o only with the the filesystem vnode, and hence can
1589          * retrieve the wrong block from the buffer cache for a plain file.
1590          * So, fail attempts to readdir() on a plain file.
1591          */
1592         if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1593                 return (ENOTDIR);
1594
1595         /*
1596          * If the user buffer is smaller than the size of one dos directory
1597          * entry or the file offset is not a multiple of the size of a
1598          * directory entry, then we fail the read.
1599          */
1600         off = offset = uio->uio_offset;
1601         if (uio->uio_resid < sizeof(struct direntry) ||
1602             (offset & (sizeof(struct direntry) - 1)))
1603                 return (EINVAL);
1604
1605         if (ap->a_ncookies) {
1606                 ncookies = uio->uio_resid / 16;
1607                 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1608                        M_WAITOK);
1609                 *ap->a_cookies = cookies;
1610                 *ap->a_ncookies = ncookies;
1611         }
1612
1613         dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1614
1615         /*
1616          * If they are reading from the root directory then, we simulate
1617          * the . and .. entries since these don't exist in the root
1618          * directory.  We also set the offset bias to make up for having to
1619          * simulate these entries. By this I mean that at file offset 64 we
1620          * read the first entry in the root directory that lives on disk.
1621          */
1622         if (dep->de_StartCluster == MSDOSFSROOT
1623             || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1624 #if 0
1625                 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1626                     offset);
1627 #endif
1628                 bias = 2 * sizeof(struct direntry);
1629                 if (offset < bias) {
1630                         for (n = (int)offset / sizeof(struct direntry); n < 2;
1631                              n++) {
1632                                 if (FAT32(pmp))
1633                                         d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1634                                             * dirsperblk;
1635                                 else
1636                                         d_ino = 1;
1637                                 d_type = DT_DIR;
1638                                 if (n == 0) {
1639                                         d_namlen = 1;
1640                                         d_name = ".";
1641                                 } else /* if (n == 1) */{
1642                                         d_namlen = 2;
1643                                         d_name = "..";
1644                                 }
1645                                 if (vop_write_dirent(&error, uio, d_ino, d_type,
1646                                     d_namlen, d_name))
1647                                         goto out;
1648                                 if (error)
1649                                         goto out;
1650                                 offset += sizeof(struct direntry);
1651                                 off = offset;
1652                                 if (cookies) {
1653                                         *cookies++ = offset;
1654                                         if (--ncookies <= 0)
1655                                                 goto out;
1656                                 }
1657                         }
1658                 }
1659         }
1660
1661         d_name_storage = malloc(WIN_MAXLEN, M_TEMP, M_WAITOK);
1662         off = offset;
1663
1664         while (uio->uio_resid > 0) {
1665                 lbn = de_off2cn(pmp, offset - bias);
1666                 on = (offset - bias) & pmp->pm_crbomask;
1667                 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1668                 diff = dep->de_FileSize - (offset - bias);
1669                 if (diff <= 0)
1670                         break;
1671                 n = min(n, diff);
1672                 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1673                 if (error)
1674                         break;
1675                 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
1676                 if (error) {
1677                         brelse(bp);
1678                         free(d_name_storage, M_TEMP);
1679                         return (error);
1680                 }
1681                 n = min(n, blsize - bp->b_resid);
1682
1683                 /*
1684                  * Convert from dos directory entries to fs-independent
1685                  * directory entries.
1686                  */
1687                 for (dentp = (struct direntry *)(bp->b_data + on);
1688                      (char *)dentp < bp->b_data + on + n;
1689                      dentp++, offset += sizeof(struct direntry)) {
1690 #if 0
1691                         printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1692                             dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1693 #endif
1694                         d_name = d_name_storage;
1695                         /*
1696                          * If this is an unused entry, we can stop.
1697                          */
1698                         if (dentp->deName[0] == SLOT_EMPTY) {
1699                                 brelse(bp);
1700                                 goto out;
1701                         }
1702                         /*
1703                          * Skip deleted entries.
1704                          */
1705                         if (dentp->deName[0] == SLOT_DELETED) {
1706                                 chksum = -1;
1707                                 continue;
1708                         }
1709
1710                         /*
1711                          * Handle Win95 long directory entries
1712                          */
1713                         if (dentp->deAttributes == ATTR_WIN95) {
1714                                 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1715                                         continue;
1716                                 chksum = win2unixfn((struct winentry *)dentp,
1717                                         d_name, &d_namlen, chksum,
1718                                         pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1719                                         pmp->pm_u2w);
1720                                 continue;
1721                         }
1722
1723                         /*
1724                          * Skip volume labels
1725                          */
1726                         if (dentp->deAttributes & ATTR_VOLUME) {
1727                                 chksum = -1;
1728                                 continue;
1729                         }
1730                         /*
1731                          * This computation of d_ino must match
1732                          * the computation of va_fileid in
1733                          * msdosfs_getattr.
1734                          */
1735                         if (dentp->deAttributes & ATTR_DIRECTORY) {
1736                                 d_ino = getushort(dentp->deStartCluster);
1737                                 if (FAT32(pmp))
1738                                         d_ino |= getushort(dentp->deHighClust) << 16;
1739                                 /* if this is the root directory */
1740                                 if (d_ino != MSDOSFSROOT)
1741                                         d_ino = xcntobn(pmp, d_ino) * dirsperblk;
1742                                 else if (FAT32(pmp))
1743                                         d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1744                                             * dirsperblk;
1745                                 else
1746                                         d_ino = 1;
1747                                 d_type = DT_DIR;
1748                         } else {
1749                                 d_ino = offset / sizeof(struct direntry);
1750                                 d_type = DT_REG;
1751                         }
1752                         if (chksum != winChksum(dentp->deName)) {
1753                                 d_namlen = dos2unixfn(dentp->deName,
1754                                     (u_char *)d_name,
1755                                     dentp->deLowerCase |
1756                                         ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1757                                         (LCASE_BASE | LCASE_EXT) : 0),
1758                                     pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1759                                     pmp->pm_d2u,
1760                                     pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1761                                     pmp->pm_ul);
1762                         }
1763                         chksum = -1;
1764                         if (vop_write_dirent(&error, uio, d_ino, d_type,
1765                             d_namlen, d_name)) {
1766                                 brelse(bp);
1767                                 goto out;
1768                         }
1769                         if (error) {
1770                                 brelse(bp);
1771                                 goto out;
1772                         }
1773
1774                         if (cookies) {
1775                                 *cookies++ = offset + sizeof(struct direntry);
1776                                 if (--ncookies <= 0) {
1777                                         brelse(bp);
1778                                         goto out;
1779                                 }
1780                         }
1781                         off = offset + sizeof(struct direntry);
1782                 }
1783                 brelse(bp);
1784         }
1785 out:
1786         if (d_name_storage != NULL)
1787                 free(d_name_storage, M_TEMP);
1788
1789         /* Subtract unused cookies */
1790         if (ap->a_ncookies)
1791                 *ap->a_ncookies -= ncookies;
1792
1793         uio->uio_offset = off;
1794
1795         /*
1796          * Set the eofflag (NFS uses it)
1797          */
1798         if (ap->a_eofflag) {
1799                 if (dep->de_FileSize - (offset - bias) <= 0)
1800                         *ap->a_eofflag = 1;
1801                 else
1802                         *ap->a_eofflag = 0;
1803         }
1804         return (error);
1805 }
1806
1807 /*
1808  * vp  - address of vnode file the file
1809  * bn  - which cluster we are interested in mapping to a filesystem block number.
1810  * vpp - returns the vnode for the block special file holding the filesystem
1811  *       containing the file of interest
1812  * bnp - address of where to return the filesystem relative block number
1813  *
1814  * msdosfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
1815  *              off_t *a_doffsetp, int *a_runp, int *a_runb)
1816  */
1817 static int
1818 msdosfs_bmap(struct vop_bmap_args *ap)
1819 {
1820         struct denode *dep = VTODE(ap->a_vp);
1821         struct msdosfsmount *pmp = dep->de_pmp;
1822         daddr_t lbn;
1823         daddr_t dbn;
1824         int error;
1825
1826         if (ap->a_vpp != NULL)
1827                 *ap->a_vpp = dep->de_devvp;
1828         if (ap->a_doffsetp == NULL)
1829                 return (0);
1830         if (ap->a_runp) {
1831                 /*
1832                  * Sequential clusters should be counted here.
1833                  */
1834                 *ap->a_runp = 0;
1835         }
1836         if (ap->a_runb) {
1837                 *ap->a_runb = 0;
1838         }
1839         KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0);
1840         lbn = de_off2cn(pmp, ap->a_loffset);
1841         error = pcbmap(dep, lbn, &dbn, NULL, NULL);
1842         if (error || dbn == (daddr_t)-1) {
1843                 *ap->a_doffsetp = NOOFFSET;
1844         } else {
1845                 *ap->a_doffsetp = de_bntodoff(pmp, dbn);
1846         }
1847         return (error);
1848 }
1849
1850 /*
1851  * msdosfs_strategy(struct vnode *a_vp, struct bio *a_bio)
1852  */
1853 static int
1854 msdosfs_strategy(struct vop_strategy_args *ap)
1855 {
1856         struct bio *bio = ap->a_bio;
1857         struct bio *nbio;
1858         struct buf *bp = bio->bio_buf;
1859         struct vnode *vp = ap->a_vp;
1860         struct denode *dep = VTODE(vp);
1861         struct msdosfsmount *pmp = dep->de_pmp;
1862         int error = 0;
1863         daddr_t dblkno;
1864
1865         if (vp->v_type == VBLK || vp->v_type == VCHR)
1866                 panic("msdosfs_strategy: spec");
1867         /*
1868          * If we don't already know the filesystem relative block number
1869          * then get it using pcbmap().  If pcbmap() returns the block
1870          * number as -1 then we've got a hole in the file.  DOS filesystems
1871          * don't allow files with holes, so we shouldn't ever see this.
1872          */
1873         nbio = push_bio(bio);
1874         if (nbio->bio_offset == NOOFFSET) {
1875                 error = pcbmap(dep, de_off2cn(pmp, bio->bio_offset),
1876                                &dblkno, NULL, NULL);
1877                 if (error) {
1878                         bp->b_error = error;
1879                         bp->b_flags |= B_ERROR;
1880                         /* I/O was never started on nbio, must biodone(bio) */
1881                         biodone(bio);
1882                         return (error);
1883                 }
1884                 if (dblkno == (daddr_t)-1) {
1885                         nbio->bio_offset = NOOFFSET;
1886                         vfs_bio_clrbuf(bp);
1887                 } else {
1888                         nbio->bio_offset = de_bntodoff(pmp, dblkno);
1889                 }
1890         }
1891         if (nbio->bio_offset == NOOFFSET) {
1892                 /* I/O was never started on nbio, must biodone(bio) */
1893                 biodone(bio);
1894                 return (0);
1895         }
1896         /*
1897          * Read/write the block from/to the disk that contains the desired
1898          * file block.
1899          */
1900         vn_strategy(dep->de_devvp, nbio);
1901         return (0);
1902 }
1903
1904 /*
1905  * msdosfs_print(struct vnode *vp)
1906  */
1907 static int
1908 msdosfs_print(struct vop_print_args *ap)
1909 {
1910         struct denode *dep = VTODE(ap->a_vp);
1911
1912         printf(
1913             "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1914                dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1915         printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
1916         lockmgr_printinfo(&ap->a_vp->v_lock);
1917         printf("\n");
1918         return (0);
1919 }
1920
1921 /*
1922  * msdosfs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1923  */
1924 static int
1925 msdosfs_pathconf(struct vop_pathconf_args *ap)
1926 {
1927         struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1928
1929         switch (ap->a_name) {
1930         case _PC_LINK_MAX:
1931                 *ap->a_retval = 1;
1932                 return (0);
1933         case _PC_NAME_MAX:
1934                 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1935                 return (0);
1936         case _PC_PATH_MAX:
1937                 *ap->a_retval = PATH_MAX;
1938                 return (0);
1939         case _PC_CHOWN_RESTRICTED:
1940                 *ap->a_retval = 1;
1941                 return (0);
1942         case _PC_NO_TRUNC:
1943                 *ap->a_retval = 0;
1944                 return (0);
1945         default:
1946                 return (EINVAL);
1947         }
1948         /* NOTREACHED */
1949 }
1950
1951 /*
1952  * get page routine
1953  *
1954  * XXX By default, wimp out... note that a_offset is ignored (and always
1955  * XXX has been).
1956  */
1957 int
1958 msdosfs_getpages(struct vop_getpages_args *ap)
1959 {
1960         return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1961                 ap->a_reqpage);
1962 }
1963
1964 /*
1965  * put page routine
1966  *
1967  * XXX By default, wimp out... note that a_offset is ignored (and always
1968  * XXX has been).
1969  */
1970 int
1971 msdosfs_putpages(struct vop_putpages_args *ap)
1972 {
1973         return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1974                 ap->a_sync, ap->a_rtvals);
1975 }
1976
1977 /* Global vfs data structures for msdosfs */
1978 struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1979         { &vop_default_desc,            vop_defaultop },
1980         { &vop_access_desc,             (vnodeopv_entry_t) msdosfs_access },
1981         { &vop_bmap_desc,               (vnodeopv_entry_t) msdosfs_bmap },
1982         { &vop_old_lookup_desc,         (vnodeopv_entry_t) msdosfs_lookup },
1983         { &vop_open_desc,               (vnodeopv_entry_t) msdosfs_open },
1984         { &vop_close_desc,              (vnodeopv_entry_t) msdosfs_close },
1985         { &vop_old_create_desc,         (vnodeopv_entry_t) msdosfs_create },
1986         { &vop_fsync_desc,              (vnodeopv_entry_t) msdosfs_fsync },
1987         { &vop_getattr_desc,            (vnodeopv_entry_t) msdosfs_getattr },
1988         { &vop_inactive_desc,           (vnodeopv_entry_t) msdosfs_inactive },
1989         { &vop_islocked_desc,           (vnodeopv_entry_t) vop_stdislocked },
1990         { &vop_old_link_desc,           (vnodeopv_entry_t) msdosfs_link },
1991         { &vop_lock_desc,               (vnodeopv_entry_t) vop_stdlock },
1992         { &vop_old_mkdir_desc,          (vnodeopv_entry_t) msdosfs_mkdir },
1993         { &vop_old_mknod_desc,          (vnodeopv_entry_t) msdosfs_mknod },
1994         { &vop_pathconf_desc,           (vnodeopv_entry_t) msdosfs_pathconf },
1995         { &vop_print_desc,              (vnodeopv_entry_t) msdosfs_print },
1996         { &vop_read_desc,               (vnodeopv_entry_t) msdosfs_read },
1997         { &vop_readdir_desc,            (vnodeopv_entry_t) msdosfs_readdir },
1998         { &vop_reclaim_desc,            (vnodeopv_entry_t) msdosfs_reclaim },
1999         { &vop_old_remove_desc,         (vnodeopv_entry_t) msdosfs_remove },
2000         { &vop_old_rename_desc,         (vnodeopv_entry_t) msdosfs_rename },
2001         { &vop_old_rmdir_desc,          (vnodeopv_entry_t) msdosfs_rmdir },
2002         { &vop_setattr_desc,            (vnodeopv_entry_t) msdosfs_setattr },
2003         { &vop_strategy_desc,           (vnodeopv_entry_t) msdosfs_strategy },
2004         { &vop_old_symlink_desc,        (vnodeopv_entry_t) msdosfs_symlink },
2005         { &vop_unlock_desc,             (vnodeopv_entry_t) vop_stdunlock },
2006         { &vop_write_desc,              (vnodeopv_entry_t) msdosfs_write },
2007         { &vop_getpages_desc,           (vnodeopv_entry_t) msdosfs_getpages },
2008         { &vop_putpages_desc,           (vnodeopv_entry_t) msdosfs_putpages },
2009         { NULL, NULL }
2010 };