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