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