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