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