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