msdosfs_vnops.c revision 1.108 1 /* $NetBSD: msdosfs_vnops.c,v 1.108 2021/10/23 07:38:33 hannken Exp $ */
2
3 /*-
4 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6 * All rights reserved.
7 * Original code by Paul Popelka (paulp (at) uts.amdahl.com) (see below).
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34 /*
35 * Written by Paul Popelka (paulp (at) uts.amdahl.com)
36 *
37 * You can do anything you want with this software, just don't say you wrote
38 * it, and don't remove this notice.
39 *
40 * This software is provided "as is".
41 *
42 * The author supplies this software to be publicly redistributed on the
43 * understanding that the author is not responsible for the correct
44 * functioning of this software in any circumstances and is not liable for
45 * any damages caused by this software.
46 *
47 * October 1992
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vnops.c,v 1.108 2021/10/23 07:38:33 hannken Exp $");
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/namei.h>
56 #include <sys/resourcevar.h> /* defines plimit structure in proc struct */
57 #include <sys/kernel.h>
58 #include <sys/file.h> /* define FWRITE ... */
59 #include <sys/stat.h>
60 #include <sys/buf.h>
61 #include <sys/proc.h>
62 #include <sys/mount.h>
63 #include <sys/vnode.h>
64 #include <sys/signalvar.h>
65 #include <sys/malloc.h>
66 #include <sys/dirent.h>
67 #include <sys/lockf.h>
68 #include <sys/kauth.h>
69
70 #include <miscfs/genfs/genfs.h>
71 #include <miscfs/specfs/specdev.h> /* XXX */ /* defines v_rdev */
72
73 #include <uvm/uvm_extern.h>
74
75 #include <fs/msdosfs/bpb.h>
76 #include <fs/msdosfs/direntry.h>
77 #include <fs/msdosfs/denode.h>
78 #include <fs/msdosfs/msdosfsmount.h>
79 #include <fs/msdosfs/fat.h>
80
81 /*
82 * Some general notes:
83 *
84 * In the ufs filesystem the inodes, superblocks, and indirect blocks are
85 * read/written using the vnode for the filesystem. Blocks that represent
86 * the contents of a file are read/written using the vnode for the file
87 * (including directories when they are read/written as files). This
88 * presents problems for the dos filesystem because data that should be in
89 * an inode (if dos had them) resides in the directory itself. Since we
90 * must update directory entries without the benefit of having the vnode
91 * for the directory we must use the vnode for the filesystem. This means
92 * that when a directory is actually read/written (via read, write, or
93 * readdir, or seek) we must use the vnode for the filesystem instead of
94 * the vnode for the directory as would happen in ufs. This is to insure we
95 * retrieve the correct block from the buffer cache since the hash value is
96 * based upon the vnode address and the desired block number.
97 */
98
99 /*
100 * Create a regular file. On entry the directory to contain the file being
101 * created is locked. We must release before we return.
102 */
103 int
104 msdosfs_create(void *v)
105 {
106 struct vop_create_v3_args /* {
107 struct vnode *a_dvp;
108 struct vnode **a_vpp;
109 struct componentname *a_cnp;
110 struct vattr *a_vap;
111 } */ *ap = v;
112 struct componentname *cnp = ap->a_cnp;
113 struct denode ndirent;
114 struct denode *dep;
115 struct denode *pdep = VTODE(ap->a_dvp);
116 int error;
117
118 #ifdef MSDOSFS_DEBUG
119 printf("msdosfs_create(cnp %p, vap %p\n", cnp, ap->a_vap);
120 #endif
121
122 /*
123 * If this is the root directory and there is no space left we
124 * can't do anything. This is because the root directory can not
125 * change size.
126 */
127 if (pdep->de_StartCluster == MSDOSFSROOT
128 && pdep->de_crap.mlr_fndoffset >= pdep->de_FileSize) {
129 error = ENOSPC;
130 goto bad;
131 }
132
133 /*
134 * Create a directory entry for the file, then call createde() to
135 * have it installed. NOTE: DOS files are always executable. We
136 * use the absence of the owner write bit to make the file
137 * readonly.
138 */
139 memset(&ndirent, 0, sizeof(ndirent));
140 if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
141 goto bad;
142
143 ndirent.de_Attributes = (ap->a_vap->va_mode & S_IWUSR) ?
144 ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
145 ndirent.de_StartCluster = 0;
146 ndirent.de_FileSize = 0;
147 ndirent.de_dev = pdep->de_dev;
148 ndirent.de_devvp = pdep->de_devvp;
149 ndirent.de_pmp = pdep->de_pmp;
150 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
151 DETIMES(&ndirent, NULL, NULL, NULL, pdep->de_pmp->pm_gmtoff);
152 if ((error = createde(&ndirent, pdep, &pdep->de_crap, &dep, cnp)) != 0)
153 goto bad;
154 *ap->a_vpp = DETOV(dep);
155 cache_enter(ap->a_dvp, *ap->a_vpp, cnp->cn_nameptr, cnp->cn_namelen,
156 cnp->cn_flags);
157 return (0);
158
159 bad:
160 return (error);
161 }
162
163 int
164 msdosfs_close(void *v)
165 {
166 struct vop_close_args /* {
167 struct vnode *a_vp;
168 int a_fflag;
169 kauth_cred_t a_cred;
170 } */ *ap = v;
171 struct vnode *vp = ap->a_vp;
172 struct denode *dep = VTODE(vp);
173
174 mutex_enter(vp->v_interlock);
175 if (vrefcnt(vp) > 1)
176 DETIMES(dep, NULL, NULL, NULL, dep->de_pmp->pm_gmtoff);
177 mutex_exit(vp->v_interlock);
178 return (0);
179 }
180
181 static int
182 msdosfs_check_possible(struct vnode *vp, struct denode *dep, accmode_t accmode)
183 {
184
185 /*
186 * Disallow write attempts on read-only file systems;
187 * unless the file is a socket, fifo, or a block or
188 * character device resident on the file system.
189 */
190 if (accmode & VWRITE) {
191 switch (vp->v_type) {
192 case VDIR:
193 case VLNK:
194 case VREG:
195 if (vp->v_mount->mnt_flag & MNT_RDONLY)
196 return (EROFS);
197 default:
198 break;
199 }
200 }
201
202 return 0;
203 }
204
205 static int
206 msdosfs_check_permitted(struct vnode *vp, struct denode *dep, accmode_t accmode,
207 kauth_cred_t cred)
208 {
209 struct msdosfsmount *pmp = dep->de_pmp;
210 mode_t file_mode;
211
212 if ((dep->de_Attributes & ATTR_READONLY) == 0)
213 file_mode = S_IRWXU|S_IRWXG|S_IRWXO;
214 else
215 file_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
216
217 file_mode &= (vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
218
219 return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
220 vp->v_type, file_mode), vp, NULL, genfs_can_access(vp, cred,
221 pmp->pm_uid, pmp->pm_gid, file_mode, NULL, accmode));
222 }
223
224 int
225 msdosfs_access(void *v)
226 {
227 struct vop_access_args /* {
228 struct vnode *a_vp;
229 accmode_t a_accmode;
230 kauth_cred_t a_cred;
231 } */ *ap = v;
232 struct vnode *vp = ap->a_vp;
233 struct denode *dep = VTODE(vp);
234 int error;
235
236 error = msdosfs_check_possible(vp, dep, ap->a_accmode);
237 if (error)
238 return error;
239
240 error = msdosfs_check_permitted(vp, dep, ap->a_accmode, ap->a_cred);
241
242 return error;
243 }
244
245 int
246 msdosfs_getattr(void *v)
247 {
248 struct vop_getattr_args /* {
249 struct vnode *a_vp;
250 struct vattr *a_vap;
251 kauth_cred_t a_cred;
252 } */ *ap = v;
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 u_long dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
258 ino_t fileid;
259
260 DETIMES(dep, NULL, NULL, NULL, pmp->pm_gmtoff);
261 vap->va_fsid = dep->de_dev;
262 /*
263 * The following computation of the fileid must be the same as that
264 * used in msdosfs_readdir() to compute d_fileno. If not, pwd
265 * doesn't work.
266 */
267 if (dep->de_Attributes & ATTR_DIRECTORY) {
268 fileid = cntobn(pmp, (ino_t)dep->de_StartCluster) * dirsperblk;
269 if (dep->de_StartCluster == MSDOSFSROOT)
270 fileid = 1;
271 } else {
272 fileid = cntobn(pmp, (ino_t)dep->de_dirclust) * dirsperblk;
273 if (dep->de_dirclust == MSDOSFSROOT)
274 fileid = roottobn(pmp, 0) * dirsperblk;
275 fileid += dep->de_diroffset / sizeof(struct direntry);
276 }
277 vap->va_fileid = fileid;
278 if ((dep->de_Attributes & ATTR_READONLY) == 0)
279 mode = S_IRWXU|S_IRWXG|S_IRWXO;
280 else
281 mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
282 vap->va_mode =
283 mode & (ap->a_vp->v_type == VDIR ? pmp->pm_dirmask : pmp->pm_mask);
284 vap->va_uid = pmp->pm_uid;
285 vap->va_gid = pmp->pm_gid;
286 vap->va_nlink = 1;
287 vap->va_rdev = 0;
288 vap->va_size = ap->a_vp->v_size;
289 dos2unixtime(dep->de_MDate, dep->de_MTime, 0, pmp->pm_gmtoff,
290 &vap->va_mtime);
291 if (dep->de_pmp->pm_flags & MSDOSFSMNT_LONGNAME) {
292 dos2unixtime(dep->de_ADate, 0, 0, pmp->pm_gmtoff,
293 &vap->va_atime);
294 dos2unixtime(dep->de_CDate, dep->de_CTime, dep->de_CHun,
295 pmp->pm_gmtoff, &vap->va_ctime);
296 } else {
297 vap->va_atime = vap->va_mtime;
298 vap->va_ctime = vap->va_mtime;
299 }
300 vap->va_flags = 0;
301 if ((dep->de_Attributes & ATTR_ARCHIVE) == 0) {
302 vap->va_flags |= SF_ARCHIVED;
303 vap->va_mode |= S_ARCH1;
304 }
305 vap->va_gen = 0;
306 vap->va_blocksize = pmp->pm_bpcluster;
307 vap->va_bytes =
308 (dep->de_FileSize + pmp->pm_crbomask) & ~pmp->pm_crbomask;
309 vap->va_type = ap->a_vp->v_type;
310 return (0);
311 }
312
313 int
314 msdosfs_setattr(void *v)
315 {
316 struct vop_setattr_args /* {
317 struct vnode *a_vp;
318 struct vattr *a_vap;
319 kauth_cred_t a_cred;
320 } */ *ap = v;
321 int error = 0, de_changed = 0;
322 struct denode *dep = VTODE(ap->a_vp);
323 struct msdosfsmount *pmp = dep->de_pmp;
324 struct vnode *vp = ap->a_vp;
325 struct vattr *vap = ap->a_vap;
326 kauth_cred_t cred = ap->a_cred;
327
328 #ifdef MSDOSFS_DEBUG
329 printf("msdosfs_setattr(): vp %p, vap %p, cred %p\n",
330 ap->a_vp, vap, cred);
331 #endif
332 /*
333 * Note we silently ignore uid or gid changes.
334 */
335 if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) ||
336 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||
337 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||
338 (vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL) ||
339 (vap->va_uid != VNOVAL && vap->va_uid != pmp->pm_uid) ||
340 (vap->va_gid != VNOVAL && vap->va_gid != pmp->pm_gid)) {
341 #ifdef MSDOSFS_DEBUG
342 printf("msdosfs_setattr(): returning EINVAL\n");
343 printf(" va_type %d, va_nlink %x, va_fsid %"PRIx64", va_fileid %llx\n",
344 vap->va_type, vap->va_nlink, vap->va_fsid,
345 (unsigned long long)vap->va_fileid);
346 printf(" va_blocksize %lx, va_rdev %"PRIx64", va_bytes %"PRIx64", va_gen %lx\n",
347 vap->va_blocksize, vap->va_rdev, vap->va_bytes, vap->va_gen);
348 #endif
349 return (EINVAL);
350 }
351 /*
352 * Silently ignore attributes modifications on directories.
353 */
354 if (ap->a_vp->v_type == VDIR)
355 return 0;
356
357 if (vap->va_size != VNOVAL) {
358 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
359 error = EROFS;
360 goto bad;
361 }
362 error = detrunc(dep, (u_long)vap->va_size, 0, cred);
363 if (error)
364 goto bad;
365 de_changed = 1;
366 }
367 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
368 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
369 error = EROFS;
370 goto bad;
371 }
372 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES,
373 ap->a_vp, NULL, genfs_can_chtimes(ap->a_vp, cred,
374 pmp->pm_uid, vap->va_vaflags));
375 if (error)
376 goto bad;
377 if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0 &&
378 vap->va_atime.tv_sec != VNOVAL)
379 unix2dostime(&vap->va_atime, pmp->pm_gmtoff, &dep->de_ADate, NULL, NULL);
380 if (vap->va_mtime.tv_sec != VNOVAL)
381 unix2dostime(&vap->va_mtime, pmp->pm_gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
382 dep->de_Attributes |= ATTR_ARCHIVE;
383 dep->de_flag |= DE_MODIFIED;
384 de_changed = 1;
385 }
386 /*
387 * DOS files only have the ability to have their writability
388 * attribute set, so we use the owner write bit to set the readonly
389 * attribute.
390 */
391 if (vap->va_mode != (mode_t)VNOVAL) {
392 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
393 error = EROFS;
394 goto bad;
395 }
396 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS, vp,
397 NULL, genfs_can_chflags(vp, cred, pmp->pm_uid, false));
398 if (error)
399 goto bad;
400 /* We ignore the read and execute bits. */
401 if (vap->va_mode & S_IWUSR)
402 dep->de_Attributes &= ~ATTR_READONLY;
403 else
404 dep->de_Attributes |= ATTR_READONLY;
405 dep->de_flag |= DE_MODIFIED;
406 de_changed = 1;
407 }
408 /*
409 * Allow the `archived' bit to be toggled.
410 */
411 if (vap->va_flags != VNOVAL) {
412 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
413 error = EROFS;
414 goto bad;
415 }
416 error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_FLAGS, vp,
417 NULL, genfs_can_chflags(vp, cred, pmp->pm_uid, false));
418 if (error)
419 goto bad;
420 if (vap->va_flags & SF_ARCHIVED)
421 dep->de_Attributes &= ~ATTR_ARCHIVE;
422 else
423 dep->de_Attributes |= ATTR_ARCHIVE;
424 dep->de_flag |= DE_MODIFIED;
425 de_changed = 1;
426 }
427
428 if (de_changed) {
429 error = deupdat(dep, 1);
430 if (error)
431 goto bad;
432 }
433
434 bad:
435 return error;
436 }
437
438 int
439 msdosfs_read(void *v)
440 {
441 struct vop_read_args /* {
442 struct vnode *a_vp;
443 struct uio *a_uio;
444 int a_ioflag;
445 kauth_cred_t a_cred;
446 } */ *ap = v;
447 int error = 0;
448 int64_t diff;
449 int blsize;
450 long n;
451 long on;
452 daddr_t lbn;
453 vsize_t bytelen;
454 struct buf *bp;
455 struct vnode *vp = ap->a_vp;
456 struct denode *dep = VTODE(vp);
457 struct msdosfsmount *pmp = dep->de_pmp;
458 struct uio *uio = ap->a_uio;
459
460 /*
461 * If they didn't ask for any data, then we are done.
462 */
463
464 if (uio->uio_resid == 0)
465 return (0);
466 if (uio->uio_offset < 0)
467 return (EINVAL);
468 if (uio->uio_offset >= dep->de_FileSize)
469 return (0);
470
471 if (vp->v_type == VREG) {
472 const int advice = IO_ADV_DECODE(ap->a_ioflag);
473
474 while (uio->uio_resid > 0) {
475 bytelen = MIN(dep->de_FileSize - uio->uio_offset,
476 uio->uio_resid);
477
478 if (bytelen == 0)
479 break;
480 error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
481 UBC_READ | UBC_PARTIALOK | UBC_VNODE_FLAGS(vp));
482 if (error)
483 break;
484 }
485 dep->de_flag |= DE_ACCESS;
486 goto out;
487 }
488
489 /* this loop is only for directories now */
490 do {
491 lbn = de_cluster(pmp, uio->uio_offset);
492 on = uio->uio_offset & pmp->pm_crbomask;
493 n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
494 if (uio->uio_offset >= dep->de_FileSize) {
495 return (0);
496 }
497 /* file size (and hence diff) may be up to 4GB */
498 diff = dep->de_FileSize - uio->uio_offset;
499 if (diff < n)
500 n = (long) diff;
501
502 /* convert cluster # to sector # */
503 error = pcbmap(dep, lbn, &lbn, 0, &blsize);
504 if (error)
505 goto bad;
506
507 /*
508 * If we are operating on a directory file then be sure to
509 * do i/o with the vnode for the filesystem instead of the
510 * vnode for the directory.
511 */
512 error = bread(pmp->pm_devvp, de_bn2kb(pmp, lbn), blsize,
513 0, &bp);
514 if (error) {
515 goto bad;
516 }
517 n = MIN(n, pmp->pm_bpcluster - bp->b_resid);
518 error = uiomove((char *)bp->b_data + on, (int) n, uio);
519 brelse(bp, 0);
520 } while (error == 0 && uio->uio_resid > 0 && n != 0);
521
522 out:
523 if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
524 int uerror;
525
526 uerror = deupdat(dep, 1);
527 if (error == 0)
528 error = uerror;
529 }
530 bad:
531 return (error);
532 }
533
534 /*
535 * Write data to a file or directory.
536 */
537 int
538 msdosfs_write(void *v)
539 {
540 struct vop_write_args /* {
541 struct vnode *a_vp;
542 struct uio *a_uio;
543 int a_ioflag;
544 kauth_cred_t a_cred;
545 } */ *ap = v;
546 int resid;
547 int error = 0;
548 int ioflag = ap->a_ioflag;
549 u_long osize;
550 u_long count;
551 vsize_t bytelen;
552 off_t oldoff;
553 size_t rem;
554 struct uio *uio = ap->a_uio;
555 struct vnode *vp = ap->a_vp;
556 struct denode *dep = VTODE(vp);
557 struct msdosfsmount *pmp = dep->de_pmp;
558 kauth_cred_t cred = ap->a_cred;
559 bool async;
560
561 #ifdef MSDOSFS_DEBUG
562 printf("msdosfs_write(vp %p, uio %p, ioflag %x, cred %p\n",
563 vp, uio, ioflag, cred);
564 printf("msdosfs_write(): diroff %lu, dirclust %lu, startcluster %lu\n",
565 dep->de_diroffset, dep->de_dirclust, dep->de_StartCluster);
566 #endif
567
568 switch (vp->v_type) {
569 case VREG:
570 if (ioflag & IO_APPEND)
571 uio->uio_offset = dep->de_FileSize;
572 break;
573 case VDIR:
574 return EISDIR;
575 default:
576 panic("msdosfs_write(): bad file type");
577 }
578
579 if (uio->uio_offset < 0)
580 return (EINVAL);
581
582 if (uio->uio_resid == 0)
583 return (0);
584
585 /* Don't bother to try to write files larger than the fs limit */
586 if (uio->uio_offset + uio->uio_resid > MSDOSFS_FILESIZE_MAX)
587 return (EFBIG);
588
589 /*
590 * If the offset we are starting the write at is beyond the end of
591 * the file, then they've done a seek. Unix filesystems allow
592 * files with holes in them, DOS doesn't so we must fill the hole
593 * with zeroed blocks.
594 */
595 if (uio->uio_offset > dep->de_FileSize) {
596 if ((error = deextend(dep, uio->uio_offset, cred)) != 0) {
597 return (error);
598 }
599 }
600
601 /*
602 * Remember some values in case the write fails.
603 */
604 async = vp->v_mount->mnt_flag & MNT_ASYNC;
605 resid = uio->uio_resid;
606 osize = dep->de_FileSize;
607
608 /*
609 * If we write beyond the end of the file, extend it to its ultimate
610 * size ahead of the time to hopefully get a contiguous area.
611 */
612 if (uio->uio_offset + resid > osize) {
613 count = de_clcount(pmp, uio->uio_offset + resid) -
614 de_clcount(pmp, osize);
615 if ((error = extendfile(dep, count, NULL, NULL, 0)))
616 goto errexit;
617
618 dep->de_FileSize = uio->uio_offset + resid;
619 /* hint uvm to not read in extended part */
620 uvm_vnp_setwritesize(vp, dep->de_FileSize);
621 /* zero out the remainder of the last page */
622 rem = round_page(dep->de_FileSize) - dep->de_FileSize;
623 if (rem > 0)
624 ubc_zerorange(&vp->v_uobj, (off_t)dep->de_FileSize,
625 rem, UBC_VNODE_FLAGS(vp));
626 }
627
628 do {
629 oldoff = uio->uio_offset;
630 bytelen = uio->uio_resid;
631
632 error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
633 IO_ADV_DECODE(ioflag), UBC_WRITE | UBC_VNODE_FLAGS(vp));
634 if (error)
635 break;
636
637 /*
638 * flush what we just wrote if necessary.
639 * XXXUBC simplistic async flushing.
640 */
641
642 if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
643 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
644 error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
645 (uio->uio_offset >> 16) << 16,
646 PGO_CLEANIT | PGO_LAZY);
647 }
648 } while (error == 0 && uio->uio_resid > 0);
649
650 /* set final size */
651 uvm_vnp_setsize(vp, dep->de_FileSize);
652 if (error == 0 && ioflag & IO_SYNC) {
653 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
654 error = VOP_PUTPAGES(vp, trunc_page(oldoff),
655 round_page(oldoff + bytelen), PGO_CLEANIT | PGO_SYNCIO);
656 }
657 dep->de_flag |= DE_UPDATE;
658
659 /*
660 * If the write failed and they want us to, truncate the file back
661 * to the size it was before the write was attempted.
662 */
663 errexit:
664 if (error) {
665 detrunc(dep, osize, ioflag & IO_SYNC, NOCRED);
666 uio->uio_offset -= resid - uio->uio_resid;
667 uio->uio_resid = resid;
668 } else if ((ioflag & IO_SYNC) == IO_SYNC)
669 error = deupdat(dep, 1);
670 KASSERT(vp->v_size == dep->de_FileSize);
671 return (error);
672 }
673
674 int
675 msdosfs_update(struct vnode *vp, const struct timespec *acc,
676 const struct timespec *mod, int flags)
677 {
678 struct buf *bp;
679 struct direntry *dirp;
680 struct denode *dep;
681 int error;
682
683 if (vp->v_mount->mnt_flag & MNT_RDONLY)
684 return (0);
685 dep = VTODE(vp);
686 DETIMES(dep, acc, mod, NULL, dep->de_pmp->pm_gmtoff);
687 if ((dep->de_flag & DE_MODIFIED) == 0)
688 return (0);
689 dep->de_flag &= ~DE_MODIFIED;
690 if (dep->de_Attributes & ATTR_DIRECTORY)
691 return (0);
692 if (dep->de_refcnt <= 0)
693 return (0);
694 error = readde(dep, &bp, &dirp);
695 if (error)
696 return (error);
697 DE_EXTERNALIZE(dirp, dep);
698 if (flags & (UPDATE_WAIT|UPDATE_DIROP))
699 return (bwrite(bp));
700 else {
701 bdwrite(bp);
702 return (0);
703 }
704 }
705
706 /*
707 * Flush the blocks of a file to disk.
708 *
709 * This function is worthless for vnodes that represent directories. Maybe we
710 * could just do a sync if they try an fsync on a directory file.
711 */
712 int
713 msdosfs_remove(void *v)
714 {
715 struct vop_remove_v3_args /* {
716 struct vnode *a_dvp;
717 struct vnode *a_vp;
718 struct componentname *a_cnp;
719 nlink_t ctx_vp_new_nlink;
720 } */ *ap = v;
721 struct denode *dep = VTODE(ap->a_vp);
722 struct denode *ddep = VTODE(ap->a_dvp);
723 int error;
724
725 if (ap->a_vp->v_type == VDIR)
726 error = EPERM;
727 else
728 error = removede(ddep, dep, &ddep->de_crap);
729 #ifdef MSDOSFS_DEBUG
730 printf("msdosfs_remove(), dep %p, usecount %d\n",
731 dep, vrefcnt(ap->a_vp));
732 #endif
733 if (ddep == dep)
734 vrele(ap->a_vp);
735 else
736 vput(ap->a_vp); /* causes msdosfs_inactive() to be called
737 * via vrele() */
738
739 return (error);
740 }
741
742 /*
743 * Renames on files require moving the denode to a new hash queue since the
744 * denode's location is used to compute which hash queue to put the file
745 * in. Unless it is a rename in place. For example "mv a b".
746 *
747 * What follows is the basic algorithm:
748 *
749 * if (file move) {
750 * if (dest file exists) {
751 * remove dest file
752 * }
753 * if (dest and src in same directory) {
754 * rewrite name in existing directory slot
755 * } else {
756 * write new entry in dest directory
757 * update offset and dirclust in denode
758 * move denode to new hash chain
759 * clear old directory entry
760 * }
761 * } else {
762 * directory move
763 * if (dest directory exists) {
764 * if (dest is not empty) {
765 * return ENOTEMPTY
766 * }
767 * remove dest directory
768 * }
769 * if (dest and src in same directory) {
770 * rewrite name in existing entry
771 * } else {
772 * be sure dest is not a child of src directory
773 * write entry in dest directory
774 * update "." and ".." in moved directory
775 * update offset and dirclust in denode
776 * move denode to new hash chain
777 * clear old directory entry for moved directory
778 * }
779 * }
780 *
781 * On entry:
782 * source's parent directory is unlocked
783 * source file or directory is unlocked
784 * destination's parent directory is locked
785 * destination file or directory is locked if it exists
786 *
787 * On exit:
788 * all denodes should be released
789 *
790 * Notes:
791 * I'm not sure how the memory containing the pathnames pointed at by the
792 * componentname structures is freed, there may be some memory bleeding
793 * for each rename done.
794 *
795 * --More-- Notes:
796 * This routine needs help. badly.
797 */
798 int
799 msdosfs_rename(void *v)
800 {
801 struct vop_rename_args /* {
802 struct vnode *a_fdvp;
803 struct vnode *a_fvp;
804 struct componentname *a_fcnp;
805 struct vnode *a_tdvp;
806 struct vnode *a_tvp;
807 struct componentname *a_tcnp;
808 } */ *ap = v;
809 struct vnode *tvp = ap->a_tvp;
810 struct vnode *tdvp = ap->a_tdvp;
811 struct vnode *fvp = ap->a_fvp;
812 struct vnode *fdvp = ap->a_fdvp;
813 struct componentname *tcnp = ap->a_tcnp;
814 struct componentname *fcnp = ap->a_fcnp;
815 struct denode *ip, *xp, *dp, *zp;
816 u_char toname[12], oldname[12];
817 u_long from_diroffset, to_diroffset;
818 u_char to_count;
819 int doingdirectory = 0, newparent = 0;
820 int error;
821 u_long cn;
822 daddr_t bn;
823 struct msdosfsmount *pmp;
824 struct direntry *dotdotp;
825 struct buf *bp;
826
827 pmp = VFSTOMSDOSFS(fdvp->v_mount);
828
829 /*
830 * Check for cross-device rename.
831 */
832 if ((fvp->v_mount != tdvp->v_mount) ||
833 (tvp && (fvp->v_mount != tvp->v_mount))) {
834 error = EXDEV;
835 abortit:
836 VOP_ABORTOP(tdvp, tcnp);
837 if (tdvp == tvp)
838 vrele(tdvp);
839 else
840 vput(tdvp);
841 if (tvp)
842 vput(tvp);
843 VOP_ABORTOP(fdvp, fcnp);
844 vrele(fdvp);
845 vrele(fvp);
846 return (error);
847 }
848
849 /*
850 * If source and dest are the same, do nothing.
851 */
852 if (tvp == fvp) {
853 error = 0;
854 goto abortit;
855 }
856
857 /*
858 * XXX: This can deadlock since we hold tdvp/tvp locked.
859 * But I'm not going to fix it now.
860 */
861 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
862 goto abortit;
863 dp = VTODE(fdvp);
864 ip = VTODE(fvp);
865
866 /*
867 * Be sure we are not renaming ".", "..", or an alias of ".". This
868 * leads to a crippled directory tree. It's pretty tough to do a
869 * "ls" or "pwd" with the "." directory entry missing, and "cd .."
870 * doesn't work if the ".." entry is missing.
871 */
872 if (ip->de_Attributes & ATTR_DIRECTORY) {
873 /*
874 * Avoid ".", "..", and aliases of "." for obvious reasons.
875 */
876 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
877 dp == ip ||
878 (fcnp->cn_flags & ISDOTDOT) ||
879 (tcnp->cn_flags & ISDOTDOT) ||
880 (ip->de_flag & DE_RENAME)) {
881 VOP_UNLOCK(fvp);
882 error = EINVAL;
883 goto abortit;
884 }
885 ip->de_flag |= DE_RENAME;
886 doingdirectory++;
887 }
888 VN_KNOTE(fdvp, NOTE_WRITE); /* XXXLUKEM/XXX: right place? */
889
890 /*
891 * When the target exists, both the directory
892 * and target vnodes are returned locked.
893 */
894 dp = VTODE(tdvp);
895 xp = tvp ? VTODE(tvp) : NULL;
896 /*
897 * Remember direntry place to use for destination
898 */
899 to_diroffset = dp->de_crap.mlr_fndoffset;
900 to_count = dp->de_crap.mlr_fndcnt;
901
902 /*
903 * If ".." must be changed (ie the directory gets a new
904 * parent) then the source directory must not be in the
905 * directory hierarchy above the target, as this would
906 * orphan everything below the source directory. Also
907 * the user must have write permission in the source so
908 * as to be able to change "..". We must repeat the call
909 * to namei, as the parent directory is unlocked by the
910 * call to doscheckpath().
911 */
912 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
913 VOP_UNLOCK(fvp);
914 if (VTODE(fdvp)->de_StartCluster != VTODE(tdvp)->de_StartCluster)
915 newparent = 1;
916
917 if (doingdirectory && newparent) {
918 if (error) /* write access check above */
919 goto tdvpbad;
920 if (xp != NULL)
921 vput(tvp);
922 tvp = NULL;
923 /*
924 * doscheckpath() vput()'s tdvp (dp == VTODE(tdvp)),
925 * so we have to get an extra ref to it first, and
926 * because it's been unlocked we need to do a relookup
927 * afterwards in case tvp has changed.
928 */
929 vref(tdvp);
930 if ((error = doscheckpath(ip, dp)) != 0)
931 goto bad;
932 vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
933 if ((error = relookup(tdvp, &tvp, tcnp, 0)) != 0) {
934 VOP_UNLOCK(tdvp);
935 goto bad;
936 }
937 dp = VTODE(tdvp);
938 xp = tvp ? VTODE(tvp) : NULL;
939 }
940
941 if (xp != NULL) {
942 /*
943 * Target must be empty if a directory and have no links
944 * to it. Also, ensure source and target are compatible
945 * (both directories, or both not directories).
946 */
947 if (xp->de_Attributes & ATTR_DIRECTORY) {
948 if (!dosdirempty(xp)) {
949 error = ENOTEMPTY;
950 goto tdvpbad;
951 }
952 if (!doingdirectory) {
953 error = ENOTDIR;
954 goto tdvpbad;
955 }
956 } else if (doingdirectory) {
957 error = EISDIR;
958 goto tdvpbad;
959 }
960 if ((error = removede(dp, xp, &dp->de_crap)) != 0)
961 goto tdvpbad;
962 VN_KNOTE(tdvp, NOTE_WRITE);
963 VN_KNOTE(tvp, NOTE_DELETE);
964 cache_purge(tvp);
965 vput(tvp);
966 tvp = NULL;
967 xp = NULL;
968 }
969
970 /*
971 * Convert the filename in tcnp into a dos filename. We copy this
972 * into the denode and directory entry for the destination
973 * file/directory.
974 */
975 if ((error = uniqdosname(VTODE(tdvp), tcnp, toname)) != 0) {
976 goto abortit;
977 }
978
979 /*
980 * Since from wasn't locked at various places above,
981 * have to do a relookup here.
982 */
983 fcnp->cn_flags &= ~MODMASK;
984 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
985 VOP_UNLOCK(tdvp);
986 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
987 if ((error = relookup(fdvp, &fvp, fcnp, 0))) {
988 VOP_UNLOCK(fdvp);
989 vrele(ap->a_fvp);
990 vrele(tdvp);
991 return (error);
992 }
993 if (fvp == NULL) {
994 /*
995 * From name has disappeared.
996 */
997 if (doingdirectory)
998 panic("rename: lost dir entry");
999 vput(fdvp);
1000 vrele(ap->a_fvp);
1001 vrele(tdvp);
1002 return 0;
1003 }
1004 VOP_UNLOCK(fdvp);
1005 xp = VTODE(fvp);
1006 zp = VTODE(fdvp);
1007 from_diroffset = zp->de_crap.mlr_fndoffset;
1008
1009 /*
1010 * Ensure that the directory entry still exists and has not
1011 * changed till now. If the source is a file the entry may
1012 * have been unlinked or renamed. In either case there is
1013 * no further work to be done. If the source is a directory
1014 * then it cannot have been rmdir'ed or renamed; this is
1015 * prohibited by the DE_RENAME flag.
1016 */
1017 if (xp != ip) {
1018 if (doingdirectory)
1019 panic("rename: lost dir entry");
1020 vrele(ap->a_fvp);
1021 xp = NULL;
1022 } else {
1023 vrele(fvp);
1024 xp = NULL;
1025
1026 /*
1027 * First write a new entry in the destination
1028 * directory and mark the entry in the source directory
1029 * as deleted. Then move the denode to the correct hash
1030 * chain for its new location in the filesystem. And, if
1031 * we moved a directory, then update its .. entry to point
1032 * to the new parent directory.
1033 */
1034 memcpy(oldname, ip->de_Name, 11);
1035 memcpy(ip->de_Name, toname, 11); /* update denode */
1036 dp->de_crap.mlr_fndoffset = to_diroffset;
1037 dp->de_crap.mlr_fndcnt = to_count;
1038 error = createde(ip, dp, &dp->de_crap, (struct denode **)0,
1039 tcnp);
1040 if (error) {
1041 memcpy(ip->de_Name, oldname, 11);
1042 VOP_UNLOCK(fvp);
1043 goto bad;
1044 }
1045 ip->de_refcnt++;
1046 zp->de_crap.mlr_fndoffset = from_diroffset;
1047 if ((error = removede(zp, ip, &zp->de_crap)) != 0) {
1048 /* XXX should really panic here, fs is corrupt */
1049 VOP_UNLOCK(fvp);
1050 goto bad;
1051 }
1052 cache_purge(fvp);
1053 if (!doingdirectory) {
1054 struct denode_key old_key = ip->de_key;
1055 struct denode_key new_key = ip->de_key;
1056
1057 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1058 &new_key.dk_dirclust, 0);
1059 if (error) {
1060 /* XXX should really panic here, fs is corrupt */
1061 VOP_UNLOCK(fvp);
1062 goto bad;
1063 }
1064 new_key.dk_diroffset = to_diroffset;
1065 if (new_key.dk_dirclust != MSDOSFSROOT)
1066 new_key.dk_diroffset &= pmp->pm_crbomask;
1067 vcache_rekey_enter(pmp->pm_mountp, fvp, &old_key,
1068 sizeof(old_key), &new_key, sizeof(new_key));
1069 ip->de_key = new_key;
1070 vcache_rekey_exit(pmp->pm_mountp, fvp, &old_key,
1071 sizeof(old_key), &ip->de_key, sizeof(ip->de_key));
1072 }
1073 }
1074
1075 /*
1076 * If we moved a directory to a new parent directory, then we must
1077 * fixup the ".." entry in the moved directory.
1078 */
1079 if (doingdirectory && newparent) {
1080 cn = ip->de_StartCluster;
1081 if (cn == MSDOSFSROOT) {
1082 /* this should never happen */
1083 panic("msdosfs_rename: updating .. in root directory?");
1084 } else
1085 bn = cntobn(pmp, cn);
1086 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
1087 pmp->pm_bpcluster, B_MODIFY, &bp);
1088 if (error) {
1089 /* XXX should really panic here, fs is corrupt */
1090 VOP_UNLOCK(fvp);
1091 goto bad;
1092 }
1093 dotdotp = (struct direntry *)bp->b_data + 1;
1094 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1095 if (FAT32(pmp)) {
1096 putushort(dotdotp->deHighClust,
1097 dp->de_StartCluster >> 16);
1098 } else {
1099 putushort(dotdotp->deHighClust, 0);
1100 }
1101 if ((error = bwrite(bp)) != 0) {
1102 /* XXX should really panic here, fs is corrupt */
1103 VOP_UNLOCK(fvp);
1104 goto bad;
1105 }
1106 }
1107
1108 VN_KNOTE(fvp, NOTE_RENAME);
1109 VOP_UNLOCK(fvp);
1110 bad:
1111 if (tvp)
1112 vput(tvp);
1113 vrele(tdvp);
1114 ip->de_flag &= ~DE_RENAME;
1115 vrele(fdvp);
1116 vrele(fvp);
1117 return (error);
1118
1119 /* XXX: uuuh */
1120 tdvpbad:
1121 VOP_UNLOCK(tdvp);
1122 goto bad;
1123 }
1124
1125 static const struct {
1126 struct direntry dot;
1127 struct direntry dotdot;
1128 } dosdirtemplate = {
1129 { ". ", " ", /* the . entry */
1130 ATTR_DIRECTORY, /* file attribute */
1131 0, /* reserved */
1132 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1133 { 0, 0 }, /* access date */
1134 { 0, 0 }, /* high bits of start cluster */
1135 { 210, 4 }, { 210, 4 }, /* modify time & date */
1136 { 0, 0 }, /* startcluster */
1137 { 0, 0, 0, 0 } /* filesize */
1138 },
1139 { ".. ", " ", /* the .. entry */
1140 ATTR_DIRECTORY, /* file attribute */
1141 0, /* reserved */
1142 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1143 { 0, 0 }, /* access date */
1144 { 0, 0 }, /* high bits of start cluster */
1145 { 210, 4 }, { 210, 4 }, /* modify time & date */
1146 { 0, 0 }, /* startcluster */
1147 { 0, 0, 0, 0 } /* filesize */
1148 }
1149 };
1150
1151 int
1152 msdosfs_mkdir(void *v)
1153 {
1154 struct vop_mkdir_v3_args /* {
1155 struct vnode *a_dvp;
1156 struvt vnode **a_vpp;
1157 struvt componentname *a_cnp;
1158 struct vattr *a_vap;
1159 } */ *ap = v;
1160 struct componentname *cnp = ap->a_cnp;
1161 struct denode ndirent;
1162 struct denode *dep;
1163 struct denode *pdep = VTODE(ap->a_dvp);
1164 int error;
1165 int bn;
1166 u_long newcluster, pcl;
1167 daddr_t lbn;
1168 struct direntry *denp;
1169 struct msdosfsmount *pmp = pdep->de_pmp;
1170 struct buf *bp;
1171 int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
1172
1173 /*
1174 * If this is the root directory and there is no space left we
1175 * can't do anything. This is because the root directory can not
1176 * change size.
1177 */
1178 if (pdep->de_StartCluster == MSDOSFSROOT
1179 && pdep->de_crap.mlr_fndoffset >= pdep->de_FileSize) {
1180 error = ENOSPC;
1181 goto bad2;
1182 }
1183
1184 /*
1185 * Allocate a cluster to hold the about to be created directory.
1186 */
1187 error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
1188 if (error)
1189 goto bad2;
1190
1191 memset(&ndirent, 0, sizeof(ndirent));
1192 ndirent.de_pmp = pmp;
1193 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1194 DETIMES(&ndirent, NULL, NULL, NULL, pmp->pm_gmtoff);
1195
1196 /*
1197 * Now fill the cluster with the "." and ".." entries. And write
1198 * the cluster to disk. This way it is there for the parent
1199 * directory to be pointing at if there were a crash.
1200 */
1201 bn = cntobn(pmp, newcluster);
1202 lbn = de_bn2kb(pmp, bn);
1203 /* always succeeds */
1204 bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
1205 memset(bp->b_data, 0, pmp->pm_bpcluster);
1206 memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
1207 denp = (struct direntry *)bp->b_data;
1208 putushort(denp[0].deStartCluster, newcluster);
1209 putushort(denp[0].deCDate, ndirent.de_CDate);
1210 putushort(denp[0].deCTime, ndirent.de_CTime);
1211 denp[0].deCHundredth = ndirent.de_CHun;
1212 putushort(denp[0].deADate, ndirent.de_ADate);
1213 putushort(denp[0].deMDate, ndirent.de_MDate);
1214 putushort(denp[0].deMTime, ndirent.de_MTime);
1215 pcl = pdep->de_StartCluster;
1216 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1217 pcl = 0;
1218 putushort(denp[1].deStartCluster, pcl);
1219 putushort(denp[1].deCDate, ndirent.de_CDate);
1220 putushort(denp[1].deCTime, ndirent.de_CTime);
1221 denp[1].deCHundredth = ndirent.de_CHun;
1222 putushort(denp[1].deADate, ndirent.de_ADate);
1223 putushort(denp[1].deMDate, ndirent.de_MDate);
1224 putushort(denp[1].deMTime, ndirent.de_MTime);
1225 if (FAT32(pmp)) {
1226 putushort(denp[0].deHighClust, newcluster >> 16);
1227 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1228 } else {
1229 putushort(denp[0].deHighClust, 0);
1230 putushort(denp[1].deHighClust, 0);
1231 }
1232
1233 if (async)
1234 bdwrite(bp);
1235 else if ((error = bwrite(bp)) != 0)
1236 goto bad;
1237
1238 /*
1239 * Now build up a directory entry pointing to the newly allocated
1240 * cluster. This will be written to an empty slot in the parent
1241 * directory.
1242 */
1243 if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
1244 goto bad;
1245
1246 ndirent.de_Attributes = ATTR_DIRECTORY;
1247 ndirent.de_StartCluster = newcluster;
1248 ndirent.de_FileSize = 0;
1249 ndirent.de_dev = pdep->de_dev;
1250 ndirent.de_devvp = pdep->de_devvp;
1251 if ((error = createde(&ndirent, pdep, &pdep->de_crap, &dep, cnp)) != 0)
1252 goto bad;
1253 *ap->a_vpp = DETOV(dep);
1254 return (0);
1255
1256 bad:
1257 clusterfree(pmp, newcluster, NULL);
1258 bad2:
1259 return (error);
1260 }
1261
1262 int
1263 msdosfs_rmdir(void *v)
1264 {
1265 struct vop_rmdir_v2_args /* {
1266 struct vnode *a_dvp;
1267 struct vnode *a_vp;
1268 struct componentname *a_cnp;
1269 } */ *ap = v;
1270 struct vnode *vp = ap->a_vp;
1271 struct vnode *dvp = ap->a_dvp;
1272 struct componentname *cnp = ap->a_cnp;
1273 struct denode *ip, *dp;
1274 int error;
1275
1276 ip = VTODE(vp);
1277 dp = VTODE(dvp);
1278 /*
1279 * No rmdir "." please.
1280 */
1281 if (dp == ip) {
1282 vrele(vp);
1283 return (EINVAL);
1284 }
1285 /*
1286 * Verify the directory is empty (and valid).
1287 * (Rmdir ".." won't be valid since
1288 * ".." will contain a reference to
1289 * the current directory and thus be
1290 * non-empty.)
1291 */
1292 error = 0;
1293 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1294 error = ENOTEMPTY;
1295 goto out;
1296 }
1297 /*
1298 * Delete the entry from the directory. For dos filesystems this
1299 * gets rid of the directory entry on disk, the in memory copy
1300 * still exists but the de_refcnt is <= 0. This prevents it from
1301 * being found by deget(). When the vput() on dep is done we give
1302 * up access and eventually msdosfs_reclaim() will be called which
1303 * will remove it from the denode cache.
1304 */
1305 if ((error = removede(dp, ip, &dp->de_crap)) != 0)
1306 goto out;
1307 /*
1308 * This is where we decrement the link count in the parent
1309 * directory. Since dos filesystems don't do this we just purge
1310 * the name cache and let go of the parent directory denode.
1311 */
1312 cache_purge(dvp);
1313 /*
1314 * Truncate the directory that is being deleted.
1315 */
1316 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
1317 cache_purge(vp);
1318 out:
1319 vput(vp);
1320 return (error);
1321 }
1322
1323 int
1324 msdosfs_readdir(void *v)
1325 {
1326 struct vop_readdir_args /* {
1327 struct vnode *a_vp;
1328 struct uio *a_uio;
1329 kauth_cred_t a_cred;
1330 int *a_eofflag;
1331 off_t **a_cookies;
1332 int *a_ncookies;
1333 } */ *ap = v;
1334 int error = 0;
1335 int diff;
1336 long n;
1337 int blsize;
1338 long on;
1339 long lost;
1340 long count;
1341 u_long cn;
1342 ino_t fileno;
1343 u_long dirsperblk;
1344 long bias = 0;
1345 daddr_t bn, lbn;
1346 struct buf *bp;
1347 struct denode *dep = VTODE(ap->a_vp);
1348 struct msdosfsmount *pmp = dep->de_pmp;
1349 struct direntry *dentp;
1350 struct dirent *dirbuf;
1351 struct uio *uio = ap->a_uio;
1352 off_t *cookies = NULL;
1353 int ncookies = 0, nc = 0;
1354 off_t offset, uio_off;
1355 int chksum = -1;
1356 uint16_t namlen;
1357
1358 #ifdef MSDOSFS_DEBUG
1359 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1360 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1361 #endif
1362
1363 /*
1364 * msdosfs_readdir() won't operate properly on regular files since
1365 * it does i/o only with the filesystem vnode, and hence can
1366 * retrieve the wrong block from the buffer cache for a plain file.
1367 * So, fail attempts to readdir() on a plain file.
1368 */
1369 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1370 return (ENOTDIR);
1371
1372 /*
1373 * If the user buffer is smaller than the size of one dos directory
1374 * entry or the file offset is not a multiple of the size of a
1375 * directory entry, then we fail the read.
1376 */
1377 count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1378 offset = uio->uio_offset;
1379 if (count < sizeof(struct direntry) ||
1380 (offset & (sizeof(struct direntry) - 1)))
1381 return (EINVAL);
1382 lost = uio->uio_resid - count;
1383 uio->uio_resid = count;
1384 uio_off = uio->uio_offset;
1385
1386
1387 /* Allocate a temporary dirent buffer. */
1388 dirbuf = malloc(sizeof(struct dirent), M_MSDOSFSTMP, M_WAITOK | M_ZERO);
1389
1390 if (ap->a_ncookies) {
1391 nc = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
1392 cookies = malloc(nc * sizeof (off_t), M_TEMP, M_WAITOK);
1393 *ap->a_cookies = cookies;
1394 }
1395
1396 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1397
1398 /*
1399 * If they are reading from the root directory then, we simulate
1400 * the . and .. entries since these don't exist in the root
1401 * directory. We also set the offset bias to make up for having to
1402 * simulate these entries. By this I mean that at file offset 64 we
1403 * read the first entry in the root directory that lives on disk.
1404 */
1405 if (dep->de_StartCluster == MSDOSFSROOT
1406 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1407 #if 0
1408 printf("msdosfs_readdir(): going after . or .. in root dir, "
1409 "offset %" PRIu64 "\n", offset);
1410 #endif
1411 bias = 2 * sizeof(struct direntry);
1412 if (offset < bias) {
1413 for (n = (int)offset / sizeof(struct direntry);
1414 n < 2; n++) {
1415 if (FAT32(pmp))
1416 dirbuf->d_fileno = cntobn(pmp,
1417 (ino_t)pmp->pm_rootdirblk)
1418 * dirsperblk;
1419 else
1420 dirbuf->d_fileno = 1;
1421 dirbuf->d_type = DT_DIR;
1422 switch (n) {
1423 case 0:
1424 dirbuf->d_namlen = 1;
1425 strlcpy(dirbuf->d_name, ".",
1426 sizeof(dirbuf->d_name));
1427 break;
1428 case 1:
1429 dirbuf->d_namlen = 2;
1430 strlcpy(dirbuf->d_name, "..",
1431 sizeof(dirbuf->d_name));
1432 break;
1433 }
1434 dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
1435 if (uio->uio_resid < dirbuf->d_reclen)
1436 goto out;
1437 error = uiomove(dirbuf, dirbuf->d_reclen, uio);
1438 if (error)
1439 goto out;
1440 offset += sizeof(struct direntry);
1441 uio_off = offset;
1442 if (cookies) {
1443 *cookies++ = offset;
1444 ncookies++;
1445 if (ncookies >= nc)
1446 goto out;
1447 }
1448 }
1449 }
1450 }
1451
1452 while (uio->uio_resid > 0) {
1453 lbn = de_cluster(pmp, offset - bias);
1454 on = (offset - bias) & pmp->pm_crbomask;
1455 n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
1456 diff = dep->de_FileSize - (offset - bias);
1457 if (diff <= 0)
1458 break;
1459 n = MIN(n, diff);
1460 if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
1461 break;
1462 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1463 0, &bp);
1464 if (error) {
1465 goto bad;
1466 }
1467 n = MIN(n, blsize - bp->b_resid);
1468
1469 /*
1470 * Convert from dos directory entries to fs-independent
1471 * directory entries.
1472 */
1473 for (dentp = (struct direntry *)((char *)bp->b_data + on);
1474 (char *)dentp < (char *)bp->b_data + on + n;
1475 dentp++, offset += sizeof(struct direntry)) {
1476 #if 0
1477
1478 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1479 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1480 #endif
1481 /*
1482 * If this is an unused entry, we can stop.
1483 */
1484 if (dentp->deName[0] == SLOT_EMPTY) {
1485 brelse(bp, 0);
1486 goto out;
1487 }
1488 /*
1489 * Skip deleted entries.
1490 */
1491 if (dentp->deName[0] == SLOT_DELETED) {
1492 chksum = -1;
1493 continue;
1494 }
1495
1496 /*
1497 * Handle Win95 long directory entries
1498 */
1499 if (dentp->deAttributes == ATTR_WIN95) {
1500 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1501 continue;
1502 chksum = win2unixfn((struct winentry *)dentp,
1503 dirbuf, chksum, &namlen,
1504 pmp->pm_flags & MSDOSFSMNT_UTF8);
1505 if (chksum != -1)
1506 dirbuf->d_namlen = namlen;
1507 continue;
1508 }
1509
1510 /*
1511 * Skip volume labels
1512 */
1513 if (dentp->deAttributes & ATTR_VOLUME) {
1514 chksum = -1;
1515 continue;
1516 }
1517 /*
1518 * This computation of d_fileno must match
1519 * the computation of va_fileid in
1520 * msdosfs_getattr.
1521 */
1522 if (dentp->deAttributes & ATTR_DIRECTORY) {
1523 fileno = getushort(dentp->deStartCluster);
1524 if (FAT32(pmp))
1525 fileno |= ((ino_t)getushort(dentp->deHighClust)) << 16;
1526 /* if this is the root directory */
1527 if (fileno == MSDOSFSROOT)
1528 if (FAT32(pmp))
1529 fileno = cntobn(pmp,
1530 (ino_t)pmp->pm_rootdirblk)
1531 * dirsperblk;
1532 else
1533 fileno = 1;
1534 else
1535 fileno = cntobn(pmp, fileno) * dirsperblk;
1536 dirbuf->d_fileno = fileno;
1537 dirbuf->d_type = DT_DIR;
1538 } else {
1539 dirbuf->d_fileno =
1540 offset / sizeof(struct direntry);
1541 dirbuf->d_type = DT_REG;
1542 }
1543 if (chksum != winChksum(dentp->deName))
1544 dirbuf->d_namlen = dos2unixfn(dentp->deName,
1545 (u_char *)dirbuf->d_name,
1546 pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
1547 else
1548 dirbuf->d_name[dirbuf->d_namlen] = 0;
1549 namlen = dirbuf->d_namlen;
1550 chksum = -1;
1551 dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
1552 if (uio->uio_resid < dirbuf->d_reclen) {
1553 brelse(bp, 0);
1554 goto out;
1555 }
1556 error = uiomove(dirbuf, dirbuf->d_reclen, uio);
1557 if (error) {
1558 brelse(bp, 0);
1559 goto out;
1560 }
1561 uio_off = offset + sizeof(struct direntry);
1562 if (cookies) {
1563 *cookies++ = offset + sizeof(struct direntry);
1564 ncookies++;
1565 if (ncookies >= nc) {
1566 brelse(bp, 0);
1567 goto out;
1568 }
1569 }
1570 }
1571 brelse(bp, 0);
1572 }
1573
1574 out:
1575 uio->uio_offset = uio_off;
1576 uio->uio_resid += lost;
1577 if (dep->de_FileSize - (offset - bias) <= 0)
1578 *ap->a_eofflag = 1;
1579 else
1580 *ap->a_eofflag = 0;
1581
1582 if (ap->a_ncookies) {
1583 if (error) {
1584 free(*ap->a_cookies, M_TEMP);
1585 *ap->a_ncookies = 0;
1586 *ap->a_cookies = NULL;
1587 } else
1588 *ap->a_ncookies = ncookies;
1589 }
1590
1591 bad:
1592 free(dirbuf, M_MSDOSFSTMP);
1593 return (error);
1594 }
1595
1596 /*
1597 * vp - address of vnode file the file
1598 * bn - which cluster we are interested in mapping to a filesystem block number.
1599 * vpp - returns the vnode for the block special file holding the filesystem
1600 * containing the file of interest
1601 * bnp - address of where to return the filesystem relative block number
1602 */
1603 int
1604 msdosfs_bmap(void *v)
1605 {
1606 struct vop_bmap_args /* {
1607 struct vnode *a_vp;
1608 daddr_t a_bn;
1609 struct vnode **a_vpp;
1610 daddr_t *a_bnp;
1611 int *a_runp;
1612 } */ *ap = v;
1613 struct denode *dep = VTODE(ap->a_vp);
1614 int run, maxrun;
1615 daddr_t runbn;
1616 int status;
1617
1618 if (ap->a_vpp != NULL)
1619 *ap->a_vpp = dep->de_devvp;
1620 if (ap->a_bnp == NULL)
1621 return (0);
1622 status = pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0);
1623
1624 /*
1625 * From FreeBSD:
1626 * A little kludgy, but we loop calling pcbmap until we
1627 * reach the end of the contiguous piece, or reach MAXPHYS.
1628 * Since it reduces disk I/Os, the "wasted" CPU is put to
1629 * good use (4 to 5 fold sequential read I/O improvement on USB
1630 * drives).
1631 */
1632 if (ap->a_runp != NULL) {
1633 /* taken from ufs_bmap */
1634 maxrun = ulmin(MAXPHYS / dep->de_pmp->pm_bpcluster - 1,
1635 dep->de_pmp->pm_maxcluster - ap->a_bn);
1636 for (run = 1; run <= maxrun; run++) {
1637 if (pcbmap(dep, ap->a_bn + run, &runbn, NULL, NULL)
1638 != 0 || runbn !=
1639 *ap->a_bnp + de_cn2bn(dep->de_pmp, run))
1640 break;
1641 }
1642 *ap->a_runp = run - 1;
1643 }
1644
1645 /*
1646 * We need to scale *ap->a_bnp by sector_size/DEV_BSIZE
1647 */
1648 *ap->a_bnp = de_bn2kb(dep->de_pmp, *ap->a_bnp);
1649 return status;
1650 }
1651
1652 int
1653 msdosfs_strategy(void *v)
1654 {
1655 struct vop_strategy_args /* {
1656 struct vnode *a_vp;
1657 struct buf *a_bp;
1658 } */ *ap = v;
1659 struct vnode *vp = ap->a_vp;
1660 struct buf *bp = ap->a_bp;
1661 struct denode *dep = VTODE(bp->b_vp);
1662 int error = 0;
1663
1664 if (vp->v_type == VBLK || vp->v_type == VCHR)
1665 panic("msdosfs_strategy: spec");
1666 /*
1667 * If we don't already know the filesystem relative block number
1668 * then get it using pcbmap(). If pcbmap() returns the block
1669 * number as -1 then we've got a hole in the file. DOS filesystems
1670 * don't allow files with holes, so we shouldn't ever see this.
1671 */
1672 if (bp->b_blkno == bp->b_lblkno) {
1673 error = pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
1674 &bp->b_blkno, 0, 0);
1675 if (error)
1676 bp->b_blkno = -1;
1677 if (bp->b_blkno == -1)
1678 clrbuf(bp);
1679 else
1680 bp->b_blkno = de_bn2kb(dep->de_pmp, bp->b_blkno);
1681 }
1682 if (bp->b_blkno == -1) {
1683 biodone(bp);
1684 return (error);
1685 }
1686
1687 /*
1688 * Read/write the block from/to the disk that contains the desired
1689 * file block.
1690 */
1691
1692 vp = dep->de_devvp;
1693 return (VOP_STRATEGY(vp, bp));
1694 }
1695
1696 int
1697 msdosfs_print(void *v)
1698 {
1699 struct vop_print_args /* {
1700 struct vnode *vp;
1701 } */ *ap = v;
1702 struct denode *dep = VTODE(ap->a_vp);
1703
1704 printf(
1705 "tag VT_MSDOSFS, startcluster %ld, dircluster %ld, diroffset %ld ",
1706 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1707 printf(" dev %llu, %llu ", (unsigned long long)major(dep->de_dev),
1708 (unsigned long long)minor(dep->de_dev));
1709 printf("\n");
1710 return (0);
1711 }
1712
1713 int
1714 msdosfs_advlock(void *v)
1715 {
1716 struct vop_advlock_args /* {
1717 struct vnode *a_vp;
1718 void *a_id;
1719 int a_op;
1720 struct flock *a_fl;
1721 int a_flags;
1722 } */ *ap = v;
1723 struct denode *dep = VTODE(ap->a_vp);
1724
1725 return lf_advlock(ap, &dep->de_lockf, dep->de_FileSize);
1726 }
1727
1728 int
1729 msdosfs_pathconf(void *v)
1730 {
1731 struct vop_pathconf_args /* {
1732 struct vnode *a_vp;
1733 int a_name;
1734 register_t *a_retval;
1735 } */ *ap = v;
1736
1737 switch (ap->a_name) {
1738 case _PC_LINK_MAX:
1739 *ap->a_retval = 1;
1740 return (0);
1741 case _PC_NAME_MAX:
1742 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
1743 return (0);
1744 case _PC_PATH_MAX:
1745 *ap->a_retval = PATH_MAX;
1746 return (0);
1747 case _PC_CHOWN_RESTRICTED:
1748 *ap->a_retval = 1;
1749 return (0);
1750 case _PC_NO_TRUNC:
1751 *ap->a_retval = 1;
1752 return (0);
1753 case _PC_SYNC_IO:
1754 *ap->a_retval = 1;
1755 return (0);
1756 case _PC_FILESIZEBITS:
1757 *ap->a_retval = 32;
1758 return (0);
1759 default:
1760 return genfs_pathconf(ap);
1761 }
1762 /* NOTREACHED */
1763 }
1764
1765 int
1766 msdosfs_fsync(void *v)
1767 {
1768 struct vop_fsync_args /* {
1769 struct vnode *a_vp;
1770 kauth_cred_t a_cred;
1771 int a_flags;
1772 off_t offlo;
1773 off_t offhi;
1774 } */ *ap = v;
1775 struct vnode *vp = ap->a_vp;
1776 int wait;
1777 int error;
1778
1779 wait = (ap->a_flags & FSYNC_WAIT) != 0;
1780 error = vflushbuf(vp, ap->a_flags);
1781 if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
1782 error = msdosfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
1783
1784 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
1785 struct denode *dep = VTODE(vp);
1786 struct vnode *devvp = dep->de_devvp;
1787
1788 int l = 0;
1789 error = VOP_IOCTL(devvp, DIOCCACHESYNC, &l, FWRITE,
1790 curlwp->l_cred);
1791 }
1792
1793 return (error);
1794 }
1795
1796 void
1797 msdosfs_detimes(struct denode *dep, const struct timespec *acc,
1798 const struct timespec *mod, const struct timespec *cre, int gmtoff)
1799 {
1800 struct timespec *ts = NULL, tsb;
1801
1802 KASSERT(dep->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS));
1803 /* XXX just call getnanotime early and use result if needed? */
1804 dep->de_flag |= DE_MODIFIED;
1805 if (dep->de_flag & DE_UPDATE) {
1806 if (mod == NULL) {
1807 getnanotime(&tsb);
1808 mod = ts = &tsb;
1809 }
1810 unix2dostime(mod, gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
1811 dep->de_Attributes |= ATTR_ARCHIVE;
1812 }
1813 if ((dep->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0) {
1814 if (dep->de_flag & DE_ACCESS) {
1815 if (acc == NULL)
1816 acc = ts == NULL ?
1817 (getnanotime(&tsb), ts = &tsb) : ts;
1818 unix2dostime(acc, gmtoff, &dep->de_ADate, NULL, NULL);
1819 }
1820 if (dep->de_flag & DE_CREATE) {
1821 if (cre == NULL)
1822 cre = ts == NULL ?
1823 (getnanotime(&tsb), ts = &tsb) : ts;
1824 unix2dostime(cre, gmtoff, &dep->de_CDate,
1825 &dep->de_CTime, &dep->de_CHun);
1826 }
1827 }
1828
1829 dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);
1830 }
1831
1832 /* Global vfs data structures for msdosfs */
1833 int (**msdosfs_vnodeop_p)(void *);
1834 const struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1835 { &vop_default_desc, vn_default_error },
1836 { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
1837 { &vop_lookup_desc, msdosfs_lookup }, /* lookup */
1838 { &vop_create_desc, msdosfs_create }, /* create */
1839 { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */
1840 { &vop_open_desc, genfs_nullop }, /* open */
1841 { &vop_close_desc, msdosfs_close }, /* close */
1842 { &vop_access_desc, msdosfs_access }, /* access */
1843 { &vop_accessx_desc, genfs_accessx }, /* accessx */
1844 { &vop_getattr_desc, msdosfs_getattr }, /* getattr */
1845 { &vop_setattr_desc, msdosfs_setattr }, /* setattr */
1846 { &vop_read_desc, msdosfs_read }, /* read */
1847 { &vop_write_desc, msdosfs_write }, /* write */
1848 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
1849 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
1850 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
1851 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */
1852 { &vop_poll_desc, genfs_poll }, /* poll */
1853 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
1854 { &vop_revoke_desc, genfs_revoke }, /* revoke */
1855 { &vop_mmap_desc, genfs_mmap }, /* mmap */
1856 { &vop_fsync_desc, msdosfs_fsync }, /* fsync */
1857 { &vop_seek_desc, genfs_seek }, /* seek */
1858 { &vop_remove_desc, msdosfs_remove }, /* remove */
1859 { &vop_link_desc, genfs_eopnotsupp }, /* link */
1860 { &vop_rename_desc, msdosfs_rename }, /* rename */
1861 { &vop_mkdir_desc, msdosfs_mkdir }, /* mkdir */
1862 { &vop_rmdir_desc, msdosfs_rmdir }, /* rmdir */
1863 { &vop_symlink_desc, genfs_eopnotsupp }, /* symlink */
1864 { &vop_readdir_desc, msdosfs_readdir }, /* readdir */
1865 { &vop_readlink_desc, genfs_einval }, /* readlink */
1866 { &vop_abortop_desc, genfs_abortop }, /* abortop */
1867 { &vop_inactive_desc, msdosfs_inactive }, /* inactive */
1868 { &vop_reclaim_desc, msdosfs_reclaim }, /* reclaim */
1869 { &vop_lock_desc, genfs_lock }, /* lock */
1870 { &vop_unlock_desc, genfs_unlock }, /* unlock */
1871 { &vop_bmap_desc, msdosfs_bmap }, /* bmap */
1872 { &vop_strategy_desc, msdosfs_strategy }, /* strategy */
1873 { &vop_print_desc, msdosfs_print }, /* print */
1874 { &vop_islocked_desc, genfs_islocked }, /* islocked */
1875 { &vop_pathconf_desc, msdosfs_pathconf }, /* pathconf */
1876 { &vop_advlock_desc, msdosfs_advlock }, /* advlock */
1877 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1878 { &vop_getpages_desc, genfs_getpages }, /* getpages */
1879 { &vop_putpages_desc, genfs_putpages }, /* putpages */
1880 { NULL, NULL }
1881 };
1882 const struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1883 { &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
1884