msdosfs_vnops.c revision 1.107 1 /* $NetBSD: msdosfs_vnops.c,v 1.107 2021/10/20 03:08:17 thorpej 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.107 2021/10/20 03:08:17 thorpej 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_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, &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);
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_fndoffset;
900 to_count = dp->de_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)) != 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_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_fndoffset = to_diroffset;
1037 dp->de_fndcnt = to_count;
1038 error = createde(ip, dp, (struct denode **)0, tcnp);
1039 if (error) {
1040 memcpy(ip->de_Name, oldname, 11);
1041 VOP_UNLOCK(fvp);
1042 goto bad;
1043 }
1044 ip->de_refcnt++;
1045 zp->de_fndoffset = from_diroffset;
1046 if ((error = removede(zp, ip)) != 0) {
1047 /* XXX should really panic here, fs is corrupt */
1048 VOP_UNLOCK(fvp);
1049 goto bad;
1050 }
1051 cache_purge(fvp);
1052 if (!doingdirectory) {
1053 struct denode_key old_key = ip->de_key;
1054 struct denode_key new_key = ip->de_key;
1055
1056 error = pcbmap(dp, de_cluster(pmp, to_diroffset), 0,
1057 &new_key.dk_dirclust, 0);
1058 if (error) {
1059 /* XXX should really panic here, fs is corrupt */
1060 VOP_UNLOCK(fvp);
1061 goto bad;
1062 }
1063 new_key.dk_diroffset = to_diroffset;
1064 if (new_key.dk_dirclust != MSDOSFSROOT)
1065 new_key.dk_diroffset &= pmp->pm_crbomask;
1066 vcache_rekey_enter(pmp->pm_mountp, fvp, &old_key,
1067 sizeof(old_key), &new_key, sizeof(new_key));
1068 ip->de_key = new_key;
1069 vcache_rekey_exit(pmp->pm_mountp, fvp, &old_key,
1070 sizeof(old_key), &ip->de_key, sizeof(ip->de_key));
1071 }
1072 }
1073
1074 /*
1075 * If we moved a directory to a new parent directory, then we must
1076 * fixup the ".." entry in the moved directory.
1077 */
1078 if (doingdirectory && newparent) {
1079 cn = ip->de_StartCluster;
1080 if (cn == MSDOSFSROOT) {
1081 /* this should never happen */
1082 panic("msdosfs_rename: updating .. in root directory?");
1083 } else
1084 bn = cntobn(pmp, cn);
1085 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn),
1086 pmp->pm_bpcluster, B_MODIFY, &bp);
1087 if (error) {
1088 /* XXX should really panic here, fs is corrupt */
1089 VOP_UNLOCK(fvp);
1090 goto bad;
1091 }
1092 dotdotp = (struct direntry *)bp->b_data + 1;
1093 putushort(dotdotp->deStartCluster, dp->de_StartCluster);
1094 if (FAT32(pmp)) {
1095 putushort(dotdotp->deHighClust,
1096 dp->de_StartCluster >> 16);
1097 } else {
1098 putushort(dotdotp->deHighClust, 0);
1099 }
1100 if ((error = bwrite(bp)) != 0) {
1101 /* XXX should really panic here, fs is corrupt */
1102 VOP_UNLOCK(fvp);
1103 goto bad;
1104 }
1105 }
1106
1107 VN_KNOTE(fvp, NOTE_RENAME);
1108 VOP_UNLOCK(fvp);
1109 bad:
1110 if (tvp)
1111 vput(tvp);
1112 vrele(tdvp);
1113 ip->de_flag &= ~DE_RENAME;
1114 vrele(fdvp);
1115 vrele(fvp);
1116 return (error);
1117
1118 /* XXX: uuuh */
1119 tdvpbad:
1120 VOP_UNLOCK(tdvp);
1121 goto bad;
1122 }
1123
1124 static const struct {
1125 struct direntry dot;
1126 struct direntry dotdot;
1127 } dosdirtemplate = {
1128 { ". ", " ", /* the . entry */
1129 ATTR_DIRECTORY, /* file attribute */
1130 0, /* reserved */
1131 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1132 { 0, 0 }, /* access date */
1133 { 0, 0 }, /* high bits of start cluster */
1134 { 210, 4 }, { 210, 4 }, /* modify time & date */
1135 { 0, 0 }, /* startcluster */
1136 { 0, 0, 0, 0 } /* filesize */
1137 },
1138 { ".. ", " ", /* the .. entry */
1139 ATTR_DIRECTORY, /* file attribute */
1140 0, /* reserved */
1141 0, { 0, 0 }, { 0, 0 }, /* create time & date */
1142 { 0, 0 }, /* access date */
1143 { 0, 0 }, /* high bits of start cluster */
1144 { 210, 4 }, { 210, 4 }, /* modify time & date */
1145 { 0, 0 }, /* startcluster */
1146 { 0, 0, 0, 0 } /* filesize */
1147 }
1148 };
1149
1150 int
1151 msdosfs_mkdir(void *v)
1152 {
1153 struct vop_mkdir_v3_args /* {
1154 struct vnode *a_dvp;
1155 struvt vnode **a_vpp;
1156 struvt componentname *a_cnp;
1157 struct vattr *a_vap;
1158 } */ *ap = v;
1159 struct componentname *cnp = ap->a_cnp;
1160 struct denode ndirent;
1161 struct denode *dep;
1162 struct denode *pdep = VTODE(ap->a_dvp);
1163 int error;
1164 int bn;
1165 u_long newcluster, pcl;
1166 daddr_t lbn;
1167 struct direntry *denp;
1168 struct msdosfsmount *pmp = pdep->de_pmp;
1169 struct buf *bp;
1170 int async = pdep->de_pmp->pm_mountp->mnt_flag & MNT_ASYNC;
1171
1172 /*
1173 * If this is the root directory and there is no space left we
1174 * can't do anything. This is because the root directory can not
1175 * change size.
1176 */
1177 if (pdep->de_StartCluster == MSDOSFSROOT
1178 && pdep->de_fndoffset >= pdep->de_FileSize) {
1179 error = ENOSPC;
1180 goto bad2;
1181 }
1182
1183 /*
1184 * Allocate a cluster to hold the about to be created directory.
1185 */
1186 error = clusteralloc(pmp, 0, 1, &newcluster, NULL);
1187 if (error)
1188 goto bad2;
1189
1190 memset(&ndirent, 0, sizeof(ndirent));
1191 ndirent.de_pmp = pmp;
1192 ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
1193 DETIMES(&ndirent, NULL, NULL, NULL, pmp->pm_gmtoff);
1194
1195 /*
1196 * Now fill the cluster with the "." and ".." entries. And write
1197 * the cluster to disk. This way it is there for the parent
1198 * directory to be pointing at if there were a crash.
1199 */
1200 bn = cntobn(pmp, newcluster);
1201 lbn = de_bn2kb(pmp, bn);
1202 /* always succeeds */
1203 bp = getblk(pmp->pm_devvp, lbn, pmp->pm_bpcluster, 0, 0);
1204 memset(bp->b_data, 0, pmp->pm_bpcluster);
1205 memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
1206 denp = (struct direntry *)bp->b_data;
1207 putushort(denp[0].deStartCluster, newcluster);
1208 putushort(denp[0].deCDate, ndirent.de_CDate);
1209 putushort(denp[0].deCTime, ndirent.de_CTime);
1210 denp[0].deCHundredth = ndirent.de_CHun;
1211 putushort(denp[0].deADate, ndirent.de_ADate);
1212 putushort(denp[0].deMDate, ndirent.de_MDate);
1213 putushort(denp[0].deMTime, ndirent.de_MTime);
1214 pcl = pdep->de_StartCluster;
1215 if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
1216 pcl = 0;
1217 putushort(denp[1].deStartCluster, pcl);
1218 putushort(denp[1].deCDate, ndirent.de_CDate);
1219 putushort(denp[1].deCTime, ndirent.de_CTime);
1220 denp[1].deCHundredth = ndirent.de_CHun;
1221 putushort(denp[1].deADate, ndirent.de_ADate);
1222 putushort(denp[1].deMDate, ndirent.de_MDate);
1223 putushort(denp[1].deMTime, ndirent.de_MTime);
1224 if (FAT32(pmp)) {
1225 putushort(denp[0].deHighClust, newcluster >> 16);
1226 putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
1227 } else {
1228 putushort(denp[0].deHighClust, 0);
1229 putushort(denp[1].deHighClust, 0);
1230 }
1231
1232 if (async)
1233 bdwrite(bp);
1234 else if ((error = bwrite(bp)) != 0)
1235 goto bad;
1236
1237 /*
1238 * Now build up a directory entry pointing to the newly allocated
1239 * cluster. This will be written to an empty slot in the parent
1240 * directory.
1241 */
1242 if ((error = uniqdosname(pdep, cnp, ndirent.de_Name)) != 0)
1243 goto bad;
1244
1245 ndirent.de_Attributes = ATTR_DIRECTORY;
1246 ndirent.de_StartCluster = newcluster;
1247 ndirent.de_FileSize = 0;
1248 ndirent.de_dev = pdep->de_dev;
1249 ndirent.de_devvp = pdep->de_devvp;
1250 if ((error = createde(&ndirent, pdep, &dep, cnp)) != 0)
1251 goto bad;
1252 *ap->a_vpp = DETOV(dep);
1253 return (0);
1254
1255 bad:
1256 clusterfree(pmp, newcluster, NULL);
1257 bad2:
1258 return (error);
1259 }
1260
1261 int
1262 msdosfs_rmdir(void *v)
1263 {
1264 struct vop_rmdir_v2_args /* {
1265 struct vnode *a_dvp;
1266 struct vnode *a_vp;
1267 struct componentname *a_cnp;
1268 } */ *ap = v;
1269 struct vnode *vp = ap->a_vp;
1270 struct vnode *dvp = ap->a_dvp;
1271 struct componentname *cnp = ap->a_cnp;
1272 struct denode *ip, *dp;
1273 int error;
1274
1275 ip = VTODE(vp);
1276 dp = VTODE(dvp);
1277 /*
1278 * No rmdir "." please.
1279 */
1280 if (dp == ip) {
1281 vrele(vp);
1282 return (EINVAL);
1283 }
1284 /*
1285 * Verify the directory is empty (and valid).
1286 * (Rmdir ".." won't be valid since
1287 * ".." will contain a reference to
1288 * the current directory and thus be
1289 * non-empty.)
1290 */
1291 error = 0;
1292 if (!dosdirempty(ip) || ip->de_flag & DE_RENAME) {
1293 error = ENOTEMPTY;
1294 goto out;
1295 }
1296 /*
1297 * Delete the entry from the directory. For dos filesystems this
1298 * gets rid of the directory entry on disk, the in memory copy
1299 * still exists but the de_refcnt is <= 0. This prevents it from
1300 * being found by deget(). When the vput() on dep is done we give
1301 * up access and eventually msdosfs_reclaim() will be called which
1302 * will remove it from the denode cache.
1303 */
1304 if ((error = removede(dp, ip)) != 0)
1305 goto out;
1306 /*
1307 * This is where we decrement the link count in the parent
1308 * directory. Since dos filesystems don't do this we just purge
1309 * the name cache and let go of the parent directory denode.
1310 */
1311 cache_purge(dvp);
1312 /*
1313 * Truncate the directory that is being deleted.
1314 */
1315 error = detrunc(ip, (u_long)0, IO_SYNC, cnp->cn_cred);
1316 cache_purge(vp);
1317 out:
1318 vput(vp);
1319 return (error);
1320 }
1321
1322 int
1323 msdosfs_readdir(void *v)
1324 {
1325 struct vop_readdir_args /* {
1326 struct vnode *a_vp;
1327 struct uio *a_uio;
1328 kauth_cred_t a_cred;
1329 int *a_eofflag;
1330 off_t **a_cookies;
1331 int *a_ncookies;
1332 } */ *ap = v;
1333 int error = 0;
1334 int diff;
1335 long n;
1336 int blsize;
1337 long on;
1338 long lost;
1339 long count;
1340 u_long cn;
1341 ino_t fileno;
1342 u_long dirsperblk;
1343 long bias = 0;
1344 daddr_t bn, lbn;
1345 struct buf *bp;
1346 struct denode *dep = VTODE(ap->a_vp);
1347 struct msdosfsmount *pmp = dep->de_pmp;
1348 struct direntry *dentp;
1349 struct dirent *dirbuf;
1350 struct uio *uio = ap->a_uio;
1351 off_t *cookies = NULL;
1352 int ncookies = 0, nc = 0;
1353 off_t offset, uio_off;
1354 int chksum = -1;
1355 uint16_t namlen;
1356
1357 #ifdef MSDOSFS_DEBUG
1358 printf("msdosfs_readdir(): vp %p, uio %p, cred %p, eofflagp %p\n",
1359 ap->a_vp, uio, ap->a_cred, ap->a_eofflag);
1360 #endif
1361
1362 /*
1363 * msdosfs_readdir() won't operate properly on regular files since
1364 * it does i/o only with the filesystem vnode, and hence can
1365 * retrieve the wrong block from the buffer cache for a plain file.
1366 * So, fail attempts to readdir() on a plain file.
1367 */
1368 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0)
1369 return (ENOTDIR);
1370
1371 /*
1372 * If the user buffer is smaller than the size of one dos directory
1373 * entry or the file offset is not a multiple of the size of a
1374 * directory entry, then we fail the read.
1375 */
1376 count = uio->uio_resid & ~(sizeof(struct direntry) - 1);
1377 offset = uio->uio_offset;
1378 if (count < sizeof(struct direntry) ||
1379 (offset & (sizeof(struct direntry) - 1)))
1380 return (EINVAL);
1381 lost = uio->uio_resid - count;
1382 uio->uio_resid = count;
1383 uio_off = uio->uio_offset;
1384
1385
1386 /* Allocate a temporary dirent buffer. */
1387 dirbuf = malloc(sizeof(struct dirent), M_MSDOSFSTMP, M_WAITOK | M_ZERO);
1388
1389 if (ap->a_ncookies) {
1390 nc = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
1391 cookies = malloc(nc * sizeof (off_t), M_TEMP, M_WAITOK);
1392 *ap->a_cookies = cookies;
1393 }
1394
1395 dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
1396
1397 /*
1398 * If they are reading from the root directory then, we simulate
1399 * the . and .. entries since these don't exist in the root
1400 * directory. We also set the offset bias to make up for having to
1401 * simulate these entries. By this I mean that at file offset 64 we
1402 * read the first entry in the root directory that lives on disk.
1403 */
1404 if (dep->de_StartCluster == MSDOSFSROOT
1405 || (FAT32(pmp) && dep->de_StartCluster == pmp->pm_rootdirblk)) {
1406 #if 0
1407 printf("msdosfs_readdir(): going after . or .. in root dir, "
1408 "offset %" PRIu64 "\n", offset);
1409 #endif
1410 bias = 2 * sizeof(struct direntry);
1411 if (offset < bias) {
1412 for (n = (int)offset / sizeof(struct direntry);
1413 n < 2; n++) {
1414 if (FAT32(pmp))
1415 dirbuf->d_fileno = cntobn(pmp,
1416 (ino_t)pmp->pm_rootdirblk)
1417 * dirsperblk;
1418 else
1419 dirbuf->d_fileno = 1;
1420 dirbuf->d_type = DT_DIR;
1421 switch (n) {
1422 case 0:
1423 dirbuf->d_namlen = 1;
1424 strlcpy(dirbuf->d_name, ".",
1425 sizeof(dirbuf->d_name));
1426 break;
1427 case 1:
1428 dirbuf->d_namlen = 2;
1429 strlcpy(dirbuf->d_name, "..",
1430 sizeof(dirbuf->d_name));
1431 break;
1432 }
1433 dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
1434 if (uio->uio_resid < dirbuf->d_reclen)
1435 goto out;
1436 error = uiomove(dirbuf, dirbuf->d_reclen, uio);
1437 if (error)
1438 goto out;
1439 offset += sizeof(struct direntry);
1440 uio_off = offset;
1441 if (cookies) {
1442 *cookies++ = offset;
1443 ncookies++;
1444 if (ncookies >= nc)
1445 goto out;
1446 }
1447 }
1448 }
1449 }
1450
1451 while (uio->uio_resid > 0) {
1452 lbn = de_cluster(pmp, offset - bias);
1453 on = (offset - bias) & pmp->pm_crbomask;
1454 n = MIN(pmp->pm_bpcluster - on, uio->uio_resid);
1455 diff = dep->de_FileSize - (offset - bias);
1456 if (diff <= 0)
1457 break;
1458 n = MIN(n, diff);
1459 if ((error = pcbmap(dep, lbn, &bn, &cn, &blsize)) != 0)
1460 break;
1461 error = bread(pmp->pm_devvp, de_bn2kb(pmp, bn), blsize,
1462 0, &bp);
1463 if (error) {
1464 goto bad;
1465 }
1466 n = MIN(n, blsize - bp->b_resid);
1467
1468 /*
1469 * Convert from dos directory entries to fs-independent
1470 * directory entries.
1471 */
1472 for (dentp = (struct direntry *)((char *)bp->b_data + on);
1473 (char *)dentp < (char *)bp->b_data + on + n;
1474 dentp++, offset += sizeof(struct direntry)) {
1475 #if 0
1476
1477 printf("rd: dentp %08x prev %08x crnt %08x deName %02x attr %02x\n",
1478 dentp, prev, crnt, dentp->deName[0], dentp->deAttributes);
1479 #endif
1480 /*
1481 * If this is an unused entry, we can stop.
1482 */
1483 if (dentp->deName[0] == SLOT_EMPTY) {
1484 brelse(bp, 0);
1485 goto out;
1486 }
1487 /*
1488 * Skip deleted entries.
1489 */
1490 if (dentp->deName[0] == SLOT_DELETED) {
1491 chksum = -1;
1492 continue;
1493 }
1494
1495 /*
1496 * Handle Win95 long directory entries
1497 */
1498 if (dentp->deAttributes == ATTR_WIN95) {
1499 if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
1500 continue;
1501 chksum = win2unixfn((struct winentry *)dentp,
1502 dirbuf, chksum, &namlen,
1503 pmp->pm_flags & MSDOSFSMNT_UTF8);
1504 if (chksum != -1)
1505 dirbuf->d_namlen = namlen;
1506 continue;
1507 }
1508
1509 /*
1510 * Skip volume labels
1511 */
1512 if (dentp->deAttributes & ATTR_VOLUME) {
1513 chksum = -1;
1514 continue;
1515 }
1516 /*
1517 * This computation of d_fileno must match
1518 * the computation of va_fileid in
1519 * msdosfs_getattr.
1520 */
1521 if (dentp->deAttributes & ATTR_DIRECTORY) {
1522 fileno = getushort(dentp->deStartCluster);
1523 if (FAT32(pmp))
1524 fileno |= ((ino_t)getushort(dentp->deHighClust)) << 16;
1525 /* if this is the root directory */
1526 if (fileno == MSDOSFSROOT)
1527 if (FAT32(pmp))
1528 fileno = cntobn(pmp,
1529 (ino_t)pmp->pm_rootdirblk)
1530 * dirsperblk;
1531 else
1532 fileno = 1;
1533 else
1534 fileno = cntobn(pmp, fileno) * dirsperblk;
1535 dirbuf->d_fileno = fileno;
1536 dirbuf->d_type = DT_DIR;
1537 } else {
1538 dirbuf->d_fileno =
1539 offset / sizeof(struct direntry);
1540 dirbuf->d_type = DT_REG;
1541 }
1542 if (chksum != winChksum(dentp->deName))
1543 dirbuf->d_namlen = dos2unixfn(dentp->deName,
1544 (u_char *)dirbuf->d_name,
1545 pmp->pm_flags & MSDOSFSMNT_SHORTNAME);
1546 else
1547 dirbuf->d_name[dirbuf->d_namlen] = 0;
1548 namlen = dirbuf->d_namlen;
1549 chksum = -1;
1550 dirbuf->d_reclen = _DIRENT_SIZE(dirbuf);
1551 if (uio->uio_resid < dirbuf->d_reclen) {
1552 brelse(bp, 0);
1553 goto out;
1554 }
1555 error = uiomove(dirbuf, dirbuf->d_reclen, uio);
1556 if (error) {
1557 brelse(bp, 0);
1558 goto out;
1559 }
1560 uio_off = offset + sizeof(struct direntry);
1561 if (cookies) {
1562 *cookies++ = offset + sizeof(struct direntry);
1563 ncookies++;
1564 if (ncookies >= nc) {
1565 brelse(bp, 0);
1566 goto out;
1567 }
1568 }
1569 }
1570 brelse(bp, 0);
1571 }
1572
1573 out:
1574 uio->uio_offset = uio_off;
1575 uio->uio_resid += lost;
1576 if (dep->de_FileSize - (offset - bias) <= 0)
1577 *ap->a_eofflag = 1;
1578 else
1579 *ap->a_eofflag = 0;
1580
1581 if (ap->a_ncookies) {
1582 if (error) {
1583 free(*ap->a_cookies, M_TEMP);
1584 *ap->a_ncookies = 0;
1585 *ap->a_cookies = NULL;
1586 } else
1587 *ap->a_ncookies = ncookies;
1588 }
1589
1590 bad:
1591 free(dirbuf, M_MSDOSFSTMP);
1592 return (error);
1593 }
1594
1595 /*
1596 * vp - address of vnode file the file
1597 * bn - which cluster we are interested in mapping to a filesystem block number.
1598 * vpp - returns the vnode for the block special file holding the filesystem
1599 * containing the file of interest
1600 * bnp - address of where to return the filesystem relative block number
1601 */
1602 int
1603 msdosfs_bmap(void *v)
1604 {
1605 struct vop_bmap_args /* {
1606 struct vnode *a_vp;
1607 daddr_t a_bn;
1608 struct vnode **a_vpp;
1609 daddr_t *a_bnp;
1610 int *a_runp;
1611 } */ *ap = v;
1612 struct denode *dep = VTODE(ap->a_vp);
1613 int run, maxrun;
1614 daddr_t runbn;
1615 int status;
1616
1617 if (ap->a_vpp != NULL)
1618 *ap->a_vpp = dep->de_devvp;
1619 if (ap->a_bnp == NULL)
1620 return (0);
1621 status = pcbmap(dep, ap->a_bn, ap->a_bnp, 0, 0);
1622
1623 /*
1624 * From FreeBSD:
1625 * A little kludgy, but we loop calling pcbmap until we
1626 * reach the end of the contiguous piece, or reach MAXPHYS.
1627 * Since it reduces disk I/Os, the "wasted" CPU is put to
1628 * good use (4 to 5 fold sequential read I/O improvement on USB
1629 * drives).
1630 */
1631 if (ap->a_runp != NULL) {
1632 /* taken from ufs_bmap */
1633 maxrun = ulmin(MAXPHYS / dep->de_pmp->pm_bpcluster - 1,
1634 dep->de_pmp->pm_maxcluster - ap->a_bn);
1635 for (run = 1; run <= maxrun; run++) {
1636 if (pcbmap(dep, ap->a_bn + run, &runbn, NULL, NULL)
1637 != 0 || runbn !=
1638 *ap->a_bnp + de_cn2bn(dep->de_pmp, run))
1639 break;
1640 }
1641 *ap->a_runp = run - 1;
1642 }
1643
1644 /*
1645 * We need to scale *ap->a_bnp by sector_size/DEV_BSIZE
1646 */
1647 *ap->a_bnp = de_bn2kb(dep->de_pmp, *ap->a_bnp);
1648 return status;
1649 }
1650
1651 int
1652 msdosfs_strategy(void *v)
1653 {
1654 struct vop_strategy_args /* {
1655 struct vnode *a_vp;
1656 struct buf *a_bp;
1657 } */ *ap = v;
1658 struct vnode *vp = ap->a_vp;
1659 struct buf *bp = ap->a_bp;
1660 struct denode *dep = VTODE(bp->b_vp);
1661 int error = 0;
1662
1663 if (vp->v_type == VBLK || vp->v_type == VCHR)
1664 panic("msdosfs_strategy: spec");
1665 /*
1666 * If we don't already know the filesystem relative block number
1667 * then get it using pcbmap(). If pcbmap() returns the block
1668 * number as -1 then we've got a hole in the file. DOS filesystems
1669 * don't allow files with holes, so we shouldn't ever see this.
1670 */
1671 if (bp->b_blkno == bp->b_lblkno) {
1672 error = pcbmap(dep, de_bn2cn(dep->de_pmp, bp->b_lblkno),
1673 &bp->b_blkno, 0, 0);
1674 if (error)
1675 bp->b_blkno = -1;
1676 if (bp->b_blkno == -1)
1677 clrbuf(bp);
1678 else
1679 bp->b_blkno = de_bn2kb(dep->de_pmp, bp->b_blkno);
1680 }
1681 if (bp->b_blkno == -1) {
1682 biodone(bp);
1683 return (error);
1684 }
1685
1686 /*
1687 * Read/write the block from/to the disk that contains the desired
1688 * file block.
1689 */
1690
1691 vp = dep->de_devvp;
1692 return (VOP_STRATEGY(vp, bp));
1693 }
1694
1695 int
1696 msdosfs_print(void *v)
1697 {
1698 struct vop_print_args /* {
1699 struct vnode *vp;
1700 } */ *ap = v;
1701 struct denode *dep = VTODE(ap->a_vp);
1702
1703 printf(
1704 "tag VT_MSDOSFS, startcluster %ld, dircluster %ld, diroffset %ld ",
1705 dep->de_StartCluster, dep->de_dirclust, dep->de_diroffset);
1706 printf(" dev %llu, %llu ", (unsigned long long)major(dep->de_dev),
1707 (unsigned long long)minor(dep->de_dev));
1708 printf("\n");
1709 return (0);
1710 }
1711
1712 int
1713 msdosfs_advlock(void *v)
1714 {
1715 struct vop_advlock_args /* {
1716 struct vnode *a_vp;
1717 void *a_id;
1718 int a_op;
1719 struct flock *a_fl;
1720 int a_flags;
1721 } */ *ap = v;
1722 struct denode *dep = VTODE(ap->a_vp);
1723
1724 return lf_advlock(ap, &dep->de_lockf, dep->de_FileSize);
1725 }
1726
1727 int
1728 msdosfs_pathconf(void *v)
1729 {
1730 struct vop_pathconf_args /* {
1731 struct vnode *a_vp;
1732 int a_name;
1733 register_t *a_retval;
1734 } */ *ap = v;
1735
1736 switch (ap->a_name) {
1737 case _PC_LINK_MAX:
1738 *ap->a_retval = 1;
1739 return (0);
1740 case _PC_NAME_MAX:
1741 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_namemax;
1742 return (0);
1743 case _PC_PATH_MAX:
1744 *ap->a_retval = PATH_MAX;
1745 return (0);
1746 case _PC_CHOWN_RESTRICTED:
1747 *ap->a_retval = 1;
1748 return (0);
1749 case _PC_NO_TRUNC:
1750 *ap->a_retval = 1;
1751 return (0);
1752 case _PC_SYNC_IO:
1753 *ap->a_retval = 1;
1754 return (0);
1755 case _PC_FILESIZEBITS:
1756 *ap->a_retval = 32;
1757 return (0);
1758 default:
1759 return genfs_pathconf(ap);
1760 }
1761 /* NOTREACHED */
1762 }
1763
1764 int
1765 msdosfs_fsync(void *v)
1766 {
1767 struct vop_fsync_args /* {
1768 struct vnode *a_vp;
1769 kauth_cred_t a_cred;
1770 int a_flags;
1771 off_t offlo;
1772 off_t offhi;
1773 } */ *ap = v;
1774 struct vnode *vp = ap->a_vp;
1775 int wait;
1776 int error;
1777
1778 wait = (ap->a_flags & FSYNC_WAIT) != 0;
1779 error = vflushbuf(vp, ap->a_flags);
1780 if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0)
1781 error = msdosfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
1782
1783 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
1784 struct denode *dep = VTODE(vp);
1785 struct vnode *devvp = dep->de_devvp;
1786
1787 int l = 0;
1788 error = VOP_IOCTL(devvp, DIOCCACHESYNC, &l, FWRITE,
1789 curlwp->l_cred);
1790 }
1791
1792 return (error);
1793 }
1794
1795 void
1796 msdosfs_detimes(struct denode *dep, const struct timespec *acc,
1797 const struct timespec *mod, const struct timespec *cre, int gmtoff)
1798 {
1799 struct timespec *ts = NULL, tsb;
1800
1801 KASSERT(dep->de_flag & (DE_UPDATE | DE_CREATE | DE_ACCESS));
1802 /* XXX just call getnanotime early and use result if needed? */
1803 dep->de_flag |= DE_MODIFIED;
1804 if (dep->de_flag & DE_UPDATE) {
1805 if (mod == NULL) {
1806 getnanotime(&tsb);
1807 mod = ts = &tsb;
1808 }
1809 unix2dostime(mod, gmtoff, &dep->de_MDate, &dep->de_MTime, NULL);
1810 dep->de_Attributes |= ATTR_ARCHIVE;
1811 }
1812 if ((dep->de_pmp->pm_flags & MSDOSFSMNT_NOWIN95) == 0) {
1813 if (dep->de_flag & DE_ACCESS) {
1814 if (acc == NULL)
1815 acc = ts == NULL ?
1816 (getnanotime(&tsb), ts = &tsb) : ts;
1817 unix2dostime(acc, gmtoff, &dep->de_ADate, NULL, NULL);
1818 }
1819 if (dep->de_flag & DE_CREATE) {
1820 if (cre == NULL)
1821 cre = ts == NULL ?
1822 (getnanotime(&tsb), ts = &tsb) : ts;
1823 unix2dostime(cre, gmtoff, &dep->de_CDate,
1824 &dep->de_CTime, &dep->de_CHun);
1825 }
1826 }
1827
1828 dep->de_flag &= ~(DE_UPDATE | DE_CREATE | DE_ACCESS);
1829 }
1830
1831 /* Global vfs data structures for msdosfs */
1832 int (**msdosfs_vnodeop_p)(void *);
1833 const struct vnodeopv_entry_desc msdosfs_vnodeop_entries[] = {
1834 { &vop_default_desc, vn_default_error },
1835 { &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
1836 { &vop_lookup_desc, msdosfs_lookup }, /* lookup */
1837 { &vop_create_desc, msdosfs_create }, /* create */
1838 { &vop_mknod_desc, genfs_eopnotsupp }, /* mknod */
1839 { &vop_open_desc, genfs_nullop }, /* open */
1840 { &vop_close_desc, msdosfs_close }, /* close */
1841 { &vop_access_desc, msdosfs_access }, /* access */
1842 { &vop_accessx_desc, genfs_accessx }, /* accessx */
1843 { &vop_getattr_desc, msdosfs_getattr }, /* getattr */
1844 { &vop_setattr_desc, msdosfs_setattr }, /* setattr */
1845 { &vop_read_desc, msdosfs_read }, /* read */
1846 { &vop_write_desc, msdosfs_write }, /* write */
1847 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
1848 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
1849 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
1850 { &vop_ioctl_desc, genfs_enoioctl }, /* ioctl */
1851 { &vop_poll_desc, genfs_poll }, /* poll */
1852 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
1853 { &vop_revoke_desc, genfs_revoke }, /* revoke */
1854 { &vop_mmap_desc, genfs_mmap }, /* mmap */
1855 { &vop_fsync_desc, msdosfs_fsync }, /* fsync */
1856 { &vop_seek_desc, genfs_seek }, /* seek */
1857 { &vop_remove_desc, msdosfs_remove }, /* remove */
1858 { &vop_link_desc, genfs_eopnotsupp }, /* link */
1859 { &vop_rename_desc, msdosfs_rename }, /* rename */
1860 { &vop_mkdir_desc, msdosfs_mkdir }, /* mkdir */
1861 { &vop_rmdir_desc, msdosfs_rmdir }, /* rmdir */
1862 { &vop_symlink_desc, genfs_eopnotsupp }, /* symlink */
1863 { &vop_readdir_desc, msdosfs_readdir }, /* readdir */
1864 { &vop_readlink_desc, genfs_einval }, /* readlink */
1865 { &vop_abortop_desc, genfs_abortop }, /* abortop */
1866 { &vop_inactive_desc, msdosfs_inactive }, /* inactive */
1867 { &vop_reclaim_desc, msdosfs_reclaim }, /* reclaim */
1868 { &vop_lock_desc, genfs_lock }, /* lock */
1869 { &vop_unlock_desc, genfs_unlock }, /* unlock */
1870 { &vop_bmap_desc, msdosfs_bmap }, /* bmap */
1871 { &vop_strategy_desc, msdosfs_strategy }, /* strategy */
1872 { &vop_print_desc, msdosfs_print }, /* print */
1873 { &vop_islocked_desc, genfs_islocked }, /* islocked */
1874 { &vop_pathconf_desc, msdosfs_pathconf }, /* pathconf */
1875 { &vop_advlock_desc, msdosfs_advlock }, /* advlock */
1876 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
1877 { &vop_getpages_desc, genfs_getpages }, /* getpages */
1878 { &vop_putpages_desc, genfs_putpages }, /* putpages */
1879 { NULL, NULL }
1880 };
1881 const struct vnodeopv_desc msdosfs_vnodeop_opv_desc =
1882 { &msdosfs_vnodeop_p, msdosfs_vnodeop_entries };
1883