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