Add PCI IDs for Intel's ICH7 and ICH7M ATA/SATA hardware, used in
[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 $ */
4afd80f1 2/* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vnops.c,v 1.31 2006/03/24 22:39:22 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) {
4afd80f1 332 fileid = xcntobn(pmp, dep->de_StartCluster) * dirsperblk;
984263bc
MD
333 if (dep->de_StartCluster == MSDOSFSROOT)
334 fileid = 1;
335 } else {
4afd80f1 336 fileid = xcntobn(pmp, dep->de_dirclust) * dirsperblk;
984263bc
MD
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;
4afd80f1
MD
574
575 /*
576 * note: lbn is a cluster number, not a device block number.
577 */
578 lbn = de_off2cn(pmp, uio->uio_offset);
579 loffset = de_cn2doff(pmp, lbn);
580
984263bc
MD
581 /*
582 * If we are operating on a directory file then be sure to
583 * do i/o with the vnode for the filesystem instead of the
584 * vnode for the directory.
585 */
586 if (isadir) {
4afd80f1
MD
587 /*
588 * convert cluster # to block #. lbn is a
589 * device block number after this.
590 */
591 error = pcbmap(dep, lbn, &lbn, NULL, &blsize);
592 loffset = de_bntodoff(pmp, lbn);
984263bc
MD
593 if (error == E2BIG) {
594 error = EINVAL;
595 break;
596 } else if (error)
597 break;
54078292 598 error = bread(pmp->pm_devvp, loffset, blsize, &bp);
984263bc
MD
599 } else {
600 blsize = pmp->pm_bpcluster;
601 rablock = lbn + 1;
4afd80f1 602 raoffset = de_cn2doff(pmp, rablock);
984263bc 603 if (seqcount > 1 &&
54078292 604 raoffset < dep->de_FileSize) {
984263bc 605 rasize = pmp->pm_bpcluster;
54078292
MD
606 error = breadn(vp, loffset, blsize,
607 &raoffset, &rasize, 1, &bp);
984263bc 608 } else {
54078292 609 error = bread(vp, loffset, blsize, &bp);
984263bc
MD
610 }
611 }
612 if (error) {
613 brelse(bp);
614 break;
615 }
616 on = uio->uio_offset & pmp->pm_crbomask;
617 diff = pmp->pm_bpcluster - on;
618 n = diff > uio->uio_resid ? uio->uio_resid : diff;
619 diff = dep->de_FileSize - uio->uio_offset;
620 if (diff < n)
621 n = diff;
622 diff = blsize - bp->b_resid;
623 if (diff < n)
624 n = diff;
625 error = uiomove(bp->b_data + on, (int) n, uio);
626 brelse(bp);
627 } while (error == 0 && uio->uio_resid > 0 && n != 0);
628 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
629 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
630 dep->de_flag |= DE_ACCESS;
631 return (error);
632}
633
634/*
635 * Write data to a file or directory.
4625f023
CP
636 *
637 * msdosfs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
638 * struct ucred *a_cred)
984263bc
MD
639 */
640static int
4625f023 641msdosfs_write(struct vop_write_args *ap)
984263bc
MD
642{
643 int n;
644 int croffset;
645 int resid;
646 u_long osize;
647 int error = 0;
648 u_long count;
4afd80f1 649 daddr_t cn, lastcn;
984263bc
MD
650 struct buf *bp;
651 int ioflag = ap->a_ioflag;
652 struct uio *uio = ap->a_uio;
dadab5e9 653 struct thread *td = uio->uio_td;
984263bc
MD
654 struct vnode *vp = ap->a_vp;
655 struct vnode *thisvp;
656 struct denode *dep = VTODE(vp);
657 struct msdosfsmount *pmp = dep->de_pmp;
049d6e6d 658 struct proc *p = (td ? td->td_proc : NULL);
984263bc
MD
659
660#ifdef MSDOSFS_DEBUG
661 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
662 vp, uio, ioflag, cred);
663 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
664 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
665#endif
666
667 switch (vp->v_type) {
668 case VREG:
669 if (ioflag & IO_APPEND)
670 uio->uio_offset = dep->de_FileSize;
671 thisvp = vp;
672 break;
673 case VDIR:
674 return EISDIR;
675 default:
676 panic("msdosfs_write(): bad file type");
677 }
678
679 if (uio->uio_offset < 0)
680 return (EFBIG);
681
682 if (uio->uio_resid == 0)
683 return (0);
684
685 /*
686 * If they've exceeded their filesize limit, tell them about it.
687 */
688 if (p &&
689 ((uoff_t)uio->uio_offset + uio->uio_resid >
690 p->p_rlimit[RLIMIT_FSIZE].rlim_cur)) {
691 psignal(p, SIGXFSZ);
692 return (EFBIG);
693 }
694
695 if ((uoff_t)uio->uio_offset + uio->uio_resid > DOS_FILESIZE_MAX)
696 return (EFBIG);
697
698 /*
699 * If the offset we are starting the write at is beyond the end of
700 * the file, then they've done a seek. Unix filesystems allow
701 * files with holes in them, DOS doesn't so we must fill the hole
702 * with zeroed blocks.
703 */
704 if (uio->uio_offset > dep->de_FileSize) {
3b568787 705 error = deextend(dep, uio->uio_offset);
984263bc
MD
706 if (error)
707 return (error);
708 }
709
710 /*
711 * Remember some values in case the write fails.
712 */
713 resid = uio->uio_resid;
714 osize = dep->de_FileSize;
715
716 /*
717 * If we write beyond the end of the file, extend it to its ultimate
718 * size ahead of the time to hopefully get a contiguous area.
719 */
720 if (uio->uio_offset + resid > osize) {
721 count = de_clcount(pmp, uio->uio_offset + resid) -
722 de_clcount(pmp, osize);
723 error = extendfile(dep, count, NULL, NULL, 0);
724 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
725 goto errexit;
726 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
727 } else
728 lastcn = de_clcount(pmp, osize) - 1;
729
730 do {
4afd80f1 731 if (de_off2cn(pmp, uio->uio_offset) > lastcn) {
984263bc
MD
732 error = ENOSPC;
733 break;
734 }
735
736 croffset = uio->uio_offset & pmp->pm_crbomask;
737 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
738 if (uio->uio_offset + n > dep->de_FileSize) {
739 dep->de_FileSize = uio->uio_offset + n;
740 /* The object size needs to be set before buffer is allocated */
741 vnode_pager_setsize(vp, dep->de_FileSize);
742 }
743
4afd80f1 744 cn = de_off2cn(pmp, uio->uio_offset);
984263bc 745 if ((uio->uio_offset & pmp->pm_crbomask) == 0
4afd80f1
MD
746 && (de_off2cn(pmp, uio->uio_offset + uio->uio_resid)
747 > de_off2cn(pmp, uio->uio_offset)
984263bc
MD
748 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
749 /*
750 * If either the whole cluster gets written,
751 * or we write the cluster from its start beyond EOF,
752 * then no need to read data from disk.
753 */
4afd80f1 754 bp = getblk(thisvp, de_cn2doff(pmp, cn),
54078292 755 pmp->pm_bpcluster, 0, 0);
984263bc
MD
756 clrbuf(bp);
757 /*
758 * Do the bmap now, since pcbmap needs buffers
759 * for the fat table. (see msdosfs_strategy)
760 */
54078292 761 if (bp->b_bio2.bio_offset == NOOFFSET) {
4afd80f1 762 daddr_t lblkno = de_off2cn(pmp, bp->b_loffset);
54078292
MD
763 daddr_t dblkno;
764
4afd80f1
MD
765 error = pcbmap(dep, lblkno,
766 &dblkno, NULL, NULL);
54078292
MD
767 if (error || dblkno == (daddr_t)-1) {
768 bp->b_bio2.bio_offset = NOOFFSET;
769 } else {
4afd80f1 770 bp->b_bio2.bio_offset = de_bntodoff(pmp, dblkno);
54078292 771 }
984263bc 772 }
54078292 773 if (bp->b_bio2.bio_offset == NOOFFSET) {
984263bc
MD
774 brelse(bp);
775 if (!error)
776 error = EIO; /* XXX */
777 break;
778 }
779 } else {
780 /*
4afd80f1
MD
781 * The block we need to write into exists, so read
782 * it in.
984263bc 783 */
4afd80f1
MD
784 error = bread(thisvp, de_cn2doff(pmp, cn),
785 pmp->pm_bpcluster, &bp);
984263bc
MD
786 if (error) {
787 brelse(bp);
788 break;
789 }
790 }
791
792 /*
793 * Should these vnode_pager_* functions be done on dir
794 * files?
795 */
796
797 /*
798 * Copy the data from user space into the buf header.
799 */
800 error = uiomove(bp->b_data + croffset, n, uio);
801 if (error) {
802 brelse(bp);
803 break;
804 }
805
806 /*
807 * If they want this synchronous then write it and wait for
808 * it. Otherwise, if on a cluster boundary write it
809 * asynchronously so we can move on to the next block
810 * without delay. Otherwise do a delayed write because we
811 * may want to write somemore into the block later.
812 */
813 if (ioflag & IO_SYNC)
b1ce5639 814 bwrite(bp);
984263bc
MD
815 else if (n + croffset == pmp->pm_bpcluster)
816 bawrite(bp);
817 else
818 bdwrite(bp);
819 dep->de_flag |= DE_UPDATE;
820 } while (error == 0 && uio->uio_resid > 0);
821
822 /*
823 * If the write failed and they want us to, truncate the file back
824 * to the size it was before the write was attempted.
825 */
826errexit:
827 if (error) {
828 if (ioflag & IO_UNIT) {
3b568787 829 detrunc(dep, osize, ioflag & IO_SYNC, NULL);
984263bc
MD
830 uio->uio_offset -= resid - uio->uio_resid;
831 uio->uio_resid = resid;
832 } else {
3b568787 833 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NULL);
984263bc
MD
834 if (uio->uio_resid != resid)
835 error = 0;
836 }
837 } else if (ioflag & IO_SYNC)
838 error = deupdat(dep, 1);
839 return (error);
840}
841
842/*
843 * Flush the blocks of a file to disk.
844 *
845 * This function is worthless for vnodes that represent directories. Maybe we
846 * could just do a sync if they try an fsync on a directory file.
4625f023
CP
847 *
848 * msdosfs_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
849 * struct thread *a_td)
984263bc
MD
850 */
851static int
4625f023 852msdosfs_fsync(struct vop_fsync_args *ap)
984263bc
MD
853{
854 struct vnode *vp = ap->a_vp;
984263bc
MD
855
856 /*
857 * Flush all dirty buffers associated with a vnode.
858 */
6bae6177 859#ifdef DIAGNOSTIC
984263bc 860loop:
6bae6177 861#endif
54078292 862 vfsync(vp, ap->a_waitfor, 0, NOOFFSET, NULL, NULL);
984263bc 863#ifdef DIAGNOSTIC
6bae6177 864 if (ap->a_waitfor == MNT_WAIT && !RB_EMPTY(&vp->v_rbdirty_tree)) {
984263bc
MD
865 vprint("msdosfs_fsync: dirty", vp);
866 goto loop;
867 }
868#endif
984263bc
MD
869 return (deupdat(VTODE(vp), ap->a_waitfor == MNT_WAIT));
870}
871
4625f023
CP
872/*
873 * msdosfs_remove(struct vnode *a_dvp, struct vnode *a_vp,
874 * struct componentname *a_cnp)
875 */
984263bc 876static int
e62afb5f 877msdosfs_remove(struct vop_old_remove_args *ap)
984263bc
MD
878{
879 struct denode *dep = VTODE(ap->a_vp);
880 struct denode *ddep = VTODE(ap->a_dvp);
881 int error;
882
883 if (ap->a_vp->v_type == VDIR)
884 error = EPERM;
885 else
886 error = removede(ddep, dep);
887#ifdef MSDOSFS_DEBUG
888 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
889#endif
890 return (error);
891}
892
893/*
894 * DOS filesystems don't know what links are. But since we already called
895 * msdosfs_lookup() with create and lockparent, the parent is locked so we
896 * have to free it before we return the error.
4625f023
CP
897 *
898 * msdosfs_link(struct vnode *a_tdvp, struct vnode *a_vp,
899 * struct componentname *a_cnp)
984263bc
MD
900 */
901static int
e62afb5f 902msdosfs_link(struct vop_old_link_args *ap)
984263bc
MD
903{
904 return (EOPNOTSUPP);
905}
906
907/*
908 * Renames on files require moving the denode to a new hash queue since the
909 * denode's location is used to compute which hash queue to put the file
910 * in. Unless it is a rename in place. For example "mv a b".
911 *
912 * What follows is the basic algorithm:
913 *
914 * if (file move) {
915 * if (dest file exists) {
916 * remove dest file
917 * }
918 * if (dest and src in same directory) {
919 * rewrite name in existing directory slot
920 * } else {
921 * write new entry in dest directory
922 * update offset and dirclust in denode
923 * move denode to new hash chain
924 * clear old directory entry
925 * }
926 * } else {
927 * directory move
928 * if (dest directory exists) {
929 * if (dest is not empty) {
930 * return ENOTEMPTY
931 * }
932 * remove dest directory
933 * }
934 * if (dest and src in same directory) {
935 * rewrite name in existing entry
936 * } else {
937 * be sure dest is not a child of src directory
938 * write entry in dest directory
939 * update "." and ".." in moved directory
940 * clear old directory entry for moved directory
941 * }
942 * }
943 *
944 * On entry:
945 * source's parent directory is unlocked
946 * source file or directory is unlocked
947 * destination's parent directory is locked
948 * destination file or directory is locked if it exists
949 *
950 * On exit:
951 * all denodes should be released
952 *
953 * Notes:
954 * I'm not sure how the memory containing the pathnames pointed at by the
955 * componentname structures is freed, there may be some memory bleeding
956 * for each rename done.
4625f023
CP
957 *
958 * msdosfs_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
959 * struct componentname *a_fcnp, struct vnode *a_tdvp,
960 * struct vnode *a_tvp, struct componentname *a_tcnp)
984263bc
MD
961 */
962static int
e62afb5f 963msdosfs_rename(struct vop_old_rename_args *ap)
984263bc
MD
964{
965 struct vnode *tdvp = ap->a_tdvp;
966 struct vnode *fvp = ap->a_fvp;
967 struct vnode *fdvp = ap->a_fdvp;
968 struct vnode *tvp = ap->a_tvp;
969 struct componentname *tcnp = ap->a_tcnp;
970 struct componentname *fcnp = ap->a_fcnp;
dadab5e9 971 struct thread *td = fcnp->cn_td;
984263bc
MD
972 struct denode *ip, *xp, *dp, *zp;
973 u_char toname[11], oldname[11];
974 u_long from_diroffset, to_diroffset;
975 u_char to_count;
976 int doingdirectory = 0, newparent = 0;
977 int error;
978 u_long cn;
979 daddr_t bn;
980 struct denode *fddep; /* from file's parent directory */
981 struct msdosfsmount *pmp;
982 struct direntry *dotdotp;
983 struct buf *bp;
984
985 fddep = VTODE(ap->a_fdvp);
986 pmp = fddep->de_pmp;
987
988 pmp = VFSTOMSDOSFS(fdvp->v_mount);
989
984263bc
MD
990 /*
991 * Check for cross-device rename.
992 */
993 if ((fvp->v_mount != tdvp->v_mount) ||
994 (tvp && (fvp->v_mount != tvp->v_mount))) {
995 error = EXDEV;
996abortit:
997 if (tdvp == tvp)
998 vrele(tdvp);
999 else
1000 vput(tdvp);
1001 if (tvp)
1002 vput(tvp);
1003 vrele(fdvp);
1004 vrele(fvp);
1005 return (error);
1006 }
1007
1008 /*
1009 * If source and dest are the same, do nothing.
1010 */
1011 if (tvp == fvp) {
1012 error = 0;
1013 goto abortit;
1014 }
1015
fad57d0e
MD
1016 /*
1017 * fvp, fdvp are unlocked, tvp, tdvp are locked. Lock fvp and note
1018 * that we have to unlock it to use the abortit target.
1019 */
5fd012e0 1020 error = vn_lock(fvp, LK_EXCLUSIVE, td);
984263bc
MD
1021 if (error)
1022 goto abortit;
1023 dp = VTODE(fdvp);
1024 ip = VTODE(fvp);
1025
1026 /*
1027 * Be sure we are not renaming ".", "..", or an alias of ".". This
1028 * leads to a crippled directory tree. It's pretty tough to do a
1029 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1030 * doesn't work if the ".." entry is missing.
1031 */
1032 if (ip->de_Attributes & ATTR_DIRECTORY) {
1033 /*
1034 * Avoid ".", "..", and aliases of "." for obvious reasons.
1035 */
1036 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1037 dp == ip ||
2b69e610
MD
1038 (fcnp->cn_flags & CNP_ISDOTDOT) ||
1039 (tcnp->cn_flags & CNP_ISDOTDOT) ||
984263bc 1040 (ip->de_flag & DE_RENAME)) {
5fd012e0 1041 VOP_UNLOCK(fvp, 0, td);
984263bc
MD
1042 error = EINVAL;
1043 goto abortit;
1044 }
1045 ip->de_flag |= DE_RENAME;
1046 doingdirectory++;
1047 }
1048
1049 /*
fad57d0e
MD
1050 * fvp locked, fdvp unlocked, tvp, tdvp locked. DE_RENAME only
1051 * set if doingdirectory. We will get fvp unlocked in fairly
1052 * short order. dp and xp must be setup and fvp must be unlocked
1053 * for the out and bad targets to work properly.
984263bc
MD
1054 */
1055 dp = VTODE(tdvp);
1056 xp = tvp ? VTODE(tvp) : NULL;
fad57d0e 1057
984263bc
MD
1058 /*
1059 * Remember direntry place to use for destination
1060 */
1061 to_diroffset = dp->de_fndoffset;
1062 to_count = dp->de_fndcnt;
1063
1064 /*
1065 * If ".." must be changed (ie the directory gets a new
1066 * parent) then the source directory must not be in the
1067 * directory heirarchy above the target, as this would
1068 * orphan everything below the source directory. Also
1069 * the user must have write permission in the source so
1070 * as to be able to change "..". We must repeat the call
1071 * to namei, as the parent directory is unlocked by the
1072 * call to doscheckpath().
1073 */
dadab5e9 1074 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td);
5fd012e0 1075 VOP_UNLOCK(fvp, 0, td);
984263bc
MD
1076 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
1077 newparent = 1;
fad57d0e
MD
1078
1079 /*
1080 * ok. fvp, fdvp unlocked, tvp, tdvp locked. tvp may be NULL.
1081 * DE_RENAME only set if doingdirectory.
1082 */
984263bc
MD
1083 if (doingdirectory && newparent) {
1084 if (error) /* write access check above */
1085 goto bad;
fad57d0e 1086 if (xp != NULL) {
984263bc 1087 vput(tvp);
fad57d0e
MD
1088 xp = NULL;
1089 }
984263bc 1090 /*
fad57d0e
MD
1091 * checkpath vput's tdvp (VTOI(dp)) on return no matter what,
1092 * get an extra ref so we wind up with just an unlocked, ref'd
1093 * tdvp. The 'out' target skips tvp and tdvp cleanups (tdvp
1094 * isn't locked so we can't use the out target).
984263bc 1095 */
fad57d0e 1096 vref(tdvp);
984263bc 1097 error = doscheckpath(ip, dp);
6970b52a 1098 tcnp->cn_flags |= CNP_PDIRUNLOCK;
fad57d0e
MD
1099 if (error) {
1100 vrele(tdvp);
984263bc 1101 goto out;
fad57d0e
MD
1102 }
1103 /*
1104 * relookup no longer messes with the ref count. tdvp must
1105 * be unlocked on entry and on success will be locked on
1106 * return.
1107 */
984263bc 1108 error = relookup(tdvp, &tvp, tcnp);
fad57d0e
MD
1109 if (error) {
1110 if (tcnp->cn_flags & CNP_PDIRUNLOCK)
1111 vrele(tdvp);
1112 else
1113 vput(tdvp);
984263bc 1114 goto out;
fad57d0e
MD
1115 }
1116
1117 /*
1118 * tvp and tdvp are now locked again.
1119 */
984263bc
MD
1120 dp = VTODE(tdvp);
1121 xp = tvp ? VTODE(tvp) : NULL;
1122 }
1123
fad57d0e
MD
1124 /*
1125 * tvp and tdvp are now locked again, the 'bad' target can be used
1126 * to clean them up again. Delete an existant target and clean
1127 * up tvp. Set xp to NULL to indicate that tvp has been cleaned up.
1128 */
984263bc
MD
1129 if (xp != NULL) {
1130 /*
1131 * Target must be empty if a directory and have no links
1132 * to it. Also, ensure source and target are compatible
1133 * (both directories, or both not directories).
1134 */
1135 if (xp->de_Attributes & ATTR_DIRECTORY) {
1136 if (!dosdirempty(xp)) {
1137 error = ENOTEMPTY;
1138 goto bad;
1139 }
1140 if (!doingdirectory) {
1141 error = ENOTDIR;
1142 goto bad;
1143 }
984263bc
MD
1144 } else if (doingdirectory) {
1145 error = EISDIR;
1146 goto bad;
1147 }
1148 error = removede(dp, xp);
1149 if (error)
1150 goto bad;
1151 vput(tvp);
1152 xp = NULL;
fad57d0e 1153 tvp = NULL;
984263bc
MD
1154 }
1155
1156 /*
1157 * Convert the filename in tcnp into a dos filename. We copy this
1158 * into the denode and directory entry for the destination
1159 * file/directory.
1160 */
1161 error = uniqdosname(VTODE(tdvp), tcnp, toname);
1162 if (error)
fad57d0e 1163 goto bad;
984263bc
MD
1164
1165 /*
fad57d0e
MD
1166 * Since from wasn't locked at various places above, we have to do
1167 * a relookup here. If the target and source are the same directory
1168 * we have to unlock the target directory in order to safely relookup
1169 * the source, because relookup expects its directory to be unlocked.
1170 *
1171 * Note that ap->a_fvp is still valid and ref'd. Any cleanup must
1172 * now take that into account.
1173 *
1174 * The tdvp locking issues make this a real mess.
984263bc 1175 */
2b69e610 1176 fcnp->cn_flags &= ~CNP_MODMASK;
fad57d0e
MD
1177 fcnp->cn_flags |= CNP_LOCKPARENT;
1178 if (newparent == 0)
5fd012e0 1179 VOP_UNLOCK(tdvp, 0, td);
fad57d0e
MD
1180 error = relookup(fdvp, &fvp, fcnp);
1181 if (error || fvp == NULL) {
984263bc 1182 /*
fad57d0e
MD
1183 * From name has disappeared. Note: fdvp might == tdvp.
1184 *
1185 * DE_RENAME is only set if doingdirectory.
984263bc
MD
1186 */
1187 if (doingdirectory)
1188 panic("rename: lost dir entry");
fad57d0e
MD
1189 if (fcnp->cn_flags & CNP_PDIRUNLOCK)
1190 vrele(fdvp);
1191 else
1192 vput(fdvp);
1193 if (newparent == 0)
1194 vrele(tdvp);
1195 else
1196 vput(tdvp);
984263bc 1197 vrele(ap->a_fvp);
fad57d0e 1198 return(0);
984263bc 1199 }
fad57d0e
MD
1200
1201 /*
1202 * No error occured. tdvp, fdvp and fvp are all locked. If
1203 * newparent was 0 be aware that fdvp == tdvp. tvp has been cleaned
1204 * up. ap->a_fvp is still refd.
1205 */
984263bc
MD
1206 xp = VTODE(fvp);
1207 zp = VTODE(fdvp);
1208 from_diroffset = zp->de_fndoffset;
1209
1210 /*
1211 * Ensure that the directory entry still exists and has not
1212 * changed till now. If the source is a file the entry may
1213 * have been unlinked or renamed. In either case there is
1214 * no further work to be done. If the source is a directory
1215 * then it cannot have been rmdir'ed or renamed; this is
1216 * prohibited by the DE_RENAME flag.
fad57d0e
MD
1217 *
1218 * DE_RENAME is only set if doingdirectory.
984263bc
MD
1219 */
1220 if (xp != ip) {
1221 if (doingdirectory)
1222 panic("rename: lost dir entry");
fad57d0e 1223 goto done;
984263bc 1224 } else {
3446c007
MD
1225 u_long new_dirclust;
1226 u_long new_diroffset;
1227
984263bc
MD
1228 /*
1229 * First write a new entry in the destination
1230 * directory and mark the entry in the source directory
1231 * as deleted. Then move the denode to the correct hash
1232 * chain for its new location in the filesystem. And, if
1233 * we moved a directory, then update its .. entry to point
1234 * to the new parent directory.
1235 */
1236 bcopy(ip->de_Name, oldname, 11);
1237 bcopy(toname, ip->de_Name, 11); /* update denode */
1238 dp->de_fndoffset = to_diroffset;
1239 dp->de_fndcnt = to_count;
1240 error = createde(ip, dp, (struct denode **)0, tcnp);
1241 if (error) {
1242 bcopy(oldname, ip->de_Name, 11);
fad57d0e 1243 goto done;
984263bc
MD
1244 }
1245 ip->de_refcnt++;
1246 zp->de_fndoffset = from_diroffset;
1247 error = removede(zp, ip);
1248 if (error) {
1249 /* XXX should really panic here, fs is corrupt */
fad57d0e 1250 goto done;
984263bc
MD
1251 }
1252 if (!doingdirectory) {
4afd80f1
MD
1253 error = pcbmap(dp, de_cluster(pmp, to_diroffset),
1254 NULL, &new_dirclust, NULL);
984263bc
MD
1255 if (error) {
1256 /* XXX should really panic here, fs is corrupt */
fad57d0e 1257 goto done;
984263bc 1258 }
3446c007
MD
1259 if (new_dirclust == MSDOSFSROOT)
1260 new_diroffset = to_diroffset;
984263bc 1261 else
3446c007
MD
1262 new_diroffset = to_diroffset & pmp->pm_crbomask;
1263 msdosfs_reinsert(ip, new_dirclust, new_diroffset);
984263bc 1264 }
984263bc
MD
1265 }
1266
1267 /*
1268 * If we moved a directory to a new parent directory, then we must
1269 * fixup the ".." entry in the moved directory.
1270 */
1271 if (doingdirectory && newparent) {
1272 cn = ip->de_StartCluster;
1273 if (cn == MSDOSFSROOT) {
1274 /* this should never happen */
1275 panic("msdosfs_rename(): updating .. in root directory?");
fad57d0e 1276 } else {
4afd80f1 1277 bn = xcntobn(pmp, cn);
fad57d0e 1278 }
54078292 1279 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), pmp->pm_bpcluster, &bp);
984263bc
MD
1280 if (error) {
1281 /* XXX should really panic here, fs is corrupt */
1282 brelse(bp);
fad57d0e 1283 goto done;
984263bc
MD
1284 }
1285 dotdotp = (struct direntry *)bp->b_data + 1;
1286 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1287 if (FAT32(pmp))
1288 putushort(dotdotp->deHighClust, dp->de_StartCluster >> 16);
1289 error = bwrite(bp);
1290 if (error) {
1291 /* XXX should really panic here, fs is corrupt */
fad57d0e 1292 goto done;
984263bc
MD
1293 }
1294 }
1295
fad57d0e
MD
1296 /*
1297 * done case fvp, fdvp, tdvp are locked. ap->a_fvp is refd
1298 */
1299done:
1300 if (doingdirectory)
1301 ip->de_flag &= ~DE_RENAME; /* XXX fvp not locked */
1302 vput(fvp);
1303 if (newparent)
1304 vput(fdvp);
1305 else
1306 vrele(fdvp);
1307 vput(tdvp);
1308 vrele(ap->a_fvp);
1309 return (error);
1310
1311 /*
1312 * 'bad' target: xp governs tvp. tvp and tdvp arel ocked, fdvp and fvp
1313 * are not locked. ip points to fvp's inode which may have DE_RENAME
1314 * set.
1315 */
984263bc
MD
1316bad:
1317 if (xp)
1318 vput(tvp);
1319 vput(tdvp);
1320out:
fad57d0e
MD
1321 /*
1322 * 'out' target: tvp and tdvp have already been cleaned up.
1323 */
1324 if (doingdirectory)
1325 ip->de_flag &= ~DE_RENAME;
984263bc
MD
1326 vrele(fdvp);
1327 vrele(fvp);
1328 return (error);
1329
1330}
1331
1332static struct {
1333 struct direntry dot;
1334 struct direntry dotdot;
1335} dosdirtemplate = {
1336 { ". ", " ", /* the . entry */
1337 ATTR_DIRECTORY, /* file attribute */
1338 0, /* reserved */
1339 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1340 { 0, 0 }, /* access date */
1341 { 0, 0 }, /* high bits of start cluster */
1342 { 210, 4 }, { 210, 4 }, /* modify time & date */
1343 { 0, 0 }, /* startcluster */
1344 { 0, 0, 0, 0 } /* filesize */
1345 },
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};
1357
4625f023
CP
1358/*
1359 * msdosfs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1360 * struct componentname *a_cnp, struct vattr *a_vap)
1361 */
984263bc 1362static int
e62afb5f 1363msdosfs_mkdir(struct vop_old_mkdir_args *ap)
984263bc
MD
1364{
1365 struct componentname *cnp = ap->a_cnp;
1366 struct denode *dep;
1367 struct denode *pdep = VTODE(ap->a_dvp);
1368 struct direntry *denp;
1369 struct msdosfsmount *pmp = pdep->de_pmp;
1370 struct buf *bp;
1371 u_long newcluster, pcl;
1372 int bn;
1373 int error;
1374 struct denode ndirent;
1375 struct timespec ts;
1376
1377 /*
1378 * If this is the root directory and there is no space left we
1379 * can't do anything. This is because the root directory can not
1380 * change size.
1381 */
1382 if (pdep->de_StartCluster == MSDOSFSROOT
1383 && pdep->de_fndoffset >= pdep->de_FileSize) {
1384 error = ENOSPC;
1385 goto bad2;
1386 }
1387
1388 /*
1389 * Allocate a cluster to hold the about to be created directory.
1390 */
1391 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1392 if (error)
1393 goto bad2;
1394
1395 bzero(&ndirent, sizeof(ndirent));
1396 ndirent.de_pmp = pmp;
1397 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1398 getnanotime(&ts);
1399 DETIMES(&ndirent, &ts, &ts, &ts);
1400
1401 /*
1402 * Now fill the cluster with the "." and ".." entries. And write
1403 * the cluster to disk. This way it is there for the parent
1404 * directory to be pointing at if there were a crash.
1405 */
4afd80f1 1406 bn = xcntobn(pmp, newcluster);
984263bc 1407 /* always succeeds */
54078292
MD
1408 bp = getblk(pmp->pm_devvp, de_bntodoff(pmp, bn),
1409 pmp->pm_bpcluster, 0, 0);
984263bc
MD
1410 bzero(bp->b_data, pmp->pm_bpcluster);
1411 bcopy(&dosdirtemplate, bp->b_data, sizeof dosdirtemplate);
1412 denp = (struct direntry *)bp->b_data;
1413 putushort(denp[0].deStartCluster, newcluster);
1414 putushort(denp[0].deCDate, ndirent.de_CDate);
1415 putushort(denp[0].deCTime, ndirent.de_CTime);
1416 denp[0].deCHundredth = ndirent.de_CHun;
1417 putushort(denp[0].deADate, ndirent.de_ADate);
1418 putushort(denp[0].deMDate, ndirent.de_MDate);
1419 putushort(denp[0].deMTime, ndirent.de_MTime);
1420 pcl = pdep->de_StartCluster;
1421 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1422 pcl = 0;
1423 putushort(denp[1].deStartCluster, pcl);
1424 putushort(denp[1].deCDate, ndirent.de_CDate);
1425 putushort(denp[1].deCTime, ndirent.de_CTime);
1426 denp[1].deCHundredth = ndirent.de_CHun;
1427 putushort(denp[1].deADate, ndirent.de_ADate);
1428 putushort(denp[1].deMDate, ndirent.de_MDate);
1429 putushort(denp[1].deMTime, ndirent.de_MTime);
1430 if (FAT32(pmp)) {
1431 putushort(denp[0].deHighClust, newcluster >> 16);
1432 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1433 }
1434
1435 error = bwrite(bp);
1436 if (error)
1437 goto bad;
1438
1439 /*
1440 * Now build up a directory entry pointing to the newly allocated
1441 * cluster. This will be written to an empty slot in the parent
1442 * directory.
1443 */
984263bc
MD
1444 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1445 if (error)
1446 goto bad;
1447
1448 ndirent.de_Attributes = ATTR_DIRECTORY;
1449 ndirent.de_LowerCase = 0;
1450 ndirent.de_StartCluster = newcluster;
1451 ndirent.de_FileSize = 0;
1452 ndirent.de_dev = pdep->de_dev;
1453 ndirent.de_devvp = pdep->de_devvp;
1454 error = createde(&ndirent, pdep, &dep, cnp);
1455 if (error)
1456 goto bad;
1457 *ap->a_vpp = DETOV(dep);
1458 return (0);
1459
1460bad:
1461 clusterfree(pmp, newcluster, NULL);
1462bad2:
1463 return (error);
1464}
1465
4625f023
CP
1466/*
1467 * msdosfs_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1468 * struct componentname *a_cnp)
1469 */
984263bc 1470static int
e62afb5f 1471msdosfs_rmdir(struct vop_old_rmdir_args *ap)
984263bc 1472{
dadab5e9
MD
1473 struct vnode *vp = ap->a_vp;
1474 struct vnode *dvp = ap->a_dvp;
1475 struct componentname *cnp = ap->a_cnp;
1476 struct denode *ip, *dp;
1477 struct thread *td = cnp->cn_td;
984263bc
MD
1478 int error;
1479
1480 ip = VTODE(vp);
1481 dp = VTODE(dvp);
1482
1483 /*
1484 * Verify the directory is empty (and valid).
1485 * (Rmdir ".." won't be valid since
1486 * ".." will contain a reference to
1487 * the current directory and thus be
1488 * non-empty.)
1489 */
1490 error = 0;
1491 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1492 error = ENOTEMPTY;
1493 goto out;
1494 }
1495 /*
1496 * Delete the entry from the directory. For dos filesystems this
1497 * gets rid of the directory entry on disk, the in memory copy
1498 * still exists but the de_refcnt is <= 0. This prevents it from
1499 * being found by deget(). When the vput() on dep is done we give
1500 * up access and eventually msdosfs_reclaim() will be called which
1501 * will remove it from the denode cache.
1502 */
1503 error = removede(dp, ip);
1504 if (error)
1505 goto out;
1506 /*
1507 * This is where we decrement the link count in the parent
1508 * directory. Since dos filesystems don't do this we just purge
1509 * the name cache.
1510 */
5fd012e0 1511 VOP_UNLOCK(dvp, 0, td);
984263bc
MD
1512 /*
1513 * Truncate the directory that is being deleted.
1514 */
3b568787 1515 error = detrunc(ip, (u_long)0, IO_SYNC, td);
984263bc 1516
5fd012e0 1517 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
1518out:
1519 return (error);
1520}
1521
1522/*
1523 * DOS filesystems don't know what symlinks are.
4625f023
CP
1524 *
1525 * msdosfs_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1526 * struct componentname *a_cnp, struct vattr *a_vap,
1527 * char *a_target)
984263bc
MD
1528 */
1529static int
e62afb5f 1530msdosfs_symlink(struct vop_old_symlink_args *ap)
984263bc
MD
1531{
1532 return (EOPNOTSUPP);
1533}
1534
4625f023
CP
1535/*
1536 * msdosfs_readdir(struct vnode *a_vp, struct uio *a_uio,
1537 * struct ucred *a_cred, int *a_eofflag, int *a_ncookies,
1538 * u_long **a_cookies)
1539 */
984263bc 1540static int
4625f023 1541msdosfs_readdir(struct vop_readdir_args *ap)
984263bc
MD
1542{
1543 int error = 0;
1544 int diff;
1545 long n;
1546 int blsize;
1547 long on;
1548 u_long cn;
984263bc
MD
1549 u_long dirsperblk;
1550 long bias = 0;
1551 daddr_t bn, lbn;
1552 struct buf *bp;
1553 struct denode *dep = VTODE(ap->a_vp);
1554 struct msdosfsmount *pmp = dep->de_pmp;
1555 struct direntry *dentp;
984263bc
MD
1556 struct uio *uio = ap->a_uio;
1557 u_long *cookies = NULL;
1558 int ncookies = 0;
1559 off_t offset, off;
1560 int chksum = -1;
1754e0da
JS
1561 ino_t d_ino;
1562 uint16_t d_namlen;
1563 uint8_t d_type;
1564 char *d_name_storage = NULL;
1565 char *d_name;
984263bc
MD
1566
1567#ifdef MSDOSFS_DEBUG
1568 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1569 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1570#endif
1571
1572 /*
1573 * msdosfs_readdir() won't operate properly on regular files since
1574 * it does i/o only with the the filesystem vnode, and hence can
1575 * retrieve the wrong block from the buffer cache for a plain file.
1576 * So, fail attempts to readdir() on a plain file.
1577 */
1578 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1579 return (ENOTDIR);
1580
984263bc
MD
1581 /*
1582 * If the user buffer is smaller than the size of one dos directory
1583 * entry or the file offset is not a multiple of the size of a
1584 * directory entry, then we fail the read.
1585 */
1586 off = offset = uio->uio_offset;
1587 if (uio->uio_resid < sizeof(struct direntry) ||
1588 (offset & (sizeof(struct direntry) - 1)))
1589 return (EINVAL);
1590
1591 if (ap->a_ncookies) {
1592 ncookies = uio->uio_resid / 16;
1593 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
1594 M_WAITOK);
1595 *ap->a_cookies = cookies;
1596 *ap->a_ncookies = ncookies;
1597 }
1598
1599 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1600
1601 /*
1602 * If they are reading from the root directory then, we simulate
1603 * the . and .. entries since these don't exist in the root
1604 * directory. We also set the offset bias to make up for having to
1605 * simulate these entries. By this I mean that at file offset 64 we
1606 * read the first entry in the root directory that lives on disk.
1607 */
1608 if (dep->de_StartCluster == MSDOSFSROOT
1609 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1610#if 0
1611 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1612 offset);
1613#endif
1614 bias = 2 * sizeof(struct direntry);
1615 if (offset < bias) {
1754e0da
JS
1616 for (n = (int)offset / sizeof(struct direntry); n < 2;
1617 n++) {
984263bc 1618 if (FAT32(pmp))
4afd80f1 1619 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1754e0da 1620 * dirsperblk;
984263bc 1621 else
1754e0da
JS
1622 d_ino = 1;
1623 d_type = DT_DIR;
1624 if (n == 0) {
1625 d_namlen = 1;
1626 d_name = ".";
1627 } else /* if (n == 1) */{
1628 d_namlen = 2;
1629 d_name = "..";
984263bc 1630 }
1754e0da
JS
1631 if (vop_write_dirent(&error, uio, d_ino, d_type,
1632 d_namlen, d_name))
984263bc 1633 goto out;
984263bc
MD
1634 if (error)
1635 goto out;
1636 offset += sizeof(struct direntry);
1637 off = offset;
1638 if (cookies) {
1639 *cookies++ = offset;
1640 if (--ncookies <= 0)
1641 goto out;
1642 }
1643 }
1644 }
1645 }
1646
1754e0da 1647 d_name_storage = malloc(WIN_MAXLEN, M_TEMP, M_WAITOK);
984263bc 1648 off = offset;
1754e0da 1649
984263bc 1650 while (uio->uio_resid > 0) {
4afd80f1 1651 lbn = de_off2cn(pmp, offset - bias);
984263bc
MD
1652 on = (offset - bias) & pmp->pm_crbomask;
1653 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1654 diff = dep->de_FileSize - (offset - bias);
1655 if (diff <= 0)
1656 break;
1657 n = min(n, diff);
1658 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1659 if (error)
1660 break;
54078292 1661 error = bread(pmp->pm_devvp, de_bntodoff(pmp, bn), blsize, &bp);
984263bc
MD
1662 if (error) {
1663 brelse(bp);
1754e0da 1664 free(d_name_storage, M_TEMP);
984263bc
MD
1665 return (error);
1666 }
1667 n = min(n, blsize - bp->b_resid);
1668
1669 /*
1670 * Convert from dos directory entries to fs-independent
1671 * directory entries.
1672 */
1673 for (dentp = (struct direntry *)(bp->b_data + on);
1674 (char *)dentp < bp->b_data + on + n;
1675 dentp++, offset += sizeof(struct direntry)) {
1676#if 0
1677 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1678 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1679#endif
1754e0da 1680 d_name = d_name_storage;
984263bc
MD
1681 /*
1682 * If this is an unused entry, we can stop.
1683 */
1684 if (dentp->deName[0] == SLOT_EMPTY) {
1685 brelse(bp);
1686 goto out;
1687 }
1688 /*
1689 * Skip deleted entries.
1690 */
1691 if (dentp->deName[0] == SLOT_DELETED) {
1692 chksum = -1;
1693 continue;
1694 }
1695
1696 /*
1697 * Handle Win95 long directory entries
1698 */
1699 if (dentp->deAttributes == ATTR_WIN95) {
1700 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1701 continue;
1702 chksum = win2unixfn((struct winentry *)dentp,
1754e0da 1703 d_name, &d_namlen, chksum,
984263bc
MD
1704 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1705 pmp->pm_u2w);
1706 continue;
1707 }
1708
1709 /*
1710 * Skip volume labels
1711 */
1712 if (dentp->deAttributes & ATTR_VOLUME) {
1713 chksum = -1;
1714 continue;
1715 }
1716 /*
1754e0da 1717 * This computation of d_ino must match
984263bc
MD
1718 * the computation of va_fileid in
1719 * msdosfs_getattr.
1720 */
1721 if (dentp->deAttributes & ATTR_DIRECTORY) {
1754e0da 1722 d_ino = getushort(dentp->deStartCluster);
984263bc 1723 if (FAT32(pmp))
1754e0da 1724 d_ino |= getushort(dentp->deHighClust) << 16;
984263bc 1725 /* if this is the root directory */
1754e0da 1726 if (d_ino != MSDOSFSROOT)
4afd80f1 1727 d_ino = xcntobn(pmp, d_ino) * dirsperblk;
1754e0da 1728 else if (FAT32(pmp))
4afd80f1 1729 d_ino = xcntobn(pmp, pmp->pm_rootdirblk)
1754e0da 1730 * dirsperblk;
984263bc 1731 else
1754e0da
JS
1732 d_ino = 1;
1733 d_type = DT_DIR;
984263bc 1734 } else {
1754e0da
JS
1735 d_ino = offset / sizeof(struct direntry);
1736 d_type = DT_REG;
984263bc 1737 }
1754e0da
JS
1738 if (chksum != winChksum(dentp->deName)) {
1739 d_namlen = dos2unixfn(dentp->deName,
1740 (u_char *)d_name,
984263bc
MD
1741 dentp->deLowerCase |
1742 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1743 (LCASE_BASE | LCASE_EXT) : 0),
1744 pmp->pm_flags & MSDOSFSMNT_U2WTABLE,
1745 pmp->pm_d2u,
1746 pmp->pm_flags & MSDOSFSMNT_ULTABLE,
1747 pmp->pm_ul);
1754e0da 1748 }
984263bc 1749 chksum = -1;
1754e0da
JS
1750 if (vop_write_dirent(&error, uio, d_ino, d_type,
1751 d_namlen, d_name)) {
984263bc
MD
1752 brelse(bp);
1753 goto out;
1754 }
984263bc
MD
1755 if (error) {
1756 brelse(bp);
1757 goto out;
1758 }
1754e0da 1759
984263bc
MD
1760 if (cookies) {
1761 *cookies++ = offset + sizeof(struct direntry);
1762 if (--ncookies <= 0) {
1763 brelse(bp);
1764 goto out;
1765 }
1766 }
1767 off = offset + sizeof(struct direntry);
1768 }
1769 brelse(bp);
1770 }
1771out:
1754e0da
JS
1772 if (d_name_storage != NULL)
1773 free(d_name_storage, M_TEMP);
1774
984263bc
MD
1775 /* Subtract unused cookies */
1776 if (ap->a_ncookies)
1777 *ap->a_ncookies -= ncookies;
1778
1779 uio->uio_offset = off;
1780
1781 /*
1782 * Set the eofflag (NFS uses it)
1783 */
1784 if (ap->a_eofflag) {
1785 if (dep->de_FileSize - (offset - bias) <= 0)
1786 *ap->a_eofflag = 1;
1787 else
1788 *ap->a_eofflag = 0;
1789 }
1790 return (error);
1791}
1792
1793/*
1794 * vp - address of vnode file the file
1795 * bn - which cluster we are interested in mapping to a filesystem block number.
1796 * vpp - returns the vnode for the block special file holding the filesystem
1797 * containing the file of interest
1798 * bnp - address of where to return the filesystem relative block number
4625f023 1799 *
54078292
MD
1800 * msdosfs_bmap(struct vnode *a_vp, off_t a_loffset, struct vnode **a_vpp,
1801 * off_t *a_doffsetp, int *a_runp, int *a_runb)
984263bc
MD
1802 */
1803static int
4625f023 1804msdosfs_bmap(struct vop_bmap_args *ap)
984263bc
MD
1805{
1806 struct denode *dep = VTODE(ap->a_vp);
54078292
MD
1807 struct msdosfsmount *pmp = dep->de_pmp;
1808 daddr_t lbn;
1809 daddr_t dbn;
1810 int error;
984263bc
MD
1811
1812 if (ap->a_vpp != NULL)
1813 *ap->a_vpp = dep->de_devvp;
54078292 1814 if (ap->a_doffsetp == NULL)
984263bc
MD
1815 return (0);
1816 if (ap->a_runp) {
1817 /*
1818 * Sequential clusters should be counted here.
1819 */
1820 *ap->a_runp = 0;
1821 }
1822 if (ap->a_runb) {
1823 *ap->a_runb = 0;
1824 }
4afd80f1
MD
1825 KKASSERT(((int)ap->a_loffset & ((1 << pmp->pm_cnshift) - 1)) == 0);
1826 lbn = de_off2cn(pmp, ap->a_loffset);
1827 error = pcbmap(dep, lbn, &dbn, NULL, NULL);
54078292
MD
1828 if (error || dbn == (daddr_t)-1) {
1829 *ap->a_doffsetp = NOOFFSET;
1830 } else {
4afd80f1 1831 *ap->a_doffsetp = de_bntodoff(pmp, dbn);
54078292
MD
1832 }
1833 return (error);
984263bc
MD
1834}
1835
4625f023 1836/*
81b5c339 1837 * msdosfs_strategy(struct vnode *a_vp, struct bio *a_bio)
4625f023 1838 */
984263bc 1839static int
4625f023 1840msdosfs_strategy(struct vop_strategy_args *ap)
984263bc 1841{
81b5c339
MD
1842 struct bio *bio = ap->a_bio;
1843 struct bio *nbio;
1844 struct buf *bp = bio->bio_buf;
1845 struct vnode *vp = ap->a_vp;
1846 struct denode *dep = VTODE(vp);
54078292 1847 struct msdosfsmount *pmp = dep->de_pmp;
984263bc 1848 int error = 0;
54078292 1849 daddr_t dblkno;
984263bc 1850
81b5c339 1851 if (vp->v_type == VBLK || vp->v_type == VCHR)
984263bc
MD
1852 panic("msdosfs_strategy: spec");
1853 /*
1854 * If we don't already know the filesystem relative block number
1855 * then get it using pcbmap(). If pcbmap() returns the block
1856 * number as -1 then we've got a hole in the file. DOS filesystems
1857 * don't allow files with holes, so we shouldn't ever see this.
1858 */
81b5c339 1859 nbio = push_bio(bio);
54078292 1860 if (nbio->bio_offset == NOOFFSET) {
4afd80f1
MD
1861 error = pcbmap(dep, de_off2cn(pmp, bio->bio_offset),
1862 &dblkno, NULL, NULL);
984263bc
MD
1863 if (error) {
1864 bp->b_error = error;
1865 bp->b_flags |= B_ERROR;
81b5c339
MD
1866 /* I/O was never started on nbio, must biodone(bio) */
1867 biodone(bio);
984263bc
MD
1868 return (error);
1869 }
54078292
MD
1870 if (dblkno == (daddr_t)-1) {
1871 nbio->bio_offset = NOOFFSET;
984263bc 1872 vfs_bio_clrbuf(bp);
54078292 1873 } else {
4afd80f1 1874 nbio->bio_offset = de_bntodoff(pmp, dblkno);
54078292 1875 }
984263bc 1876 }
54078292 1877 if (nbio->bio_offset == NOOFFSET) {
81b5c339
MD
1878 /* I/O was never started on nbio, must biodone(bio) */
1879 biodone(bio);
984263bc
MD
1880 return (0);
1881 }
1882 /*
1883 * Read/write the block from/to the disk that contains the desired
1884 * file block.
1885 */
81b5c339 1886 vn_strategy(dep->de_devvp, nbio);
984263bc
MD
1887 return (0);
1888}
1889
4625f023
CP
1890/*
1891 * msdosfs_print(struct vnode *vp)
1892 */
984263bc 1893static int
4625f023 1894msdosfs_print(struct vop_print_args *ap)
984263bc
MD
1895{
1896 struct denode *dep = VTODE(ap->a_vp);
1897
1898 printf(
1899 "tag VT_MSDOSFS, startcluster %lu, dircluster %lu, diroffset %lu ",
1900 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1901 printf(" dev %d, %d", major(dep->de_dev), minor(dep->de_dev));
3446c007 1902 lockmgr_printinfo(&ap->a_vp->v_lock);
984263bc
MD
1903 printf("\n");
1904 return (0);
1905}
1906
4625f023
CP
1907/*
1908 * msdosfs_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1909 */
984263bc 1910static int
4625f023 1911msdosfs_pathconf(struct vop_pathconf_args *ap)
984263bc
MD
1912{
1913 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1914
1915 switch (ap->a_name) {
1916 case _PC_LINK_MAX:
1917 *ap->a_retval = 1;
1918 return (0);
1919 case _PC_NAME_MAX:
1920 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1921 return (0);
1922 case _PC_PATH_MAX:
1923 *ap->a_retval = PATH_MAX;
1924 return (0);
1925 case _PC_CHOWN_RESTRICTED:
1926 *ap->a_retval = 1;
1927 return (0);
1928 case _PC_NO_TRUNC:
1929 *ap->a_retval = 0;
1930 return (0);
1931 default:
1932 return (EINVAL);
1933 }
1934 /* NOTREACHED */
1935}
1936
1937/*
1938 * get page routine
1939 *
1940 * XXX By default, wimp out... note that a_offset is ignored (and always
1941 * XXX has been).
1942 */
1943int
4625f023 1944msdosfs_getpages(struct vop_getpages_args *ap)
984263bc
MD
1945{
1946 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1947 ap->a_reqpage);
1948}
1949
1950/*
1951 * put page routine
1952 *
1953 * XXX By default, wimp out... note that a_offset is ignored (and always
1954 * XXX has been).
1955 */
1956int
4625f023 1957msdosfs_putpages(struct vop_putpages_args *ap)
984263bc
MD
1958{
1959 return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1960 ap->a_sync, ap->a_rtvals);
1961}
1962
1963/* Global vfs data structures for msdosfs */
0961aa92 1964struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
2d3e977e 1965 { &vop_default_desc, vop_defaultop },
625ddaba
JS
1966 { &vop_access_desc, (vnodeopv_entry_t) msdosfs_access },
1967 { &vop_bmap_desc, (vnodeopv_entry_t) msdosfs_bmap },
e62afb5f 1968 { &vop_old_lookup_desc, (vnodeopv_entry_t) msdosfs_lookup },
625ddaba 1969 { &vop_close_desc, (vnodeopv_entry_t) msdosfs_close },
e62afb5f 1970 { &vop_old_create_desc, (vnodeopv_entry_t) msdosfs_create },
625ddaba
JS
1971 { &vop_fsync_desc, (vnodeopv_entry_t) msdosfs_fsync },
1972 { &vop_getattr_desc, (vnodeopv_entry_t) msdosfs_getattr },
1973 { &vop_inactive_desc, (vnodeopv_entry_t) msdosfs_inactive },
1974 { &vop_islocked_desc, (vnodeopv_entry_t) vop_stdislocked },
e62afb5f 1975 { &vop_old_link_desc, (vnodeopv_entry_t) msdosfs_link },
625ddaba 1976 { &vop_lock_desc, (vnodeopv_entry_t) vop_stdlock },
e62afb5f
MD
1977 { &vop_old_mkdir_desc, (vnodeopv_entry_t) msdosfs_mkdir },
1978 { &vop_old_mknod_desc, (vnodeopv_entry_t) msdosfs_mknod },
625ddaba
JS
1979 { &vop_pathconf_desc, (vnodeopv_entry_t) msdosfs_pathconf },
1980 { &vop_print_desc, (vnodeopv_entry_t) msdosfs_print },
1981 { &vop_read_desc, (vnodeopv_entry_t) msdosfs_read },
1982 { &vop_readdir_desc, (vnodeopv_entry_t) msdosfs_readdir },
1983 { &vop_reclaim_desc, (vnodeopv_entry_t) msdosfs_reclaim },
e62afb5f
MD
1984 { &vop_old_remove_desc, (vnodeopv_entry_t) msdosfs_remove },
1985 { &vop_old_rename_desc, (vnodeopv_entry_t) msdosfs_rename },
1986 { &vop_old_rmdir_desc, (vnodeopv_entry_t) msdosfs_rmdir },
625ddaba
JS
1987 { &vop_setattr_desc, (vnodeopv_entry_t) msdosfs_setattr },
1988 { &vop_strategy_desc, (vnodeopv_entry_t) msdosfs_strategy },
e62afb5f 1989 { &vop_old_symlink_desc, (vnodeopv_entry_t) msdosfs_symlink },
625ddaba
JS
1990 { &vop_unlock_desc, (vnodeopv_entry_t) vop_stdunlock },
1991 { &vop_write_desc, (vnodeopv_entry_t) msdosfs_write },
1992 { &vop_getpages_desc, (vnodeopv_entry_t) msdosfs_getpages },
1993 { &vop_putpages_desc, (vnodeopv_entry_t) msdosfs_putpages },
984263bc
MD
1994 { NULL, NULL }
1995};