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