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