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