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