1 /* $NetBSD: msdosfs_vnops.c,v 1.68 1998/02/10 14:10:04 mrg Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
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>
54 #include <sys/bio.h>
55 #include <sys/buf.h>
56 #include <sys/clock.h>
57 #include <sys/dirent.h>
58 #include <sys/lock.h>
59 #include <sys/lockf.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/mutex.h>
63 #include <sys/namei.h>
64 #include <sys/priv.h>
65 #include <sys/stat.h>
66 #include <sys/sysctl.h>
67 #include <sys/unistd.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
70
71 #include <vm/vm.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vnode_pager.h>
74
75 #include <fs/msdosfs/bpb.h>
76 #include <fs/msdosfs/direntry.h>
77 #include <fs/msdosfs/denode.h>
78 #include <fs/msdosfs/fat.h>
79 #include <fs/msdosfs/msdosfsmount.h>
80
81 /*
82 * Prototypes for MSDOSFS vnode operations
83 */
84 static vop_create_t msdosfs_create;
85 static vop_mknod_t msdosfs_mknod;
86 static vop_open_t msdosfs_open;
87 static vop_close_t msdosfs_close;
88 static vop_access_t msdosfs_access;
89 static vop_getattr_t msdosfs_getattr;
90 static vop_setattr_t msdosfs_setattr;
91 static vop_read_t msdosfs_read;
92 static vop_write_t msdosfs_write;
93 static vop_fsync_t msdosfs_fsync;
94 static vop_remove_t msdosfs_remove;
95 static vop_link_t msdosfs_link;
96 static vop_rename_t msdosfs_rename;
97 static vop_mkdir_t msdosfs_mkdir;
98 static vop_rmdir_t msdosfs_rmdir;
99 static vop_symlink_t msdosfs_symlink;
100 static vop_readdir_t msdosfs_readdir;
101 static vop_bmap_t msdosfs_bmap;
102 static vop_getpages_t msdosfs_getpages;
103 static vop_strategy_t msdosfs_strategy;
104 static vop_print_t msdosfs_print;
105 static vop_pathconf_t msdosfs_pathconf;
106 static vop_vptofh_t msdosfs_vptofh;
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 * retrieve 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
128 * created is locked. We must release before we return. We must also free
129 * the pathname buffer pointed at by cnp->cn_pnbuf, always on error.
130 */
131 static int
msdosfs_create(struct vop_create_args * ap)132 msdosfs_create(struct vop_create_args *ap)
133 {
134 struct componentname *cnp = ap->a_cnp;
135 struct denode ndirent;
136 struct denode *dep;
137 struct denode *pdep = VTODE(ap->a_dvp);
138 struct timespec ts;
139 int error;
140
141 #ifdef MSDOSFS_DEBUG
142 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
143 #endif
144
145 /*
146 * If this is the root directory and there is no space left we
147 * can't do anything. This is because the root directory can not
148 * change size.
149 */
150 if (pdep->de_StartCluster == MSDOSFSROOT
151 && pdep->de_fndoffset >= pdep->de_FileSize) {
152 error = ENOSPC;
153 goto bad;
154 }
155
156 /*
157 * Create a directory entry for the file, then call createde() to
158 * have it installed. NOTE: DOS files are always executable. We
159 * use the absence of the owner write bit to make the file
160 * readonly.
161 */
162 memset(&ndirent, 0, sizeof(ndirent));
163 error = uniqdosname(pdep, cnp, ndirent.de_Name);
164 if (error)
165 goto bad;
166
167 ndirent.de_Attributes = ATTR_ARCHIVE;
168 ndirent.de_LowerCase = 0;
169 ndirent.de_StartCluster = 0;
170 ndirent.de_FileSize = 0;
171 ndirent.de_pmp = pdep->de_pmp;
172 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
173 vfs_timestamp(&ts);
174 DETIMES(&ndirent, &ts, &ts, &ts);
175 error = createde(&ndirent, pdep, &dep, cnp);
176 if (error)
177 goto bad;
178 *ap->a_vpp = DETOV(dep);
179 if ((cnp->cn_flags & MAKEENTRY) != 0)
180 cache_enter(ap->a_dvp, *ap->a_vpp, cnp);
181 return (0);
182
183 bad:
184 return (error);
185 }
186
187 static int
msdosfs_mknod(struct vop_mknod_args * ap)188 msdosfs_mknod(struct vop_mknod_args *ap)
189 {
190
191 return (EINVAL);
192 }
193
194 static int
msdosfs_open(struct vop_open_args * ap)195 msdosfs_open(struct vop_open_args *ap)
196 {
197 struct denode *dep = VTODE(ap->a_vp);
198 vnode_create_vobject(ap->a_vp, dep->de_FileSize, ap->a_td);
199 return 0;
200 }
201
202 static int
msdosfs_close(struct vop_close_args * ap)203 msdosfs_close(struct vop_close_args *ap)
204 {
205 struct vnode *vp = ap->a_vp;
206 struct denode *dep = VTODE(vp);
207 struct timespec ts;
208
209 VI_LOCK(vp);
210 if (vp->v_usecount > 1) {
211 vfs_timestamp(&ts);
212 DETIMES(dep, &ts, &ts, &ts);
213 }
214 VI_UNLOCK(vp);
215 return 0;
216 }
217
218 static int
msdosfs_access(struct vop_access_args * ap)219 msdosfs_access(struct vop_access_args *ap)
220 {
221 struct vnode *vp = ap->a_vp;
222 struct denode *dep = VTODE(ap->a_vp);
223 struct msdosfsmount *pmp = dep->de_pmp;
224 mode_t file_mode;
225 accmode_t accmode = ap->a_accmode;
226
227 file_mode = S_IRWXU|S_IRWXG|S_IRWXO;
228 file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
229
230 /*
231 * Disallow writing to directories and regular files if the
232 * filesystem is read-only.
233 */
234 if (accmode & VWRITE) {
235 switch (vp->v_type) {
236 case VREG:
237 case VDIR:
238 if (vp->v_mount->mnt_flag & MNT_RDONLY)
239 return (EROFS);
240 break;
241 default:
242 break;
243 }
244 }
245
246 return (vaccess(vp->v_type, file_mode, pmp->pm_uid, pmp->pm_gid,
247 ap->a_accmode, ap->a_cred));
248 }
249
250 static int
msdosfs_getattr(struct vop_getattr_args * ap)251 msdosfs_getattr(struct vop_getattr_args *ap)
252 {
253 struct denode *dep = VTODE(ap->a_vp);
254 struct msdosfsmount *pmp = dep->de_pmp;
255 struct vattr *vap = ap->a_vap;
256 mode_t mode;
257 struct timespec ts;
258 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
259 uint64_t fileid;
260
261 vfs_timestamp(&ts);
262 DETIMES(dep, &ts, &ts, &ts);
263 vap->va_fsid = dev2udev(pmp->pm_dev);
264 /*
265 * The following computation of the fileid must be the same as that
266 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
267 * doesn't work.
268 */
269 if (dep->de_Attributes & ATTR_DIRECTORY) {
270 fileid = (uint64_t)cntobn(pmp, dep->de_StartCluster) *
271 dirsperblk;
272 if (dep->de_StartCluster == MSDOSFSROOT)
273 fileid = 1;
274 } else {
275 fileid = (uint64_t)cntobn(pmp, dep->de_dirclust) *
276 dirsperblk;
277 if (dep->de_dirclust == MSDOSFSROOT)
278 fileid = (uint64_t)roottobn(pmp, 0) * dirsperblk;
279 fileid += (uoff_t)dep->de_diroffset / sizeof(struct direntry);
280 }
281 vap->va_fileid = fileid;
282
283 mode = S_IRWXU|S_IRWXG|S_IRWXO;
284 if (dep->de_Attributes & ATTR_READONLY)
285 mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
286 vap->va_mode = mode &
287 (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
288 vap->va_uid = pmp->pm_uid;
289 vap->va_gid = pmp->pm_gid;
290 vap->va_nlink = 1;
291 vap->va_rdev = NODEV;
292 vap->va_size = dep->de_FileSize;
293 fattime2timespec(dep->de_MDate, dep->de_MTime, 0, 0, &vap->va_mtime);
294 vap->va_ctime = vap->va_mtime;
295 if (pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
296 fattime2timespec(dep->de_ADate, 0, 0, 0, &vap->va_atime);
297 fattime2timespec(dep->de_CDate, dep->de_CTime, dep->de_CHun,
298 0, &vap->va_birthtime);
299 } else {
300 vap->va_atime = vap->va_mtime;
301 vap->va_birthtime.tv_sec = -1;
302 vap->va_birthtime.tv_nsec = 0;
303 }
304 vap->va_flags = 0;
305 if (dep->de_Attributes & ATTR_ARCHIVE)
306 vap->va_flags |= UF_ARCHIVE;
307 if (dep->de_Attributes & ATTR_HIDDEN)
308 vap->va_flags |= UF_HIDDEN;
309 if (dep->de_Attributes & ATTR_READONLY)
310 vap->va_flags |= UF_READONLY;
311 if (dep->de_Attributes & ATTR_SYSTEM)
312 vap->va_flags |= UF_SYSTEM;
313 vap->va_gen = 0;
314 vap->va_blocksize = pmp->pm_bpcluster;
315 if (dep->de_StartCluster != MSDOSFSROOT)
316 vap->va_bytes =
317 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
318 else
319 vap->va_bytes = 0; /* FAT12/FAT16 root dir in reserved area */
320 vap->va_type = ap->a_vp->v_type;
321 vap->va_filerev = dep->de_modrev;
322 return (0);
323 }
324
325 static int
msdosfs_setattr(struct vop_setattr_args * ap)326 msdosfs_setattr(struct vop_setattr_args *ap)
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 struct thread *td = curthread;
334 int error = 0;
335
336 #ifdef MSDOSFS_DEBUG
337 printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
338 ap->a_vp, vap, cred);
339 #endif
340
341 /*
342 * Check for unsettable attributes.
343 */
344 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||
345 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
346 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
347 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {
348 #ifdef MSDOSFS_DEBUG
349 printf("msdosfs_setattr(): returning EINVAL\n");
350 printf(" va_type %d, va_nlink %llx, va_fsid %llx, va_fileid %llx\n",
351 vap->va_type, (unsigned long long)vap->va_nlink,
352 (unsigned long long)vap->va_fsid,
353 (unsigned long long)vap->va_fileid);
354 printf(" va_blocksize %lx, va_rdev %llx, va_bytes %llx, va_gen %lx\n",
355 vap->va_blocksize, (unsigned long long)vap->va_rdev,
356 (unsigned long long)vap->va_bytes, vap->va_gen);
357 printf(" va_uid %x, va_gid %x\n",
358 vap->va_uid, vap->va_gid);
359 #endif
360 return (EINVAL);
361 }
362
363 /*
364 * We don't allow setting attributes on the root directory.
365 * The special case for the root directory is because before
366 * FAT32, the root directory didn't have an entry for itself
367 * (and was otherwise special). With FAT32, the root
368 * directory is not so special, but still doesn't have an
369 * entry for itself.
370 */
371 if (vp->v_vflag & VV_ROOT)
372 return (EINVAL);
373
374 if (vap->va_flags != VNOVAL) {
375 if (vp->v_mount->mnt_flag & MNT_RDONLY)
376 return (EROFS);
377 if (cred->cr_uid != pmp->pm_uid) {
378 error = priv_check_cred(cred, PRIV_VFS_ADMIN);
379 if (error)
380 return (error);
381 }
382 /*
383 * We are very inconsistent about handling unsupported
384 * attributes. We ignored the access time and the
385 * read and execute bits. We were strict for the other
386 * attributes.
387 */
388 if (vap->va_flags & ~(UF_ARCHIVE | UF_HIDDEN | UF_READONLY |
389 UF_SYSTEM))
390 return EOPNOTSUPP;
391 if (vap->va_flags & UF_ARCHIVE)
392 dep->de_Attributes |= ATTR_ARCHIVE;
393 else
394 dep->de_Attributes &= ~ATTR_ARCHIVE;
395 if (vap->va_flags & UF_HIDDEN)
396 dep->de_Attributes |= ATTR_HIDDEN;
397 else
398 dep->de_Attributes &= ~ATTR_HIDDEN;
399 /* We don't allow changing the readonly bit on directories. */
400 if (vp->v_type != VDIR) {
401 if (vap->va_flags & UF_READONLY)
402 dep->de_Attributes |= ATTR_READONLY;
403 else
404 dep->de_Attributes &= ~ATTR_READONLY;
405 }
406 if (vap->va_flags & UF_SYSTEM)
407 dep->de_Attributes |= ATTR_SYSTEM;
408 else
409 dep->de_Attributes &= ~ATTR_SYSTEM;
410 dep->de_flag |= DE_MODIFIED;
411 }
412
413 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
414 uid_t uid;
415 gid_t gid;
416
417 if (vp->v_mount->mnt_flag & MNT_RDONLY)
418 return (EROFS);
419 uid = vap->va_uid;
420 if (uid == (uid_t)VNOVAL)
421 uid = pmp->pm_uid;
422 gid = vap->va_gid;
423 if (gid == (gid_t)VNOVAL)
424 gid = pmp->pm_gid;
425 if (cred->cr_uid != pmp->pm_uid || uid != pmp->pm_uid ||
426 (gid != pmp->pm_gid && !groupmember(gid, cred))) {
427 error = priv_check_cred(cred, PRIV_VFS_CHOWN);
428 if (error)
429 return (error);
430 }
431 if (uid != pmp->pm_uid || gid != pmp->pm_gid)
432 return EINVAL;
433 }
434
435 if (vap->va_size != VNOVAL) {
436 switch (vp->v_type) {
437 case VDIR:
438 return (EISDIR);
439 case VREG:
440 /*
441 * Truncation is only supported for regular files,
442 * Disallow it if the filesystem is read-only.
443 */
444 if (vp->v_mount->mnt_flag & MNT_RDONLY)
445 return (EROFS);
446 break;
447 default:
448 /*
449 * According to POSIX, the result is unspecified
450 * for file types other than regular files,
451 * directories and shared memory objects. We
452 * don't support any file types except regular
453 * files and directories in this file system, so
454 * this (default) case is unreachable and can do
455 * anything. Keep falling through to detrunc()
456 * for now.
457 */
458 break;
459 }
460 error = vn_rlimit_trunc(vap->va_size, td);
461 if (error != 0)
462 return (error);
463 error = detrunc(dep, vap->va_size, 0, cred);
464 if (error)
465 return error;
466 }
467 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
468 if (vp->v_mount->mnt_flag & MNT_RDONLY)
469 return (EROFS);
470 error = vn_utimes_perm(vp, vap, cred, td);
471 if (error != 0)
472 return (error);
473 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
474 vap->va_atime.tv_sec != VNOVAL) {
475 dep->de_flag &= ~DE_ACCESS;
476 timespec2fattime(&vap->va_atime, 0,
477 &dep->de_ADate, NULL, NULL);
478 }
479 if (vap->va_mtime.tv_sec != VNOVAL) {
480 dep->de_flag &= ~DE_UPDATE;
481 timespec2fattime(&vap->va_mtime, 0,
482 &dep->de_MDate, &dep->de_MTime, NULL);
483 }
484 /*
485 * We don't set the archive bit when modifying the time of
486 * a directory to emulate the Windows/DOS behavior.
487 */
488 if (vp->v_type != VDIR)
489 dep->de_Attributes |= ATTR_ARCHIVE;
490 dep->de_flag |= DE_MODIFIED;
491 }
492 /*
493 * DOS files only have the ability to have their writability
494 * attribute set, so we use the owner write bit to set the readonly
495 * attribute.
496 */
497 if (vap->va_mode != (mode_t)VNOVAL) {
498 if (vp->v_mount->mnt_flag & MNT_RDONLY)
499 return (EROFS);
500 if (cred->cr_uid != pmp->pm_uid) {
501 error = priv_check_cred(cred, PRIV_VFS_ADMIN);
502 if (error)
503 return (error);
504 }
505 if (vp->v_type != VDIR) {
506 /* We ignore the read and execute bits. */
507 if (vap->va_mode & S_IWUSR)
508 dep->de_Attributes &= ~ATTR_READONLY;
509 else
510 dep->de_Attributes |= ATTR_READONLY;
511 dep->de_Attributes |= ATTR_ARCHIVE;
512 dep->de_flag |= DE_MODIFIED;
513 }
514 }
515 return (deupdat(dep, 0));
516 }
517
518 static int
msdosfs_read(struct vop_read_args * ap)519 msdosfs_read(struct vop_read_args *ap)
520 {
521 int error = 0;
522 int blsize;
523 int isadir;
524 ssize_t orig_resid;
525 u_int n;
526 u_long diff;
527 u_long on;
528 daddr_t lbn;
529 daddr_t rablock;
530 int rasize;
531 int seqcount;
532 struct buf *bp;
533 struct vnode *vp = ap->a_vp;
534 struct denode *dep = VTODE(vp);
535 struct msdosfsmount *pmp = dep->de_pmp;
536 struct uio *uio = ap->a_uio;
537
538 /*
539 * If they didn't ask for any data, then we are done.
540 */
541 orig_resid = uio->uio_resid;
542 if (orig_resid == 0)
543 return (0);
544
545 /*
546 * The caller is supposed to ensure that
547 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
548 * We don't need to check for large offsets as in ffs because
549 * dep->de_FileSize <= MSDOSFS_FILESIZE_MAX < OFF_MAX, so large
550 * offsets cannot cause overflow even in theory.
551 */
552
553 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
554
555 isadir = dep->de_Attributes & ATTR_DIRECTORY;
556 do {
557 if (uio->uio_offset >= dep->de_FileSize)
558 break;
559 lbn = de_cluster(pmp, uio->uio_offset);
560 rablock = lbn + 1;
561 blsize = pmp->pm_bpcluster;
562 on = uio->uio_offset & pmp->pm_crbomask;
563 /*
564 * If we are operating on a directory file then be sure to
565 * do i/o with the vnode for the filesystem instead of the
566 * vnode for the directory.
567 */
568 if (isadir) {
569 /* convert cluster # to block # */
570 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
571 if (error == E2BIG) {
572 error = EINVAL;
573 break;
574 } else if (error)
575 break;
576 error = bread(pmp->pm_devvp, lbn, blsize, NOCRED, &bp);
577 } else if (de_cn2off(pmp, rablock) >= dep->de_FileSize) {
578 error = bread(vp, lbn, blsize, NOCRED, &bp);
579 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
580 error = cluster_read(vp, dep->de_FileSize, lbn, blsize,
581 NOCRED, on + uio->uio_resid, seqcount, 0, &bp);
582 } else if (seqcount > 1) {
583 rasize = blsize;
584 error = breadn(vp, lbn,
585 blsize, &rablock, &rasize, 1, NOCRED, &bp);
586 } else {
587 error = bread(vp, lbn, blsize, NOCRED, &bp);
588 }
589 if (error) {
590 brelse(bp);
591 break;
592 }
593 diff = pmp->pm_bpcluster - on;
594 n = diff > uio->uio_resid ? uio->uio_resid : diff;
595 diff = dep->de_FileSize - uio->uio_offset;
596 if (diff < n)
597 n = diff;
598 diff = blsize - bp->b_resid;
599 if (diff < n)
600 n = diff;
601 error = vn_io_fault_uiomove(bp->b_data + on, (int) n, uio);
602 brelse(bp);
603 } while (error == 0 && uio->uio_resid > 0 && n != 0);
604 if (!isadir && (error == 0 || uio->uio_resid != orig_resid) &&
605 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
606 dep->de_flag |= DE_ACCESS;
607 return (error);
608 }
609
610 /*
611 * Write data to a file or directory.
612 */
613 static int
msdosfs_write(struct vop_write_args * ap)614 msdosfs_write(struct vop_write_args *ap)
615 {
616 int n;
617 int croffset;
618 ssize_t resid, r;
619 u_long osize;
620 int error = 0;
621 u_long count;
622 int seqcount;
623 daddr_t bn, lastcn;
624 struct buf *bp;
625 int ioflag = ap->a_ioflag;
626 struct uio *uio = ap->a_uio;
627 struct vnode *vp = ap->a_vp;
628 struct vnode *thisvp;
629 struct denode *dep = VTODE(vp);
630 struct msdosfsmount *pmp = dep->de_pmp;
631 struct ucred *cred = ap->a_cred;
632
633 #ifdef MSDOSFS_DEBUG
634 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
635 vp, uio, ioflag, cred);
636 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
637 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
638 #endif
639
640 switch (vp->v_type) {
641 case VREG:
642 if (ioflag & IO_APPEND)
643 uio->uio_offset = dep->de_FileSize;
644 thisvp = vp;
645 break;
646 case VDIR:
647 return EISDIR;
648 default:
649 panic("msdosfs_write(): bad file type");
650 }
651
652 /*
653 * This is needed (unlike in ffs_write()) because we extend the
654 * file outside of the loop but we don't want to extend the file
655 * for writes of 0 bytes.
656 */
657 if (uio->uio_resid == 0)
658 return (0);
659
660 /*
661 * The caller is supposed to ensure that
662 * uio->uio_offset >= 0 and uio->uio_resid >= 0.
663 *
664 * If they've exceeded their filesize limit, tell them about it.
665 */
666 error = vn_rlimit_fsizex(vp, uio, MSDOSFS_FILESIZE_MAX, &r,
667 uio->uio_td);
668 if (error != 0) {
669 vn_rlimit_fsizex_res(uio, r);
670 return (error);
671 }
672
673 /*
674 * If the offset we are starting the write at is beyond the end of
675 * the file, then they've done a seek. Unix filesystems allow
676 * files with holes in them, DOS doesn't so we must fill the hole
677 * with zeroed blocks.
678 */
679 if (uio->uio_offset > dep->de_FileSize) {
680 error = deextend(dep, uio->uio_offset, cred);
681 if (error != 0) {
682 vn_rlimit_fsizex_res(uio, r);
683 return (error);
684 }
685 }
686
687 /*
688 * Remember some values in case the write fails.
689 */
690 resid = uio->uio_resid;
691 osize = dep->de_FileSize;
692
693 /*
694 * If we write beyond the end of the file, extend it to its ultimate
695 * size ahead of the time to hopefully get a contiguous area.
696 */
697 if (uio->uio_offset + resid > osize) {
698 count = de_clcount(pmp, uio->uio_offset + resid) -
699 de_clcount(pmp, osize);
700 error = extendfile(dep, count, NULL, NULL, 0);
701 if (error && (error != ENOSPC || (ioflag & IO_UNIT)))
702 goto errexit;
703 lastcn = dep->de_fc[FC_LASTFC].fc_frcn;
704 } else
705 lastcn = de_clcount(pmp, osize) - 1;
706
707 seqcount = ioflag >> IO_SEQSHIFT;
708 do {
709 if (de_cluster(pmp, uio->uio_offset) > lastcn) {
710 error = ENOSPC;
711 break;
712 }
713
714 croffset = uio->uio_offset & pmp->pm_crbomask;
715 n = min(uio->uio_resid, pmp->pm_bpcluster - croffset);
716 if (uio->uio_offset + n > dep->de_FileSize) {
717 dep->de_FileSize = uio->uio_offset + n;
718 /* The object size needs to be set before buffer is allocated */
719 vnode_pager_setsize(vp, dep->de_FileSize);
720 }
721
722 bn = de_cluster(pmp, uio->uio_offset);
723 if ((uio->uio_offset & pmp->pm_crbomask) == 0
724 && (de_cluster(pmp, uio->uio_offset + uio->uio_resid)
725 > de_cluster(pmp, uio->uio_offset)
726 || uio->uio_offset + uio->uio_resid >= dep->de_FileSize)) {
727 /*
728 * If either the whole cluster gets written,
729 * or we write the cluster from its start beyond EOF,
730 * then no need to read data from disk.
731 */
732 bp = getblk(thisvp, bn, pmp->pm_bpcluster, 0, 0, 0);
733 /*
734 * This call to vfs_bio_clrbuf() ensures that
735 * even if vn_io_fault_uiomove() below faults,
736 * garbage from the newly instantiated buffer
737 * is not exposed to the userspace via mmap().
738 */
739 vfs_bio_clrbuf(bp);
740 /*
741 * Do the bmap now, since pcbmap needs buffers
742 * for the FAT table. (see msdosfs_strategy)
743 */
744 if (bp->b_blkno == bp->b_lblkno) {
745 error = pcbmap(dep, bp->b_lblkno, &bn, 0, 0);
746 if (error)
747 bp->b_blkno = -1;
748 else
749 bp->b_blkno = bn;
750 }
751 if (bp->b_blkno == -1) {
752 brelse(bp);
753 if (!error)
754 error = EIO; /* XXX */
755 break;
756 }
757 } else {
758 /*
759 * The block we need to write into exists, so read it in.
760 */
761 error = bread(thisvp, bn, pmp->pm_bpcluster, cred, &bp);
762 if (error) {
763 break;
764 }
765 }
766
767 /*
768 * Should these vnode_pager_* functions be done on dir
769 * files?
770 */
771
772 /*
773 * Copy the data from user space into the buf header.
774 */
775 error = vn_io_fault_uiomove(bp->b_data + croffset, n, uio);
776 if (error) {
777 brelse(bp);
778 break;
779 }
780
781 /* Prepare for clustered writes in some else clauses. */
782 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
783 bp->b_flags |= B_CLUSTEROK;
784
785 /*
786 * If IO_SYNC, then each buffer is written synchronously.
787 * Otherwise, if we have a severe page deficiency then
788 * write the buffer asynchronously. Otherwise, if on a
789 * cluster boundary then write the buffer asynchronously,
790 * combining it with contiguous clusters if permitted and
791 * possible, since we don't expect more writes into this
792 * buffer soon. Otherwise, do a delayed write because we
793 * expect more writes into this buffer soon.
794 */
795 if (ioflag & IO_SYNC)
796 (void)bwrite(bp);
797 else if (vm_page_count_severe() || buf_dirty_count_severe())
798 bawrite(bp);
799 else if (n + croffset == pmp->pm_bpcluster) {
800 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0)
801 cluster_write(vp, &dep->de_clusterw, bp,
802 dep->de_FileSize, seqcount, 0);
803 else
804 bawrite(bp);
805 } else
806 bdwrite(bp);
807 dep->de_flag |= DE_UPDATE;
808 } while (error == 0 && uio->uio_resid > 0);
809
810 /*
811 * If the write failed and they want us to, truncate the file back
812 * to the size it was before the write was attempted.
813 */
814 errexit:
815 if (error) {
816 if (ioflag & IO_UNIT) {
817 detrunc(dep, osize, ioflag & IO_SYNC, NOCRED);
818 uio->uio_offset -= resid - uio->uio_resid;
819 uio->uio_resid = resid;
820 } else {
821 detrunc(dep, dep->de_FileSize, ioflag & IO_SYNC, NOCRED);
822 if (uio->uio_resid != resid)
823 error = 0;
824 }
825 } else if (ioflag & IO_SYNC)
826 error = deupdat(dep, 1);
827 vn_rlimit_fsizex_res(uio, r);
828 return (error);
829 }
830
831 /*
832 * Flush the blocks of a file to disk.
833 */
834 static int
msdosfs_fsync(struct vop_fsync_args * ap)835 msdosfs_fsync(struct vop_fsync_args *ap)
836 {
837 struct vnode *devvp;
838 int allerror, error;
839
840 vop_stdfsync(ap);
841
842 /*
843 * If the syncing request comes from fsync(2), sync the entire
844 * FAT and any other metadata that happens to be on devvp. We
845 * need this mainly for the FAT. We write the FAT sloppily, and
846 * syncing it all now is the best we can easily do to get all
847 * directory entries associated with the file (not just the file)
848 * fully synced. The other metadata includes critical metadata
849 * for all directory entries, but only in the MNT_ASYNC case. We
850 * will soon sync all metadata in the file's directory entry.
851 * Non-critical metadata for associated directory entries only
852 * gets synced accidentally, as in most file systems.
853 */
854 if (ap->a_waitfor != MNT_NOWAIT) {
855 devvp = VTODE(ap->a_vp)->de_pmp->pm_devvp;
856 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
857 allerror = VOP_FSYNC(devvp, MNT_WAIT, ap->a_td);
858 VOP_UNLOCK(devvp);
859 } else
860 allerror = 0;
861
862 error = deupdat(VTODE(ap->a_vp), ap->a_waitfor != MNT_NOWAIT);
863 if (allerror == 0)
864 allerror = error;
865 return (allerror);
866 }
867
868 static int
msdosfs_remove(struct vop_remove_args * ap)869 msdosfs_remove(struct vop_remove_args *ap)
870 {
871 struct denode *dep = VTODE(ap->a_vp);
872 struct denode *ddep = VTODE(ap->a_dvp);
873 int error;
874
875 if (ap->a_vp->v_type == VDIR)
876 error = EPERM;
877 else
878 error = removede(ddep, dep);
879 #ifdef MSDOSFS_DEBUG
880 printf("msdosfs_remove(), dep %p, v_usecount %d\n", dep, ap->a_vp->v_usecount);
881 #endif
882 return (error);
883 }
884
885 /*
886 * DOS filesystems don't know what links are.
887 */
888 static int
msdosfs_link(struct vop_link_args * ap)889 msdosfs_link(struct vop_link_args *ap)
890 {
891 return (EOPNOTSUPP);
892 }
893
894 /*
895 * Renames on files require moving the denode to a new hash queue since the
896 * denode's location is used to compute which hash queue to put the file
897 * in. Unless it is a rename in place. For example "mv a b".
898 *
899 * What follows is the basic algorithm:
900 *
901 * if (file move) {
902 * if (dest file exists) {
903 * remove dest file
904 * }
905 * if (dest and src in same directory) {
906 * rewrite name in existing directory slot
907 * } else {
908 * write new entry in dest directory
909 * update offset and dirclust in denode
910 * move denode to new hash chain
911 * clear old directory entry
912 * }
913 * } else {
914 * directory move
915 * if (dest directory exists) {
916 * if (dest is not empty) {
917 * return ENOTEMPTY
918 * }
919 * remove dest directory
920 * }
921 * if (dest and src in same directory) {
922 * rewrite name in existing entry
923 * } else {
924 * be sure dest is not a child of src directory
925 * write entry in dest directory
926 * update "." and ".." in moved directory
927 * clear old directory entry for moved directory
928 * }
929 * }
930 *
931 * On entry:
932 * source's parent directory is unlocked
933 * source file or directory is unlocked
934 * destination's parent directory is locked
935 * destination file or directory is locked if it exists
936 *
937 * On exit:
938 * all denodes should be released
939 */
940 static int
msdosfs_rename(struct vop_rename_args * ap)941 msdosfs_rename(struct vop_rename_args *ap)
942 {
943 struct vnode *fdvp, *fvp, *tdvp, *tvp, *vp;
944 struct componentname *fcnp, *tcnp;
945 struct denode *fdip, *fip, *tdip, *tip, *nip;
946 u_char toname[12], oldname[11];
947 u_long to_diroffset;
948 bool doingdirectory, newparent;
949 int error;
950 u_long cn, pcl, blkoff;
951 daddr_t bn, wait_scn, scn;
952 struct msdosfsmount *pmp;
953 struct direntry *dotdotp;
954 struct buf *bp;
955
956 tdvp = ap->a_tdvp;
957 fvp = ap->a_fvp;
958 fdvp = ap->a_fdvp;
959 tvp = ap->a_tvp;
960 tcnp = ap->a_tcnp;
961 fcnp = ap->a_fcnp;
962 pmp = VFSTOMSDOSFS(fdvp->v_mount);
963
964 /*
965 * Check for cross-device rename.
966 */
967 if (fvp->v_mount != tdvp->v_mount ||
968 (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
969 error = EXDEV;
970 goto abortit;
971 }
972
973 /*
974 * If source and dest are the same, do nothing.
975 */
976 if (tvp == fvp) {
977 error = 0;
978 goto abortit;
979 }
980
981 /*
982 * When the target exists, both the directory
983 * and target vnodes are passed locked.
984 */
985 VOP_UNLOCK(tdvp);
986 if (tvp != NULL && tvp != tdvp)
987 VOP_UNLOCK(tvp);
988
989 relock:
990 doingdirectory = newparent = false;
991
992 error = vn_lock(fdvp, LK_EXCLUSIVE);
993 if (error != 0)
994 goto releout;
995 if (vn_lock(tdvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
996 VOP_UNLOCK(fdvp);
997 error = vn_lock(tdvp, LK_EXCLUSIVE);
998 if (error != 0)
999 goto releout;
1000 VOP_UNLOCK(tdvp);
1001 goto relock;
1002 }
1003
1004 error = msdosfs_lookup_ino(fdvp, NULL, fcnp, &scn, &blkoff);
1005 if (error != 0) {
1006 VOP_UNLOCK(fdvp);
1007 VOP_UNLOCK(tdvp);
1008 goto releout;
1009 }
1010 error = deget(pmp, scn, blkoff, LK_EXCLUSIVE | LK_NOWAIT, &nip);
1011 if (error != 0) {
1012 VOP_UNLOCK(fdvp);
1013 VOP_UNLOCK(tdvp);
1014 if (error != EBUSY)
1015 goto releout;
1016 error = deget(pmp, scn, blkoff, LK_EXCLUSIVE, &nip);
1017 if (error != 0)
1018 goto releout;
1019 vp = fvp;
1020 fvp = DETOV(nip);
1021 VOP_UNLOCK(fvp);
1022 vrele(vp);
1023 goto relock;
1024 }
1025 vrele(fvp);
1026 fvp = DETOV(nip);
1027
1028 error = msdosfs_lookup_ino(tdvp, NULL, tcnp, &scn, &blkoff);
1029 if (error != 0 && error != EJUSTRETURN) {
1030 VOP_UNLOCK(fdvp);
1031 VOP_UNLOCK(tdvp);
1032 VOP_UNLOCK(fvp);
1033 goto releout;
1034 }
1035 if (error == EJUSTRETURN && tvp != NULL) {
1036 vrele(tvp);
1037 tvp = NULL;
1038 }
1039 if (error == 0) {
1040 nip = NULL;
1041 error = deget(pmp, scn, blkoff, LK_EXCLUSIVE | LK_NOWAIT,
1042 &nip);
1043 if (tvp != NULL) {
1044 vrele(tvp);
1045 tvp = NULL;
1046 }
1047 if (error != 0) {
1048 VOP_UNLOCK(fdvp);
1049 VOP_UNLOCK(tdvp);
1050 VOP_UNLOCK(fvp);
1051 if (error != EBUSY)
1052 goto releout;
1053 error = deget(pmp, scn, blkoff, LK_EXCLUSIVE,
1054 &nip);
1055 if (error != 0)
1056 goto releout;
1057 vput(DETOV(nip));
1058 goto relock;
1059 }
1060 tvp = DETOV(nip);
1061 }
1062
1063 fdip = VTODE(fdvp);
1064 fip = VTODE(fvp);
1065 tdip = VTODE(tdvp);
1066 tip = tvp != NULL ? VTODE(tvp) : NULL;
1067
1068 /*
1069 * Remember direntry place to use for destination
1070 */
1071 to_diroffset = tdip->de_fndoffset;
1072
1073 /*
1074 * Be sure we are not renaming ".", "..", or an alias of ".". This
1075 * leads to a crippled directory tree. It's pretty tough to do a
1076 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
1077 * doesn't work if the ".." entry is missing.
1078 */
1079 if ((fip->de_Attributes & ATTR_DIRECTORY) != 0) {
1080 /*
1081 * Avoid ".", "..", and aliases of "." for obvious reasons.
1082 */
1083 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1084 fdip == fip ||
1085 (fcnp->cn_flags & ISDOTDOT) != 0 ||
1086 (tcnp->cn_flags & ISDOTDOT) != 0) {
1087 error = EINVAL;
1088 goto unlock;
1089 }
1090 doingdirectory = true;
1091 }
1092
1093 /*
1094 * If ".." must be changed (ie the directory gets a new
1095 * parent) then the source directory must not be in the
1096 * directory hierarchy above the target, as this would
1097 * orphan everything below the source directory. Also
1098 * the user must have write permission in the source so
1099 * as to be able to change "..". We must repeat the call
1100 * to namei, as the parent directory is unlocked by the
1101 * call to doscheckpath().
1102 */
1103 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, curthread);
1104 if (fdip->de_StartCluster != tdip->de_StartCluster)
1105 newparent = true;
1106 if (doingdirectory && newparent) {
1107 if (error != 0) /* write access check above */
1108 goto unlock;
1109 error = doscheckpath(fip, tdip, &wait_scn);
1110 if (wait_scn != 0) {
1111 VOP_UNLOCK(fdvp);
1112 VOP_UNLOCK(tdvp);
1113 VOP_UNLOCK(fvp);
1114 if (tvp != NULL && tvp != tdvp)
1115 VOP_UNLOCK(tvp);
1116 error = deget(pmp, wait_scn, 0, LK_EXCLUSIVE,
1117 &nip);
1118 if (error == 0) {
1119 vput(DETOV(nip));
1120 goto relock;
1121 }
1122 }
1123 if (error != 0)
1124 goto unlock;
1125 }
1126
1127 if (tip != NULL) {
1128 /*
1129 * Target must be empty if a directory and have no links
1130 * to it. Also, ensure source and target are compatible
1131 * (both directories, or both not directories).
1132 */
1133 if ((tip->de_Attributes & ATTR_DIRECTORY) != 0) {
1134 if (!dosdirempty(tip)) {
1135 error = ENOTEMPTY;
1136 goto unlock;
1137 }
1138 if (!doingdirectory) {
1139 error = ENOTDIR;
1140 goto unlock;
1141 }
1142 cache_purge(tdvp);
1143 } else if (doingdirectory) {
1144 error = EISDIR;
1145 goto unlock;
1146 }
1147 error = msdosfs_lookup_ino(tdvp, NULL, tcnp, &scn, &blkoff);
1148 MPASS(error == 0);
1149 error = removede(tdip, tip);
1150 if (error != 0)
1151 goto unlock;
1152 vput(tvp);
1153 tvp = NULL;
1154 tip = NULL;
1155 }
1156
1157 /*
1158 * Convert the filename in tcnp into a dos filename. We copy this
1159 * into the denode and directory entry for the destination
1160 * file/directory.
1161 */
1162 error = uniqdosname(tdip, tcnp, toname);
1163 if (error != 0)
1164 goto unlock;
1165
1166 /*
1167 * First write a new entry in the destination
1168 * directory and mark the entry in the source directory
1169 * as deleted. Then move the denode to the correct hash
1170 * chain for its new location in the filesystem. And, if
1171 * we moved a directory, then update its .. entry to point
1172 * to the new parent directory.
1173 */
1174 memcpy(oldname, fip->de_Name, 11);
1175 memcpy(fip->de_Name, toname, 11); /* update denode */
1176 error = msdosfs_lookup_ino(tdvp, NULL, tcnp, &scn, &blkoff);
1177 if (error == EJUSTRETURN) {
1178 tdip->de_fndoffset = to_diroffset;
1179 error = createde(fip, tdip, NULL, tcnp);
1180 }
1181 if (error != 0) {
1182 memcpy(fip->de_Name, oldname, 11);
1183 goto unlock;
1184 }
1185
1186 /*
1187 * If fip is for a directory, then its name should always
1188 * be "." since it is for the directory entry in the
1189 * directory itself (msdosfs_lookup() always translates
1190 * to the "." entry so as to get a unique denode, except
1191 * for the root directory there are different
1192 * complications). However, we just corrupted its name
1193 * to pass the correct name to createde(). Undo this.
1194 */
1195 if ((fip->de_Attributes & ATTR_DIRECTORY) != 0)
1196 memcpy(fip->de_Name, oldname, 11);
1197 fip->de_refcnt++;
1198 error = msdosfs_lookup_ino(fdvp, NULL, fcnp, &scn, &blkoff);
1199 MPASS(error == 0);
1200 error = removede(fdip, fip);
1201 if (error != 0) {
1202 printf("%s: removede %s %s err %d\n",
1203 pmp->pm_mountp->mnt_stat.f_mntonname,
1204 fdip->de_Name, fip->de_Name, error);
1205 msdosfs_integrity_error(pmp);
1206 goto unlock;
1207 }
1208 if (!doingdirectory) {
1209 error = pcbmap(tdip, de_cluster(pmp, to_diroffset), 0,
1210 &fip->de_dirclust, 0);
1211 if (error != 0) {
1212 /*
1213 * XXX should downgrade to ro here,
1214 * fs is corrupt
1215 */
1216 goto unlock;
1217 }
1218 if (fip->de_dirclust == MSDOSFSROOT)
1219 fip->de_diroffset = to_diroffset;
1220 else
1221 fip->de_diroffset = to_diroffset & pmp->pm_crbomask;
1222 }
1223 reinsert(fip);
1224
1225 /*
1226 * If we moved a directory to a new parent directory, then we must
1227 * fixup the ".." entry in the moved directory.
1228 */
1229 if (doingdirectory && newparent) {
1230 cn = fip->de_StartCluster;
1231 if (cn == MSDOSFSROOT) {
1232 /* this should never happen */
1233 panic("msdosfs_rename(): updating .. in root directory?");
1234 } else
1235 bn = cntobn(pmp, cn);
1236 error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
1237 NOCRED, &bp);
1238 if (error != 0) {
1239 printf("%s: block read error %d while renaming dir\n",
1240 pmp->pm_mountp->mnt_stat.f_mntonname,
1241 error);
1242 msdosfs_integrity_error(pmp);
1243 goto unlock;
1244 }
1245 dotdotp = (struct direntry *)bp->b_data + 1;
1246 pcl = tdip->de_StartCluster;
1247 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1248 pcl = MSDOSFSROOT;
1249 putushort(dotdotp->deStartCluster, pcl);
1250 if (FAT32(pmp))
1251 putushort(dotdotp->deHighClust, pcl >> 16);
1252 if (DOINGASYNC(fvp))
1253 bdwrite(bp);
1254 else if ((error = bwrite(bp)) != 0) {
1255 printf("%s: block write error %d while renaming dir\n",
1256 pmp->pm_mountp->mnt_stat.f_mntonname,
1257 error);
1258 msdosfs_integrity_error(pmp);
1259 goto unlock;
1260 }
1261 }
1262
1263 /*
1264 * The msdosfs lookup is case insensitive. Several aliases may
1265 * be inserted for a single directory entry. As a consequnce,
1266 * name cache purge done by lookup for fvp when DELETE op for
1267 * namei is specified, might be not enough to expunge all
1268 * namecache entries that were installed for this direntry.
1269 */
1270 cache_purge(fvp);
1271
1272 unlock:
1273 vput(fdvp);
1274 vput(fvp);
1275 if (tvp != NULL) {
1276 if (tvp != tdvp)
1277 vput(tvp);
1278 else
1279 vrele(tvp);
1280 }
1281 vput(tdvp);
1282 return (error);
1283 releout:
1284 vrele(tdvp);
1285 if (tvp != NULL)
1286 vrele(tvp);
1287 vrele(fdvp);
1288 vrele(fvp);
1289 return (error);
1290 abortit:
1291 if (tdvp == tvp)
1292 vrele(tdvp);
1293 else
1294 vput(tdvp);
1295 if (tvp != NULL)
1296 vput(tvp);
1297 vrele(fdvp);
1298 vrele(fvp);
1299 return (error);
1300 }
1301
1302 static struct {
1303 struct direntry dot;
1304 struct direntry dotdot;
1305 } dosdirtemplate = {
1306 { ". ", /* the . entry */
1307 ATTR_DIRECTORY, /* file attribute */
1308 0, /* reserved */
1309 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1310 { 0, 0 }, /* access date */
1311 { 0, 0 }, /* high bits of start cluster */
1312 { 210, 4 }, { 210, 4 }, /* modify time & date */
1313 { 0, 0 }, /* startcluster */
1314 { 0, 0, 0, 0 } /* filesize */
1315 },
1316 { ".. ", /* the .. entry */
1317 ATTR_DIRECTORY, /* file attribute */
1318 0, /* reserved */
1319 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1320 { 0, 0 }, /* access date */
1321 { 0, 0 }, /* high bits of start cluster */
1322 { 210, 4 }, { 210, 4 }, /* modify time & date */
1323 { 0, 0 }, /* startcluster */
1324 { 0, 0, 0, 0 } /* filesize */
1325 }
1326 };
1327
1328 static int
msdosfs_mkdir(struct vop_mkdir_args * ap)1329 msdosfs_mkdir(struct vop_mkdir_args *ap)
1330 {
1331 struct componentname *cnp = ap->a_cnp;
1332 struct denode *dep;
1333 struct denode *pdep = VTODE(ap->a_dvp);
1334 struct direntry *denp;
1335 struct msdosfsmount *pmp = pdep->de_pmp;
1336 struct buf *bp;
1337 u_long newcluster, pcl;
1338 int bn;
1339 int error;
1340 struct denode ndirent;
1341 struct timespec ts;
1342
1343 /*
1344 * If this is the root directory and there is no space left we
1345 * can't do anything. This is because the root directory can not
1346 * change size.
1347 */
1348 if (pdep->de_StartCluster == MSDOSFSROOT
1349 && pdep->de_fndoffset >= pdep->de_FileSize) {
1350 error = ENOSPC;
1351 goto bad2;
1352 }
1353
1354 /*
1355 * Allocate a cluster to hold the about to be created directory.
1356 */
1357 error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
1358 if (error)
1359 goto bad2;
1360
1361 memset(&ndirent, 0, sizeof(ndirent));
1362 ndirent.de_pmp = pmp;
1363 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1364 vfs_timestamp(&ts);
1365 DETIMES(&ndirent, &ts, &ts, &ts);
1366
1367 /*
1368 * Now fill the cluster with the "." and ".." entries. And write
1369 * the cluster to disk. This way it is there for the parent
1370 * directory to be pointing at if there were a crash.
1371 */
1372 bn = cntobn(pmp, newcluster);
1373 /* always succeeds */
1374 bp = getblk(pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
1375 memset(bp->b_data, 0, pmp->pm_bpcluster);
1376 memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
1377 denp = (struct direntry *)bp->b_data;
1378 putushort(denp[0].deStartCluster, newcluster);
1379 putushort(denp[0].deCDate, ndirent.de_CDate);
1380 putushort(denp[0].deCTime, ndirent.de_CTime);
1381 denp[0].deCHundredth = ndirent.de_CHun;
1382 putushort(denp[0].deADate, ndirent.de_ADate);
1383 putushort(denp[0].deMDate, ndirent.de_MDate);
1384 putushort(denp[0].deMTime, ndirent.de_MTime);
1385 pcl = pdep->de_StartCluster;
1386 /*
1387 * Although the root directory has a non-magic starting cluster
1388 * number for FAT32, chkdsk and fsck_msdosfs still require
1389 * references to it in dotdot entries to be magic.
1390 */
1391 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1392 pcl = MSDOSFSROOT;
1393 putushort(denp[1].deStartCluster, pcl);
1394 putushort(denp[1].deCDate, ndirent.de_CDate);
1395 putushort(denp[1].deCTime, ndirent.de_CTime);
1396 denp[1].deCHundredth = ndirent.de_CHun;
1397 putushort(denp[1].deADate, ndirent.de_ADate);
1398 putushort(denp[1].deMDate, ndirent.de_MDate);
1399 putushort(denp[1].deMTime, ndirent.de_MTime);
1400 if (FAT32(pmp)) {
1401 putushort(denp[0].deHighClust, newcluster >> 16);
1402 putushort(denp[1].deHighClust, pcl >> 16);
1403 }
1404
1405 if (DOINGASYNC(ap->a_dvp))
1406 bdwrite(bp);
1407 else if ((error = bwrite(bp)) != 0)
1408 goto bad;
1409
1410 /*
1411 * Now build up a directory entry pointing to the newly allocated
1412 * cluster. This will be written to an empty slot in the parent
1413 * directory.
1414 */
1415 error = uniqdosname(pdep, cnp, ndirent.de_Name);
1416 if (error)
1417 goto bad;
1418
1419 ndirent.de_Attributes = ATTR_DIRECTORY;
1420 ndirent.de_LowerCase = 0;
1421 ndirent.de_StartCluster = newcluster;
1422 ndirent.de_FileSize = 0;
1423 error = createde(&ndirent, pdep, &dep, cnp);
1424 if (error)
1425 goto bad;
1426 *ap->a_vpp = DETOV(dep);
1427 return (0);
1428
1429 bad:
1430 clusterfree(pmp, newcluster);
1431 bad2:
1432 return (error);
1433 }
1434
1435 static int
msdosfs_rmdir(struct vop_rmdir_args * ap)1436 msdosfs_rmdir(struct vop_rmdir_args *ap)
1437 {
1438 struct vnode *vp = ap->a_vp;
1439 struct vnode *dvp = ap->a_dvp;
1440 struct componentname *cnp = ap->a_cnp;
1441 struct denode *ip, *dp;
1442 int error;
1443
1444 ip = VTODE(vp);
1445 dp = VTODE(dvp);
1446
1447 /*
1448 * Verify the directory is empty (and valid).
1449 * (Rmdir ".." won't be valid since
1450 * ".." will contain a reference to
1451 * the current directory and thus be
1452 * non-empty.)
1453 */
1454 error = 0;
1455 if (!dosdirempty(ip)) {
1456 error = ENOTEMPTY;
1457 goto out;
1458 }
1459 /*
1460 * Delete the entry from the directory. For dos filesystems this
1461 * gets rid of the directory entry on disk, the in memory copy
1462 * still exists but the de_refcnt is <= 0. This prevents it from
1463 * being found by deget(). When the vput() on dep is done we give
1464 * up access and eventually msdosfs_reclaim() will be called which
1465 * will remove it from the denode cache.
1466 */
1467 error = removede(dp, ip);
1468 if (error)
1469 goto out;
1470 /*
1471 * This is where we decrement the link count in the parent
1472 * directory. Since dos filesystems don't do this we just purge
1473 * the name cache.
1474 */
1475 cache_purge(dvp);
1476 /*
1477 * Truncate the directory that is being deleted.
1478 */
1479 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
1480 cache_purge(vp);
1481
1482 out:
1483 return (error);
1484 }
1485
1486 /*
1487 * DOS filesystems don't know what symlinks are.
1488 */
1489 static int
msdosfs_symlink(struct vop_symlink_args * ap)1490 msdosfs_symlink(struct vop_symlink_args *ap)
1491 {
1492 return (EOPNOTSUPP);
1493 }
1494
1495 static int
msdosfs_readdir(struct vop_readdir_args * ap)1496 msdosfs_readdir(struct vop_readdir_args *ap)
1497 {
1498 struct mbnambuf nb;
1499 int error = 0;
1500 int diff;
1501 long n;
1502 int blsize;
1503 long on;
1504 u_long cn;
1505 u_long dirsperblk;
1506 long bias = 0;
1507 daddr_t bn, lbn;
1508 struct buf *bp;
1509 struct denode *dep = VTODE(ap->a_vp);
1510 struct msdosfsmount *pmp = dep->de_pmp;
1511 struct direntry *dentp;
1512 struct dirent dirbuf;
1513 struct uio *uio = ap->a_uio;
1514 uint64_t *cookies = NULL;
1515 int ncookies = 0;
1516 off_t offset, off;
1517 int chksum = -1;
1518
1519 #ifdef MSDOSFS_DEBUG
1520 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1521 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1522 #endif
1523
1524 if (ap->a_eofflag != NULL)
1525 *ap->a_eofflag = 0;
1526
1527 /*
1528 * msdosfs_readdir() won't operate properly on regular files since
1529 * it does i/o only with the filesystem vnode, and hence can
1530 * retrieve the wrong block from the buffer cache for a plain file.
1531 * So, fail attempts to readdir() on a plain file.
1532 */
1533 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1534 return (ENOTDIR);
1535
1536 /*
1537 * To be safe, initialize dirbuf
1538 */
1539 memset(dirbuf.d_name, 0, sizeof(dirbuf.d_name));
1540
1541 /*
1542 * If the user buffer is smaller than the size of one dos directory
1543 * entry or the file offset is not a multiple of the size of a
1544 * directory entry, then we fail the read.
1545 */
1546 off = offset = uio->uio_offset;
1547 if (uio->uio_resid < sizeof(struct direntry) ||
1548 (offset & (sizeof(struct direntry) - 1)))
1549 return (EINVAL);
1550
1551 if (ap->a_ncookies) {
1552 ncookies = uio->uio_resid / 16;
1553 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP,
1554 M_WAITOK);
1555 *ap->a_cookies = cookies;
1556 *ap->a_ncookies = ncookies;
1557 }
1558
1559 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1560
1561 /*
1562 * If they are reading from the root directory then, we simulate
1563 * the . and .. entries since these don't exist in the root
1564 * directory. We also set the offset bias to make up for having to
1565 * simulate these entries. By this I mean that at file offset 64 we
1566 * read the first entry in the root directory that lives on disk.
1567 */
1568 if (dep->de_StartCluster == MSDOSFSROOT
1569 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1570 #if 0
1571 printf("msdosfs_readdir(): going after . or .. in root dir, offset %d\n",
1572 offset);
1573 #endif
1574 bias = 2 * sizeof(struct direntry);
1575 if (offset < bias) {
1576 for (n = (int)offset / sizeof(struct direntry);
1577 n < 2; n++) {
1578 dirbuf.d_fileno = FAT32(pmp) ?
1579 (uint64_t)cntobn(pmp, pmp->pm_rootdirblk) *
1580 dirsperblk : 1;
1581 dirbuf.d_type = DT_DIR;
1582 switch (n) {
1583 case 0:
1584 dirbuf.d_namlen = 1;
1585 dirbuf.d_name[0] = '.';
1586 break;
1587 case 1:
1588 dirbuf.d_namlen = 2;
1589 dirbuf.d_name[0] = '.';
1590 dirbuf.d_name[1] = '.';
1591 break;
1592 }
1593 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1594 /* NOTE: d_off is the offset of the *next* entry. */
1595 dirbuf.d_off = offset + sizeof(struct direntry);
1596 dirent_terminate(&dirbuf);
1597 if (uio->uio_resid < dirbuf.d_reclen)
1598 goto out;
1599 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1600 if (error)
1601 goto out;
1602 offset += sizeof(struct direntry);
1603 off = offset;
1604 if (cookies) {
1605 *cookies++ = offset;
1606 if (--ncookies <= 0)
1607 goto out;
1608 }
1609 }
1610 }
1611 }
1612
1613 mbnambuf_init(&nb);
1614 off = offset;
1615 while (uio->uio_resid > 0) {
1616 lbn = de_cluster(pmp, offset - bias);
1617 on = (offset - bias) & pmp->pm_crbomask;
1618 n = min(pmp->pm_bpcluster - on, uio->uio_resid);
1619 diff = dep->de_FileSize - (offset - bias);
1620 if (diff <= 0) {
1621 if (ap->a_eofflag != NULL)
1622 *ap->a_eofflag = 1;
1623 goto out;
1624 }
1625 n = min(n, diff);
1626 error = pcbmap(dep, lbn, &bn, &cn, &blsize);
1627 if (error)
1628 break;
1629 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
1630 if (error) {
1631 return (error);
1632 }
1633 n = min(n, blsize - bp->b_resid);
1634 if (n == 0) {
1635 brelse(bp);
1636 return (EIO);
1637 }
1638
1639 /*
1640 * Convert from dos directory entries to fs-independent
1641 * directory entries.
1642 */
1643 for (dentp = (struct direntry *)(bp->b_data + on);
1644 (char *)dentp < bp->b_data + on + n;
1645 dentp++, offset += sizeof(struct direntry)) {
1646 #if 0
1647 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1648 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1649 #endif
1650 /*
1651 * If this is an unused entry, we can stop.
1652 */
1653 if (dentp->deName[0] == SLOT_EMPTY) {
1654 brelse(bp);
1655 if (ap->a_eofflag != NULL)
1656 *ap->a_eofflag = 1;
1657 goto out;
1658 }
1659 /*
1660 * Skip deleted entries.
1661 */
1662 if (dentp->deName[0] == SLOT_DELETED) {
1663 chksum = -1;
1664 mbnambuf_init(&nb);
1665 continue;
1666 }
1667
1668 /*
1669 * Handle Win95 long directory entries
1670 */
1671 if (dentp->deAttributes == ATTR_WIN95) {
1672 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1673 continue;
1674 chksum = win2unixfn(&nb,
1675 (struct winentry *)dentp, chksum, pmp);
1676 continue;
1677 }
1678
1679 /*
1680 * Skip volume labels
1681 */
1682 if (dentp->deAttributes & ATTR_VOLUME) {
1683 chksum = -1;
1684 mbnambuf_init(&nb);
1685 continue;
1686 }
1687 /*
1688 * This computation of d_fileno must match
1689 * the computation of va_fileid in
1690 * msdosfs_getattr.
1691 */
1692 if (dentp->deAttributes & ATTR_DIRECTORY) {
1693 cn = getushort(dentp->deStartCluster);
1694 if (FAT32(pmp)) {
1695 cn |= getushort(dentp->deHighClust) <<
1696 16;
1697 if (cn == MSDOSFSROOT)
1698 cn = pmp->pm_rootdirblk;
1699 }
1700 if (cn == MSDOSFSROOT && !FAT32(pmp))
1701 dirbuf.d_fileno = 1;
1702 else
1703 dirbuf.d_fileno = cntobn(pmp, cn) *
1704 dirsperblk;
1705 dirbuf.d_type = DT_DIR;
1706 } else {
1707 dirbuf.d_fileno = (uoff_t)offset /
1708 sizeof(struct direntry);
1709 dirbuf.d_type = DT_REG;
1710 }
1711
1712 if (chksum != winChksum(dentp->deName)) {
1713 dirbuf.d_namlen = dos2unixfn(dentp->deName,
1714 (u_char *)dirbuf.d_name,
1715 dentp->deLowerCase |
1716 ((pmp->pm_flags & MSDOSFSMNT_SHORTNAME) ?
1717 (LCASE_BASE | LCASE_EXT) : 0),
1718 pmp);
1719 mbnambuf_init(&nb);
1720 } else
1721 mbnambuf_flush(&nb, &dirbuf);
1722 chksum = -1;
1723 dirbuf.d_reclen = GENERIC_DIRSIZ(&dirbuf);
1724 /* NOTE: d_off is the offset of the *next* entry. */
1725 dirbuf.d_off = offset + sizeof(struct direntry);
1726 dirent_terminate(&dirbuf);
1727 if (uio->uio_resid < dirbuf.d_reclen) {
1728 brelse(bp);
1729 goto out;
1730 }
1731 error = uiomove(&dirbuf, dirbuf.d_reclen, uio);
1732 if (error) {
1733 brelse(bp);
1734 goto out;
1735 }
1736 if (cookies) {
1737 *cookies++ = offset + sizeof(struct direntry);
1738 if (--ncookies <= 0) {
1739 brelse(bp);
1740 goto out;
1741 }
1742 }
1743 off = offset + sizeof(struct direntry);
1744 }
1745 brelse(bp);
1746 }
1747 out:
1748 /* Subtract unused cookies */
1749 if (ap->a_ncookies)
1750 *ap->a_ncookies -= ncookies;
1751
1752 uio->uio_offset = off;
1753
1754 return (error);
1755 }
1756
1757 /*-
1758 * a_vp - pointer to the file's vnode
1759 * a_bn - logical block number within the file (cluster number for us)
1760 * a_bop - where to return the bufobj of the special file containing the fs
1761 * a_bnp - where to return the "physical" block number corresponding to a_bn
1762 * (relative to the special file; units are blocks of size DEV_BSIZE)
1763 * a_runp - where to return the "run past" a_bn. This is the count of logical
1764 * blocks whose physical blocks (together with a_bn's physical block)
1765 * are contiguous.
1766 * a_runb - where to return the "run before" a_bn.
1767 */
1768 static int
msdosfs_bmap(struct vop_bmap_args * ap)1769 msdosfs_bmap(struct vop_bmap_args *ap)
1770 {
1771 struct fatcache savefc;
1772 struct denode *dep;
1773 struct mount *mp;
1774 struct msdosfsmount *pmp;
1775 struct vnode *vp;
1776 daddr_t runbn;
1777 u_long cn;
1778 int bnpercn, error, maxio, maxrun, run;
1779
1780 vp = ap->a_vp;
1781 dep = VTODE(vp);
1782 pmp = dep->de_pmp;
1783 if (ap->a_bop != NULL)
1784 *ap->a_bop = &pmp->pm_devvp->v_bufobj;
1785 if (ap->a_bnp == NULL)
1786 return (0);
1787 if (ap->a_runp != NULL)
1788 *ap->a_runp = 0;
1789 if (ap->a_runb != NULL)
1790 *ap->a_runb = 0;
1791 cn = ap->a_bn;
1792 if (cn != ap->a_bn)
1793 return (EFBIG);
1794 error = pcbmap(dep, cn, ap->a_bnp, NULL, NULL);
1795 if (error != 0 || (ap->a_runp == NULL && ap->a_runb == NULL))
1796 return (error);
1797
1798 /*
1799 * Prepare to back out updates of the fatchain cache after the one
1800 * for the first block done by pcbmap() above. Without the backout,
1801 * then whenever the caller doesn't do i/o to all of the blocks that
1802 * we find, the single useful cache entry would be too far in advance
1803 * of the actual i/o to work for the next sequential i/o. Then the
1804 * FAT would be searched from the beginning. With the backout, the
1805 * FAT is searched starting at most a few blocks early. This wastes
1806 * much less time. Time is also wasted finding more blocks than the
1807 * caller will do i/o to. This is necessary because the runlength
1808 * parameters are output-only.
1809 */
1810 savefc = dep->de_fc[FC_LASTMAP];
1811
1812 mp = vp->v_mount;
1813 maxio = mp->mnt_iosize_max / mp->mnt_stat.f_iosize;
1814 bnpercn = de_cn2bn(pmp, 1);
1815 if (ap->a_runp != NULL) {
1816 maxrun = ulmin(maxio - 1, pmp->pm_maxcluster - cn);
1817 for (run = 1; run <= maxrun; run++) {
1818 if (pcbmap(dep, cn + run, &runbn, NULL, NULL) != 0 ||
1819 runbn != *ap->a_bnp + run * bnpercn)
1820 break;
1821 }
1822 *ap->a_runp = run - 1;
1823 }
1824 if (ap->a_runb != NULL) {
1825 maxrun = ulmin(maxio - 1, cn);
1826 for (run = 1; run < maxrun; run++) {
1827 if (pcbmap(dep, cn - run, &runbn, NULL, NULL) != 0 ||
1828 runbn != *ap->a_bnp - run * bnpercn)
1829 break;
1830 }
1831 *ap->a_runb = run - 1;
1832 }
1833 dep->de_fc[FC_LASTMAP] = savefc;
1834 return (0);
1835 }
1836
1837 SYSCTL_NODE(_vfs, OID_AUTO, msdosfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1838 "msdos filesystem");
1839 static int use_buf_pager = 1;
1840 SYSCTL_INT(_vfs_msdosfs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
1841 &use_buf_pager, 0,
1842 "Use buffer pager instead of bmap");
1843
1844 static daddr_t
msdosfs_gbp_getblkno(struct vnode * vp,vm_ooffset_t off)1845 msdosfs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1846 {
1847
1848 return (de_cluster(VTODE(vp)->de_pmp, off));
1849 }
1850
1851 static int
msdosfs_gbp_getblksz(struct vnode * vp,daddr_t lbn,long * sz)1852 msdosfs_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *sz)
1853 {
1854
1855 *sz = VTODE(vp)->de_pmp->pm_bpcluster;
1856 return (0);
1857 }
1858
1859 static int
msdosfs_getpages(struct vop_getpages_args * ap)1860 msdosfs_getpages(struct vop_getpages_args *ap)
1861 {
1862
1863 if (use_buf_pager)
1864 return (vfs_bio_getpages(ap->a_vp, ap->a_m, ap->a_count,
1865 ap->a_rbehind, ap->a_rahead, msdosfs_gbp_getblkno,
1866 msdosfs_gbp_getblksz));
1867 return (vnode_pager_generic_getpages(ap->a_vp, ap->a_m, ap->a_count,
1868 ap->a_rbehind, ap->a_rahead, NULL, NULL));
1869 }
1870
1871 static int
msdosfs_strategy(struct vop_strategy_args * ap)1872 msdosfs_strategy(struct vop_strategy_args *ap)
1873 {
1874 struct buf *bp = ap->a_bp;
1875 struct denode *dep = VTODE(ap->a_vp);
1876 struct bufobj *bo;
1877 int error = 0;
1878 daddr_t blkno;
1879
1880 /*
1881 * If we don't already know the filesystem relative block number
1882 * then get it using pcbmap(). If pcbmap() returns the block
1883 * number as -1 then we've got a hole in the file. DOS filesystems
1884 * don't allow files with holes, so we shouldn't ever see this.
1885 */
1886 if (bp->b_blkno == bp->b_lblkno) {
1887 error = pcbmap(dep, bp->b_lblkno, &blkno, 0, 0);
1888 bp->b_blkno = blkno;
1889 if (error) {
1890 bp->b_error = error;
1891 bp->b_ioflags |= BIO_ERROR;
1892 bufdone(bp);
1893 return (0);
1894 }
1895 if ((long)bp->b_blkno == -1)
1896 vfs_bio_clrbuf(bp);
1897 }
1898 if (bp->b_blkno == -1) {
1899 bufdone(bp);
1900 return (0);
1901 }
1902 /*
1903 * Read/write the block from/to the disk that contains the desired
1904 * file block.
1905 */
1906 bp->b_iooffset = dbtob(bp->b_blkno);
1907 bo = dep->de_pmp->pm_bo;
1908 BO_STRATEGY(bo, bp);
1909 return (0);
1910 }
1911
1912 static int
msdosfs_print(struct vop_print_args * ap)1913 msdosfs_print(struct vop_print_args *ap)
1914 {
1915 struct denode *dep = VTODE(ap->a_vp);
1916
1917 printf("\tstartcluster %lu, dircluster %lu, diroffset %lu, ",
1918 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1919 printf("on dev %s\n", devtoname(dep->de_pmp->pm_dev));
1920 return (0);
1921 }
1922
1923 static int
msdosfs_pathconf(struct vop_pathconf_args * ap)1924 msdosfs_pathconf(struct vop_pathconf_args *ap)
1925 {
1926 struct msdosfsmount *pmp = VTODE(ap->a_vp)->de_pmp;
1927
1928 switch (ap->a_name) {
1929 case _PC_FILESIZEBITS:
1930 *ap->a_retval = 32;
1931 return (0);
1932 case _PC_LINK_MAX:
1933 *ap->a_retval = 1;
1934 return (0);
1935 case _PC_NAME_MAX:
1936 *ap->a_retval = pmp->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12;
1937 return (0);
1938 case _PC_CHOWN_RESTRICTED:
1939 *ap->a_retval = 1;
1940 return (0);
1941 case _PC_NO_TRUNC:
1942 *ap->a_retval = 0;
1943 return (0);
1944 case _PC_HAS_HIDDENSYSTEM:
1945 *ap->a_retval = 1;
1946 return (0);
1947 default:
1948 return (vop_stdpathconf(ap));
1949 }
1950 /* NOTREACHED */
1951 }
1952
1953 static int
msdosfs_vptofh(struct vop_vptofh_args * ap)1954 msdosfs_vptofh(struct vop_vptofh_args *ap)
1955 {
1956 struct denode *dep;
1957 struct defid *defhp;
1958 _Static_assert(sizeof(struct defid) <= sizeof(struct fid),
1959 "struct defid cannot be larger than struct fid");
1960
1961 dep = VTODE(ap->a_vp);
1962 defhp = (struct defid *)ap->a_fhp;
1963 defhp->defid_len = sizeof(struct defid);
1964 defhp->defid_dirclust = dep->de_dirclust;
1965 defhp->defid_dirofs = dep->de_diroffset;
1966 /* defhp->defid_gen = dep->de_gen; */
1967 return (0);
1968 }
1969
1970 /* Global vfs data structures for msdosfs */
1971 struct vop_vector msdosfs_vnodeops = {
1972 .vop_default = &default_vnodeops,
1973
1974 .vop_access = msdosfs_access,
1975 .vop_bmap = msdosfs_bmap,
1976 .vop_getpages = msdosfs_getpages,
1977 .vop_cachedlookup = msdosfs_lookup,
1978 .vop_open = msdosfs_open,
1979 .vop_close = msdosfs_close,
1980 .vop_create = msdosfs_create,
1981 .vop_fsync = msdosfs_fsync,
1982 .vop_fdatasync = vop_stdfdatasync_buf,
1983 .vop_getattr = msdosfs_getattr,
1984 .vop_inactive = msdosfs_inactive,
1985 .vop_link = msdosfs_link,
1986 .vop_lookup = vfs_cache_lookup,
1987 .vop_mkdir = msdosfs_mkdir,
1988 .vop_mknod = msdosfs_mknod,
1989 .vop_pathconf = msdosfs_pathconf,
1990 .vop_print = msdosfs_print,
1991 .vop_read = msdosfs_read,
1992 .vop_readdir = msdosfs_readdir,
1993 .vop_reclaim = msdosfs_reclaim,
1994 .vop_remove = msdosfs_remove,
1995 .vop_rename = msdosfs_rename,
1996 .vop_rmdir = msdosfs_rmdir,
1997 .vop_setattr = msdosfs_setattr,
1998 .vop_strategy = msdosfs_strategy,
1999 .vop_symlink = msdosfs_symlink,
2000 .vop_write = msdosfs_write,
2001 .vop_vptofh = msdosfs_vptofh,
2002 };
2003 VFS_VOP_VECTOR_REGISTER(msdosfs_vnodeops);
2004